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
Symbol | Base units | Name | Description |
---|---|---|---|
Ohm | Ohm | resistance, impedance, reactance | |
atm | Standard atmosphere | pressure | |
eV | electron Volt | energy | |
erg | Erg | energy | |
degC | Celsius | temperature - 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 | |
degF | Fahrenheit | temperature - 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 | |
degR | Rankine | temperature | |
Ang | Angstrom | length | |
m | meter | length | |
g | gram | mass | |
s | second | time | |
A | Ampere | electric current | |
K | Kelvin | temperature | |
mol | mole | amount of substance | |
cd | candela | luminous intensity | |
N | Newton | force, weight | |
Pa | Pascal | pressure, stress | |
J | Joule | energy, work, heat | |
cal | Calorie | energy, work, heat | |
W | Watt | power, radiant flux | |
C | Coulomb | electric charge | |
V | Volt | voltage (electrical potential), emf | |
F | Farad | capacitance | |
S | Siemens | electrical conductance | |
Wb | Weber | magnetic flux | |
T | Tesla | magnetic flux density | |
H | Henry | inductance | |
Ba | barye | Pressure | |
dyn | dyne | force, weight | |
ft | Foot | length | |
in | Inch | length | |
lb | pound | mass | |
lbf | pound-force | force | |
psi | pound-force per square inch | pressure, stress | |
BTU | ISO 31-4 British thermal unit | heat | |
bar | Bar | pressure, stress | |
h | hour | time | |
day | day | time (d would create an ambiguity between centi-day and candela) | |
year | annum | time (Julian calendar year, a would create an ambiguity between Peta-annum and Pascal) | |
l | liter | volume | |
u | unified atomic mass unit | mass | |
at | atom | single 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.