freegsnke.critical_old module
Routines to find critical points (O- and X-points)
Copyright 2016 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk
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.critical_old.core_mask(R: ndarray, Z: ndarray, psi: ndarray, opoint: ndarray, xpoint: ndarray | list = [], psi_bndry: float | None = None) ndarray[source]
Mark the parts of the domain which are in the core.
- Parameters:
R (np.ndarray) – Major radius values, shape (nx, ny).
Z (np.ndarray) – Height values, shape (nx, ny).
psi (np.ndarray) – Poloidal flux, shape (nx, ny).
opoint (np.ndarray) – O-points as returned by find_critical.
xpoint (np.ndarray or list, optional) – X-points as returned by find_critical.
psi_bndry (float or None, optional) – If not None, this is used to find the separatrix, not the X-points.
- Returns:
Array of shape (nx, ny) which is 1 inside the core, 0 outside.
- Return type:
np.ndarray
- freegsnke.critical_old.discard_xpoints_f(opoint: ndarray, xpt: ndarray, f: Callable[[...], ndarray]) bool[source]
Check that psi is monotonic along the line from the O-point to an X-point.
- Parameters:
opoint (np.ndarray) – The primary O-point as (R, Z, psi).
xpt (np.ndarray) – An individual X-point as (R, Z, psi).
f (callable) – Spline interpolator of psi, called as
f(R, Z, grid=False).
- Returns:
True if the X-point is acceptable (monotonic), False if it should be discarded.
- Return type:
bool
- freegsnke.critical_old.fastcrit(R: ndarray, Z: ndarray, psi: ndarray, mask_inside_limiter: ndarray | None) tuple[ndarray, ndarray][source]
Find and order the critical points of psi using the fast grid scan.
- Parameters:
R (np.ndarray) – Major radii, shape (nr, nz).
Z (np.ndarray) – Heights, shape (nr, nz).
psi (np.ndarray) – Poloidal flux values, shape (nr, nz).
mask_inside_limiter (np.ndarray of bool or None) – Mask of grid points inside the limiter. If not None, O-points outside it are removed.
- Returns:
opoint (np.ndarray) – O-points as rows of (R, Z, psi), ordered by distance from the domain centre.
xpoint (np.ndarray) – X-points as rows of (R, Z, psi), ordered by closeness of their flux to the axis flux.
- Raises:
ValueError – If no O-points are found.
- freegsnke.critical_old.find_critical(R: ndarray, Z: ndarray, psi: ndarray, mask_inside_limiter: ndarray | None = None, signIp: float | None = 1, discard_xpoints: bool = True) tuple[ndarray, ndarray][source]
Find the O- and X-points of psi and select the X-points consistent with the plasma current.
- Parameters:
R (np.ndarray) – Major radii, shape (nr, nz).
Z (np.ndarray) – Heights, shape (nr, nz).
psi (np.ndarray) – Poloidal flux values, shape (nr, nz).
mask_inside_limiter (np.ndarray of bool or None, optional) – Mask of grid points inside the limiter, used to discard O-points outside it.
signIp (float or None, optional) – Sign of the plasma current. X-points are kept only if their flux is on the correct side of the axis flux. If None, no such selection is made.
discard_xpoints (bool, optional) – Unused (kept for backwards compatibility).
- Returns:
opoint (np.ndarray) – O-points as rows of (R, Z, psi), primary O-point first.
xpoint (np.ndarray) – X-points as rows of (R, Z, psi), primary X-point first.
- freegsnke.critical_old.find_critical_old(R: ndarray, Z: ndarray, psi: ndarray, discard_xpoints: bool = True) tuple[list[tuple[float, float, float]], list[tuple[float, float, float]]][source]
Find critical points (legacy FreeGS implementation using spline Newton iterations).
- Parameters:
R (np.ndarray) – Major radii, shape (nr, nz).
Z (np.ndarray) – Heights, shape (nr, nz).
psi (np.ndarray) – Poloidal flux values, shape (nr, nz).
discard_xpoints (bool, optional) – If True, discard X-points for which psi is not monotonic along the line from the primary O-point.
- Returns:
opoint (list of tuple) – O-points as (R, Z, psi) tuples. The first is the primary O-point (magnetic axis).
xpoint (list of tuple) – X-points as (R, Z, psi) tuples. The first is the primary X-point (separatrix), ordered by closeness to the axis flux.
- freegsnke.critical_old.find_psisurface(eq: object, psifunc: Callable[[...], ndarray], r0: float, z0: float, r1: float, z1: float, psival: float = 1.0, n: int = 100, axis: object | None = None) tuple[float, float][source]
Find where a line from inside the separatrix crosses a given psi surface.
- Parameters:
eq (Equilibrium) – Equilibrium object (provides the domain limits Rmin, Rmax, Zmin, Zmax).
psifunc (callable) – Function
psifunc(r, z, grid=False)returning normalised psi.r0 (float) – R of the start location, inside the separatrix.
z0 (float) – Z of the start location, inside the separatrix.
r1 (float) – R of the end location, outside the separatrix (clipped to the domain).
z1 (float) – Z of the end location, outside the separatrix (clipped to the domain).
psival (float, optional) – Value of psi for which to find the surface.
n (int, optional) – Number of starting points to use along the line.
axis (matplotlib axis or None, optional) – If given, the line and the result are plotted on it.
- Returns:
r, z – Coordinates of the crossing point. If psival has a length, the points along the line are returned unchanged.
- Return type:
float
- freegsnke.critical_old.find_safety(eq: object, npsi: int = 1, psinorm: float | ndarray | list | None = None, ntheta: int = 128, psi: ndarray | None = None, opoint: ndarray | list | None = None, xpoint: ndarray | list | None = None, axis: object | None = None) ndarray[source]
Find the safety factor for each value of psi.
Calculates equally spaced flux surfaces. Points on each flux surface are equally spaced in poloidal angle. Performs line integral around flux surface to get q. If opoint, xpoint or psi are not given, they are calculated from eq.
- Parameters:
eq (Equilibrium) – The equilibrium object.
npsi (int, optional) – Number of flux surface values to find q for (overridden by the length of psinorm, or by 100 if psinorm is None).
psinorm (float or array_like or None, optional) – Normalised flux surface value(s) to calculate q for. If None, 100 values are used.
ntheta (int, optional) – Number of poloidal points to find it on.
psi (np.ndarray or None, optional) – Grid of psi on (R, Z).
opoint (np.ndarray or list or None, optional) – O-points as (R, Z, psi) tuples/rows.
xpoint (np.ndarray or list or None, optional) – X-points as (R, Z, psi) tuples/rows.
axis (matplotlib axis or None, optional) – Axis to plot points on.
- Returns:
Safety factor at each of the npsi normalised psi values.
- Return type:
np.ndarray
- Raises:
ValueError – If there is no X-point.
- freegsnke.critical_old.find_separatrix(eq: object, opoint: ndarray | list | None = None, xpoint: ndarray | list | None = None, ntheta: int = 20, psi: ndarray | None = None, axis: object | None = None, psival: float = 1.0) list[tuple[float, float, float, float]][source]
Find the R, Z coordinates of the separatrix for equilibrium eq.
Points are equally spaced in geometric poloidal angle. If opoint, xpoint or psi are not given, they are calculated from eq.
- Parameters:
eq (Equilibrium) – Equilibrium object.
opoint (np.ndarray or list or None, optional) – O-points as (R, Z, psi) tuples/rows.
xpoint (np.ndarray or list or None, optional) – X-points as (R, Z, psi) tuples/rows.
ntheta (int, optional) – Number of points to find.
psi (np.ndarray or None, optional) – Grid of psi on (R, Z).
axis (matplotlib axis or None, optional) – Axis to plot points on.
psival (float, optional) – Normalised psi value of the surface to find.
- Returns:
One tuple (R, Z, R_X, Z_X) per poloidal angle, where R_X, Z_X are the coordinates of the X-point on the separatrix.
- Return type:
list of tuple
- freegsnke.critical_old.inside_mask(R: np.ndarray, Z: np.ndarray, psi: np.ndarray, opoint: np.ndarray, xpoint: np.ndarray | list = [], mask_outside_limiter: np.ndarray | None = None, psi_bndry: float | None = None) np.ndarray[source]
Mark the parts of the domain which are in the core (JIT-compilable version of core_mask).
Similar to core_mask above, except: (1) it’s all stuff that can be JIT-compiled (2) some stuff is vectorised (3) the stack does not necessarily explore in only one direction, and it should be neat around the X-points
- Parameters:
R (np.ndarray) – Major radius values, shape (nx, ny).
Z (np.ndarray) – Height values, shape (nx, ny).
psi (np.ndarray) – Poloidal flux, shape (nx, ny).
opoint (np.ndarray) – O-points as rows of (R, Z, psi); the first is the primary O-point.
xpoint (np.ndarray or list, optional) – X-points as rows of (R, Z, psi).
mask_outside_limiter (np.ndarray or None, optional) – Array of shape (nx, ny), non-zero outside the limiter, used to block the fill.
psi_bndry (float or None, optional) – If not None, this is used as the boundary flux instead of that of the first X-point.
- Returns:
Mask of shape (nx, ny), True inside the core.
- Return type:
np.ndarray of bool
- freegsnke.critical_old.remove_dup(points: list[tuple[float, float, float]]) list[tuple[float, float, float]][source]
Remove duplicate critical points (those closer than ~3e-3 in R-Z distance).
- Parameters:
points (list of tuple) – Critical points as (R, Z, psi) tuples.
- Returns:
The points with duplicates removed (first occurrence kept).
- Return type:
list of tuple
- freegsnke.critical_old.scan_for_crit(R: np.ndarray, Z: np.ndarray, psi: np.ndarray) tuple[np.ndarray, np.ndarray][source]
Scan the grid for local minima of |grad psi|^2 and classify them as O- or X-points.
- Parameters:
R (np.ndarray) – Major radii, shape (nr, nz).
Z (np.ndarray) – Heights, shape (nr, nz).
psi (np.ndarray) – Poloidal flux values, shape (nr, nz).
- Returns:
opoint (np.ndarray) – O-points as rows of (R, Z, psi), shape (n_o, 3).
xpoint (np.ndarray) – X-points as rows of (R, Z, psi), shape (n_x, 3).