FiPy简单对流

时间:2016-03-23 11:48:24

标签: numerical-methods pde fipy conservation-laws

我试图通过一个例子来理解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))

1 个答案:

答案 0 :(得分:1)

正如FiPy mailing list所述,FiPy并不擅长处理仅有PDE(没有扩散,纯双曲线)的对流,因为它缺少高阶对流方案。最好将CLAWPACK用于此类问题。

FiPy确实有一个可能有助于解决此问题的二阶方案,即VanLeerConvectionTerm,see an example

如果在上述问题中使用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))