Converting between equivalent quantities
This package extends the uconvert and ustrip functions from Unitful.jl to accept an additional argument of type Equivalence. Supplying this argument allows converting between units of different dimensions that are linked by the specified equivalence, e.g., the mass–energy equivalence $E=mc^2$:
julia> using Unitful, UnitfulEquivalences
julia> uconvert(u"keV", 1u"me", MassEnergy()) # electron rest mass is equivalent to ≈511 keV
510.9989499961642 keV
julia> ustrip(u"keV", 1u"me", MassEnergy())
510.9989499961642The equivalences MassEnergy, Spectral, and Thermal are defined and exported by this package:
UnitfulEquivalences.MassEnergy — TypeMassEnergy()Equivalence to convert between mass and energy according to the relation $E = mc^2$, where
- $E$ is the energy,
- $m$ is the mass and
- $c$ is the speed of light in vacuum.
Example
julia> uconvert(u"keV", 1u"me", MassEnergy()) # electron rest mass is equivalent to ≈511 keV
510.9989499961642 keVUnitfulEquivalences.Spectral — TypeSpectral(; frequency=:linear, wavelength=:linear, wavenumber=:linear)Equivalence that relates the energy of a photon to its (linear or angular) frequency, wavelength, and wavenumber. Whether to convert to linear or angular quantities is determined by optional keyword arguments, :linear is the default for all quantities.
Equivalent quantities are converted according to the relations $E = hf = ħω = hc/λ = ħc/ƛ = hcν̃ = ħck$, where
- $E$ is the photon energy,
- $f$ is the (temporal) frequency (
frequency=:linear), - $ω$ is the angular frequency (
frequency=:angular), - $λ$ is the wavelength (
wavelength=:linear), - $ƛ$ is the angular (also called reduced) wavelength (
wavelength=:angular), - $ν̃$ is the spectroscopic wavenumber (
wavenumber=:linear), - $k$ is the angular wavenumber (
wavelength=:angular), - $h$ is the Planck constant,
- $ħ$ is the reduced Planck constant and
- $c$ is the speed of light in vacuum.
Examples
julia> uconvert(u"nm", 13.6u"eV", Spectral()) # photon wavelength needed to ionize hydrogen
91.16485178911785 nm
julia> uconvert(u"Hz", 589u"nm", Spectral(frequency=:angular)) # angular frequency of sodium D line
3.1980501991661345e15 HzUnitfulEquivalences.Thermal — TypeThermal()Equivalence to convert between temperature and energy according to the relation $E = kT$, where
- $E$ is the energy,
- $T$ is the temperature and
- $k$ is the Boltzmann constant.
Example
julia> uconvert(u"eV", 20u"°C", Thermal()) # room temperature is equivalent to ≈1/40 eV
0.025261712457978588 eVAPI
UnitfulEquivalences.Equivalence — TypeEquivalenceAbstract supertype for all equivalences.
Unitful.uconvert — Functionuconvert(u::Units, x::Quantity, e::Equivalence)Convert x to the units u (of different dimensions) by using the specified equivalence.
Examples
julia> uconvert(u"keV", 1u"me", MassEnergy()) # electron rest mass is equivalent to ≈511 keV
510.9989499961642 keV
julia> uconvert(u"eV", 589u"nm", Spectral()) # photon energy of sodium D₂ line (≈589 nm)
2.104994880020378 eVUnitful.ustrip — Functionustrip([T::Type,] u::Units, x::Quantity, e::Equivalence)Convert x to the units u (of different dimensions) by using the specified equivalence and return the numeric value of the resulting quantity. If T is supplied, also convert the resulting number to type T.
Examples
julia> ustrip(u"keV", 1u"me", MassEnergy()) # electron rest mass is equivalent to ≈511 keV
510.9989499961642
julia> ustrip(u"eV", 589u"nm", Spectral()) # photon energy (in eV) of sodium D₂ line
2.104994880020378