freegsnke.control module
Legacy FreeGS plasma-control constraints.
These classes belong to the inherited standalone Picard inverse workflow. They are not used by FreeGSNKE, which supplies its own inverse-solver constraints. The module is currently retained for compatibility while the legacy API is reviewed.
Copyright 2016-2021 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk Copyright 2018 Bhavin Patel Copyright 2026 Nicola C. Amorisco
Derived from FreeGS through FreeGS4E.
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.control.ConstrainPsi2D(target_psi: ndarray, weights: float | ndarray | None = None)[source]
Bases:
objectAdjusts coil currents to minimise the square differences between psi[R,Z] and a target psi.
Ignores constant offset differences between psi array
- __init__(target_psi: ndarray, weights: float | ndarray | None = None) None[source]
Store the target psi and the weights.
- Parameters:
target_psi (np.ndarray) – 2D (R, Z) array; must be the same size as the equilibrium psi.
weights (float or np.ndarray or None, optional) – Relative importance of each (R, Z) point in the fitting, a float or a 2D array of the same size as target_psi. By default every point is equally weighted. Set points to zero to ignore them in fitting.
- psi_difference(currents: ndarray, eq: object) ndarray[source]
Compute the weighted difference between psi and the target psi.
Sets the control currents of the equilibrium to currents and removes the constant offset from its psi before comparing.
- Parameters:
currents (np.ndarray) – Control currents to apply to the equilibrium.
eq (object) – Equilibrium object, modified in place.
- Returns:
Flattened weighted difference between psi and the target.
- Return type:
np.ndarray
- class freegsnke.control.ConstrainPsiNorm2D(target_psinorm: ndarray, weights: float | ndarray = 1.0)[source]
Bases:
objectAdjusts coil currents to minimise the square differences between normalised psi[R,Z] and a target normalised psi.
- __init__(target_psinorm: ndarray, weights: float | ndarray = 1.0) None[source]
Store the target normalised psi and the weights.
- Parameters:
target_psinorm (np.ndarray) – 2D (R, Z) array; must be the same size as the equilibrium psi.
weights (float or np.ndarray, optional) – Relative importance of each (R, Z) point in the fitting, a float or a 2D array of the same size as target_psinorm. By default every point is equally weighted. Set points to zero to ignore them.
- psinorm_difference(currents: ndarray, eq: object) ndarray[source]
Compute the weighted difference between normalised psi and the target.
Sets the control currents of the equilibrium to currents. Normalised psi is 0 at the magnetic axis and 1 at the plasma boundary (first X-point).
- Parameters:
currents (np.ndarray) – Control currents to apply to the equilibrium.
eq (object) – Equilibrium object, modified in place.
- Returns:
Flattened weighted difference between psi_norm and the target.
- Return type:
np.ndarray
- Raises:
ValueError – If no O-points or no X-points are found.
- class freegsnke.control.constrain(xpoints: list[tuple[float, float]] = [], gamma: float = 1e-12, isoflux: list[tuple[float, float, float, float]] = [], psivals: list[tuple[float, float, float]] = [])[source]
Bases:
objectAdjust coil currents using the inherited FreeGS constraint implementation.
This is not a supported FreeGSNKE inverse-solver interface. To use this legacy class, first create an instance by specifying the constraints:
>>> controlsystem = constrain(xpoints = [(1.0, 1.1), (1.0,-1.0)])
controlsystem will now attempt to create x-points at (R,Z) = (1.0, 1.1) and (1.0, -1.1) in any Equilibrium
>>> controlsystem(eq)
where eq is an Equilibrium object which is modified by the call.
The constraints which can be set are:
xpoints - A list of X-point (R,Z) locations
isoflux - A list of tuples (R1,Z1, R2,Z2)
psivals - A list of (R,Z,psi) values
At least one constraint must be included. The coil currents are found by Tikhonov-regularised least squares, with gamma penalising the magnitude of the current changes.
- __init__(xpoints: list[tuple[float, float]] = [], gamma: float = 1e-12, isoflux: list[tuple[float, float, float, float]] = [], psivals: list[tuple[float, float, float]] = []) None[source]
Create an instance, specifying the constraints to apply.
- Parameters:
xpoints (list[tuple[float, float]], optional) – X-point
(R, Z)locations.gamma (float, optional) – Tikhonov regularisation scalar, which minimises the magnitude of the coil currents.
isoflux (list[tuple[float, float, float, float]], optional) – Tuples
(R1, Z1, R2, Z2)of points constrained to have the same flux.psivals (list[tuple[float, float, float]], optional) –
(R, Z, psi)values to impose.
- plot(axis: Axes | None = None, show: bool = True) Axes[source]
Plot the constraints used for coil current control.
- Parameters:
axis (matplotlib.axes.Axes or None, optional) – Axis on which to plot; a new one is created if None.
show (bool, optional) – Call
matplotlib.pyplot.show()before returning.
- Returns:
The axis containing the plotted constraints.
- Return type:
matplotlib.axes.Axes