freegsnke.equilibrium_update module
Defines the FreeGSNKE equilibrium object, extending the native base equilibrium.
Copyright 2025 UKAEA, UKRI-STFC, and The Authors, as per the COPYRIGHT and README files.
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.equilibrium_update.Equilibrium(*args: object, **kwargs: object)[source]
Bases:
EquilibriumFreeGSNKE equilibrium object, extending the native equilibrium.
Adds limiter handling, a self-checking plasma-flux interpolator, in-place machine-description updates, lightweight auxiliary copies, and optional initialisation of the plasma flux from a file given by the
EQUILIBRIUM_PATHenvironment variable.Notes
Code that replaces
plasma_psishould useset_plasma_psi()so the interpolator used bypsi_func()(and hencepsiRZ,Br,Bzand related diagnostics) stays consistent with the flux array.- __init__(*args: object, **kwargs: object) None[source]
Initialise the equilibrium.
- Parameters:
*args – Passed to
freegsnke.equilibrium.Equilibrium.__init__, e.g.tokamak,Rmin,Rmax,Zmin,Zmax,nx,ny,boundary,psi,current.**kwargs – Passed to
freegsnke.equilibrium.Equilibrium.__init__, e.g.tokamak,Rmin,Rmax,Zmin,Zmax,nx,ny,boundary,psi,current.
Notes
If the
EQUILIBRIUM_PATHenvironment variable is set, the initial plasma flux is loaded from that file viainitialize_from_equilibrium(). The equilibrium is marked as not solved (self.solved = False) and the limiter handler and inside/outside-limiter masks are built fromtokamak.limiter.
- add_active_coil(coil_name: str, active_coil_data: dict, *, strict_overlap_check: bool = False) Equilibrium[source]
Add one new active coil/circuit and extend equilibrium-level coil caches.
- Parameters:
coil_name (str) – Label for the new active coil/circuit.
active_coil_data (dict) – Machine-description entry for the new active coil/circuit.
strict_overlap_check (bool, optional) – If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises.
- Returns:
This equilibrium object, updated in place.
- Return type:
Notes
Only the Greens functions for the new coil are calculated; existing coil Greens functions and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed.
- add_passive_structure(passive_data: dict, name: str | None = None, refine_mode: str = 'G', *, strict_overlap_check: bool = False) Equilibrium[source]
Add one new passive structure and extend equilibrium-level coil caches.
- Parameters:
passive_data (dict) – Machine-description entry for the new passive structure.
name (str, optional) – Label for the new passive structure. If omitted,
passive_data["name"]is used, and if that is also absent a default of the formf"passive_{self.tokamak.n_passive_coils}"is used.refine_mode (str, optional) – Refinement mode for extended (polygonal) passive structures. Defaults to
"G".strict_overlap_check (bool, optional) – If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises.
- Returns:
This equilibrium object, updated in place.
- Return type:
Notes
Only the Greens functions for the new passive structure are calculated; existing coil Greens functions and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed.
- adjust_psi_plasma() None[source]
Operates an initial rescaling of the psi_plasma guess so to ensure a viable O-point and at least an X-point within the domain.
Only use after appropriate coil currents have been set as desired!
The guess is first scaled up (by factors of 1.5) until an O-point is found inside the limiter, then made more compact by exponentiation until an X-point appears, and finally scaled up (by factors of 1.15) to enlarge the diverted core.
Notes
Modifies
tokamak_psi,plasma_psi,psi_func_interp,gmodandgexpin place.Failures are reported by printing, not by raising. If no O-point can be generated, or exponentiation removes the O-point, the method returns early and leaves
plasma_psiat its last scaled value without refreshing the interpolator. If exponentiation completes without producing an X-point, the last exponentiated trial is still installed.
- create_auxiliary_equilibrium() Equilibrium[source]
Creates the auxiliary equilibrium object.
The auxiliary object returned from this method is essentially a copy of the equilibrium object (self) however it is manually setup and so won’t contain all attributes on self (especially custom attributes). It is NOT _guaranteed_ to be the same as a deepcopy, or even a shallow copy.
- Returns:
New equilibrium object, created without calling
__init__. Grids, fluxes, masks, Greens functions and critical-point data are copied; the tokamak is copied withcopy_tokamak; the limiter handler and the plasma-flux interpolator are shared withself.- Return type:
- initialize_from_equilibrium() None[source]
This function loads a pickle file containing an initial guess for the plasma flux (and the corners of the grid points it is located on).
Interpolation is carried out and mapped to the computational grid specified in the eq class.
The file is read from
self.equilibrium_path(set from theEQUILIBRIUM_PATHenvironment variable) and must contain a dict with keys"Rmin","Rmax","Zmin","Zmax"and"psi_plasma"(a 2D array on a uniform grid spanning those limits). Setsplasma_psiandpsi_func_interpin place.- Raises:
ValueError – If the pickle data is missing any of the required keys.
- plot(axis: Axes | None = None, xpoints: bool = True, opoints: bool = True, wall: bool = True, limiter: bool = True, legend: bool = False, show: bool = True) Axes[source]
Plot a solved FreeGSNKE equilibrium.
This overrides the base plotting wrapper so limited equilibria without a relevant X-point in the solution domain can still be plotted. In that case the LCFS is drawn from
psi_bndryand no primary X-point separatrix is requested.- Parameters:
axis (matplotlib.axes.Axes, optional) – Axes to draw on. If None, a new figure and axes are created.
xpoints (bool, optional) – If True, plot the X-points (only when a relevant X-point exists). Defaults to True.
opoints (bool, optional) – If True, plot the O-points. Defaults to True.
wall (bool, optional) – If True, plot the wall (when the tokamak has one). Defaults to True.
limiter (bool, optional) – If True, plot the limiter (when the tokamak has one). Defaults to True.
legend (bool, optional) – If True, add a legend. Defaults to False.
show (bool, optional) – If True, call
plt.show()after plotting. Defaults to True.
- Returns:
The axes the equilibrium was drawn on.
- Return type:
matplotlib.axes.Axes
- Raises:
RuntimeError – If the equilibrium has not been solved (no
_profileswith critical-point data).
- psi_func(R: ndarray, Z: ndarray, *args: object, **kwargs: object) ndarray[source]
Scipy interpolation of plasma_psi function. Replaces the original FreeGS interpolation. It now includes a check which leads to the update of the interpolation when needed.
- Parameters:
R (ndarray) – R coordinates where the interpolation is needed
Z (ndarray) – Z coordinates where the interpolation is needed
*args – Passed to
scipy.interpolate.RectBivariateSpline.__call__(e.g.dx,dy,grid).**kwargs – Passed to
scipy.interpolate.RectBivariateSpline.__call__(e.g.dx,dy,grid).
- Returns:
Interpolated values of plasma_psi
- Return type:
ndarray
Notes
The staleness check compares the spline and
plasma_psionly at the grid centre, so it can miss changes made elsewhere. Useset_plasma_psi()to replace the flux.
- refresh_machine_dependent_state(refresh_limiter: bool = True) Equilibrium[source]
Refresh cached data derived from the current tokamak description.
Rebuilds the coil Greens functions on this equilibrium grid for changed coil labels only when possible, updates the tokamak flux from the refreshed Greens functions and current vector, and optionally recreates the limiter handler and inside/outside-limiter masks. A full Greens rebuild is used when the coil ordering or number of coils changed, or when no previous cached Greens functions are available.
- Parameters:
refresh_limiter (bool, optional) – If True, rebuild the limiter handler and limiter masks. Set to False for coil-only updates that leave limiter geometry unchanged.
- Returns:
This equilibrium object, refreshed in place.
- Return type:
- remove_active_coil(coil_name: str) Equilibrium[source]
Remove one active coil/circuit and shrink equilibrium-level coil caches.
- Parameters:
coil_name (str) – Existing active coil/circuit label to remove.
- Returns:
This equilibrium object, updated in place.
- Return type:
- Raises:
ValueError – If the tokamak does not contain an active coil with that label.
Notes
Greens functions for the remaining coils and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed.
- remove_passive_structure(name: str) Equilibrium[source]
Remove one passive structure and shrink equilibrium-level coil caches.
- Parameters:
name (str) – Existing passive-structure label to remove.
- Returns:
This equilibrium object, updated in place.
- Return type:
- Raises:
ValueError – If the tokamak does not contain a passive structure with that label.
Notes
Greens functions for the remaining coils and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed.
- set_plasma_psi(plasma_psi: ndarray) None[source]
Install plasma flux and synchronise its interpolation cache.
Unlike
_updatePlasmaPsi, this lightweight update does not recalculate critical points, masks, or boundary flux. Solvers should use it when committing a flux map before updating topology through their existing profile-aware path.- Parameters:
plasma_psi (array_like) – Plasma poloidal flux on the (R, Z) grid, shape
(nx, ny)[Webers/2pi]. A copy is stored, so later changes to the caller’s array do not affect the equilibrium.
- update_active_coil(coil_name: str, active_coil_data: dict, preserve_current: bool = True, *, strict_overlap_check: bool = False) Equilibrium[source]
Update one active coil/circuit and refresh equilibrium-level coil caches.
- Parameters:
coil_name (str) – Existing active coil/circuit label to replace.
active_coil_data (dict) – Machine-description entry for
coil_name.preserve_current (bool, optional) – If True, the old current on
coil_nameis copied onto the replacement coil/circuit. Defaults to True.strict_overlap_check (bool, optional) – If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises.
- Returns:
This equilibrium object, updated in place.
- Return type:
Notes
This method refreshes only the Greens functions affected by the updated coil when the coil ordering is unchanged. Limiter masks are reused because the limiter geometry is not modified by an active-coil update. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data.
- update_machine_description(active_coils_data: dict | None = None, passive_coils_data: list | None = None, limiter_data: list | None = None, wall_data: list | None = None, magnetic_probe_data: dict | None = None, active_coils_path: str | None = None, passive_coils_path: str | None = None, limiter_path: str | None = None, wall_path: str | None = None, magnetic_probe_path: str | None = None, refine_mode: str = 'G', preserve_currents: bool = True, *, strict_overlap_check: bool = False) Equilibrium[source]
Update the equilibrium’s tokamak directly and refresh machine-dependent caches.
The computational domain, plasma current, and plasma flux are left in place. Coil Greens functions, tokamak flux, limiter masks, and probe descriptions are rebuilt for the updated machine.
- Parameters:
active_coils_data (dict, optional) – Dictionary containing the active coil description.
passive_coils_data (list, optional) – List containing passive structure descriptions. If omitted, no passive structures are added.
limiter_data (list, optional) – List of limiter boundary points.
wall_data (list, optional) – List of wall boundary points. If omitted, the limiter is used as the wall.
magnetic_probe_data (dict, optional) – Dictionary containing magnetic probe descriptions.
active_coils_path (str, optional) – Path to a pickle file containing the active coil description.
passive_coils_path (str, optional) – Path to a pickle file containing passive structure descriptions.
limiter_path (str, optional) – Path to a pickle file containing limiter boundary points.
wall_path (str, optional) – Path to a pickle file containing wall boundary points.
magnetic_probe_path (str, optional) – Path to a pickle file containing magnetic probe descriptions.
refine_mode (str, optional) – Refinement mode for extended passive structures. Defaults to
"G".preserve_currents (bool, optional) – If True, currents for labels that are present in both the old and new machine descriptions are copied onto the updated coil objects.
strict_overlap_check (bool, optional) – If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises.
- Returns:
This equilibrium object, updated in place.
- Return type:
Notes
This method refreshes equilibrium-level data derived from the tokamak, but it does not update existing solver objects. Reinstantiate static or nonlinear solvers after changing machine geometry.
- update_passive_structure(name: str, passive_data: dict, preserve_current: bool = True, refine_mode: str = 'G', *, strict_overlap_check: bool = False) Equilibrium[source]
Update one passive structure and refresh equilibrium-level coil caches.
- Parameters:
name (str) – Existing passive-structure label to replace.
passive_data (dict) – Machine-description entry for
name.preserve_current (bool, optional) – If True, the old current on
nameis copied onto the replacement passive structure. Defaults to True.refine_mode (str, optional) – Refinement mode for extended (polygonal) passive structures. Defaults to
"G".strict_overlap_check (bool, optional) – If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises.
- Returns:
This equilibrium object, updated in place.
- Return type:
Notes
This method refreshes only the Greens functions affected by the updated passive structure. Limiter masks are reused because the limiter geometry is not modified by a passive-structure update. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data.