0

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)
  • Some details about the Cython code might be handy, like is it explicit or implicit. Also, more details about how long steps in FiPy take compared to the Cython code. – wd15 Dec 02 '16 at 15:53

1 Answers1

0

If the Cython code is explicit, then it will have a time step restriction. FiPy is implicit so there is no time step restriction due to stability. There may be issues with accuracy as the time step size is increased. If the time step size in the above problem is increased 10 fold and 10 steps are run (instead of 100), the solution changes a little, but looks similar when just plotting the results. The utility and benefit of using FiPy depends on the nature of the problem and whether high accuracy is required or just an engineering solution.

Also, note that the first time step in FiPy is quite slow as it is building the variable relationships and caching data. For example in the above code, the first time step takes ~1s, while the subsequent time steps take ~0.1s for me. This is worth noting when doing time comparisons with FiPy.

wd15
  • 1,068
  • 5
  • 8