MooseUnits

MooseUnits is a physical units parsing and conversion helper class. A unit object can be constructed form a string (e.g. N*m, eV/at, kg*(m/s)^2, 1/s, m^-3). The unit parser supports the *,/, and ^ operators as well as parenthesis (, ). The argument of the ^ operator is expected to be a positive or negative integer.

Upon parsing all units are resolved to a combination of the seven base SI units (m, g, s, A, K, mol, cd) with their respective exponents and a prefactor. A canonical form is generated that permits unit comparisons to determine conformity (i.e. whether two units can be converted between).

MooseUnits supports prefixing all units with a metric prefix ranging from Y (yotta) to y (yocto).

Supported derived units

The following units are currently supported

SymbolBase unitsNameDescription
Ohmkgm2s3A2\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-3}\cdot \text{A}^{-2}Ohmresistance, impedance, reactance
atm101325kgm1s2101325\, \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}Standard atmospherepressure
eV1.602181019kgm2s21.60218\cdot10^{-19}\, \text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}electron Voltenergy
erg107kgm2s210^{-7}\, \text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}Ergenergy
degCK\text{K}Celsiustemperature - As long as this unit stands alone the additive shift in the scale is taken into account. In all other cases this unit behaves like Kelvin
degF59K\frac59\,\text{K}Fahrenheittemperature - As long as this unit stands alone the additive shift in the scale is taken into account. In all other cases this unit behaves like Rankine
degR59K\frac59\,\text{K}Rankinetemperature
Ang1010m10^{-10}\, \text{m}Angstromlength
mm\text{m}meterlength
g0.001kg0.001\, \text{kg}grammass
ss\text{s}secondtime
AA\text{A}Ampereelectric current
KK\text{K}Kelvintemperature
mol6.02214076e231023at6.02214076e23\cdot10^{23}\, \text{at}moleamount of substance
cdcd\text{cd}candelaluminous intensity
Nkgms2\text{kg}\cdot \text{m}\cdot \text{s}^{-2}Newtonforce, weight
Pakgm1s2\text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}Pascalpressure, stress
Jkgm2s2\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}Jouleenergy, work, heat
cal4.184kgm2s24.184\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}Calorieenergy, work, heat
Wkgm2s3\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-3}Wattpower, radiant flux
CAs\text{A}\cdot \text{s}Coulombelectric charge
Vkgm2s3A1\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-3}\cdot \text{A}^{-1}Voltvoltage (electrical potential), emf
Fkg1m2s4A2\text{kg}^{-1}\cdot \text{m}^{-2}\cdot \text{s}^{4}\cdot \text{A}^{2}Faradcapacitance
Skg1m2s3A2\text{kg}^{-1}\cdot \text{m}^{-2}\cdot \text{s}^{3}\cdot \text{A}^{2}Siemenselectrical conductance
Wbkgm2s2A1\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}\cdot \text{A}^{-1}Webermagnetic flux
Tkgs2A1\text{kg}\cdot \text{s}^{-2}\cdot \text{A}^{-1}Teslamagnetic flux density
Hkgm2s2A2\text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}\cdot \text{A}^{-2}Henryinductance
Ba0.1kgm1s20.1\, \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}baryePressure
dyn105kgms210^{-5}\, \text{kg}\cdot \text{m}\cdot \text{s}^{-2}dyneforce, weight
ft0.3048m0.3048\, \text{m}Footlength
in0.0254m0.0254\, \text{m}Inchlength
lb0.45359237kg0.45359237\, \text{kg}poundmass
lbf4.4482216152605kgms24.4482216152605\, \text{kg}\cdot \text{m}\cdot \text{s}^{-2}pound-forceforce
psi6894.757kgm1s26894.757\, \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}pound-force per square inchpressure, stress
BTU1055.06kgm2s21055.06\, \text{kg}\cdot \text{m}^{2}\cdot \text{s}^{-2}ISO 31-4 British thermal unitheat
bar105kgm1s210^5\, \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}Barpressure, stress
h3600s3600\, \text{s}hourtime
day86400s86400\, \text{s}daytime (d would create an ambiguity between centi-day and candela)
year365.2586400s365.25\cdot86400\, \text{s}annumtime (Julian calendar year, a would create an ambiguity between Peta-annum and Pascal)
l0.001m30.001\, \text{m}^{3}litervolume
u1.66053906661027kg31.6605390666\cdot10^{-27}\, \text{kg}^{3}unified atomic mass unitmass
atat\text{at}atomsingle count of substance

Operators

Unit objects support the * and / operators to multiply and divide units respectively. An overload of std::pow is provided to exponentiate units.

The == equal operator is implemented for comparison of two units and a unit and a real number. It returns true if the two units are exactly identical including the prefactor. A comparison that omits the prefactor is provided by the conformsTo method.

A unit is considered equal to a real number if all its base unit exponents are zero and the prefactor matches the real number.

Dimension check

In addition to the conformsTo check a few bool member functions are provided to check if the unit represents a given physical dimension.

  • isLength()

  • isTime()

  • isMass()

  • isCurrent()

  • isTemperature()

Output

MooseUnits objects can be output to streams using the << operator. The output consists of the prefactor and the SI base units with their respective exponents. Two stream manipulators are provided to toggle between plain text and LaTeX formatted output of the units.

See also

Unit conversion using MooseUnits is available in input files through the ${units ...} Brace Expression.