How to use the plasmapy.utils function in plasmapy

To help you get started, we’ve selected a few plasmapy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PlasmaPy / PlasmaPy / plasmapy / formulary / braginskii.py View on Github external
# first, determine if arbitrary Z values are allowed in the theory
    arbitrary_Z_allowed = False
    the_arbitrary_idx = np.nan
    for idx, allowed_Z_val in enumerate(allowed_Z):
        if allowed_Z_val == "arbitrary":
            arbitrary_Z_allowed = True
            the_arbitrary_idx = idx
    # next, search the allowed_Z for a match to the current Z
    Z_idx = np.nan
    for idx, allowed_Z_val in enumerate(allowed_Z):
        if Z == allowed_Z_val:
            Z_idx = idx
    # at this point we have looped through allowed_Z and either found a match
    # or not. If we haven't found a match and arbitrary Z aren't allowed, break
    if np.isnan(Z_idx) and not arbitrary_Z_allowed:
        raise utils.PhysicsError(f"{Z} is not an allowed Z value")
    elif np.isnan(Z_idx):  # allowed arbitrary Z
        # return a Z_idx pointing to the 'arbitrary'
        Z_idx = the_arbitrary_idx
    else:  # allowed Z
        pass
    # we have got the Z_idx we want. return
    return Z_idx
github PlasmaPy / PlasmaPy / plasmapy / formulary / collisions.py View on Github external
def _replaceNanVwithThermalV(V, T, m):
    """
    Get thermal velocity of system if no velocity is given, for a given mass.
    Handles vector checks for V, you must already know that T and m are okay.
    """
    if np.any(V == 0):
        raise utils.PhysicsError("You cannot have a collision for zero velocity!")
    # getting thermal velocity of system if no velocity is given
    if V is None:
        V = parameters.thermal_speed(T, mass=m)
    elif np.any(np.isnan(V)):
        if np.isscalar(V.value) and np.isscalar(T.value):
            V = parameters.thermal_speed(T, mass=m)
        elif np.isscalar(V.value):
            V = parameters.thermal_speed(T, mass=m)
        elif np.isscalar(T.value):
            V = V.copy()
            V[np.isnan(V)] = parameters.thermal_speed(T, mass=m)
        else:
            V = V.copy()
            V[np.isnan(V)] = parameters.thermal_speed(T[np.isnan(V)], mass=m)
    return V
github PlasmaPy / PlasmaPy / plasmapy / formulary / collisions.py View on Github external
)
    # applying dimensionless units
    ln_Lambda = ln_Lambda.to(u.dimensionless_unscaled).value
    # Allow NaNs through the < checks without warning
    with np.errstate(invalid="ignore"):
        if np.any(ln_Lambda < 2) and method in ["classical", "GMS-1", "GMS-2"]:
            warnings.warn(
                f"Coulomb logarithm is {ln_Lambda} and {method} relies on "
                "weak coupling.",
                utils.CouplingWarning,
            )
        elif np.any(ln_Lambda < 4):
            warnings.warn(
                f"Coulomb logarithm is {ln_Lambda}, you might have strong "
                "coupling effects",
                utils.CouplingWarning,
            )
    return ln_Lambda
github PlasmaPy / PlasmaPy / plasmapy / formulary / collisions.py View on Github external
ln_Lambda[ln_Lambda < 2] = 2 * u.dimensionless_unscaled
    elif method in ("GMS-4", "GMS-5", "GMS-6"):
        ln_Lambda = 0.5 * np.log(1 + bmax ** 2 / bmin ** 2)
    else:
        raise ValueError(
            "Unknown method! Choose from 'classical' and 'GMS-N', N from 1 to 6."
        )
    # applying dimensionless units
    ln_Lambda = ln_Lambda.to(u.dimensionless_unscaled).value
    # Allow NaNs through the < checks without warning
    with np.errstate(invalid="ignore"):
        if np.any(ln_Lambda < 2) and method in ["classical", "GMS-1", "GMS-2"]:
            warnings.warn(
                f"Coulomb logarithm is {ln_Lambda} and {method} relies on "
                "weak coupling.",
                utils.CouplingWarning,
            )
        elif np.any(ln_Lambda < 4):
            warnings.warn(
                f"Coulomb logarithm is {ln_Lambda}, you might have strong "
                "coupling effects",
                utils.CouplingWarning,
            )
    return ln_Lambda
github PlasmaPy / PlasmaPy / plasmapy / formulary / relativity.py View on Github external
The Lorentz factor is approximately one for sub-relativistic
    velocities, and goes to infinity as the velocity approaches the
    speed of light.

    Examples
    --------
    >>> from astropy import units as u
    >>> velocity = 1.4e8 * u.m / u.s
    >>> Lorentz_factor(velocity)
    1.130885603948959
    >>> Lorentz_factor(299792458*u.m/u.s)
    inf
    """

    if not np.all(np.abs(V) <= c):
        raise utils.RelativityError(
            "The Lorentz factor cannot be calculated for "
            "speeds faster than the speed of light. "
        )

    if V.size > 1:

        gamma = np.zeros_like(V.value)

        equals_c = np.abs(V) == c
        is_slow = ~equals_c

        gamma[is_slow] = ((1 - (V[is_slow] / c) ** 2) ** -0.5).value
        gamma[equals_c] = np.inf

    else:
        if np.abs(V) == c:
github PlasmaPy / PlasmaPy / plasmapy / formulary / braginskii.py View on Github external
else:
            self.coulomb_log_ei = Coulomb_logarithm(
                T_e, n_e, (self.e_particle, self.ion), V_ei, method=coulomb_log_method
            )

        if self.coulomb_log_ei < 1:
            # TODO discuss whether this is not too strict
            raise PhysicsError(
                f"Coulomb logarithm is {coulomb_log_ei} (below 1),"
                "this is probably not physical!"
            )
        elif self.coulomb_log_ei < 4:
            warnings.warn(
                f"Coulomb logarithm is {coulomb_log_ei},"
                f" you might have strong coupling effects",
                utils.CouplingWarning,
            )

        if coulomb_log_ii is not None:
            self.coulomb_log_ii = coulomb_log_ii
        else:
            self.coulomb_log_ii = Coulomb_logarithm(
                T_i,
                n_e,  # this is not a typo!
                (self.ion, self.ion),
                V_ii,
                method=coulomb_log_method,
            )

        if self.coulomb_log_ii < 1:
            # TODO discuss whether this is not too strict
            raise PhysicsError(
github PlasmaPy / PlasmaPy / plasmapy / formulary / braginskii.py View on Github external
(self.ion, self.ion),
                V_ii,
                method=coulomb_log_method,
            )

        if self.coulomb_log_ii < 1:
            # TODO discuss whether this is not too strict
            raise PhysicsError(
                f"Coulomb logarithm is {coulomb_log_ii} (below 1),"
                "this is probably not physical!"
            )
        elif self.coulomb_log_ii < 4:
            warnings.warn(
                f"Coulomb logarithm is {coulomb_log_ii},"
                f" you might have strong coupling effects",
                utils.CouplingWarning,
            )

        # calculate Hall parameters if not forced in input
        if hall_e is not None:
            self.hall_e = hall_e
        else:
            self.hall_e = Hall_parameter(
                n_e,
                T_e,
                B,
                self.ion,
                self.e_particle,
                coulomb_log_ei,
                V_ei,
                coulomb_log_method=coulomb_log_method,
            )