This page contains the variables Package documentation.
Bases: fipy.variables.noiseVariable.NoiseVariable
Represents a beta distribution of random numbers with the probability distribution
with a shape parameter , a rate parameter , and .
We generate noise on a uniform cartesian mesh
>>> from fipy.variables.variable import Variable
>>> alpha = Variable()
>>> beta = Variable()
>>> from fipy.meshes.grid2D import Grid2D
>>> noise = BetaNoiseVariable(mesh = Grid2D(nx = 100, ny = 100), alpha = alpha, beta = beta)
We histogram the root-volume-weighted noise distribution
>>> from fipy.variables.histogramVariable import HistogramVariable
>>> histogram = HistogramVariable(distribution = noise, dx = 0.01, nx = 100)
and compare to a Gaussian distribution
>>> from fipy.variables.cellVariable import CellVariable
>>> betadist = CellVariable(mesh = histogram.getMesh())
>>> x = histogram.getMesh().getCellCenters()[0]
>>> if __name__ == '__main__':
... from fipy import Viewer
... viewer = Viewer(vars=noise, datamin=0, datamax=1)
... histoplot = Viewer(vars=(histogram, betadist),
... datamin=0, datamax=1.5)
>>> from fipy.tools.numerix import arange, exp
>>> from scipy.special import gamma as Gamma
>>> for a in arange(0.5,5,0.5):
... alpha.setValue(a)
... for b in arange(0.5,5,0.5):
... beta.setValue(b)
... betadist.setValue((Gamma(alpha + beta) / (Gamma(alpha) * Gamma(beta)))
... * x**(alpha - 1) * (1 - x)**(beta - 1))
... if __name__ == '__main__':
... import sys
... print >>sys.stderr, "alpha: %g, beta: %g" % (alpha, beta)
... viewer.plot()
... histoplot.plot()
>>> print abs(noise.getFaceGrad().getDivergence().getCellVolumeAverage()) < 5e-15
1
Parameters : |
|
---|
Bases: fipy.variables.meshVariable._MeshVariable
Represents the field of values of a variable on a Mesh.
A CellVariable can be pickled to persistent storage (disk) for later use:
>>> from fipy.meshes.grid2D import Grid2D
>>> mesh = Grid2D(dx = 1., dy = 1., nx = 10, ny = 10)
>>> var = CellVariable(mesh = mesh, value = 1., hasOld = 1, name = 'test')
>>> x, y = mesh.getCellCenters()
>>> var.setValue(x * y)
>>> from fipy.tools import dump
>>> (f, filename) = dump.write(var, extension = '.gz')
>>> unPickledVar = dump.read(filename, f)
>>> print var.allclose(unPickledVar, atol = 1e-10, rtol = 1e-10)
1
Returns a FaceVariable whose value corresponds to the arithmetic interpolation of the adjacent cells:
>>> from fipy.meshes.grid1D import Grid1D
>>> from fipy import numerix
>>> mesh = Grid1D(dx = (1., 1.))
>>> L = 1
>>> R = 2
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (0.5 / 1.) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (2., 4.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (1.0 / 3.0) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (10., 100.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (5.0 / 55.0) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
Return the cell-volume-weighted average of the CellVariable:
>>> from fipy.meshes.grid2D import Grid2D
>>> from fipy.variables.cellVariable import CellVariable
>>> mesh = Grid2D(nx = 3, ny = 1, dx = .5, dy = .1)
>>> var = CellVariable(value = (1, 2, 6), mesh = mesh)
>>> print var.getCellVolumeAverage()
3.0
Return as a rank-1 FaceVariable using differencing for the normal direction(second-order gradient).
Return as a rank-1 FaceVariable using averaging for the normal direction(second-order gradient)
Returns a FaceVariable whose value corresponds to the arithmetic interpolation of the adjacent cells:
>>> from fipy.meshes.grid1D import Grid1D
>>> from fipy import numerix
>>> mesh = Grid1D(dx = (1., 1.))
>>> L = 1
>>> R = 2
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (0.5 / 1.) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (2., 4.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (1.0 / 3.0) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (10., 100.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getArithmeticFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = (R - L) * (5.0 / 55.0) + L
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
Return as a rank-1 CellVariable (first-order gradient).
Concatenate and return values from all processors
When running on a single processor, the result is identical to getValue().
Return as a rank-1 CellVariable (first-order gradient).
Returns a FaceVariable whose value corresponds to the harmonic interpolation of the adjacent cells:
>>> from fipy.meshes.grid1D import Grid1D
>>> from fipy import numerix
>>> mesh = Grid1D(dx = (1., 1.))
>>> L = 1
>>> R = 2
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getHarmonicFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = L * R / ((R - L) * (0.5 / 1.) + L)
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (2., 4.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getHarmonicFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = L * R / ((R - L) * (1.0 / 3.0) + L)
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
>>> mesh = Grid1D(dx = (10., 100.))
>>> var = CellVariable(mesh = mesh, value = (L, R))
>>> faceValue = var.getHarmonicFaceValue()[mesh.getInteriorFaces().getValue()]
>>> answer = L * R / ((R - L) * (5.0 / 55.0) + L)
>>> print numerix.allclose(faceValue, answer, atol = 1e-10, rtol = 1e-10)
True
Return , which is determined by solving for in the following matrix equation,
The matrix equation is derived by minimizing the following least squares sum,
Tests
>>> from fipy import Grid2D
>>> m = Grid2D(nx=2, ny=2, dx=0.1, dy=2.0)
>>> print numerix.allclose(CellVariable(mesh=m, value=(0,1,3,6)).getLeastSquaresGrad().getGlobalValue(), \
... [[8.0, 8.0, 24.0, 24.0],
... [1.2, 2.0, 1.2, 2.0]])
True
>>> from fipy import Grid1D
>>> print numerix.allclose(CellVariable(mesh=Grid1D(dx=(2.0, 1.0, 0.5)),
... value=(0, 1, 2)).getLeastSquaresGrad().getGlobalValue(), [[0.461538461538, 0.8, 1.2]])
True
Returns a FaceVariable with a value that is the minimum of the absolute values of the adjacent cells. If the values are of opposite sign then the result is zero:
>>> from fipy import *
>>> print CellVariable(mesh=Grid1D(nx=2), value=(1, 2)).getMinmodFaceValue()
[1 1 2]
>>> print CellVariable(mesh=Grid1D(nx=2), value=(-1, -2)).getMinmodFaceValue()
[-1 -1 -2]
>>> print CellVariable(mesh=Grid1D(nx=2), value=(-1, 2)).getMinmodFaceValue()
[-1 0 2]
Return the values of the CellVariable from the previous solution sweep.
Combinations of CellVariable’s should also return old values.
>>> from fipy.meshes.grid1D import Grid1D
>>> mesh = Grid1D(nx = 2)
>>> from fipy.variables.cellVariable import CellVariable
>>> var1 = CellVariable(mesh = mesh, value = (2, 3), hasOld = 1)
>>> var2 = CellVariable(mesh = mesh, value = (3, 4))
>>> v = var1 * var2
>>> print v
[ 6 12]
>>> var1.setValue((3,2))
>>> print v
[9 8]
>>> print v.getOld()
[ 6 12]
The following small test is to correct for a bug when the operator does not just use variables.
>>> v1 = var1 * 3
>>> print v1
[9 6]
>>> print v1.getOld()
[6 9]
Set the values of the previous solution sweep to the current values.
Bases: fipy.variables.noiseVariable.NoiseVariable
Represents an exponential distribution of random numbers with the probability distribution
with a mean parameter .
We generate noise on a uniform cartesian mesh
>>> from fipy.variables.variable import Variable
>>> mean = Variable()
>>> from fipy.meshes.grid2D import Grid2D
>>> noise = ExponentialNoiseVariable(mesh = Grid2D(nx = 100, ny = 100), mean = mean)
We histogram the root-volume-weighted noise distribution
>>> from fipy.variables.histogramVariable import HistogramVariable
>>> histogram = HistogramVariable(distribution = noise, dx = 0.1, nx = 100)
and compare to a Gaussian distribution
>>> from fipy.variables.cellVariable import CellVariable
>>> expdist = CellVariable(mesh = histogram.getMesh())
>>> x = histogram.getMesh().getCellCenters()[0]
>>> if __name__ == '__main__':
... from fipy import Viewer
... viewer = Viewer(vars=noise, datamin=0, datamax=5)
... histoplot = Viewer(vars=(histogram, expdist),
... datamin=0, datamax=1.5)
>>> from fipy.tools.numerix import arange, exp
>>> for mu in arange(0.5,3,0.5):
... mean.setValue(mu)
... expdist.setValue((1/mean)*exp(-x/mean))
... if __name__ == '__main__':
... import sys
... print >>sys.stderr, "mean: %g" % mean
... viewer.plot()
... histoplot.plot()
>>> print abs(noise.getFaceGrad().getDivergence().getCellVolumeAverage()) < 5e-15
1
Parameters : |
|
---|
Bases: fipy.variables.meshVariable._MeshVariable
Parameters : |
|
---|
>>> from fipy.meshes.grid2D import Grid2D
>>> from fipy.variables.cellVariable import CellVariable
>>> mesh = Grid2D(nx=3, ny=2)
>>> var = CellVariable(mesh=mesh, value=range(3*2))
>>> print var.getFaceGrad().getDivergence()
[ 4. 3. 2. -2. -3. -4.]
Bases: fipy.variables.noiseVariable.NoiseVariable
Represents a gamma distribution of random numbers with the probability distribution
with a shape parameter , a rate parameter , and .
We generate noise on a uniform cartesian mesh
>>> from fipy.variables.variable import Variable
>>> alpha = Variable()
>>> beta = Variable()
>>> from fipy.meshes.grid2D import Grid2D
>>> noise = GammaNoiseVariable(mesh = Grid2D(nx = 100, ny = 100), shape = alpha, rate = beta)
We histogram the root-volume-weighted noise distribution
>>> from fipy.variables.histogramVariable import HistogramVariable
>>> histogram = HistogramVariable(distribution = noise, dx = 0.1, nx = 300)
and compare to a Gaussian distribution
>>> from fipy.variables.cellVariable import CellVariable
>>> gammadist = CellVariable(mesh = histogram.getMesh())
>>> x = histogram.getMesh().getCellCenters()[0]
>>> if __name__ == '__main__':
... from fipy import Viewer
... viewer = Viewer(vars=noise, datamin=0, datamax=30)
... histoplot = Viewer(vars=(histogram, gammadist),
... datamin=0, datamax=1)
>>> from fipy.tools.numerix import arange, exp
>>> from scipy.special import gamma as Gamma
>>> for shape in arange(1,8,1):
... alpha.setValue(shape)
... for rate in arange(0.5,2.5,0.5):
... beta.setValue(rate)
... gammadist.setValue(x**(alpha - 1) * (beta**alpha * exp(-beta * x)) / Gamma(alpha))
... if __name__ == '__main__':
... import sys
... print >>sys.stderr, "alpha: %g, beta: %g" % (alpha, beta)
... viewer.plot()
... histoplot.plot()
>>> print abs(noise.getFaceGrad().getDivergence().getCellVolumeAverage()) < 5e-15
1
Parameters : |
|
---|
Bases: fipy.variables.noiseVariable.NoiseVariable
Represents a normal (Gaussian) distribution of random numbers with mean and variance , which has the probability distribution
For example, the variance of thermal noise that is uncorrelated in space and time is often expressed as
which can be obtained with:
sigmaSqrd = Mobility * kBoltzmann * Temperature / (mesh.getCellVolumes() * timeStep)
GaussianNoiseVariable(mesh = mesh, variance = sigmaSqrd)
Note
If the time step will change as the simulation progresses, either through use of an adaptive iterator or by making manual changes at different stages, remember to declare timeStep as a Variable and to change its value with its setValue() method.
>>> import sys
>>> from fipy.tools.numerix import *
>>> mean = 0.
>>> variance = 4.
We generate noise on a non-uniform cartesian mesh with cell dimensions of and .
>>> from fipy.meshes.grid2D import Grid2D
>>> mesh = Grid2D(dx = arange(0.1, 5., 0.1)**2, dy = arange(0.1, 3., 0.1)**3)
>>> from fipy.variables.cellVariable import CellVariable
>>> volumes = CellVariable(mesh=mesh,value=mesh.getCellVolumes())
>>> noise = GaussianNoiseVariable(mesh = mesh, mean = mean,
... variance = variance / volumes)
We histogram the root-volume-weighted noise distribution
>>> from fipy.variables.histogramVariable import HistogramVariable
>>> histogram = HistogramVariable(distribution = noise * sqrt(volumes),
... dx = 0.1, nx = 600, offset = -30)
and compare to a Gaussian distribution
>>> gauss = CellVariable(mesh = histogram.getMesh())
>>> x = histogram.getMesh().getCellCenters()[0]
>>> gauss.setValue((1/(sqrt(variance * 2 * pi))) * exp(-(x - mean)**2 / (2 * variance)))
>>> if __name__ == '__main__':
... from fipy import viewers
... viewer = Viewer(vars=noise,
... datamin=-5, datamax=5)
... histoplot = Viewer(vars=(histogram, gauss))
>>> for i in range(10):
... noise.scramble()
... if __name__ == '__main__':
... viewer.plot()
... histoplot.plot()
>>> print abs(noise.getFaceGrad().getDivergence().getCellVolumeAverage()) < 5e-15
1
Note that the noise exhibits larger amplitude in the small cells than in the large ones
but that the root-volume-weighted histogram is Gaussian.
Parameters : |
|
---|
Bases: fipy.variables.cellVariable.CellVariable
Produces a histogram of the values of the supplied distribution.
Parameters : |
|
---|
Bases: fipy.variables.cellVariable.CellVariable
The ModularVariable defines a variable that exisits on the circle between and
The following examples show how ModularVariable works. When subtracting the answer wraps back around the circle.
>>> from fipy.meshes.grid1D import Grid1D
>>> mesh = Grid1D(nx = 2)
>>> from fipy.tools import numerix
>>> pi = numerix.pi
>>> v1 = ModularVariable(mesh = mesh, value = (2*pi/3, -2*pi/3))
>>> v2 = ModularVariable(mesh = mesh, value = -2*pi/3)
>>> print numerix.allclose(v2 - v1, (2*pi/3, 0))
1
Obtaining the arithmetic face value.
>>> print numerix.allclose(v1.getArithmeticFaceValue(), (2*pi/3, pi, -2*pi/3))
1
Obtaining the gradient.
>>> print numerix.allclose(v1.getGrad(), ((pi/3, pi/3),))
1
Obtaining the gradient at the faces.
>>> print numerix.allclose(v1.getFaceGrad(), ((0, 2*pi/3, 0),))
1
Obtaining the gradient at the faces but without modular arithmetic.
>>> print numerix.allclose(v1.getFaceGradNoMod(), ((0, -4*pi/3, 0),))
1
Returns a FaceVariable whose value corresponds to the arithmetic interpolation of the adjacent cells:
Adjusted for a ModularVariable
Return as a rank-1 FaceVariable (second-order gradient). Adjusted for a ModularVariable
Return as a rank-1 FaceVariable (second-order gradient). Not adjusted for a ModularVariable
Return as a rank-1 CellVariable (first-order gradient). Adjusted for a ModularVariable
Set the values of the previous solution sweep to the current values. Test case due to bug.
>>> from fipy.meshes.grid1D import Grid1D
>>> mesh = Grid1D(nx = 1)
>>> var = ModularVariable(mesh=mesh, value=1., hasOld=1)
>>> var.updateOld()
>>> var[:] = 2
>>> answer = CellVariable(mesh=mesh, value=1.)
>>> print var.getOld().allclose(answer)
True
Bases: fipy.variables.cellVariable.CellVariable
Attention
This class is abstract. Always create one of its subclasses.
A generic base class for sources of noise distributed over the cells of a mesh.
In the event that the noise should be conserved, use:
<Specific>NoiseVariable(...).getFaceGrad().getDivergence()
The seed() and get_seed() functions of the fipy.tools.numerix.random module can be set and query the random number generated used by all NoiseVariable objects.
Copy the value of the NoiseVariable to a static CellVariable.
Generate a new random distribution.
Bases: fipy.variables.cellToFaceVariable._CellToFaceVariable
Bases: fipy.variables.noiseVariable.NoiseVariable
Represents a uniform distribution of random numbers.
We generate noise on a uniform cartesian mesh
>>> from fipy.meshes.grid2D import Grid2D
>>> noise = UniformNoiseVariable(mesh=Grid2D(nx=100, ny=100))
and histogram the noise
>>> from fipy.variables.histogramVariable import HistogramVariable
>>> histogram = HistogramVariable(distribution=noise, dx=0.01, nx=120, offset=-.1)
>>> if __name__ == '__main__':
... from fipy import Viewer
... viewer = Viewer(vars=noise,
... datamin=0, datamax=1)
... histoplot = Viewer(vars=histogram)
>>> for i in range(10):
... noise.scramble()
... if __name__ == '__main__':
... viewer.plot()
... histoplot.plot()
Parameters : |
|
---|
Bases: object
Lazily evaluated quantity with units.
Using a Variable in a mathematical expression will create an automatic dependency Variable, 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 dependent Variable objects
>>> a.setValue(5)
>>> b
(Variable(value=array(5)) * 4)
>>> 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)))
Variable(value=PhysicalField(array([[3, 3],
[3, 3],
[3, 3]]),'m'))
Parameters : |
|
---|
>>> print Variable(value=(0, 0, 1, 1)).all()
0
>>> print Variable(value=(1, 1, 1, 1)).all()
1
>>> 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))
False
>>> print Variable(value=0).any()
0
>>> print Variable(value=(0, 0, 1, 1)).any()
1
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]))
>>> Variable(value=3).shape
()
>>> Variable(value=(3,)).shape
(1,)
>>> Variable(value=(3,4)).shape
(2,)
>>> Variable(value="3 m").shape
()
>>> Variable(value=(3,), unit="m").shape
(1,)
>>> Variable(value=(3,4), unit="m").shape
(2,)
Return the unit object of self.
>>> Variable(value="1 m").getUnit()
<PhysicalUnit m>
“Evaluate” the Variable and return its value (longhand)
>>> a = Variable(value=3)
>>> print a.getValue()
3
>>> b = a + 4
>>> b
(Variable(value=array(3)) + 4)
>>> b.getValue()
7
Returns the Numpy sctype of the underlying array.
>>> Variable(1).getsctype() == numerix.NUMERIX.obj2sctype(numerix.array(1))
True
>>> Variable(1.).getsctype() == numerix.NUMERIX.obj2sctype(numerix.array(1.))
True
>>> Variable((1,1.)).getsctype() == numerix.NUMERIX.obj2sctype(numerix.array((1., 1.)))
True
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()
7088849.01085 kg*m**2/s**2/mol
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')
32.0 degF
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')
>>> [str(element) for element in t.inUnitsOf('d','h','min','s')]
['3.0 d', '15.0 h', '15.0 min', '59.0 s']
Change the unit object of self to unit
>>> a = Variable(value="1 m")
>>> a.setUnit("m**2/s")
>>> print a
1.0 m**2/s
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.setValue(3)
>>> print a
[3 3 3]
>>> b = numerix.array((3,4,5))
>>> a.setValue(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
Tuple of array dimensions.
>>> from fipy.meshes.grid1D import Grid1D
>>> mesh= Grid1D(nx=3)
>>> from fipy.variables.cellVariable import CellVariable
>>> var = CellVariable(mesh=mesh, value=((0., 2., 3.),), rank=1)
>>> print (var.dot(var)).sqrt()
[ 0. 2. 3.]
It is not longer needed.