fipy.variables.faceVariable¶
Classes
|
|
- class fipy.variables.faceVariable.FaceVariable(*args, **kwds)¶
Bases:
MeshVariable
- Parameters:
mesh (Mesh) – the mesh that defines the geometry of this Variable
name (str) – the user-readable name of the Variable
value (float or array_like) – the initial value
rank (int) – the rank (number of dimensions) of each element of this Variable. Default: 0
elementshape (
tuple
ofint
) – the shape of each element of this variable Default: rank * (mesh.dim,)unit (str or PhysicalUnit) – The physical units of the variable
- __abs__()¶
Following test it to fix a bug with C inline string using abs() instead of fabs()
>>> print(abs(Variable(2.3) - Variable(1.2))) 1.1
Check representation works with different versions of numpy
>>> print(repr(abs(Variable(2.3)))) numerix.fabs(Variable(value=array(2.3)))
- __and__(other)¶
This test case has been added due to a weird bug that was appearing.
>>> a = Variable(value=(0, 0, 1, 1)) >>> b = Variable(value=(0, 1, 0, 1)) >>> print(numerix.equal((a == 0) & (b == 1), [False, True, False, False]).all()) True >>> print(a & b) [0 0 0 1] >>> from fipy.meshes import Grid1D >>> mesh = Grid1D(nx=4) >>> from fipy.variables.cellVariable import CellVariable >>> a = CellVariable(value=(0, 0, 1, 1), mesh=mesh) >>> b = CellVariable(value=(0, 1, 0, 1), mesh=mesh) >>> print(numerix.allequal((a == 0) & (b == 1), [False, True, False, False])) True >>> print(a & b) [0 0 0 1]
- __array__(dtype=None, copy=None)¶
Attempt to convert the Variable to a numerix array object
>>> v = Variable(value=[2, 3]) >>> print(numerix.array(v)) [2 3]
A dimensional Variable will convert to the numeric value in its base units
>>> v = Variable(value=[2, 3], unit="mm") >>> numerix.array(v) array([ 0.002, 0.003])
- __array_wrap__(arr, context=None, return_scalar=False)¶
Required to prevent numpy not calling the reverse binary operations. Both the following tests are examples ufuncs.
>>> print(type(numerix.array([1.0, 2.0]) * Variable([1.0, 2.0]))) <class 'fipy.variables.binaryOperatorVariable...binOp'>
>>> from scipy.special import gamma as Gamma >>> print(type(Gamma(Variable([1.0, 2.0])))) <class 'fipy.variables.unaryOperatorVariable...unOp'>
- __bool__()¶
>>> print(bool(Variable(value=0))) 0 >>> print(bool(Variable(value=(0, 0, 1, 1)))) Traceback (most recent call last): ... ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- __call__()¶
“Evaluate” the Variable and return its value
>>> a = Variable(value=3) >>> print(a()) 3 >>> b = a + 4 >>> b (Variable(value=array(3)) + 4) >>> b() 7
- __eq__(other)¶
Test if a Variable is equal to another quantity
>>> a = Variable(value=3) >>> b = (a == 4) >>> b (Variable(value=array(3)) == 4) >>> b() 0
- __ge__(other)¶
Test if a Variable is greater than or equal to another quantity
>>> a = Variable(value=3) >>> b = (a >= 4) >>> b (Variable(value=array(3)) >= 4) >>> b() 0 >>> a.value = 4 >>> print(b()) 1 >>> a.value = 5 >>> print(b()) 1
- __getitem__(index)¶
“Evaluate” the Variable and return the specified element
>>> a = Variable(value=((3., 4.), (5., 6.)), unit="m") + "4 m" >>> print(a[1, 1]) 10.0 m
It is an error to slice a Variable whose value is not sliceable
>>> Variable(value=3)[2] Traceback (most recent call last): ... IndexError: 0-d arrays can't be indexed
- __getstate__()¶
Used internally to collect the necessary information to
pickle
theMeshVariable
to persistent storage.
- __gt__(other)¶
Test if a Variable is greater than another quantity
>>> a = Variable(value=3) >>> b = (a > 4) >>> b (Variable(value=array(3)) > 4) >>> print(b()) 0 >>> a.value = 5 >>> print(b()) 1
- __hash__()¶
Return hash(self).
- __invert__()¶
Returns logical “not” of the Variable
>>> a = Variable(value=True) >>> print(~a) False
- __le__(other)¶
Test if a Variable is less than or equal to another quantity
>>> a = Variable(value=3) >>> b = (a <= 4) >>> b (Variable(value=array(3)) <= 4) >>> b() 1 >>> a.value = 4 >>> print(b()) 1 >>> a.value = 5 >>> print(b()) 0
- __lt__(other)¶
Test if a Variable is less than another quantity
>>> a = Variable(value=3) >>> b = (a < 4) >>> b (Variable(value=array(3)) < 4) >>> b() 1 >>> a.value = 4 >>> print(b()) 0 >>> print(1000000000000000000 * Variable(1) < 1.) 0 >>> print(1000 * Variable(1) < 1.) 0
Python automatically reverses the arguments when necessary
>>> 4 > Variable(value=3) (Variable(value=array(3)) < 4)
- __ne__(other)¶
Test if a Variable is not equal to another quantity
>>> a = Variable(value=3) >>> b = (a != 4) >>> b (Variable(value=array(3)) != 4) >>> b() 1
- static __new__(cls, *args, **kwds)¶
- __nonzero__()¶
>>> print(bool(Variable(value=0))) 0 >>> print(bool(Variable(value=(0, 0, 1, 1)))) Traceback (most recent call last): ... ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- __or__(other)¶
This test case has been added due to a weird bug that was appearing.
>>> a = Variable(value=(0, 0, 1, 1)) >>> b = Variable(value=(0, 1, 0, 1)) >>> print(numerix.equal((a == 0) | (b == 1), [True, True, False, True]).all()) True >>> print(a | b) [0 1 1 1] >>> from fipy.meshes import Grid1D >>> mesh = Grid1D(nx=4) >>> from fipy.variables.cellVariable import CellVariable >>> a = CellVariable(value=(0, 0, 1, 1), mesh=mesh) >>> b = CellVariable(value=(0, 1, 0, 1), mesh=mesh) >>> print(numerix.allequal((a == 0) | (b == 1), [True, True, False, True])) True >>> print(a | b) [0 1 1 1]
- __pow__(other)¶
return self**other, or self raised to power other
>>> print(Variable(1, "mol/l")**3) 1.0 mol**3/l**3 >>> print((Variable(1, "mol/l")**3).unit) <PhysicalUnit mol**3/l**3>
- __repr__()¶
Return repr(self).
- __setstate__(dict)¶
Used internally to create a new Variable from
pickled
persistent storage.
- __str__()¶
Return str(self).
- all(axis=None)¶
>>> print(Variable(value=(0, 0, 1, 1)).all()) 0 >>> print(Variable(value=(1, 1, 1, 1)).all()) 1
- allclose(other, rtol=1e-05, atol=1e-08)¶
>>> var = Variable((1, 1)) >>> print(var.allclose((1, 1))) 1 >>> print(var.allclose((1,))) 1
The following test is to check that the system does not run out of memory.
>>> from fipy.tools import numerix >>> var = Variable(numerix.ones(10000)) >>> print(var.allclose(numerix.zeros(10000, 'l'))) False
- any(axis=None)¶
>>> print(Variable(value=0).any()) 0 >>> print(Variable(value=(0, 0, 1, 1)).any()) 1
- constrain(value, where=None)¶
Constrain the Variable to have a value at an index or mask location specified by where.
>>> v = Variable((0, 1, 2, 3)) >>> v.constrain(2, numerix.array((True, False, False, False))) >>> print(v) [2 1 2 3] >>> v[:] = 10 >>> print(v) [ 2 10 10 10] >>> v.constrain(5, numerix.array((False, False, True, False))) >>> print(v) [ 2 10 5 10] >>> v[:] = 6 >>> print(v) [2 6 5 6] >>> v.constrain(8) >>> print(v) [8 8 8 8] >>> v[:] = 10 >>> print(v) [8 8 8 8] >>> del v.constraints[2] >>> print(v) [ 2 10 5 10]
>>> from fipy.variables.cellVariable import CellVariable >>> from fipy.meshes import Grid2D >>> m = Grid2D(nx=2, ny=2) >>> x, y = m.cellCenters >>> v = CellVariable(mesh=m, rank=1, value=(x, y)) >>> v.constrain(((0.,), (-1.,)), where=m.facesLeft) >>> print(v.faceValue) [[ 0.5 1.5 0.5 1.5 0.5 1.5 0. 1. 1.5 0. 1. 1.5] [ 0.5 0.5 1. 1. 1.5 1.5 -1. 0.5 0.5 -1. 1.5 1.5]]
- Parameters:
value (float or array_like) – The value of the constraint
where (array_like of
bool
) – The constraint mask or index specifying the location of the constraint
- property constraintMask¶
Test that constraintMask returns a Variable that updates itself whenever the constraints change.
>>> from fipy import *
>>> m = Grid2D(nx=2, ny=2) >>> x, y = m.cellCenters >>> v0 = CellVariable(mesh=m) >>> v0.constrain(1., where=m.facesLeft) >>> print(v0.faceValue.constraintMask) [False False False False False False True False False True False False] >>> print(v0.faceValue) [ 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0.] >>> v0.constrain(3., where=m.facesRight) >>> print(v0.faceValue.constraintMask) [False False False False False False True False True True False True] >>> print(v0.faceValue) [ 0. 0. 0. 0. 0. 0. 1. 0. 3. 1. 0. 3.] >>> v1 = CellVariable(mesh=m) >>> v1.constrain(1., where=(x < 1) & (y < 1)) >>> print(v1.constraintMask) [ True False False False] >>> print(v1) [ 1. 0. 0. 0.] >>> v1.constrain(3., where=(x > 1) & (y > 1)) >>> print(v1.constraintMask) [ True False False True] >>> print(v1) [ 1. 0. 0. 3.]
- copy()¶
Make an duplicate of the Variable
>>> a = Variable(value=3) >>> b = a.copy() >>> b Variable(value=array(3))
The duplicate will not reflect changes made to the original
>>> a.setValue(5) >>> b Variable(value=array(3))
Check that this works for arrays.
>>> a = Variable(value=numerix.array((0, 1, 2))) >>> b = a.copy() >>> b Variable(value=array([0, 1, 2])) >>> a[1] = 3 >>> b Variable(value=array([0, 1, 2]))
- property divergence¶
the divergence of self, \(\vec{u}\),
\[\nabla\cdot\vec{u} \approx \frac{\sum_f (\vec{u}\cdot\hat{n})_f A_f}{V_P}\]- Returns:
divergence – one rank lower than self
- Return type:
Examples
>>> from fipy.meshes import Grid2D >>> from fipy.variables.cellVariable import CellVariable >>> mesh = Grid2D(nx=3, ny=2) >>> from builtins import range >>> var = CellVariable(mesh=mesh, value=list(range(3*2))) >>> print(var.faceGrad.divergence) [ 4. 3. 2. -2. -3. -4.]
- dot(other, opShape=None, operatorClass=None)¶
Return the mesh-element–by–mesh-element (cell-by-cell, face-by-face, etc.) scalar product
\[ext{self} \cdot ext{other}\]Both self and other can be of arbitrary rank, and other does not need to be a
MeshVariable
.
- property dtype¶
Returns the Numpy dtype of the underlying array.
>>> issubclass(Variable(1).dtype.type, numerix.integer) True >>> issubclass(Variable(1.).dtype.type, numerix.floating) True >>> issubclass(Variable((1, 1.)).dtype.type, numerix.floating) True
- inBaseUnits()¶
Return the value of the Variable with all units reduced to their base SI elements.
>>> e = Variable(value="2.7 Hartree*Nav") >>> print(e.inBaseUnits().allclose("7088849.01085 kg*m**2/s**2/mol")) 1
- inUnitsOf(*units)¶
Returns one or more Variable objects that express the same physical quantity in different units. The units are specified by strings containing their names. The units must be compatible with the unit of the object. If one unit is specified, the return value is a single Variable.
>>> freeze = Variable('0 degC') >>> print(freeze.inUnitsOf('degF').allclose("32.0 degF")) 1
If several units are specified, the return value is a tuple of Variable instances with with one element per unit such that the sum of all quantities in the tuple equals the the original quantity and all the values except for the last one are integers. This is used to convert to irregular unit systems like hour/minute/second. The original object will not be changed.
>>> t = Variable(value=314159., unit='s') >>> from builtins import zip >>> print(numerix.allclose([e.allclose(v) for (e, v) in zip(t.inUnitsOf('d', 'h', 'min', 's'), ... ['3.0 d', '15.0 h', '15.0 min', '59.0 s'])], ... True)) 1
- property mag¶
The magnitude of the
Variable
, e.g., \(|\vec{\psi}| = \sqrt{\vec{\psi}\cdot\vec{\psi}}\).
- max(axis=None)¶
Return the maximum along a given axis.
- min(axis=None)¶
>>> from fipy import Grid2D, CellVariable >>> mesh = Grid2D(nx=5, ny=5) >>> x, y = mesh.cellCenters >>> v = CellVariable(mesh=mesh, value=x*y) >>> print(v.min()) 0.25
- rdot(other, opShape=None, operatorClass=None)¶
Return the mesh-element–by–mesh-element (cell-by-cell, face-by-face, etc.) scalar product
\[ext{other} \cdot ext{self}\]Both self and other can be of arbitrary rank, and other does not need to be a
MeshVariable
.
- release(constraint)¶
Remove constraint from self
>>> v = Variable((0, 1, 2, 3)) >>> v.constrain(2, numerix.array((True, False, False, False))) >>> v[:] = 10 >>> from fipy.boundaryConditions.constraint import Constraint >>> c1 = Constraint(5, numerix.array((False, False, True, False))) >>> v.constrain(c1) >>> v[:] = 6 >>> v.constrain(8) >>> v[:] = 10 >>> del v.constraints[2] >>> v.release(constraint=c1) >>> print(v) [ 2 10 10 10]
- setValue(value, unit=None, where=None)¶
Set the value of the Variable. Can take a masked array.
>>> a = Variable((1, 2, 3)) >>> a.setValue(5, where=(1, 0, 1)) >>> print(a) [5 2 5]
>>> b = Variable((4, 5, 6)) >>> a.setValue(b, where=(1, 0, 1)) >>> print(a) [4 2 6] >>> print(b) [4 5 6] >>> a.value = 3 >>> print(a) [3 3 3]
>>> b = numerix.array((3, 4, 5)) >>> a.value = b >>> a[:] = 1 >>> print(b) [3 4 5]
>>> a.setValue((4, 5, 6), where=(1, 0)) Traceback (most recent call last): .... ValueError: shape mismatch: objects cannot be broadcast to a single shape
- property shape¶
>>> from fipy.meshes import Grid2D >>> from fipy.variables.cellVariable import CellVariable >>> mesh = Grid2D(nx=2, ny=3) >>> var = CellVariable(mesh=mesh) >>> print(numerix.allequal(var.shape, (6,))) True >>> print(numerix.allequal(var.arithmeticFaceValue.shape, (17,))) True >>> print(numerix.allequal(var.grad.shape, (2, 6))) True >>> print(numerix.allequal(var.faceGrad.shape, (2, 17))) True
Have to account for zero length arrays
>>> from fipy import Grid1D >>> m = Grid1D(nx=0) >>> v = CellVariable(mesh=m, elementshape=(2,)) >>> numerix.allequal((v * 1).shape, (2, 0)) True
- std(axis=None, **kwargs)¶
Evaluate standard deviation of all the elements of a
MeshVariable
.Adapted from http://mpitutorial.com/tutorials/mpi-reduce-and-allreduce/
>>> import fipy as fp >>> mesh = fp.Grid2D(nx=2, ny=2, dx=2., dy=5.) >>> var = fp.CellVariable(value=(1., 2., 3., 4.), mesh=mesh) >>> print((var.std()**2).allclose(1.25)) True
- property unit¶
Return the unit object of self.
>>> Variable(value="1 m").unit <PhysicalUnit m>
- property value¶
“Evaluate” the Variable and return its value (longhand)
>>> a = Variable(value=3) >>> print(a.value) 3 >>> b = a + 4 >>> b (Variable(value=array(3)) + 4) >>> b.value 7