Example #1
0
 def test_ion_thermal_conductivity_by_model(self, model, expected):
     with pytest.warns(RelativityWarning):
         ct2 = ClassicalTransport(T_e=self.T_e,
                                  n_e=self.n_e,
                                  T_i=self.T_i,
                                  n_i=self.n_i,
                                  ion_particle=self.ion_particle,
                                  model=model)
         testTrue = np.allclose(ct2.ion_thermal_conductivity(),
                                expected,
                                atol=1e-6 * u.W / (u.K * u.m))
         errStr = (f"Ion thermal conductivity in {model} model "
                   f"should be close to {expected} and not "
                   f"{ct2.ion_thermal_conductivity()}.")
     assert testTrue, errStr
Example #2
0
class Test_classical_transport:
    @classmethod
    def setup_class(self):
        """set up some initial values for tests"""
        u.set_enabled_equivalencies(u.temperature_energy())
        self.T_e = 1000 * u.eV
        self.n_e = 2e13 / u.cm**3
        self.ion_particle = 'D +1'
        self.m_i = particle_mass(self.ion_particle)
        self.Z = integer_charge(self.ion_particle)
        self.T_i = self.T_e
        self.n_i = self.n_e / self.Z
        self.B = 0.01 * u.T
        self.coulomb_log_val_ei = 17
        self.coulomb_log_val_ii = 17
        self.hall_e = None
        self.hall_i = None
        self.V_ei = None
        self.V_ii = None
        self.mu = m_e / self.m_i
        self.theta = self.T_e / self.T_i
        self.model = 'Braginskii'
        self.field_orientation = 'all'
        with pytest.warns(RelativityWarning):
            self.ct = ClassicalTransport(
                T_e=self.T_e,
                n_e=self.n_e,
                T_i=self.T_i,
                n_i=self.n_i,
                ion_particle=self.ion_particle,
                Z=self.Z,
                B=self.B,
                model=self.model,
                field_orientation=self.field_orientation,
                coulomb_log_ei=self.coulomb_log_val_ei,
                coulomb_log_ii=self.coulomb_log_val_ii,
                V_ei=self.V_ei,
                V_ii=self.V_ii,
                hall_e=self.hall_e,
                hall_i=self.hall_i,
                mu=self.mu,
                theta=self.theta,
            )
            self.ct_wrapper = ClassicalTransport(
                T_e=self.T_e,
                n_e=self.n_e,
                T_i=self.T_i,
                n_i=self.n_i,
                ion_particle=self.ion_particle,
                Z=self.Z,
                B=self.B,
                model=self.model,
                field_orientation=self.field_orientation,
                mu=self.mu,
                theta=self.theta,
            )
            self.all_variables = self.ct.all_variables()

    def test_spitzer_vs_formulary(self):
        """Spitzer resistivity should agree with approx. in NRL formulary"""
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model='spitzer',
                                     field_orientation='perp')
            alpha_spitzer_perp_NRL = (1.03e-4 * ct2.Z * ct2.coulomb_log_ei *
                                      (ct2.T_e.to(u.eV)).value**(-3 / 2) *
                                      u.Ohm * u.m)
            testTrue = np.isclose(ct2.resistivity().value,
                                  alpha_spitzer_perp_NRL.value,
                                  rtol=2e-2)
            errStr = (f"Resistivity should be close to "
                      f"{alpha_spitzer_perp_NRL.value} "
                      f"and not {ct2.resistivity().value}.")
        assert testTrue, errStr

    def test_resistivity_units(self):
        """output should be a Quantity with units of Ohm m"""
        testTrue = self.ct.resistivity().unit == u.Ohm * u.m
        errStr = (f"Resistivity units should be {u.Ohm * u.m} and "
                  f"not {self.ct.resistivity().unit}.")
        assert testTrue, errStr

    def test_thermoelectric_conductivity_units(self):
        """output should be a Quantity with units of dimensionless"""
        testTrue = self.ct.thermoelectric_conductivity().unit == u.m / u.m
        errStr = (f"Thermoelectric conductivity units should be dimensionless "
                  f"and not {self.ct.thermoelectric_conductivity().unit}.")
        assert testTrue, errStr

    def test_ion_thermal_conductivity_units(self):
        """output should be Quantity with units of W / (m K)"""
        testTrue = self.ct.ion_thermal_conductivity().unit == u.W / u.m / u.K
        errStr = (f"Ion thermal conductivity units "
                  f"should be {u.W / u.m / u.K} "
                  f"and not {self.ct.ion_thermal_conductivity().unit}.")
        assert testTrue, errStr

    def test_electron_thermal_conductivity_units(self):
        """output should be Quantity with units of W / (m K)"""
        testTrue = (self.ct.electron_thermal_conductivity().unit == u.W / u.m /
                    u.K)
        errStr = (f"Electron thermal conductivity units "
                  f"should be {u.W / u.m / u.K} "
                  f"and not {self.ct.electron_thermal_conductivity().unit}.")
        assert testTrue, errStr

    def test_ion_viscosity_units(self):
        """output should be Quantity with units of Pa s """
        testTrue = self.ct.ion_viscosity().unit == u.Pa * u.s
        errStr = (f"Ion viscosity units should be {u.Pa * u.s} "
                  f"and not {self.ct.ion_viscosity().unit}.")
        assert testTrue, errStr

    def test_electron_viscosity_units(self):
        """output should be Quantity with units of Pa s"""
        testTrue = self.ct.electron_viscosity().unit == u.Pa * u.s
        errStr = (f"Electron viscosity units should be {u.Pa * u.s} "
                  f"and not {self.ct.electron_viscosity().unit}.")
        assert testTrue, errStr

    def test_particle_mass(self):
        """should raise ValueError if particle mass not found"""
        with pytest.raises(ValueError):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle='empty moment',
                                     Z=1)

    def test_particle_charge_state(self):
        """should raise ValueError if particle charge state not found"""
        with pytest.raises(InvalidParticleError):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle='empty moment',
                                     m_i=m_p)

    def test_Z_checks(self):
        """should raise ValueError if Z is negative"""
        with pytest.raises(ValueError):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     Z=-1)

    def test_coulomb_log_warnings(self):
        """should warn PhysicsWarning if coulomb log is near 1"""
        with pytest.warns(PhysicsWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     coulomb_log_ii=1.3)

        with pytest.warns(PhysicsWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     coulomb_log_ei=1.3)

    def test_coulomb_log_errors(self):
        """should raise PhysicsError if coulomb log is < 1"""
        with pytest.warns(PhysicsWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     coulomb_log_ii=0.3)

        with pytest.warns(PhysicsWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     coulomb_log_ei=0.3)

    def test_coulomb_log_calc(self):
        """if no coulomb logs are input, they should be calculated"""
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle)
            cl_ii = Coulomb_logarithm(self.T_i, self.n_e,
                                      [self.ion_particle, self.ion_particle],
                                      self.V_ii)
            cl_ei = Coulomb_logarithm(self.T_e, self.n_e,
                                      ['e', self.ion_particle], self.V_ei)
            testTrue = cl_ii == ct2.coulomb_log_ii
            errStr = (f"Ion-ion coulomb logarithm should be {cl_ii} "
                      f"and not {ct2.coulomb_log_ii}.")
        assert testTrue, errStr
        testTrue = cl_ei == ct2.coulomb_log_ei
        errStr = (f"Electron-ion coulomb logarithm should be {cl_ei} "
                  f"and not {ct2.coulomb_log_ei}.")
        assert testTrue, errStr

    def test_hall_calc(self):
        """if no hall parameters are input, they should be calculated"""
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle)
            hall_i = Hall_parameter(ct2.n_i, ct2.T_i, ct2.B, ct2.ion_particle,
                                    ct2.ion_particle, ct2.coulomb_log_ii,
                                    ct2.V_ii)
            hall_e = Hall_parameter(ct2.n_e, ct2.T_e, ct2.B, ct2.ion_particle,
                                    ct2.e_particle, ct2.coulomb_log_ei,
                                    ct2.V_ei)
            testTrue = hall_i == ct2.hall_i
            errStr = (f"Ion hall parameter should be {hall_i} "
                      f"and not {ct2.hall_i}.")
        assert testTrue, errStr
        testTrue = hall_e == ct2.hall_e
        errStr = (f"Electron hall parameter should be {hall_e} "
                  f"and not {ct2.hall_e}.")
        assert testTrue, errStr

    def test_invalid_model(self):
        with pytest.raises(ValueError):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model="standard")

    def test_invalid_field(self):
        with pytest.raises(ValueError):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     field_orientation='to the left')

    def test_precalculated_parameters(self):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     hall_i=0,
                                     hall_e=0)
            testTrue = np.isclose(ct2.resistivity(),
                                  2.8184954e-8 * u.Ohm * u.m,
                                  atol=1e-6 * u.Ohm * u.m)
            errStr = (
                f"Resistivity should be close to "
                f"{2.8184954e-8 * u.Ohm * u.m} and not {ct2.resistivity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize("model, method, field_orientation, expected", [
        ("ji-held", "resistivity", "all", 3),
        ("ji-held", "thermoelectric_conductivity", "all", 3),
        ("ji-held", "electron_thermal_conductivity", "all", 3),
        ("ji-held", "ion_thermal_conductivity", "all", 3),
        ("spitzer", "resistivity", "all", 2),
    ])
    def test_number_of_returns(self, model, method, field_orientation,
                               expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model,
                                     field_orientation=field_orientation)
            method_to_call = getattr(ct2, method)
            testTrue = np.size(method_to_call()) == expected
            errStr = (f"{method} in {model} model returns "
                      f"{np.size(method_to_call())} objects. "
                      f"Expected to return {expected} objects.")
        assert testTrue, errStr

    @pytest.mark.parametrize("model, expected",
                             [("ji-held", 2.77028546e-8 * u.Ohm * u.m),
                              ("spitzer", 2.78349687e-8 * u.Ohm * u.m),
                              ("braginskii", 2.78349687e-8 * u.Ohm * u.m)])
    def test_resistivity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.isclose(ct2.resistivity(),
                                  expected,
                                  atol=1e-6 * u.Ohm * u.m)
            errStr = (f"Resistivity in {model} model should be "
                      f"close to {expected} and not {ct2.resistivity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize("model, expected",
                             [("ji-held", 0.702 * u.s / u.s),
                              ("spitzer", 0.69944979 * u.s / u.s),
                              ("braginskii", 0.711084 * u.s / u.s)])
    def test_thermoelectric_conductivity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.isclose(ct2.thermoelectric_conductivity(),
                                  expected,
                                  atol=1e-6 * u.s / u.s)
            errStr = (f"Thermoelectric conductivity in {model} model "
                      f"should be close {expected} and not "
                      f"{ct2.thermoelectric_conductivity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize(
        "model, expected",
        [("ji-held",
          np.array([0.07674402, 0.07674402, 0.07674402, 0, 0]) * u.Pa * u.s),
         ("braginskii",
          np.array([0.07674402, 0.07671874, 0.07671874, 0, 0]) * u.Pa * u.s)])
    def test_electron_viscosity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.allclose(ct2.electron_viscosity(),
                                   expected,
                                   atol=1e-6 * u.Pa * u.s)
            errStr = (
                f"Electron viscosity in {model} model should be close to "
                f"{expected} and not {ct2.electron_viscosity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize(
        "model, expected",
        [("ji-held",
          np.array([7.96226452, 7.96226452, 7.96226452, 0, 0]) * u.Pa * u.s),
         ("braginskii",
          np.array([7.91936173, 7.89528642, 7.89528642, 0, 0]) * u.Pa * u.s)])
    def test_ion_viscosity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.allclose(ct2.ion_viscosity(),
                                   expected,
                                   atol=1e-6 * u.Pa * u.s)
            errStr = (
                f"Electron viscosity in {model} model should be close to "
                f"{expected} and not {ct2.electron_viscosity()}")
        assert testTrue, errStr

    @pytest.mark.parametrize(
        "model, expected",
        [("ji-held", 5084253.556001088 * u.W / (u.K * u.m)),
         ("spitzer", 5082147.824377487 * u.W / (u.K * u.m)),
         ("braginskii", 5016895.3386957785 * u.W / (u.K * u.m))])
    def test_electron_thermal_conductivity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.allclose(ct2.electron_thermal_conductivity(),
                                   expected,
                                   atol=1e-6 * u.W / (u.K * u.m))
            errStr = (f"Electron thermal conductivity in {model} model "
                      f"should be close to {expected} and not "
                      f"{ct2.electron_thermal_conductivity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize(
        "model, expected",
        [("ji-held", 134547.55528106514 * u.W / (u.K * u.m)),
         ("braginskii", 133052.21732349042 * u.W / (u.K * u.m))])
    def test_ion_thermal_conductivity_by_model(self, model, expected):
        with pytest.warns(RelativityWarning):
            ct2 = ClassicalTransport(T_e=self.T_e,
                                     n_e=self.n_e,
                                     T_i=self.T_i,
                                     n_i=self.n_i,
                                     ion_particle=self.ion_particle,
                                     model=model)
            testTrue = np.allclose(ct2.ion_thermal_conductivity(),
                                   expected,
                                   atol=1e-6 * u.W / (u.K * u.m))
            errStr = (f"Ion thermal conductivity in {model} model "
                      f"should be close to {expected} and not "
                      f"{ct2.ion_thermal_conductivity()}.")
        assert testTrue, errStr

    @pytest.mark.parametrize(
        "key, expected", {
            'resistivity': [2.84304305e-08, 5.54447070e-08, 1.67853407e-12],
            'thermoelectric conductivity':
            [7.11083999e-01, 1.61011272e-09, 2.66496639e-05],
            'electron thermal conductivity':
            [4.91374931e+06, 2.28808496e-03, 6.90324259e+01],
            'electron viscosity': [
                7.51661800e-02, 5.23617668e-21, 2.09447067e-20, 1.61841341e-11,
                3.23682681e-11
            ],
            'ion thermal conductivity':
            [1.41709276e+05, 4.20329493e-02, 6.90323924e+01],
            'ion viscosity': [
                8.43463595e+00, 8.84513731e-13, 3.53805159e-12, 2.54483240e-06,
                5.08966116e-06
            ]
        }.items())
    def test_dictionary(self, key, expected):
        calculated = self.all_variables[key]
        testTrue = np.allclose(expected, calculated.si.value)
        errStr = (f"Expected values of {key} are {expected} and not"
                  f"{calculated.si.value}.")
        assert testTrue, errStr

    def test_resistivity_wrapper(self):
        with pytest.warns(RelativityWarning):
            assert_quantity_allclose(
                resistivity(
                    T_e=self.T_e,
                    n_e=self.n_e,
                    T_i=self.T_i,
                    n_i=self.n_i,
                    ion_particle=self.ion_particle,
                    Z=self.Z,
                    B=self.B,
                    model=self.model,
                    field_orientation=self.field_orientation,
                    mu=self.mu,
                    theta=self.theta,
                ), self.ct_wrapper.resistivity())

    def test_thermoelectric_conductivity_wrapper(self):
        with pytest.warns(RelativityWarning):
            val1 = thermoelectric_conductivity(
                T_e=self.T_e,
                n_e=self.n_e,
                T_i=self.T_i,
                n_i=self.n_i,
                ion_particle=self.ion_particle,
                Z=self.Z,
                B=self.B,
                model=self.model,
                field_orientation=self.field_orientation,
                mu=self.mu,
                theta=self.theta,
            )
            val2 = self.ct_wrapper.thermoelectric_conductivity()
            assert_quantity_allclose(val1, val2)

    def test_ion_thermal_conductivity_wrapper(self):
        with pytest.warns(RelativityWarning):
            wrapped = ion_thermal_conductivity(
                T_e=self.T_e,
                n_e=self.n_e,
                T_i=self.T_i,
                n_i=self.n_i,
                ion_particle=self.ion_particle,
                Z=self.Z,
                B=self.B,
                model=self.model,
                field_orientation=self.field_orientation,
                mu=self.mu,
                theta=self.theta,
            )
            assert_quantity_allclose(
                wrapped, self.ct_wrapper.ion_thermal_conductivity())

    def test_electron_thermal_conductivity_wrapper(self):
        with pytest.warns(RelativityWarning):
            wrapped = electron_thermal_conductivity(
                T_e=self.T_e,
                n_e=self.n_e,
                T_i=self.T_i,
                n_i=self.n_i,
                ion_particle=self.ion_particle,
                Z=self.Z,
                B=self.B,
                model=self.model,
                field_orientation=self.field_orientation,
                mu=self.mu,
                theta=self.theta,
            )
            assert_quantity_allclose(
                wrapped, self.ct_wrapper.electron_thermal_conductivity())

    def test_ion_viscosity_wrapper(self):
        with pytest.warns(RelativityWarning):
            assert_quantity_allclose(
                ion_viscosity(
                    T_e=self.T_e,
                    n_e=self.n_e,
                    T_i=self.T_i,
                    n_i=self.n_i,
                    ion_particle=self.ion_particle,
                    Z=self.Z,
                    B=self.B,
                    model=self.model,
                    field_orientation=self.field_orientation,
                    mu=self.mu,
                    theta=self.theta,
                ), self.ct_wrapper.ion_viscosity())

    def test_electron_viscosity_wrapper(self):
        with pytest.warns(RelativityWarning):
            assert_quantity_allclose(
                electron_viscosity(
                    T_e=self.T_e,
                    n_e=self.n_e,
                    T_i=self.T_i,
                    n_i=self.n_i,
                    ion_particle=self.ion_particle,
                    Z=self.Z,
                    B=self.B,
                    model=self.model,
                    field_orientation=self.field_orientation,
                    mu=self.mu,
                    theta=self.theta,
                ), self.ct_wrapper.electron_viscosity())