5.5. Tools#

Utilities complementing the core functionalities.

5.5.1. Phantom Emissivity#

Contains utility functions for creation of emissivity phantoms.

The phantoms are based on a gaussian profile as a function of magnetic flux coordinates. A function for creation of a simple flux map with elliptical shape is also provided. Additionally, a function for creation of a polar phase matrix is provided, which can be used to create magnetic island-like structures in the emissivity phantom.

tomotok.tools.phantoms.regular_elliptical_flux(grid: RegularGrid, span: float | tuple[float, float] = 1.5) ndarray

Creates a matrix of artificial flux surfaces with elliptical shape based on provided grid.

tomotok.tools.phantoms.elliptical_flux(radial_num: int, vertical_num: int, span: float | tuple[float, float] = 1.5) ndarray

Creates a matrix of artificial flux surfaces with elliptical shape.

Minimum values at the border are determined by the span parameter.

Parameters:
radial_numint

number of nodes along radial axis

vertical_numint

number of nodes along vertical axis

spanfloat or tuple of two floats, optional

flux value at the center of grid edge if tuple, first value is for radial axis, second for vertical

Returns:
numpy.ndarray

Matrix with generated fluxes

tomotok.tools.phantoms.gaussian_on_flux(flux: ndarray, amplitude: float = 1, center: float = 0, width: float = 0.1, limit: float = 1, limit_width: float = 0.2, limit_power: int = 2) ndarray

Creates gaussian artificial emissivity profile by transforming provided flux values

\[f = a \mathrm{e}^{-(f - c)^2 / w }\]

The limit width parameter allows to create a smooth transition from gaussian profile to zero at the limit value of flux. This is done by multiplying the gaussian profile with a polynomial function that goes from 1 to 0 in the range of limit - limit_width to limit.

Parameters:
fluxnp.ndarray

Flux values for transformation, any shape is supported

amplitudefloat, optional

maximum of gaussian profile,

centerfloat, optional

center of gaussian profile allows hollow profile generation when mapped on psi

widthfloat, optional

width of gaussian profile

limitfloat, optional

flux value where emissivity is forced to reach zero if flux > limit emissivity is set to zero

limit_widthfloat, optional

width of transition from gaussian profile to zero at limit value of flux

limit_powerfloat, optional

power of polynomial transition from gaussian profile to zero at limit value of flux

Returns:
numpy.ndarray

Transformed values of x with same dimensions

tomotok.tools.phantoms.polar_phase_matrix(grid: RegularGrid, num: int = 3, shift: float = 0.0, center: tuple[float, float] | None = None) ndarray

Creates a matrix with sine phase in poloidal direction relative to provided center.

The intended use is to modify an emissivity pattern by multiplying it with the returned matrix. This could be used to create island-like radiating structures in the phantom. The phase is applied in the positive angle direction (counterclockwise). If center is not provided, the grid centre is used.

Parameters:
gridRegularGrid

grid for which the phase matrix is generated

numint, optional

number of periods per one rotation

shiftfloat, optional

initial phase shift in degrees

centertuple of two floats, optional

center of polar phase, if None, grid centre is used

tomotok.tools.phantoms.iso_psi(nx: int, ny: int, span: float = 1.5) ndarray

Creates matrix of artificial isotropic psi profile with border values for each axis equal to span.

Deprecated since version 2.0: Use elliptical_flux() instead.

Parameters:
nxint

Number of pixels on x axis

nyint

Number of pixels on y axis

spanfloat, optional

Value of result on the center of border

Returns:
numpy.ndarray

Matrix with generated profile

tomotok.tools.phantoms.gauss(x: ndarray, w: float = 0.1, lim: float = 1, amp: float = 1, cen: float = 0.0) ndarray

Creates anisotropic gaussian artificial emissivity by 1D transform of x

\[f = amp \left( \mathrm{e}^{-(x-cen)^2 / w } - \mathrm{e}^{-(lim-cen)^2 / w)} \right)\]

Deprecated since version 2.0: Use gaussian_on_flux() instead.

Can be used on np.ndarray. Lim should be greater than cen.

Parameters:
xfloat, array, np.ndarray

Contains values to be transformed, usually psi

wfloat, optional

width of gaussian profile

limfloat, optional

minimal value of x where transform gives zero if x > lim emissivity is set to zero

ampfloat, optional

amplitude of gaussian profile,

cenfloat, optional

center of gaussian profile allows hollow profile generation when mapped on psi

Returns:
numpy.ndarray

Transformed values of x with same dimensions

tomotok.tools.phantoms.gauss_iso(nx: int, ny: int, span: float = 1.2, w: float = 0.1, lim: float = 1, amp: float = 1, cen: float = 0.0) ndarray

Creates isotropic gaussian distribution. See references for iso_psi and gauss

Deprecated since version 2.0: Use gaussian_on_flux() instead.

tomotok.tools.phantoms.polar_phase(x: ndarray, num: int = 3, shift: float = 0) ndarray

Applies sine phase in radial angle direction to given profile x.

Assumes equal dimension of grid elements in both axes.

Deprecated since version 2.0: Use polar_phase_matrix() instead.

Parameters:
xnumpy.ndarray

Profile for application of polar phase.

numint, optional

number of periods per one rotation

shiftfloat, optional

initial phase shift

Returns:
numpy.ndarray

Matrix with applied polar phase

tomotok.tools.phantoms.islands(psi: ndarray, w: float = 0.01, lim: float = 1, amp: float = 1, cen: float = 0.4, num: int = 3, shift: float = 0.0) ndarray

Creates island like phantom from given psi profile. See references for gauss and polar_phase.

Deprecated since version 2.0: Use gaussian_on_flux() and polar_phase_matrix() instead

See also

gauss, polar_phase

5.5.2. HDF interface#

Handles saving and loading of sparse matrices to/from HDF files.

Currently supported formats:
  • scipy.sparse.csc_array

  • scipy.sparse.csr_array

  • scipy.sparse.dia_array

tomotok.tools.hdf.sparse_to_hdf(matrix: csc_array | csr_array | dia_array, group: Group)

Saves scipy.sparse matrix of formats (csc, csr, dia) into hdf file group.

tomotok.tools.hdf.hdf_to_sparse(group: Group) spmatrix

Loads scipy.sparse matrix of formats (csc, csr, dia) from hdf file group.

tomotok.tools.hdf.dia_to_hdf(matrix: dia_array, group: Group)

Saves dia matrix to hdf group.

tomotok.tools.hdf.hdf_to_dia(group: Group) dia_array

Loads dia matrix from hdf group.

tomotok.tools.hdf.cs_to_hdf(matrix: csc_array | csr_array, group: Group)

Saves compressed sparse matrix to hdf group.

tomotok.tools.hdf.hdf_to_cs(group: Group) csc_array | csr_array

Loads compressed sparse matrix from hdf group.