fipy.terms package

Submodules

fipy.terms.abstractBinaryTerm module

fipy.terms.abstractConvectionTerm module

fipy.terms.abstractDiffusionTerm module

fipy.terms.abstractUpwindConvectionTerm module

fipy.terms.advectionTerm module

class fipy.terms.advectionTerm.AdvectionTerm(coeff=None)

Bases: FirstOrderAdvectionTerm

The AdvectionTerm object constructs the b vector contribution for the advection term given by

u \abs{\nabla \phi}

from the advection equation given by:

\frac{\partial \phi}{\partial t} + u \abs{\nabla \phi} = 0

The construction of the gradient magnitude term requires upwinding as in the standard FirstOrderAdvectionTerm. The higher order terms are incorporated as follows. The formula used here is given by:

u_P \abs{\nabla \phi}_P = \max \left( u_P , 0 \right) \left[  \sum_A \min \left( D_{AP}, 0 \right)^2 \right]^{1/2} +  \min \left( u_P , 0 \right) \left[  \sum_A \max \left( D_{AP}, 0 \right)^2 \right]^{1/2}

where,

D_{AP} = \frac{ \phi_A - \phi_P } { d_{AP}} - \frac{ d_{AP} } {2} m \left(L_A, L_P \right)

and

m\left(x, y\right) &= x \qquad \text{if $\abs{x} \le \abs{y} \forall xy \ge 0$} \\
m\left(x, y\right) &= y \qquad \text{if $\abs{x} > \abs{y} \forall xy \ge 0$} \\
m\left(x, y\right) &= 0 \qquad \text{if $xy < 0$}

also,

L_A &= \frac{\phi_{AA} + \phi_P - 2 \phi_A}{d_{AP}^2} \\
L_P &= \frac{\phi_{A} + \phi_{PP} - 2 \phi_P}{d_{AP}^2}

Here are some simple test cases for this problem:

>>> from fipy.meshes import Grid1D
>>> from fipy.solvers import *
>>> SparseMatrix = LinearPCGSolver()._matrixClass
>>> mesh = Grid1D(dx = 1., nx = 3)

Trivial test:

>>> from fipy.variables.cellVariable import CellVariable
>>> coeff = CellVariable(mesh = mesh, value = numerix.zeros(3, 'd'))
>>> v, L, b = AdvectionTerm(0.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.zeros(3, 'd'), atol = 1e-10)) 
True

Less trivial test:

>>> coeff = CellVariable(mesh = mesh, value = numerix.arange(3))
>>> v, L, b = AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((0., -1., -1.)), atol = 1e-10)) 
True

Even less trivial

>>> coeff = CellVariable(mesh = mesh, value = numerix.arange(3))
>>> v, L, b = AdvectionTerm(-1.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((1., 1., 0.)), atol = 1e-10)) 
True

Another trivial test case (more trivial than a trivial test case standing on a harpsichord singing “trivial test cases are here again”)

>>> vel = numerix.array((-1, 2, -3))
>>> coeff = CellVariable(mesh = mesh, value = numerix.array((4, 6, 1)))
>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, -vel * numerix.array((2, numerix.sqrt(5**2 + 2**2), 5)), atol = 1e-10)) 
True

Somewhat less trivial test case:

>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(dx = 1., dy = 1., nx = 2, ny = 2)
>>> vel = numerix.array((3, -5, -6, -3))
>>> coeff = CellVariable(mesh = mesh, value = numerix.array((3, 1, 6, 7)))
>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> answer = -vel * numerix.array((2, numerix.sqrt(2**2 + 6**2), 1, 0))
>>> print(numerix.allclose(b, answer, atol = 1e-10)) 
True

For the above test cases the AdvectionTerm gives the same result as the AdvectionTerm. The following test imposes a quadratic field. The higher order term can resolve this field correctly.

\phi = x^2

The returned vector b should have the value:

-\abs{\nabla \phi} = -\left|\frac{\partial \phi}{\partial x}\right| = - 2 \abs{x}

Build the test case in the following way,

>>> mesh = Grid1D(dx = 1., nx = 5)
>>> vel = 1.
>>> coeff = CellVariable(mesh = mesh, value = mesh.cellCenters[0]**2)
>>> v, L, b = __AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)

The first order term is not accurate. The first and last element are ignored because they don’t have any neighbors for higher order evaluation

>>> print(numerix.allclose(CellVariable(mesh=mesh,
... value=b).globalValue[1:-1], -2 * mesh.cellCenters.globalValue[0][1:-1]))
False

The higher order term is spot on.

>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(CellVariable(mesh=mesh,
... value=b).globalValue[1:-1], -2 * mesh.cellCenters.globalValue[0][1:-1]))
True

The AdvectionTerm will also resolve a circular field with more accuracy,

\phi = \left( x^2 + y^2 \right)^{1/2}

Build the test case in the following way,

>>> mesh = Grid2D(dx = 1., dy = 1., nx = 10, ny = 10)
>>> vel = 1.
>>> x, y = mesh.cellCenters
>>> r = numerix.sqrt(x**2 + y**2)
>>> coeff = CellVariable(mesh = mesh, value = r)
>>> v, L, b = __AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> error = CellVariable(mesh=mesh, value=b + 1)
>>> ans = CellVariable(mesh=mesh, value=b + 1)
>>> ans[(x > 2) & (x < 8) & (y > 2) & (y < 8)] = 0.123105625618
>>> print((error <= ans).all())
True

The maximum error is large (about 12 %) for the first order advection.

>>> v, L, b = AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> error = CellVariable(mesh=mesh, value=b + 1)
>>> ans = CellVariable(mesh=mesh, value=b + 1)
>>> ans[(x > 2) & (x < 8) & (y > 2) & (y < 8)] = 0.0201715476598
>>> print((error <= ans).all())
True

The maximum error is 2 % when using a higher order contribution.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.asymmetricConvectionTerm module

fipy.terms.binaryTerm module

fipy.terms.cellTerm module

class fipy.terms.cellTerm.CellTerm(coeff=1.0, var=None)

Bases: _NonDiffusionTerm

Attention

This class is abstract. Always create one of its subclasses.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.centralDiffConvectionTerm module

class fipy.terms.centralDiffConvectionTerm.CentralDifferenceConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractConvectionTerm

This Term represents

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the central differencing scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.coupledBinaryTerm module

fipy.terms.diffusionTerm module

class fipy.terms.diffusionTerm.DiffusionTerm(coeff=(1.0,), var=None)

Bases: DiffusionTermNoCorrection

This term represents a higher order diffusion term. The order of the term is determined by the number of coeffs, such that:

DiffusionTerm(D1)

represents a typical 2nd-order diffusion term of the form

\nabla\cdot\left(D_1 \nabla \phi\right)

and:

DiffusionTerm((D1,D2))

represents a 4th-order Cahn-Hilliard term of the form

\nabla \cdot \left\{ D_1 \nabla \left[ \nabla\cdot\left( D_2 \nabla \phi\right) \right] \right\}

and so on.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.diffusionTerm.DiffusionTermCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.diffusionTerm.DiffusionTermNoCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.diffusionTermCorrection module

class fipy.terms.diffusionTermCorrection.DiffusionTermCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.diffusionTermNoCorrection module

class fipy.terms.diffusionTermNoCorrection.DiffusionTermNoCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.explicitDiffusionTerm module

class fipy.terms.explicitDiffusionTerm.ExplicitDiffusionTerm(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

The discretization for the ExplicitDiffusionTerm is given by

\int_V \nabla \cdot (\Gamma\nabla\phi) dV \simeq \sum_f \Gamma_f
\frac{\phi_A^\text{old}-\phi_P^\text{old}}{d_{AP}} A_f

where \phi_A^\text{old} and \phi_P^\text{old} are the old values of the variable. The term is added to the RHS vector and makes no contribution to the solution matrix.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.explicitSourceTerm module

fipy.terms.explicitUpwindConvectionTerm module

class fipy.terms.explicitUpwindConvectionTerm.ExplicitUpwindConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractUpwindConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P^\text{old} +(1-\alpha_f)\phi_A^\text{old} and \alpha_f is calculated using the upwind scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.exponentialConvectionTerm module

class fipy.terms.exponentialConvectionTerm.ExponentialConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the exponential scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.faceTerm module

class fipy.terms.faceTerm.FaceTerm(coeff=1.0, var=None)

Bases: _NonDiffusionTerm

Attention

This class is abstract. Always create one of its subclasses.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.firstOrderAdvectionTerm module

class fipy.terms.firstOrderAdvectionTerm.FirstOrderAdvectionTerm(coeff=None)

Bases: _NonDiffusionTerm

The FirstOrderAdvectionTerm object constructs the b vector contribution for the advection term given by

u \abs{\nabla \phi}

from the advection equation given by:

\frac{\partial \phi}{\partial t} + u \abs{\nabla \phi} = 0

The construction of the gradient magnitude term requires upwinding. The formula used here is given by:

u_P \abs{\nabla \phi}_P = \max \left( u_P , 0 \right) \left[  \sum_A \min \left( \frac{ \phi_A - \phi_P } { d_{AP}}, 0 \right)^2 \right]^{1/2} +  \min \left( u_P , 0 \right) \left[  \sum_A \max \left( \frac{ \phi_A - \phi_P } { d_{AP}}, 0 \right)^2 \right]^{1/2}

Here are some simple test cases for this problem:

>>> from fipy.meshes import Grid1D
>>> from fipy.solvers import *
>>> SparseMatrix = LinearLUSolver()._matrixClass
>>> mesh = Grid1D(dx = 1., nx = 3)
>>> from fipy.variables.cellVariable import CellVariable

Trivial test:

>>> var = CellVariable(value = numerix.zeros(3, 'd'), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(0.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.zeros(3, 'd'), atol = 1e-10)) 
True

Less trivial test:

>>> var = CellVariable(value = numerix.arange(3), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(1.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((0., -1., -1.)), atol = 1e-10)) 
True

Even less trivial

>>> var = CellVariable(value = numerix.arange(3), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(-1.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((1., 1., 0.)), atol = 1e-10)) 
True

Another trivial test case (more trivial than a trivial test case standing on a harpsichord singing “trivial test cases are here again”)

>>> vel = numerix.array((-1, 2, -3))
>>> var = CellVariable(value = numerix.array((4, 6, 1)), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(vel)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, -vel * numerix.array((2, numerix.sqrt(5**2 + 2**2), 5)), atol = 1e-10)) 
True

Somewhat less trivial test case:

>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(dx = 1., dy = 1., nx = 2, ny = 2)
>>> vel = numerix.array((3, -5, -6, -3))
>>> var = CellVariable(value = numerix.array((3, 1, 6, 7)), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(vel)._buildMatrix(var, SparseMatrix)
>>> answer = -vel * numerix.array((2, numerix.sqrt(2**2 + 6**2), 1, 0))
>>> print(numerix.allclose(b, answer, atol = 1e-10)) 
True

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.hybridConvectionTerm module

class fipy.terms.hybridConvectionTerm.HybridConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the hybrid scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.implicitDiffusionTerm module

fipy.terms.implicitDiffusionTerm.ImplicitDiffusionTerm

alias of DiffusionTerm

fipy.terms.implicitSourceTerm module

class fipy.terms.implicitSourceTerm.ImplicitSourceTerm(coeff=0.0, var=None)

Bases: SourceTerm

The ImplicitSourceTerm represents

\int_V \phi S \,dV \simeq \phi_P S_P V_P

where S is the coeff value.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.nonDiffusionTerm module

fipy.terms.powerLawConvectionTerm module

class fipy.terms.powerLawConvectionTerm.PowerLawConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the power law scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.residualTerm module

class fipy.terms.residualTerm.ResidualTerm(equation, underRelaxation=1.0)

Bases: _ExplicitSourceTerm

The ResidualTerm is a special form of explicit SourceTerm that adds the residual of one equation to another equation. Useful for Newton’s method.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.sourceTerm module

class fipy.terms.sourceTerm.SourceTerm(coeff=0.0, var=None)

Bases: CellTerm

Attention

This class is abstract. Always create one of its subclasses.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.term module

class fipy.terms.term.Term(coeff=1.0, var=None)

Bases: object

Attention

This class is abstract. Always create one of its subclasses.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

Return repr(self).

__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.test module

fipy.terms.transientTerm module

class fipy.terms.transientTerm.TransientTerm(coeff=1.0, var=None)

Bases: CellTerm

The TransientTerm represents

\int_V \frac{\partial (\rho \phi)}{\partial t} dV \simeq
\frac{(\rho_{P} \phi_{P} - \rho_{P}^\text{old} \phi_P^\text{old}) V_P}{\Delta t}

where \rho is the coeff value.

The following test case verifies that variable coefficients and old coefficient values work correctly. We will solve the following equation

\frac{ \partial \phi^2 } { \partial t } = k.

The analytic solution is given by

\phi = \sqrt{ \phi_0^2 + k t },

where \phi_0 is the initial value.

>>> phi0 = 1.
>>> k = 1.
>>> dt = 1.
>>> relaxationFactor = 1.5
>>> steps = 2
>>> sweeps = 8
>>> from fipy.meshes import Grid1D
>>> mesh = Grid1D(nx = 1)
>>> from fipy.variables.cellVariable import CellVariable
>>> var = CellVariable(mesh = mesh, value = phi0, hasOld = 1)
>>> from fipy.terms.transientTerm import TransientTerm
>>> from fipy.terms.implicitSourceTerm import ImplicitSourceTerm

Relaxation, given by relaxationFactor, is required for a converged solution.

>>> eq = TransientTerm(var) == ImplicitSourceTerm(-relaxationFactor) \
...                            + var * relaxationFactor + k

A number of sweeps at each time step are required to let the relaxation take effect.

>>> from builtins import range
>>> for step in range(steps):
...     var.updateOld()
...     for sweep in range(sweeps):
...         eq.solve(var, dt = dt)

Compare the final result with the analytical solution.

>>> from fipy.tools import numerix
>>> print(var.allclose(numerix.sqrt(k * dt * steps + phi0**2)))
1

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.unaryTerm module

fipy.terms.upwindConvectionTerm module

class fipy.terms.upwindConvectionTerm.UpwindConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractUpwindConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the upwind convection scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.vanLeerConvectionTerm module

class fipy.terms.vanLeerConvectionTerm.VanLeerConvectionTerm(coeff=1.0, var=None)

Bases: ExplicitUpwindConvectionTerm

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

Module contents

exception fipy.terms.AbstractBaseClassError(s="can't instantiate abstract base class")

Bases: NotImplementedError

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class fipy.terms.AdvectionTerm(coeff=None)

Bases: FirstOrderAdvectionTerm

The AdvectionTerm object constructs the b vector contribution for the advection term given by

u \abs{\nabla \phi}

from the advection equation given by:

\frac{\partial \phi}{\partial t} + u \abs{\nabla \phi} = 0

The construction of the gradient magnitude term requires upwinding as in the standard FirstOrderAdvectionTerm. The higher order terms are incorporated as follows. The formula used here is given by:

u_P \abs{\nabla \phi}_P = \max \left( u_P , 0 \right) \left[  \sum_A \min \left( D_{AP}, 0 \right)^2 \right]^{1/2} +  \min \left( u_P , 0 \right) \left[  \sum_A \max \left( D_{AP}, 0 \right)^2 \right]^{1/2}

where,

D_{AP} = \frac{ \phi_A - \phi_P } { d_{AP}} - \frac{ d_{AP} } {2} m \left(L_A, L_P \right)

and

m\left(x, y\right) &= x \qquad \text{if $\abs{x} \le \abs{y} \forall xy \ge 0$} \\
m\left(x, y\right) &= y \qquad \text{if $\abs{x} > \abs{y} \forall xy \ge 0$} \\
m\left(x, y\right) &= 0 \qquad \text{if $xy < 0$}

also,

L_A &= \frac{\phi_{AA} + \phi_P - 2 \phi_A}{d_{AP}^2} \\
L_P &= \frac{\phi_{A} + \phi_{PP} - 2 \phi_P}{d_{AP}^2}

Here are some simple test cases for this problem:

>>> from fipy.meshes import Grid1D
>>> from fipy.solvers import *
>>> SparseMatrix = LinearPCGSolver()._matrixClass
>>> mesh = Grid1D(dx = 1., nx = 3)

Trivial test:

>>> from fipy.variables.cellVariable import CellVariable
>>> coeff = CellVariable(mesh = mesh, value = numerix.zeros(3, 'd'))
>>> v, L, b = AdvectionTerm(0.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.zeros(3, 'd'), atol = 1e-10)) 
True

Less trivial test:

>>> coeff = CellVariable(mesh = mesh, value = numerix.arange(3))
>>> v, L, b = AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((0., -1., -1.)), atol = 1e-10)) 
True

Even less trivial

>>> coeff = CellVariable(mesh = mesh, value = numerix.arange(3))
>>> v, L, b = AdvectionTerm(-1.)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((1., 1., 0.)), atol = 1e-10)) 
True

Another trivial test case (more trivial than a trivial test case standing on a harpsichord singing “trivial test cases are here again”)

>>> vel = numerix.array((-1, 2, -3))
>>> coeff = CellVariable(mesh = mesh, value = numerix.array((4, 6, 1)))
>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(b, -vel * numerix.array((2, numerix.sqrt(5**2 + 2**2), 5)), atol = 1e-10)) 
True

Somewhat less trivial test case:

>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(dx = 1., dy = 1., nx = 2, ny = 2)
>>> vel = numerix.array((3, -5, -6, -3))
>>> coeff = CellVariable(mesh = mesh, value = numerix.array((3, 1, 6, 7)))
>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> answer = -vel * numerix.array((2, numerix.sqrt(2**2 + 6**2), 1, 0))
>>> print(numerix.allclose(b, answer, atol = 1e-10)) 
True

For the above test cases the AdvectionTerm gives the same result as the AdvectionTerm. The following test imposes a quadratic field. The higher order term can resolve this field correctly.

\phi = x^2

The returned vector b should have the value:

-\abs{\nabla \phi} = -\left|\frac{\partial \phi}{\partial x}\right| = - 2 \abs{x}

Build the test case in the following way,

>>> mesh = Grid1D(dx = 1., nx = 5)
>>> vel = 1.
>>> coeff = CellVariable(mesh = mesh, value = mesh.cellCenters[0]**2)
>>> v, L, b = __AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)

The first order term is not accurate. The first and last element are ignored because they don’t have any neighbors for higher order evaluation

>>> print(numerix.allclose(CellVariable(mesh=mesh,
... value=b).globalValue[1:-1], -2 * mesh.cellCenters.globalValue[0][1:-1]))
False

The higher order term is spot on.

>>> v, L, b = AdvectionTerm(vel)._buildMatrix(coeff, SparseMatrix)
>>> print(numerix.allclose(CellVariable(mesh=mesh,
... value=b).globalValue[1:-1], -2 * mesh.cellCenters.globalValue[0][1:-1]))
True

The AdvectionTerm will also resolve a circular field with more accuracy,

\phi = \left( x^2 + y^2 \right)^{1/2}

Build the test case in the following way,

>>> mesh = Grid2D(dx = 1., dy = 1., nx = 10, ny = 10)
>>> vel = 1.
>>> x, y = mesh.cellCenters
>>> r = numerix.sqrt(x**2 + y**2)
>>> coeff = CellVariable(mesh = mesh, value = r)
>>> v, L, b = __AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> error = CellVariable(mesh=mesh, value=b + 1)
>>> ans = CellVariable(mesh=mesh, value=b + 1)
>>> ans[(x > 2) & (x < 8) & (y > 2) & (y < 8)] = 0.123105625618
>>> print((error <= ans).all())
True

The maximum error is large (about 12 %) for the first order advection.

>>> v, L, b = AdvectionTerm(1.)._buildMatrix(coeff, SparseMatrix)
>>> error = CellVariable(mesh=mesh, value=b + 1)
>>> ans = CellVariable(mesh=mesh, value=b + 1)
>>> ans[(x > 2) & (x < 8) & (y > 2) & (y < 8)] = 0.0201715476598
>>> print((error <= ans).all())
True

The maximum error is 2 % when using a higher order contribution.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.CentralDifferenceConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractConvectionTerm

This Term represents

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the central differencing scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.ConvectionTerm

alias of PowerLawConvectionTerm

class fipy.terms.DiffusionTerm(coeff=(1.0,), var=None)

Bases: DiffusionTermNoCorrection

This term represents a higher order diffusion term. The order of the term is determined by the number of coeffs, such that:

DiffusionTerm(D1)

represents a typical 2nd-order diffusion term of the form

\nabla\cdot\left(D_1 \nabla \phi\right)

and:

DiffusionTerm((D1,D2))

represents a 4th-order Cahn-Hilliard term of the form

\nabla \cdot \left\{ D_1 \nabla \left[ \nabla\cdot\left( D_2 \nabla \phi\right) \right] \right\}

and so on.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.DiffusionTermCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.DiffusionTermNoCorrection(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.ExplicitDiffusionTerm(coeff=(1.0,), var=None)

Bases: _AbstractDiffusionTerm

The discretization for the ExplicitDiffusionTerm is given by

\int_V \nabla \cdot (\Gamma\nabla\phi) dV \simeq \sum_f \Gamma_f
\frac{\phi_A^\text{old}-\phi_P^\text{old}}{d_{AP}} A_f

where \phi_A^\text{old} and \phi_P^\text{old} are the old values of the variable. The term is added to the RHS vector and makes no contribution to the solution matrix.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)
__neg__()
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.ExplicitUpwindConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractUpwindConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P^\text{old} +(1-\alpha_f)\phi_A^\text{old} and \alpha_f is calculated using the upwind scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

exception fipy.terms.ExplicitVariableError(s='Terms with explicit Variables cannot mix with Terms with implicit Variables.')

Bases: Exception

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class fipy.terms.ExponentialConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the exponential scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.FirstOrderAdvectionTerm(coeff=None)

Bases: _NonDiffusionTerm

The FirstOrderAdvectionTerm object constructs the b vector contribution for the advection term given by

u \abs{\nabla \phi}

from the advection equation given by:

\frac{\partial \phi}{\partial t} + u \abs{\nabla \phi} = 0

The construction of the gradient magnitude term requires upwinding. The formula used here is given by:

u_P \abs{\nabla \phi}_P = \max \left( u_P , 0 \right) \left[  \sum_A \min \left( \frac{ \phi_A - \phi_P } { d_{AP}}, 0 \right)^2 \right]^{1/2} +  \min \left( u_P , 0 \right) \left[  \sum_A \max \left( \frac{ \phi_A - \phi_P } { d_{AP}}, 0 \right)^2 \right]^{1/2}

Here are some simple test cases for this problem:

>>> from fipy.meshes import Grid1D
>>> from fipy.solvers import *
>>> SparseMatrix = LinearLUSolver()._matrixClass
>>> mesh = Grid1D(dx = 1., nx = 3)
>>> from fipy.variables.cellVariable import CellVariable

Trivial test:

>>> var = CellVariable(value = numerix.zeros(3, 'd'), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(0.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.zeros(3, 'd'), atol = 1e-10)) 
True

Less trivial test:

>>> var = CellVariable(value = numerix.arange(3), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(1.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((0., -1., -1.)), atol = 1e-10)) 
True

Even less trivial

>>> var = CellVariable(value = numerix.arange(3), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(-1.)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, numerix.array((1., 1., 0.)), atol = 1e-10)) 
True

Another trivial test case (more trivial than a trivial test case standing on a harpsichord singing “trivial test cases are here again”)

>>> vel = numerix.array((-1, 2, -3))
>>> var = CellVariable(value = numerix.array((4, 6, 1)), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(vel)._buildMatrix(var, SparseMatrix)
>>> print(numerix.allclose(b, -vel * numerix.array((2, numerix.sqrt(5**2 + 2**2), 5)), atol = 1e-10)) 
True

Somewhat less trivial test case:

>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(dx = 1., dy = 1., nx = 2, ny = 2)
>>> vel = numerix.array((3, -5, -6, -3))
>>> var = CellVariable(value = numerix.array((3, 1, 6, 7)), mesh = mesh)
>>> v, L, b = FirstOrderAdvectionTerm(vel)._buildMatrix(var, SparseMatrix)
>>> answer = -vel * numerix.array((2, numerix.sqrt(2**2 + 6**2), 1, 0))
>>> print(numerix.allclose(b, answer, atol = 1e-10)) 
True

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.HybridConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the hybrid scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

fipy.terms.ImplicitDiffusionTerm

alias of DiffusionTerm

class fipy.terms.ImplicitSourceTerm(coeff=0.0, var=None)

Bases: SourceTerm

The ImplicitSourceTerm represents

\int_V \phi S \,dV \simeq \phi_P S_P V_P

where S is the coeff value.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

exception fipy.terms.IncorrectSolutionVariable(s='The solution variable is incorrect.')

Bases: Exception

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class fipy.terms.PowerLawConvectionTerm(coeff=1.0, var=None)

Bases: _AsymmetricConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the power law scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.ResidualTerm(equation, underRelaxation=1.0)

Bases: _ExplicitSourceTerm

The ResidualTerm is a special form of explicit SourceTerm that adds the residual of one equation to another equation. Useful for Newton’s method.

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

exception fipy.terms.SolutionVariableNumberError(s='Different number of solution variables and equations.')

Bases: Exception

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception fipy.terms.SolutionVariableRequiredError(s='The solution variable needs to be specified.')

Bases: Exception

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception fipy.terms.TermMultiplyError(s='Must multiply terms by int or float.')

Bases: Exception

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class fipy.terms.TransientTerm(coeff=1.0, var=None)

Bases: CellTerm

The TransientTerm represents

\int_V \frac{\partial (\rho \phi)}{\partial t} dV \simeq
\frac{(\rho_{P} \phi_{P} - \rho_{P}^\text{old} \phi_P^\text{old}) V_P}{\Delta t}

where \rho is the coeff value.

The following test case verifies that variable coefficients and old coefficient values work correctly. We will solve the following equation

\frac{ \partial \phi^2 } { \partial t } = k.

The analytic solution is given by

\phi = \sqrt{ \phi_0^2 + k t },

where \phi_0 is the initial value.

>>> phi0 = 1.
>>> k = 1.
>>> dt = 1.
>>> relaxationFactor = 1.5
>>> steps = 2
>>> sweeps = 8
>>> from fipy.meshes import Grid1D
>>> mesh = Grid1D(nx = 1)
>>> from fipy.variables.cellVariable import CellVariable
>>> var = CellVariable(mesh = mesh, value = phi0, hasOld = 1)
>>> from fipy.terms.transientTerm import TransientTerm
>>> from fipy.terms.implicitSourceTerm import ImplicitSourceTerm

Relaxation, given by relaxationFactor, is required for a converged solution.

>>> eq = TransientTerm(var) == ImplicitSourceTerm(-relaxationFactor) \
...                            + var * relaxationFactor + k

A number of sweeps at each time step are required to let the relaxation take effect.

>>> from builtins import range
>>> for step in range(steps):
...     var.updateOld()
...     for sweep in range(sweeps):
...         eq.solve(var, dt = dt)

Compare the final result with the analytical solution.

>>> from fipy.tools import numerix
>>> print(var.allclose(numerix.sqrt(k * dt * steps + phi0**2)))
1

Create a Term.

Parameters:

coeff (float or CellVariable or FaceVariable) – Coefficient for the term. FaceVariable objects are only acceptable for diffusion or convection terms.

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.UpwindConvectionTerm(coeff=1.0, var=None)

Bases: _AbstractUpwindConvectionTerm

The discretization for this Term is given by

\int_V \nabla \cdot (\vec{u} \phi)\,dV \simeq \sum_{f} (\vec{n}
\cdot \vec{u})_f \phi_f A_f

where \phi_f=\alpha_f \phi_P +(1-\alpha_f)\phi_A and \alpha_f is calculated using the upwind convection scheme. For further details see Numerical Schemes.

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

class fipy.terms.VanLeerConvectionTerm(coeff=1.0, var=None)

Bases: ExplicitUpwindConvectionTerm

Create a _AbstractConvectionTerm object.

>>> from fipy import *
>>> m = Grid1D(nx = 2)
>>> cv = CellVariable(mesh = m)
>>> fv = FaceVariable(mesh = m)
>>> vcv = CellVariable(mesh=m, rank=1)
>>> vfv = FaceVariable(mesh=m, rank=1)
>>> __ConvectionTerm(coeff = cv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = fv) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> __ConvectionTerm(coeff = vcv)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = vfv)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.]]), mesh=UniformGrid1D(dx=1.0, nx=2)))
>>> __ConvectionTerm(coeff = (1,))
__ConvectionTerm(coeff=(1,))
>>> ExplicitUpwindConvectionTerm(coeff = (0,)).solve(var=cv, solver=DummySolver()) 
Traceback (most recent call last):
    ...
TransientTermError: The equation requires a TransientTerm with explicit convection.
>>> (TransientTerm(0.) - ExplicitUpwindConvectionTerm(coeff = (0,))).solve(var=cv, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = 1)).solve(var=cv, solver=DummySolver(), dt=1.) 
Traceback (most recent call last):
    ...
VectorCoeffError: The coefficient must be a vector value.
>>> m2 = Grid2D(nx=2, ny=1)
>>> cv2 = CellVariable(mesh=m2)
>>> vcv2 = CellVariable(mesh=m2, rank=1)
>>> vfv2 = FaceVariable(mesh=m2, rank=1)
>>> __ConvectionTerm(coeff=vcv2)
__ConvectionTerm(coeff=_ArithmeticCellToFaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> __ConvectionTerm(coeff=vfv2)
__ConvectionTerm(coeff=FaceVariable(value=array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]]), mesh=UniformGrid2D(dx=1.0, nx=2, dy=1.0, ny=1)))
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = ((0,), (0,)))).solve(var=cv2, solver=DummySolver(), dt=1.)
>>> (TransientTerm() - ExplicitUpwindConvectionTerm(coeff = (0, 0))).solve(var=cv2, solver=DummySolver(), dt=1.)
Parameters:

coeff (The Term’s coefficient value.) –

property RHSvector

Return the RHS vector calculated in solve() or sweep(). The cacheRHSvector() method should be called before solve() or sweep() to cache the vector.

__add__(other)
__and__(other)
__div__(other)
__eq__(other)

Return self==value.

__hash__()

Return hash(self).

__mul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__neg__()

Negate a Term.

>>> -__NonDiffusionTerm(coeff=1.)
__NonDiffusionTerm(coeff=-1.0)
__pos__()
__radd__(other)
__rand__(other)
__repr__()

The representation of a Term object is given by,

>>> print(__UnaryTerm(123.456))
__UnaryTerm(coeff=123.456)
__rmul__(other)

Multiply a term

>>> 2. * __NonDiffusionTerm(coeff=0.5)
__NonDiffusionTerm(coeff=1.0)

Test for ticket:291.

>>> from fipy import PowerLawConvectionTerm
>>> PowerLawConvectionTerm(coeff=[[1], [0]]) * 1.0
PowerLawConvectionTerm(coeff=array([[ 1.],
       [ 0.]]))
__rsub__(other)
__sub__(other)
__truediv__(other)
cacheMatrix()

Informs solve() and sweep() to cache their matrix so that matrix can return the matrix.

cacheRHSvector()

Informs solve() and sweep() to cache their right hand side vector so that getRHSvector() can return it.

copy()
getDefaultSolver(var=None, solver=None, *args, **kwargs)
justErrorVector(var=None, solver=None, boundaryConditions=(), dt=1.0, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the error as well as applying under-relaxation.

justErrorVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy.solvers import DummySolver
>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(DiffusionTerm().justErrorVector(v, solver=DummySolver())) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

error – The residual vector \vec{e}=\mathsf{L}\vec{x}_\text{old} - \vec{b}

Return type:

CellVariable

justResidualVector(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

justResidualVector returns the overlapping local value in parallel (not the non-overlapping value).

>>> from fipy import *
>>> m = Grid1D(nx=10)
>>> v = CellVariable(mesh=m)
>>> len(numerix.asarray(DiffusionTerm().justResidualVector(v))) == m.numberOfCells
True
Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

property matrix

Return the matrix calculated in solve() or sweep(). The cacheMatrix() method should be called before solve() or sweep() to cache the matrix.

residualVectorAndNorm(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None)

Builds the Term’s linear system once.

This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

Returns:

  • residual (~fipy.variables.cellVariable.CellVariable) – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

  • norm (float) – The L2 norm of residual, \|\vec{r}\|_2

solve(var=None, solver=None, boundaryConditions=(), dt=None)

Builds and solves the Term’s linear system once. This method does not return the residual. It should be used when the residual is not required.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

sweep(var=None, solver=None, boundaryConditions=(), dt=None, underRelaxation=None, residualFn=None, cacheResidual=False, cacheError=False)

Builds and solves the Term’s linear system once. This method also recalculates and returns the residual as well as applying under-relaxation.

Parameters:
  • var (CellVariable) – Variable to be solved for. Provides the initial condition, the old value and holds the solution on completion.

  • solver (Solver) – Iterative solver to be used to solve the linear system of equations. The default sovler depends on the solver package selected.

  • boundaryConditions (tuple of BoundaryCondition) –

  • dt (float) – Timestep size.

  • underRelaxation (float) – Usually a value between 0 and 1 or None in the case of no under-relaxation

  • residualFn (function) – Takes var, matrix, and RHSvector arguments, used to customize the residual calculation.

  • cacheResidual (bool) – If True, calculate and store the residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b} in the residualVector member of Term

  • cacheError (bool) – If True, use the residual vector \vec{r} to solve \mathsf{L}\vec{e}=\vec{r} for the error vector \vec{e} and store it in the errorVector member of Term

Returns:

residual – The residual vector \vec{r}=\mathsf{L}\vec{x} - \vec{b}

Return type:

CellVariable

exception fipy.terms.VectorCoeffError(s='The coefficient must be a vector value.')

Bases: TypeError

__cause__

exception cause

__context__

exception context

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__reduce__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setstate__()
__str__()

Return str(self).

__suppress_context__
__traceback__
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Last updated on Jun 27, 2023. Created using Sphinx 6.2.1.