python代码段有什么用_python程序当中总有着一段是什么作用?

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值