5.3. Derivative matrix

Used in regularised inversion methods MFR and LAME. Various schemes for computation are used. Anisotropic derivatives can be computed using magnetics data.

5.3.1. Implementation

Convenience functions for computation return list of tuples holding a pair of sparse matrices.

Contains functions for derivative matrix computation.

  • new functions (isotropic) derivative_matrix and laplace_matrix
  • compute_aniso_dmats and compute_iso_dmats takes care of obtaining flux dependent or flux independent derivative matrices,
  • prepare_mag_data is an auxiliary function to calculate magnetic flux gradients and
  • px_norm is a simple auxiliary function to calculate norms for coefficients
tomotok.core.derivative.compute_aniso_dmats(grid, magflux, derivative=4, mask=None)

Prepares matrix of finite differences, magnetic configuration dependent or fixed simple differences. First or second derivative can be used

Parameters:

grid : RegularGrid

A reconstruction grid

magflux : numpy.ndarray

values of psi normalized mapped to grid

mask : numpy.ndarray, optional

bool mask

derivative : int in {1,2,3,4}

specifies type of derivative scheme, default is 4

Returns:

tuple of scipy.sparse.csrmatrix pairs

derivative matrix

tomotok.core.derivative.compute_iso_dmats(grid, derivatives=[1, 2], mask=None)

Computes isotropic derivative matrix pairs for given differential schemes.

Parameters:

grid : RegularGrid

mask : np.ndarray, optional

derivatives : list of int/str, optional

Returns:

list of dmat pairs

tomotok.core.derivative.derivative_matrix(grid: tomotok.core.geometry.grids.RegularGrid, direction: str, scheme: str = ‘forward’, mask: numpy.ndarray = None, compensate_edges=False)

Creates a derivative matrix using numerical differences

Parameters:

grid : RegularGrid

direction : str

Determines one of the 8 possible directions that can be used. Specified using the over edge neighbor ‘right’, ‘top’, ‘left’, ‘bottom’ or combination over corner directions like ‘top-left’

scheme : str, optional

Selects numerical scheme to be used. Can be ‘forward’, ‘backward’, ‘central’, ‘second’. The default value is ‘forward’.

mask : numpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edges : bool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

Returns:

scipy.sparse.csc_matrix

tomotok.core.derivative.generate_anizo_matrix(grid, atan2, derivative)

Write prepared directions atan2 into the derivative matrix. Main magic of this algorithm. Rewrite arctan into the directions and decompose directions to parallel and oblique direction.

Parameters:

grid : RegularGrid

Object with pixel grid gridinates

atan2 : numpy.ndarray

3D array of arcus tangents of Psi(R,z) evolution

derivative : int

derivative type identificator

Returns:

bper : scipy.sparse

sparse matrix with perpendicular derivatives

bpar : scipy.sparse

sparse matrix with parallel derivatives

bpar_tmp: numpy.array

(optional) dense matrix with parallel derivatives

bper_tmp: numpy.array

(optional) dense matrix with perpendicular derivatives

tomotok.core.derivative.generate_iso_dmat(grid: tomotok.core.geometry.grids.RegularGrid, derivative=4)

Computes isotropic derivative matrix.

Parameters:

grid : RegularGrid

derivative : list of int

Returns:

scipy.sparse.csc_matrix, scipy.sparse.csc_matrix

tomotok.core.derivative.get_index_shift(col_num: int, direction: str, distance: int = 1) → int

Computes the index difference of a node neighbor on a flatten grid.

Parameters:

col_num : int

number of columns in reconstruction grid

direction : str

Determines one of the 8 possible directions that can be used. Specified using the over edge neighbor ‘right’, ‘top’, ‘left’, ‘bottom’ or combination over corner directions like ‘top-left’

distance : int, optional

expressed in number of nodes, by default 1

Returns:

int

relative index of neighbor in flattened grid

tomotok.core.derivative.laplace_matrix(grid: tomotok.core.geometry.grids.RegularGrid, mask=None, compensate_edge=False, diagonals=True)

Creates sparse laplace matrix.

Parameters:

grid : RegularGrid

mask : numpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edges : bool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

diagonals : bool, optional

Selects whether to use diagonal neighbors in matrix, by default True.

Returns:

scipy.sparse.csc_matrix

tomotok.core.derivative.prepare_mag_data(flux)

Calculates gradient map of magnetic flux function Psi(R,z) and returns arcus tangens of dPsi_y/-dPsi_x that is a suitable funciton for anizotropic diffusion matrix calculation

Parameters:

flux : numpy.ndarray

array of Psi(R,z) evolution, with axes (z, R, t)

Returns

——

numpy.ndarray

arcus tangens of Psi gradient

tomotok.core.derivative.px_norm(direc)

Computed (normalised?) distance between centers of nodes.

tomotok.core.derivative.reduce_dmats(dmats, idx)

Creates reduced derivative matrices by cutting out rows and columns representing unwanted nodes.

Parameters:

dmats : tuples of scipy.sparse.csr_matrix

tuple containing full derivative matrices

idx : numpy.ndarray of bool

array or mask array to select desired nodes

Returns:

list of scipy.sparse.csr_matrix pairs

derivative matrices with removed rows and columns representing nodes outside the bdr polygon (vacuum vessel)