Gibberish

Posts tagged ‘Python’

Matplotlib color chart

Here is the color chart for using inside matplotlib

 

lfzum

Image gotten from a discussion on stack: https://stackoverflow.com/questions/22408237/named-colors-in-matplotlib

Advertisements

Make python wait for you by sleep function

import time
time.sleep(1)

will make python wait for you 1 second before processing next line. To make python wait for you with another value of time, change 1 to whatever you need, remember the unit here is second.
Very useful when you have to check your code, see the temporary result and so on.

Happy coding!

Chạy python qua CMD

Trong window, để bật python và ipython thông qua cửa sổ cmd, cần phải sửa mục path của hệ thống trước, sau đó, vào trong CMD, có thể chạy python bằng lệnh tắt python và enter. Quá trình làm như sau:

1. Control panel –> system –> Advanced .. –> Environment …

2. Tại đây trong cửa sổ PATH, thêm path của python vào. Ví dụ trong trường hợp python 2.7 được cài trong thư mục C:\Python27\python.exe thì giá trị path cần thêm vào là C:\Python27, để phân biệt path này với các path có sẵn, chỉ cần nhét dấu chấm phẩy (;) giữa các path, sau đó save lại. Có thể cần phải khởi động lại để thay đổi có hiệu lực.

Để test tính năng này có thể vào cửa sổ RUN, gõ cmd, sau đó bấm python để kiểm tra.

Python 3 làm tương tự.

Definition: Unbalanced Force

Compute the ratio of mean (or maximum, if use MaxForce) summary force on bodies and maximum force magnitude on interactions. For perfectly static equilibrium, summary force on all bodies is zero (since forces from interactions cancel out and induce no acceleration or particles); this ratio will tend to zero as simulation stabilizes, though zero is never reached because of finite precision computation. Sufficiently small value can be e.g. 1e-2 or smaller, depending on how much  equilibrium it should be.

-Yade document-

PSD generator

This is kinda suck but I still have to work with, hope someday I’ll find the passion. Trying my best.

# encoding: utf-8
#
# demonstrate how to generate sphere packing based on arbitrary PSD (particle size distribution)
# show the difference between size-based and mass-based (? volume-based in our case) PSD
#
import matplotlib; matplotlib.rc('axes',grid=True)
from yade import pack
import pylab
# PSD given as points of piecewise-linear function
psdSizes,psdCumm=[.02,0.04,0.045,.05,.06,.08,.12],[0.,0.1,0.3,0.3,.3,.7,1.]
pylab.plot(psdSizes,psdCumm,label='precribed mass PSD')
sp0=pack.SpherePack();
sp0.makeCloud((0,0,0),(1,1,1),psdSizes=psdSizes,psdCumm=psdCumm,distributeMass=True)
sp1=pack.SpherePack();
sp1.makeCloud((0,0,0),(1,1,1),psdSizes=psdSizes,psdCumm=psdCumm,distributeMass=True,num=5000)
sp2=pack.SpherePack();
sp2.makeCloud((0,0,0),(1,1,1),psdSizes=psdSizes,psdCumm=psdCumm,distributeMass=True,num=20000)
pylab.semilogx(*sp0.psd(bins=30,mass=True),label='Mass PSD of (free) %d random spheres'%len(sp0))
pylab.semilogx(*sp1.psd(bins=30,mass=True),label='Mass PSD of (imposed) %d random spheres'%len(sp1))
pylab.semilogx(*sp2.psd(bins=30,mass=True),label='Mass PSD of (imposed) %d random spheres (scaled down)'%len(sp2))

pylab.legend()

# uniform distribution of size (sp3) and of mass (sp4)
sp3=pack.SpherePack(); sp3.makeCloud((0,0,0),(1,1,1),rMean=0.03,rRelFuzz=2/3.,distributeMass=False);
sp4=pack.SpherePack(); sp4.makeCloud((0,0,0),(1,1,1),rMean=0.03,rRelFuzz=2/3.,distributeMass=True);
pylab.figure()
pylab.plot(*(sp3.psd(mass=True)+('g',)+sp4.psd(mass=True)+('r',)))
pylab.legend(['Mass PSD of size-uniform distribution','Mass PSD of mass-uniform distribution'])

pylab.figure()
pylab.plot(*(sp3.psd(mass=False)+('g',)+sp4.psd(mass=False)+('r',)))
pylab.legend(['Size PSD of size-uniform distribution','Size PSD of mass-uniform distribution'])
pylab.show()

pylab.show()

Tag Cloud

%d bloggers like this: