def test_Alfven_speed(): r"""Test the Alfven_speed function in parameters.py.""" assert np.isclose( Alfven_speed(1 * u.T, 1e-8 * u.kg * u.m**-3).value, 8920620.580763856) V_A = Alfven_speed(B, n_i) assert np.isclose(V_A.value, (B / np.sqrt(mu0 * n_i * (m_p + m_e))).si.value) assert Alfven_speed(B, rho) == Alfven_speed(B, n_i) assert Alfven_speed(B, rho).unit.is_equivalent(u.m / u.s) assert Alfven_speed(B, rho) == Alfven_speed(-B, rho) assert Alfven_speed(B, 4 * rho) == 0.5 * Alfven_speed(B, rho) assert Alfven_speed(2 * B, rho) == 2 * Alfven_speed(B, rho) # Case when Z=1 is assumed with pytest.warns(RelativityWarning): assert np.isclose(Alfven_speed(5 * u.T, 5e19 * u.m**-3, ion='H+'), Alfven_speed(5 * u.T, 5e19 * u.m**-3, ion='p'), atol=0 * u.m / u.s, rtol=1e-3) # Case where magnetic field and density are Quantity arrays V_A_arr = Alfven_speed(B_arr, rho_arr) V_A_arr0 = Alfven_speed(B_arr[0], rho_arr[0]) V_A_arr1 = Alfven_speed(B_arr[1], rho_arr[1]) assert np.isclose(V_A_arr0.value, V_A_arr[0].value) assert np.isclose(V_A_arr1.value, V_A_arr[1].value) # Case where magnetic field is an array but density is a scalar Quantity V_A_arr = Alfven_speed(B_arr, rho) V_A_arr0 = Alfven_speed(B_arr[0], rho) V_A_arr1 = Alfven_speed(B_arr[1], rho) assert np.isclose(V_A_arr0.value, V_A_arr[0].value) assert np.isclose(V_A_arr1.value, V_A_arr[1].value) with pytest.raises(ValueError): Alfven_speed(np.array([5, 6, 7]) * u.T, np.array([5, 6]) * u.m**-3) with pytest.raises(ValueError): Alfven_speed(B_nanarr, rho_arr) with pytest.raises(ValueError): Alfven_speed(B_arr, rho_negarr) with pytest.raises(u.UnitConversionError): Alfven_speed(5 * u.A, n_i, ion='p') with pytest.raises(TypeError): Alfven_speed(B, 5, ion='p') with pytest.raises(u.UnitsError): Alfven_speed(B, 5 * u.m**-2, ion='p') with pytest.raises(InvalidParticleError): Alfven_speed(B, n_i, ion='spacecats') with pytest.warns(RelativityWarning): # relativistic Alfven_speed(5e1 * u.T, 5e19 * u.m**-3, ion='p') with pytest.raises(RelativityError): # super-relativistic Alfven_speed(5e8 * u.T, 5e19 * u.m**-3, ion='p') with pytest.raises(ValueError): Alfven_speed(0.001 * u.T, -5e19 * u.m**-3, ion='p') with pytest.raises(ValueError): Alfven_speed(np.nan * u.T, 1 * u.m**-3, ion='p') with pytest.raises(ValueError): Alfven_speed(1 * u.T, np.nan * u.m**-3, ion='p') with pytest.raises(RelativityError): assert Alfven_speed(np.inf * u.T, 1 * u.m**-3, ion='p') == np.inf * u.m / u.s with pytest.raises(RelativityError): assert Alfven_speed(-np.inf * u.T, 1 * u.m**-3, ion='p') == np.inf * u.m / u.s with pytest.warns(u.UnitsWarning): assert Alfven_speed(1.0, n_i) == Alfven_speed(1.0 * u.T, n_i) Alfven_speed(1 * u.T, 5e19 * u.m**-3, ion='p') # testing for user input z_mean testMeth1 = Alfven_speed(1 * u.T, 5e19 * u.m**-3, ion='p', z_mean=0.8).si.value testTrue1 = 3084015.75214846 errStr = f"Alfven_speed() gave {testMeth1}, should be {testTrue1}." assert np.isclose(testMeth1, testTrue1, atol=0.0, rtol=1e-15), errStr assert_can_handle_nparray(Alfven_speed)
def test_ion_sound_speed(): r"""Test the ion_sound_speed function in parameters.py.""" assert np.isclose(ion_sound_speed(T_i=1.3232 * u.MK, T_e=1.831 * u.MK, ion='p', gamma_e=1, gamma_i=3).value, 218816.06086407552) assert np.isclose(ion_sound_speed(T_i=1.3232 * u.MK, T_e=1.831 * u.MK, n_e=n_e, k=k_1, ion='p', gamma_e=1, gamma_i=3).value, 218816.06086407552) assert np.isclose(ion_sound_speed(T_i=1.3232 * u.MK, T_e=1.831 * u.MK, n_e=n_e, k=k_2, ion='p', gamma_e=1, gamma_i=3).value, 552.3212936293337) assert np.isclose(ion_sound_speed( T_i=0.88 * u.MK, T_e=1.28 * u.MK, n_e=n_e, k=0 * u.m ** -1, ion='p', gamma_e=1.2, gamma_i=3.4).value, 193328.52857788358) # case when Z=1 is assumed # assert ion_sound_speed(T_i=T_i, T_e=T_e, ion='p+') == ion_sound_speed(T_i=T_i, T_e=T_e, # ion='H-1') assert ion_sound_speed(T_i=T_i, T_e=0 * u.K, n_e=n_e, k=k_1, ion='p+').unit.is_equivalent(u.m / u.s) with pytest.raises(RelativityError): ion_sound_speed(T_i=T_i, T_e=T_e, n_e=n_e, k=k_1, gamma_i=np.inf) with pytest.warns(PhysicsWarning): ion_sound_speed(T_i=T_i, T_e=T_e, n_e=n_e) with pytest.warns(PhysicsWarning): ion_sound_speed(T_i=T_i, T_e=T_e, k=k_1) with pytest.raises(u.UnitTypeError): ion_sound_speed(T_i=np.array([5, 6, 5]) * u.K, T_e=np.array([3, 4]) * u.K, n_e=np.array([5, 6, 5]) * u.m ** -3, k=np.array([3, 4]) * u.m ** -3) with pytest.raises(TypeError): # Is this test right?????? ion_sound_speed(5 * u.T) with pytest.raises(TypeError): ion_sound_speed('p') with pytest.raises(PhysicsError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, gamma_i=0.9999) with pytest.raises(PhysicsError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, gamma_e=0.9999) with pytest.raises(TypeError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, gamma_e='sdjklsf') with pytest.raises(TypeError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, gamma_i='fsdfas') with pytest.raises(InvalidParticleError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, ion='cupcakes') with pytest.raises(ValueError): ion_sound_speed(T_i=-np.abs(T_i), T_e=0 * u.K) with pytest.raises(ValueError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, n_e=-np.abs(n_e), k=k_1) with pytest.raises(ValueError): ion_sound_speed(T_i=T_i, T_e=0 * u.K, n_e=n_e, k=-np.abs(k_1)) with pytest.warns(RelativityWarning): ion_sound_speed(T_i=5e11 * u.K, T_e=0 * u.K) with pytest.raises(RelativityError): ion_sound_speed(T_i=5e19 * u.K, T_e=0 * u.K) with pytest.raises(u.UnitTypeError): ion_sound_speed(T_i=5 * u.A, T_e=0 * u.K, n_e=n_e, k=k_1) assert np.isnan(ion_sound_speed(T_i=T_nanarr, T_e=0 * u.K, n_e=n_e, k=k_1)[1]) assert np.isnan(ion_sound_speed(T_e=T_nanarr, T_i=0 * u.K, n_e=n_e, k=k_1)[1]) with pytest.raises(ValueError): ion_sound_speed(T_i=T_negarr, T_e=0 * u.K, n_e=n_e, k=k_1) with pytest.raises(ValueError): ion_sound_speed(T_e=T_negarr, T_i=0 * u.K, n_e=n_e, k=k_1) with pytest.warns(u.UnitsWarning): assert ion_sound_speed(T_e=1.2e6, T_i=0 * u.K, n_e=n_e, k=k_1) == \ ion_sound_speed(T_e=1.2e6 * u.K, T_i=0 * u.K, n_e=n_e, k=k_1) with pytest.warns(u.UnitsWarning): assert ion_sound_speed(T_i=1.3e6, T_e=0 * u.K, n_e=n_e, k=k_1) == \ ion_sound_speed(T_i=1.3e6 * u.K, T_e=0 * u.K, n_e=n_e, k=k_1) ion_sound_speed(T_e=1.2e6 * u.K, T_i=0 * u.K, n_e=n_e, k=k_1) # testing for user input z_mean testMeth1 = ion_sound_speed(T_e=1.2e6 * u.K, T_i=0 * u.K, n_e=n_e, k=0 * u.m ** -1, z_mean=0.8).si.value testTrue1 = 89018.09 errStr = f"ion_sound_speed() gave {testMeth1}, should be {testTrue1}." assert np.isclose(testMeth1, testTrue1, atol=0.0, rtol=1e-6), errStr assert_can_handle_nparray(ion_sound_speed)
def test_handle_nparrays(self): assert_can_handle_nparray(ion_sound_speed)
def test_handle_nparrays(self): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(mass_density)
def test_handle_nparrays(self, insert_some_nans, insert_all_nans): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( Spitzer_resistivity, insert_some_nans, insert_all_nans, {} )
def test_handle_nparrays(self): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(Alfven_speed)
def test_handle_nparrays(self, insert_some_nans, insert_all_nans): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( fundamental_ion_collision_freq, insert_some_nans, insert_all_nans, {} )
def test_handle_nparrays(self, kwargs={"kappa": 2}): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(kappa_thermal_speed, kwargs=kwargs)
def test_handle_nparrays(self, insert_some_nans, insert_all_nans, kwargs): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( impact_parameter, insert_some_nans, insert_all_nans, kwargs )
def test_handle_nparrays(self, insert_some_nans, insert_all_nans, kwargs): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( collision_frequency, insert_some_nans, insert_all_nans, kwargs )
def test_handle_nparrays(self, insert_some_nans, insert_all_nans, kwargs): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( Coulomb_logarithm, insert_some_nans, insert_all_nans, kwargs )
def test_handle_nparrays(self, insert_some_nans, insert_all_nans): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray( coupling_parameter, insert_some_nans, insert_all_nans, {} )
def test_handle_nparrays(self, insert_some_nans, insert_all_nans): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(Knudsen_number, insert_some_nans, insert_all_nans, {})
def test_thermal_speed(): r"""Test the thermal_speed function in parameters.py""" assert thermal_speed(T_e).unit.is_equivalent(u.m / u.s) assert thermal_speed(T_e) > thermal_speed(T_e, 'p') # The NRL Plasma Formulary uses a definition of the electron # thermal speed that differs by a factor of sqrt(2). assert np.isclose(thermal_speed(1 * u.MK).value, 5505694.743141063) with pytest.raises(u.UnitTypeError): thermal_speed(5 * u.m) with pytest.raises(ValueError): thermal_speed(-T_e) with pytest.warns(RelativityWarning): thermal_speed(1e9 * u.K) with pytest.raises(RelativityError): thermal_speed(5e19 * u.K) with pytest.warns(u.UnitsWarning): assert thermal_speed(1e5) == thermal_speed(1e5 * u.K) assert thermal_speed(T_i, particle='p').unit.is_equivalent(u.m / u.s) # The NRL Plasma Formulary uses a definition of the particle thermal # speed that differs by a factor of sqrt(2). assert np.isclose(thermal_speed(1 * u.MK, particle='p').si.value, 128486.56960876315) # Case when Z=1 is assumed assert thermal_speed(T_i, particle='p') == thermal_speed(T_i, particle='H-1+') assert thermal_speed(1 * u.MK, particle='e+') == thermal_speed(1 * u.MK) with pytest.raises(u.UnitTypeError): thermal_speed(5 * u.m, particle='p') with pytest.raises(ValueError): thermal_speed(-T_e, particle='p') with pytest.warns(RelativityWarning): thermal_speed(1e11 * u.K, particle='p') with pytest.raises(RelativityError): thermal_speed(1e14 * u.K, particle='p') with pytest.raises(InvalidParticleError): thermal_speed(T_i, particle='asdfasd') with pytest.warns(u.UnitsWarning): assert thermal_speed(1e6, particle='p') == thermal_speed(1e6 * u.K, particle='p') assert np.isclose(thermal_speed(1e6 * u.K, method="mean_magnitude").si.value, 6212510.3969422) assert np.isclose(thermal_speed(1e6 * u.K, method="rms").si.value, 6743070.475775486) with pytest.raises(ValueError): thermal_speed(T_i, method="sadks") assert_can_handle_nparray(thermal_speed)
def test_thermal_speed(): r"""Test the thermal_speed function in parameters.py""" assert thermal_speed(T_e).unit.is_equivalent(u.m / u.s) assert thermal_speed(T_e) > thermal_speed(T_e, "p") # The NRL Plasma Formulary uses a definition of the electron # thermal speed that differs by a factor of sqrt(2). assert np.isclose(thermal_speed(1 * u.MK).value, 5505694.743141063) with pytest.raises(u.UnitTypeError): thermal_speed(5 * u.m) with pytest.raises(ValueError): thermal_speed(-T_e) with pytest.warns(RelativityWarning): thermal_speed(1e9 * u.K) with pytest.raises(RelativityError): thermal_speed(5e19 * u.K) with pytest.warns(u.UnitsWarning): assert thermal_speed(1e5) == thermal_speed(1e5 * u.K) assert thermal_speed(T_i, particle="p").unit.is_equivalent(u.m / u.s) # The NRL Plasma Formulary uses a definition of the particle thermal # speed that differs by a factor of sqrt(2). assert np.isclose( thermal_speed(1 * u.MK, particle="p").si.value, 128486.56960876315) # Explicitly check all three modes and dimensionalities # ndim = 1 assert np.isclose( thermal_speed(T_e, method="most_probable", ndim=1).si.value, 0.0) # Regression tests start here! assert np.isclose( thermal_speed(T_e, method="rms", ndim=1).si.value, 3893114.2008620175) assert np.isclose( thermal_speed(T_e, method="mean_magnitude", ndim=1).si.value, 3106255.714310189) # ndim = 2 assert np.isclose( thermal_speed(T_e, method="most_probable", ndim=2).si.value, 3893114.2008620175) assert np.isclose( thermal_speed(T_e, method="rms", ndim=2).si.value, 5505694.902726359) assert np.isclose( thermal_speed(T_e, method="mean_magnitude", ndim=2).si.value, 4879295.066124102) # ndim = 3 assert np.isclose( thermal_speed(T_e, method="most_probable", ndim=3).si.value, 5505694.902726359) assert np.isclose( thermal_speed(T_e, method="rms", ndim=3).si.value, 6743071.595560921) assert np.isclose( thermal_speed(T_e, method="mean_magnitude", ndim=3).si.value, 6212511.428620378) # Case when Z=1 is assumed assert thermal_speed(T_i, particle="p") == thermal_speed(T_i, particle="H-1+") assert thermal_speed(1 * u.MK, particle="e+") == thermal_speed(1 * u.MK) with pytest.raises(u.UnitTypeError): thermal_speed(5 * u.m, particle="p") with pytest.raises(ValueError): thermal_speed(-T_e, particle="p") with pytest.warns(RelativityWarning): thermal_speed(1e11 * u.K, particle="p") with pytest.raises(RelativityError): thermal_speed(1e14 * u.K, particle="p") with pytest.raises(InvalidParticleError): thermal_speed(T_i, particle="asdfasd") with pytest.warns(u.UnitsWarning): assert thermal_speed(1e6, particle="p") == thermal_speed(1e6 * u.K, particle="p") assert np.isclose( thermal_speed(1e6 * u.K, method="mean_magnitude").si.value, 6212510.3969422) assert np.isclose( thermal_speed(1e6 * u.K, method="rms").si.value, 6743070.475775486) # Test invalid method with pytest.raises(ValueError): thermal_speed(T_i, method="sadks") # Test invalid ndim with pytest.raises(ValueError): thermal_speed(T_i, ndim=4) assert_can_handle_nparray(thermal_speed)
def test_thermal_pressure(): assert thermal_pressure(T_e, n_i).unit.is_equivalent(u.Pa) # TODO: may be array issues with arg "mass" assert_can_handle_nparray(thermal_pressure)
def test_can_handle_numpy_arrays(self): assert_can_handle_nparray(plasma_frequency)
def test_gyrofrequency(): r"""Test the gyrofrequency function in parameters.py.""" assert gyrofrequency(B).unit.is_equivalent(u.rad / u.s) assert gyrofrequency(B, to_hz=True).unit.is_equivalent(u.Hz) assert np.isclose(gyrofrequency(1 * u.T).value, 175882008784.72018) assert np.isclose(gyrofrequency(2.4 * u.T).value, 422116821083.3284) assert np.isclose(gyrofrequency(1 * u.T, to_hz=True).value, 27992490076.528206) assert np.isclose(gyrofrequency(2.4 * u.T, signed=True).value, -422116821083.3284) assert np.isclose(gyrofrequency(1 * u.G).cgs.value, 1.76e7, rtol=1e-3) with pytest.raises(TypeError): gyrofrequency(u.m) with pytest.raises(u.UnitTypeError): gyrofrequency(u.m * 1) assert np.isnan(gyrofrequency(B_nanarr)[-1]) # The following is a test to check that equivalencies from astropy # are working. omega_ce = gyrofrequency(2.2 * u.T) f_ce = (omega_ce / (2 * np.pi)) / u.rad f_ce_use_equiv = omega_ce.to(u.Hz, equivalencies=[(u.cy / u.s, u.Hz)]) assert np.isclose(f_ce.value, f_ce_use_equiv.value) with pytest.warns(u.UnitsWarning): assert gyrofrequency(5.0) == gyrofrequency(5.0 * u.T) assert gyrofrequency(B, particle=ion).unit.is_equivalent(u.rad / u.s) assert np.isclose(gyrofrequency(1 * u.T, particle='p').value, 95788335.834874) assert np.isclose(gyrofrequency(2.4 * u.T, particle='p').value, 229892006.00369796) assert np.isclose(gyrofrequency(1 * u.G, particle='p').cgs.value, 9.58e3, rtol=2e-3) assert gyrofrequency(-5 * u.T, 'p') == gyrofrequency(5 * u.T, 'p') # Case when Z=1 is assumed # assert gyrofrequency(B, particle='p+') == gyrofrequency(B, particle='H-1') assert gyrofrequency(B, particle='e+') == gyrofrequency(B) with pytest.warns(u.UnitsWarning): gyrofrequency(8, 'p') with pytest.raises(u.UnitTypeError): gyrofrequency(5 * u.m, 'p') with pytest.raises(InvalidParticleError): gyrofrequency(8 * u.T, particle='asdfasd') with pytest.warns(u.UnitsWarning): # TODO this should be WARNS, not RAISES. and it's probably still raised assert gyrofrequency(5.0, 'p') == gyrofrequency(5.0 * u.T, 'p') gyrofrequency(1 * u.T, particle='p') # testing for user input Z testMeth1 = gyrofrequency(1 * u.T, particle='p', Z=0.8).si.value testTrue1 = 76630665.79318453 errStr = f"gyrofrequency() gave {testMeth1}, should be {testTrue1}." assert np.isclose(testMeth1, testTrue1, atol=0.0, rtol=1e-5), errStr assert_can_handle_nparray(gyrofrequency, kwargs={"signed": True}) assert_can_handle_nparray(gyrofrequency, kwargs={"signed": False})
def test_handle_nparrays(self, insert_some_nans, insert_all_nans): """Test for ability to handle numpy array quantities""" assert_can_handle_nparray(mean_free_path, insert_some_nans, insert_all_nans, {})