freegsnke.critical module
Routines to find critical points (O- and X-points).
Copyright 2024 Nicola C. Amorisco, Adriano Agnello, George K. Holt, Ben Dudson.
Modified substantially from the original FreeGS code.
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.core_mask(R: ndarray, Z: ndarray, psi: ndarray, opoint: ndarray | list, xpoint: ndarray | list = [], psi_bndry: float | None = None) ndarray[source]
Generates a masking array of computational grid points (R,Z) that reside within the last closed flux surface (plasma boundary).
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
opoint (np.ndarray or list) – The O-point tuples/rows (R,Z,psi); the first is the primary O-point.
xpoint (np.ndarray or list, optional) – The X-point tuples/rows (R,Z,psi).
psi_bndry (float or None, optional) – Total poloidal flux on the plasma boundary [Webers/2pi]. If None, the flux of the first X-point is used.
- Returns:
Float array of shape (nx, ny) where 1 denotes a point inside the core and 0 denotes a point outside.
- Return type:
np.ndarray
- freegsnke.critical.discard_xpoints_f(R: ndarray, Z: ndarray, psi: ndarray, opoint: ndarray, xpt: ndarray) bool[source]
Checks whether an X-point is reachable from the O-point along a line of monotonic flux, so that X-points not on (or close to) the separatrix can be discarded.
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
opoint (np.ndarray) – The (primary) O-point as (R, Z, psi).
xpt (np.ndarray) – The individual X-point to test, as (R, Z, psi).
- Returns:
Returns True if the X-point is to be kept, False if discarded.
- Return type:
bool
- freegsnke.critical.fastcrit(R: ndarray, Z: ndarray, psi: ndarray, mask_inside_limiter: ndarray | None) tuple[ndarray, ndarray][source]
Finds and orders the critical points in the total poloidal flux map ψ.
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
mask_inside_limiter (np.ndarray or None) – Masking array, describing which (R, Z) grid points are inside the limiter. If not None, O-points outside the limiter are discarded.
- Returns:
opoint (np.ndarray) – O-point locations and flux values (R, Z, psi) as rows, shape (n_o, 3), ordered by distance from the domain centre. The first row is the primary O-point (i.e. the magnetic axis).
xpoint (np.ndarray) – X-point locations and flux values (R, Z, psi) as rows, shape (n_x, 3), ordered by closeness of their flux to the axis flux. The first row is the primary X-point.
- Raises:
ValueError – If no (valid) O-points are found.
- freegsnke.critical.find_critical(R: ndarray, Z: ndarray, psi: ndarray, mask_inside_limiter: ndarray | None = None, signIp: int | None = 1, discard_xpoints: bool = True) tuple[ndarray, ndarray][source]
Finds the critical points in the total poloidal flux map ψ.
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
mask_inside_limiter (np.ndarray or None, optional) – Masking array, describing which (R, Z) grid points are inside the limiter.
signIp (int or None, optional) – Sign of the plasma current (+1 or -1). If None, X-points are not selected by their flux ordering relative to the O-point.
discard_xpoints (bool, optional) – Unused (X-points not on a monotonic line of sight from the O-point are always discarded).
- Returns:
opoint (np.ndarray) – O-point locations and flux values (R, Z, psi) as rows, shape (n_o, 3). The first row is the primary O-point (i.e. the magnetic axis).
xpoint (np.ndarray) – X-point locations and flux values (R, Z, psi) as rows, shape (n_x, 3). The first row is the primary X-point (i.e. closest in flux to the magnetic axis, usually on the plasma separatrix). May be empty.
- freegsnke.critical.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]
Finds the critical points in the total poloidal flux map ψ (legacy Newton-iteration version).
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
discard_xpoints (bool, optional) – Discard X-points not on (or close to) the separatrix.
- Returns:
opoint (list of tuple) – O-point locations and flux values (R, Z, psi). The first tuple is the primary O-point (i.e. the magnetic axis).
xpoint (list of tuple) – X-point locations and flux values (R, Z, psi). The first tuple is the primary X-point (i.e. closest in flux to the magnetic axis, usually on the plasma separatrix).
- freegsnke.critical.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]
Determines the (R,Z) location on a specified magnetic flux surface by interpolating along a straight line in the poloidal plane (defined by (r0,z0) and (r1,z1)).
- Parameters:
eq (object) – Equilibrium object (provides the domain limits Rmin, Rmax, Zmin, Zmax).
psifunc (callable) – Callable function for the (normalised) flux map, called as
psifunc(r, z, grid=False).r0 (float) – Starting R location inside the separatrix [m].
z0 (float) – Starting Z location inside the separatrix [m].
r1 (float) – Final R location outside the separatrix [m].
z1 (float) – Final Z location outside the separatrix [m].
psival (float, optional) – Value of the flux (in the units of psifunc) to find the surface of.
n (int, optional) – Number of starting points to use along the line.
axis (object or None, optional) – Matplotlib axis object, used for plotting.
- Returns:
r (float) – R coordinate of the point on the flux surface [m].
z (float) – Z coordinate of the point on the flux surface [m].
- freegsnke.critical.find_safety(eq: object, npsi: int = 1, psinorm: float | ndarray | list[float] | 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]
Determines the safety factor profile for a given equilbirium. Points on each flux surface are equally paced in poloidal angle. The function performs a line integral around each flux surface to get q (on that flux surface):
q = 1/(2 pi) * integral of F / (R^2 B_p) dl,
where the poloidal field is B_p = |grad(psi)| / R. The flux surfaces are located along rays from the magnetic axis, for all surfaces and angles at once.
- Parameters:
eq (object) – Equilibrium object.
npsi (int) – Number of flux surface values to find q for.
psinorm (float or np.array) – The flux surface(s) to calculate q for (a scalar or an array-like, which is flattened). If None, 100 surfaces are used.
ntheta (int) – Number of poloidal points to find q values on.
psi (np.ndarray or None, optional) – Total poloidal flux [Webers/2pi], shape (nx, ny). Defaults to
eq.psi().opoint (np.ndarray or list or None, optional) – The O-point tuples/rows (R,Z,psi). Calculated if opoint or xpoint is None.
xpoint (np.ndarray or list or None, optional) – The X-point tuples/rows (R,Z,psi). Calculated if opoint or xpoint is None.
axis (object or None, optional) – Matplotlib axis object, used for plotting.
- Returns:
Returns the safety factor at ‘npsi’ points in normalised psi (a 1d array, one value per element of the flattened ‘psinorm’).
- Return type:
np.array
- freegsnke.critical.find_separatrix(eq: object, ntheta: int = 20, axis: object | None = None, psival: float = 1.0) list[tuple[float, float, float, float]][source]
Determines the (R,Z) locations (at ‘ntheta’ points) of the last closed flux surface (i.e. the separatrix). The ‘ntheta’ points are equally spaced in the geometrix poloidal angle.
- Parameters:
eq (object) – Equilibrium object.
ntheta (int, optional) – Number of points to spawn in the geometric poloidal angle.
axis (object or None, optional) – Matplotlib axis object, used for plotting.
psival (float, optional) – Normalised flux value (0 at the axis, 1 at the boundary) of the surface to find.
- Returns:
One tuple (R, Z, R_X, Z_X) per poloidal angle: the point on the plasma boundary (last closed flux surface) and the (R, Z) of the primary X-point.
- Return type:
list of tuple
- freegsnke.critical.geom_inside_mask(R: ndarray, Z: ndarray, opoint: ndarray, xpoint: ndarray) ndarray[source]
Excludes grid regions based on a line perpendicular to the segment from the O-point to the primary X-point. Points on the O-point’s side of that line are considered part of the plasma core.
- Parameters:
R (np.ndarray) – Grid R coordinates, shape (nx, ny).
Z (np.ndarray) – Grid Z coordinates, shape (nx, ny).
opoint (np.ndarray) – O-points as rows; only the (R, Z) of the first row is used.
xpoint (np.ndarray) – X-points as rows; only the (R, Z) of the first row (the primary X-point) is used.
- Returns:
geom_mask – True where the grid point lies in the core (O-point) half-plane, False otherwise.
- Return type:
ndarray of bool
- freegsnke.critical.inside_mask(R: ndarray, Z: ndarray, psi: ndarray, opoint: ndarray, xpoint: ndarray | list = [], mask_outside_limiter: ndarray | None = None, psi_bndry: float | None = None, use_geom: bool = True) ndarray[source]
Calls inside_mask_ to find plasma region inside the separatrix (with the option of calling geom_inside_mask too).
geom_inside_mask applies an additional geometrical contraint aimed at resolving cases of ‘flooding’ of the core mask through the primary Xpoint. It excludes regions based on perpendicular to segment from O-point to primary X-point.
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
opoint (np.ndarray) – O-points as rows (R,Z,psi); the first is the primary O-point.
xpoint (np.ndarray or list, optional) – X-points as rows (R,Z,psi).
mask_outside_limiter (np.ndarray or None, optional) – Masking array of shape (nx, ny), non-zero for (R, Z) grid points outside the limiter.
psi_bndry (float or None, optional) – Total poloidal flux on the plasma boundary [Webers/2pi].
use_geom (bool, optional) – Use (refined) geom_inside_mask function in conjuction with inside_mask_ to find the mask when at least one X-point is available (if set to True).
- Returns:
Boolean array of shape (nx, ny) where True denotes a point inside the core and False a point outside.
- Return type:
np.ndarray
- freegsnke.critical.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]
Identifies the plasma region inside the separatrix by performing a flood-fill algorithm starting from the magnetic axis (O-point).
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
opoint (np.ndarray) – O-points as rows (R,Z,psi); the first is the primary O-point.
xpoint (np.ndarray or list, optional) – X-points as rows (R,Z,psi).
mask_outside_limiter (np.ndarray or None, optional) – Masking array of shape (nx, ny), non-zero for (R, Z) grid points outside the limiter.
psi_bndry (float or None, optional) – Total poloidal flux on the plasma boundary [Webers/2pi]. If None, the flux of the first X-point is used.
- Returns:
Boolean array of shape (nx, ny) where True denotes a point inside the core and False a point outside.
- Return type:
np.ndarray
- freegsnke.critical.remove_dup(points: list[tuple[float, float, float]]) list[tuple[float, float, float]][source]
Removes duplicate points in the list ‘points’ based on a squared Euclidean distance (in R and Z) below 1e-5.
- Parameters:
points (list of tuple) – List of points, each with (R, Z, …) as its first two entries.
- Returns:
List of “unique” points (first occurrence kept).
- Return type:
list of tuple
- freegsnke.critical.scan_for_crit(R: np.ndarray, Z: np.ndarray, psi: np.ndarray) tuple[np.ndarray, np.ndarray][source]
Scans the grid for critical points of the total poloidal flux map ψ.
Candidate points are local minima of |grad psi|^2 on the grid, refined by a single quadratic (Newton) step and classified as O- or X-points by the sign of the Hessian determinant. The points are not ordered.
- Parameters:
R (np.ndarray) – Radial positions at which flux measured, shape (nx, ny).
Z (np.ndarray) – Vertical positions at which flux measured, shape (nx, ny).
psi (np.ndarray) – Total poloidal flux [Webers/2pi], shape (nx, ny).
- Returns:
opoint (np.ndarray) – O-point locations and flux values (R, Z, psi) as rows, shape (n_o, 3).
xpoint (np.ndarray) – X-point locations and flux values (R, Z, psi) as rows, shape (n_x, 3).