Package: model#

The model is what a groundwater solver reads, written without any solver. A [domain] table says where the aquifer lies, a [flow] table gives its parameters, initial heads, boundaries and sinks and sources, and a [transport] table holds the tracking settings. The package turns those tables into typed config models and the config models into runtime objects. It puts the forcings on the cells of a mesh, maps the properties onto those cells, binds loaded data to the flow and holds the arithmetic of the daily soil reservoir that turns the weather into recharge. Flow (flow.py) is the object the setup binds loaded data to, Model (model.py) is the frozen object a solver reads, and prepare_solver builds it from the Flow. The same step gives it the top and the bottom of every cell, by the rule the backend declares in domain/cell_column.py, so no solver samples the DEM itself. The package is a layer because the solvers, the workflow, the calibration and the results all need one description of the model, and none of them should own it.

The model reads core, schema, catchment and mesh, and never data, results or solver. It sees loaded data through the two protocols of contracts.py. It is read by solver (most of it), workflow, calibration, config, data, results, project and the package root. The order of the layers is in tests/unit/architecture/layer_matrix.yaml.

The package has nine folders: domain/, properties/, forcing/, boundaries/, sinks_sources/, initial/, time/, soil/ and transport/. The folder boundaries/ is a port and has its own README, which names the boundary ids a solver backend can serve. The page of the package lists every module with the first line of its docstring, and it says how a flow is built and where to add what.

Modules#

boundaries/: The boundary conditions of a flow, and the binders that put the loaded data on a built Flow. The boundary ids form a closed table, FLOW_BOUNDARY_DEFINITIONS of boundary_condition_registry.py, where a new condition is a row added in the code and never a plugin. The table names no backend: a backend lists the ids it poses in its supports, and config/cross_checks.py refuses a file that asks for one it does not list.

Module

What it does

Lines

barriers.py

Flow barrier traces read from config or preloaded objects, and put on a Flow.

95

binder_payloads.py

The lake and SFR payloads of a Flow as plain dicts the binders can enrich.

33

boundary_condition_registry.py

Flow boundary-condition registry and runtime bundle helpers.

325

boundary_conditions.py

Flow Boundary Condition Models

564

climate.py

Sea level, recharge and ETP put on a Flow, from loaded forcings.

272

cutoff_wall_shore.py

Lay a declared cutoff-wall trace on the lake shore it faces.

239

lakes.py

Geometry, abacus, bathymetry, cutoff wall and forcings put on the lakes of a Flow.

414

streams.py

SFR reach networks put on a Flow, and the catchment runoff they route.

115

top level

Module

What it does

Lines

contracts.py

Shared process contracts used by HydroModPy internals.

45

flow.py

The flow process as the setup binds it, before the Model is built.

456

flow_config.py

Pydantic configuration model for flow-process definitions.

872

initial_config.py

The [flow.ic] and [spinup] sections, and the start state [flow.ic] asks for.

633

model.py

The frozen Model every flow backend reads, and the functions that derive one model from another.

474

payloads.py

The generic initial-condition, boundary-condition and sink/source models, and their normalizers.

75

supports.py

The closed set of names a solver backend lists in supports: the traits of the Model it honours.

50

domain/

Module

What it does

Lines

active_cells.py

The cells a domain is defined on, read from the terrain or from a mask layer.

133

build.py

The one constructor of a domain geometry.

205

capability.py

domain-build: a terrain and a depth model in, a sealed vertical extent out.

188

cell_column.py

The top and the bottom of each cell, read from the DEM and the substratum before the solve.

698

depth_model_config.py

The depth models of [domain], the four ways to place the base of the aquifer.

182

domain.py

Domain assembly logic built on top of Surface and DomainConfig.

358

domain_config.py

The [domain] table, DomainConfig.

84

domain_document.py

The document that describes a built domain, written for the mesh generator.

149

geometry.py

The DEM grid and the catchment a model is built on, read once from the delineation.

167

lake_bed_abacus.py

Compute the simulated abacus by flooding a per-cell lake bed.

196

lake_bed_regrid.py

Conservative resampling of a bathymetry raster onto mesh cells.

164

layering.py

Layer bottoms and idomain of a layering, from shares of the column or from one base per layer.

317

layers_config.py

Pydantic configuration of [domain.layers], the vertical layering of the aquifer.

353

spatial_support.py

The support providers, their registry and the contract of the supports a parameter asks for.

417

spatial_support_config.py

The kinds of spatial support a parameter can be zoned by, as config models.

278

structure_binders.py

Geographic-side binders for data-to-structure updates.

157

support_fields.py

The support fields that cut a domain into zones a parameter can vary on.

255

top_sampling_config.py

How the top of a cell column is sampled from the DEM, the top_sampling table of a solver.

130

worker.py

domain-build, executed: a terrain and a depth model in, a geometry out.

483

forcing/

Module

What it does

Lines

area_weights.py

Area-weighted mean of a gridded source over the cells of a mesh.

184

cells.py

Forcing fields and station series on the cells of a mesh.

250

dated_forcing.py

One quantity that varies over the stress periods, read the same way wherever it is mounted.

384

etp_computed.py

The ETP a [data.etp] formula asks for, computed on the grid of the loaded temperature.

67

forcing_bridge.py

Generic bridge from data-manager LoadResult to solver-ready forcing.

240

interpolation.py

Shared spatial interpolation utilities for structured grids.

323

native.py

The forcing at its native step, beside the stress-period view.

244

rasters.py

xarray and file-based fields on cell centers, and their spatial mean.

587

recharge_computed.py

The steady recharge a [data.recharge] formula asks for, from the yearly weather.

92

recharge_estimates.py

Recharge estimated from the weather, the gauge or the piezometers, to compare with the run.

274

recharge_estimates_config.py

TOML surface of [recharge_estimates], the recharge compared with the run.

224

recharge_estimates_run.py

Run the [recharge_estimates] methods on the loaded data and lay out their rows.

513

recharge_estimators.py

The methods a [recharge_estimates] section can name: what each reads and gives.

39

recharge_rates.py

The mean recharge the stream-network criterion divides by.

148

recharge_scenario.py

A recharge scenario built from the loaded recharge: the mean year, with dry years inserted.

136

soil_budget.py

The printed water-budget block of the soil reservoir (spec section 5.2).

333

soil_columns.py

The soil columns: capacity classes inside each SAFRAN cell, and their catchment weights.

342

soil_config.py

TOML surface of the soil reservoir.

574

soil_forcing.py

Soil reservoir orchestrator: forcings in, MODFLOW-ready grids out.

381

soil_grids.py

The precipitation and ETP grids the soil reads, and the guards they must pass.

309

soil_ru_budget.py

The soil capacity that closes the multi-year water budget against the gauge.

195

soil_stored.py

Write and read the soil reservoir outputs as soil.npz and soil.json.

226

sources.py

Readers of a gridded or located source, shared by the forcing discretizers.

389

stations.py

Point-record discretization on cell centers.

165

steady_periods.py

The rate a steady stress period takes: one rule for every solver.

251

synthetic.py

Shared hydrological forcing utilities reusable across workflows.

155

time_alignment.py

Temporal alignment helpers for forcing series.

67

time_forcing.py

Shared helpers to resolve flow time-dependent forcings against simulation time.

307

types.py

Shared forcing type aliases.

12

validation.py

Validation helpers for numeric forcing payloads.

150

volume_closure.py

Volume closure: recharge and runoff times one factor, so their mean matches the gauge.

295

initial/

Module

What it does

Lines

heads.py

The head a run starts from, as arrays on a backend’s own grid.

203

state.py

The five states a run can start from, as frozen records in SI units.

208

properties/

Module

What it does

Lines

cell_sampling.py

Deterministic interior sampling of mesh cells for support-field fractions.

182

field_param.py

Field parameter container supporting homogeneous, heterogeneous and raster values.

559

field_param_io.py

TOML factory for FieldParam.

107

field_spatial.py

Generic field interface and shared discretization structure.

138

geology_config.py

Geology polygons or raster a mesh follows, and one geology field definition.

152

geology_field.py

Geology spatial field for heterogeneous parameter mapping on simulation meshes.

331

mesh_cells.py

The cells of a planar cell mesh as the field supports read them.

129

physical_properties.py

Pydantic model for flow physical properties with pint units.

84

plotting.py

Draw one value per cell of a planar mesh on a matplotlib axes.

155

property_ids.py

The one table of the ids of [flow.param.<id>].

129

property_table.py

The one mapping of the hydraulic properties of a model onto a cell mesh.

625

raster_cells.py

The cells of a mesh read on a property map, one value per cell from the pixels it covers.

234

two_zone_field.py

Concrete field geometry on the unit square split into two zones.

202

unit_square_mesh.py

Planar cell meshes of the unit square, for the square-domain field demos.

216

vertical_profile.py

Depth profile of a field parameter: a multiplicative factor f(depth).

261

zone_fractions.py

Zone fractions of a spatial support on the cells of a planar cell mesh.

247

sinks_sources/

Module

What it does

Lines

container.py

Top-level container grouping wells, recharge, and ETP payloads.

153

etp.py

Diffuse evapotranspiration payload for the flow process.

145

flow_barrier.py

Horizontal flow barrier (HFB) payload for the flow process.

289

lake.py

Lake / reservoir boundary-condition payload for the flow process.

647

recharge.py

Diffuse recharge payload for the flow process.

248

sfr.py

Streamflow-routing (SFR) boundary-condition payload for the flow process.

540

wells.py

Pumping/injection well payloads for the flow process.

444

soil/

Module

What it does

Lines

etp.py

Potential evapotranspiration from the daily air temperature, numpy only.

112

kernel.py

Daily soil reservoir, vectorized over columns, looped over days.

648

laws.py

The storage laws of the soil reservoir, one function per method, numpy only.

150

time/

Module

What it does

Lines

history_contract.py

Shared temporal contract helpers for transient flow histories.

175

regime.py

Shared flow-regime vocabulary.

23

transport/

Module

What it does

Lines

transport_config.py

Pydantic configuration model for transport-process definitions.

570

Inside the package#

How a flow is built#

FlowConfig.from_toml_section in flow_config.py canonicalizes the boundary ids, resolves the forcing paths and validates the [flow] table. Then Flow(config=cfg.flow) runs set_config. It fills the parameters (FieldParam objects), the initial and boundary conditions and the sinks and sources, and it converts the well fluxes and the recharge to SI. ensure_flow in workflow/steps/data.py builds it at setup.

After the data step, workflow/steps/data.py calls the apply_*_to_flow functions of boundaries/. They put the loaded data on the Flow, and the same cascade runs for a trial.

Then model_from_flow in model.py returns a Model. It holds the same objects as the Flow, in SI, with the geometry, the time grid and the shared solver options beside them. A prior run is read into a PriorState there, and the river network A is read on the cells (Geometry.channel_exits). So no adapter opens a Zarr store or projects A itself.

A solver adapter reads the Model and never the Flow. A change to the model is a new object (with_properties, steady_variant, cycle_variant).

The domain#

The domain is the aquifer as a body of rock. The mesh decides the cells in the plan, and domain/ decides what is above and below each cell. build_domain in build.py is the one constructor of a Domain, called by the setup step and the domain-build capability. layering.py turns [domain.layers] and the two surfaces into the bottom and the idomain of every layer. It works on arrays only, so it knows no Domain.

structure_binders.py writes the geology and the catchment zones into the domain once the data is loaded. The zones are a CatchmentZonesField, defined in catchment/algorithms/catchment_zones_field.py beside the code that computes them. A heterogeneous parameter asks for a support, which spatial_support.py builds.

The lake bed rebuilt from a bathymetry raster lives here because it changes the top and the layers of the cells under a lake. carve_lake_bed in solver/modflow6/builders/lake_cells.py calls it and imports the two lake bed modules inside its body.

The domain-build capability runs build_domain as a job for outside tools, and hmp process run domain-build <job> runs it.

Properties and forcing#

A FieldParam holds the values of one parameter in SI units, one per zone, and its depth profile. FieldParam.to_mesh_field weights the zone values with the zone share of each cell (a Field of field_spatial.py) and returns a MeshWithValues. map_flow_properties in property_table.py does this for K, Kv, Sy, Ss and the K tensor of every solver, and it refuses a value that is not positive. A parameter of kind raster holds no zone values: the build of the model reads its map from [data.property_map] and raster_cells.py gives each cell the mean of the pixels it covers.

A forcing reaches a solver by two paths. A grid or a set of stations goes through discretize_fields_on_cells in forcing/cells.py. It gives one rate per cell and per stress period, in m/s, whatever the mesh engine. A CRS other than the mesh CRS is refused, because a grid is never reprojected. The area method weighs each pixel by the area it shares with the cell, so a cell rate times its area is the integral of the grid. A series that does not go through a mesh is aligned to the stress periods by time_alignment.py and resolved by time_forcing.resolve_period_values_from_forcing. steady_periods.py is the one rule every solver shares for a steady period. xarray, rasterio, pyproj and the scipy interpolators are imported inside the functions that use them, so a run without a grid forcing does not load them.

The soil reservoir#

The daily soil reservoir makes a recharge from the weather before the groundwater solve. soil/kernel.py holds the arithmetic, one function per equation with its reference. It is a science module, so it needs only numpy and reads from top to bottom. The rest sits in forcing/ (soil_*.py) because it reads the records that a project loads, through contracts.py, so nothing here imports data. soil_forcing.compute_soil_forcings runs the kernel day by day. The soil applies to MODFLOW 6 only. The PyHELP recharge model sits in hydromodpy_annex/physics/pyhelp/.

Boundaries of the package#

The model never imports data, results or solver. It sees loaded data through the LoadResultProto and FieldRecordProto of contracts.py. Inside the package, boundaries/ reads flow.py for annotations only, and this is the one cycle. soil/ and initial/ are science folders, where a module may import only numpy and one neighbour of its folder, and tests/unit/architecture/test_science_module_imports.py holds it. Nothing in properties/ imports domain/, and the mesh never imports this package. The import table is the generated page Layer graph.

Where to add what#

A flow parameter is a field on FlowConfig with a profile and a description, as Add a config field says. A boundary id is an entry of FLOW_BOUNDARY_DEFINITIONS in boundaries/boundary_condition_registry.py, then the solver that builds it. A sink or source kind is a payload module in sinks_sources/, a field on FlowSinksSourcesConfig in container.py and a key in Flow.set_sinks_sources. A way to bring loaded data into a Flow is an apply_*_to_flow function in boundaries/, called from workflow/steps/data.py. An initial-condition type is a model in initial_config.py, added to _IC_VARIANTS and to the union, and a state in initial/state.py. A field the solvers read is a member of Model, filled by model_from_flow. A new process is a subpackage with its own runtime class and a config on HydroModelBase, and Add a process gives the recipe. A depth model is a model in depth_model_config.py, and a support kind starts in spatial_support_config.py, then a provider in spatial_support.py. A soil-reservoir option is a field on SoilReservoirConfig in forcing/soil_config.py, its bound in _RANGE_RULES, and its use in soil/kernel.py or forcing/soil_forcing.py. A new key of the field-parameter TOML is a field of a Pydantic model in core/config_kit/field_param/sections.py with its description.

Tests#

The unit tests mirror the code under tests/unit/model/. To read the property layer, start at field_spatial.py, then field_param.py, and run tools/cases/field/square/.