def _check_relativistic(V, funcname, betafrac=0.05): r""" Warn or raise error for relativistic or superrelativistic velocities. Parameters ---------- V : ~astropy.units.Quantity A velocity. funcname : str The name of the original function to be printed in the error messages. betafrac : float, optional The minimum fraction of the speed of light that will generate a warning. Defaults to 5%. Raises ------ TypeError If `V` is not a `~astropy.units.Quantity`. ~astropy.units.UnitConversionError If `V` is not in units of velocity. ValueError If `V` contains any `~numpy.nan` values. RelativityError If `V` is greater than or equal to the speed of light. Warns ----- ~plasmapy.utils.RelativityWarning If `V` is greater than or equal to the specified fraction of the speed of light. Examples -------- >>> from astropy import units as u >>> _check_relativistic(1*u.m/u.s, 'function_calling_this') """ # TODO: Replace `funcname` with func.__name__? errmsg = ("V must be a Quantity with units of velocity in" "_check_relativistic") if not isinstance(V, u.Quantity): raise TypeError(errmsg) try: V_over_c = (V / c).to_value(u.dimensionless_unscaled) except Exception: raise u.UnitConversionError(errmsg) beta = np.max(np.abs((V_over_c))) if beta == np.inf: raise RelativityError(f"{funcname} is yielding an infinite velocity.") elif beta >= 1: raise RelativityError( f"{funcname} is yielding a velocity that is {str(round(beta, 3))} " f"times the speed of light.") elif beta >= betafrac: warnings.warn( f"{funcname} is yielding a velocity that is " f"{str(round(beta * 100, 3))}% of the speed of " f"light. Relativistic effects may be important.", RelativityWarning)
def _check_relativistic(V, funcname, betafrac=0.1): r""" Warn or raise error for relativistic or superrelativistic velocities. Parameters ---------- V : ~astropy.units.Quantity A velocity. funcname : str The name of the original function to be printed in the error messages. betafrac : float The minimum fraction of the speed of light that will generate a warning. Raises ------ TypeError If `V` is not a `~astropy.units.Quantity`. ~astropy.units.UnitConversionError If `V` is not in units of velocity. ValueError If `V` contains any `~numpy.nan` values. RelativityError If `V` is greater than or equal to the speed of light. Warns ----- RelativityWarning If `V` is greater than or equal to the specified fraction of the speed of light. Examples -------- >>> from astropy import units as u >>> _check_relativistic(1*u.m/u.s, 'function_calling_this') """ # TODO: Replace `funcname` with func.__name__? errmsg = ("V must be a Quantity with units of velocity in" "_check_relativistic") if not isinstance(V, u.Quantity): raise TypeError(errmsg) if V.si.unit != u.m/u.s: raise u.UnitConversionError(errmsg) if np.any(np.isnan(V.value)): raise ValueError("V includes NaNs in " + funcname) beta = np.max(np.abs((V.si/c).value)) if beta == np.inf: raise RelativityError(funcname + " is yielding an infinite velocity.") elif beta >= 1: raise RelativityError(funcname + " is yielding a velocity that is " + str(round(beta, 3)) + " times the speed of " + "light.") elif beta >= betafrac: warnings.warn(funcname + " is yielding a velocity that is " + str(round(beta*100, 3)) + "% of the speed of " + "light. Relativistic effects may be important.", RelativityWarning)