def test_nanprod(self): # Testing a 1D array mag_1d = np.array([5., 6., np.nan]) speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s') units_1d = speeds_1d.units expected_nanprod_1d = Quantity._from_qty(mag=np.nanprod(mag_1d), units=units_1d*len(mag_1d)) self.assertEqual(expected_nanprod_1d, np.nanprod(speeds_1d)) # Testing a 2D array mag_2d = np.array([[5., 6.], [7., np.nan]]) speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s') units_2d = speeds_2d.units # Axis not specified expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d), units=units_2d*mag_2d.size) self.assertEqual(expected_nanprod_2d, np.nanprod(speeds_2d)) # Axis = 0 axis = 0 expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis), units=units_2d*mag_2d.shape[1]) np.testing.assert_array_equal(expected_nanprod_2d, np.nanprod(speeds_2d, axis=axis)) # Axis = 1 axis = 1 expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis), units=units_2d*mag_2d.shape[0]) np.testing.assert_array_equal(expected_nanprod_2d, np.nanprod(speeds_2d, axis=axis))
def test_nancumsum(self): # Testing a 1D array mag_1d = np.array([5., np.nan]) speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s') units_1d = speeds_1d.units expected_nancumsum_1d = Quantity._from_qty(mag=np.nancumsum(mag_1d), units=units_1d) np.testing.assert_array_equal(expected_nancumsum_1d, np.nancumsum(speeds_1d)) # Testing a 2D array mag_2d = np.array([[5., 6.], [7., np.nan]]) speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s') units_2d = speeds_2d.units # Axis not specified expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d)) # Axis = 0 axis = 0 expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d, axis=axis)) # Axis = 1 axis = 1 expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d, axis=axis))
def test_from_units(self): self.assertEqual(Quantity.from_units(mag=self.mag1, units='m/s'), self.vel1) self.assertEqual(Quantity.from_units(mag=self.mag1, units='m s-1'), self.vel1) self.assertEqual(Quantity.from_units(mag=self.mag1, units='m s^-1'), self.vel1) self.assertEqual(Quantity.from_units(mag=self.accel1.mag, units='m/s2'), self.accel1)
def test_debye_to_einstein(self): debye_temp = Quantity.from_units(200., 'K') einstein_temp = Quantity.from_units( self.ans.at['test_debye_to_einstein', 0], 'K') # Check that func works with floats self.assertAlmostEqual(c.debye_to_einstein(debye_temp('K')), einstein_temp('K')) # Check that func works with Quantity objects self.assertAlmostEqual(c.debye_to_einstein(debye_temp), einstein_temp)
def test_einstein_to_debye(self): einstein_temp = Quantity.from_units(161., 'K') debye_temp = Quantity.from_units( self.ans.at['test_einstein_to_debye', 0], 'K') # Check that func works with floats self.assertAlmostEqual(c.einstein_to_debye(einstein_temp('K')), debye_temp('K')) # Check that func works with Quantity objects self.assertAlmostEqual(c.einstein_to_debye(einstein_temp), debye_temp)
def test_energy_to_temp(self): E = Quantity.from_units(0.1, 'eV') temp = Quantity.from_units(self.ans.at['test_energy_to_temp', 0], 'K') # Check that Quantity objects work self.assertAlmostEqual(c.energy_to_temp(E), temp('K')) # Check that inputted floats work self.assertAlmostEqual(c.energy_to_temp(E('J')), temp('K')) self.assertAlmostEqual(c.energy_to_temp(E('eV'), units_in='eV'), temp('K')) self.assertAlmostEqual(c.energy_to_temp(E, units_out='oC'), temp('oC')) # Check if successfully outputs Quantity objects self.assertEqual(c.energy_to_temp(E, return_quantity=True), temp)
def test_energy_to_freq(self): E = Quantity.from_units(0.1, 'eV') freq = Quantity.from_units(self.ans.at['test_energy_to_freq', 0], 'Hz') # Check that Quantity objects work self.assertAlmostEqual(c.energy_to_freq(E), freq('Hz')) # Check that inputted floats work self.assertAlmostEqual(c.energy_to_freq(E('J')), freq('Hz')) self.assertAlmostEqual(c.energy_to_freq(E('eV'), units_in='eV'), freq('Hz')) self.assertAlmostEqual(c.energy_to_freq(E, units_out='min-1'), freq('min-1')) # Check if successfully outputs Quantity objects self.assertEqual(c.energy_to_freq(E, return_quantity=True), freq)
def test_inertia_to_temp(self): inertia = Quantity.from_units(3.5E-49, 'kg m2') temp = Quantity.from_units(self.ans.at['test_inertia_to_temp', 0], 'K') # Check that Quantity objects work self.assertAlmostEqual(c.inertia_to_temp(inertia), temp('K'), places=6) # Check that inputted floats work self.assertAlmostEqual(c.inertia_to_temp(inertia('kg m2')), temp('K'), places=6) self.assertAlmostEqual( c.inertia_to_temp(inertia('g cm2'), units_in='g cm2'), temp('K'), places=6) self.assertAlmostEqual(c.inertia_to_temp(inertia, units_out='oC'), temp('oC'), places=6)
def test_freq_to_temp(self): freq = Quantity.from_units(2.4e13, 'Hz') temp = Quantity.from_units(self.ans.at['test_freq_to_temp', 0], 'K') # Check that Quantity objects work self.assertAlmostEqual(c.freq_to_temp(freq), temp('K')) # Check that inputted floats work self.assertAlmostEqual(c.freq_to_temp(freq('Hz')), temp('K')) self.assertAlmostEqual( c.freq_to_temp(freq('min-1'), units_in='min-1'), temp('K')) self.assertAlmostEqual( c.freq_to_temp(freq, units_out='oC'), temp('oC')) # Check if successfully outputs Quantity objects self.assertEqual(c.freq_to_temp(freq, return_quantity=True), temp)
def test_freq_to_energy(self): freq = Quantity.from_units(2.4e13, 'Hz') E = Quantity.from_units(self.ans.at['test_freq_to_energy', 0], 'J') # Check that Quantity objects work self.assertAlmostEqual(c.freq_to_energy(freq), E('J')) # Check that inputted floats work self.assertAlmostEqual(c.freq_to_energy(freq('Hz')), E('J')) self.assertAlmostEqual( c.freq_to_energy(freq('min-1'), units_in='min-1'), E('J')) self.assertAlmostEqual( c.freq_to_energy(freq, units_out='eV'), E('eV')) # Check if successfully outputs Quantity objects self.assertEqual(c.freq_to_energy(freq, return_quantity=True), E)
def test_temp_to_energy(self): temp = Quantity.from_units(1150., 'K') E = Quantity.from_units(self.ans.at['test_temp_to_energy', 0], 'J') # Check that Quantity objects work self.assertAlmostEqual(c.temp_to_energy(temp), E('J')) # Check that inputted floats work self.assertAlmostEqual(c.temp_to_energy(temp('K')), E('J')) self.assertAlmostEqual(c.temp_to_energy(temp('oC'), units_in='oC'), E('J')) self.assertAlmostEqual(c.temp_to_energy(temp, units_out='eV'), E('eV')) # Check if successfully outputs Quantity objects self.assertEqual(c.temp_to_energy(temp, return_quantity=True), E)
def convert_unit(num=None, initial=None, final=None): """Converts units between two unit sets Parameters ---------- num : float, optional Number to convert. If not specified, will return the appropriate conversion factor. Default is 0 for temperature-based units, 1 otherwise. initial : str Units that num is currently in. Different units must be sparated by a ' ' or '/'. Supports powers as numbers after units. e.g. 'cm/s2', 'cm s-2', or 'cm s^-2'. final : str Units you would like num to be in. Different units must be sparated by a ' ' or '/'. Supports powers as numbers after units. e.g. 'cm/s2', 'cm s-2', or 'cm s^-2'. Returns ------- conversion_num : float num in the appropriate units Raises ------ ValueError If unit types are not consistent or not supported """ if initial in _temp_units and final in _temp_units: if num is None: num = 0. return convert_temp(num=num, initial=initial, final=final) else: if num is None: num = 1. in_qty = Quantity.from_units(mag=num, units=initial) return in_qty(final)
def test_temp_to_freq(self): temp = Quantity.from_units(1150., 'K') freq = Quantity.from_units( self.ans.at['test_temp_to_freq', 0], 'Hz') # Check that Quantity objects work self.assertAlmostEqual(c.temp_to_freq(temp), freq('Hz')) # Check that inputted floats work self.assertAlmostEqual(c.temp_to_freq(temp('K')), freq('Hz')) self.assertAlmostEqual( c.temp_to_freq(temp('oC'), units_in='oC'), freq('Hz')) self.assertAlmostEqual(c.temp_to_freq(temp, units_out='min-1'), freq('min-1')) # Check if successfully outputs Quantity objects self.assertEqual(c.temp_to_freq(temp, return_quantity=True), freq)
def test_freq_to_wavenumber(self): freq = Quantity.from_units(2.4e13, 'Hz') wavenumber = Quantity.from_units( self.ans.at['test_freq_to_wavenumber', 0], 'cm-1') # Check that Quantity objects work self.assertAlmostEqual(c.freq_to_wavenumber(freq), wavenumber('cm-1')) # Check that inputted floats work self.assertAlmostEqual(c.freq_to_wavenumber(freq('Hz')), wavenumber('cm-1')) self.assertAlmostEqual( c.freq_to_wavenumber(freq('min-1'), units_in='min-1'), wavenumber('cm-1')) self.assertAlmostEqual(c.freq_to_wavenumber(freq, units_out='m-1'), wavenumber('m-1')) # Check if successfully outputs Quantity objects self.assertEqual(c.freq_to_wavenumber(freq, return_quantity=True), wavenumber)
def test_energy_to_wavenumber(self): E = Quantity.from_units(0.1, 'eV') wavenumber = Quantity.from_units( self.ans.at['test_energy_to_wavenumber', 0], 'cm-1') # Check that Quantity objects work self.assertAlmostEqual(c.energy_to_wavenumber(E), wavenumber('cm-1')) # Check that inputted floats work self.assertAlmostEqual(c.energy_to_wavenumber(E('J')), wavenumber('cm-1')) self.assertAlmostEqual(c.energy_to_wavenumber(E('eV'), units_in='eV'), wavenumber('cm-1')) self.assertAlmostEqual(c.energy_to_wavenumber(E, units_out='m-1'), wavenumber('m-1')) # Check if successfully outputs Quantity objects self.assertEqual(c.energy_to_wavenumber(E, return_quantity=True), wavenumber)
def test_wavenumber_to_freq(self): wavenumber = Quantity.from_units(800., 'cm-1') freq = Quantity.from_units( self.ans.at['test_wavenumber_to_freq', 0], 'Hz') # Check that Quantity objects work self.assertAlmostEqual(c.wavenumber_to_freq(wavenumber), freq('Hz')) # Check that inputted floats work self.assertAlmostEqual(c.wavenumber_to_freq(wavenumber('cm-1')), freq('Hz')) self.assertAlmostEqual( c.wavenumber_to_freq(wavenumber('m-1'), units_in='m-1'), freq('Hz')) self.assertAlmostEqual( c.wavenumber_to_freq(wavenumber, units_out='min-1'), freq('min-1')) # Check if successfully outputs Quantity objects self.assertEqual(c.wavenumber_to_freq(wavenumber, return_quantity=True), freq)
def test_wavenumber_to_energy(self): wavenumber = Quantity.from_units(800., 'cm-1') energy = Quantity.from_units( self.ans.at['test_wavenumber_to_energy', 0], 'J') # Check that Quantity objects work self.assertAlmostEqual(c.wavenumber_to_energy(wavenumber), energy('J')) # Check that inputted floats work self.assertAlmostEqual(c.wavenumber_to_energy(wavenumber('cm-1')), energy('J')) self.assertAlmostEqual( c.wavenumber_to_energy(wavenumber('m-1'), units_in='m-1'), energy('J')) self.assertAlmostEqual( c.wavenumber_to_energy(wavenumber, units_out='eV'), energy('eV')) # Check if successfully outputs Quantity objects self.assertEqual(c.wavenumber_to_energy(wavenumber, return_quantity=True), energy)
def test_wavenumber_to_inertia(self): wavenumber = Quantity.from_units(800., 'cm-1') inertia = Quantity.from_units( self.ans.at['test_wavenumber_to_inertia', 0], 'kg m2') # Check that Quantity objects work self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber), inertia('kg m2')) # Check that inputted floats work self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber('cm-1')), inertia('kg m2')) self.assertAlmostEqual( c.wavenumber_to_inertia(wavenumber('m-1'), units_in='m-1'), inertia('kg m2')) self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber, units_out='g cm2'), inertia('g cm2')) # Check if successfully outputs Quantity objects self.assertAlmostEqual( c.wavenumber_to_inertia(wavenumber, return_quantity=True), inertia)
def test_temp_to_wavenumber(self): temp = Quantity.from_units(1150., 'K') wavenumber = Quantity.from_units( self.ans.at['test_temp_to_wavenumber', 0], 'cm-1') # Check that Quantity objects work self.assertAlmostEqual(c.temp_to_wavenumber(temp), wavenumber('cm-1')) # Check that inputted floats work self.assertAlmostEqual(c.temp_to_wavenumber(temp('K')), wavenumber('cm-1')) self.assertAlmostEqual( c.temp_to_wavenumber(temp('oC'), units_in='oC'), wavenumber('cm-1')) self.assertAlmostEqual(c.temp_to_wavenumber(temp, units_out='m-1'), wavenumber('m-1')) # Check if successfully outputs Quantity objects # Unsure why but Quantity objects were not compatible despite magnitude # and units agreeing. Separating tests wavenumber_out = c.temp_to_wavenumber(temp, return_quantity=True) self.assertAlmostEqual(wavenumber_out.mag, wavenumber.mag) self.assertEqual(wavenumber_out.units, wavenumber.units)
def interp(x, xp, fp, **kwargs): xp_out = _return_quantity(quantity=xp, return_quantity=False, units_out=x.units_str) fp_out = _return_quantity(quantity=fp, return_quantity=False, units_out=None) try: fp_units = fp.units except AttributeError: fp_units = '' return Qty.from_units(units=fp_units, mag=np.interp(x.mag, xp=xp, fp=fp_out, **kwargs))
# -*- coding: utf-8 -*- """ vunits.constants Contains universal constants for catalysis research. """ import numpy as np from vunits.db import _temp_units from vunits.quantity import Quantity, _force_get_quantity R = Quantity.from_units(mag=8.3144598, units='J/mol/K') r""":class:`~vunits.quantity.Quantity`: Molar (univeral or ideal) gas constant. **SI Value**: 8.3144598 J/mol/K **Dimensions**: :ref:`energy <energy_table>`/:ref:`amount <amount_table>`/ :ref:`temperature <temp_table>` or equivalent quantities. """ h = Quantity.from_units(mag=6.626070040e-34, units='J s') r""":class:`~vunits.quantity.Quantity`: Planck's constant. **SI Value**: 6.626070040e-34 J s. **Dimensions**: :ref:`energy <energy_table>`/:ref:`time <time_table>` or equivalent quantities.""" h_bar = h / 2. / np.pi r""":class:`~vunits.quantity.Quantity`: Planck's constant divided by 2\ :math:`\pi`\ .