The Veros interface

Veros main class

class veros.Veros(backend='numpy', loglevel='info', logfile=None, profile=False, override=None)[source]

Bases: object

Main class for Veros, used for building a model and running it.

Note

This class is meant to be subclassed. Subclasses need to implement the methods set_parameter(), set_topography(), set_grid(), set_coriolis(), set_initial_conditions(), set_forcing(), and set_diagnostics().

Parameters:
  • backend (bool, optional) – Backend to use for array operations. Possible values are numpy and bohrium. Defaults to None, which tries to read the backend from the command line (set via a flag -b/--backend), and uses numpy if no command line argument is given.
  • loglevel (one of {debug, info, warning, error, critical}, optional) – Verbosity of the model. Tries to read value from command line if not given (-v/--loglevel). Defaults to info.
  • logfile (path, optional) – Path to a log file to write output to. Tries to read value from command line if not given (-l/--logfile). Defaults to stdout.

Example

>>> import matplotlib.pyplot as plt
>>> from climate.veros import Veros
>>>
>>> class MyModel(Veros):
>>>     ...
>>>
>>> simulation = MyModel(backend="bohrium")
>>> simulation.run()
>>> plt.imshow(simulation.psi[..., 0])
>>> plt.show()
set_parameter()[source]

To be implemented by subclass.

First function to be called during setup. Use this to modify the model settings.

Example

>>> def set_parameter(self):
>>>     self.nx, self.ny, self.nz = (360, 120, 50)
>>>     self.coord_degree = True
>>>     self.enable_cyclic = True
set_initial_conditions()[source]

To be implemented by subclass.

May be used to set initial conditions.

Example

>>> @veros_method
>>> def set_initial_conditions(self):
>>>     self.u[:, :, :, self.tau] = np.random.rand(self.u.shape[:-1])
set_grid()[source]

To be implemented by subclass.

Has to set the grid spacings dxt, dyt, and dzt, along with the coordinates of the grid origin, x_origin and y_origin.

Example

>>> @veros_method
>>> def set_grid(self):
>>>     self.x_origin, self.y_origin = 0, 0
>>>     self.dxt[...] = [0.1, 0.05, 0.025, 0.025, 0.05, 0.1]
>>>     self.dyt[...] = 1.
>>>     self.dzt[...] = [10, 10, 20, 50, 100, 200]
set_coriolis()[source]

To be implemented by subclass.

Has to set the Coriolis parameter coriolis_t at T grid cells.

Example

>>> @veros_method
>>> def set_coriolis(self):
>>>     self.coriolis_t[:, :] = 2 * self.omega * np.sin(self.yt[np.newaxis, :] / 180. * self.pi)
set_topography()[source]

To be implemented by subclass.

Must specify the model topography by setting kbot.

Example

>>> @veros_method
>>> def set_topography(self):
>>>     self.kbot[:, :] = 10
>>>     # add a rectangular island somewhere inside the domain
>>>     self.kbot[10:20, 10:20] = 0
set_forcing()[source]

To be implemented by subclass.

Called before every time step to update the external forcing, e.g. through forc_temp_surface, forc_salt_surface, surface_taux, surface_tauy, forc_tke_surface, temp_source, or salt_source. Use this method to implement time-dependent forcing.

Example

>>> @veros_method
>>> def set_forcing(self):
>>>     current_month = (self.time / (31 * 24 * 60 * 60)) % 12
>>>     self.surface_taux[:, :] = self._windstress_data[:, :, current_month]
set_diagnostics()[source]

To be implemented by subclass.

Called before setting up the diagnostics. Use this method e.g. to mark additional variables for output.

Example

>>> @veros_method
>>> def set_diagnostics(self):
>>>     self.diagnostics["snapshot"].output_vars += ["drho", "dsalt", "dtemp"]
after_timestep()[source]

Called at the end of each time step. Can be used to define custom, setup-specific events.

flush()[source]

Flush computations if supported by the current backend.

run()[source]

Main routine of the simulation.

veros_method decorator

veros.veros_method(function)[source]

Decorator that injects the current backend as variable np into the wrapped function.

Note

This decorator should be applied to all functions that make use of the computational backend (even when subclassing climate.veros.Veros). The first argument to the decorated function must be a Veros instance.

Example

>>> from climate.veros import Veros, veros_method
>>>
>>> class MyModel(Veros):
>>>     @veros_method
>>>     def set_topography(self):
>>>         self.kbot[...] = np.random.randint(0, self.nz, size=self.kbot.shape)
veros.veros_inline_method(function)[source]

Tools & utilities