Skip to content

The run lifecycle

rompy separates describing a model run from preparing it and from running it. A run is described by objects; generating writes the files the model reads; running adds an execution environment. Because the steps are separate, the same description can be inspected, generated on a laptop and run in Docker or on a cluster.

The steps

flowchart LR
    A["ModelRun<br/>run_id, period,<br/>output_dir, config"] -->|"generate()"| B["Workspace<br/>output_dir/run_id"]
    B -->|"run(backend config)"| C["Model outputs"]
    C -->|"postprocess(processor config)"| D["Results"]
    B -->|"zip()"| E["run_id.zip"]
    A -.->|"pipeline(): generate, run, postprocess"| D
Step Method What it needs What it does
Describe ModelRun(...) A model configuration, a period and an output directory Validates the description. Nothing is written.
Generate generate() Access to the input data Fetches and processes the data and writes the model's input files into the workspace
Run run(backend) A backend configuration, and the model executable or image Runs the model in the workspace
Postprocess postprocess(processor) A postprocessor configuration Checks, transforms or moves the outputs
All three pipeline(...) A backend and a postprocessor configuration Runs generate, run and postprocess in order
Archive zip() A generated workspace Zips the workspace and deletes the folder

Only the run step needs a model executable. Generated files can be checked before anything runs: their presence shows that preparation worked, not that the scientific setup is right.

Inside the steps, ModelRun hands the work to the objects it holds:

sequenceDiagram
    participant U as User
    participant R as ModelRun
    participant C as Model config
    participant B as Run backend
    participant P as Postprocessor
    U->>R: generate()
    R->>C: config(run)
    Note over C: data objects read their sources<br/>and write files into the workspace
    C-->>R: context["config"]
    R->>C: render(context, output_dir)
    C-->>R: input files in the workspace
    U->>R: run(backend_config, workspace_dir)
    R->>B: backend_config.get_backend_class()
    R->>B: run(model_run, config, workspace_dir)
    B-->>R: True or False
    U->>R: postprocess(processor_config)
    R->>P: process(model_run, **fields)
    P-->>R: results

The data objects, such as DataGrid or DataBlob, belong to the model configuration. A DataGrid reads a source (a file, an intake catalogue, Datamesh) and writes the part the model needs; a DataBlob copies a file from a path or URI into the workspace.

ModelRun

ModelRun holds what is common to every model:

Field Default Meaning
run_id "run_id" Name of the run, and of its workspace folder
period 2020-02-21 04:00 to 2020-02-24 04:00, 15 min TimeRange of the simulation
output_dir ./simulations Folder the workspace is created in
config BaseConfig The model configuration, selected by its model_type
delete_existing False Delete an existing workspace before generating
run_id_subdir True Put the workspace in output_dir/run_id; if False, use output_dir itself

config is a discriminated union of the configuration classes registered by the installed model plugins under the rompy.config entry point. model_type: xbeach selects rompy-xbeach's configuration, for example; see How rompy-xbeach works for what that configuration contains.

The workspace

The workspace, or staging directory, is where the model's input files are written and where the model runs. It is output_dir/run_id, or output_dir when run_id_subdir=False. It is created the first time staging_dir is used, and emptied first if delete_existing=True. Otherwise existing files are kept and overwritten.

Generate

This run uses rompy's base configuration, which renders a small example template:

from pathlib import Path

from rompy.core.config import BaseConfig
from rompy.core.time import TimeRange
from rompy.model import ModelRun

run = ModelRun(
    run_id="demo",
    period=TimeRange(start="2023-01-01", end="2023-01-02", interval="1h"),
    output_dir=OUT_DIR,
    config=BaseConfig(),
)
workspace = run.generate()

for path in sorted(workspace.rglob("*")):
    print(path.relative_to(OUT_DIR))
demo/INPUT
demo/datasets
demo/datasets/readme.md
demo/outputs
demo/outputs/readme.md

The INPUT file shows values taken from the run:

text = (workspace / "INPUT").read_text()
print("\n".join(line for line in text.splitlines() if not line.startswith("$")))
run_id: 'demo'
compute_start: 20230101.000000
compute_interval: 1.0 HR
compute_stop: 20230102.000000

generate() does three things:

  1. Builds the runtime context. context["runtime"] is the run itself as a dictionary (run.model_dump()), plus staging_dir, generation metadata (_generated_at, _generated_by, _generated_on) and the date format _datefmt.
  2. Calls the configuration. If the configuration is callable, context["config"] = config(run); otherwise it is the configuration object. Model plugins do their data work here: a call reads the sources, writes forcing and grid files into the workspace and returns the values the templates need.
  3. Renders. config.render(context, run.output_dir) writes the input files. The default renders a cookiecutter template; plugins can write files directly instead. See Templates and rendering.

It returns the workspace path.

Run

run() takes a backend configuration: LocalConfig, DockerConfig or SlurmConfig. The configuration names the backend class that runs it, and run() returns True if the model succeeded.

A local command runs in the workspace. Here it is a shell command standing in for a model executable:

from rompy.backends import LocalConfig

ok = run.run(LocalConfig(command="echo 'model finished' > run.log"), workspace_dir=workspace)
print(ok, (workspace / "run.log").read_text())
True model finished

Pass workspace_dir when the workspace is already generated. Without it, the backend calls generate() first. Backends, postprocessors and pipelines describes the backends and their settings.

Postprocess

postprocess() takes a postprocessor configuration and returns a dictionary of results. The built-in no-op postprocessor only checks that the workspace exists:

from rompy.postprocess.config import NoopPostprocessorConfig

print(run.postprocess(NoopPostprocessorConfig()))
{'success': True, 'message': 'No postprocessing requested - validation only', 'run_id': 'demo', 'output_dir': '/tmp/tmpfua6j1ac/demo', 'validated': True}

Pipeline

pipeline() runs the three steps with a pipeline backend, "local" by default. It stops at the first step that fails and reports which one:

results = run.pipeline(
    backend_config=LocalConfig(command="ls > files.txt"),
    processor=NoopPostprocessorConfig(),
)
print(results["success"], results["stages_completed"])
True ['generate', 'run', 'postprocess']

Zip

zip() writes the workspace to <workspace>.zip and then deletes the workspace folder:

archive = run.zip()
print(archive.name, workspace.exists())
demo.zip False

Variants and chained runs

Because a run is described by objects, a variant is a new run with one change: a different friction coefficient, wave boundary or period. Give each variant its own run_id so that each gets its own workspace; the inputs can then be compared before anything runs and the outputs afterwards. The same pattern covers sensitivity tests, calibration and ensembles.

for day in ["2023-01-01", "2023-01-02"]:
    variant = ModelRun(
        run_id=f"day-{day}",
        period=TimeRange(start=day, duration="1d", interval="1h"),
        output_dir=OUT_DIR,
        config=run.config,
    )
    print(variant.generate().name)
day-2023-01-01
day-2023-01-02

Copying a ModelRun

A ModelRun keeps its workspace path once it has been used. A copy made with model_copy(update={"run_id": ...}) after generate() still points to the original workspace. Create variants as new ModelRun objects, as above.

Chained runs work the same way: the second run starts from the saved state of the first instead of from rest.

See it in the notebooks