freegsnke.limiter_func module
Defines the functionality related to the implementation of the limiter in FreeGSNKE.
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.limiter_func.Limiter_handler(eq: object, limiter: object)[source]
Bases:
objectHandles limiter-related geometric operations for FreeGSNKE profile evaluation.
This class provides functionality for enforcing and querying the plasma boundary defined by the limiter geometry. It is primarily used by profile objects to determine whether grid points lie inside the allowable plasma region and to support limiter-dependent calculations.
An equilibrium owns one handler; profile objects share its fixed grid geometry and limiter masks.
Notes
The limiter is treated as a closed boundary defining the valid plasma domain.
- Iy_from_jtor(jtor: ndarray) ndarray[source]
Generate 1d vector of plasma current values at the grid points of the reduced plasma domain.
- Parameters:
jtor (np.ndarray) – Plasma current density on full domain. np.shape(jtor) = np.shape(eq.R)
- Returns:
Iy – Reduced 1d plasma current vector (jtor * dR * dZ inside the limiter).
- Return type:
np.ndarray
- __init__(eq: object, limiter: object) None[source]
Object to handle additional calculations due to the limiter.
This is primarily used by the profile functions.
- Parameters:
eq (FreeGSNKE equilibrium object) – Used as a source of info on the solver’s grid.
limiter (a tokamak.Wall object) – Contains a list of R and Z coordinates (the vertices) which define the region accessible to the plasma. The boundary itself is the limiter.
- broaden_mask(mask: ndarray, layer_size: int = 3) ndarray[source]
Create a mask that is wider than the input mask, by a width=`layer_size`.
- Parameters:
mask (np.ndarray of bool) – Mask to broaden, shape (nx, ny).
layer_size (int, optional) – Width of the layer, by default 3
- Returns:
layer_mask – Broader mask, same shape as mask.
- Return type:
np.ndarray of bool
- build_mask_inside_limiter() None[source]
Use the coordinates of points along the edge of the limiter region to generate the mask of contained domain points.
Sets
mask_inside_limiter(boolean mask over the full domain of grid points inside the limiter region) andpath(the limitermatplotlib.path.Path).
- build_reduced_rect_domain() None[source]
Build smallest rectangular domain around limiter mask.
Sets
Rrange,Zrange(index ranges) andmask_inside_limiter_red.
- core_mask_limiter(psi: ndarray, psi_bndry: float, core_mask: ndarray, limiter_mask_out: ndarray, current_sign: float = 1.0) tuple[float, ndarray, bool][source]
Check if the plasma is in a limiter configuration rather than a diverted one.
This is obtained by checking whether the core mask deriving from the assumption of a diverted configuration implies an overlap with the limiter. If so, an interpolation of psi on the limiter boundary points is called to determine the value of psi_boundary and to recalculate the core_mask accordingly.
- Parameters:
psi (np.ndarray) – The flux function, including both plasma and metal components. np.shape(psi) = (eq.nx, eq.ny)
psi_bndry (float) – The value of the flux function at the boundary. This is xpt[0][2] for a diverted configuration, where xpt is the output of critical.find_critical
core_mask (np.ndarray) – The mask identifying the plasma region under the assumption of a diverted configuration. This is the result of FreeGSNKE’s critical.core_mask Same size as psi.
limiter_mask_out (np.ndarray) – The mask identifying the border of the limiter, including points just inside it, the ‘last’ accessible to the plasma. Same size as psi. Currently unused.
current_sign (float, optional) – Sign of the plasma current, used to orient flux comparisons.
- Returns:
psi_bndry (float) – The value of the flux function at the boundary.
core_mask (np.ndarray) – The core mask after correction (float array if no correction was applied, as the input is cast to float).
flag_limiter (bool) – Flag to identify if the plasma is in a diverted or limiter configuration.
- down_project(vec: ndarray) ndarray[source]
Projects a (discretized current) vector defined on the full plasma domain onto the reduced plasma domain.
- Parameters:
vec (np.ndarray) – Vector on the full plasma domain, with size self.map2d.shape
- Returns:
Vector on the reduced plasma domain, of length len(self.mask_inside_limiter)
- Return type:
np.ndarray
- extract_index_mask(mask: ndarray) ndarray[source]
Extract the grid indices of the points selected by a mask (e.g. inside the limiter).
- Parameters:
mask (np.ndarray of bool) – Specifies the mask of the relevant region, shape (nx, ny).
- Returns:
Integer array of shape (2, n_points) with the R and Z indices of the points where mask is True.
- Return type:
np.ndarray
- extract_plasma_pts(R: ndarray, Z: ndarray, mask: ndarray) ndarray[source]
Extract R and Z coordinates of the grid points selected by a mask (e.g. inside the limiter).
- Parameters:
R (np.ndarray) – R coordinates on the domain grid, e.g. eq.R
Z (np.ndarray) – Z coordinates on the domain grid, e.g. eq.Z
mask (np.ndarray of bool) – Specifies the mask of the relevant region
- Returns:
Array of shape (n_points, 2) of (R, Z) coordinates of the masked points.
- Return type:
np.ndarray
- hat_Iy_from_jtor(jtor: ndarray) ndarray[source]
Generate 1d vector on reduced plasma domain for the normalised vector $$ Jtor*dR*dZ/I_p $$.
- Parameters:
jtor (np.ndarray) – Plasma current density on full domain. np.shape(jtor) = np.shape(eq.R)
- Returns:
hat_Iy – Reduced 1d plasma current vector, normalized to its (regularised) sum
- Return type:
np.ndarray
- property idx_grid_points: ndarray
Grid indices for contour fallback, built only when first required.
- Returns:
Array of shape (nx*ny, 2) of (R, Z) grid indices.
- Return type:
np.ndarray
- interp_on_limiter_points(id_R: int, id_Z: int, psi: ndarray) tuple[ndarray, ndarray][source]
Interpolate psi on the limiter boundary points of the 9 cells around a cell.
Uses interp_on_limiter_points_cell to interpolate the flux function psi on the refined limiter boundary points relevant to the 9 cells {id_R-1, id_R, id_R+1} X {id_Z-1, id_Z, id_Z+1}. Interpolated values on the boundary points relevant to the cells above are collated and returned. Not currently called by
core_mask_limiter(the corresponding code there is commented out).- Parameters:
id_R (int) – Index of the R coordinate for the central grid cell.
id_Z (int) – Index of the Z coordinate for the central grid cell.
psi (np.ndarray) – Values of the total flux function on the solver’s grid, shape (nx, ny).
- Returns:
vals (np.ndarray) – Interpolated values of the flux function at the self.fine_point_per_cell locations relevant to all of the 9 cells.
idxs (np.ndarray) – Indices into
self.fine_pointof the corresponding points.
- interp_on_limiter_points_cell(id_R: int, id_Z: int, psi: ndarray) tuple[ndarray | list, list][source]
Bilinearly interpolate the flux function psi in one grid cell.
The cell is [eq.R[id_R], eq.R[id_R + 1]] x [eq.Z[id_Z], eq.Z[id_Z + 1]]. The interpolation is returned directly for the refined points on the limiter boundary that fall in that grid cell, as assigned through the self.fine_point_per_cell objects.
- Parameters:
id_R (int) – Index of the R coordinate for the relevant grid cell.
id_Z (int) – Index of the Z coordinate for the relevant grid cell.
psi (np.ndarray) – Values of the total flux function on the solver’s grid, shape (nx, ny).
- Returns:
vals (np.ndarray or list) – Interpolated values of the flux function at the self.fine_point_per_cell[id_R, id_Z] locations (empty list if the cell contains no limiter points).
idxs (list) – Indices into
self.fine_pointof the corresponding points (empty list if the cell contains no limiter points).
- limiter_points(refine: int = 16) None[source]
Build the refined list of points on the limiter boundary from the limiter vertices.
These refined boundary points (the intersections of the limiter segments with the solver grid lines, plus the vertices) are those on which the flux function is interpolated to find the value of psi_boundary in the case of a limiter plasma. Sets
fine_point,grid_per_limiter_fine_point,mask_limiter_cells,limiter_mask_out,offending_maskand thefine_point_per_cell*dictionaries.- Parameters:
refine (int, optional) – Upsampling ratio with respect to the solver’s grid. Currently unused (the fine points are the grid-line intersections), by default 16.
- make_layer_mask(mask: ndarray, layer_size: int = 3) ndarray[source]
Create a mask for the points just outside the input mask, with a width=`layer_size`.
- Parameters:
mask (np.ndarray of bool) – Input mask, shape (nx, ny).
layer_size (int, optional) – Width of the layer outside the limiter, by default 3
- Returns:
layer_mask – Mask of the points outside the mask within a distance of layer_size
- Return type:
np.ndarray of bool
- normalize_sum(Iy: ndarray, epsilon: float = 1e-06) ndarray[source]
Normalise any vector by the linear sum of its elements.
- Parameters:
Iy (np.ndarray) – Vector to normalise.
epsilon (float, optional) – Added to the sum to avoid divergences, by default 1e-6
- Returns:
Iy / (sum(Iy) + epsilon).- Return type:
np.ndarray
- psi_on_limiter_boundary(psi: ndarray) ndarray[source]
Interpolate the flux function on all refined limiter boundary points.
- Parameters:
psi (np.ndarray) – Total poloidal flux on the solver grid.
- Returns:
Flux values interpolated on the refined limiter boundary.
- Return type:
np.ndarray
- rebuild_map2d(reduced_vector: ndarray, map_dummy: ndarray, idxs_mask: ndarray) ndarray[source]
Rebuild 2d map on full domain corresponding to 1d vector reduced_vector on smaller plasma domain.
- Parameters:
reduced_vector (np.ndarray) – 1d vector on reduced plasma domain
map_dummy (np.ndarray) – Specifies the size of the desired rectangular map
idxs_mask (np.ndarray) – Specifies the location of the pixels of the reduced vector in the rectangular domain Note this is specific to map_dummy, e.g. use self.extract_index_mask
- Returns:
map2d – 2d map on domain as map_dummy. Values on gridpoints outside the reduced plasma domain are set to zero.
- Return type:
np.ndarray
- reduce_rect_domain(map: ndarray) ndarray[source]
Reduce map from the whole domain to the smallest rectangular domain around limiter mask.
Requires
build_reduced_rect_domainto have been called (setsRrange,Zrange).- Parameters:
map (np.ndarray) – Same dimensions as eq.R
- Returns:
The sub-array of map covering the reduced rectangular domain.
- Return type:
np.ndarray
- up_project(vec: ndarray) ndarray[source]
Projects a (discretized current) vector defined on the reduced plasma domain onto the full plasma domain.
- Parameters:
vec (np.ndarray) – Vector on the reduced plasma domain, of length len(self.mask_inside_limiter)
- Returns:
Vector on the full plasma domain, with size self.map2d.shape
- Return type:
np.ndarray
- validate_limiter_inside_domain() None[source]
Raise a clear error if the limiter is not inside the solution domain.
Limiter boundary interpolation assumes that every limiter segment lies strictly inside the rectangular solver grid, so that each refined boundary point belongs to a valid interpolation cell.
- Raises:
ValueError – If any limiter vertex lies on or outside the edge of the solver grid.