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,

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)}}](../../../_images/math/fc79758e4ae86da2393c7983eb1c21b7541c20c5.png)
where

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