Units

The two main functions for working with units are uparse and u_str:

DynamicQuantities.UnitsParse.@u_strMacro
u"[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.

source
DynamicQuantities.UnitsParse.uparseFunction
uparse(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.

source

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.sConstant

Time in seconds. Available variants: fs, ps, ns, μs (/us), ms, min (/minute), h (/hr), day (/d), wk, yr, kyr, Myr, Gyr.

source

Derived units

Several derived SI units are available as well:

DynamicQuantities.Units.SConstant

Electrical conductance, electric susceptance, and electric admittance in siemens. Available variants: nS, μS (/uS), mS, kS, MS, GS.

source
DynamicQuantities.Units.radConstant

Angle 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.

source
DynamicQuantities.Units.ΩConstant

Resistance in Ohms. Available variant: nΩ, μΩ (/uΩ), , , , . Also available is ASCII ohm (with variants nohm, μohm (/uohm), mohm, kohm, Mohm, Gohm).

source

Custom Units

You can define custom units with the @register_unit macro:

DynamicQuantities.@register_unitMacro
@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⁻³
source

Affine Units

DynamicQuantities also supports affine units like Celsius and Fahrenheit through the AffineUnit{R} type and the ua string macro. For example,

# Define temperature in Celsius
room_temp = 22ua"degC"    # 295.15 K

# Define temperature in Fahrenheit
freezing = 32ua"degF"     # 273.15 K

# Can take differences normally, as these are now regular Quantities:
room_temp - freezing
# 22 K

Note there are some subtleties about working with these:

DynamicQuantities.@ua_strMacro
ua"unit"

Parse a string containing an affine unit expression. Currently only supports °C (or degC) and °F (or degF).

For example:

room_temp = 22ua"degC"  # The multiplication returns a Quantity
Warning

This is an experimental feature and may change in the future.

source
DynamicQuantities.aff_uparseFunction
aff_uparse(s::AbstractString)

Parse a string into an affine unit (°C/degC, °F/degF). Function equivalent of ua"unit".

Warning

This is an experimental feature and may change in the future.

source
DynamicQuantities.AffineUnitType
AffineUnit{R}

A simple struct for representing affine units like Celsius and Fahrenheit. This is not part of the AbstractDimensions hierarchy.

AffineUnit only supports scalar multiplication in the form number * unit (e.g., 22ua"degC"), which immediately converts it to a regular Quantity{Float64,Dimensions{R}}. Other operations like unit * number, division, addition, or subtraction with AffineUnit are not supported.

Non-associative multiplication

Multiplication with AffineUnit is non-associative due to the auto-conversion property. For example, (2 * 3) * ua"degC"2 * (3 * ua"degC") because when a number multiplies an AffineUnit, it immediately converts to a regular Quantity with the affine transformation applied.

Warning

This is an experimental feature and may change in the future.

source

Currently, the only supported affine units are:

  • °C or degC - Degrees Celsius
  • °F or degF - Degrees Fahrenheit