freegsnke.Myy_builder module

Defines the plasma_current Object, which handles the lumped parameter model used as an effective circuit equation for the plasma.

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.Myy_builder.FFT_Myy_handler(*args, **kwargs)[source]

Bases: Myy_handler

Object handling all operations which involve the Myy matrix, i.e. the mututal inductance matrix of all domain grid points. To reduce memory usage, the Green’s function is computed in Fourier space, which reduces the size scaling from quartic to cubic.

__init__(limiter_handler: object) → None[source]

Instantiates the object

Parameters:

limiter_handler (FreeGSNKE limiter object, i.e. eq.limiter_handler) – Sets the properties of the domain grid and those of the limiter

check_Myy(hatIy: ndarray) → bool[source]

Dummy, always returns False. Defined only for compatibility with non_linear_solver

dot(hatIy: ndarray) → ndarray[source]

Performs the product with a vector defined on the reduced domain, i.e. inside the limiter. Returns a vector on the input domain.

Parameters:

hatIy (np.ndarray) – 1d vector on reduced plasma domain, e.g. inside the limiter

Returns:

Resulting 1d vector on the same domain.

Return type:

np.ndarray

force_build_Myy(hatIy: ndarray) → None[source]

Ignored. Defined only for compatibility with non_linear_solver

grid_greens(R_1D: ndarray, Z_1D: ndarray) → ndarray[source]

Calculates the Green’s function values on the Fourier space of the full geometric domain defined by the limiter. Relies on the fact that Green’s functions are invariant for vertical translations.

Parameters:
  • R_1D (np.ndarray) – Like eq.R, but only in 1D i.e. self.make_1D(limiter_handler.eqR,…)

  • Z_1D (np.ndarray) – Like eq.Z, but only in 1D

Returns:

gg_fft – Array of shape (nR,nR,L_fft) containing the values of the Green’s function in Fourier space. (L_fft = (2*nZ - 1) // 2 + 1)

Return type:

np.ndarray

update_caching_behavior(cache: bool | None) → None[source]

Ignored. Defined only for compatibility with non_linear_solver

class freegsnke.Myy_builder.Myy_handler(*args, **kwargs)[source]

Bases: ABC

Abstract base class for objects handling all operations which involve the Myy matrix, i.e. the mututal inductance matrix of all domain grid points.

abstract check_Myy(hatIy: ndarray) → int | bool[source]

As defined in Reduced_Myy_handler. Definition enforced for compatibility with non_linear_solver

abstract dot(hatIy: ndarray) → ndarray[source]

Performs the product with a vector defined on the reduced plasma domain, i.e. inside the limiter, independently of the domain of Myy. Returns a vector on the input domain.

Parameters:

hatIy (np.ndarray) – 1d vector on reduced plasma domain, e.g. inside the limiter

abstract force_build_Myy(hatIy: ndarray) → None[source]

As defined in Reduced_Myy_handler. Definition enforced for compatibility with non_linear_solver

abstract grid_greens(R_1D: ndarray, Z_1D: ndarray) → ndarray[source]

Calculates and stores the Green’s function values in the corresponding Myy domain.

Parameters:
  • R_1D (np.ndarray) – Array of dim=1 representing the R values of the grid in the appropriate Myy domain.

  • Z_1D (np.ndarray) – Array of dim=1 representing the Z values of the grid in the appropriate Myy domain.

Returns:

Array containing the Greens’s function in the appropriate representation.

Return type:

np.ndarray

make_1D(R: ndarray, Z: ndarray) → tuple[ndarray, ndarray][source]

Utility function to obtain a 1D vector representation of R and Z

Parameters:
  • R (np.ndarray) – 2D array with the R values of the grid

  • Z (np.ndarray) – 2D array with the Z values of the grid

Returns:

1D vector of R-values and 1D vector of Z-values

Return type:

tuple[np.ndarray, np.ndarray]

abstract update_caching_behavior(cache: bool | None) → None[source]

As defined in Reduced_Myy_handler. Definition enforced for compatibility with non_linear_solver

class freegsnke.Myy_builder.Reduced_Myy_handler(*args, **kwargs)[source]

Bases: Myy_handler

Handles all operations involving the Myy matrix.

Myy is the mutual inductance matrix of the domain grid points. To reduce memory usage, the domain on which Myy is built and stored is set adaptively, so as to cover the plasma. This object handles that adaptivity.

__init__(limiter_handler: object, layer_size: int = 5, tolerance: int = 3, cache_myy: bool = True) → None[source]

Instantiate the object.

Parameters:
  • limiter_handler (object) – FreeGSNKE limiter handler (i.e. eq.limiter_handler). Sets the properties of the domain grid and those of the limiter.

  • layer_size (int, optional) – Used when recalculating Myy. A layer of layer_size pixels is added to envelop the mask defined by the plasma. This ‘broadened’ mask defines the pixels included in the Myy matrix.

  • tolerance (int, optional) – Used to check if Myy needs recalculating. Myy is not recalculated if the mask defined by the plasma region, broadened by tolerance pixels, is fully contained in the domain of the current Myy matrix.

  • cache_myy (bool, optional) – Determines whether the mutual inductance matrix is pre-calculated and stored, or re-calculated every time a product with Myy is computed.

build_Myy_from_mask(mask: ndarray) → None[source]

Build the Myy matrix only including domain points in the input mask.

Sets myy_mask_red, outside_myy_mask, idxs_myy_mask_red and myy.

Parameters:

mask (np.ndarray) – Mask of the domain points to include. Defined on the reduced rectangular domain grid, i.e. the smallest rectangular domain around the limiter mask (same size as self.mask_inside_limiter_red).

build_mask_from_hatIy(hatIy: ndarray, layer_size: int) → ndarray[source]

Build the mask used by build_Myy_from_mask from a hatIy map.

The mask is broadened by layer_size pixels, and the limiter mask is taken into account.

Parameters:
  • hatIy (np.ndarray) – 1d vector on the reduced plasma domain, e.g. inside the limiter.

  • layer_size (int) – Number of pixels by which the mask is broadened.

Returns:

Boolean mask on the reduced rectangular domain, of the same shape as self.mask_inside_limiter_red.

Return type:

np.ndarray

check_Myy(hatIy: ndarray) → int[source]

Check whether Myy needs rebuilding for the input hatIy.

hatIy, broadened by tolerance pixels, is compared with the current Myy mask. Note 1: tolerance should be smaller than layer_size in build_mask_from_hatIy. Note 2: tolerance should be larger than the number of pixels by which the plasma is expected to move every timestep of the evolution.

Parameters:

hatIy (np.ndarray) – 1d vector on the reduced plasma domain, e.g. inside the limiter.

Returns:

Number of broadened-hatIy pixels lying outside the current Myy mask. Nonzero means Myy should be rebuilt.

Return type:

int

dot(hatIy: ndarray) → ndarray[source]

Multiply Myy by a vector defined on the reduced plasma domain.

Parameters:

hatIy (np.ndarray) – 1d vector on the reduced plasma domain, e.g. inside the limiter.

Returns:

Resulting 1d vector on the same domain.

Return type:

np.ndarray

force_build_Myy(hatIy: ndarray) → None[source]

Build the Myy matrix only including domain points in the input vector.

The vector is broadened by layer_size pixels and need not be a mask.

Parameters:

hatIy (np.ndarray) – 1d vector on the reduced plasma domain, e.g. inside the limiter.

grid_greens(R_1D: ndarray, Z_1D: ndarray) → ndarray[source]

Calculate the Green’s function values on the reduced rectangular domain.

The values are computed on the minimal rectangular region that fully encompasses the limiter, using that the Green’s functions are invariant under vertical translations.

Parameters:
  • R_1D (np.ndarray) – Like eq.R, but on the rectangular reduced domain and only in 1D, i.e. self.reduce_rect_domain(limiter_handler.eqR)[:, 0]

  • Z_1D (np.ndarray) – Like eq.Z, but on the rectangular reduced domain and only in 1D, uniform spacing is assumed.

Returns:

Array of shape (nR, nR, nZ) with entry [i, j, k] equal to 2 pi times the Green’s function between (R[i, 0], k dz) and (R[j, 0], 0).

Return type:

np.ndarray

update_caching_behavior(cache: bool | None) → None[source]

Changes the caching behavior to the value of the cache parameter.

Parameters:

cache (bool | None) – Tells whether Myy should be cached, or recomputed for every Myy product. None means to preserve whatever cachaing behavior was preset.

freegsnke.Myy_builder.make_Myy_handler(domain_type: str, limiter_handler: object, layer_size: int | None = None, tolerance: int | None = None, cache_myy: bool | None = None) → Myy_handler[source]

Returns an Myy_handler object of appropriate type given the domain_type specified. Note that if `domain_type`==”fft” all optional arguments are ignored.

Parameters:
  • domain_type ({"reduced", "fft"}) – Type of domain to define Myy over, reduced means Myy is defined of over the reduced plasma domain, fft means it is defined over frequency space (not limited to the plasma).

  • limiter_handler (FreeGSNKE limiter object, i.e. eq.limiter_handler) – Sets the properties of the domain grid and those of the limiter

  • layer_size (int, optional) – (for reduced domain only) Used when recalculating myy. A layer of layer_size pixels is added to envelop the mask defined by the plasma. This ‘broadened’ mask defines the pixels included in the myy matrix By default 5

  • tolerance (int, optional) – (for reduced domain only) Used to check if myy needs recalculating. Myy is not recalculated if the mask defined by the plasma region, broadened by tolerance pixels, is fully contained in the domain of the current myy matrix, By default 3

  • cache_myy (bool, optional) – Determines whether the mutual inductance matrix should be pre-calculated and stored, or re-calculated every time a product with Myy is computed (reducing memory usage by up to an order of magnitude). True by default.

Returns:

Handler instance for the requested Myy representation.

Return type:

Myy_handler