如何根据 Fipy 中的位置和时间表示源项 python

How to represent source terms depending on position and time in Fipy python

我有以下偏微分方程

我想知道如何在 Fipy 中表示源术语 python。我尝试了以下

from fipy import *
nx = 50
ny = 1
dx = dy = 0.025                     # grid spacing
L = dx * nx
mesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny)
phi = CellVariable(name="solution variable", mesh=mesh, value=0.)
convCoeff = ((10.,), (10.,))
Gamma = 1.
eqX = TransientTerm() == DiffusionTerm(coeff=Gamma) - ConvectionTerm(coeff=convCoeff) + t*numerix(exp(x*y))
valueTopLeft = 0
valueBottomRight = 1
X, Y = mesh.faceCenters
facesTopLeft = ((mesh.facesLeft & (Y > L / 2)) | (mesh.facesTop & (X < L / 2)))
facesBottomRight = ((mesh.facesRight & (Y < L / 2)) |
                    (mesh.facesBottom & (X > L / 2)))
#
phi.constrain(valueTopLeft, facesTopLeft)
phi.constrain(valueBottomRight, facesBottomRight)
timeStepDuration = 10 * 0.9 * dx ** 2 / (2 * D)
steps = 10
results = []
for step in range(steps):
    eqX.solve(var=phi, dt=timeStepDuration)
    results.append(phi.value)

这不起作用。根据 fipy 手册,我看到他们说源术语以它们出现的方式表示,并且建议使用 numerix 模块而不是 numpy 等其他模块。我不知道这段代码中没有。谢谢

我发现代码有很多问题

  • t 未定义
  • 在源代码中使用 numerixexp 是荒谬的并且给出了错误
  • D 未定义

要使源时间依赖,t 需要是 Variable 以便在时间更改时重新评估源。时间变量也需要在每一步更新。

这是实际运行的代码的更正版本。

from fipy import *
nx = 50
ny = 1
dx = dy = 0.025                     # grid spacing
L = dx * nx
mesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny)
phi = CellVariable(name="solution variable", mesh=mesh, value=0.)
t = Variable(0.)
x = mesh.x
y = mesh.y
convCoeff = ((10.,), (10.,))
Gamma = 1.
D = Gamma
eqX = TransientTerm() == DiffusionTerm(coeff=Gamma) - ConvectionTerm(coeff=convCoeff) + t*numerix.exp(x*y)
valueTopLeft = 0
valueBottomRight = 1
X, Y = mesh.faceCenters
facesTopLeft = ((mesh.facesLeft & (Y > L / 2)) | (mesh.facesTop & (X < L / 2)))
facesBottomRight = ((mesh.facesRight & (Y < L / 2)) |
                    (mesh.facesBottom & (X > L / 2)))
#
phi.constrain(valueTopLeft, facesTopLeft)
phi.constrain(valueBottomRight, facesBottomRight)
timeStepDuration = 10 * 0.9 * dx ** 2 / (2 * D)
steps = 10
results = []
for step in range(steps):
    eqX.solve(var=phi, dt=timeStepDuration)
    results.append(phi.value)
    t.setValue(t.value + timeStepDuration)
    print('step:', step)