fipy.variables.variable¶
Classes
|
Lazily evaluated quantity with units. |
- class fipy.variables.variable.Variable(*args, **kwds)¶
Bases:
object
Lazily evaluated quantity with units.
Using a
Variable
in a mathematical expression will create an automatic dependencyVariable
, e.g.,>>> a = Variable(value=3) >>> b = 4 * a >>> b (Variable(value=array(3)) * 4) >>> b() 12
Changes to the value of a
Variable
will automatically trigger changes in any dependentVariable
objects>>> a.setValue(5) >>> b (Variable(value=array(5)) * 4) >>> print(b()) 20
Create a Variable.
>>> Variable(value=3) Variable(value=array(3)) >>> Variable(value=3, unit="m") Variable(value=PhysicalField(3,'m')) >>> Variable(value=3, unit="m", array=numerix.zeros((3, 2), ... dtype=int)) Variable(value=PhysicalField(array([[3, 3], [3, 3], [3, 3]]),'m'))
- Parameters:
value (int or float or array_like) –
unit (str or PhysicalUnit) – The physical units of the variable
array (ndarray, optional) – The storage array for the Variable
name (str) – The user-readable name of the Variable
cached (bool) – whether to cache or always recalculate the value
- __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
the Variable 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
- 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 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)¶
Return the minimum along a given axis.
- 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¶
Tuple of array dimensions.
>>> Variable(value=3).shape () >>> numerix.allequal(Variable(value=(3,)).shape, (1,)) True >>> numerix.allequal(Variable(value=(3, 4)).shape, (2,)) True
>>> Variable(value="3 m").shape () >>> numerix.allequal(Variable(value=(3,), unit="m").shape, (1,)) True >>> numerix.allequal(Variable(value=(3, 4), unit="m").shape, (2,)) True
- std(axis=None, **kwargs)¶
Return the standard deviation along a given axis.
- 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