class Example(cosmo_cls): param = Parameter(unit=u.eV, equivalencies=u.mass_energy()) def __init__(self, param, *, name=None, meta=None): cls = self.__class__ with u.add_enabled_equivalencies(cls.param.equivalencies): self._param = param << cls.param.unit
def test_Parameter_init(self): """Test :class:`astropy.cosmology.Parameter` instantiation.""" # defaults parameter = Parameter() assert parameter.fvalidate is _validate_with_unit assert parameter.unit is None assert parameter.equivalencies == [] assert parameter.derived is False assert parameter.name is None # setting all kwargs parameter = Parameter(fvalidate="float", doc="DOCSTRING", unit="km", equivalencies=[u.mass_energy()], derived=True) assert parameter.fvalidate is _validate_to_float assert parameter.unit is u.km assert parameter.equivalencies == [u.mass_energy()] assert parameter.derived is True
class Example(cosmo_cls): param = Parameter(unit=u.eV, equivalencies=u.mass_energy()) def __init__(self, param, *, name=None, meta=None): self.param = param @property def is_flat(self): return super().is_flat()
class Example1(Cosmology): param = Parameter(doc="Description of example parameter.", unit=u.m, equivalencies=u.mass_energy()) def __init__(self, param=15): self.param = param @property def is_flat(self): return super().is_flat()
def test_massenergy(): # The relative tolerance of these tests is set by the uncertainties # in the charge of the electron, which is known to about # 3e-9 (relative tolerance). Therefore, we limit the # precision of the tests to 1e-7 to be safe. The masses are # (loosely) known to ~ 5e-8 rel tolerance, so we couldn't test to # 1e-7 if we used the values from astropy.constants; that is, # they might change by more than 1e-7 in some future update, so instead # they are hardwired here. # Electron, proton, neutron, muon, 1g mass_eV = u.Quantity([510.998928e3, 938.272046e6, 939.565378e6, 105.6583715e6, 5.60958884539e32], u.eV) mass_g = u.Quantity([9.10938291e-28, 1.672621777e-24, 1.674927351e-24, 1.88353147e-25, 1], u.g) # Test both ways assert np.allclose(mass_eV.to_value(u.g, equivalencies=u.mass_energy()), mass_g.value, rtol=1e-7) assert np.allclose(mass_g.to_value(u.eV, equivalencies=u.mass_energy()), mass_eV.value, rtol=1e-7) # Basic tests of 'derived' equivalencies # Surface density sdens_eV = u.Quantity(5.60958884539e32, u.eV / u.m**2) sdens_g = u.Quantity(1e-4, u.g / u.cm**2) assert np.allclose(sdens_eV.to_value(u.g / u.cm**2, equivalencies=u.mass_energy()), sdens_g.value, rtol=1e-7) assert np.allclose(sdens_g.to_value(u.eV / u.m**2, equivalencies=u.mass_energy()), sdens_eV.value, rtol=1e-7) # Density dens_eV = u.Quantity(5.60958884539e32, u.eV / u.m**3) dens_g = u.Quantity(1e-6, u.g / u.cm**3) assert np.allclose(dens_eV.to_value(u.g / u.cm**3, equivalencies=u.mass_energy()), dens_g.value, rtol=1e-7) assert np.allclose(dens_g.to_value(u.eV / u.m**3, equivalencies=u.mass_energy()), dens_eV.value, rtol=1e-7) # Power pow_eV = u.Quantity(5.60958884539e32, u.eV / u.s) pow_g = u.Quantity(1, u.g / u.s) assert np.allclose(pow_eV.to_value(u.g / u.s, equivalencies=u.mass_energy()), pow_g.value, rtol=1e-7) assert np.allclose(pow_g.to_value(u.eV / u.s, equivalencies=u.mass_energy()), pow_eV.value, rtol=1e-7)
def test_kwarg_equivalencies(x_unit, energy_unit): @u.quantity_input(x=x_unit, energy=energy_unit, equivalencies=u.mass_energy()) def myfunc_args(x, energy=10*u.eV): return x, energy+(10*u.J) # Add an energy to check equiv is working x, energy = myfunc_args(1*u.arcsec, 100*u.gram) assert isinstance(x, u.Quantity) assert isinstance(energy, u.Quantity) assert x.unit == u.arcsec assert energy.unit == u.gram
def test_arg_equivalencies3(solarx_unit, solary_unit): @u.quantity_input(equivalencies=u.mass_energy()) def myfunc_args(solarx: solarx_unit, solary: solary_unit): return solarx, solary+(10*u.J) # Add an energy to check equiv is working solarx, solary = myfunc_args(1*u.arcsec, 100*u.gram) assert isinstance(solarx, u.Quantity) assert isinstance(solary, u.Quantity) assert solarx.unit == u.arcsec assert solary.unit == u.gram
def test_kwarg_equivalencies3(solarx_unit, energy): @u.quantity_input(equivalencies=u.mass_energy()) def myfunc_args(solarx: solarx_unit, energy: energy=10*u.eV): return solarx, energy+(10*u.J) # Add an energy to check equiv is working solarx, energy = myfunc_args(1*u.arcsec, 100*u.gram) assert isinstance(solarx, Quantity) assert isinstance(energy, Quantity) assert solarx.unit == u.arcsec assert energy.unit == u.gram
def test_arg_equivalencies3(solarx_unit, solary_unit): @u.quantity_input(equivalencies=u.mass_energy()) def myfunc_args(solarx: solarx_unit, solary: solary_unit): return solarx, solary + (10 * u.J ) # Add an energy to check equiv is working solarx, solary = myfunc_args(1 * u.arcsec, 100 * u.gram) assert isinstance(solarx, u.Quantity) assert isinstance(solary, u.Quantity) assert solarx.unit == u.arcsec assert solary.unit == u.gram
def test_make_from_Parameter(self, cosmo_cls, clean_registry): """Test the parameter creation process.""" class Example(cosmo_cls): param = Parameter(unit=u.eV, equivalencies=u.mass_energy()) def __init__(self, param, *, name=None, meta=None): cls = self.__class__ with u.add_enabled_equivalencies(cls.param.equivalencies): self._param = param << cls.param.unit assert Example(1).param == 1 * u.eV assert Example(1 * u.eV).param == 1 * u.eV assert Example(1 * u.J).param == (1 * u.J).to(u.eV) assert Example(1 * u.kg).param == (1 * u.kg).to(u.eV, u.mass_energy())
def test_Parameter_instance_attributes(self, param): """Test :class:`astropy.cosmology.Parameter` attributes from init.""" super().test_Parameter_instance_attributes(param) # property assert param.__doc__ == "Description of example parameter." # custom from init assert param._unit == u.m assert param._equivalencies == u.mass_energy() assert param._fmt == "" assert param._derived == np.False_ # custom from set_name assert param._attr_name == "param" assert param._attr_name_private == "_param"
def test_make_from_Parameter(self, cosmo_cls, clean_registry): """Test the parameter creation process. Uses ``__set__``.""" class Example(cosmo_cls): param = Parameter(unit=u.eV, equivalencies=u.mass_energy()) def __init__(self, param, *, name=None, meta=None): self.param = param @property def is_flat(self): return super().is_flat() assert Example(1).param == 1 * u.eV assert Example(1 * u.eV).param == 1 * u.eV assert Example(1 * u.J).param == (1 * u.J).to(u.eV) assert Example(1 * u.kg).param == (1 * u.kg).to(u.eV, u.mass_energy())
def models_with_input_eq(): # 1D model m1 = astmodels.Shift(1*u.kg) m1.input_units_equivalencies = {'x': u.mass_energy()} # 2D model m2 = astmodels.Const2D(10*u.Hz) m2.input_units_equivalencies = {'x': u.dimensionless_angles(), 'y': u.dimensionless_angles()} # 2D model with only one input equivalencies m3 = astmodels.Const2D(10*u.Hz) m3.input_units_equivalencies = {'x': u.dimensionless_angles()} # model using equivalency that has args using units m4 = astmodels.PowerLaw1D(amplitude=1*u.m, x_0=10*u.pix, alpha=7) m4.input_units_equivalencies = {'x': u.equivalencies.pixel_scale(0.5*u.arcsec/u.pix)} return[m1, m2, m3, m4]
def get_mass(argv, unit=units.MeV): """Get mass of isotope. """ global data_manager df = data_manager.df df_units = data_manager.units if isinstance(argv, str): A, Z = get_A_Z(argv) elif isinstance(argv, tuple) and len(argv) == 2: A, Z = tuple if (A, Z) not in df.index: raise ValueError('"(A, Z) = (%d, %d)" not found' % (A, Z)) amu = constants.u.to(units.MeV, units.mass_energy()) mass_excess = units.Quantity(df.loc[(A, Z)]['mass_excess'], df_units['mass_excess']) mass = A * amu + mass_excess return mass.value
def test_m_nu(self, cosmo_cls, cosmo): """Test Parameter ``m_nu``.""" # on the class assert isinstance(cosmo_cls.m_nu, Parameter) assert "Mass of neutrino species" in cosmo_cls.m_nu.__doc__ assert cosmo_cls.m_nu.unit == u.eV assert cosmo_cls.m_nu.equivalencies == u.mass_energy() # on the instance # assert cosmo.m_nu is cosmo._m_nu assert u.allclose(cosmo.m_nu, [0.0, 0.0, 0.0] * u.eV) # set differently depending on the other inputs if cosmo.Tnu0.value == 0: assert cosmo.m_nu is None elif not cosmo._massivenu: # only massless assert u.allclose(cosmo.m_nu, 0 * u.eV) elif self._nmasslessnu == 0: # only massive assert cosmo.m_nu == cosmo._massivenu_mass else: # a mix -- the most complicated case assert u.allclose(cosmo.m_nu[:self._nmasslessnu], 0 * u.eV) assert u.allclose(cosmo.m_nu[self._nmasslessnu], cosmo._massivenu_mass)
import numpy as np from astropy.constants import h, e, c, m_e, sigma_T import astropy.units as u e = e.gauss mec2 = m_e.to("erg", equivalencies=u.mass_energy()) B_cr = 4.414e13 * u.G # critical magnetic field lambda_c = (h / (m_e * c)).to("cm") # Compton wavelength # equivalency to transform frequencies to energies in electron rest mass units epsilon_equivalency = [(u.Hz, u.Unit(""), lambda x: h.cgs * x / mec2, lambda x: x * mec2 / h.cgs)] __all__ = ["R", "U_B", "Synchrotron"] def R(x): """Eq. 7.45 in [Dermer2009]_, angle-averaged integrand of the radiated power, the approximation of this function, given in Eq. D7 of [Aharonian2010]_, is used. """ term_1_num = 1.808 * np.power(x, 1 / 3) term_1_denom = np.sqrt(1 + 3.4 * np.power(x, 2 / 3)) term_2_num = 1 + 2.21 * np.power(x, 2 / 3) + 0.347 * np.power(x, 4 / 3) term_2_denom = 1 + 1.353 * np.power(x, 2 / 3) + 0.217 * np.power(x, 4 / 3) value = term_1_num / term_1_denom * term_2_num / term_2_denom * np.exp(-x) return value def U_B(B): """:math:`U_B` Eq. 7.14 in [DermerMenon2009]_""" return (np.power(B, 2) / (8 * np.pi)).to("erg cm-3")
def test_Parameter_equivalencies(self, param): """Test :attr:`astropy.cosmology.Parameter.equivalencies`.""" super().test_Parameter_equivalencies(param) assert param.equivalencies == u.mass_energy()
def main(): lines = read_in() value1, unit1 = lines # display correlation between # 1 - energy # 2 - frequency (=E/h) # 3 - wavelength (=E/hc) # 4 - eV # 5 - T (=E/k) # 6 - E/m_e*c2 # 7 - E/m_p*c2 if (unit1 == "mp") | (unit1 == "m_p"): unit = value1 * const.m_p else: if (unit1 == "me") | (unit1 == "m_e"): unit = value1 * const.m_e else: unit = value1 * u.Unit(unit1) if (unit.unit.name == "K"): # convert everything from K to erg first c0 = str(unit.to(u.erg, equivalencies=u.temperature_energy())) temp = unit.to(u.erg, equivalencies=u.temperature_energy()) c1 = str(temp.to(u.Hz, equivalencies=u.spectral())) temp = unit.to(u.erg, equivalencies=u.temperature_energy()) c2 = str(temp.to(u.AA, equivalencies=u.spectral())) temp = unit.to(u.erg, equivalencies=u.temperature_energy()) c3 = str(temp.to(u.eV, equivalencies=u.spectral())) c4 = str(unit.to(u.K, equivalencies=u.temperature_energy()).value) + " K" # unitless because the returned value is E/(m_e * c^2) temp = unit.to(u.erg, equivalencies=u.temperature_energy()) c5 = str(1.0 / const.m_e.to(temp, equivalencies=u.mass_energy()).value) # unitless because the returned value is E/(m_p * c^2) temp = unit.to(u.erg, equivalencies=u.temperature_energy()) c6 = str(1.0 / const.m_p.to(temp, equivalencies=u.mass_energy()).value) else: if (unit.unit.name == "erg") | (unit.unit.name == "eV"): c0 = str(unit.to(u.erg, equivalencies=u.spectral())) c1 = str(unit.to(u.Hz, equivalencies=u.spectral())) c2 = str(unit.to(u.AA, equivalencies=u.spectral())) c3 = str(unit.to(u.eV, equivalencies=u.spectral())) c4 = str(unit.to( u.K, equivalencies=u.temperature_energy()).value) + " K" # unitless because the returned value is E/(m_e * c^2) c5 = str(1.0 / const.m_e.to(unit, equivalencies=u.mass_energy()).value) # unitless because the returned value is E/(m_p * c^2) c6 = str(1.0 / const.m_p.to(unit, equivalencies=u.mass_energy()).value) else: if (unit.unit.name == "Hz") | (unit.unit.name == "Angstrom"): c0 = str(unit.to(u.erg, equivalencies=u.spectral())) c1 = str(unit.to(u.Hz, equivalencies=u.spectral())) c2 = str(unit.to(u.AA, equivalencies=u.spectral())) c3 = str(unit.to(u.eV, equivalencies=u.spectral())) temp = unit.to(u.erg, equivalencies=u.spectral()) c4 = str( temp.to(u.K, equivalencies=u.temperature_energy()).value) + " K" # unitless because the returned value is E/(m_e * c^2) temp = unit.to(u.erg, equivalencies=u.spectral()) c5 = str( 1.0 / const.m_e.to(temp, equivalencies=u.mass_energy()).value) # unitless because the returned value is E/(m_p * c^2) temp = unit.to(u.erg, equivalencies=u.spectral()) c6 = str( 1.0 / const.m_p.to(temp, equivalencies=u.mass_energy()).value) else: c0 = str(unit.to(u.erg, equivalencies=u.mass_energy())) temp = unit.to(u.erg, equivalencies=u.mass_energy()) c1 = str(temp.to(u.Hz, equivalencies=u.spectral())) c2 = str(temp.to(u.AA, equivalencies=u.spectral())) c3 = str(temp.to(u.eV, equivalencies=u.spectral())) temp = unit.to(u.erg, equivalencies=u.mass_energy()) c4 = str( temp.to(u.K, equivalencies=u.temperature_energy()).value) + " K" # unitless because the returned value is E/(m_e * c^2) c5 = str( 1.0 / const.m_e.to(unit, equivalencies=u.mass_energy()).value) # unitless because the returned value is E/(m_p * c^2) c6 = str( 1.0 / const.m_p.to(unit, equivalencies=u.mass_energy()).value) # c0,c1,c2,c3,c4,c5,c6 = "1","1","1","1","1","1","1" # each print will be concatenated to the string # passed to the NodeJS server print(c0 + ",", c1 + ",", c2 + ",", c3 + ",", c4 + ",", c5 + ",", c6)
class Example1(Cosmology): param = Parameter(doc="example parameter", unit=u.m, equivalencies=u.mass_energy()) def __init__(self, param=15): self._param = param
def test_equivalencies(self, parameter): """Test :attr:`astropy.cosmology.Parameter.equivalencies`.""" assert parameter.equivalencies == u.mass_energy()