Bookmark and Share FiPy: A Finite Volume PDE Solver Using Python
Version 3.0.1-dev139-ge5d2233

This Page

Contact

FiPy developers
Jonathan Guyer
Daniel Wheeler
James Warren

Join our mailing list

100 Bureau Drive, M/S 6555
Gaithersburg, MD 20899

301-975-5329 Telephone
301-975-4553 Facsimile

examples.convection.robinΒΆ

Solve an advection-diffusion equation with a Robin boundary condition.

This example demonstrates how to apply a Robin boundary condition to an advection-diffusion equation. The equation we wish to solve is given by,

0 &=\frac{\partial^2 C}{\partial x^2}
 - P \frac{\partial C}{\partial x} -D C \qquad 0 < x <1\\
x=0: P &= -\frac{\partial C}{\partial x} + P C \\
x=1: \frac{\partial C}{\partial x} & = 0

The analytical solution for this equation is given by,

C \left( x \right) =
\frac{ 2 P \exp{\left(\frac{P x}{2}\right)}
       \left[ \left(P + A \right) \exp{\left(\frac{A}{2} \left(x - 1\right)\right)} -
              \left(P - A \right) \exp{\left(-\frac{A}{2} \left(x - 1\right)\right)} \right]}
     { \left(P + A \right)^2 \exp{\left(\frac{A}{2}\right)} -
       \left(P - A \right)^2 \exp{\left(-\frac{A}{2}\right)}}

where

A = \sqrt{P + 4D^2}

>>> from fipy import *
>>> nx = 100
>>> dx = 1.0 / nx
>>> mesh = Grid1D(nx=nx, dx=dx)
>>> C = CellVariable(mesh=mesh)
>>> D = 2.0
>>> P = 3.0
>>> C.faceGrad.constrain([-P + P * C.faceValue], mesh.facesLeft)
>>> C.faceGrad.constrain([0], mesh.facesRight)
>>> eq = PowerLawConvectionTerm((P,)) == \
...      DiffusionTerm() - ImplicitSourceTerm(D)
>>> A = numerix.sqrt(P**2 + 4 * D)
>>> x = mesh.cellCenters[0]
>>> CAnalytical = CellVariable(mesh=mesh)
>>> CAnalytical.setValue(2 * P * numerix.exp(P * x / 2) * ((P + A) * numerix.exp(A / 2 * (1 - x))
...             - (P - A) * numerix.exp(-A / 2 *(1 - x)))/
...             ((P + A)**2*numerix.exp(A / 2)- (P - A)**2 * numerix.exp(-A / 2)))
>>> if __name__ == '__main__':
...     C.name = 'C'
...     viewer = Viewer(vars=(C, CAnalytical))
>>> if __name__ == '__main__':
...     restol = 1e-5
...     anstol = 1e-3
... else:
...     restol = 0.5
...     anstol = 0.15
>>> res = 1e+10
>>> while res > restol:
...     res = eq.sweep(var=C)
...     if __name__ == '__main__':
...         viewer.plot()
>>> print C.allclose(CAnalytical, rtol=anstol, atol=anstol)
True