我正在使用 fipy 解决计算域中孤立区域的扩散问题。见示意图。下图: 孤立的 BC 之间没有通量,而周期性 BC 存在通量
该问题在 Fipy 下建模,在fipy.FaceVariable
@Daniel Wheeler 的帮助下定义了可变系数。
但是,计算速度无法满足我的命令,这比使用有限差分法的 cython 代码慢得多。如果我想加快 fipy 计算,我该怎么办?这是我的演示代码:
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)