如何在fipypython中根据位置和时间表示源项

2024-09-27 21:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的偏微分方程

enter image description here

我想知道如何用fipypython表示源代码项。我试过以下方法

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等其他模块。我不知道我在这个代码中丢失了什么。谢谢


Tags: 源代码valuenxphimeshgammadyny
1条回答
网友
1楼 · 发布于 2024-09-27 21:29:10

我发现代码有很多问题

  • 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)

相关问题 更多 >

    热门问题