We want to be able to use physical quantities rather than just measures (represented by e.g. floats or integers on computers) in order to raise the ambiguity caused by the implicit choice of units. Serving this purpose, AMUSE comes with a quantity class. Like particle sets (fundamental data structure in AMUSE), quantities are fundamental variables. When interacting with code all data has units, even scaled systems. In handling quantities we regard units as being integral part of the mathematical description of the variable [D.C. Ipsen, Units, Dimension, and Dimensionless Numbers, 1960, McGraw-Hill Book Company], i.e. we can state things like:
1 AU = 149597870.691 km
>>> from amuse.units import units
>>> q1 = 1.0|units.MSun
>>> q2 = 1.98892e30|units.kg
>>> q1 == q2
True
Quantity objects have basic conversion ability, when different units belonging to the same dimension exist the quantity object will convert from one to the other. For more elaborate conversion facilities, like interfacing with a natural unit code, AMUSE provides the generic_unit_converter and the derived nbody_system modules.
A Quantity objects represents a scalar or vector with a specific unit. Quantity is an abstract base class for VectorQuantity and ScalarQuantity.
Quantities should be constructed using or-operator (“|”), new_quantity or unit.new_quantity.
Quantities emulate numeric types.
Examples
>>> from amuse.units import units
>>> 100 | units.m
quantity<100 m>
>>> (100 | units.m) + (1 | units.km)
quantity<1100.0 m>
Quantities can be tested
>>> from amuse.units import units
>>> x = 100 | units.m
>>> x.is_quantity()
True
>>> x.is_scalar()
True
>>> x.is_vector()
False
>>> v = [100, 200, 300] | units.g
>>> v.is_quantity()
True
>>> v.is_scalar()
False
>>> v.is_vector()
True
Quantities can be converted to numbers
>>> from amuse.units import units
>>> x = 1000.0 | units.m
>>> x.value_in(units.m)
1000.0
>>> x.value_in(units.km)
1.0
>>> x.value_in(units.g) # but only if the units are compatible!
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IncompatibleUnitsException: Cannot express m in g, the units do not have the same bases
Reproduce quantity in another unit. The new unit must have the same basic si quantities.
Parameters: | another_unit – unit to convert quantity to |
---|---|
Returns: | quantity converted to new unit |
Create a string representing the quantity in another unit. The new unit must have the same basic si quantities.
Parameters: | another_unit – unit to convert quantity to |
---|---|
Returns: | string representing quantity converted to new unit |
True for all quantities.
True for scalar quantities.
True for vector quantities.
Calculate the square root of each component
>>> from amuse.units import units
>>> s1 = 144.0 | units.m**2
>>> s1.sqrt()
quantity<12.0 m>
>>> v1 = [16.0, 25.0, 36.0] | units.kg
>>> v1.sqrt()
quantity<[4.0, 5.0, 6.0] kg**0.5>
Return a numeric value (for scalars) or array (for vectors) in the given unit.
A number is returned without any unit information. Use this function only to transfer values to other libraries that have no support for quantities (for example plotting).
Parameters: | unit – wanted unit of the value |
---|---|
Returns: | number in the given unit |
>>> from amuse.units import units
>>> x = 10 | units.km
>>> x.value_in(units.m)
10000.0
A ScalarQuantity object represents a physical scalar quantity.
A VectorQuantity object represents a physical vector quantity.
>>> from amuse.units import units
>>> v1 = [0.0, 1.0, 2.0] | units.kg
>>> v2 = [2.0, 4.0, 6.0] | units.kg
>>> v1 + v2
quantity<[2.0, 5.0, 8.0] kg>
>>> len(v1)
3
Return the “index” component as a quantity.
Parameters: | index – index of the component, valid values for 3 dimensional vectors are: [0,1,2] |
---|---|
Returns: | quantity with the same units |
>>> from amuse.units import si
>>> vector = [0.0, 1.0, 2.0] | si.kg
>>> print vector[1]
1.0 kg
>>> print vector[0:2]
[0.0, 1.0] kg
>>> print vector[[0,2,]]
[0.0, 2.0] kg
Update the “index” component to the specified quantity.
Parameters: | index – index of the component, valid values for 3 dimensional vectors are: [0,1,2] |
---|---|
Quantity : | quantity to set, will be converted to the unit of this vector |
>>> from amuse.units import si
>>> vector = [0.0, 1.0, 2.0] | si.kg
>>> g = si.kg / 1000
>>> vector[1] = 3500 | g
>>> print vector
[0.0, 3.5, 2.0] kg
Return the maximum along an axis.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v1.amax()
quantity<3.0 kg>
Return the minimum value along an axis.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v1.amin()
quantity<1.0 kg>
Append a scalar quantity to this vector.
>>> from amuse.units import si
>>> vector = [1.0, 2.0, 3.0] | si.kg
>>> vector.append(4.0 | si.kg)
>>> print vector
[1.0, 2.0, 3.0, 4.0] kg
Return the indices of the maximum values along an axis.
>>> from amuse.units import si
>>> v1 = [[1.0, 2.0, 3.0], [2.5, 2.5, 2.5]] | si.kg
>>> v1.argmax(axis=0)
array([1, 1, 0])
Return the indices of the minimum values along an axis.
>>> from amuse.units import si
>>> v1 = [[1.0, 2.0, 3.0], [2.5, 2.5, 2.5]] | si.kg
>>> v1.argmin(axis=0)
array([0, 0, 1])
Returns the indices that would sort an array.
>>> from amuse.units import si
>>> v1 = [3.0, 1.0, 2.0] | si.kg
>>> v1.argsort()
array([1, 2, 0])
Return the cross product of this vector quantity with the supplied vector (quantity).
Return the dot product of this vector quantity with the supplied vector (quantity).
Concatenate the vector quantity to this vector. If the units differ, the vector_quantity argument is converted to the units of this vector.
>>> from amuse.units import units
>>> vector1 = [1.0, 2.0, 3.0] | units.kg
>>> vector2 = [1500, 2500, 6000] | units.g
>>> vector1.extend(vector2)
>>> print vector1
[1.0, 2.0, 3.0, 1.5, 2.5, 6.0] kg
Calculate the length of the vector.
>>> from amuse.units import units
>>> v1 = [0.0, 3.0, 4.0] | units.m
>>> v1.length()
quantity<5.0 m>
Calculate the squared length of the vector.
>>> from amuse.units import units
>>> v1 = [2.0, 3.0, 4.0] | units.m
>>> v1.length_squared()
quantity<29.0 m**2>
Calculate the length of the vectors in this vector.
>>> from amuse.units import units
>>> v1 = [[0.0, 3.0, 4.0],[2.0 , 2.0 , 1.0]] | units.m
>>> v1.lengths()
quantity<[5.0, 3.0] m>
Calculate the length of the vectors in this vector
>>> from amuse.units import units
>>> v1 = [[0.0, 3.0, 4.0],[4.0, 2.0, 1.0]] | units.m
>>> v1.lengths_squared()
quantity<[25.0, 21.0] m**2>
Return the maximum along an axis.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v1.amax()
quantity<3.0 kg>
Return the maximum of self and the argument.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v2 = [0.0, 3.0, 4.0] | si.kg
>>> v1.maximum(v2)
quantity<[1.0, 3.0, 4.0] kg>
Return the minimum value along an axis.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v1.amin()
quantity<1.0 kg>
Return the minimum of self and the argument.
>>> from amuse.units import si
>>> v1 = [1.0, 2.0, 3.0] | si.kg
>>> v2 = [0.0, 3.0, 4.0] | si.kg
>>> v1.minimum(v2)
quantity<[0.0, 2.0, 3.0] kg>
Prepend the scalar quantity before this vector. If the units differ, the scalar_quantity argument is converted to the units of this vector.
>>> from amuse.units import units
>>> vector1 = [1.0, 2.0, 3.0] | units.kg
>>> vector1.prepend(0.0 | units.kg)
>>> print vector1
[0.0, 1.0, 2.0, 3.0] kg
Calculate the product of the vector components
>>> from amuse.units import units
>>> v1 = [1.0, 2.0, 3.0] | units.m
>>> v1.prod()
quantity<6.0 m**3>
>>> v1 = [[2.0, 3.0], [2.0, 4.0], [5.0,3.0] ] | units.m
>>> v1.prod()
quantity<720.0 m**6>
>>> v1.prod(0)
quantity<[20.0, 36.0] m**3>
>>> v1.prod(1)
quantity<[6.0, 8.0, 15.0] m**2>
>>> v1 = [[[2.0, 3.0], [2.0, 4.0]],[[5.0, 2.0], [3.0, 4.0]]] | units.m
>>> v1.prod()
quantity<5760.0 m**8>
>>> v1.prod(0)
quantity<[[ 10. 6.], [ 6. 16.]] m**2>
>>> v1.prod(1)
quantity<[[ 4. 12.], [ 15. 8.]] m**2>
>>> v1.prod(2)
quantity<[[ 6. 8.], [ 10. 12.]] m**2>
Return a new vector with all items sorted.
>>> from amuse.units import si
>>> v1 = [3.0, 1.0, 2.0] | si.kg
>>> v1.sorted()
quantity<[1.0, 2.0, 3.0] kg>
Return a new vector with all items sorted. Perform all the same move operations on the other vectors.
Argument : | kind, the sort method for supported kinds see the numpy.sort documentation |
---|
>>> from amuse.units import si
>>> v1 = [3.0, 1.0, 2.0] | si.kg
>>> v2 = [2.0, 3.0, 2.0] | si.m
>>> v3 = [1.0, 4.0, 5.0] | si.s
>>> list(v1.sorted_with(v2, v3))
[quantity<[1.0, 2.0, 3.0] kg>, quantity<[3.0, 2.0, 2.0] m>, quantity<[4.0, 5.0, 1.0] s>]
Calculate the sum of the vector components
>>> from amuse.units import units
>>> v1 = [0.0, 1.0, 2.0] | units.kg
>>> v1.sum()
quantity<3.0 kg>
The x axis component of a 3 dimensional vector. This is equavalent to the first component of vector.
Returns: | x axis component as a quantity |
---|
>>> from amuse.units import si
>>> vector = [1.0, 2.0, 3.0] | si.kg
>>> print vector.x
1.0 kg
The y axis component of a 3 dimensional vector. This is equavalent to the second component of vector.
Returns: | y axis component as a quantity |
---|
>>> from amuse.units import si
>>> vector = [1.0, 2.0, 3.0] | si.kg
>>> print vector.y
2.0 kg
The z axis component of a 3 dimensional vector. This is equavalent to the third component of vector.
Returns: | z axis component as a quantity |
---|
>>> from amuse.units import si
>>> vector = [1.0, 2.0, 3.0] | si.kg
>>> print vector.z
3.0 kg
A Non Numeric Quantity object represents a quantity without a physical meaning.
These Quantity objects cannot be used in numeric operations (like addition or multiplication). Also, conversion to another unit is not possible.
Examples are string quantities or enumerated value quantities.
>>> from amuse.units.core import enumeration_unit
>>> my_unit = enumeration_unit(
... "x",
... "x",
... [1,3,4],
... ["first", "second", "third"])
...
>>> 3 | my_unit
quantity<3 - second>
>>> (3 | my_unit).value_in(my_unit)
3
base_unit objects are orthogonal, indivisable units of a sytem of units.
A system of units contains a set of base units
Parameters: |
|
---|
>>> cgs = system("cgs")
>>> cm = base_unit("length", "centimetre", "cm", cgs)
>>> cm
unit<cm>
The base represented as a list of tuples. Each tuple consists of an power and a unit.
The multiplication factor of a unit. For example, factor is 1000 for km.
Abstract base class of derived units. New units can be derived from base_units. Each operation on a unit creates a new derived_unit.
A div_unit object defines a unit multiplied by another unit. Do not call this method directly, div_unit objects are supposed to be created by dividing units.
Parameters: |
|
---|
>>> from amuse.units import si
>>> speed = si.m / si.s
>>> speed
unit<m / s>
>>> speed_with_powers = si.m * si.s ** -1
>>> speed.as_quantity_in(speed_with_powers)
quantity<1.0 m * s**-1>
Enumeration unit objects define a fixed set of quantities.
A quantity with a enumeration_unit can only have a value defined in the set of values of the enumeration_unit.
Parameters: |
|
---|
Examples
>>> my_unit = enumeration_unit('my_unit','my_unit', [1,2,5], ["star","gas","planet"])
>>> 2 | my_unit
quantity<2 - gas>
>>> list(my_unit.quantities())
[quantity<1 - star>, quantity<2 - gas>, quantity<5 - planet>]
>>> 3 | my_unit
Traceback (most recent call last):
...
AmuseException: <3> is not a valid value for unit<my_unit>
Or, with default values:
>>> my_unit = enumeration_unit('my_unit','my_unit', None, ["star","gas","planet"])
>>> 2 | my_unit
quantity<2 - planet>
>>> list(my_unit.quantities())
[quantity<0 - star>, quantity<1 - gas>, quantity<2 - planet>]
A factor_unit object defines a unit multiplied by a number. Do not call this method directly, factor_unit objects are supposed to be created by multiplying a number with a unit.
Parameters: |
|
---|
>>> from amuse.units import si
>>> minute = 60.0 * si.s
>>> minute.as_quantity_in(si.s)
quantity<60.0 s>
>>> hour = 60.0 * minute
>>> hour
unit<60.0 * 60.0 * s>
>>> hour.as_quantity_in(si.s)
quantity<3600.0 s>
Create a new factor unit with factor 1000. Prepend k to the symbol of the unit.
This is a very simple utility function that only prepends a name and has no knowledge about anything else than kilo’s.
Examples
>>> from amuse.units import si
>>> km = k(si.m)
>>> km
unit<km>
>>> kkg = k(si.kg)
>>> kkg
unit<kkg>
>>> kkg.as_quantity_in(si.kg)
quantity<1000.0 kg>
A mul_unit object defines a unit multiplied by another unit. Do not call this method directly, mul_unit objects are supposed to be created by multiplying units.
Parameters: |
|
---|
>>> from amuse.units import si
>>> area = si.m * si.m
>>> area
unit<m * m>
>>> hectare = (100 * si.m) * (100 * si.m)
>>> hectare.as_quantity_in(area)
quantity<10000.0 m * m>
A named_unit object defines an alias for another unit. When printing a named_unit, the symbol is shown and not the unit parts. For all other operations the named_units works exactly like the aliased unit.
Parameters: |
|
---|
>>> from amuse.units import si
>>> 60 * si.s
unit<60 * s>
>>> minute = named_unit("minute","min", 60*si.s)
>>> minute
unit<min>
>>> (20 | (60 * si.s)).as_quantity_in(minute)
quantity<20.0 min>
nonnumeric_unit objects are indivisable units not connected to any system of units.
nonnumeric_units cannot be used to derive new units from.
nonnumeric_units have no physical meaning.
A pow_unit object defines a unit as another unit to a specified power.
Do not call this method directly, pow_unit objects are supposed to be created by taking powers of units.
Parameters: |
|
---|
>>> from amuse.units import si
>>> area = si.m**2
>>> area
unit<m**2>
>>> area.as_quantity_in(si.m * si.m)
quantity<1.0 m * m>
>>> hectare = (100 * si.m) ** 2
>>> hectare.as_quantity_in(area)
quantity<10000.0 m**2>
String unit objects define quantities with a string value. These have no physical meaning, but are needed for some legacy codes. For example the path of a file.
Abstract base class for unit objects.
Two classes of units are defined:
Units can also be named, by creating a named unit.
Express this unit as a quantity in the given unit
Parameters: | unit – The unit to express this unit in |
---|---|
Result : | A Quantity object |
Examples
>>> from amuse.units import units
>>> ton = 1000 * units.kg
>>> ton.as_quantity_in(units.kg)
quantity<1000.0 kg>
The parts of this units as a list of tuple with factor, power and unit
Determine if the base of other is the same as the base of self.
Parameters: | other – unit to compare base to |
---|---|
Result : | True, if bases are compatiple. |
>>> from amuse.units import units
>>> mps = units.m / units.s
>>> kph = units.km / units.hour
>>> mps.has_same_base_as(kph)
True
>>> mps.has_same_base_as(units.km)
False
Express this quantity in the given unit
Parameters: | unit – The unit to express this quantity in |
---|---|
Result : | A Quantity object |
Examples
>>> from amuse.units import units
>>> l = 1 | units.AU
>>> l.in_(units.km)
quantity<149597870.691 km>
Create a new Quantity object.
Parameters: | value – numeric value of the quantity, can be a number or a sequence (list or ndarray) |
---|---|
Returns: | new ScalarQuantity or VectorQuantity object with this unit |
Convert unit to a reduced (simpler) form
Convert unit to a reduced (simpler) form
Convert unit to a form with only one factor and powers
Result : | Unit with only a factor and power terms |
---|
>>> from amuse.units import units
>>> N = (units.m * units.kg) / (units.s * units.s)
>>> N
unit<m * kg / (s * s)>
>>> J = N * units.m
>>> J
unit<m * kg / (s * s) * m>
>>> J.to_simple_form()
unit<m**2 * kg * s**-2>
Return a numeric value of this unit in the given unit. Works only when the units are compatible, i.e. from tonnage to kg’s.
A number is returned without any unit information.
Parameters: | unit – wanted unit of the value |
---|---|
Returns: | number in the given unit |
>>> from amuse.units import units
>>> x = units.km
>>> x.value_in(units.m)
1000.0
The amuse framework gives you the ability to choose a unit system for your model through the ‘generic_unit_converter’ module. This enables you to work with e.g. natural units or n-body units within AMUSE.
The implementation makes use of a dimension-space, which is a vector-space where the chosen units form a base. For a detailed description of the method see e.g.: Maksymowicz, A, American Journal of Physics, Vol.44, No.3, 1976.
The generic unit system knows the seven base quantities in the International System of Quantities, I.S.Q.
Base quantity | Name in generic unit | Name in S.I. unit |
length | generic_system.length | units.m |
time | generic_system.time | units.s |
mass | generic_system.mass | units.kg |
current | generic_system.current | units.A |
temperature | generic_system.temperature | units.K |
amount of substance | generic_system.amount_of_substance | units.mol |
luminous intensity | generic_system.luminous_intensity | units.cd |
The n-body unit system knows the three base quantities in the International System of Quantities, I.S.Q. and defines the gravitational constant to be 1:
G = 1 | (length**3) / (mass * (time**2))
Base quantity | Name in generic unit | Name in S.I. unit |
length | generic_system.length | units.m |
time | generic_system.time | units.s |
mass | generic_system.mass | units.kg |
acceleration | length / (time ** 2) |
potential | (length ** 2) / (time ** 2) |
energy | mass * potential |
specific_energy | potential |
speed | length / time |
volume | (length ** 3) |
density | mass / volume |
momentum_density | density * speed |
energy_density | density * specific_energy |
A ConvertBetweenGenericAndSiUnits object is a converter from arbitrary units which user gives, to si units (and vice versa).
The ``generic_unit_converter’’ ConvertBetweenGenericAndSiUnits is the actual class through which you define the unit system. Upon instantiation you choose the base units. In the example below we chose the speed of light as a unit: c = 1 unit length/second, and the second as the unit of time.
Note that the system has two base dimensions, length and time. By the second argument we have assigned the unit second to time and by the requirement that unit lenght / second equals one, the new unit length will be {c} meters in S.I. units.
Example:
>>> from amuse.units.generic_unit_converter import ConvertBetweenGenericAndSiUnits
>>> from amuse.units import units, constants
>>> converter = ConvertBetweenGenericAndSiUnits(constants.c, units.m)
Convert a quantity in S.I units to a quantity in generic units.
>>> from amuse.units.generic_unit_converter import ConvertBetweenGenericAndSiUnits
>>> from amuse.units import units, constants
>>> converter = ConvertBetweenGenericAndSiUnits(constants.c, units.s)
>>> print converter.to_generic(constants.c)
1.0 length * time**-1
Convert a quantity in generic units to a quantity in S.I. units.
>>> from amuse.units.generic_unit_converter import ConvertBetweenGenericAndSiUnits
>>> from amuse.units import units, constants
>>> converter = ConvertBetweenGenericAndSiUnits(constants.c, units.s)
>>> print converter.to_si(length)
299792458.0 m
For convenience, the gravitational dynamics interface works with a unit-converter which converts between the units used by the code and the preferred units in the script (user’s choice).
We show two examples. The first one uses the (derived from the generic units converter) nbody_system converter, which is the logical choice for dynamics (n-body) codes.
The second one uses the generic unit converter directly, this is just an example.
>>> from amuse.community.hermite0.interface import Hermite
>>> from amuse.units import nbody_system
>>> from amuse.units import constants, units
>>> convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km)
>>> hermite = Hermite(convert_nbody)
>>> from amuse.community.hermite0.interface import Hermite
>>> from amuse.units import generic_unit_converter as gc
>>> from amuse.units import constants, units
>>> converter = gc.ConvertBetweenGenericAndSiUnits(constants.G, 1.0 | units.MSun, 1 | units.AU)
>>> hermite = Hermite(converter)
More examples can be found in the tutorial, Working with Units
Stellar Dynamics Interface Defintion