Units
The two main functions for working with units are uparse
and u_str
:
DynamicQuantities.UnitsParse.@u_str
— Macrou"[unit expression]"
Parse a string containing an expression of units and return the corresponding Quantity
object with Float64
value. For example, u"km/s^2"
would be parsed to Quantity(1000.0, length=1, time=-2)
.
Note that inside this expression, you also have access to the Constants
module. So, for example, u"Constants.c^2 * Hz^2"
would evaluate to the quantity corresponding to the speed of light multiplied by Hertz, squared.
DynamicQuantities.UnitsParse.uparse
— Functionuparse(s::AbstractString)
Parse a string containing an expression of units and return the corresponding Quantity
object with Float64
value. For example, uparse("m/s")
would be parsed to Quantity(1.0, length=1, time=-1)
.
Note that inside this expression, you also have access to the Constants
module. So, for example, uparse("Constants.c^2 * Hz^2")
would evaluate to the quantity corresponding to the speed of light multiplied by Hertz, squared.
Available units
The base SI units are as follows. You can either use the @u_str
macro like 1.5u"m"
, or simply import these explicitly from the package with using DynamicQuantities: m
.
DynamicQuantities.Units.m
— ConstantLength in meters. Available variants: fm
, pm
, nm
, μm
(/um
), cm
, inch
, dm
, mm
, ft
, km
, mi
, Mm
, Gm
.
DynamicQuantities.Units.kg
— ConstantMass in kilograms. Available variants: pg
, ng
, μg
(/ug
), mg
, g
.
DynamicQuantities.Units.s
— ConstantTime in seconds. Available variants: fs
, ps
, ns
, μs
(/us
), ms
, min
(/minute
), h
(/hr
), day
(/d
), wk
, yr
, kyr
, Myr
, Gyr
.
DynamicQuantities.Units.A
— ConstantCurrent in Amperes. Available variants: nA
, μA
(/uA
), mA
, kA
.
DynamicQuantities.Units.K
— ConstantTemperature in Kelvin. Available variant: mK
.
DynamicQuantities.Units.cd
— ConstantLuminosity in candela. Available variant: mcd
.
DynamicQuantities.Units.mol
— ConstantAmount in moles. Available variant: pmol
, nmol
, μmol
(/umol
), mmol
.
Derived units
Several derived SI units are available as well:
DynamicQuantities.Units.C
— ConstantCharge in Coulombs.
DynamicQuantities.Units.F
— ConstantCapacitance in Farads. Available variants: fF
, pF
, nF
, μF
(/uF
), mF
.
DynamicQuantities.Units.H
— ConstantElectrical inductance in henries.
DynamicQuantities.Units.Hz
— ConstantFrequency in Hertz. Available variants: nHz
, μHz
(/uHz
), mHz
, kHz
, MHz
, GHz
.
DynamicQuantities.Units.J
— ConstantEnergy in Joules. Available variant: kJ
.
DynamicQuantities.Units.L
— ConstantVolume in liters. Available variants: μL
(/uL
), mL
, cL
, dL
.
DynamicQuantities.Units.M
— ConstantMolar concentration in molar. Available variants: pM
, nM
, μM
(/uM
), mM
.
DynamicQuantities.Units.N
— ConstantForce in Newtons. Available variants: μN
(/uN
), mN
, kN
.
DynamicQuantities.Units.Pa
— ConstantPressure in Pascals. Available variant: kPa
.
DynamicQuantities.Units.S
— ConstantElectrical conductance, electric susceptance, and electric admittance in siemens. Available variants: nS
, μS
(/uS
), mS
, kS
, MS
, GS
.
DynamicQuantities.Units.T
— ConstantMagnetic flux density in Teslas. Available variants: Gauss
DynamicQuantities.Units.Torr
— ConstantPressure in Torr. Available variants: mTorr
.
DynamicQuantities.Units.V
— ConstantVoltage in Volts. Available variants: pV
, nV
, μV
(/uV
), mV
, kV,
MV,
GV`.
DynamicQuantities.Units.W
— ConstantPower in Watts. Available variants: mW
, kW
, MW
, GW
.
DynamicQuantities.Units.Wb
— ConstantMagnetic flux in webers. Available variants: nWb
, μWb
(/uWb
), mWb
.
DynamicQuantities.Units.bar
— ConstantPressure in bars. Available variants: mbar
.
DynamicQuantities.Units.mmHg
— ConstantPressure in mmHg.
DynamicQuantities.Units.rad
— ConstantAngle in radians. Note that the SI definition is simply 1 rad = 1, so use symbolic units to avoid this. Available variants: nrad
, μrad
(/urad
), mrad
, deg
, arcmin
, arcsec
, μarcsec
(/uarcsec
), marcsec
.
DynamicQuantities.Units.sr
— ConstantSolid angle in steradians. Note that the SI definition is simply 1 sr = 1, so use symbolic units to avoid this.
DynamicQuantities.Units.Ω
— ConstantResistance in Ohms. Available variant: nΩ
, μΩ
(/uΩ
), mΩ
, kΩ
, MΩ
, GΩ
. Also available is ASCII ohm
(with variants nohm
, μohm
(/uohm
), mohm
, kohm
, Mohm
, Gohm
).
Custom Units
You can define custom units with the @register_unit
macro:
DynamicQuantities.@register_unit
— Macro@register_unit symbol value
Register a new unit under the given symbol to have a particular value.
Example
julia> @register_unit MyVolt 1.5u"V"
This will register a new unit MyVolt
with a value of 1.5u"V"
. You can then use this unit in your calculations:
julia> x = 20us"MyVolt^2"
20.0 MyVolt²
julia> y = 2.5us"A"
2.5 A
julia> x * y^2 |> us"W^2"
281.25 W²
julia> x * y^2 |> us"W^2" |> sqrt |> uexpand
16.77050983124842 m² kg s⁻³