Traceback (most recent call last):
File "C:\Users\zhangjq058\workspace\22222222\src\222.py", line 20, in
x, y = meshgrid(fftfreq(th.shape, dx), fftfreq(th.shape, dx))
File "C:\Python27\lib\site-packages\numpy\fft\helper.py", line 153, in fftfreq
assert isinstance(n,types.IntType) or isinstance(n, integer)
AssertionError
我的code如下:
from pylab import *
from numpy import *
N = 100 #lattice points per axis
dt = 1 #time step
dx = 1 #lattice spacing
t = arange(0, 10000*dt, dt) #time
a = 1 #cofficient
epsilon = 100 #cofficient
M = 1.0 #cofficient
every = 100 #dump an image every
phi_0 = 0.5 #initial mean value of the order parameter
noise = 0.1 #initial amplitude of thermal fluctuations in the order parameter
th = phi_0*ones((N, N)) + noise*(rand(N, N) - 0.5) #initial condition
x, y = meshgrid(fftfreq(th.shape, dx), fftfreq(th.shape, dx))
k2 = (x*x + y*y) #k is a victor in the Fourier space, k2=x^2+y^2
g = lambda th, a: 4*a*th*(1-th)*(1-2*th) #function g
def update(th, dt, a, k2):
return ifft2((fft2(th)-dt*M*k2*fft2(g(th,a)))/(1+2*epsilon*M*dt*k2**2))
for i in range(size(t)):
print t
if mod(i, every)==0:
imshow(abs(th), vmin=0.0, vmax=1.0)
colorbar()
savefig('t'+str(i/every).zfill(3)+'.png', dpi=100)
clf()
th=update(th, dt, a, k2)
Originally posted by wpwupingwp at 2014-05-21 19:59:03
x, y = meshgrid(fftfreq(th.shape, dx), fftfreq(th.shape, dx))
這行
fftfreq要求參數為整型,你的程序給的是小數了 類型錯誤
我现在把程序修改成如下:
from pylab import *
from numpy import *
N = 256 #lattice points per axis
dt = 1 #time step
dx = 1 #lattice spacing
t = arange(0, 1000000*dt, dt) #time
a = 1 #cofficient
epsilon = 100 #cofficient
M = 1.0 #cofficient
C11 = 30
C12 = 5
C44 = 7.5
rou = (C11-C12-2*C44)/C44
eta = 0.08
every = 100 #dump an image every
phi_0 = 0.5 #initial mean value of the order parameter
noise = 0.1 #initial amplitude of thermal fluctuations in the order parameter
th = phi_0*ones((N, N)) + noise*(rand(N, N) - 0.5) #initial condition
x, y = meshgrid(fftfreq(int(th.shape), dx), fftfreq(int(th.shape), dx))
k2 = (x*x + y*y) #k is a victor in the Fourier space, k2=x^2+y^2
nx2 = (x*x)/k2
ny2 = (y*y)/k2
#B = eta**2*(C11+2*C12)*(3-(C11+2*C12)*(1+2*rou*nx2*ny2)/(C11+rou*(C11+C12)*nx2*ny2))
B = lambda eta, rou, C11, C12, C44, nx2, ny2: eta**2*(C11+2*C12)*(3-(C11+2*C12)*(1+2*rou*nx2*ny2)/(C11+rou*(C11+C12)*nx2*ny2))
g = lambda th, a: 4*a*th*(1-th)*(1-2*th) #function g
def update(th, dt, a, k2):
return ifft2(((1-B(eta, rou, C11, C12, C44, nx2, ny2)*k2*dt*M)*fft2(th)-dt*M*k2*fft2(g(th,a)))/(1+2*epsilon*M*dt*k2**2))
for i in range(size(t)):
print t
if mod(i, every)==0:
imshow(abs(th), vmin=0.0, vmax=1.0)
colorbar()
show()
#savefig('t'+str(i/every).zfill(3)+'.png', dpi=100)
clf()
th=update(th, dt, a, k2)
但是又出现了新的错误:
C:\Users\zhangjq058\workspace\22222222\src\222.py:25: RuntimeWarning: invalid value encountered in divide
nx2 = (x*x)/k2
C:\Users\zhangjq058\workspace\22222222\src\222.py:26: RuntimeWarning: invalid value encountered in divide
ny2 = (y*y)/k2
请指教,谢谢!
k2 = (x*x + y*y) #k is a victor in the Fourier space, k2=x^2+y^2
這行後面打印一下k2的值 看看有沒有問題
我这边还有很多的资料和免费的课程,想学习的加q群301056069

被折叠的 条评论
为什么被折叠?



