freegsnke.plotting module

Plot aspects of the tokamak and equilibrium.

Copyright 2024 Nicola C. Amorisco, Adriano Agnello, George K. Holt, Ben Dudson.

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/>.

freegsnke.plotting.make_broad_mask(mask: ndarray, layer_size: int = 1) → ndarray[source]

Expand a binary mask by adding a border layer of specified thickness.

This function broadens a 2D boolean or binary mask by marking all points that lie within a specified distance (layer_size pixels along each axis, i.e. a square neighbourhood) from the original masked region. The result is a new mask that includes both the original region and its expanded boundary.

Parameters:
  • mask (np.ndarray) – 2D boolean or binary array representing the region to be expanded.

  • layer_size (int, optional) – Number of pixels by which to expand the mask in all directions. Default is 1.

Returns:

Boolean array of the same shape as mask, where True values correspond to the original region plus an outer layer of width layer_size.

Return type:

np.ndarray

Notes

  • The expansion is performed by summing shifted copies of the mask over a square window of size (2 * layer_size + 1), effectively performing a morphological dilation.

  • This operation is useful for defining buffer regions (e.g., just outside a limiter or wall in equilibrium simulations).

Examples

>>> mask = np.array([[0, 0, 0],
...                  [0, 1, 0],
...                  [0, 0, 0]], dtype=bool)
>>> make_broad_mask(mask, layer_size=1)
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]])
freegsnke.plotting.plotCoils(coils: object, axis: Axes | None = None) → Axes[source]

Plot the geometry of magnetic field coils.

This function prepares a plot of the coil set used in a tokamak or other magnetic confinement configuration. It creates a Matplotlib axis if one is not provided, allowing additional geometric or field features to be overlaid later.

Parameters:
  • coils (object) – Coil geometry object containing spatial information about the magnetic coils (e.g., current centerlines or cross-sections). The exact structure of coils depends on the specific equilibrium or control framework.

  • axis (matplotlib.axes.Axes, optional) – Axis object on which to plot. If None, a new figure and axis are created.

Returns:

axis – The matplotlib axis prepared for coil plotting.

Return type:

matplotlib.axes.Axes

Notes

  • This function currently initializes the plot but does not draw coil shapes. It can be extended to plot coil positions or outlines using data from coils.

freegsnke.plotting.plotConstraints(control: object, axis: Axes | None = None, show: bool = True) → Axes[source]

Plot magnetic control constraints such as X-points and isoflux surfaces.

This function visualizes the spatial constraints used for coil current optimization or magnetic equilibrium control, including X-point locations and isoflux surface definitions. These constraints are typically used in inverse equilibrium solvers or feedback control algorithms.

Parameters:
  • control (object) –

    Control data object containing constraint information with attributes: - xpoints : list of (R, Z) tuples

    Target locations for X-point constraints.

    • isofluxlist of (R1, Z1, R2, Z2) tuples

      Pairs of points defining line segments that should lie on the same poloidal flux surface.

  • axis (matplotlib.axes.Axes, optional) – Axis object on which to plot. If None, a new figure and axis are created.

  • show (bool, default=True) – If True, call matplotlib.pyplot.show() before returning.

Returns:

axis – The matplotlib axis containing the plotted constraints.

Return type:

matplotlib.axes.Axes

Notes

  • X-point constraints are plotted as blue crosses (“bx”).

  • Isoflux constraints are plotted as dashed blue line segments with triangle markers (“:b^”).

  • A legend is displayed automatically if show=True.

Examples

axis = plotConstraints(control) axis.set_title(“Coil Control Constraints”)

freegsnke.plotting.plotEquilibrium(eq: object, 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 poloidal magnetic ψ(R, Z) flux surfaces and key geometric features of an equilibrium.

It can also overlay the separatrix, magnetic X- and O-points, the plasma-facing wall, and the limiter geometry.

Parameters:
  • eq (object) – An equilibrium object containing fields R, Z, and psi(), as well as profile data (_profiles.opt, _profiles.xpt) and tokamak geometry (tokamak.wall, tokamak.limiter).

  • axis (matplotlib.axes.Axes, optional) – Axis object to plot on. If None, a new figure and axis are created.

  • xpoints (bool, default=True) – If True, plot magnetic X-points (red ‘x’ markers).

  • opoints (bool, default=True) – If True, plot magnetic O-points (green ‘*’ markers).

  • wall (bool, default=True) – If True, plot the tokamak wall outline as a solid black curve.

  • limiter (bool, default=True) – If True, plot the limiter outline as a dashed black curve.

  • legend (bool, default=False) – If True, display a legend describing plotted features.

  • show (bool, default=True) – If True, call matplotlib.pyplot.show() before returning.

Returns:

axis – The matplotlib axis containing the plotted equilibrium.

Return type:

matplotlib.axes.Axes

Raises:

RuntimeError – If the equilibrium has not been solved (i.e. psi() or the profile O-/X-points are unavailable).

Notes

  • The function assumes that the equilibrium has already been solved.

  • The separatrix is plotted using a solid red line for diverted equilibria and a dashed red line for limited equilibria.

Examples

axis = plotEquilibrium(eq, legend=True) axis.set_title(“Tokamak Equilibrium Flux Surfaces”)

freegsnke.plotting.plotIOConstraints(control: object, axis: Axes | None = None, show: bool = True) → Axes[source]

Plot magnetic control constraints for equilibrium reconstruction.

This function visualizes the geometric constraints used in coil current control or equilibrium reconstruction, including magnetic null points and isoflux target locations. These constraints are typically used to ensure that a magnetic configuration satisfies specified plasma shape or field conditions.

Parameters:
  • control (object) –

    An object containing magnetic control data with attributes: - null_points : tuple of array-like or None

    Coordinates (R, Z) of magnetic null points, if defined.

    • null_points_2nd_ordertuple of array-like or None

      Coordinates (R, Z) of second-order null points, if defined.

    • isoflux_setlist of array-like or None

      A list of isoflux constraint points, where each element is a pair of arrays (R, Z) defining a set of target locations for equal poloidal flux.

  • axis (matplotlib.axes.Axes, optional) – Axis object on which to plot. If None, a new figure and axis are created.

  • show (bool, default=True) – If True, call matplotlib.pyplot.show() before returning.

Returns:

axis – The matplotlib axis containing the plotted constraints.

Return type:

matplotlib.axes.Axes

Notes

  • Null points are plotted as purple downward tick markers (“1”) and second-order null points as orange ones.

  • Isoflux constraints are plotted as plus markers (“+”) in distinct colors.

  • A legend is automatically displayed in the upper-right corner if show=True.

Examples

axis = plotIOConstraints(control) axis.set_title(“Magnetic Control Constraints: Null Points and Isoflux Sets”)

freegsnke.plotting.plotProbes(probes: object, axis: Axes | None = None, show: bool = True, floops: bool = True, pickups: bool = True, pickups_scale: float = 0.05) → Axes[source]

Plot magnetic diagnostics including flux loops and pickup coils.

This function visualises diagnostic probe locations used for equilibrium reconstruction, showing the spatial distribution of flux loops and pickup coils. Pickup coil orientations are indicated by small line segments scaled by a user-defined factor.

Parameters:
  • probes (object) –

    An object containing probe geometry data with the following attributes: - floop_pos : ndarray of shape (N, 2)

    Positions of flux loops in (R, Z) coordinates.

    • pickup_posndarray of shape (M, 3)

      Positions of pickup coils in (R, φ, Z) coordinates (only R and Z used).

    • pickup_orndarray of shape (M, 3)

      Orientation vectors of the pickup coils.

  • axis (matplotlib.axes.Axes, optional) – Axis object on which to plot. If None, a new figure and axis are created.

  • show (bool, default=True) – If True, call matplotlib.pyplot.show() before returning.

  • floops (bool, default=True) – If True, plot flux loop probe locations as orange diamonds.

  • pickups (bool, default=True) – If True, plot pickup coil locations as brown circles and their orientations as short brown line segments.

  • pickups_scale (float, default=0.05) – Scaling factor applied to pickup coil orientation vectors for visualization.

Returns:

axis – The matplotlib axis containing the plotted probes.

Return type:

matplotlib.axes.Axes

Notes

  • The function assumes probes.floop_pos, probes.pickup_pos, and probes.pickup_or are NumPy arrays with appropriate dimensions.

  • The orientation lines are plotted using (R, Z) components only.

Examples

axis = plotProbes(probes, pickups_scale=0.1) axis.set_title(“Magnetic Diagnostics: Flux Loops and Pickup Coils”)