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, UnitfulEquivalencesjulia> uconvert(u"keV", 1u"me", MassEnergy()) # electron rest mass is equivalent to ≈511 keV510.9989499961642 keVjulia> ustrip(u"keV", 1u"me", MassEnergy())510.9989499961642
To simplify conversion, a partially applied method of uconvert can be used: uconvert(unit, equivalence) returns a function that converts to unit via the specified equivalence. Since units itself are callable (unit(x) is equivalent to uconvert(unit, x)), this enables a convenient syntax for conversion:
julia> 1u"me" |> uconvert(u"keV", MassEnergy())510.9989499961642 keVjulia> 1u"me" |> u"keV"(MassEnergy())510.9989499961642 keV
The equivalences MassEnergy, Spectral, SpectralDensity, and Thermal are defined and exported by this package:
UnitfulEquivalences.MassEnergy — Type
MassEnergy()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 — Type
Spectral(; 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.SpectralDensity — Type
SpectralDensity(at)Equivalence between the three spectral flux density conventions: per unit wavelength ($F_λ$, e.g., W m^-2 nm^-1), per unit frequency ($F_ν$, e.g., W m^-2 Hz^-1), and per unit photon energy ($F_E$, e.g., W m^-2 eV^-1). The three conventions describe the same energy flux distributed over equivalent spectral intervals, $F_λ |dλ| = F_ν |dν| = F_E |dE|$, which gives $F_ν = F_λ λ^2/c$ and $F_E = F_ν/h$, where:
- $λ$ is the wavelength,
- $h$ is the Planck constant, and
- $c$ is the speed of light in vacuum.
Unlike the other equivalences, SpectralDensity is parameterized by the location of the flux density sample: at is the spectral coordinate at which the density is evaluated, and may itself be given as a wavelength, frequency ($ν = c/λ$), or photon energy ($E = hν$).
Example
julia> F_λ = 1e-15u"W/m^2/nm";
julia> uconvert(u"W/m^2/Hz", F_λ, SpectralDensity(500u"nm")) ≈ F_λ * (500u"nm")^2 / Unitful.c0
trueUnitfulEquivalences.Thermal — Type
Thermal()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 — Type
EquivalenceAbstract supertype for all equivalences.
Unitful.uconvert — Function
uconvert(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 eVuconvert(u::Units, e::Equivalence)Create a function for converting quantities to the units u (of different dimensions) by using the specified equivalence e. This is useful for calling a function with |>, where a unit can be converted after calculation.
Since units themselves are callable, u(e) is a convenient shortcut for uconvert(u, e).
Examples
julia> using Unitful: me, q, ε0, h
julia> 1me |> uconvert(u"keV", MassEnergy())
510.9989499961642 keV
julia> uconvert(u"eV", Spectral())(589u"nm") # photon energy (in eV) of sodium D₂ line
2.104994880020378 eV
julia> me*q^4 / (8*ε0^2*h^3) |> u"Hz" # Rydberg frequency
3.2898419566425655e15 Hz
julia> me*q^4 / (8*ε0^2*h^3) |> u"eV"(Spectral()) # Rydberg energy
13.605693108071442 eVUnitful.ustrip — Function
ustrip([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