我正在寻找使用 FiPy 求解扩散方程并阅读了他们的一些文档,但似乎找不到任何与编写包含作为自变量(即空间)函数的附加项的扩散项相关的内容。我发现的最接近的东西是在常见问题解答中,他们建议将附加条款重写为ConvectionTerm
. 但是,我认为这仅适用于附加项是解变量而不是自变量的函数的情况。例如,我正在尝试使用以下扩散项求解一维扩散方程(其中导数与自变量 x 相关,y 是解变量):
D * sin(x) * Div_x {sin(x) * Grad_x {y}}
我觉得这是一个非常简单的表达式,但是我找不到如何用 FiPy 表示法来表达它。任何帮助将不胜感激!
确切的代码:
from fipy import Variable,FaceVariable,CellVariable,Grid1D,ImplicitSourceTerm,TransientTerm,DiffusionTerm,Viewer,ConvectionTerm
from fipy.tools import numerix
D = 1
c0 = 1
ka = 1
r0 = 1
nx = 100
dx = 2*math.pi/100
mesh = Grid1D(nx=nx, dx=dx)
conc = CellVariable(name="concentration", mesh=mesh, value=0.) # This is the "phi" in the docs
valueLeft = c0
valueRight = 0
conc.constrain(valueRight, mesh.facesRight)
conc.constrain(valueLeft, mesh.facesLeft)
timeStepDuration = 0.9 * dx**2 / (2 * D)
steps = 100
show_per_steps = 50
A = 1 / (r0**2 * numerix.sin(mesh.x)[0])
dA = -(numerix.cos(mesh.x)[0])/(r0**2 * numerix.sin(mesh.x)[0]**2)
dsindA = (numerix.cos(mesh.x)[0])**3/(numerix.sin(mesh.x)[0])**2
eqX = TransientTerm() + ImplicitSourceTerm(ka) == DiffusionTerm(D*A*numerix.sin(mesh.x)[0]) - ConvectionTerm(D*dA*numerix.cos(mesh.x)[0])+ D*conc*dsindA
from builtins import range
for step in range(steps):
eqX.solve(var=conc, dt=timeStepDuration)
if __name__ == '__main__' and step % show_per_steps == 0:
viewer = Viewer(vars=(conc), datamin=0., datamax=c0)
viewer.plot()