I am using fipy to solve a diffusion problem with isolated regions in a calculation domain. See the schematic pic. below: where there is no flux between isolated BC, and flux exists at periodic BC
The problem is modeled under Fipy with varing coeff defined by fipy.FaceVariable
with the help from @Daniel Wheeler.
However, the calculation speed could not meet my command which is much slower than a cython code with Finite Difference Method. What can I do if I want to speed fipy calculation? Here is my demo code:
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)