pylbo.data_containers

Classes

LegolasDataContainer

Baseclass for a Legolas data container.

LegolasDataSet

Main container for a single Legolas dataset.

LegolasDataSeries

Baseclass for a Legolas data container.

Functions

ensure_dataset(→ None)

Ensures that the given data is a LegolasDataSet.

ensure_dataseries(→ None)

Ensures that the given data is a LegolasDataSeries.

Module Contents

pylbo.data_containers.ensure_dataset(data: any) None

Ensures that the given data is a LegolasDataSet.

pylbo.data_containers.ensure_dataseries(data: any) None

Ensures that the given data is a LegolasDataSeries.

class pylbo.data_containers.LegolasDataContainer

Bases: abc.ABC

Baseclass for a Legolas data container.

abstract continua()
abstract parameters()
abstract has_efs()
abstract ef_grid()
abstract ef_names()
abstract has_background()
abstract has_derived_efs()
abstract derived_ef_names()
abstract has_ef_subset()
abstract has_matrices()
abstract has_eigenvectors()
abstract has_residuals()
abstract get_sound_speed(which_values=None)
abstract get_alfven_speed(which_values=None)
abstract get_tube_speed(which_values=None)
abstract get_reynolds_nb(which_values=None)
abstract get_magnetic_reynolds_nb(which_values=None)
abstract get_k0_squared()
class pylbo.data_containers.LegolasDataSet(datfile)

Bases: LegolasDataContainer

Main container for a single Legolas dataset.

Parameters:

datfile (str, PathLike) – Path to the datfile.

header

The datfile header.

Type:

dict

grid

The base grid.

Type:

numpy.ndarray

grid_gauss

The Gaussian grid.

Type:

numpy.ndarray

equilibria

Dictionary containing the equilibrium arrays.

Type:

dict

eigenvalues

Array containing the complex eigenvalues.

Type:

numpy.ndarray

geometry

The geometry.

Type:

str

scale_factor

Array with the scale factor. One for Cartesian geometries, r for cylindrical.

Type:

numpy.ndarray

x_start

Start of the grid.

Type:

float

x_end

End of the grid

Type:

float

gridpoints

The number of base gridpoints.

Type:

int

gauss_gridpoints

The number of Gaussian gridpoints.

Type:

int

matrix_gridpoints

The dimension of the matrix.

Type:

int

ef_gridpoints

The number of eigenfunction gridpoints.

Type:

int

gamma

The ratio of specific heats.

Type:

float

eq_type

The type of equilibrium selected.

Type:

str

cgs

If True, all units are in cgs.

Type:

bool

units

Dictionary containing the unit normalisations.

Type:

dict

eq_names

Array containing the names of the equilibrium arrays.

Type:

numpy.ndarray

datfile
filereader
header
grid
grid_gauss
equilibria
eigenvalues
geometry
x_start
x_end
gridpoints
gauss_gridpoints
ef_gridpoints
gamma
eq_type
_parameters
units
cgs
eq_names
_continua
property legolas_version: pylbo._version.VersionHandler
property k2_str: str

Returns the \(k_2\) string.

property k3_str: str

Returns the \(k_3\) string.

property u1_str: str

Returns the \(u_1\) string.

property u2_str: str

Returns the \(u_2\) string.

property u3_str: str

Returns the \(u_3\) string.

property continua: dict

Returns the continua in a dict with the continua names as keys.

property parameters: dict

Returns the parameters in a dict with the parameter names as keys

property has_background: bool

Returns True if background is present.

property has_efs: bool

Returns True if eigenfunctions are present.

property ef_grid: numpy.ndarray

Returns the eigenfunction grid, None if eigenfunctions are not present.

property ef_names: numpy.ndarray

Retrieves the eigenfunction names, None if eigenfunctions are not present.

property has_derived_efs: bool

Returns True if derived eigenfunctions are present.

property derived_ef_names: numpy.ndarray

Retrieves the derived eigenfunction names, None if not present.

property has_ef_subset: bool

Returns True if the dataset contains a subset of the eigenfunctions.

property has_matrices: bool

Checks if matrices are present.

property has_eigenvectors: bool

Checks if eigenvectors are present.

property has_residuals: bool

Checks if residuals are present.

property is_mhd: bool

Checks if the dataset is MHD.

_ensure_compatibility() None

Makes sure that the dataset is backwards compatible with new changes. Mainly used for older (<2.0.0) datasets.

get_sound_speed(which_values=None) float | numpy.ndarray

Calculates the sound speed based on the equilibrium arrays, given by \(c_s = \sqrt{\frac{\gamma p_0}{\rho_0}}\).

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

Array with the sound speed at every grid point, or a float corresponding to the value of which_values if provided.

Return type:

float or numpy.ndarray

get_alfven_speed(which_values=None) float | numpy.ndarray

Calculates the Alfvén speed based on the equilibrium arrays, given by \(c_A = \sqrt{\frac{B_0^2}{\rho_0}}\).

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

Array with the Alfvén speed at every grid point, or a float corresponding to the value of which_values if provided.

Return type:

float or numpy.ndarray

get_tube_speed(which_values=None) float | numpy.ndarray

Calculates the tube speed for a cylinder, given by \(c_t = \frac{c_s c_A}{\sqrt{c_s^2 + c_A^2}}\).

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

Array with the tube speed at every grid point, or a float corresponding to the value of which_values if provided. Returns None if the geometry is not cylindrical.

Return type:

float or numpy.ndarray

get_reynolds_nb(which_values=None) float | numpy.ndarray

Calculates the Reynolds number, defined as \(R_e = \frac{ac_s}{\eta}\) where the slabsize is given by \(a = x_{end} - x_{start}\).

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

Array with the Reynolds number at every grid point, or a float corresponding to the value of which_values if provided. Returns None if the resistivity is zero somewhere on the domain.

Return type:

float or numpy.ndarray

get_magnetic_reynolds_nb(which_values=None) float | numpy.ndarray

Calculates the magnetic Reynolds number, defined as \(R_m = \frac{ac_A}{\eta}\) where the slabsize is given by \(a = x_{end} - x_{start}\).

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

Array with the magnetic Reynolds number at every grid point, or a float corresponding to the value of which_values if provided. Returns None if the resistivity is zero somewhere on the domain.

Return type:

float or numpy.ndarray

get_k0_squared() float

Calculates the squared wave number, defined as \(k_0^2 = k_2^2 + k_3^2\).

get_matrix_B() tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Retrieves the matrix B from the datfile.

Returns:

Tuple(rows – Tuple containing the rows, columns and values of the non-zero B-matrix elements. Rows and columns are integers, values are real.

Return type:

numpy.ndarray, cols: numpy.ndarray, vals: numpy.ndarray)

Raises:

MatricesNotPresent – If the matrices were not saved to the datfile.

get_matrix_A() tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Retrieves the matrix A from the datfile.

Returns:

Tuple(rows – Tuple containing the rows, columns and values of the non-zero A-matrix elements. Rows and columns are integers, values are complex.

Return type:

numpy.ndarray, cols: numpy.ndarray, vals: numpy.ndarray)

Raises:

MatricesNotPresent – If the matrices were not saved to the datfile.

get_eigenvectors() numpy.ndarray

Retrieves the eigenvectors from the datfile.

Returns:

Array containing the eigenvectors. One eigenvector in each column.

Return type:

numpy.ndarray

Raises:

EigenvectorsNotPresent – If the eigenvectors were not saved to the datfile.

get_residuals() numpy.ndarray

Retrieves the residuals from the datfile.

Returns:

Array containing the residuals.

Return type:

numpy.ndarray

Raises:

ResidualsNotPresent – If the residuals were not saved to the datfile.

_get_eigenfunction_like(ev_guesses: numpy.ndarray, ev_idxs: numpy.ndarray, getter_func: Callable) numpy.ndarray

Returns the eigenfunctions based on the supplied getter function.

Parameters:
  • ev_guesses (complex, numpy.ndarray) – Eigenvalue guesses.

  • ev_idxs (int, numpy.ndarray) – Indices of the eigenvalues to retrieve.

  • getter_func (function) – Function to retrieve the eigenfunctions.

Returns:

Array containing the eigenfunctions, items are dictionaries.

Return type:

numpy.ndarray

get_eigenfunctions(ev_guesses=None, ev_idxs=None) numpy.ndarray

Returns the eigenfunctions based on given eigenvalue guesses or their indices. An array will be returned where every item is a dictionary, containing both the eigenvalue and its eigenfunctions. Either eigenvalue guesses or indices can be supplied, but not both.

Parameters:
Returns:

Array containing the eigenfunctions and eigenvalues corresponding to the supplied indices. Every index in this array contains a dictionary with the eigenfunctions and corresponding eigenvalue. The keys of each dictionary are the eigenfunction names.

Return type:

numpy.ndarray

get_nearest_eigenvalues(ev_guesses)

Calculates the eigenvalues nearest to a given guess based on the distance between two points.

Parameters:

ev_guesses (float, complex, list of float, list of complex) – The guesses for the eigenvalues. These can be a single float/complex value, or a list/Numpy array of floats/complex values.

Returns:

The indices of the nearest eigenvalues in the eigenvalues array. The nearest eigenvalues to the provided guesses, corresponding with the indices idxs.

Return type:

Tuple(numpy.ndarray, numpy.ndarray)

get_eigenvalues_at_distance(ev_guesses, min_distance=0.0)

Calculates the nearest eigenvalues nearest to a given guess but at a minimum distance away.

Parameters:
  • ev_guesses (float, complex, list of float, list of complex) – The guesses for the eigenvalues. These can be a single float/complex value, or a list/Numpy array of floats/complex values.

  • min_distance (float) – Minimum distance from the guess the eigenvalue should have.

Returns:

The indices of the nearest eigenvalues at the minimum distance in the eigenvalues array. The nearest eigenvalues at a minimum distance to the provided guesses, corresponding with the indices idxs.

Return type:

Tuple(numpy.ndarray, numpy.ndarray)

get_omega_max(real=True, re_range=None)

Calculates the maximum eigenvalue. The real or imaginary part is used, depending on the real argument. If a range is specified, the maximum eigenvalue is calculated within that range on the real axis.

Parameters:
  • eigenvalues (numpy.ndarray(dtype=complex)) – The array of eigenvalues.

  • real (bool) – If True, the real part of the eigenvalues is used.

  • re_range (tuple(float, float)) – The range on the real axis to calculate the maximum eigenvalue. Defaults to None, which means all eigenvalues are considered.

Returns:

The maximum eigenvalue.

Return type:

complex

class pylbo.data_containers.LegolasDataSeries(datfiles)

Bases: LegolasDataContainer

Baseclass for a Legolas data container.

datasets
geometry
property continua: dict

Returns the continua. Each key corresponds to a multiple Numpy arrays, one for each dataset.

property parameters: dict

Returns the parameters. Each key corresponds to multiple Numpy arrays, one for each dataset.

property has_background: numpy.ndarray

Returns True if background is present.

property has_efs: numpy.ndarray

Returns True if eigenfunctions are present.

property ef_names: numpy.ndarray

Returns the eigenfunction names.

property ef_grid: numpy.ndarray

Returns the eigenfunction grid.

property has_derived_efs: numpy.ndarray

Returns True at index i if eigenfunctions are present in dataset i.

property derived_ef_names: numpy.ndarray

Returns the derived eigenfunction names.

property has_ef_subset: numpy.ndarray

Returns True at index i if the i-th dataset contains a subset.

property has_matrices: numpy.ndarray

Returns True at index i if the i-th dataset contains matrices.

property has_eigenvectors: numpy.ndarray

Returns True at index i if the i-th dataset contains eigenvectorst.

property has_residuals: numpy.ndarray

Returns True at index i if the i-th dataset contains residuals.

get_sound_speed(which_values=None) numpy.ndarray

Calculates the sound speed for the various datasets.

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

A Numpy array of same length as the number of datasets, containing the sound speeds. Elements are either arrays themselves or floats, depending on the value of which_values.

Return type:

numpy.ndarray

get_alfven_speed(which_values=None) numpy.ndarray

Calculates the Alfvén speed for the various datasets.

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

A Numpy array of same length as the number of datasets, containing the Alfvén speeds. Elements are either arrays themselves or floats, depending on the value of which_values.

Return type:

numpy.ndarray

get_tube_speed(which_values=None) numpy.ndarray

Calculates the tube speed for the various datasets.

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

A Numpy array of same length as the number of datasets, containing the tube speeds. Elements are either arrays themselves or floats, depending on the value of which_values. Elements are None if the geometry is not cylindrical.

Return type:

numpy.ndarray

get_reynolds_nb(which_values=None) numpy.ndarray

Calculates the Reynolds number for the various datasets.

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

A Numpy array of same length as the number of datasets, containing the Reynolds number. Elements are either arrays themselves or floats, depending on the value of which_values. Elements are None if the resistivity is zero.

Return type:

numpy.ndarray

get_magnetic_reynolds_nb(which_values=None) numpy.ndarray

Calculates the magnetic Reynolds number for the various datasets.

Parameters:

which_values (str) – Callback to get_values(), either “average”/”minimum”/”maximum”.

Returns:

A Numpy array of same length as the number of datasets, containing the magnetic Reynolds number. Elements are either arrays themselves or floats, depending on the value of which_values. Elements are None if the resistivity is zero.

Return type:

numpy.ndarray

get_k0_squared() numpy.ndarray

Calculates the squared wave number for the various datasets.

Returns:

A Numpy array of same length as the number of datasets, containing the squared wavenumber for each.

Return type:

numpy.ndarray

get_omega_max(real=True)

Calculates the maximum of the real or imaginary part of the spectrum for the various datasets.

Parameters:

real (bool) – Returns the largest real part if True (default option), returns the largest imaginary part if False.

Returns:

omega_max – A Numpy array of same length as the number of datasets, containing tuples of the eigenvalue that has the largest real or imaginary part.

Return type:

numpy.ndarray