我想设置 fipy 来求解具有正弦边界的一维扩散对流方程。
我最终得到以下代码:
from fipy import *
import numpy as np
import matplotlib.pylab as plt
def boundary(t):
return 1 + 0.1 * np.sin(6*np.pi*t)
nx = 50
dx = 1./nx
mesh = Grid1D(nx=nx, dx=dx)
n_model = CellVariable(name="density",mesh=mesh,value=1., hasOld=True)
D_model = CellVariable(name="D",mesh=mesh,value=mesh.x[::-1]*5.+3)
v_model = FaceVariable(name="v",mesh=mesh,value=1. )
v_model = (-1*mesh.x) * [[1.]]
n_model.constrain(boundary(0.), mesh.facesRight)
equation = (TransientTerm(var=n_model) == DiffusionTerm(coeff=D_model,var=n_model) \
+ ExponentialConvectionTerm(coeff=v_model,var=n_model))
timeStepDuration = 0.9 * dx**2 / (2 * 1) * 1e2
time_length = 2
steps = np.int(time_length/timeStepDuration)
t = 0
n_out = np.zeros((steps,nx))
import time
t1 = time.time()
for step in xrange(steps):
t += timeStepDuration
n_model.updateOld()
n_out[step] = n_model.globalValue
n_model.constrain(boundary(t), mesh.facesRight)
equation.solve(dt=timeStepDuration)
print "Execution time: %.3f"%(time.time()-t1)
plt.figure()
plt.imshow(n_out.T)
plt.colorbar()
plt.show()
代码运行良好,我得到了合理的结果。然而,它也很慢,大约 3.5 秒的周期。有没有更好的方法来实现这一点?或者我怎样才能加快系统速度?