pylbo.data_containers

Module Contents

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.

pylbo.data_containers.ensure_dataset(data: any) None[source]

Ensures that the given data is a LegolasDataSet.

pylbo.data_containers.ensure_dataseries(data: any) None[source]

Ensures that the given data is a LegolasDataSeries.

class pylbo.data_containers.LegolasDataContainer[source]

Bases: abc.ABC

Baseclass for a Legolas data container.

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

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

property legolas_version: pylbo._version.VersionHandler[source]
property k2_str: str[source]

Returns the \(k_2\) string.

property k3_str: str[source]

Returns the \(k_3\) string.

property u1_str: str[source]

Returns the \(u_1\) string.

property u2_str: str[source]

Returns the \(u_2\) string.

property u3_str: str[source]

Returns the \(u_3\) string.

property continua: dict[source]

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

property parameters: dict[source]

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

property has_background: bool[source]

Returns True if background is present.

property has_efs: bool[source]

Returns True if eigenfunctions are present.

property ef_grid: numpy.ndarray[source]

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

property ef_names: numpy.ndarray[source]

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

property has_derived_efs: bool[source]

Returns True if derived eigenfunctions are present.

property derived_ef_names: numpy.ndarray[source]

Retrieves the derived eigenfunction names, None if not present.

property has_ef_subset: bool[source]

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

property has_matrices: bool[source]

Checks if matrices are present.

property has_eigenvectors: bool[source]

Checks if eigenvectors are present.

property has_residuals: bool[source]

Checks if residuals are present.

property is_mhd: bool[source]

Checks if the dataset is MHD.

_ensure_compatibility() None[source]

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[source]

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[source]

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[source]

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[source]

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[source]

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[source]

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][source]

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][source]

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[source]

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[source]

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[source]

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[source]

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_derived_eigenfunctions(ev_guesses=None, ev_idxs=None) numpy.ndarray[source]

Returns the derived 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 quantities. Either eigenvalue guesses or indices can be supplied, but not both.

Parameters:
Returns:

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

Return type:

numpy.ndarray

get_nearest_eigenvalues(ev_guesses)[source]

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

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, strip=False, range_omega=(0.0, 1e+24))[source]

Calculates the maximum of the real or imaginary part of a spectrum.

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

  • strip (bool) – Look for maximum in a horizontal half-plane if True. Default False.

  • range_omega (tuple of floats) – The horizontal range of the strip if strip=True.

Returns:

omega_max – The eigenvalue that has the largest real or imaginary part in the chosen strip. Default: in the whole complex plane.

Return type:

complex

class pylbo.data_containers.LegolasDataSeries(datfiles)[source]

Bases: LegolasDataContainer

Baseclass for a Legolas data container.

property continua: dict[source]

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

property parameters: dict[source]

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

property has_background: numpy.ndarray[source]

Returns True if background is present.

property has_efs: numpy.ndarray[source]

Returns True if eigenfunctions are present.

property ef_names: numpy.ndarray[source]

Returns the eigenfunction names.

property ef_grid: numpy.ndarray[source]

Returns the eigenfunction grid.

property has_derived_efs: numpy.ndarray[source]

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

property derived_ef_names: numpy.ndarray[source]

Returns the derived eigenfunction names.

property has_ef_subset: numpy.ndarray[source]

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

property has_matrices: numpy.ndarray[source]

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

property has_eigenvectors: numpy.ndarray[source]

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

property has_residuals: numpy.ndarray[source]

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

get_sound_speed(which_values=None) numpy.ndarray[source]

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[source]

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[source]

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[source]

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[source]

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[source]

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

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