我试图通过一个例子来理解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))发布于 2016-03-23 15:18:08
正如在FiPy邮件列表上讨论的那样,由于缺少高阶对流格式,FiPy不太适合只处理PDE(无扩散,纯双曲)。对于这类问题,最好使用CLAWPACK。
FiPy确实有一个可以帮助解决这个问题的二阶方案,VanLeerConvectionTerm,见示例。
如果在上述问题中使用VanLeerConvectionTerm,那么它在保持冲击方面做得更好。
import numpy as np
import fipy
# Generate mesh
nx = 20
dx = 2*np.pi/nx
mesh = fipy.PeriodicGrid1D(nx=nx, dx=dx)
# Generate solution object with initial discontinuity
phi = fipy.CellVariable(name="solution variable", mesh=mesh)
phiAnalytical = fipy.CellVariable(name="analytical value", mesh=mesh)
phi.setValue(1.)
phi.setValue(0., where=mesh.x > 1.)
# Define the pde
D = [[-1.]]
eq = fipy.TransientTerm() == fipy.VanLeerConvectionTerm(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))
viewer = fipy.Viewer(phi)
for step in range(steps):
eq.solve(var=phi, dt=dt)
viewer.plot()
raw_input('stopped')
print(phi.allclose(phiAnalytical, atol=1e-1))https://stackoverflow.com/questions/36177369
复制相似问题