我试图通过一个示例来了解 FiPy 的工作原理,特别是我想解决以下具有周期性边界的简单对流方程:
$$\partial_t u + \partial_x u = 0$$
如果初始数据由 $u(x, 0) = F(x)$ 给出,则解析解为 $u(x, t) = F(x - t)$。我确实得到了解决方案,但它不正确。
我错过了什么?有没有比文档更好的资源来理解 FiPy?非常稀疏...
这是我的尝试
from fipy import *
import numpy as np
# Generate mesh
nx = 20
dx = 2*np.pi/nx
mesh = PeriodicGrid1D(nx=nx, dx=dx)
# Generate solution object with initial discontinuity
phi = CellVariable(name="solution variable", mesh=mesh)
phiAnalytical = CellVariable(name="analytical value", mesh=mesh)
phi.setValue(1.)
phi.setValue(0., where=x > 1.)
# Define the pde
D = [[-1.]]
eq = TransientTerm() == ConvectionTerm(coeff=D)
# Set discretization so analytical solution is exactly one cell translation
dt = 0.01*dx
steps = 2*int(dx/dt)
# Set the analytical value at the end of simulation
phiAnalytical.setValue(np.roll(phi.value, 1))
for step in range(steps):
eq.solve(var=phi, dt=dt)
print(phi.allclose(phiAnalytical, atol=1e-1))