我正在使用fipy来解决计算域中孤立区域的扩散问题。参见示意图。下面: where there is no flux between isolated BC, and flux exists at periodic BC
问题在Fipy下建模,fipy.FaceVariable
在@Daniel Wheeler的帮助下定义了varing coeff。
然而,计算速度不能满足我的命令,这比使用有限差分法的cython代码要慢得多。如果我想加快计算速度,我该怎么办? 这是我的演示代码:
from pylab import *
from numpy import *
import fipy
from scipy.spatial import Delaunay
from fipy.variables.cellVariable import CellVariable
from fipy.terms.transientTerm import TransientTerm
from fipy.terms.diffusionTerm import DiffusionTerm
from fipy.viewers import Viewer
import time
nx, ny = 100.0, 100.0
dx, dy = 1.0, 1.0
mesh = fipy.PeriodicGrid2D(dx=dx, dy=dy, nx=nx, ny=ny)
x, y = mesh.cellCenters
D1 = 10.0
D2 = 1.0
X, Y = mesh.faceCenters
print x
phi = CellVariable(name="Carbon", mesh=mesh, value=0.0)
coeff = fipy.FaceVariable(mesh=mesh, value=10.0)
pos1 = X == 50.0
pos2 = Y == 50.0
pos = pos1+ pos2
coeff[pos] = 0
posA1 = logical_and(x >= 20.0, x <= 30.0)
posA2 = logical_and(y >= 20.0, y <= 30.0)
posA = logical_and(posA1, posA2)
posB1 = logical_and(x >= 20.0, x <= 30.0)
posB2 = logical_and(y >= 70.0, y <= 80.0)
posB = logical_and(posB1, posB2)
posC1 = logical_and(x >= 70.0, x <= 80.0)
posC2 = logical_and(y >= 20.0, y <= 30.0)
posC = logical_and(posC1, posC2)
posD1 = logical_and(x >= 70.0, x <= 80.0)
posD2 = logical_and(y >= 70.0, y <= 80.0)
posD = logical_and(posD1, posD2)
phi[posA] = 10
phi[posB] = 20
phi[posC] = 100
phi[posD] = 30
eq = TransientTerm() == DiffusionTerm(coeff=coeff)
timeStepDuration = 10 * 0.9 * 1.0**2 / (2 * 1.0)
steps = 100
for step in range(steps):
eq.solve(var=phi, dt=timeStepDuration)
viewer = Viewer(vars=phi)
viewer.plot()
time.sleep(60)
答案 0 :(得分:0)
如果Cython代码是显式的,那么它将具有时间步长限制。 FiPy是隐含的,因此稳定性没有时间步长限制。随着时间步长的增加,可能存在准确性问题。如果上述问题中的时间步长增加10倍并且运行10步(而不是100步),则解决方案会稍微改变,但在绘制结果时看起来相似。使用FiPy的实用性和好处取决于问题的性质以及是否需要高精度或仅仅是工程解决方案。
另外,请注意,FiPy中的第一个步骤非常慢,因为它正在构建变量关系和缓存数据。例如,在上面的代码中,第一个时间步长需要大约1秒,而后续时间步长需要大约0.1秒。在与FiPy进行时间比较时,值得注意。