freegsnke.gradshafranov module
Contains various classes and functions related to the elliptic operator of the Grad-Shafranov equation.
Copyright 2026 Ben Dudson, Tomas Rubio Cruz
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/>.
- class freegsnke.gradshafranov.GSElliptic(Rmin: float, *, dtype: type = <class 'numpy.float64'>)[source]
Bases:
objectClass representing the elliptc operator within the Grad-Shafranov equation:
Δ^* = d^2/dR^2 + d^2/dZ^2 - (1/R)*d/dR
- where:
R is the radial coordinate.
Z is the vertical coordinate.
- class freegsnke.gradshafranov.GSsparse(Rmin: float, Rmax: float, Zmin: float, Zmax: float, dtype: type = <class 'numpy.float64'>)[source]
Bases:
objectClass representing the elliptic operator within the Grad-Shafranov equation:
Δ^* = d^2/dR^2 + d^2/dZ^2 - (1/R)*d/dR
- where:
R is the radial coordinate.
Z is the vertical coordinate.
This class calculates the sparse version to second-order accuracy.
- __init__(Rmin: float, Rmax: float, Zmin: float, Zmax: float, dtype: type = <class 'numpy.float64'>) None[source]
Initializes the class.
- Parameters:
Rmin (float) – Minimum major radius [m].
Rmax (float) – Maximum major radius [m].
Zmin (float) – Minimum height [m].
Zmax (float) – Maximum height [m].
dtype (numpy dtype, optional) – Data type of the matrix entries (default: np.float64).
- discretize(nx: int, ny: int, format: str = 'csr') csr_array | csc_array | csr_matrix | csc_matrix[source]
Generates the sparse elliptic operator Δ^* for a given number of grid points. Computes to second-order accuracy.
- Parameters:
nx (int) – Number of radial grid points (including boundaries).
ny (int) – Number of vertical grid points (including boundaries).
format (str, optional) – Sparse format of the returned matrix, either “csr” (default) or “csc”. Any other value falls back to “csr” with a warning.
- Returns:
The operator matrix, in the requested sparse format.
- Return type:
scipy.sparse matrix/array
- order = 2
- class freegsnke.gradshafranov.GSsparse4thOrder(Rmin: float, Rmax: float, Zmin: float, Zmax: float, dtype: type = <class 'numpy.float64'>)[source]
Bases:
GSsparseClass representing the elliptic operator within the Grad-Shafranov equation:
Δ^* = d^2/dR^2 + d^2/dZ^2 - (1/R)*d/dR
- where:
R is the radial coordinate.
Z is the vertical coordinate.
This class calculates the sparse version to fourth-order accuracy.
- centred_1st = [(-2, 0.08333333333333333), (-1, -0.6666666666666666), (1, 0.6666666666666666), (2, -0.08333333333333333)]
- centred_2nd = [(-2, -0.08333333333333333), (-1, 1.3333333333333333), (0, -2.5), (1, 1.3333333333333333), (2, -0.08333333333333333)]
- discretize(nx: int, ny: int, format: str = 'csr') csr_array | csc_array | csr_matrix | csc_matrix[source]
Generates the sparse elliptic operator Δ^* for a given number of grid points. Computes to fourth-order accuracy.
- Parameters:
nx (int) – Number of radial grid points (including boundaries).
ny (int) – Number of vertical grid points (including boundaries).
format (str, optional) – Sparse format of the returned matrix, either “csr” (default) or “csc”. Any other value falls back to “csr” with a warning.
- Returns:
The operator matrix, in the requested sparse format.
- Return type:
scipy.sparse matrix/array
- Raises:
KeyError – If an unrecognised derivative operator is encountered while assembling the stencil (internal error).
- offset_1st = [(-1, -0.25), (0, -0.8333333333333334), (1, 1.5), (2, -0.5), (3, 0.08333333333333333)]
- offset_2nd = [(-1, 0.8333333333333334), (0, -1.25), (1, -0.3333333333333333), (2, 1.1666666666666667), (3, -0.5), (4, 0.08333333333333333)]
- order = 4
- freegsnke.gradshafranov.Greens(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, limit_threading: bool = False, scale_factor: float = 1, out: ndarray | None = None) float | ndarray[source]
Calculate poloidal flux at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above. Greens function is given by:
G(R, Z; Rc, Zc) = (μ0 / (2π)) * sqrt(R * Rc) * ((2 - k^2) * K(k^2) - 2 * E(k^2)) / k
- where:
k^2 = 4 R Rc / ((R + Rc)^2 + (Z - Zc)^2)
k = sqrt(k^2)
and K(k^2) and E(k^2) are the complete elliptic integrals of the first and second kind.
This function is multithreaded using numexpr and freegsnke’s custom parallel library. For details consult the relevant section in the README.
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where poloidal flux is to be calculated [m].
Z (float or np.ndarray) – Vertical position where poloidal flux is to be calculated [m].
limit_threading (bool, optional) – If True, forces SOME internal functions, with high threading overhead, to run single threaded. Multiple threads will still be used for low overhead functionalities. Recommended when the input arrays are small (default: False).
scale_factor (float, optional) – Scalar factor to apply on the final result (default: 1).
out (np.ndarray, optional) – Pre-allocated buffer for the final result.
- Returns:
Value of the poloidal flux at (R,Z) [Webers/2pi].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensBr(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.0001) float | ndarray[source]
Calculate radial magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above.
Br(R,Z) = -(1/R) d psi/dZ,
where psi is found with the Greens function finite difference.
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the vertical direction [m].
- Returns:
Value of the radial magnetic field at (R,Z) [T].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensBz(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.0001) float | ndarray[source]
Calculate vertical magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above.
Bz(R,Z) = (1/R) d psi/dR,
where psi is found with the Greens function finite difference.
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the radial direction [m].
- Returns:
Value of the vertical magnetic field at (R,Z) [T].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensdBrdr(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.002) float | ndarray[source]
Calculate radial derivative of radial magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above:
dBr/dR (R,Z) = (Br(R + eps, Z) - Br(R - eps, Z))/ (2 * eps).
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the radial direction [m].
- Returns:
Value of the derivative at (R,Z) [T/m].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensdBrdz(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.002) float | ndarray[source]
Calculate vertical derivative of radial magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above:
dBr/dZ (R,Z) = (Br(R, Z + eps) - Br(R, Z - eps))/ (2 * eps).
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the vertical direction [m].
- Returns:
Value of the derivative at (R,Z) [T/m].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensdBzdr(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.002) float | ndarray[source]
Calculate radial derivative of vertical magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above:
dBz/dR (R,Z) = (Bz(R + eps, Z) - Bz(R - eps, Z))/ (2 * eps).
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the radial direction [m].
- Returns:
Value of the derivative at (R,Z) [T/m].
- Return type:
float or np.ndarray
- freegsnke.gradshafranov.GreensdBzdz(Rc: float | ndarray, Zc: float | ndarray, R: float | ndarray, Z: float | ndarray, eps: float = 0.002) float | ndarray[source]
Calculate vertical derivative of vertical magnetic field at (R,Z) due to a single unit of current at (Rc,Zc) using Greens function for the elliptic operator above:
dBz/dZ (R,Z) = (Bz(R, Z + eps) - Bz(R, Z - eps)) / (2 * eps).
- Parameters:
Rc (float or np.ndarray) – Radial position where current is located [m].
Zc (float or np.ndarray) – Vertical position where current is located [m].
R (float or np.ndarray) – Radial position where the field is evaluated [m].
Z (float or np.ndarray) – Vertical position where the field is evaluated [m].
eps (float, optional) – Small step size for numerical differentiation in the vertical direction [m].
- Returns:
Value of the derivative at (R,Z) [T/m].
- Return type:
float or np.ndarray