"""
Classes representing different toroidal current density profiles
in the plasma.
Copyright 2024 Nicola C. Amorisco, Adriano Agnello, George K. Holt, Ben Dudson.
Modified substantially from the original FreeGS code.
This file is part of FreeGSNKE.
FreeGSNKE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeGSNKE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import logging
import numpy as np
from numpy import pi, sqrt
from scipy.interpolate import CubicSpline, UnivariateSpline
from scipy.special import beta as spbeta
from scipy.special import betainc as spbinc
from . import critical
from .gradshafranov import mu0
logger = logging.getLogger(__name__)
# Gauss-Legendre nodes/weights (on [-1, 1]) used to integrate p' and FF' over psi_n
_GAUSS_LEGENDRE_NODES, _GAUSS_LEGENDRE_WEIGHTS = np.polynomial.legendre.leggauss(128)
[docs]
class Profile(object):
"""
Base class from which profiles classes can inherit.
"""
[docs]
def Jtor(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_bndry: float | None = None,
) -> np.ndarray:
"""
Calculate the toroidal current density on the computational grid.
This convenience wrapper combines the generic critical-point and
core-mask identification in :meth:`Jtor_part1` with the profile-specific
current-density calculation implemented by each subclass in
:meth:`Jtor_part2`.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_bndry : float or None, optional
Value of the poloidal field flux at the plasma boundary.
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2].
"""
if not hasattr(self, "mask_inside_limiter"):
self.mask_inside_limiter = np.ones_like(psi, dtype=bool)
if not hasattr(self, "mask_outside_limiter"):
self.mask_outside_limiter = np.zeros_like(psi, dtype=float)
opt, xpt, core_mask, psi_bndry = self.Jtor_part1(
R,
Z,
psi,
psi_bndry=psi_bndry,
mask_outside_limiter=self.mask_outside_limiter,
)
if core_mask is None:
core_mask = np.ones_like(psi, dtype=bool)
uses_xpoint_boundary = len(xpt) > 0 and np.isclose(psi_bndry, xpt[0][2])
self.opt = opt
self.xpt = xpt
self.psi_axis = opt[0][2]
self.psi_bndry = psi_bndry
self.diverted_core_mask = core_mask
self.limiter_core_mask = core_mask
self.flag_limiter = not uses_xpoint_boundary
return self.Jtor_part2(R, Z, psi, opt[0][2], psi_bndry, core_mask)
@staticmethod
def _integrate_to_boundary(func, psinorm):
"""
Integrate a function of the normalised flux from psinorm to 1 (all values at once).
Uses a vectorised Gauss-Legendre rule, so `func` is called once with an array.
Parameters
----------
func : callable
Function of the normalised psi (accepting arrays), e.g. pprime or ffprime.
psinorm : float or np.array
Lower integration limit(s) (normalised psi), between 0 and 1.
Returns
-------
float or np.array
The integral of `func` over [psinorm, 1], in the shape of `psinorm`.
"""
psinorm = np.asarray(psinorm, dtype=float)
lower = psinorm.reshape(-1, 1)
half_width = 0.5 * (1.0 - lower)
points = half_width * (_GAUSS_LEGENDRE_NODES[None, :] + 1.0) + lower
values = np.sum(
half_width * _GAUSS_LEGENDRE_WEIGHTS[None, :] * func(points), axis=1
)
if psinorm.ndim == 0:
return float(values[0])
return values.reshape(psinorm.shape)
[docs]
def pressure(self, psinorm):
"""
Calculate the 1D pressure profile in the plasma (vs. the normalised
poloidal flux) by integrating the pprime profile.
Parameters
----------
psinorm : float or np.array
Normalised psi value(s) between 0 and 1.
Returns
-------
float or np.array
The computed pressure values at each normalised psi value.
"""
# integral in normalised psi, converted to an integral in psi
# (pressure at the edge is zero so no need for constant of integration)
return self._integrate_to_boundary(self.pprime, psinorm) * (
self.psi_axis - self.psi_bndry
)
[docs]
def fpol(self, psinorm: np.ndarray | float) -> np.ndarray | float:
"""
Calculate the 1D toroidal magnetic profile in the plasma (vs. the normalised
poloidal flux) by integrating the ffprime profile (ffprime = 0.5*d/dpsi(f^2)).
Parameters
----------
psinorm : float or np.array
Normalised psi value(s) between 0 and 1.
Returns
-------
float or np.array
The computed F values at each normalised psi value.
Raises
------
ValueError
If the input and output arrays have different lengths.
"""
# integral in normalised psi, converted to an integral in psi
val = self._integrate_to_boundary(self.ffprime, psinorm) * (
self.psi_axis - self.psi_bndry
)
# ffprime = 0.5*d/dpsi(f^2)
# apply boundary condition at psinorm=1 val = fvac**2
return sqrt(2.0 * val + self.fvac() ** 2)
[docs]
def Jtor_part1(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_bndry: float | None = None,
mask_outside_limiter: np.ndarray | None = None,
) -> tuple[np.ndarray, np.ndarray, np.ndarray | None, float]:
"""
First part of the Jtor calculation that identifies critical points in the
poloidal flux (generic to all profile functions). The second stage (in
specific classes below) will be used for calculating Jtor using their
explicit profile parameterisations.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_bndry : float or None, optional
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface).
mask_outside_limiter : np.ndarray or None, optional
Mask of points outside the limiter, if any.
Returns
-------
opt : np.ndarray
Each row represents an O-point of the form [R, Z, ψ(R,Z)] [m, m, Webers/2pi].
xpt : np.ndarray
Each row represents an X-point of the form [R, Z, ψ(R,Z)] [m, m, Webers/2pi].
diverted_core_mask : np.ndarray of bool or None
An array, the same shape as the computational grid, indicating the locations
at which the core plasma resides (True) and where it does not (False). None
if there are no X-points and no `psi_bndry` was given.
psi_bndry : float
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface).
Raises
------
ValueError
If the ordering of the axis and boundary fluxes is inconsistent with the
sign of the plasma current (unsuitable ``psi_plasma`` guess).
"""
# find O- and X-points of equilibrium
opt, xpt = critical.find_critical(R, Z, psi, self.mask_inside_limiter, self.Ip)
# flux on the magnetic axis (needed by fpol, pressure, and q, also without X-points)
self.psi_axis = opt[0][2]
# find core plasma mask (using user-defined psi_bndry)
if psi_bndry is not None:
diverted_core_mask = critical.inside_mask(
R, Z, psi, opt, xpt, mask_outside_limiter, psi_bndry
)
elif len(xpt) > 0:
# find core plasma mask using psi_bndry from xpt
psi_bndry = xpt[0][2]
# check correct sorting between psi_axis and psi_bndry
if (self.psi_axis - psi_bndry) * self.Ip < 0:
raise ValueError(
"Incorrect critical points! Likely due to unsuitable 'psi_plasma' guess."
)
diverted_core_mask = critical.inside_mask(
R, Z, psi, opt, xpt, mask_outside_limiter, psi_bndry
)
# check of any abrupt change of size in the diverted core mask
if hasattr(self, "diverted_core_mask"):
if self.diverted_core_mask is not None:
previous_core_size = np.sum(self.diverted_core_mask)
# check size change
check = np.sum(diverted_core_mask) / previous_core_size < 0.5
candidate_index = 1
while check and candidate_index < len(xpt):
# try using the next xpt as primary xpt
candidate_xpt = xpt[candidate_index:]
alt_diverted_core_mask = critical.inside_mask(
R,
Z,
psi,
opt,
candidate_xpt,
mask_outside_limiter,
candidate_xpt[0, 2],
)
# check the alternative Xpoint gives rise to a valid core
edge_pixels = np.sum(self.edge_mask * alt_diverted_core_mask)
if edge_pixels == 0:
# the candidate is valid
xpt = candidate_xpt
psi_bndry = xpt[0, 2]
diverted_core_mask = alt_diverted_core_mask.copy()
# check if there could be better candidates
check = (
np.sum(diverted_core_mask) / previous_core_size < 0.5
)
candidate_index = 1
else:
candidate_index += 1
else:
# No X-points
psi_bndry = psi[0, 0]
diverted_core_mask = None
return opt, xpt, diverted_core_mask, psi_bndry
[docs]
class ConstrainBetapIp(Profile):
"""
Class describing the toroidal current density profile formulation defined in:
Y. M. Jeon, 2015, "Development of a free-boundary tokamak equilibrium solver
for advanced study of tokamak equilibria".
(https://link.springer.com/article/10.3938/jkps.67.843)
This formulation constrains profiles using the poloidal beta and plasma
current.
"""
[docs]
def __init__(
self,
betap: float,
Ip: float,
fvac: float,
alpha_m: float = 1.0,
alpha_n: float = 2.0,
Raxis: float = 1.0,
) -> None:
"""
Initialise the class.
Parameters
----------
betap : float
Poloidal beta.
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
alpha_m : float, optional
Shape/peakedness parameter (non-negative).
alpha_n : float, optional
Shape/peakedness parameter (non-negative).
Raxis : float, optional
Radial scaling parameter (non-negative).
Raises
------
ValueError
If `alpha_m`, `alpha_n` or `Raxis` is negative.
"""
# set parameters
self.betap = betap
self.Ip = Ip
self._fvac = fvac
if alpha_m < 0 or alpha_n < 0:
raise ValueError("alpha_m and alpha_n must be positive.")
self.alpha_m = alpha_m
self.alpha_n = alpha_n
if Raxis < 0:
raise ValueError("Raxis must be positive.")
self.Raxis = Raxis
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
) -> np.ndarray:
"""
Second part of the calculation that will use the explicit
parameterisation of the chosen profile function to calculate Jtor.
This is given by:
Jtor(ψ, R, Z) = L * [ (beta0 * R / Raxis) + ((1 - Beta0) * Raxis / R) ] * (1 - ψ^alpha_m)^alpha_n.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2].
Raises
------
ValueError
If the core mask is empty.
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
# shape function
jtorshape = (1.0 - np.clip(psi_norm, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
# if there is a masking function, use it
if mask is not None:
jtorshape *= mask
# now apply constraints to define constants
shapeintegral0 = spbeta(1.0 / self.alpha_m, 1.0 + self.alpha_n) / self.alpha_m
# pressure function
pfunc = (
shapeintegral0
* (psi_bndry - psi_axis)
* (
1.0
- spbinc(
1.0 / self.alpha_m,
1.0 + self.alpha_n,
np.clip(psi_norm, 0.0, 1.0) ** self.alpha_m,
)
)
)
# mask with core plasma
if mask is not None:
pfunc *= mask
# integrate over plasma
intp = np.sum(pfunc) * dR * dZ # romb(romb(pfunc)) * dR * dZ
# find Lbeta0 scaling
LBeta0 = -self.betap * (mu0 / (8.0 * pi)) * self.Raxis * self.Ip**2 / intp
# integrate current density components
IR = (
np.sum(jtorshape * R / self.Raxis) * dR * dZ
) # romb(romb(jtorshape * R / self.Raxis)) * dR * dZ
if IR == 0:
raise ValueError("No core mask!")
I_R = (
np.sum(jtorshape * self.Raxis / R) * dR * dZ
) # romb(romb(jtorshape * self.Raxis / R)) * dR * dZ
# find L scaling parameter and scaled beta
L = self.Ip / I_R - LBeta0 * (IR / I_R - 1)
Beta0 = LBeta0 / L
# calculate final toroidal current density
Jtor = L * (Beta0 * R / self.Raxis + (1 - Beta0) * self.Raxis / R) * jtorshape
# store parameters
self.jtor = Jtor
self.L = L
self.Beta0 = Beta0
return Jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The pprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * self.Beta0 / self.Raxis * shape
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The ffprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return mu0 * self.L * (1 - self.Beta0) * self.Raxis * shape
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac
[docs]
class ConstrainPaxisIp(Profile):
"""
Class describing the toroidal current density profile formulation defined in:
Y. M. Jeon, 2015, "Development of a free-boundary tokamak equilibrium solver
for advanced study of tokamak equilibria".
(https://link.springer.com/article/10.3938/jkps.67.843)
This formulation constrains profiles using the pressure on axis and plasma
current.
"""
[docs]
def __init__(
self,
paxis: float,
Ip: float,
fvac: float,
alpha_m: float = 1.0,
alpha_n: float = 2.0,
Raxis: float = 1.0,
) -> None:
"""
Initialise the class.
Parameters
----------
paxis : float
Pressure on the magnetic axis [Pa].
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
alpha_m : float, optional
Shape/peakedness parameter (non-negative).
alpha_n : float, optional
Shape/peakedness parameter (non-negative).
Raxis : float, optional
Radial scaling parameter (non-negative).
Raises
------
ValueError
If `alpha_m`, `alpha_n` or `Raxis` is negative.
"""
# set parameters
self.paxis = paxis
self.Ip = Ip
self._fvac = fvac
if alpha_m < 0 or alpha_n < 0:
raise ValueError("alpha_m and alpha_n must be positive.")
self.alpha_m = alpha_m
self.alpha_n = alpha_n
if Raxis < 0:
raise ValueError("Raxis must be positive.")
self.Raxis = Raxis
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
) -> np.ndarray:
"""
Second part of the calculation that will use the explicit
parameterisation of the chosen profile function to calculate Jtor.
This is given by:
Jtor(ψ, R, Z) = L * [ (beta0 * R / Raxis) + ((1 - Beta0) * Raxis / R) ] * (1 - ψ^alpha_m)^alpha_n.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2].
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
# shape function
jtorshape = (1.0 - np.clip(psi_norm, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
# if there is a masking function, use it
if mask is not None:
jtorshape *= mask
# now apply constraints to define constants
shapeintegral = spbeta(1.0 / self.alpha_m, 1.0 + self.alpha_n) / self.alpha_m
shapeintegral *= psi_bndry - psi_axis
# integrate current density components
IR = (
np.sum(jtorshape * R / self.Raxis) * dR * dZ
) # romb(romb(jtorshape * R / self.Raxis)) * dR * dZ
I_R = (
np.sum(jtorshape * self.Raxis / R) * dR * dZ
) # romb(romb(jtorshape * self.Raxis / R)) * dR * dZ
# find L scaling parameter and scaled beta
LBeta0 = -self.paxis * self.Raxis / shapeintegral
L = self.Ip / I_R - LBeta0 * (IR / I_R - 1)
Beta0 = LBeta0 / L
# calculate final toroidal current density
Jtor = L * (Beta0 * R / self.Raxis + (1 - Beta0) * self.Raxis / R) * jtorshape
# store parameters
self.jtor = Jtor
self.L = L
self.Beta0 = Beta0
return Jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The pprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
# self.L = 1
# print(
# "This is using self.L=1, which is likely not appropriate. Please calculate Jtor first to ensure the correct normalization."
# )
return self.L * self.Beta0 / self.Raxis * shape
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The ffprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return mu0 * self.L * (1 - self.Beta0) * self.Raxis * shape
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac
[docs]
class Fiesta_Topeol(Profile):
"""
Class describing the toroidal current density profile formulation defined in:
Y. M. Jeon, 2015, "Development of a free-boundary tokamak equilibrium solver
for advanced study of tokamak equilibria".
(https://link.springer.com/article/10.3938/jkps.67.843)
In this class, Beta0 is specified directly.
"""
[docs]
def __init__(
self,
Beta0: float,
Ip: float,
fvac: float,
alpha_m: float = 1.0,
alpha_n: float = 2.0,
Raxis: float = 1.0,
) -> None:
"""
Initialise the class.
Parameters
----------
Beta0 : float
Scaling parameter.
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
alpha_m : float, optional
Shape/peakedness parameter (non-negative).
alpha_n : float, optional
Shape/peakedness parameter (non-negative).
Raxis : float, optional
Radial scaling parameter (non-negative).
Raises
------
ValueError
If `alpha_m`, `alpha_n` or `Raxis` is negative.
"""
# set parameters
self.Beta0 = Beta0
self.Ip = Ip
self._fvac = fvac
if alpha_m < 0 or alpha_n < 0:
raise ValueError("alpha_m and alpha_n must be positive.")
self.alpha_m = alpha_m
self.alpha_n = alpha_n
if Raxis < 0:
raise ValueError("Raxis must be positive.")
self.Raxis = Raxis
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
) -> np.ndarray:
"""
Second part of the calculation that will use the explicit
parameterisation of the chosen profile function to calculate Jtor.
This is given by:
Jtor(ψ, R, Z) = L * [ (beta0 * R / Raxis) + ((1 - Beta0) * Raxis / R) ] * (1 - ψ^alpha_m)^alpha_n.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2].
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
# shape function
jtorshape = (1.0 - np.clip(psi_norm, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
# if there is a masking function, use it
if mask is not None:
jtorshape *= mask
# calculate final toroidal current density
Jtor = (
self.Beta0 * R / self.Raxis + (1 - self.Beta0) * self.Raxis / R
) * jtorshape
L = self.Ip / (np.sum(Jtor) * dR * dZ)
# store parameters
self.jtor = L * Jtor
self.L = L
return self.jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The pprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * self.Beta0 / self.Raxis * shape
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
Raises
------
ValueError
If Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
shape = (1.0 - np.clip(pn, 0.0, 1.0) ** self.alpha_m) ** self.alpha_n
if hasattr(self, "Beta0") is False:
raise ValueError(
"The ffprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return mu0 * self.L * (1 - self.Beta0) * self.Raxis * shape
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac
[docs]
class Lao85(Profile):
"""
Class describing the toroidal current density profile formulation defined in:
Lao et al, 1985, "Reconstruction of current profile parameters and plasma
shapes in tokamaks".
(https://dx.doi.org/10.1088/0029-5515/25/11/007)
See equations 2, 4, and 5.
"""
[docs]
def __init__(
self,
Ip: float,
fvac: float,
alpha: list[float] | np.ndarray,
beta: list[float] | np.ndarray,
alpha_logic: bool = True,
beta_logic: bool = True,
Raxis: float = 1,
Ip_logic: bool = True,
) -> None:
"""
Initialise the class.
Parameters
----------
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
alpha : list or np.ndarray
Polynomial coefficients for pprime.
beta : list or np.ndarray
Polynomial coefficients for ffprime.
alpha_logic : bool, optional
If True, include the n_{P+1} term to ensure pprime(1)=0 (see Jtor_part2).
beta_logic : bool, optional
If True, include the n_{F+1} term to ensure ffprime(1)=0 (see Jtor_part2).
Raxis : float, optional
Radial scaling parameter (non-negative).
Ip_logic : bool, optional
If True, entire profile is re-normalised to satisfy Ip identically.
"""
# set parameters for later use
self.Ip = Ip
self._fvac = fvac
self.alpha = np.array(alpha)
self.alpha_logic = alpha_logic
self.beta = np.array(beta)
self.beta_logic = beta_logic
self.Raxis = Raxis
self.Ip_logic = Ip_logic
if self.Ip_logic is False:
self.L = 1 # no normalisation in this case
# initialise profile ready for calculation
self.initialize_profile()
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def initialize_profile(self) -> None:
"""
Set up the attributes in the class ready for Jtor calculations.
Appends the extra polynomial terms (if `alpha_logic`/`beta_logic`) and
sets the exponent arrays ``alpha_exp`` and ``beta_exp``.
"""
# set up the required exponents in the Lao profile
if self.alpha_logic:
self.alpha = np.concatenate((self.alpha, [-np.sum(self.alpha)]))
self.alpha_exp = np.arange(0, len(self.alpha))
if self.beta_logic:
self.beta = np.concatenate((self.beta, [-np.sum(self.beta)]))
self.beta_exp = np.arange(0, len(self.beta))
[docs]
def build_dJtorpsin1(
self,
) -> None:
"""
Calculate dJtor/dpsi_n at psi_n=1 and store it in ``self.dJtorpsin1``.
Notes
-----
Requires the attribute ``self.eqR`` (the R grid), which is not set by this
class itself.
"""
# calculate dJ/dpsi_n at psi_n=1
pprime_term = (
self.alpha[1:, np.newaxis, np.newaxis]
* self.alpha_exp[1:, np.newaxis, np.newaxis]
)
pprime_term = np.sum(pprime_term, axis=0)
pprime_term *= self.eqR / self.Raxis
ffprime_term = (
self.beta[1:, np.newaxis, np.newaxis]
* self.beta_exp[1:, np.newaxis, np.newaxis]
)
ffprime_term = np.sum(ffprime_term, axis=0)
ffprime_term *= self.Raxis / self.eqR
ffprime_term /= mu0
self.dJtorpsin1 = pprime_term + ffprime_term
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
torefine: bool = False,
refineR: np.ndarray | None = None,
) -> np.ndarray:
"""
Second part of the calculation that will use the explicit
parameterisation of the chosen profile function to calculate Jtor.
This is given by:
Jtor(ψ, R, Z) = L * [ (R / Raxis) * p'(ψ) + (Raxis / R) * FF'(ψ) / mu0 ],
where
p'(ψ) = sum(alpha_i ψ^i) - sum(alpha_i) ψ^(n_P+1),
FF'(ψ) = sum(beta_i ψ^i) - sum(beta_i) ψ^(n_F+1),
and note ψ here is the normalised psi.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
torefine : bool, optional
If True, evaluate Jtor at refined sub-grid points only: `psi` and
`refineR` hold the values at those points, and the unmasked,
unnormalised current density is returned.
refineR : np.ndarray or None, optional
R coordinates of the refined points (same shape as `psi`); used in
place of `R` when `torefine` is True.
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2] (or at the
refined points if `torefine` is True).
Raises
------
ValueError
If the total plasma current of the (masked) profile is zero, so it cannot be renormalised.
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
if torefine:
R = 1.0 * refineR
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
psi_norm = np.clip(psi_norm, 0.0, 1.0)
# calculate the p' and FF' profiles
pprime_term = (
psi_norm[np.newaxis, :, :] ** self.alpha_exp[:, np.newaxis, np.newaxis]
)
pprime_term *= self.alpha[:, np.newaxis, np.newaxis]
pprime_term = np.sum(pprime_term, axis=0)
pprime_term *= R / self.Raxis
ffprime_term = (
psi_norm[np.newaxis, :, :] ** self.beta_exp[:, np.newaxis, np.newaxis]
)
ffprime_term *= self.beta[:, np.newaxis, np.newaxis]
ffprime_term = np.sum(ffprime_term, axis=0)
ffprime_term *= self.Raxis / R
ffprime_term /= mu0
# sum together
Jtor = pprime_term + ffprime_term
# calculate dJ/dpsi_n
pprime_term = (
psi_norm[np.newaxis, :, :] ** self.alpha_exp[:-1, np.newaxis, np.newaxis]
)
pprime_term *= (
self.alpha[1:, np.newaxis, np.newaxis]
* self.alpha_exp[1:, np.newaxis, np.newaxis]
)
pprime_term = np.sum(pprime_term, axis=0)
pprime_term *= R / self.Raxis
ffprime_term = (
psi_norm[np.newaxis, :, :] ** self.beta_exp[:-1, np.newaxis, np.newaxis]
)
ffprime_term *= (
self.beta[1:, np.newaxis, np.newaxis]
* self.beta_exp[1:, np.newaxis, np.newaxis]
)
ffprime_term = np.sum(ffprime_term, axis=0)
ffprime_term *= self.Raxis / R
ffprime_term /= mu0
dJtordpsin = pprime_term + ffprime_term
self.dJtordpsi = dJtordpsin / (psi_axis - psi_bndry)
# put to zero all current outside the LCFS
# Jtor *= psi > psi_bndry
# Jtor *= self.Ip * Jtor > 0
if torefine:
return Jtor
if mask is not None:
Jtor *= mask
self.dJtordpsi *= mask
# if Ip normalisation is required, do it
if self.Ip_logic:
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
jtorIp = np.sum(Jtor)
if jtorIp == 0:
self.problem_psi = psi
raise ValueError("Total plasma current is zero! Cannot renormalise.")
L = self.Ip / (jtorIp * dR * dZ)
Jtor = L * Jtor
self.dJtordpsi *= L
else:
L = 1.0
# store parameters
self.jtor = Jtor.copy()
self.L = L
return self.jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
Raises
------
ValueError
If `Ip_logic` is True and Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
pn_ = np.clip(np.array(pn), 0, 1)
shape_pn = np.shape(pn_)
shape = pn_[np.newaxis] ** self.alpha_exp.reshape(
list(np.shape(self.alpha_exp)) + [1] * len(shape_pn)
)
shape *= self.alpha.reshape(list(np.shape(self.alpha)) + [1] * len(shape_pn))
shape = np.sum(shape, axis=0)
if self.Ip_logic is True:
if hasattr(self, "L") is False:
raise ValueError(
"The pprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * shape / self.Raxis
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
Raises
------
ValueError
If `Ip_logic` is True and Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
pn_ = np.clip(np.array(pn), 0, 1)
shape_pn = np.shape(pn_)
shape = pn_[np.newaxis] ** self.beta_exp.reshape(
list(np.shape(self.beta_exp)) + [1] * len(shape_pn)
)
shape *= self.beta.reshape(list(np.shape(self.beta)) + [1] * len(shape_pn))
shape = np.sum(shape, axis=0)
if self.Ip_logic is True:
if hasattr(self, "L") is False:
raise ValueError(
"The ffprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * shape * self.Raxis
[docs]
def pressure(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pressure profile in the plasma (vs. the normalised
poloidal flux) without integrating the pprime profile (specifically
for the Lao profile).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pressure values at each normalised psi value.
"""
pn_ = np.clip(np.array(pn), 0, 1)[np.newaxis]
shape_pn = np.shape(pn_)
ones = np.ones_like(pn)
integrated_coeffs = self.alpha / np.arange(1, len(self.alpha_exp) + 1)
norm_pressure = np.sum(
integrated_coeffs.reshape(
list(np.shape(integrated_coeffs)) + [1] * len(shape_pn)
)
* (
ones
- pn
** (
self.alpha_exp.reshape(
list(np.shape(self.alpha_exp)) + [1] * len(shape_pn)
)
+ 1
)
),
axis=0,
)
if hasattr(self, "L") is False:
self.L = 1
logger.warning(
"This is using self.L=1, which is likely not appropriate. Please calculate Jtor first to ensure the correct normalization."
)
pressure = self.L * norm_pressure * (self.psi_axis - self.psi_bndry)
return pressure
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac
[docs]
class TensionSpline(Profile):
"""
Class describing the toroidal current density profile formulation that uses
tension spline.
"""
[docs]
def __init__(
self,
Ip: float,
fvac: float,
pp_knots: list[float] | np.ndarray,
pp_values: list[float] | np.ndarray,
pp_values_2: list[float] | np.ndarray,
pp_sigma: float,
ffp_knots: list[float] | np.ndarray,
ffp_values: list[float] | np.ndarray,
ffp_values_2: list[float] | np.ndarray,
ffp_sigma: float,
Raxis: float = 1,
Ip_logic: bool = True,
) -> None:
"""
Initialise the class.
Parameters
----------
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
pp_knots : list or np.ndarray
Knot points of pprime profile.
pp_values : list or np.ndarray
Values of pprime at knot points.
pp_values_2 : list or np.ndarray
Values of 2nd derivative of pprime at knot points.
pp_sigma : float
Tension spline parameter for pprime (non-negative).
ffp_knots : list or np.ndarray
Knot points of ffprime profile.
ffp_values : list or np.ndarray
Values of ffprime at knot points.
ffp_values_2 : list or np.ndarray
Values of 2nd derivative of ffprime at knot points.
ffp_sigma : float
Tension spline parameter for ffprime (non-negative).
Raxis : float, optional
Radial scaling parameter (non-negative).
Ip_logic : bool, optional
If True, entire profile is re-normalised to satisfy Ip identically.
"""
# set parameters for later use
self.Ip = Ip
self._fvac = fvac
self.pp_knots = np.array(pp_knots)
self.pp_values = np.array(pp_values)
self.pp_values_2 = np.array(pp_values_2)
self.pp_sigma = pp_sigma
self.ffp_knots = np.array(ffp_knots)
self.ffp_values = np.array(ffp_values)
self.ffp_values_2 = np.array(ffp_values_2)
self.ffp_sigma = ffp_sigma
self.Raxis = Raxis
self.Ip_logic = Ip_logic
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
) -> np.ndarray:
"""
Second part of the calculation that will use the explicit
parameterisation of the chosen profile function to calculate Jtor.
Typically used for more modelling more complex shaped profiles
(from magnetics + MSE plasma reconstructions).
This is given by:
Jtor(ψ, R, Z) = L * [ (R / Raxis) * p'(ψ) + (Raxis / R) * FF'(ψ) / mu0 ],
where
p'(ψ) = sum(f_n),
FF'(ψ) = sum(f_n),
where f_n is the tension spline.
See https://catxmai.github.io/pdfs/Math212_ProjectReport.pdf for definition.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2].
Raises
------
ValueError
If the total plasma current of the (masked) profile is zero, so it cannot be renormalised.
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
psi_norm = np.clip(psi_norm, 0.0, 1.0)
# calculate pprime and ffprime terms
pprime_term = self.tension_spline(
psi_norm,
self.pp_knots,
self.pp_values,
self.pp_values_2,
self.pp_sigma,
)
pprime_term *= R / self.Raxis
ffprime_term = self.tension_spline(
psi_norm,
self.ffp_knots,
self.ffp_values,
self.ffp_values_2,
self.ffp_sigma,
)
ffprime_term *= self.Raxis / R
ffprime_term /= mu0
# sum together
Jtor = pprime_term + ffprime_term
# if there is a masking function, use it
if mask is not None:
Jtor *= mask
# normalise with respect to plasma current if needed
if self.Ip_logic:
jtorIp = np.sum(Jtor)
if jtorIp == 0:
self.problem_psi = psi
raise ValueError("Total plasma current is zero! Cannot renormalise.")
L = self.Ip / (jtorIp * dR * dZ)
Jtor = L * Jtor
else:
L = 1.0
# store results
self.jtor = Jtor.copy()
self.L = L
return self.jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
"""
shape = self.tension_spline(
pn, self.pp_knots, self.pp_values, self.pp_values_2, self.pp_sigma
)
return self.L * shape / self.Raxis
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
"""
shape = self.tension_spline(
pn,
self.ffp_knots,
self.ffp_values,
self.ffp_values_2,
self.ffp_sigma,
)
return self.L * shape * self.Raxis
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac
[docs]
def tension_spline(
self,
x: float | np.ndarray,
xn: np.ndarray,
yn: np.ndarray,
zn: np.ndarray,
sigma: float,
) -> float | np.ndarray:
"""
Evaluate the tension spline at locations in x using knot points xn,
values at knot points yn, and second derivative values at knot points zn.
Tension parameter is a non-negative float sigma.
Parameters
----------
x : float or np.array
Normalised psi value(s) between 0 and 1 (a scalar or an array of any shape).
xn : np.array
Array of knot points between 0 and 1.
yn : np.ndarray
Values of the function at the knot points.
zn : np.ndarray
Values of the 2nd derivative of the function at the knot points.
sigma : float
Tension spline parameter (non-negative).
Returns
-------
float or np.array
The tension spline evaluated at the points in x (a float if x is a
scalar, otherwise an array with the same shape as x).
"""
# accept scalars as well as arrays of any shape
x = np.asarray(x, dtype=float)
size = x.shape
x = x.reshape(-1)
# fixed parameters
x_diffs = xn[1:] - xn[0:-1]
sinh_diffs = np.sinh(sigma * x_diffs)
# initial solution array (each column is f_n(x) for a different n)
X = np.tile(x, (len(x_diffs), 1)).T
# calculate the terms in the spline (vectorised for speed)
t1 = (yn[0:-1] - zn[0:-1] / (sigma**2)) * ((xn[1:] - X) / x_diffs)
t2 = (
zn[0:-1] * np.sinh(sigma * (xn[1:] - X))
+ zn[1:] * np.sinh(sigma * (X - xn[0:-1]))
) / ((sigma**2) * sinh_diffs)
t3 = (yn[1:] - zn[1:] / (sigma**2)) * ((X - xn[0:-1]) / x_diffs)
# sum the values
sol = t1 + t2 + t3
# zero out values outisde range of each f_n(x) as they're not valid (recall definition of tension spline)
for n in range(0, len(xn) - 1):
ind = (xn[n] <= x) & (x <= xn[n + 1])
sol[~ind, n] = 0
# sum to find (alomst) final solution
f = np.sum(sol, axis=1)
# points on an interior knot lie in two adjacent intervals so are double counted
f[np.isin(x, xn[1:-1])] /= 2
# restore the shape of the input (a plain float for a scalar input)
if len(size) == 0:
return float(f[0])
return f.reshape(size)
[docs]
class GeneralPprimeFFprime(Profile):
"""
Class describing the general toroidal current density profile formulation:
Jtor(ψ, R, Z) = R * p'(ψ) + FF'(ψ) / (R * mu0).
"""
[docs]
def __init__(
self,
Ip: float,
fvac: float,
psi_n: np.ndarray,
pprime_data: np.ndarray | None = None,
ffprime_data: np.ndarray | None = None,
p_data: np.ndarray | None = None,
f_data: np.ndarray | None = None,
Raxis: float = 1,
Ip_logic: bool = True,
interpolator: str = "univariate_spline",
) -> None:
"""
Initialise the class.
Parameters
----------
Ip : float
Total plasma current [A].
fvac : float
Vacuum toroidal field strength (f = R*B_tor) [T].
psi_n : np.ndarray
A 1D array of normalised poloidal flux values, corresponding to the data in pprime_data, ffprime_data,
p_data, and f_data.
pprime_data : np.ndarray or None, optional
A 1D array of dp/dpsi_n (p is pressure) values at each value of normalised flux in psi_n.
ffprime_data : np.ndarray or None, optional
A 1D array of fdf/dpsi_n (f is the toroidal magnetic field) values at each value of normalised flux in psi_n.
p_data : np.ndarray or None, optional
A 1D array of p (p is pressure) values at each value of normalised flux in psi_n.
f_data : np.ndarray or None, optional
A 1D array of f (f is the toroidal magnetic field) values at each value of normalised flux in psi_n.
Raxis : float, optional
Radial scaling parameter (non-negative).
Ip_logic : bool, optional
If True, entire profile is re-normalised to satisfy Ip identically.
interpolator : str, optional
Defaults to using a "univariate_spline" or optionally a "cubic_spline", data points in both are interpolated exactly.
"""
# set parameters for later use
self.Ip = Ip
self._fvac = fvac
self.psi_n = psi_n
self.pprime_data = pprime_data
self.ffprime_data = ffprime_data
self.p_data = p_data
self.f_data = f_data
self.Raxis = Raxis
self.Ip_logic = Ip_logic
if self.Ip_logic is False:
self.L = 1 # no normalisation in this case
self.interpolator = interpolator
# initialise profile ready for calculation
self.initialize_profile()
# Compatibility flag for native current-density profiles.
self.fast = True
[docs]
def initialize_profile(self) -> None:
"""
Set up the attributes in the class ready for Jtor calculations.
Builds the interpolating functions ``pprime_func``, ``p_func``,
``ffprime_func`` and ``f_func`` from the supplied data.
Raises
------
ValueError
If neither `pprime_data` nor `p_data` is provided, or neither
`ffprime_data` nor `f_data` is provided.
"""
# interpolate the p data
self.pprime_func = None
self.p_func = None
if self.pprime_data is not None:
if self.interpolator == "cubic_spline":
self.pprime_func = CubicSpline(
self.psi_n, self.pprime_data, bc_type="natural"
)
else:
self.pprime_func = UnivariateSpline(
self.psi_n,
self.pprime_data,
s=0,
k=1,
)
if self.p_data is not None:
if self.interpolator == "cubic_spline":
self.p_func = CubicSpline(self.psi_n, self.p_data, bc_type="natural")
else:
self.p_func = UnivariateSpline(self.psi_n, self.p_data, s=0, k=1)
# if pprime_func still not provided, use p_func derivative, else throw error
if self.pprime_func is None and self.p_func is not None:
self.pprime_func = self.p_func.derivative(n=1)
elif self.pprime_func is None and self.p_func is None:
raise ValueError("Need to provide either 'pprime_data' or 'p_data'")
# interpolate the f data
self.ffprime_func = None
self.f_func = None
if self.ffprime_data is not None:
if self.interpolator == "cubic_spline":
self.ffprime_func = CubicSpline(
self.psi_n, self.ffprime_data, bc_type="natural"
)
else:
self.ffprime_func = UnivariateSpline(
self.psi_n, self.ffprime_data, s=0, k=1
)
if self.f_data is not None:
if self.interpolator == "cubic_spline":
self.f_func = CubicSpline(self.psi_n, self.f_data, bc_type="natural")
else:
self.f_func = UnivariateSpline(self.psi_n, self.f_data, s=0, k=1)
# if ffprime_func still not provided, use f_func derivative, else throw error
if self.ffprime_func is None and self.f_func is not None:
fprime_func = self.f_func.derivative(n=1)
self.ffprime_func = lambda x: self.f_func(x) * fprime_func(x)
elif self.ffprime_func is None and self.f_func is None:
raise ValueError("Need to provide either 'ffprime_data' or 'f_data'")
[docs]
def Jtor_part2(
self,
R: np.ndarray,
Z: np.ndarray,
psi: np.ndarray,
psi_axis: float,
psi_bndry: float | None,
mask: np.ndarray | None,
torefine: bool = False,
refineR: np.ndarray | None = None,
) -> np.ndarray:
"""
Second part of the calculation that will use the interpolated profile
functions to calculate Jtor.
This is given by:
Jtor(ψ, R, Z) = L * [ (R / Raxis) * p'(ψ) + (Raxis / R) * FF'(ψ) / mu0 ],
where
p'(ψ) = pprime_func(ψ)
FF'(ψ) = ffprime_func(ψ)
and note ψ here is the normalised psi.
Parameters
----------
R : np.ndarray
Radial coordinates of the grid points.
Z : np.ndarray
Vertical coordinates of the grid points.
psi : np.ndarray
Total poloidal field flux at each grid point [Webers/2pi].
psi_axis : float
Value of the poloidal field flux at the magnetic axis of the plasma.
psi_bndry : float or None
Value of the poloidal field flux at the boundary of the plasma (last closed
flux surface). If None, ``psi[0, 0]`` is used.
mask : np.ndarray or None
Mask of points inside the last closed flux surface (no masking if None).
torefine : bool, optional
If True, evaluate Jtor at refined sub-grid points only: `psi` and
`refineR` hold the values at those points, and the unmasked,
unnormalised current density is returned.
refineR : np.ndarray or None, optional
R coordinates of the refined points (same shape as `psi`); used in
place of `R` when `torefine` is True.
Returns
-------
np.ndarray
Toroidal current density on the computational grid [A/m^2] (or at the
refined points if `torefine` is True).
Raises
------
ValueError
If the total plasma current of the (masked) profile is zero, so it cannot be renormalised.
"""
# set flux on boundary
if psi_bndry is None:
psi_bndry = psi[0, 0]
self.psi_bndry = psi_bndry
# grid sizes
dR = R[1, 0] - R[0, 0]
dZ = Z[0, 1] - Z[0, 0]
if torefine:
R = 1.0 * refineR
# calculate normalised psi
psi_norm = (psi - psi_axis) / (psi_bndry - psi_axis)
psi_norm = np.clip(psi_norm, 0.0, 1.0)
# calculate the p' and FF' profiles
pprime_term = self.pprime_func(psi_norm)
pprime_term *= R / self.Raxis
ffprime_term = self.ffprime_func(psi_norm)
ffprime_term *= self.Raxis / R
ffprime_term /= mu0
# sum together
Jtor = pprime_term + ffprime_term
if torefine:
return Jtor
# put to zero all current outside the LCFS
if mask is not None:
Jtor *= mask
# if Ip normalisation is required, do it
if self.Ip_logic:
jtorIp = np.sum(Jtor)
if jtorIp == 0:
self.problem_psi = psi
raise ValueError("Total plasma current is zero! Cannot renormalise.")
L = self.Ip / (jtorIp * dR * dZ)
Jtor = L * Jtor
else:
L = 1.0
# store parameters
self.jtor = Jtor.copy()
self.L = L
return self.jtor
[docs]
def pprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pprime (dp/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pprime values at each normalised psi value.
Raises
------
ValueError
If `Ip_logic` is True and Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
pn_ = np.clip(np.array(pn), 0, 1)
if self.Ip_logic:
if hasattr(self, "L") is False:
raise ValueError(
"The pprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * self.pprime_func(pn_) / self.Raxis
[docs]
def ffprime(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D ffprime (FdF/dpsi) profile in the plasma (vs. the
normalised poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed ffprime values at each normalised psi value.
Raises
------
ValueError
If `Ip_logic` is True and Jtor has not yet been calculated for this profile (so it cannot be normalised).
"""
pn_ = np.clip(np.array(pn), 0, 1)
if self.Ip_logic:
if hasattr(self, "L") is False:
raise ValueError(
"The ffprime profile cannot be normalised. "
"Please first calculate Jtor for this profile. "
)
return self.L * self.ffprime_func(pn_) * self.Raxis
[docs]
def pressure(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D pressure profile in the plasma (vs. the normalised
poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed pressure values at each normalised psi value.
"""
pn_ = np.clip(np.array(pn), 0, 1)
if self.p_func is not None:
return self.p_func(pn_)
else:
return super(GeneralPprimeFFprime, self).pressure(pn_)
[docs]
def fpol(self, pn: np.ndarray) -> np.ndarray:
"""
Calculate the 1D toroidal magnetic profile in the plasma (vs. the normalised
poloidal flux).
Parameters
----------
pn : np.ndarray
Array of normalised psi values between 0 and 1.
Returns
-------
np.ndarray
The computed toroidal magnetic field values at each normalised psi value.
"""
pn_ = np.clip(np.array(pn), 0, 1)
if self.f_func is not None:
return self.f_func(pn_)
else:
return super(GeneralPprimeFFprime, self).fpol(pn_)
[docs]
def fvac(self) -> float:
"""
Return the vacuum field parameter fvac = R*Btor.
Returns
-------
float
The vacuum field parameter fvac = R*Btor [T m].
"""
return self._fvac