freegsnke.gs_solver module
Defines a base class for all solvers for the GS equation, as well as some specific subclasses.
Copyright 2026 Tomas Rubio Cruz, Ubaid Qadri
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.gs_solver.GSDSTSolver(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 2, dtype: type | ~numpy.dtype = <class 'numpy.float64'>)[source]
Bases:
GSSolverDiscrete Sine Transform (DST) based solver for the Grad-Shafranov equation on a rectangular (R,Z) grid with equally spaced points. Solves:
(d^2/dR^2 + d^2/dZ^2 - (1/R)*d/dR) ψ = -μ_0 R J_{phi}(ψ),
with the Dirichlet (free-)boundary conditions applied.
Instances are called as solver(xi, rhs) (see GSSolver); the solution has the shape of rhs.
- __init__(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 2, dtype: type | ~numpy.dtype = <class 'numpy.float64'>) None[source]
Initialize the solver with the given setup. R and Z define the grid (domain size and resolution).
- Parameters:
R (np.ndarray) – Grid of shape (nr, nz) with the radius of each point.
Z (np.ndarray) – Grid of shape (nr, nz) with the height of each point.
order (int, optional) – Ignored, as the DST solver is always second order. A warning is issued if a different value is given.
dtype (type or np.dtype, optional) – Datatype to use, per numpy conventions.
- Raises:
ValueError – If R and Z have different shapes, or the grid has fewer than 3 points in R or Z.
- order = 2
- solve(xi: ndarray, rhs: ndarray) ndarray[source]
Solves the Grad-Shafranov equation with the given rhs.
- Parameters:
xi (np.ndarray) – Unused, present for cross-compatibility with other solvers.
rhs (np.ndarray) – Right-hand side of the GS equation (f), of shape (nr, nz).
- Returns:
Flattened psi obtained for the given rhs, of length nr * nz.
- Return type:
np.ndarray
- class freegsnke.gs_solver.GSLUSolver(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 4, dtype: type | ~numpy.dtype = <class 'numpy.float64'>)[source]
Bases:
GSSolverSparse-LU solver for the Grad-Shafranov equation on a rectangular (R,Z) grid with equally spaced points. Solves:
(d^2/dR^2 + d^2/dZ^2 - (1/R)*d/dR) ψ = -μ_0 R J_{phi}(ψ),
with the Dirichlet (free-)boundary conditions applied.
Instances are called as solver(xi, rhs) (see GSSolver); the solution has the shape of rhs.
- __init__(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 4, dtype: type | ~numpy.dtype = <class 'numpy.float64'>) None[source]
Initialize the solver with the given setup. R and Z define the grid (domain size and resolution).
- Parameters:
R (np.ndarray) – Grid of shape (nr, nz) with the radius of each point.
Z (np.ndarray) – Grid of shape (nr, nz) with the height of each point.
order (int, optional) – Order of the finite difference approximation to the GS operator (2 or 4).
dtype (type or np.dtype, optional) – Datatype to use, per numpy conventions.
- Raises:
ValueError – If R and Z have different shapes, or order is not 2 or 4.
- solve(xi: ndarray, rhs: ndarray) ndarray[source]
Solves the Grad-Shafranov equation with the given rhs.
- Parameters:
xi (np.ndarray) – Unused, present for cross-compatibility with other solvers.
rhs (np.ndarray) – Right-hand side of the GS equation (f), of shape (nr, nz).
- Returns:
Flattened psi obtained for the given rhs, of length nr * nz.
- Return type:
np.ndarray
- class freegsnke.gs_solver.GSSolver(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 2, dtype: type | ~numpy.dtype = <class 'numpy.float64'>)[source]
Bases:
ABCParent class for all GS solvers. Enforces the definition of a solve method and its use when calling the solver directly, as well as ensuring that the solution from call is in the right shape.
In addition to the abstract methods below, all instances should initialize self.dimensions, the dimensions of the grid where the discrete problem is solved.
- abstract __init__(R: ~numpy.ndarray, Z: ~numpy.ndarray, *, order: int = 2, dtype: type | ~numpy.dtype = <class 'numpy.float64'>) None[source]
Recommended initialization signature for subclasses.
- Parameters:
R (np.ndarray) – Grid of shape (nr, nz) with the radius of each point in the domain.
Z (np.ndarray) – Grid of shape (nr, nz) with the height of each point in the domain.
order (int, optional) – Order of the finite difference approximation to the GS operator.
dtype (type or np.dtype, optional) – Datatype to use, per numpy conventions.
- abstract solve(xi: ndarray, rhs: ndarray, **kwargs: object) ndarray[source]
Solve the GS equation for the given rhs and return psi.
The result may be flattened; __call__ restores the shape.
- Parameters:
xi (np.ndarray) – Initial guess (may be unused by the solver).
rhs (np.ndarray) – Right-hand side of the GS equation.
**kwargs (object) – Solver-specific options.
- Returns:
Solution psi.
- Return type:
np.ndarray