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

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.

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, limiter)[source]

Bases: object

Iy_from_jtor(jtor)[source]

Generates 1d vector of plasma current values at the grid points of the reduced plasma domain.

Parameters:

jtor (np.ndarray) – Plasma current distribution on full domain. np.shape(jtor) = np.shape(eq.R)

Returns:

Iy – Reduced 1d plasma current vector

Return type:

np.ndarray

__init__(eq, limiter)[source]

Object to handle additional calculations due to the limiter. This is primarily used by the profile functions. Each profile function has its own instance of a Limiter_handler.

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, layer_size=3)[source]

Creates a mask that is wider than the input mask, by a width=`layer_size`

Parameters:

layer_size (int, optional) – Width of the layer, by default 3

Returns:

layer_mask – Broader mask

Return type:

np.ndarray

build_mask_inside_limiter()[source]

Uses the coordinates of points along the edge of the limiter region to generate the mask of contained domain points.

Parameters:
  • eq (FreeGSNKE Equilibrium object) – Specifies the domain properties

  • limiter (freegs4e.machine.Wall object) – Specifies the limiter contour points

Returns:

mask_inside_limiter – Mask over the full domain of grid points inside the limiter region.

Return type:

np.array

build_reduced_rect_domain()[source]

Build smallest rectangular domain around limiter mask

core_mask_limiter(psi, psi_bndry, core_mask, limiter_mask_out)[source]

Checks if plasma is in a limiter configuration rather than a diverted configuration. 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.array) – 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.array) – The mask identifying the plasma region under the assumption of a diverted configuration. This is the result of FreeGS4E’s critical.core_mask Same size as psi.

  • limiter_mask_out (np.array) – The mask identifying the border of the limiter, including points just inside it, the ‘last’ accessible to the plasma. Same size as psi.

Returns:

  • psi_bndry (float) – The value of the flux function at the boundary.

  • core_mask (np.array) – The core mask after correction

  • flag_limiter (bool) – Flag to identify if the plasma is in a diverted or limiter configuration.

extract_index_mask(mask)[source]
Extracts the indices of the R and Z coordinates of the grid points in the reduced plasma domain

i.e. inside the limiter

Parameters:

mask (np.ndarray of bool) – Specifies the mask of the relevant region

extract_plasma_pts(R, Z, mask)[source]
Extracts R and Z coordinates of the grid points in the reduced plasma domain

i.e. 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

hat_Iy_from_jtor(jtor)[source]

Generates 1d vector on reduced plasma domain for the normalised vector $$ Jtor*dR*dZ/I_p $$.

Parameters:
  • jtor (np.ndarray) – Plasma current distribution on full domain. np.shape(jtor) = np.shape(eq.R)

  • epsilon (float, optional) – avoid divergences, by default 1e-6

Returns:

hat_Iy – Reduced 1d plasma current vector, normalized to total plasma current

Return type:

np.ndarray

interp_on_limiter_points(id_R, id_Z, psi)[source]

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. This is called by self.core_mask_limiter with id_R, id_Z corresponding to the grid cell outside the limiter (but in the diverted core) with the highest psi value (referred to as id_psi_max_out in self.core_mask_limiter)

Parameters:
  • id_R (int) – index of the R coordinate for the relevant grid cell

  • id_Z (_type_) – index of the Z coordinate for the relevant grid cell

  • psi (_type_) – Vaules of the total flux function ofn the solver’s grid.

Returns:

vals – Collection of floating point interpolated values of the flux function at the self.fine_point_per_cell locations relevant to all of the 9 cells {id_R-1, id_R, id_R+1} X {id_Z-1, id_Z, id_Z+1}

Return type:

np.array

interp_on_limiter_points_cell(id_R, id_Z, psi)[source]

Calculates a bilinear interpolation of the flux function psi in the solver’s grid cell [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.array on the solver's grid) – Vaules of the total flux function ofn the solver’s grid.

Returns:

vals – Collection of floating point interpolated values of the flux function at the self.fine_point_per_cell[id_R, id_Z] locations.

Return type:

np.array

limiter_points(refine=6)[source]

Based on the limiter vertices, it builds the refined list of points on the boundary of the region where the plasma is allowed. These refined boundary points are those on which the flux function is interpolated to find the value of psi_boundary in the case of a limiter plasma.

Parameters:

refine (int, optional) – the upsampling ratio with respect to the solver’s grid, by default 6.

make_layer_mask(mask, layer_size=3)[source]

Creates a mask for the points just outside the input mask, with a width=`layer_size`

Parameters:

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

normalize_sum(Iy, epsilon=1e-06)[source]

Normalises any vector by the linear sum of its elements.

Parameters:
  • jtor (np.ndarray) – Plasma current distribution on full domain. np.shape(jtor) = np.shape(eq.R)

  • epsilon (float, optional) – avoid divergences, by default 1e-6

Returns:

_description_

Return type:

_type_

rebuild_map2d(reduced_vector, map_dummy, idxs_mask)[source]

Rebuilds 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:

self.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)[source]

Reduce map from the whole domain to the smallest rectangular domain around limiter mask

Parameters:

map (np.ndarray) – Same dimensions as eq.R