Quantities and Units

Introduction

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.

Quantities

Inheritance diagram of amuse.units.quantities

class amuse.units.quantities.Quantity(unit)

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
as_quantity_in(another_unit)

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
as_string_in(another_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
in_(another_unit)

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
is_quantity()

True for all quantities.

is_scalar()

True for scalar quantities.

is_vector()

True for vector quantities.

sqrt()

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>
value_in(unit)

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
class amuse.units.quantities.ScalarQuantity(number, unit)

A ScalarQuantity object represents a physical scalar quantity.

class amuse.units.quantities.VectorQuantity(array, unit)

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
__getitem__(index)

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
__setitem__(index, quantity)

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
amax(axis=None)

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>
amin(axis=None)

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(scalar_quantity)

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
argmax(**options)

Returns the index of the maximum item

>>> from amuse.units import si
>>> v1 = [1.0, 3.0, 2.0] | si.kg
>>> v1.argmax()
1
argmin(axis=None)

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])
argsort(**options)

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])
cross(other, axisa=-1, axisb=-1, axisc=-1, axis=None)

Return the cross product of this vector quantity with the supplied vector (quantity).

cumsum(axis=None, dtype=None, out=None)

Calculate the cumulative sum of the elements along a given axis.

dot(other, **kwargs)

Return the dot product of this vector quantity with the supplied vector (quantity).

>>> from amuse.units import units
>>> v1 = [1.0, 2.0, 3.0] | units.m
>>> v1.dot(v1)
quantity<14.0 m**2>
extend(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
inner(other)

Calculate the inner product of self with other.

>>> from amuse.units import units
>>> v1 = [1.0, 2.0, 3.0] | units.m
>>> v1.inner(v1)
quantity<14.0 m**2>
length()

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>
length_squared()

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>
lengths()

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>
lengths_squared()

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>
max(axis=None)

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>
maximum(other)

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>
min(axis=None)

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>
minimum(other)

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(scalar_quantity)

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
prod(axis=None, dtype=None)

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.0, 6.0], [6.0, 16.0]] m**2>
>>> v1.prod(1)
quantity<[[4.0, 12.0], [15.0, 8.0]] m**2>
>>> v1.prod(2)
quantity<[[6.0, 8.0], [10.0, 12.0]] m**2>
sorted()

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>
sorted_with(*others)

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>]
sum(axis=None, dtype=None, out=None)

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>
x

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
y

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
z

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
class amuse.units.quantities.NonNumericQuantity(value, unit)

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

Units

Inheritance diagram of amuse.units.core

class amuse.units.core.base_unit(quantity, name, symbol, system)

base_unit objects are orthogonal, indivisable units of a sytem of units.

A system of units contains a set of base units

Parameters:
  • quantity – name of the base quantity, for example length
  • name – name of the unit, for example meter
  • symbol – symbol of the unit, for example m
  • system – system of units object
>>> cgs = system("cgs")
>>> cm = base_unit("length", "centimetre", "cm", cgs)
>>> cm
unit<cm>
base

The base represented as a list of tuples. Each tuple consists of an power and a unit.

factor

The multiplication factor of a unit. For example, factor is 1000 for km.

class amuse.units.core.derived_unit

Abstract base class of derived units. New units can be derived from base_units. Each operation on a unit creates a new derived_unit.

class amuse.units.core.div_unit(left_hand, right_hand)

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:
  • left_hand – Left hand side of the multiplication.
  • right_hand – Right hand side of the multiplication.
>>> 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>
class amuse.units.core.factor_unit(factor, unit, name=None, symbol=None)

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:
  • unit – The unit to derive from.
  • factor – The multiplication factor.
>>> 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>
class amuse.units.core.mul_unit(left_hand, right_hand)

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:
  • left_hand – Left hand side of the multiplication.
  • right_hand – Right hand side of the multiplication.
>>> 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>
class amuse.units.core.named_unit(name, symbol, unit)

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:
  • name – Long name or description of the unit
  • symbol – Short name to show when printing units or quantities
  • unit – The unit to alias
>>> from amuse.units import si
>>> 60.0 * si.s
unit<60.0 * s>
>>> minute = named_unit("minute","min", 60*si.s)
>>> minute
unit<min>
>>> (20 | (60.0 * si.s)).as_quantity_in(minute)
quantity<20.0 min>
class amuse.units.core.nonnumeric_unit(name, symbol)

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.

class amuse.units.core.pow_unit(power, unit)

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:
  • power – Power of the unit
  • unit – The unit to derive from
>>> 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>
class amuse.units.core.string_unit(name, symbol)

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.

class amuse.units.core.unit

Abstract base class for unit objects.

Two classes of units are defined:

base units
The base units in a given system of units. For SI, these are meter, kilogram, second, ampere, kelvin, mole and candele. See the si module amuse.units.si
derived units
Derived units are created by dividing or multiplying with a number or with another unit. For example, to get a velocity unit we can devine vel = 1000 * m / s

Units can also be named, by creating a named unit.

as_quantity_in(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>
get_parts_with_power()

The parts of this units as a list of tuple with factor, power and unit

has_same_base_as(other)

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
in_(x)

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>
to_array_of_floats()

Represent a unit as an array of 8 64-bit floats. First float represents the factor, the other 7 the power of each base unit. Cannot be used for non numeric units

to_factor_and_reduced_form()

Convert unit to a reduced (simpler) form

to_reduced_form()

Convert unit to a reduced (simpler) form

to_simple_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>
value_in(unit)

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

Unit systems and converters

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.

Generic units

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

N-body units

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

Derived quantities

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

Converter

class amuse.units.generic_unit_converter.ConvertBetweenGenericAndSiUnits(*arguments_list)

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)
to_generic(value)

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
to_si(value)

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

Use with codes

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.

Usage: example 1

>>> 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)

Usage: example 2

>>> 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)

Example

More examples can be found in the tutorial, Working with Units

Stellar Dynamics Interface Defintion

class amuse.community.interface.gd.GravitationalDynamics(legacy_interface, unit_converter=None, **options)