Skip to content

Commit

Permalink
Add support for spatial frequency to spectral equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden committed Jul 25, 2018
1 parent a167ed4 commit 7d46825
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions unyt/equivalencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
density,
number_density,
flux,
spatial_frequency,
)
from unyt.exceptions import InvalidUnitEquivalence

Expand Down Expand Up @@ -182,9 +183,9 @@ def __str__(self):
class SpectralEquivalence(Equivalence):
"""Equivalence between wavelength, frequency, and energy of a photon.
Given a photon with wavelength :math:`\\lambda`, frequency :math:`\\nu`
and Energy :math:`E`, these quantities are related by the following
forumlae:
Given a photon with wavelength :math:`\\lambda`, spatial frequency
:math:`\\bar\\nu`, frequency :math:`\\nu` and Energy :math:`E`,
these quantities are related by the following forumlae:
.. math::
Expand All @@ -195,15 +196,15 @@ class SpectralEquivalence(Equivalence):
Example
------
>>> print(SpectralEquivalence())
spectral: length <-> frequency <-> energy
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"
_dims = (length, rate, energy,)
_dims = (length, rate, energy, spatial_frequency)

def _convert(self, x, new_dims):
from unyt import physical_constants as pc
Expand All @@ -212,19 +213,32 @@ def _convert(self, x, new_dims):
return np.divide(pc.clight*pc.hmks, x, out=self._get_out(x))
elif x.units.dimensions == rate:
return np.multiply(x, pc.hmks, out=self._get_out(x))
elif x.units.dimensions == spatial_frequency:
return np.divide(x, pc.clight*pc.hmks, out=self._get_out(x))
elif new_dims == length:
if x.units.dimensions == rate:
return np.divide(pc.clight, x, out=self._get_out(x))
elif x.units.dimensions == energy:
return np.divide(pc.hmks*pc.clight, x, out=self._get_out(x))
elif x.units.dimensions == spatial_frequency:
return np.divide(1, x, out=self._get_out(x))
elif new_dims == rate:
if x.units.dimensions == length:
return np.divide(pc.clight, x, out=self._get_out(x))
elif x.units.dimensions == energy:
return np.divide(x, pc.hmks, out=self._get_out(x))
elif x.units.dimensions == spatial_frequency:
return np.divide(x, pc.clight, out=self._get_out(x))
elif new_dims == spatial_frequency:
if x.units.dimensions == length:
return np.divide(1, x, out=self._get_out(x))
elif x.units.dimensions == energy:
return np.divide(x, pc.hmks*pc.clight, out=self._get_out(x))
elif x.units.dimensions == rate:
return np.divide(x, pc.clight, out=self._get_out(x))

def __str__(self):
return "spectral: length <-> frequency <-> energy"
return "spectral: length <-> spatial_frequency <-> frequency <-> energy"


class SoundSpeedEquivalence(Equivalence):
Expand Down

0 comments on commit 7d46825

Please sign in to comment.