Package: solver#

The numerical backends. solver.backend names the flow backend, the table [solver.<name>] holds its settings, and a [[simulation.process]] entry names the solvers each process runs. The package holds the adapters that turn a built Flow or Transport into a backend run, the extractors that read the backend’s files into the results store, and the registry that pairs a <process_type>_<name> key with each. Four flow backends ship, MODFLOW 6, MODFLOW-NWT, Boussinesq and the linear reservoir, with the transport tools that go with them.

The package is a layer because every backend meets the rest of HydroModPy through one contract in base/. A layer above imports the registry from solver/base/registry.py and never a backend, so a backend can be added or replaced without touching it.

What a backend can do is declared on its adapter class, in the thirteen attributes described in base/README.md. writes lists what it stores, supports the traits of the Model it honours and category how it resolves space.

The parts are base/ (the shared contract, the plan and the runner), modflow_grid/ and modflow_common/ (what the two MODFLOW backends share), modflow6/, modflow_nwt/, boussinesq/ and linear_reservoir/. Each backend folder has a README of its port.

solver reads core, schema, catchment, mesh, model, results and itself, as tests/unit/architecture/layer_matrix.yaml says. workflow, config, calibration, cli, project and the root facade read it.

The page docs/source/contribute/architecture/packages/solver.rst lists the modules and holds the import rules, how a run reaches a backend and where to add what.

Modules#

base/: The solver port is the contract every flow and transport backend meets, so the runner solves a model and never names a solver. It also holds the plan of a simulation and the runner that walks it, because both sit between the [simulation] table and the adapter of a solver. base reads core, model, catchment and results, never a backend, and the run_solver step of the workflow reads it.

Module

What it does

Lines

adapter_protocol.py

The SolverAdapter Protocol, and the attributes a backend registers with.

346

cell_lookup.py

Locate a cell by coordinates on a solver mesh.

86

cleanup.py

Scratch cleanup helper used by per-adapter cleanup(ctx) implementations.

40

drain_conductance.py

Shared MODFLOW DRN row arithmetic: fallback conductance and discharge band.

83

observables.py

Turn a solver series or field into an observable result.

139

observation_config.py

Pydantic configuration models for the [observation] section.

134

plan.py

Immutable runtime objects describing a resolved simulation plan.

316

planner.py

Translate declarative simulation config into an executable simulation plan.

148

registry.py

The two registries of the solver port: solvers for the adapters, extractors for their output readers.

182

rerun.py

When the runner solves a model again from a solved run instead of building it.

110

results_config.py

Pydantic configuration models for [simulation.results].

64

runner.py

Execute a resolved SimulationPlan against a prepared runtime state.

519

simulation_config.py

Declarative simulation configuration models.

580

solver_config.py

The [solver] section: the flow solver of a run and the settings of each one.

398

boussinesq/: The in-house Boussinesq flow solver on triangular meshes. The unknown is the cell-centred head h, from which the solver rebuilds the saturated thickness, the transmissivity T(h) = K b(h), the fluxes between cells and through the Dirichlet supports, the drainage and the surface closure, then looks for the head that sets the finite-volume residual to zero. A transient step adds the storage term A S (h^{n+1} - h^n) / dt. The equations and the functions that compute them are in boussinesq_math_notes.tex. It is a backend of the solver port of solver/base, and it reads base, core, model, catchment, mesh and results, never a MODFLOW package.

Module

What it does

Lines

_mesh_builders.py

Constructor helpers for BoussinesqMesh.

329

adapter.py

Adapter for the flow/boussinesq solver pair.

345

boussinesq.py

High-level driver for the standalone Boussinesq flow backend.

400

calibration_extractors.py

Lightweight readers for Boussinesq calibration trials.

154

catalog.py

What the Boussinesq solver can run, and how a run picks it.

580

export_payload.py

Export helpers for the Boussinesq solver.

96

flow_to_boussinesq_adapter.py

Helpers for the BoussinesqFlowAdapter.

249

mesh.py

Solver-owned mesh view of a cell mesh.

247

options.py

The settings of the Boussinesq solve, [solver.boussinesq].

348

property_mapping.py

Hydraulic properties of the planar Boussinesq backend.

67

runtime_contract.py

Common runtime contracts for Boussinesq nonlinear solvers.

149

runtime_summary.py

Helpers that keep Boussinesq runtime summary bookkeeping out of the driver.

339

smoothing.py

Smooth C¹ approximations of the piecewise operators used in Boussinesq.

96

solver_contract.py

Process-to-runtime contract helpers for the Boussinesq solver.

229

state.py

Normalized in-memory solver state for the Boussinesq backend.

110

boussinesq/assembly/

Module

What it does

Lines

boundary_flux_reconstruction.py

Boundary-flux reconstruction helpers for prescribed-head diagnostics.

130

fluxes.py

Flux and transmissivity operators used by the Boussinesq assembly.

220

inputs.py

Input-normalization helpers for Boussinesq assembly and Jacobians.

170

period_mean.py

The assembly a stress period exports when its solve takes several steps.

63

residuals.py

Residual builders shared by Boussinesq runtimes.

403

surface.py

Surface-interaction helpers for the Boussinesq assembly.

137

types.py

Internal data containers used by the Boussinesq assembly façade.

63

boussinesq/drivers/

Module

What it does

Lines

forcing.py

Shared forcing/boundary preparation for Boussinesq driver helpers.

145

state.py

Helpers to build Boussinesq runtime state from steady/transient solve traces.

404

steady.py

Steady driver helpers for the Boussinesq solver.

139

transient.py

Transient driver helpers for the Boussinesq solver.

417

boussinesq/extractors/

Module

What it does

Lines

flow.py

Output adapter for Boussinesq solver results.

413

boussinesq/forcing/

Module

What it does

Lines

common.py

Common forcing-resolution helpers for the Boussinesq solver.

291

dirichlet_support_resolution.py

Dirichlet-support resolution helpers for the Boussinesq forcing resolver.

229

drainage_resolution.py

Drainage and ocean-support helpers for the Boussinesq forcing resolver.

96

initial_conditions.py

Initial-condition resolution helpers for the Boussinesq forcing resolver.

36

recharge_resolution.py

Recharge-resolution helpers for the Boussinesq forcing resolver.

151

well_resolution.py

Well-resolution helpers for the Boussinesq forcing resolver.

108

boussinesq/jacobian/

Module

What it does

Lines

common.py

Common helpers shared by semianalytic Boussinesq Jacobian builders.

125

fd.py

Finite-difference Jacobian helpers for Boussinesq runtimes.

302

operator_triplets.py

Base sparse Jacobian triplets for Boussinesq residual operators.

262

partition_triplets.py

Regularized-partition saturation triplets for the Boussinesq Jacobian.

128

semianalytic.py

Semi-analytic Jacobian helpers for the Boussinesq residual.

185

boussinesq/runtimes/

Module

What it does

Lines

diagnostic_files.py

Stable names for persisted solver diagnostic artifacts.

33

diagnostics_io.py

Small readers and writers the Boussinesq diagnostic files share.

144

dry_equilibrium.py

Dry-equilibrium probes for Boussinesq obstacle solves.

334

execution_common.py

Common runtime-execution helpers shared by Boussinesq backends.

69

head_only_common.py

Common helpers shared by head-only Boussinesq runtimes.

112

local.py

In-process nonlinear runtime for the Boussinesq backend.

122

newton_common.py

Shared damped-Newton template for head-only Boussinesq runtimes.

153

partition_utils.py

Shared robustness helpers for regularized-partition nonlinear runtimes.

110

petsc_common.py

Shared PETSc helpers for Boussinesq runtimes.

159

petsc_mixed.py

PETSc runtime for the Boussinesq backend on Linux.

589

petsc_mixed_common.py

Pure helpers shared by the PETSc mixed-complementarity runtime.

278

petsc_partition.py

PETSc runtime for the regularized partition surface-interaction model.

267

petsc_ts_vi_obstacle.py

Experimental PETSc TS + SNESVI runtime for a head-only obstacle problem.

727

petsc_vi_obstacle.py

Experimental PETSc SNESVI runtime for a head-only obstacle formulation.

226

scipy_dense.py

SciPy-based nonlinear runtime for the Boussinesq backend.

159

scipy_sparse.py

SciPy sparse-linear-algebra runtime for the Boussinesq backend.

421

stationary_failure_diagnostics.py

Persistent diagnostics for failed Boussinesq stationary solves.

669

ts_vi_obstacle_diagnostics.py

Persistent diagnostics for the experimental PETSc TS VI obstacle runtime.

294

vi_bounds.py

Variable-bound helpers shared by PETSc Boussinesq VI runtimes.

88

vi_obstacle_diagnostics.py

Persistent diagnostics for the experimental PETSc VI obstacle runtime.

405

boussinesq/runtimes/petsc_vi/

Module

What it does

Lines

obstacle.py

Obstacle-specific math for the PETSc VI head-only formulation.

154

petsc.py

PETSc SNESVI configuration and solver diagnostics.

160

vi.py

VI orchestration: substeps, diagnostics records, top-level PETSc SNESVI solve.

611

linear_reservoir/: A global flow model. One cell takes the whole catchment, and the solve reads the Model and its own options table and nothing else. It is the smallest backend of the solver port of solver/base, and the port tests run on it like on the others.

Module

What it does

Lines

adapter.py

The linear reservoir: a global flow model on the one-cell catchment mesh.

583

extractor.py

The extractor the linear reservoir registers under its adapter key.

136

kernel.py

Exact kernel of the linear reservoir backend, numpy only.

59

options.py

Settings of the linear reservoir, [solver.linear_reservoir].

47

modflow6/: MODFLOW 6 through FloPy: a GWF flow model, and GWT or PRT transport on the same simulation. It is a backend of the solver port of solver/base. It reads base, modflow_common and modflow_grid, never modflow_nwt or boussinesq.

Module

What it does

Lines

adapter.py

Adapter for the flow/modflow6 solver pair.

418

build.py

MODFLOW 6 pre_processing: flopy package wiring and grid template helpers.

877

modflow6.py

MODFLOW 6 flow solver facade aligned with HydroModPy workflow APIs.

201

modflow6_config.py

Pydantic and runtime configuration for MODFLOW 6 expert parameters.

392

prt.py

MODFLOW 6 PRT particle-tracking solver class.

563

rerun.py

rerun of MODFLOW 6: solve a model again for a new K or Sy, without a build.

381

run.py

MODFLOW 6 processing: write and run simulation.

298

transport.py

MODFLOW 6 GWT transport solver class.

363

modflow6/api/

Module

What it does

Lines

api_runner.py

Developer-facing MODFLOW 6 API runner driven by libmf6.so.

584

modflow6/builders/

Module

What it does

Lines

_lake_connection.py

LAK stage-volume-area table coercion and output-naming helpers.

74

_lake_definitions.py

Active-lake definition lookup shared by the LAK builder modules.

63

_lake_geometry.py

Cell / idomain / bed geometry helpers for the MF6 LAK builder.

501

_lake_outlets.py

Outlet, mover, spillway and per-period forcing helpers for the LAK builder.

444

_sfr_bed.py

Streambed elevation profile for one SFR network.

228

_sfr_drainage.py

DRN de-confliction and the hillslope-drainage MVR routing.

225

_sfr_network.py

Network-resolution internals behind resolve_sfr_networks.

693

_sfr_period.py

SFR PERIOD forcings and the per-reach OBS6 spec.

256

boundary_conditions.py

MF6 boundary-condition builders (CHD, DRN, side/ocean/stream support).

706

flow_barrier.py

Build the MODFLOW 6 HFB (Horizontal Flow Barrier) stress-period data.

594

initial_conditions.py

MF6 initial-condition and rewet builders.

143

lake.py

MF6 LAK package builder on a DISV grid.

967

lake_cells.py

Where a lake sits on the mesh: its cells, its carved bed and its inactive footprint.

468

lake_column_regrade.py

Re-grade one mesh column around a carved lake-bed elevation.

178

lake_obs.py

The LAK observations: the per-lake series and the flows through a cutoff wall.

196

lake_outflow.py

How water leaves a lake: its outlets, the movers they ride and the spillway seeds.

409

mvr.py

MF6 MVR (Water Mover) records, package-agnostic.

167

period_forcing.py

Shared per-period forcing helpers for MF6 advanced packages (LAK, SFR).

203

recharge.py

MF6 recharge / EVT stress-period data builders.

1 189

sfr.py

MF6 SFR (streamflow routing) package builder on a DISV grid.

882

sfr_rectify.py

Rebuild the SFR reach cells as a clean single-flow-direction channel on the mesh.

390

sidecar.py

The sidecar files a MODFLOW 6 build writes beside the model.

20

solver_options.py

MF6 solver and NPF option resolution.

430

wells.py

MF6 well stress-period data builder.

290

modflow6/extractors/

Module

What it does

Lines

_prt_tracks.py

Shared readers for MODFLOW 6 PRT track outputs.

161

budget.py

The per-cell budget of a MODFLOW 6 run, read from its CBC file into the store.

414

cbc_reader.py

Single-pass index reader for MODFLOW 6 cell-by-cell budget files.

152

face_flow.py

The flow between connected cells of a MODFLOW 6 run, read from its CBC file.

171

flow.py

Output adapter for MODFLOW 6 flow solver results.

869

grid_check.py

Check the grid MODFLOW 6 solved against the cell mesh the run stored.

217

lake.py

Parse MODFLOW 6 LAK outputs into per-lake timeseries and budget records.

742

obs_common.py

Shared MF6 OBS6-CSV parsing helpers for the advanced-package extractors.

201

prt.py

Output adapter for MODFLOW 6 PRT particle tracking results.

122

sfr.py

Parse MODFLOW 6 SFR outputs into per-reach timeseries and budget records.

528

transport.py

Output adapter for MODFLOW 6 GWT transport results.

158

modflow6/support/

Module

What it does

Lines

computational_top.py

The computational top of the MODFLOW 6 grid.

234

flopy_header_cache.py

Cache FloPy MF6 block-header transient keys to break an O(nper^2) scan.

63

flopy_structure_warmup.py

Serialize the first FloPy MF6 DFN-structure load across threads.

30

lake_band_runoff.py

Stage-dependent exposed-lakebed (marnage) runoff, injected via the MF6 BMI API.

94

names.py

The names MODFLOW 6 gives a model and its output files.

43

property_mapping.py

Hydraulic properties of a MODFLOW 6 model: K, Kv, Sy, Ss, k33 and the K tensor.

99

steady_initial_conditions.py

Same-solver steady-state initial-condition support for MODFLOW 6.

395

time_series.py

Package-agnostic MF6 TS6 (external time-series) attachment helpers.

151

windows_paths.py

Refuse, on Windows, a MODFLOW 6 workspace whose paths mf6 cannot open.

83

modflow_common/

Module

What it does

Lines

binaries.py

Solver binary resolution and verified download.

590

boundary_roles.py

What role each constant-head cell plays, written beside the deck.

133

budget_components.py

Classifier and canonical naming for MODFLOW budget records.

114

budget_policy.py

Record on the run store what a MODFLOW extractor kept of the per-cell budget.

31

calibration_extractors.py

Lightweight binary readers for calibration trials.

635

catchment_basin.py

The catchment of a run: the basin of D at the outlet, and the polygon support.

345

descent_store.py

Hand the descent D the build saved to the run store, for both backends.

352

field_slab.py

Shared bound for in-memory field-stack slabs across MODFLOW backends.

21

flow_directions.py

Build the descent D of a SolverMesh, the routing copy of its top.

221

flow_run.py

The flow lifecycle shared by the MODFLOW solvers: options, build, run, read back, final state.

269

listing.py

Reading the MODFLOW listing file: budget discrepancies, failure lines and the XT3D hint.

177

mt3dms_extractor_base.py

Shared base for MT3DMS / MF6-GWT concentration output adapters.

116

observable_extraction.py

Observables both MODFLOW backends read the same way.

488

options.py

Typed option containers shared by MODFLOW-family workflow stages.

57

period_steps.py

Put the saved time steps of a MODFLOW run on its stress periods.

259

progress.py

Shared flopy run wrappers with stress-period progress bars.

234

property_mapping.py

Hydraulic properties of a MODFLOW solver mesh (NWT and MODFLOW 6).

68

raster_export.py

Raster exports based on solver grid geometry rather than a DEM template.

44

routing_context.py

Solver-aligned hydrologic routing products built from a solver DEM.

77

runtime_arrays.py

Build shape-aware runtime payloads for MODFLOW-family solvers.

172

sink_mask.py

Closed depressions of the solver mesh top, the support sink_fill acts on.

124

time_units.py

Read back the time unit a MODFLOW run declared in its own input files.

128

modflow_grid/

Module

What it does

Lines

active_connectivity.py

Count the groups of active cells that no flow path joins.

110

discretization_spatial.py

Common spatial discretization builders for MODFLOW-family solvers.

416

discretization_temporal.py

Common temporal discretization builders for MODFLOW-family solvers.

129

sgrid_config.py

Pydantic configuration of the solver top sampling, the sgrid table of a MODFLOW backend.

64

solver_mesh.py

Unified solver mesh abstraction for structured and unstructured grids.

451

modflow_nwt/: MODFLOW-NWT through FloPy, on a structured grid, with MODPATH and MT3DMS for transport. It is a backend of the solver port of solver/base, kept until the lakes run on MODFLOW 6, so its payload builders are not shared with MODFLOW 6. It reads base, modflow_common and modflow_grid, never modflow6 or boussinesq.

Module

What it does

Lines

adapter.py

Adapter for the flow/modflow_nwt solver pair.

268

modflow_nwt/extractors/

Module

What it does

Lines

flow.py

Output adapter for MODFLOW-NWT flow solver results.

854

modpath.py

Output adapter for MODPATH particle tracking results.

182

mt3dms.py

MT3DMS concentration output adapter for the NWT toolchain.

11

modflow_nwt/modpath/

Module

What it does

Lines

_pre_processing.py

Pre-processing helpers for the MODPATH particle-tracking driver.

318

modpath.py

MODPATH particle tracking on a MODFLOW-NWT flow model.

574

resolvers.py

Resolvers of the delineation members and the zone-partic aliases for the MODPATH backend.

167

modflow_nwt/mt3dms/

Module

What it does

Lines

masstransfer.py

Accumulation of a flow or a mass downslope on the model grid (Masstransfer).

190

mt3dms.py

The MT3DMS transport model on a MODFLOW-NWT flow, and its transport adapter.

663

modflow_nwt/nwt/

Module

What it does

Lines

_pre_processing.py

FLOPY package assembly from solver-ready FlowModflowInputs.

263

flow_to_modflow_adapter.py

Model -> MODFLOW-NWT adaptation layer.

352

nwt_config.py

Pydantic and runtime configuration for MODFLOW-NWT expert parameters.

342

nwt_solver.py

The MODFLOW-NWT flow model: pre-processing and processing.

545

steady_initial_conditions.py

Same-solver steady-state initial-condition support for MODFLOW-NWT.

97

modflow_nwt/nwt/payloads/

Module

What it does

Lines

chd.py

CHD-related payload builders for the NWT flow-to-modflow adapter.

422

recharge_etp.py

RCH/EVT payload builders for the NWT flow-to-modflow adapter.

496

well_drainage.py

WEL/DRN payload builders for the NWT flow-to-modflow adapter.

207

Inside the package#

base/ holds what every backend shares, modflow_grid/ and modflow_common/ hold what MODFLOW 6 and MODFLOW-NWT do the same way, and each backend has its own folder. A module that only one MODFLOW backend reads lives in that backend. The backends are described in MODFLOW and Boussinesq backends.

Import rules#

tests/unit/architecture/layer_matrix.yaml gives solver its layers, and tests/unit/architecture/solver_layout.yaml orders the parts inside it.

  1. Backends do not import each other. modflow6, modflow_nwt and boussinesq each import base, and the two MODFLOW backends also import modflow_common and modflow_grid. test_a_backend_never_imports_another_backend in tests/unit/architecture/test_package_layouts.py checks it.

  2. base/registry.py names every built-in adapter and extractor by a dotted path (_BUILTIN_PATHS, _BUILTIN_EXTRACTOR_PATHS) and imports it on first lookup. Loading hydromodpy.solver.base.plan therefore loads no backend.

  3. A layer above solver calls solvers.get, solvers.create and solvers.names of the registry. calibration reaches the solvers through solver.base only, so a backend can change without touching it (see Package: calibration).

  4. A private name read inside a backend package is declared in that package, and tests/unit/architecture/test_layers.py checks it.

How a run reaches a backend#

  1. Choose. solver.backend is the name of a registered flow backend (modflow6, modflow_nwt, boussinesq, linear_reservoir or a plugin). A name the registry does not serve is refused while the file is read, with the names it serves. The settings of a backend are the table [solver.<name>], checked by the options class on its adapter. A file that uses something the backend leaves out of supports is refused by config/cross_checks.py.

  2. Plan. The planner (base/planner.py) reads the requires attribute of the backend class to bind a transport run to an earlier flow run.

  3. Look up. The runner calls solvers.create(process_type, solver). The registry imports the built-in class, or a plugin, and returns an instance. slot_for fills the RunSlot and options_for reads the table of the backend.

  4. Run. solve_one calls adapter.validate, then adapter.build, which makes the backend model in memory, then adapter.run, which writes its files in the run folder, solves and returns a SolverRun. A calibration trial that gives a solved run of the same model asks adapter.rerun first, and the adapter may decline.

  5. Extract. The workflow step run_outputs.py calls adapter.extract, which writes the fields and tables of the run into the results store through the extractor registered under the same key. Then adapter.cleanup removes the scratch files when the run keeps no solver files.

  6. Observe. adapter.observables(solver_run, requests) serves a calibration trial from the run folder. An adapter that cannot produce a requested observable raises ObservableNotAvailableError.

What a backend can do is declared on its adapter class, in the thirteen attributes of base/README.md. writes lists what it stores, supports the traits of the Model it honours and category how it resolves space.

Where to add what#

  • A solver in the tree is a package with its adapter and its extractor, one entry in each of _BUILTIN_PATHS and _BUILTIN_EXTRACTOR_PATHS, and an options class for [solver.<name>]. The steps are in Add a solver.

  • A solver out of the tree is an adapter class on the hydromodpy.solver entry-point group and an extractor on hydromodpy.solver.extractor. The entry-point name is <process_type>_<name>, and the registry compares it with the joined attributes of the loaded class, so a name that holds an underscore stays whole.

  • A MODFLOW 6 package is a module in modflow6/builders/ with its build step, called from modflow6/build.py in the build order. A builder reads another one only along that order.

  • Something both MODFLOW backends do goes in modflow_common/, or in modflow_grid/ when it is about the grid.

  • A process type is described in Add a process. A callback around a run is a field of ProcessCallbacks and its call in SimulationRunner.

  • A boundary kind is added in boundary_condition_registry.py of model/boundaries, then as a package in each backend that lists it in supports.

  • A backend option is a field on modflow6_config.py, nwt/nwt_config.py or boussinesq/options.py, with a profile and a description.

  • An observable has its reader in the extractor of the backend or in modflow_common/observable_extraction.py, and its state in base/observables.py.