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))))

nx = 50
Lx = 1.0
dx = Lx / nx
mesh = Grid1D(Lx=Lx, dx=dx)
phi2 = CellVariable(name='solution variable', mesh=mesh)
phi2.setValue(0)
x = mesh.cellCenters[0]
time = Variable()
D = 1.0

valueLeft = 1 + 0.5 * (1 + numerix.sin(0.5 * time))
fluxRight = -0.5
phi2.constrain(valueLeft, mesh.facesLeft)
phi2.faceGrad.constrain([fluxRight], mesh.facesRight)
eqI = TransientTerm() == DiffusionTerm(coeff=D)

phiAnalytical = CellVariable(name='analytical value', mesh=mesh)
viewer2 = Viewer(vars=phi2, datamin=0, datamax=3)
dt = 0.5
while time() < 50:
    time.setValue(time() + dt)
    eqI.solve(var=phi2, dt=dt)
    if __name__ == '__main__':
        viewer2.plot()