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)
Esempio n. 2
0
 def time_Alfven_speed(self):
     Alfven_speed(1 * u.T,
                 1e-8 * u.kg * u.m ** -3)