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:
objectBase class for a time-dependent, grid-based Landlab model.
Modelprovides configuration constructors, scheduled reporting and output, and model time management. Subclasses define the model physics by constructing their components and implementingupdate. They may overrideplot,report, andsaveto customize the corresponding scheduled events.- Parameters:
See also
ClockDefinition of the model time domain.
ModelRunnerTime-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.
- __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)¶
- 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.tomlextension are read as TOML; all other files are read as YAML.
- 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:
- 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
- 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