landlab.core.model

Base class and runner for a grid-based Landlab model.

Model authors subclass Model and implement update to advance their components by a supplied time step. The base class constructs the grid and clock, while ModelRunner advances time and runs scheduled output events.

The following landscape-evolution model combines uniform uplift, stream-power erosion, and linear hillslope diffusion:

(Greg Tucker, University of Colorado Boulder)

Examples

>>> import numpy as np
>>> from landlab.components import FlowAccumulator
>>> from landlab.components import LinearDiffuser
>>> from landlab.components import StreamPowerEroder
>>> from landlab.core.model import Model
>>> class LandscapeEvolutionModel(Model):
...     DEFAULT_PARAMS = {
...         "grid": {
...             "source": "create",
...             "create_grid": {
...                 "RasterModelGrid": {
...                     "shape": (5, 5),
...                     "xy_spacing": 1.0,
...                 },
...             },
...         },
...     }
...
...     def __init__(self, grid, *, clock, params):
...         super().__init__(grid, clock=clock, params=params)
...         rng = np.random.default_rng()
...         elevation = grid.add_zeros("topographic__elevation", at="node")
...         elevation[grid.core_nodes] = rng.uniform(size=len(grid.core_nodes))
...
...         self._uplift_rate = params["model"]["parameters"]["uplift_rate"]
...         self._flow_accumulator = FlowAccumulator(
...             grid, **params["model"]["components"]["flow_accumulator"]
...         )
...         self._flow_accumulator.run_one_step()
...         self._eroder = StreamPowerEroder(
...             grid, **params["model"]["components"]["eroder"]
...         )
...         self._diffuser = LinearDiffuser(
...             grid, **params["model"]["components"]["diffuser"]
...         )
...
...     def update(self, dt):
...         elevation = self.grid.at_node["topographic__elevation"]
...         elevation[self.grid.core_nodes] += self._uplift_rate * dt
...         self._diffuser.run_one_step(dt)
...         self._flow_accumulator.run_one_step()
...         self._eroder.run_one_step(dt)
...
...     def report(self, current_time):
...         print(f"model time: {current_time:g}")
...
>>> model = LandscapeEvolutionModel.from_params(
...     {
...         "clock": {"start": 0.0, "stop": 2.0, "step": 1.0},
...         "model": {
...             "parameters": {"uplift_rate": 0.001},
...             "components": {
...                 "flow_accumulator": {"flow_director": "D8"},
...                 "eroder": {"K_sp": 0.01},
...                 "diffuser": {"linear_diffusivity": 0.1},
...             },
...         },
...         "events": {
...             "report": {"times": [0.0, 1.0, 2.0]},
...         },
...     }
... )
>>> model.run()
model time: 0
model time: 1
model time: 2
>>> model.current_time
2.0
>>> np.all(np.isfinite(model.grid.at_node["topographic__elevation"]))
True
class Model[source]

Bases: object

Base class for a time-dependent, grid-based Landlab model.

Model provides configuration constructors, scheduled reporting and output, and model time management. Subclasses define the model physics by constructing their components and implementing update. They may override plot, report, and save to customize the corresponding scheduled events.

Parameters:
  • grid (ModelGrid) – Grid shared by the model’s components.

  • clock (Clock) – Start time, stop time, and default time-step duration.

  • params (mapping) – Model parameters. The events section configures the scheduled events.

See also

Clock

Definition of the model time domain.

ModelRunner

Time-stepping and event orchestration.

Initialize the model.

Parameters:
  • grid (ModelGrid) – A Landlab ModelGrid.

  • clock (Clock) – Start time, stop time, and default time-step duration.

  • params (mapping) – Mapping containing names and values of model parameters.

  • actions (mapping of str to callable, optional) – Additional named actions available to configured events. Each action is called with the current model time. Supplied actions replace built-in actions with the same name.

DEFAULT_PARAMS: ClassVar[Mapping[str, Any]] = {}
__init__(grid, *, clock, params, actions=None)[source]

Initialize the model.

Parameters:
  • grid (ModelGrid) – A Landlab ModelGrid.

  • clock (Clock) – Start time, stop time, and default time-step duration.

  • params (mapping) – Mapping containing names and values of model parameters.

  • actions (mapping of str to callable, optional) – Additional named actions available to configured events. Each action is called with the current model time. Supplied actions replace built-in actions with the same name.

Return type:

None

classmethod __new__(*args, **kwargs)
property current_time: float
property dt: float
classmethod from_file(input_file)[source]

Create a model from parameters stored in a YAML or TOML file.

The file contents are loaded into a parameter dictionary and passed to from_params. Files with a .toml extension are read as TOML; all other files are read as YAML.

Parameters:

input_file (str) – Name of the parameter file.

Returns:

Model constructed from the parameters in input_file.

Return type:

Model

classmethod from_params(params=None)[source]

Create a model from a parameter dictionary.

User parameters are merged with DEFAULT_PARAMS, references to arrays stored in files are resolved, and the model grid and clock are constructed before the class is initialized.

Parameters:

params (mapping, optional) – Model parameters that override DEFAULT_PARAMS.

Returns:

Model constructed from the merged parameters.

Return type:

Model

plot(current_time=0.0)[source]

Virtual function for plotting; to be overridden.

Parameters:

current_time (float)

Return type:

None

report(current_time)[source]

Issue a text update on status.

Parameters:

current_time (float)

Return type:

None

run(duration=None, dt=None)[source]

Advance the model while running scheduled events.

Parameters:
  • duration (float, optional) – Duration of the run. By default, advance from the current time to the stop time of the model clock.

  • dt (float, optional) – Maximum time-step duration. By default, use the step specified by the model clock.

Return type:

None

save(current_time)[source]

Save a grid.

Parameters:

current_time (float)

Return type:

None

update(dt)[source]

Advance the model by one time step of duration dt.

The derived class should override this function.

Parameters:

dt (float)

Return type:

None

update_until(update_to_time, dt)[source]

Advance the model to an absolute model time.

This method advances the model without running scheduled events.

Parameters:
  • update_to_time (float) – Model time to which the model should advance. It must be between the current model time and the clock stop time, inclusive.

  • dt (float) – Maximum time-step duration.

Return type:

None