Skip to content

energy_data

Neutron energy spectrum data.

BALLABIO_DD_NEUTRON = BallabioEnergySpectrum(energy_0=2449.5, omega_0=82.542, energy_shift_coeffs=BallabioCoefficients(a1=4.69515, a2=-0.040729, a3=0.47, a4=0.81844), width_correction_coeffs=BallabioCoefficients(a1=0.0017013, a2=0.16888, a3=0.49, a4=0.0007946)) module-attribute

Ballabio et al. fit data for relativistic fusion reaction neutron energy Gaussian spectra.

BALLABIO_DT_NEUTRON = BallabioEnergySpectrum(energy_0=14021.0, omega_0=177.259, energy_shift_coeffs=BallabioCoefficients(a1=5.30509, a2=0.0024736, a3=1.84, a4=1.3818), width_correction_coeffs=BallabioCoefficients(a1=0.00051068, a2=0.0076223, a3=1.78, a4=8.7691e-05)) module-attribute

Ballabio et al. fit data for relativistic fusion reaction neutron energy Gaussian spectra.

TWO_SQRT_2LN2 = 2.3548200450309493 module-attribute

Convert a string or number to a floating point number, if possible.

TTNeutronEnergyDataSpectrum

Fusion neutron energy data spectrum.

Source code in tokamak_neutron_source/energy_data.py
class TTNeutronEnergyDataSpectrum:
    """Fusion neutron energy data spectrum."""

    def __init__(self, file_name: str):
        """
        Parameters
        ----------
        file_name:
            Data file

        Raises
        ------
        EnergySpectrumError
            Data file path is not a file
        """
        path = get_tns_path("data/spectra")
        path = Path(path, file_name)
        if not path.is_file():
            raise EnergySpectrumError(f"Energy spectrum data file {path} is not a file!")

        file = path.as_posix()
        data = np.genfromtxt(file, comments="#")

        # The energy bins for this spectrum are hard-coded here
        # (no sense in interpolating).
        self._energy = raw_uc(data[:, 0], "MeV", "keV")

        self._min_temp = 1.0  # [keV]
        self._max_temp = 20.0  # [keV]
        temperature = np.linspace(self._min_temp, self._max_temp, 40)  # [keV]
        spectra = raw_uc(data[:, 1:], "1/MeV", "1/keV")
        self._interpolator = RegularGridInterpolator(
            (self._energy, temperature),
            spectra,
            method="linear",
            bounds_error=True,
            fill_value=np.nan,
        )

    def __call__(self, temp_kev: float) -> tuple[npt.NDArray, npt.NDArray]:
        """Get spectrum at a given temperature"""  # noqa: DOC201
        if not self._min_temp < temp_kev < self._max_temp:
            logger.warning(
                f"T-T spectral data not available at T = {temp_kev} keV, clipping back "
                f"to the bounds of {self._min_temp} <= T <= {self._max_temp} keV",
                stacklevel=2,
            )
            temp_kev = np.clip(temp_kev, self._min_temp, self._max_temp)

        return self._energy, self._interpolator((self._energy, temp_kev))

BallabioCoefficients

Ballabio et al. fit parameterisation coefficients for Ti < 40.0 keV

Source code in tokamak_neutron_source/energy_data.py
@dataclass
class BallabioCoefficients:
    """
    Ballabio et al. fit parameterisation coefficients for Ti < 40.0 keV
    """

    a1: float
    a2: float
    a3: float
    a4: float

    def fit(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
        """
        Calculate the value of the parameterisation at given temperature(s).

        Parameters
        ----------
        temp_kev:
            Ion temperatures at which to calculate the fit

        Returns
        -------
        :
            Values of the fit

        Notes
        -----
        Valid over 0.0 to 40.0 keV
        """
        return (
            self.a1 / (1 + self.a2 * temp_kev**self.a3) * temp_kev ** (2 / 3)
            + self.a4 * temp_kev
        )

fit(temp_kev)

Calculate the value of the parameterisation at given temperature(s).

Parameters:

Name Type Description Default
temp_kev float | ndarray[tuple[Any, ...], dtype[~_ScalarT]]

Ion temperatures at which to calculate the fit

required

Returns:

Type Description
float | ndarray[tuple[Any, ...], dtype[~_ScalarT]]

Values of the fit

Notes

Valid over 0.0 to 40.0 keV

Source code in tokamak_neutron_source/energy_data.py
def fit(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
    """
    Calculate the value of the parameterisation at given temperature(s).

    Parameters
    ----------
    temp_kev:
        Ion temperatures at which to calculate the fit

    Returns
    -------
    :
        Values of the fit

    Notes
    -----
    Valid over 0.0 to 40.0 keV
    """
    return (
        self.a1 / (1 + self.a2 * temp_kev**self.a3) * temp_kev ** (2 / 3)
        + self.a4 * temp_kev
    )

BallabioEnergySpectrum

Ballabio et al. fit data for relativistic fusion reaction neutron energy Gaussian spectra.

Source code in tokamak_neutron_source/energy_data.py
@dataclass
class BallabioEnergySpectrum:
    """
    Ballabio et al. fit data for relativistic fusion reaction neutron energy Gaussian
    spectra.
    """

    """E_0"""
    energy_0: float  # [keV]

    omega_0: float  # [keV]

    r"""\Delta E_{th} coefficients"""
    energy_shift_coeffs: BallabioCoefficients

    r"""\delta_{\omega} coefficients"""
    width_correction_coeffs: BallabioCoefficients

    def energy_shift(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
        r"""
        Calculate the energy shift \Delta E_{th} at a given ion temperature.
        """  # noqa: DOC201
        return self.energy_shift_coeffs.fit(temp_kev)

    def width_correction(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
        r"""
        Calculate the width correction \delta_{\omega} at a given ion temperature.
        """  # noqa: DOC201
        return self.width_correction_coeffs.fit(temp_kev)

    def mean_energy(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
        """
        Calculate the mean neutron energy at a given ion temperature
        (primary first moment: mu).
        """  # noqa: DOC201
        return self.energy_0 + self.energy_shift(temp_kev)

    def std_deviation(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
        """
        Calculate the standard deviation of the neutron energy spectrum at a given ion
        temperature (primary second moment: sigma)
        """  # noqa: DOC201
        # Full width at half maximum (FWHM)
        w_12 = self.omega_0 * (1 + self.width_correction(temp_kev)) * np.sqrt(temp_kev)
        return w_12 / TWO_SQRT_2LN2

energy_shift(temp_kev)

Calculate the energy shift \Delta E_{th} at a given ion temperature.

Source code in tokamak_neutron_source/energy_data.py
def energy_shift(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
    r"""
    Calculate the energy shift \Delta E_{th} at a given ion temperature.
    """  # noqa: DOC201
    return self.energy_shift_coeffs.fit(temp_kev)

width_correction(temp_kev)

Calculate the width correction \delta_{\omega} at a given ion temperature.

Source code in tokamak_neutron_source/energy_data.py
def width_correction(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
    r"""
    Calculate the width correction \delta_{\omega} at a given ion temperature.
    """  # noqa: DOC201
    return self.width_correction_coeffs.fit(temp_kev)

mean_energy(temp_kev)

Calculate the mean neutron energy at a given ion temperature (primary first moment: mu).

Source code in tokamak_neutron_source/energy_data.py
def mean_energy(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
    """
    Calculate the mean neutron energy at a given ion temperature
    (primary first moment: mu).
    """  # noqa: DOC201
    return self.energy_0 + self.energy_shift(temp_kev)

std_deviation(temp_kev)

Calculate the standard deviation of the neutron energy spectrum at a given ion temperature (primary second moment: sigma)

Source code in tokamak_neutron_source/energy_data.py
def std_deviation(self, temp_kev: float | npt.NDArray) -> float | npt.NDArray:
    """
    Calculate the standard deviation of the neutron energy spectrum at a given ion
    temperature (primary second moment: sigma)
    """  # noqa: DOC201
    # Full width at half maximum (FWHM)
    w_12 = self.omega_0 * (1 + self.width_correction(temp_kev)) * np.sqrt(temp_kev)
    return w_12 / TWO_SQRT_2LN2

EnergySpectrumError

Bases: tokamak_neutron_source.error.TNSError

Energy spectrum error class

Source code in tokamak_neutron_source/error.py
class EnergySpectrumError(TNSError):
    """Energy spectrum error class"""

TT_N_SPECTRUM(temp_kev)

Fusion neutron energy data spectrum.

get_tns_path(path='', subfolder='tokamak_neutron_source')

Get a tns path of a module subfolder. Defaults to root folder.

Parameters:

Name Type Description Default
path str

The desired path from which to create a full path

''
subfolder str

The subfolder (from the tokamak_neutron_source root) in which to create a path Defaults to the source code folder, but can be e.g. 'tests', or 'data'

'tokamak_neutron_source'

Returns:

Type Description
Path

The full path to the desired path in the subfolder specified

Source code in tokamak_neutron_source/tools.py
def get_tns_path(path: str = "", subfolder: str = "tokamak_neutron_source") -> Path:
    """
    Get a tns path of a module subfolder. Defaults to root folder.

    Parameters
    ----------
    path:
        The desired path from which to create a full path
    subfolder:
        The subfolder (from the tokamak_neutron_source root) in which to create a path
        Defaults to the source code folder, but can be e.g. 'tests', or 'data'

    Returns
    -------
    :
        The full path to the desired `path` in the subfolder specified
    """
    root = get_tns_root()
    if "egg" in root:
        return Path(f"/{subfolder}")

    path = path.replace("/", os.sep)
    main_path = _get_relpath(root, subfolder)
    return Path(_get_relpath(main_path, path))

raw_uc(value, unit_from, unit_to)

Raw unit converter Converts a value from one unit to another

Parameters:

Name Type Description Default
value ValueLikeT

value to convert

required
unit_from str | Unit

unit to convert from

required
unit_to str | Unit

unit to convert to

required

Returns:

Type Description
ValueLikeT

converted value

Source code in tokamak_neutron_source/constants.py
def raw_uc(
    value: ValueLikeT,
    unit_from: str | ureg.Unit,
    unit_to: str | ureg.Unit,
) -> ValueLikeT:
    """
    Raw unit converter
    Converts a value from one unit to another

    Parameters
    ----------
    value:
        value to convert
    unit_from:
        unit to convert from
    unit_to:
        unit to convert to

    Returns
    -------
    :
        converted value
    """
    try:
        return (
            ureg.Quantity(value, ureg.Unit(unit_from)).to(ureg.Unit(unit_to)).magnitude
        )
    except ValueError:
        # Catch scales on units eg the ridculousness of this unit: 10^19/m^3
        unit_from_q = ureg.Quantity(unit_from)
        unit_to_q = ureg.Quantity(unit_to)
        return (
            ureg.Quantity(value * unit_from_q).to(unit_to_q.units).magnitude
            / unit_to_q.magnitude
        )