unyt.equivalencies module

Equivalencies between different kinds of units

class unyt.equivalencies.Equivalence(in_place=False)[source]

Bases: object

convert(x, new_dims, **kwargs)[source]
class unyt.equivalencies.NumberDensityEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between mass and number density, given a mean molecular weight.

Given a number density \(n\), the mass density \(\rho\) is:

\[\rho = \mu m_{\rm H} n\]

And similarly

\[n = \rho (\mu m_{\rm H})^{-1}\]

mu (float) – The mean molecular weight. Defaults to 0.6 whcih is valid for fully ionized gas with primordial composition.


>>> print(NumberDensityEquivalence())
number density: density <-> number density
>>> from unyt import Msun, pc
>>> rho = 3*Msun/pc**3
>>> rho.to_equivalent('cm**-3', 'number_density', mu=1.4)
unyt_quantity(86.64869896, 'cm**(-3)')
type_name = 'number_density'
class unyt.equivalencies.ThermalEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between temperature and energy via the Boltzmann constant

Given a temperature \(T\) in an absolute scale (e.g. Kelvin or Rankine), the equivalent thermal energy \(E\) for that temperature is given by:

\[E = k_B T\]


\[T = E/k_B\]

Where \(k_B\) is Boltzmann’s constant.


>>> print(ThermalEquivalence())
thermal: temperature <-> energy
>>> from unyt import Kelvin
>>> temp = 1e6*Kelvin
>>> temp.to_equivalent('keV', 'thermal')
unyt_quantity(0.08617332, 'keV')
type_name = 'thermal'
class unyt.equivalencies.MassEnergyEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between mass and energy in special relativity

Given a body with mass \(m\), the self-energy \(E\) of that mass is given by

\[E = m c^2\]

where \(c\) is the speed of light.


>>> print(MassEnergyEquivalence())
mass_energy: mass <-> energy
>>> from unyt import g
>>> (3.5*g).to_equivalent('J', 'mass_energy')
unyt_quantity(3.14564313e+14, 'J')
type_name = 'mass_energy'
class unyt.equivalencies.SpectralEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between wavelength, frequency, and energy of a photon.

Given a photon with wavelength \(\lambda\), spatial frequency \(\bar\nu\), frequency \(\nu\) and Energy \(E\), these quantities are related by the following forumlae:

\[E = h \nu = h c / \lambda = h c \bar\nu\]

where \(h\) is Planck’s constant and \(c\) is the speed of light.


>>> print(SpectralEquivalence())
spectral: length <-> spatial_frequency <-> frequency <-> energy
>>> from unyt import angstrom, km
>>> (3*angstrom).to_equivalent('keV', 'spectral')
unyt_quantity(4.13280644, 'keV')
>>> (1*km).to_equivalent('MHz', 'spectral')
unyt_quantity(0.29979246, 'MHz')
type_name = 'spectral'
class unyt.equivalencies.SoundSpeedEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between the sound speed, temperature, and thermal energy of an ideal gas

For an ideal gas with sound speed \(c_s\), temperature \(T\), and thermal energy \(E\), the following equalities will hold:

\[c_s = \sqrt{\frac{\gamma k_B T}{\mu m_{\rm H}}}\]


\[E = c_s^2 \mu m_{\rm H} / \gamma = k_B T\]

where \(k_B\) is Boltzmann’s constant, \(\mu\) is the mean molecular weight of the gas, and \(\gamma\) is the ratio of specific heats.

  • gamma (float) – The ratio of specific heats. Defaults to 5/3, which is correct for monatomic species.

  • mu (float) – The mean molecular weight. Defaults to 0.6, which is valid for fully ionized gas with primordial composition.


>>> print(SoundSpeedEquivalence())
sound_speed (ideal gas): velocity <-> temperature <-> energy
>>> from unyt import Kelvin, km, s
>>> hot = 1e6*Kelvin
>>> hot.to_equivalent('km/s', 'sound_speed')
unyt_quantity(151.37249927, 'km/s')
>>> hot.to_equivalent('keV', 'sound_speed')
unyt_quantity(0.08617332, 'keV')
>>> cs = 100*km/s
>>> cs.to_equivalent('K', 'sound_speed')
unyt_quantity(436421.39881617, 'K')
>>> cs.to_equivalent('keV', 'sound_speed')
unyt_quantity(0.03760788, 'keV')
type_name = 'sound_speed'
class unyt.equivalencies.LorentzEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between velocity and the Lorentz gamma factor.

For a body with velocity \(v\), the Lorentz gamma factor, \(\gamma\) is

\[\gamma = \frac{1}{\sqrt{1 - v^2/c^2}}\]

and similarly

\[v = \frac{c}{\sqrt{1 - \gamma^2}}\]

where \(c\) is the speed of light.


>>> print(LorentzEquivalence())
lorentz: velocity <-> dimensionless
>>> from unyt import c, dimensionless
>>> v = 0.99*c
>>> print(v.to_equivalent('', 'lorentz'))
7.088812050083393 dimensionless
>>> fast = 99.9*dimensionless
>>> fast.to_equivalent('c', 'lorentz')
unyt_quantity(0.9999499, 'c')
>>> fast.to_equivalent('km/s', 'lorentz')
unyt_quantity(299777.43797656, 'km/s')
type_name = 'lorentz'
class unyt.equivalencies.SchwarzschildEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between the mass and radius of a Schwarzschild black hole

A Schwarzschild black hole of mass \(M\) has radius \(R\)

\[R = \frac{2 G M}{c^2}\]

and similarly

\[M = \frac{R c^2}{2 G}\]

where \(G\) is Newton’s gravitational constant and \(c\) is the speed of light.


>>> print(SchwarzschildEquivalence())
schwarzschild: mass <-> length
>>> from unyt import Msun, AU
>>> (10*Msun).to_equivalent('km', 'schwarzschild')
unyt_quantity(29.53161626, 'km')
>>> (1*AU).to_equivalent('Msun', 'schwarzschild')
unyt_quantity(50656851.7815179, 'Msun')
type_name = 'schwarzschild'
class unyt.equivalencies.ComptonEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between the Compton wavelength of a particle and its mass.

\[\lambda_c = h/mc\]


>>> print(ComptonEquivalence())
compton: mass <-> length
>>> from unyt import me, fm
>>> me.to_equivalent('angstrom', 'compton')
unyt_quantity(0.0242631, 'Å')
>>> (10*fm).to_equivalent('me', 'compton')
unyt_quantity(242.63102371, 'me')
type_name = 'compton'
class unyt.equivalencies.EffectiveTemperatureEquivalence(in_place=False)[source]

Bases: Equivalence

Equivalence between the emmitted flux accross all wavelengths and temperature of a blackbody

For a blackbody emitter with Temperature \(T\) emitting radiation with a flux \(F\), the following equality holds:


F = sigma T^4

where \(\sigma\) is the Stefan-Boltzmann constant.


>>> print(EffectiveTemperatureEquivalence())
effective_temperature: flux <-> temperature
>>> from unyt import K, W, m
>>> (5000.*K).to_equivalent('W/m**2', 'effective_temperature')
unyt_quantity(35439831.25, 'W/m**2')
>>> (100.*W/m**2).to_equivalent('K', 'effective_temperature')
unyt_quantity(204.92601414, 'K')
type_name = 'effective_temperature'