11. Constants and unit conversion — MDAnalysis.units

The base units of MDAnalysis trajectories are the Å (ångström) for length and ps (pico second) for time. By default, all positions are in Å and all times are in ps, regardless of how the MD code stored trajectory data. By default, MDAnalysis converts automatically to the MDAnalysis units when reading trajectories and converts back when writing. This makes it possible to write scripts that can be agnostic of the specifics of how a particular MD code stores trajectory data. Other base units are listed in the table on Base units in MDAnalysis.

Base units in MDAnalysis
quantity unit SI units
length Å \(10^{-10}\) m
time ps \(10^{-12}\) s
energy kJ/mol \(1.66053892103219 \times 10^{-21}\) J
charge \(e\) \(1.602176565 \times 10^{-19}\) As
force kJ/(mol·Å) \(1.66053892103219 \times 10^{-11}\) J/m
speed Å/ps \(100\) m/s

11.1. Implementation notes

All conversions with convert() are carried out in a simple fashion: the conversion factor \(f_{b,b'}\) from the base unit \(b\) to another unit \(b'\) is precomputed and stored (see Data). The numerical value of a quantity in unit \(b\) is \(X/b\) (e.g. for \(X = 1.23\,\mathrm{ps}\) the numerical value is \(X/\mathrm{ps} = 1.23\)). [1]

The new numerical value \(X'/b'\) of the quantity (in units of \(b'\)) is then

\[X'/b' = f_{b,b'} X/b\]

The function get_conversion_factor() returns the appropriate factor \(f_{b,b'}\).

Conversion between different units is always carried out via the base unit as an intermediate step:

x is in u1: from u1 to b:  x'  = x  / factor[u1]
            from b  to u2: x'' = x' * factor[u2]
so f[u1,u2] = factor[u2]/factor[u1]

11.1.1. Conversions

Examples for how to calculate some of the conversion factors that are hard-coded in units (see Data).

density:

Base unit is \(\mathrm{Å}^{-3}\):

n/x = n/A**3 * densityUnit_factor[x]

Example for how to calculate the conversion factor \(f_{\mathrm{Å}^{-3},\mathrm{nm}^{-3}}\) from \(\mathrm{Å^-3}\) to \(\mathrm{nm}^{-3}\):

\[f_{\mathrm{Å}^{-3},\mathrm{nm}^{-3}} = \frac{1\,\mathrm{nm}^{-3}}{1\,\mathrm{Å}^{-3}} = \frac{(10\,\mathrm{Å})^{-3}}{1\,\mathrm{Å}^{-3}} = 10^{-3}\]
concentration:

Example for how to convert the conversion factor to Molar (mol/l):

factor = 1 A**-3 / (N_Avogadro * (10**-9 dm)**-3)

relative to a density rho0 in g/cm^3:

M(H2O) = 18 g/mol   Molar mass of water

factor = 1/(1e-24 * N_Avogadro / M(H2O))

from rho/rho0 = n/(N_A * M**-1) / rho0 where [n] = 1/Volume, [rho] = mass/Volume

See also

Maybe we should simply use Quantities or scipy.constants?

11.2. Functions

MDAnalysis.units.get_conversion_factor(unit_type, u1, u2)[source]

generate the conversion factor u1 -> u2 by using the base unit as an intermediate

f[u1 -> u2] = factor[u2]/factor[u1]

Conversion of X (in u1) to X’ (in u2):

X’ = conversion_factor * X
MDAnalysis.units.convert(x, u1, u2)[source]

Convert value x in unit u1 to new value in u2.

Returns:Converted value.
Raises:ValueError if the units are not known or if one attempts to convert between incompatible units.

11.3. Data

MDAnalysis.units.constants = {u'elementary_charge': 1.602176565e-19, u'N_Avogadro': 6.02214129e+23, u'calorie': 4.184}

Values of physical constants are taken from CODATA 2010 at NIST. The thermochemical calorie is defined in the ISO 80000-5:2007 standard and is also listed in the NIST Guide to SI: Appendix B.8: Factors for Units.

New in version 0.9.0.

MDAnalysis.units.lengthUnit_factor = {u'A': 1.0, u'picometer': 100.0, u'Angstrom': 1.0, u'femtometer': 100000.0, u'nm': 0.1, u'\u212b': 1.0, u'pm': 100.0, u'nanometer': 0.1, u'fm': 100000.0, u'angstrom': 1.0}

The basic unit of length in MDAnalysis is the Angstrom. Conversion factors between the base unit and other lengthUnits x are stored. Conversions follow L/x = L/Angstrom * lengthUnit_factor[x]. x can be nm/nanometer or fm.

MDAnalysis.units.N_Avogadro = 6.02214129e+23

Avogadro’s constant in mol**-1.

Deprecated since version 0.9.0: Use N_Avogadro in dict constants instead.

Changed in version 0.9.0: Use CODATA 2010 value, which differs from the previously used value 6.02214179e+23 mol**-1 by -5.00000000e+16 mol**-1.

MDAnalysis.units.water = {u'MolarMass': 18.016, u'SPC': 0.985, u'TIP4P': 1.001, u'exp': 0.997, u'TIP3P': 1.002}
water density values at T=298K, P=1atm [Jorgensen1998]
model g cm**-3
SPC 0.985(1)
TIP3P 1.002(1)
TIP4P 1.001(1)
exp 0.997

and molar mass 18.016 g mol**-1.

MDAnalysis.units.densityUnit_factor = {u'Molar': 1660.5389210321898, u'\u212b^{-3}': 1.0, u'A^{-3}': 1.0, u'nm^{-3}': 1000.0, u'nanometer^{-3}': 1000.0, u'SPC': 30.37184690488927, u'Angstrom^{-3}': 1.0, u'water': 30.00628806551247, u'TIP4P': 29.886382818497438, u'TIP3P': 29.85655608913766}

The basic unit for densities is Angstroem**(-3), i.e. the volume per molecule in A**3. Especially for water it can be convenient to measure the density relative to bulk, and hence a number of values are pre-stored in water.

MDAnalysis.units.timeUnit_factor = {u'ps': 1.0, u'second': 1e-12, u'fs': 1000.0, u'sec': 1e-12, u'picosecond': 1.0, u'femtosecond': 1000.0, u'AKMA': 20.45482949774598, u'ns': 0.001, u'nanosecond': 0.001, u's': 1e-12}

For time, the basic unit is ps; in particular CHARMM’s 1 AKMA time unit = 4.888821E-14 sec is supported.

MDAnalysis.units.speedUnit_factor = {u'Angstrom/picosecond': 1.0, u'm/s': 100.0, u'nm/ps': 0.1, u'nm/ns': 100.0, u'\u212b/ps': 1.0, u'Angstrom/AKMA': 0.04888821, u'pm/ps': 100.0, u'angstrom/picosecond': 1.0, u'A/ps': 1.0, u'Angstrom/ps': 1.0, u'nanometer/picosecond': 0.1, u'nanometer/ps': 0.1}

For speed, the basic unit is Angstrom/ps.

MDAnalysis.units.forceUnit_factor = {u'N': 1.66053892103219e-11, u'kcal/(mol*Angstrom)': 0.2390057361376673, u'kJ/(mol*Angstrom)': 1.0, u'kJ/(mol*A)': 1.0, u'kJ/(mol*\u212b)': 1.0, u'Newton': 1.66053892103219e-11, u'J/m': 1.66053892103219e-11, u'kJ/(mol*nm)': 10.0}

For force the basic unit is kJ/(mol*Angstrom).

MDAnalysis.units.chargeUnit_factor = {u'Amber': 18.2223, u'C': 1.602176565e-19, u'e': 1.0, u'As': 1.602176565e-19}

Charge is measured in multiples of the electron charge e, with the value elementary_charge in constants. The conversion factor to Amber charge units is 18.2223.

Changed in version 0.9.0: Use CODATA 2010 value for elementary charge, which differs from the previously used value e = 1.602176487 x 10**(-19) C by 7.8000000e-27 C.

MDAnalysis.units.conversion_factor = {u'force': {u'N': 1.66053892103219e-11, u'kcal/(mol*Angstrom)': 0.2390057361376673, u'kJ/(mol*Angstrom)': 1.0, u'kJ/(mol*A)': 1.0, u'kJ/(mol*\u212b)': 1.0, u'Newton': 1.66053892103219e-11, u'J/m': 1.66053892103219e-11, u'kJ/(mol*nm)': 10.0}, u'density': {u'Molar': 1660.5389210321898, u'\u212b^{-3}': 1.0, u'A^{-3}': 1.0, u'nm^{-3}': 1000.0, u'nanometer^{-3}': 1000.0, u'SPC': 30.37184690488927, u'Angstrom^{-3}': 1.0, u'water': 30.00628806551247, u'TIP4P': 29.886382818497438, u'TIP3P': 29.85655608913766}, u'energy': {u'kcal/mol': 0.2390057361376673, u'J': 1.66053892103219e-21, u'kJ/mol': 1.0, u'eV': 0.01036426919046959}, u'charge': {u'Amber': 18.2223, u'C': 1.602176565e-19, u'e': 1.0, u'As': 1.602176565e-19}, u'length': {u'A': 1.0, u'picometer': 100.0, u'Angstrom': 1.0, u'femtometer': 100000.0, u'nm': 0.1, u'\u212b': 1.0, u'pm': 100.0, u'nanometer': 0.1, u'fm': 100000.0, u'angstrom': 1.0}, u'time': {u'ps': 1.0, u'second': 1e-12, u'fs': 1000.0, u'sec': 1e-12, u'picosecond': 1.0, u'femtosecond': 1000.0, u'AKMA': 20.45482949774598, u'ns': 0.001, u'nanosecond': 0.001, u's': 1e-12}, u'speed': {u'Angstrom/picosecond': 1.0, u'm/s': 100.0, u'nm/ps': 0.1, u'nm/ns': 100.0, u'\u212b/ps': 1.0, u'Angstrom/AKMA': 0.04888821, u'pm/ps': 100.0, u'angstrom/picosecond': 1.0, u'A/ps': 1.0, u'Angstrom/ps': 1.0, u'nanometer/picosecond': 0.1, u'nanometer/ps': 0.1}}

conversion_factor is used by get_conversion_factor(): Note: any observable with a unit (i.e. one with an entry in the unit attribute) needs an entry in conversion_factor

MDAnalysis.units.unit_types = {u'kcal/mol': u'energy', u'kJ/mol': u'energy', u'm/s': u'speed', u'kJ/(mol*\u212b)': u'force', u'Angstrom^{-3}': u'density', u'pm/ps': u'speed', u'second': u'time', u'A': u'length', u'Angstrom': u'length', u'nm/ns': u'speed', u'Angstrom/picosecond': u'speed', u'Angstrom/AKMA': u'speed', u'nanometer': u'length', u'TIP4P': u'density', u'ps': u'time', u'kcal/(mol*Angstrom)': u'force', u'angstrom/picosecond': u'speed', u'nm': u'length', u'nanometer^{-3}': u'density', u'\u212b': u'length', u'A/ps': u'speed', u'nanosecond': u'time', u'ns': u'time', u'angstrom': u'length', u'kJ/(mol*A)': u'force', u'AKMA': u'time', u'eV': u'energy', u'pm': u'length', u'Molar': u'density', u'C': u'charge', u'fs': u'time', u'femtosecond': u'time', u's': u'time', u'J': u'energy', u'nm/ps': u'speed', u'TIP3P': u'density', u'water': u'density', u'nanometer/ps': u'speed', u'nm^{-3}': u'density', u'picosecond': u'time', u'\u212b/ps': u'speed', u'N': u'force', u'fm': u'length', u'Amber': u'charge', u'A^{-3}': u'density', u'e': u'charge', u'nanometer/picosecond': u'speed', u'Newton': u'force', u'Angstrom/ps': u'speed', u'\u212b^{-3}': u'density', u'As': u'charge', u'SPC': u'density', u'kJ/(mol*Angstrom)': u'force', u'femtometer': u'length', u'sec': u'time', u'picometer': u'length', u'J/m': u'force', u'kJ/(mol*nm)': u'force'}

Generated lookup table (dict): returns the type of unit for a known input unit. Note: Any unit must be unique because this dict is used to guess the unit type.

11.4. References and footnotes

[Jorgensen1998]
  1. Jorgensen, C. Jenson, J Comp Chem 19 (1998), 1179-1186

Footnotes

[1]

One can also consider the conversion factor to carry units \(b'/b\), in which case the conversion formula would become

\[X' = f_{b,b'} X\]