from __future__ import unicode_literals from builtins import range from fipy import input from fipy import CellVariable, Tri2D, TransientTerm, ExplicitDiffusionTerm, DefaultSolver, Viewer from fipy.tools import numerix dx = 1. dy = 1. nx = 10 ny = 1 valueLeft = 0. valueRight = 1. timeStepDuration = 0.02 mesh = Tri2D(dx, dy, nx, ny) var = CellVariable(name="concentration", mesh=mesh, value=valueLeft) eq = TransientTerm() == ExplicitDiffusionTerm() solver = DefaultSolver(tolerance=1e-6, iterations=1000) var.constrain(valueLeft, mesh.facesLeft) var.constrain(valueRight, mesh.facesRight) answer = numerix.array([ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.58508452e-07, 6.84325019e-04, 7.05111362e-02, 7.81376523e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
Finally the analytical and numerical results are compared with a tolerance of `1e-10`. >>> print var.allclose(analyticalArray) 1 """ __docformat__ = 'restructuredtext' from fipy import Tri2D, CellVariable, DiffusionTerm, Viewer nx = 50 dx = 1. mesh = Tri2D(dx = dx, nx = nx) valueLeft = 0. valueRight = 1. var = CellVariable(name = "solution-variable", mesh = mesh, value = valueLeft) var.constrain(valueLeft, mesh.facesLeft) var.constrain(valueRight, mesh.facesRight) if __name__ == '__main__': DiffusionTerm().solve(var) viewer = Viewer(vars=var) viewer.plot() x = mesh.cellCenters[0] Lx = nx * dx analyticalArray = valueLeft + (valueRight - valueLeft) * x / Lx
>>> Lx = nx * dx >>> x = mesh.cellCenters[0] >>> analyticalArray = valueLeft + (valueRight - valueLeft) * x / Lx >>> print var.allclose(analyticalArray, atol = 1e-7) 1 """ from fipy import CellVariable, Tri2D, DiffusionTerm, Viewer nx = 50 ny = 50 dx = 1. valueLeft = 0. valueRight = 1. mesh = Tri2D(dx=dx, nx=nx, ny=ny) var = CellVariable(name="solution variable", mesh=mesh, value=valueLeft) var.constrain(valueLeft, mesh.facesLeft) var.constrain(valueRight, mesh.facesRight) if __name__ == '__main__': DiffusionTerm().solve(var) viewer = Viewer(vars=var) viewer.plot() raw_input("finished")