0

我有以下偏微分方程

在此处输入图像描述

我想知道如何在 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 等其他模块。我不知道这段代码中缺少。谢谢

4

1 回答 1

3

我发现代码有很多问题

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

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

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

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)
于 2019-09-18T14:39:08.983 回答