pyshqg.core.model module

Submodule dedicated to the QG model.

class pyshqg.core.model.QGModelTrajectory(model, variables)

Bases: object

Container class for a QG model trajectory.

The purpose of this class is to make a container class for a model trajectory. In practice, for each recorded variable, a list is created. Each time a snapshot is appended to the trajectory, using the append method, all the recorded variables are appended to the appropriate lists.

At any time, the trajectory can be exported to ‘numpy’ format (i.e. as a dictionnary of str to numpy.ndarray, with one entry for each recorded variable) using the to_numpy method or to ‘xarray’ format (i.e. as an xarray.Dataset, with one xarray.DataArray for each recorded variable) using the to_xarray method.

core_dimensions

Mapping from potential variable name to list of core dimension names.

Type:

dict of str to list of str

model

Reference to the QG model instance.

Type:

pyshqg.core.model.QGModel

time

List of the snapshot times.

Type:

list of float

variables

Mapping from recorded variable name to list of snapshots.

Type:

dict of str to list of numpy.ndarray

__init__(model, variables)

Constructor for the QG model trajectory.

Parameters:
  • model (pyshqg.core.model.QGModel) – Reference to the QG model instance.

  • variables (list of str) – List of variables to record.

append(time, state)

Creates a snapshot and adds it to the trajectory.

If some variables to record are not yet available, they are computed and added to the state. The present method returns the state, so that the added variables can be used later on.

Parameters:
  • time (float) – Snapshot time.

  • state (dict of str to backend array) – QG model state to record.

Returns:

state – Recorded QG model state.

Return type:

dict of str to backend array

to_numpy()

Exports the trajectory to ‘numpy’ format.

_num_batch_dimensions()

Computes the number of batch dimensions.

to_xarray()

Exports the trajectory to ‘xarray’ format.

class pyshqg.core.model.QGModel(backend: pyshqg.backend.Backend, spectral_transformations: pyshqg.core.spectral_transformations.SpectralTransformations, poisson_solver: pyshqg.core.poisson.QGPoissonSolver, dissipation: pyshqg.core.dissipation.QGDissipation, forcing: pyshqg.core.source.QGForcing)

Bases: object

QG model class.

The purpose of this class is to gather all the necessary pieces to implement the QG model. The main method of this class is compute_model_tendencies, which can be used to compute the tendencies of the QG model.

Parameters:
backend: pyshqg.backend.Backend
spectral_transformations: pyshqg.core.spectral_transformations.SpectralTransformations
poisson_solver: pyshqg.core.poisson.QGPoissonSolver
dissipation: pyshqg.core.dissipation.QGDissipation
forcing: pyshqg.core.source.QGForcing
static model_state(spec_q)

Constructs a model state.

Parameters:

spec_q (backend array, shape (..., 2, T+1, T+1)) – Relative vorticity in spectral space $hat{q}$.

Returns:

state – QG model state as dictionary of variables.

Return type:

dict of str to backend array

model_trajectory(variables)

Constructs and initialises a model trajectory.

Parameters:

variables (list of str) – List of variables to record.

Returns:

trajectory – Initialised trajectory object.

Return type:

pyshqg.core.model.QGModelTrajectory

compute_dependent_variable(state, variable)

Computes a dependent variable.

The requested variable is computed and added to the state to be used later on. If the requested variable require another variable which is unavailable, the latter unavailable variable will be computed first. Also note that if the requested variable is already available, then nothing happens.

The present method currently supports the following variables:

  • the total vorticity in spectral space $hat{q}^{mathsf{t}}$;

  • the stream function in spectral space $hat{psi}$;

  • the drag in real space $zeta$;

  • the spatial gradient of $psi$ and $q$ in real space;

  • the relative vorticity in real space $q$;

  • the stream function in real space $psi$.

Parameters:
  • state (dict of str to backend array) – Current QG model state.

  • variable (str) – Requested variable.

Returns:

state – Updated QG model state.

Return type:

dict of str to backend array

compute_dependent_variables(state, variables)

Computes a list of dependent variable.

The present method uses the compute_dependent_variable method to compute each requested variable in the list.

Parameters:
  • state (dict of str to backend array) – Current QG model state.

  • variables (list of str) – List of requested variable.

Returns:

state – Updated QG model state.

Return type:

dict of str to backend array

compute_model_tendencies(state)

Computes the model tendencies.

The model tendencies are computed only for the control variable of the QG model, i.e. the relative vorticity in spectral space $hat{q}$. They are encapsulated in a model state dictionnary before they are returned.

Parameters:

state (dict of str to backend array) – Current QG model state.

Returns:

tendencies – Tendencies associated to the QG model state.

Return type:

dict of str to backend array

apply_euler_step(state, tendencies, step)

Applies an explicit Euler integration step.

The integration step is applied only to the control variable of the QG model, i.e. the relative vorticity in spectral space $hat{q}$.

Parameters:
  • state (dict of str to backend array) – Current QG model state.

  • tendencies (dict of str to backend array) – QG model tendencies to apply.

  • step (float) – Step size.

Returns:

state – Updated QG model state.

Return type:

dict of str to backend array

Notes

This algorithmic step is at the core of every explicit integration scheme, e.g. the Runge–Kutta schemes as used in pyshqg.core.integration.RungeKuttaModelIntegrator.