# fipy.tools.dimensions package¶

## fipy.tools.dimensions.physicalField module¶

Physical quantities with units.

This module derives from Konrad Hinsen’s PhysicalQuantity <http://dirac.cnrs-orleans.fr/ScientificPython/>.

This module provides a data type that represents a physical quantity together with its unit. It is possible to add and subtract these quantities if the units are compatible, and a quantity can be converted to another compatible unit. Multiplication, subtraction, and raising to integer powers is allowed without restriction, and the result will have the correct unit. A quantity can be raised to a non-integer power only if the result can be represented by integer powers of the base units.

The values of physical constants are taken from the 2002 recommended values from CODATA. Other conversion factors (e.g. for British units) come from Appendix B of NIST Special Publication 811.

Warning

We can’t guarantee for the correctness of all entries in the unit table, so use this at your own risk!

Base SI units:

m, kg, s, A, K, mol, cd, rad, sr


SI prefixes:

 Y = 1e+24
Z = 1e+21
E = 1e+18
P = 1e+15
T = 1e+12
G = 1e+09
M = 1e+06
k = 1000
h = 100
da = 10
d = 0.1
c = 0.01
m = 0.001
mu = 1e-06
n = 1e-09
p = 1e-12
f = 1e-15
a = 1e-18
z = 1e-21
y = 1e-24


Units derived from SI (accepting SI prefixes):

  1 Bq = 1 1/s
1 C = 1 s*A
1 degC = 1 K
1 F = 1 s**4*A**2/m**2/kg
1 Gy = 1 m**2/s**2
1 H = 1 m**2*kg/s**2/A**2
1 Hz = 1 1/s
1 J = 1 m**2*kg/s**2
1 lm = 1 cd*sr
1 lx = 1 cd*sr/m**2
1 N = 1 m*kg/s**2
1 ohm = 1 m**2*kg/s**3/A**2
1 Pa = 1 kg/m/s**2
1 S = 1 s**3*A**2/m**2/kg
1 Sv = 1 m**2/s**2
1 T = 1 kg/s**2/A
1 V = 1 m**2*kg/s**3/A
1 W = 1 m**2*kg/s**3
1 Wb = 1 m**2*kg/s**2/A


Other units that accept SI prefixes:

1 eV = 1.60217653e-19 m**2*kg/s**2


  1 acres = 4046.8564224 m**2
1 amu = 1.6605402e-27 kg
1 Ang = 1e-10 m
1 atm = 101325.0 kg/m/s**2
1 b = 1e-28 m
1 bar = 100000.0 kg/m/s**2
1 Bohr = 5.291772081145378e-11 m
1 Btui = 1055.05585262 m**2*kg/s**2
1 c = 299792458.0 m/s
1 cal = 4.184 m**2*kg/s**2
1 cali = 4.1868 m**2*kg/s**2
1 cl = 1.0000000000000003e-05 m**3
1 cup = 0.00023658825600000004 m**3
1 d = 86400.0 s
1 degF = 0.5555555555555556 K
1 degR = 0.5555555555555556 K
1 dl = 0.00010000000000000003 m**3
1 dyn = 1e-05 m*kg/s**2
1 e = 1.60217653e-19 s*A
1 eps0 = 8.85418781762039e-12 s**4*A**2/m**3/kg
1 erg = 1e-07 m**2*kg/s**2
1 floz = 2.9573532000000005e-05 m**3
1 ft = 0.3048 m
1 g = 0.001 kg
1 galUK = 0.004546090000000002 m**3
1 galUS = 0.0037854120960000006 m**3
1 gn = 9.80665 m/s**2
1 Grav = 6.6742e-11 m**3/kg/s**2
1 h = 3600.0 s
1 ha = 10000.0 m**2
1 Hartree = 4.35974417680088e-18 m**2*kg/s**2
1 hbar = 1.0545716823644548e-34 m**2*kg/s
1 hpEl = 746.0 m**2*kg/s**3
1 hplanck = 6.6260693e-34 m**2*kg/s
1 hpUK = 745.7 m**2*kg/s**3
1 inch = 0.025400000000000002 m
1 invcm = 1.9864456023253395e-23 m**2*kg/s**2
1 kB = 1.3806505e-23 m**2*kg/s**2/K
1 kcal = 4184.0 m**2*kg/s**2
1 kcali = 4186.8 m**2*kg/s**2
1 Ken = 1.3806505e-23 m**2*kg/s**2
1 l = 0.0010000000000000002 m**3
1 lb = 0.45359237 kg
1 lyr = 9460730472580800.0 m
1 me = 9.1093826e-31 kg
1 mi = 1609.344 m
1 min = 60.0 s
1 ml = 1.0000000000000002e-06 m**3
1 mp = 1.67262171e-27 kg
1 mu0 = 1.2566370614359173e-06 m*kg/s**2/A**2
1 Nav = 6.0221415e+23 1/mol
1 nmi = 1852.0 m
1 oz = 0.028349523125 kg
1 psi = 6894.75729316836 kg/m/s**2
1 pt = 0.0004731765120000001 m**3
1 qt = 0.0009463530240000002 m**3
1 tbsp = 1.4786766000000002e-05 m**3
1 ton = 907.18474 kg
1 Torr = 133.32236842105263 kg/m/s**2
1 tsp = 4.9289220000000005e-06 m**3
1 wk = 604800.0 s
1 yd = 0.9144000000000001 m
1 yr = 31536000.0 s
1 yrJul = 31557600.0 s
1 yrSid = 31558152.959999997 s

class fipy.tools.dimensions.physicalField.PhysicalField(value, unit=None, array=None)

Bases: object

Physical field or quantity with units

Physical Fields can be constructed in one of two ways:

• PhysicalField(*value*, *unit*), where *value* is a number of arbitrary type and *unit* is a string containing the unit name

>>> print(PhysicalField(value = 10., unit = 'm'))
10.0 m

• PhysicalField(*string*), where *string* contains both the value and the unit. This form is provided to make interactive use more convenient

>>> print(PhysicalField(value = "10. m"))
10.0 m


Dimensionless quantities, with a unit of 1, can be specified in several ways

>>> print(PhysicalField(value = "1"))
1.0 1
>>> print(PhysicalField(value = 2., unit = " "))
2.0 1
>>> print(PhysicalField(value = 2.))
2.0 1


Physical arrays are also possible (and are the reason this code was adapted from Konrad Hinsen’s original PhysicalQuantity). The value can be a Numeric array:

>>> a = numerix.array(((3., 4.), (5., 6.)))
>>> print(PhysicalField(value = a, unit = "m"))
[[ 3.  4.]
[ 5.  6.]] m


or a tuple:

>>> print(PhysicalField(value = ((3., 4.), (5., 6.)), unit = "m"))
[[ 3.  4.]
[ 5.  6.]] m


or as a single value to be applied to every element of a supplied array:

>>> print(PhysicalField(value = 2., unit = "m", array = a))
[[ 2.  2.]
[ 2.  2.]] m


Every element in an array has the same unit, which is stored only once for the whole array.

__abs__()

Return the absolute value of the quantity. The unit is unchanged.

>>> print(abs(PhysicalField(((3., -2.), (-1., 4.)), 'm')))
[[ 3.  2.]
[ 1.  4.]] m


Add two physical quantities, so long as their units are compatible. The unit of the result is the unit of the first operand.

>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'm'))
10.01 km
>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'J'))
Traceback (most recent call last):
...
TypeError: Incompatible units

__array__(t=None)

Return a dimensionless PhysicalField as a Numeric array.

>>> print(numerix.array(PhysicalField(((2., 3.), (4., 5.)), "m/m")))
[[ 2.  3.]
[ 4.  5.]]


As a special case, fields with angular units are converted to base units (radians) and then assumed dimensionless.

>>> print(numerix.array(PhysicalField(((2., 3.), (4., 5.)), "deg")))
[[ 0.03490659  0.05235988]
[ 0.06981317  0.08726646]]


If the array is not dimensionless, the numerical value in its base units is returned.

>>> numerix.array(PhysicalField(((2., 3.), (4., 5.)), "mm"))
array([[ 0.002,  0.003],
[ 0.004,  0.005]])

__array_priority__ = 100.0
__array_wrap__(arr, context=None)

Required to prevent numpy not calling the reverse binary operations. Both the following tests are examples ufuncs.

>>> from fipy.tools.dimensions.physicalField import PhysicalField
>>> print(type(numerix.array([1.0, 2.0]) * PhysicalField([1.0, 2.0], unit="m")))
<class 'fipy.tools.dimensions.physicalField.PhysicalField'>

>>> print(type(numerix.array([1.0, 2.0]) * PhysicalField([1.0, 2.0])))
<class 'fipy.tools.dimensions.physicalField.PhysicalField'>

>>> from scipy.special import gamma as Gamma
>>> print(isinstance(Gamma(PhysicalField([1.0, 2.0])), type(numerix.array(1))))
1

__bool__()

Test if the quantity is zero.

Should this only pass if the unit offset is zero?

__div__(other)

Divide two physical quantities. The unit of the result is the unit of the first operand divided by the unit of the second.

>>> print(PhysicalField(10., 'm') / PhysicalField(2., 's'))
5.0 m/s


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units

>>> print((PhysicalField(1., 'inch')
...        / PhysicalField(1., 'mm')))
25.4

__eq__(other)

Return self==value.

__float__()

Return a dimensionless PhysicalField quantity as a float.

>>> float(PhysicalField("2. m/m"))
2.0


As a special case, quantities with angular units are converted to base units (radians) and then assumed dimensionless.

>>> print(numerix.round(float(PhysicalField("2. deg")), 6))
0.034907


If the quantity is not dimensionless, the conversion fails.

>>> float(PhysicalField("2. m"))
Traceback (most recent call last):
...
TypeError: Not possible to convert a PhysicalField with dimensions to float


Just as a Numeric array cannot be cast to float, neither can PhysicalField arrays

>>> float(PhysicalField(((2., 3.), (4., 5.)), "m/m"))
Traceback (most recent call last):
...
TypeError: only ...-1 arrays can be converted to Python scalars

__ge__(other)

Return self>=value.

__getitem__(index)

Return the specified element of the array. The unit of the result will be the unit of the array.

>>> a = PhysicalField(((3., 4.), (5., 6.)), "m")
>>> print(a[1, 1])
6.0 m

__gt__(other)

Compare self to other, returning an array of Boolean values corresponding to the test against each element.

>>> a = PhysicalField(((3., 4.), (5., 6.)), "m")
>>> print(numerix.allclose(a > PhysicalField("13 ft"),
...                        [[False, True], [ True, True]]))
True


Appropriately formatted dimensional quantity strings can also be compared.

>>> print(numerix.allclose(a > "13 ft",
...                        [[False, True], [ True, True]]))
True


Arrays are compared element to element

>>> print(numerix.allclose(a > PhysicalField(((3., 13.), (17., 6.)), "ft"),
...                        [[ True, True], [False, True]]))
True


Units must be compatible

>>> print(a > PhysicalField("1 lb"))
Traceback (most recent call last):
...
TypeError: Incompatible units


And so must array dimensions

>>> print(a > PhysicalField(((3., 13., 4.), (17., 6., 2.)), "ft"))
Traceback (most recent call last):
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape

__hash__()

Return hash(self).

__le__(other)

Return self<=value.

__len__()
__lt__(other)

Return self<value.

__mod__(other)

Return the remainder of dividing two physical quantities. The unit of the result is the unit of the first operand divided by the unit of the second.

>>> print(PhysicalField(11., 'm') % PhysicalField(2., 's'))
1.0 m/s

__mul__(other)

Multiply two physical quantities. The unit of the result is the product of the units of the operands.

>>> print(PhysicalField(10., 'N') * PhysicalField(10., 'm') == PhysicalField(100., 'N*m'))
True


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units.

>>> print((PhysicalField(10., 's') * PhysicalField(2., 'Hz')))
20.0

__ne__(other)

Return self!=value.

__neg__()

Return the negative of the quantity. The unit is unchanged.

>>> print(-PhysicalField(((3., -2.), (-1., 4.)), 'm'))
[[-3.  2.]
[ 1. -4.]] m

__nonzero__()

Test if the quantity is zero.

Should this only pass if the unit offset is zero?

__pos__()
__pow__(other)

Raise a PhysicalField to a power. The unit is raised to the same power.

>>> print(PhysicalField(10., 'm')**2)
100.0 m**2


Add two physical quantities, so long as their units are compatible. The unit of the result is the unit of the first operand.

>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'm'))
10.01 km
>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'J'))
Traceback (most recent call last):
...
TypeError: Incompatible units

__rdiv__(other)
__repr__()

Return representation of a physical quantity suitable for re-use

>>> PhysicalField(value = 3., unit = "eV")
PhysicalField(3.0,'eV')

__rmul__(other)

Multiply two physical quantities. The unit of the result is the product of the units of the operands.

>>> print(PhysicalField(10., 'N') * PhysicalField(10., 'm') == PhysicalField(100., 'N*m'))
True


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units.

>>> print((PhysicalField(10., 's') * PhysicalField(2., 'Hz')))
20.0

__rpow__(other)
__rsub__(other)
__rtruediv__(other)
__setitem__(index, value)

Assign the specified element of the array, performing appropriate conversions.

>>> a = PhysicalField(((3., 4.), (5., 6.)), "m")
>>> a[0, 1] = PhysicalField("6 ft")
>>> print(a)
[[ 3.      1.8288]
[ 5.      6.    ]] m
>>> a[1, 0] = PhysicalField("2 min")
Traceback (most recent call last):
...
TypeError: Incompatible units

__str__()

Return human-readable form of a physical quantity

>>> print(PhysicalField(value = 3., unit = "eV"))
3.0 eV

__sub__(other)

Subtract two physical quantities, so long as their units are compatible. The unit of the result is the unit of the first operand.

>>> print(PhysicalField(10., 'km') - PhysicalField(10., 'm'))
9.99 km
>>> print(PhysicalField(10., 'km') - PhysicalField(10., 'J'))
Traceback (most recent call last):
...
TypeError: Incompatible units

__truediv__(other)

Divide two physical quantities. The unit of the result is the unit of the first operand divided by the unit of the second.

>>> print(PhysicalField(10., 'm') / PhysicalField(2., 's'))
5.0 m/s


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units

>>> print((PhysicalField(1., 'inch')
...        / PhysicalField(1., 'mm')))
25.4


Add two physical quantities, so long as their units are compatible. The unit of the result is the unit of the first operand.

>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'm'))
10.01 km
>>> print(PhysicalField(10., 'km') + PhysicalField(10., 'J'))
Traceback (most recent call last):
...
TypeError: Incompatible units

allclose(other, atol=None, rtol=1e-08)

This function tests whether or not self and other are equal subject to the given relative and absolute tolerances. The formula used is:

| self - other | < atol + rtol * | other |


This means essentially that both elements are small compared to atol or their difference divided by other’s value is small compared to rtol.

allequal(other)

This function tests whether or not self and other are exactly equal.

arccos()

Return the inverse cosine of the PhysicalField in radians

>>> print(PhysicalField(0).arccos().allclose("1.57079632679 rad"))
1


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1 m").arccos(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

arccosh()

Return the inverse hyperbolic cosine of the PhysicalField

>>> print(numerix.allclose(PhysicalField(2).arccosh(),
...                        1.31695789692))
1


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1. m").arccosh(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

arcsin()

Return the inverse sine of the PhysicalField in radians

>>> print(PhysicalField(1).arcsin().allclose("1.57079632679 rad"))
1


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1 m").arcsin(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

arctan()

Return the arctangent of the PhysicalField in radians

>>> print(numerix.round(PhysicalField(1).arctan(), 6))
0.785398


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1 m").arctan(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

arctan2(other)

Return the arctangent of self divided by other in radians

>>> print(numerix.round(PhysicalField(2.).arctan2(PhysicalField(5.)), 6))
0.380506


The input PhysicalField objects must be in the same dimensions

>>> print(numerix.round(PhysicalField(2.54, "cm").arctan2(PhysicalField(1., "inch")), 6))
0.785398

>>> print(numerix.round(PhysicalField(2.).arctan2(PhysicalField("5. m")), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

arctanh()

Return the inverse hyperbolic tangent of the PhysicalField

>>> print(PhysicalField(0.5).arctanh())
0.549306144334


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1 m").arctanh(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

ceil()

Return the smallest integer greater than or equal to the PhysicalField.

>>> print(PhysicalField(2.2, "m").ceil())
3.0 m

conjugate()

Return the complex conjugate of the PhysicalField.

>>> print(PhysicalField(2.2 - 3j, "ohm").conjugate() == PhysicalField(2.2 + 3j, "ohm"))
True

convertToUnit(unit)

Changes the unit to unit and adjusts the value such that the combination is equivalent. The new unit is by a string containing its name. The new unit must be compatible with the previous unit of the object.

>>> e = PhysicalField('2.7 Hartree*Nav')
>>> e.convertToUnit('kcal/mol')
>>> print(e)
1694.27557621 kcal/mol

copy()

Make a duplicate.

>>> a = PhysicalField(1, unit = 'inch')
>>> b = a.copy()


The duplicate will not reflect changes made to the original

>>> a.convertToUnit('cm')
>>> print(a)
2.54 cm
>>> print(b)
1 inch


Likewise for arrays

>>> a = PhysicalField(numerix.array((0, 1, 2)), unit  = 'm')
>>> b = a.copy()
>>> a[0] = 3
>>> print(a)
[3 1 2] m
>>> print(b)
[0 1 2] m

cos()

Return the cosine of the PhysicalField

>>> print(numerix.round(PhysicalField(2*numerix.pi/6, "rad").cos(), 6))
0.5
>>> print(numerix.round(PhysicalField(60., "deg").cos(), 6))
0.5


The units of the PhysicalField must be an angle

>>> PhysicalField(60., "m").cos()
Traceback (most recent call last):
...
TypeError: Incompatible units

cosh()

Return the hyperbolic cosine of the PhysicalField

>>> PhysicalField(0.).cosh()
1.0


The units of the PhysicalField must be dimensionless

>>> PhysicalField(60., "m").cosh()
Traceback (most recent call last):
...
TypeError: Incompatible units

divide(other)

Divide two physical quantities. The unit of the result is the unit of the first operand divided by the unit of the second.

>>> print(PhysicalField(10., 'm') / PhysicalField(2., 's'))
5.0 m/s


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units

>>> print((PhysicalField(1., 'inch')
...        / PhysicalField(1., 'mm')))
25.4

dot(other)

Return the dot product of self with other. The resulting unit is the product of the units of self and other.

>>> v = PhysicalField(((5., 6.), (7., 8.)), "m")
>>> print(PhysicalField(((1., 2.), (3., 4.)), "m").dot(v))
[ 26.  44.] m**2

floor()

Return the largest integer less than or equal to the PhysicalField.

>>> print(PhysicalField(2.2, "m").floor())
2.0 m

getsctype(default=None)

Returns the NumPy sctype of the underlying array.

>>> PhysicalField(1, 'm').getsctype() == numerix.NUMERIX.obj2sctype(numerix.array(1))
True
>>> PhysicalField(1., 'm').getsctype() == numerix.NUMERIX.obj2sctype(numerix.array(1.))
True
>>> PhysicalField((1, 1.), 'm').getsctype() == numerix.NUMERIX.obj2sctype(numerix.array((1., 1.)))
True

inBaseUnits()

Return the quantity with all units reduced to their base SI elements.

>>> e = PhysicalField('2.7 Hartree*Nav')
>>> print(e.inBaseUnits().allclose("7088849.01085 kg*m**2/s**2/mol"))
1

inDimensionless()

Returns the numerical value of a dimensionless quantity.

>>> print(PhysicalField(((2., 3.), (4., 5.))).inDimensionless())
[[ 2.  3.]
[ 4.  5.]]


It’s an error to convert a quantity with units

>>> print(PhysicalField(((2., 3.), (4., 5.)), "m").inDimensionless())
Traceback (most recent call last):
...
TypeError: Incompatible units


Converts an angular quantity to radians and returns the numerical value.

>>> print(PhysicalField(((2., 3.), (4., 5.)), "rad").inRadians())
[[ 2.  3.]
[ 4.  5.]]
>>> print(PhysicalField(((2., 3.), (4., 5.)), "deg").inRadians())
[[ 0.03490659  0.05235988]
[ 0.06981317  0.08726646]]


As a special case, assumes a dimensionless quantity is already in radians.

>>> print(PhysicalField(((2., 3.), (4., 5.))).inRadians())
[[ 2.  3.]
[ 4.  5.]]


It’s an error to convert a quantity with non-angular units

>>> print(PhysicalField(((2., 3.), (4., 5.)), "m").inRadians())
Traceback (most recent call last):
...
TypeError: Incompatible units

inSIUnits()

Return the quantity with all units reduced to SI-compatible elements.

>>> e = PhysicalField('2.7 Hartree*Nav')
>>> print(e.inSIUnits().allclose("7088849.01085 kg*m**2/s**2/mol"))
1

inUnitsOf(*units)

Returns one or more PhysicalField 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 PhysicalField.

>>> freeze = PhysicalField('0 degC')
>>> print(freeze.inUnitsOf('degF').allclose("32.0 degF"))
1


If several units are specified, the return value is a tuple of PhysicalField 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 = PhysicalField(314159., '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

isCompatible(unit)
itemset(value)

Assign the value of a scalar array, performing appropriate conversions.

>>> a = PhysicalField(4., "m")
>>> a.itemset(PhysicalField("6 ft"))
>>> print(a.allclose("1.8288 m"))
1
>>> a = PhysicalField(((3., 4.), (5., 6.)), "m")
>>> try:
...     a.itemset(PhysicalField("6 ft"))
... except IndexError:
...     # NumPy 1.7 has changed the exception type
...     raise ValueError("can only place a scalar for an  array of size 1")
Traceback (most recent call last):
...
ValueError: can only convert an array of size 1 to a Python scalar
>>> a.itemset(PhysicalField("2 min"))
Traceback (most recent call last):
...
TypeError: Incompatible units

property itemsize
log()

Return the natural logarithm of the PhysicalField

>>> print(numerix.round(PhysicalField(10).log(), 6))
2.302585


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1. m").log(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

log10()

Return the base-10 logarithm of the PhysicalField

>>> print(numerix.round(PhysicalField(10.).log10(), 6))
1.0


The input PhysicalField must be dimensionless

>>> print(numerix.round(PhysicalField("1. m").log10(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units

multiply(other)

Multiply two physical quantities. The unit of the result is the product of the units of the operands.

>>> print(PhysicalField(10., 'N') * PhysicalField(10., 'm') == PhysicalField(100., 'N*m'))
True


As a special case, if the result is dimensionless, the value is returned without units, rather than with a dimensionless unit of 1. This facilitates passing physical quantities to packages such as Numeric that cannot use units, while ensuring the quantities have the desired units.

>>> print((PhysicalField(10., 's') * PhysicalField(2., 'Hz')))
20.0

property numericValue

Return the PhysicalField without units, after conversion to base SI units.

>>> print(numerix.round(PhysicalField("1 inch").numericValue, 6))
0.0254

put(indices, values)

put is the opposite of take. The values of self at the locations specified in indices are set to the corresponding value of values.

The indices can be any integer sequence object with values suitable for indexing into the flat form of self. The values must be any sequence of values that can be converted to the typecode of self.

>>> f = PhysicalField((1., 2., 3.), "m")
>>> f.put((2, 0), PhysicalField((2., 3.), "inch"))
>>> print(f)
[ 0.0762  2.      0.0508] m


The units of values must be compatible with self.

>>> f.put(1, PhysicalField(3, "kg"))
Traceback (most recent call last):
...
TypeError: Incompatible units

ravel()
reshape(shape)

Changes the shape of self to that specified in shape

>>> print(PhysicalField((1., 2., 3., 4.), "m").reshape((2, 2)))
[[ 1.  2.]
[ 3.  4.]] m


The new shape must have the same size as the existing one.

>>> print(PhysicalField((1., 2., 3., 4.), "m").reshape((2, 3)))
Traceback (most recent call last):
...
ValueError: total size of new array must be unchanged

property shape

Tuple of array dimensions.

sign()

Return the sign of the quantity. The unit is unchanged.

>>> from fipy.tools.numerix import sign
>>> print(sign(PhysicalField(((3., -2.), (-1., 4.)), 'm')))
[[ 1. -1.]
[-1.  1.]]

sin()

Return the sine of the PhysicalField

>>> print(PhysicalField(numerix.pi/6, "rad").sin())
0.5
>>> print(PhysicalField(30., "deg").sin())
0.5


The units of the PhysicalField must be an angle

>>> PhysicalField(30., "m").sin()
Traceback (most recent call last):
...
TypeError: Incompatible units

sinh()

Return the hyperbolic sine of the PhysicalField

>>> PhysicalField(0.).sinh()
0.0


The units of the PhysicalField must be dimensionless

>>> PhysicalField(60., "m").sinh()
Traceback (most recent call last):
...
TypeError: Incompatible units

sqrt()

Return the square root of the PhysicalField

>>> print(PhysicalField("100. m**2").sqrt())
10.0 m


The resulting unit must be integral

>>> print(PhysicalField("100. m").sqrt())
Traceback (most recent call last):
...
TypeError: Illegal exponent

subtract(other)

Subtract two physical quantities, so long as their units are compatible. The unit of the result is the unit of the first operand.

>>> print(PhysicalField(10., 'km') - PhysicalField(10., 'm'))
9.99 km
>>> print(PhysicalField(10., 'km') - PhysicalField(10., 'J'))
Traceback (most recent call last):
...
TypeError: Incompatible units

sum(index=0)

Returns the sum of all of the elements in self along the specified axis (first axis by default).

>>> print(PhysicalField(((1., 2.), (3., 4.)), "m").sum())
[ 4.  6.] m
>>> print(PhysicalField(((1., 2.), (3., 4.)), "m").sum(1))
[ 3.  7.] m

take(indices, axis=0)

Return the elements of self specified by the elements of indices. The resulting PhysicalField array has the same units as the original.

>>> print(PhysicalField((1., 2., 3.), "m").take((2, 0)))
[ 3.  1.] m


The optional third argument specifies the axis along which the selection occurs, and the default value (as in the example above) is 0, the first axis.

>>> print(PhysicalField(((1., 2., 3.), (4., 5., 6.)), "m").take((2, 0), axis = 1))
[[ 3.  1.]
[ 6.  4.]] m

tan()

Return the tangent of the PhysicalField

>>> numerix.round(PhysicalField(numerix.pi/4, "rad").tan(), 6)
1.0
>>> numerix.round(PhysicalField(45, "deg").tan(), 6)
1.0


The units of the PhysicalField must be an angle

>>> PhysicalField(45., "m").tan()
Traceback (most recent call last):
...
TypeError: Incompatible units

tanh()

Return the hyperbolic tangent of the PhysicalField

>>> print(numerix.allclose(PhysicalField(1.).tanh(), 0.761594155956))
True


The units of the PhysicalField must be dimensionless

>>> PhysicalField(60., "m").tanh()
Traceback (most recent call last):
...
TypeError: Incompatible units

tostring(max_line_width=75, precision=8, suppress_small=False, separator=' ')

Return human-readable form of a physical quantity

>>> p = PhysicalField(value = (3., 3.14159), unit = "eV")
>>> print(p.tostring(precision = 3, separator = '|'))
[ 3.   | 3.142] eV

property unit

Return the unit object of self.

>>> PhysicalField("1 m").unit
<PhysicalUnit m>

class fipy.tools.dimensions.physicalField.PhysicalUnit(names, factor, powers, offset=0)

Bases: object

A PhysicalUnit represents the units of a PhysicalField.

This class is not generally not instantiated by users of this module, but rather it is created in the process of constructing a PhysicalField.

Parameters:
• names (str) – Name of the unit

• factor (float) – Multiplier between the unit and the fundamental SI unit

• powers (array_like of float) – Nine elements representing the fundamental SI units of [“m”, “kg”, “s”, “A”, “K”, “mol”, “cd”, “rad”, “sr”]

• offset (float) – Displacement between the zero-point of the unit and the zero-point of the corresponding fundamental SI unit.

__div__(other)

Divide one unit by another

>>> a = PhysicalField("1. m")
>>> b = PhysicalField("3. ft")
>>> a.unit / b.unit
<PhysicalUnit m/ft>
>>> a.unit / b.inBaseUnits().unit
<PhysicalUnit 1>
>>> c = PhysicalField("1. s")
>>> d = PhysicalField("3. Hz")
>>> c.unit / d.unit
<PhysicalUnit s/Hz>
>>> c.unit / d.inBaseUnits().unit
<PhysicalUnit s**2/1>


or divide units by numbers

>>> a.unit / 3.
<PhysicalUnit m/3.0>


Units must have zero offset to be divided

>>> e = PhysicalField("1. J")
>>> f = PhysicalField("25. degC")
>>> e.unit / f.unit
Traceback (most recent call last):
...
TypeError: cannot divide units with non-zero offset
>>> e.unit / f.inBaseUnits().unit
<PhysicalUnit J/K>

__eq__(other)

Determine if units are identical

>>> a = PhysicalField("1. m")
>>> b = PhysicalField("3. ft")
>>> a.unit == b.unit
0
>>> a.unit == b.inBaseUnits().unit
1


Units can only be compared with other units

>>> a.unit == 3
Traceback (most recent call last):
...
TypeError: PhysicalUnits can only be compared with other PhysicalUnits

__ge__(other)

Return self>=value.

__gt__(other)

Return self>value.

__hash__ = None
__le__(other)

Return self<=value.

__lt__(other)

Return self<value.

__mul__(other)

Multiply units together

>>> a = PhysicalField("1. m")
>>> b = PhysicalField("3. ft")
>>> a.unit * b.unit == _findUnit('ft*m')
True
>>> a.unit * b.inBaseUnits().unit
<PhysicalUnit m**2>
>>> c = PhysicalField("1. s")
>>> d = PhysicalField("3. Hz")
>>> c.unit * d.unit == _findUnit('Hz*s')
True
>>> c.unit * d.inBaseUnits().unit
<PhysicalUnit 1>


or multiply units by numbers

>>> a.unit * 3.
<PhysicalUnit m*3.0>


Units must have zero offset to be multiplied

>>> e = PhysicalField("1. kB")
>>> f = PhysicalField("25. degC")
>>> e.unit * f.unit
Traceback (most recent call last):
...
TypeError: cannot multiply units with non-zero offset
>>> e.unit * f.inBaseUnits().unit
<PhysicalUnit kB*K>

__ne__(other)

Return self!=value.

__pow__(other)

Raise a unit to an integer power

>>> a = PhysicalField("1. m")
>>> a.unit**2
<PhysicalUnit m**2>
>>> a.unit**-2
<PhysicalUnit 1/m**2>


Non-integer powers are not supported

>>> a.unit**0.5
Traceback (most recent call last):
...
TypeError: Illegal exponent


Units must have zero offset to be exponentiated

>>> b = PhysicalField("25. degC")
>>> b.unit**2
Traceback (most recent call last):
...
TypeError: cannot exponentiate units with non-zero offset
>>> b.inBaseUnits().unit**2
<PhysicalUnit K**2>

__rdiv__(other)

Divide something by a unit

>>> a = PhysicalField("1. m")
>>> 3. / a.unit
<PhysicalUnit 3.0/m>


Units must have zero offset to be divided

>>> b = PhysicalField("25. degC")
>>> 3. / b.unit
Traceback (most recent call last):
...
TypeError: cannot divide units with non-zero offset
>>> 3. / b.inBaseUnits().unit
<PhysicalUnit 3.0/K>

__repr__()

Return representation of a physical unit

>>> PhysicalUnit('m',   1.,    [1, 0, 0, 0, 0, 0, 0, 0, 0])
<PhysicalUnit m>

__rmul__(other)

Multiply units together

>>> a = PhysicalField("1. m")
>>> b = PhysicalField("3. ft")
>>> a.unit * b.unit == _findUnit('ft*m')
True
>>> a.unit * b.inBaseUnits().unit
<PhysicalUnit m**2>
>>> c = PhysicalField("1. s")
>>> d = PhysicalField("3. Hz")
>>> c.unit * d.unit == _findUnit('Hz*s')
True
>>> c.unit * d.inBaseUnits().unit
<PhysicalUnit 1>


or multiply units by numbers

>>> a.unit * 3.
<PhysicalUnit m*3.0>


Units must have zero offset to be multiplied

>>> e = PhysicalField("1. kB")
>>> f = PhysicalField("25. degC")
>>> e.unit * f.unit
Traceback (most recent call last):
...
TypeError: cannot multiply units with non-zero offset
>>> e.unit * f.inBaseUnits().unit
<PhysicalUnit kB*K>

__rtruediv__(other)

Divide something by a unit

>>> a = PhysicalField("1. m")
>>> 3. / a.unit
<PhysicalUnit 3.0/m>


Units must have zero offset to be divided

>>> b = PhysicalField("25. degC")
>>> 3. / b.unit
Traceback (most recent call last):
...
TypeError: cannot divide units with non-zero offset
>>> 3. / b.inBaseUnits().unit
<PhysicalUnit 3.0/K>

__str__()

Return representation of a physical unit

>>> PhysicalUnit('m',   1.,    [1, 0, 0, 0, 0, 0, 0, 0, 0])
<PhysicalUnit m>

__truediv__(other)

Divide one unit by another

>>> a = PhysicalField("1. m")
>>> b = PhysicalField("3. ft")
>>> a.unit / b.unit
<PhysicalUnit m/ft>
>>> a.unit / b.inBaseUnits().unit
<PhysicalUnit 1>
>>> c = PhysicalField("1. s")
>>> d = PhysicalField("3. Hz")
>>> c.unit / d.unit
<PhysicalUnit s/Hz>
>>> c.unit / d.inBaseUnits().unit
<PhysicalUnit s**2/1>


or divide units by numbers

>>> a.unit / 3.
<PhysicalUnit m/3.0>


Units must have zero offset to be divided

>>> e = PhysicalField("1. J")
>>> f = PhysicalField("25. degC")
>>> e.unit / f.unit
Traceback (most recent call last):
...
TypeError: cannot divide units with non-zero offset
>>> e.unit / f.inBaseUnits().unit
<PhysicalUnit J/K>

conversionFactorTo(other)

Return the multiplication factor between two physical units

>>> a = PhysicalField("1. mm")
>>> b = PhysicalField("1. inch")
>>> print(numerix.round(b.unit.conversionFactorTo(a.unit), 6))
25.4


Units must have the same fundamental SI units

>>> c = PhysicalField("1. K")
>>> c.unit.conversionFactorTo(a.unit)
Traceback (most recent call last):
...
TypeError: Incompatible units


If units have different offsets, they must have the same factor

>>> d = PhysicalField("1. degC")
>>> c.unit.conversionFactorTo(d.unit)
1.0
>>> e = PhysicalField("1. degF")
>>> c.unit.conversionFactorTo(e.unit)
Traceback (most recent call last):
...
TypeError: Unit conversion (K to degF) cannot be expressed as a simple multiplicative factor

conversionTupleTo(other)

Return a tuple of the multiplication factor and offset between two physical units

>>> a = PhysicalField("1. K").unit
>>> b = PhysicalField("1. degF").unit
>>> from builtins import str
>>> [str(numerix.round(element, 6)) for element in b.conversionTupleTo(a)]
['0.555556', '459.67']

isAngle()

Returns True if the unit is an angle

>>> PhysicalField("1. deg").unit.isAngle()
1
1
>>> PhysicalField("1. inch").unit.isAngle()
0

isCompatible(other)

Returns a list of which fundamental SI units are compatible between self and other

>>> a = PhysicalField("1. mm")
>>> b = PhysicalField("1. inch")
>>> print(numerix.allclose(a.unit.isCompatible(b.unit),
...                        [True, True, True, True, True, True, True, True, True]))
True
>>> c = PhysicalField("1. K")
>>> print(numerix.allclose(a.unit.isCompatible(c.unit),
...                        [False, True, True, True, False, True, True, True, True]))
True

isDimensionless()

Returns True if the unit is dimensionless

>>> PhysicalField("1. m/m").unit.isDimensionless()
1
>>> PhysicalField("1. inch").unit.isDimensionless()
0

isDimensionlessOrAngle()

Returns True if the unit is dimensionless or an angle

>>> PhysicalField("1. m/m").unit.isDimensionlessOrAngle()
1
>>> PhysicalField("1. deg").unit.isDimensionlessOrAngle()
1
1
>>> PhysicalField("1. inch").unit.isDimensionlessOrAngle()
0

isInverseAngle()

Returns True if the 1 divided by the unit is an angle

>>> PhysicalField("1. deg**-1").unit.isInverseAngle()
1
1
>>> PhysicalField("1. inch").unit.isInverseAngle()
0

name()

Return the name of the unit

>>> PhysicalField("1. m").unit.name()
'm'
>>> (PhysicalField("1. m") / PhysicalField("1. s")
...  / PhysicalField("1. s")).unit.name()
'm/s**2'

setName(name)

Set the name of the unit to name

>>> a = PhysicalField("1. m/s").unit
>>> a
<PhysicalUnit m/s>
>>> a.setName('meterpersecond')
>>> a
<PhysicalUnit meterpersecond>
`

## Module contents¶

Last updated on Jun 27, 2023. Created using Sphinx 6.2.1.