image_output_2.png image_output_1.png
from fipy import Variable, FaceVariable, CellVariable, Grid1D, ExplicitDiffusionTerm, TransientTerm, DiffusionTerm, Viewer

from fipy.tools import numerix

nx = 50
Lx = 1.0
dx = Lx / nx
mesh = Grid1D(Lx=Lx, dx=dx)
x = mesh.cellCenters[0]
T = CellVariable(name='solution variable', mesh=mesh, value=0.0)
T.setValue(0)
a = 1.0
valueLeft = 1
valueRight = 0
T.constrain(valueRight, mesh.facesRight)
T.constrain(valueLeft, mesh.facesLeft)

timeStepDuration = 0.9 * dx ** 2 / (2 * a)
steps = 100
t_final = timeStepDuration * steps
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=a)
T_Analytical = CellVariable(name='analytical value', mesh=mesh)
T_Analytical.setValue(1 - erf(x / (2 * numerix.sqrt(a * t_final))))

viewer = Viewer(vars=(T, T_Analytical))
for step in range(steps):
    eqX.solve(var=T, dt=timeStepDuration)
    if __name__ == '__main__':
        viewer.plot('1Dtransient_%0d.png' % step)