Package: mesh#
This package turns a project into the cells that MODFLOW 6 and the other solvers compute on. It reads the catchment, the rivers, the lakes and the geology, returns one PlanarCellMesh that the rest of HydroModPy reads, and writes and reads the mesh in the usual exchange formats, so a mesh opens in ParaView or gmsh. It reads core, schema and catchment, and it also carries the mesh port, because its registry sits at the root of the package.
The mesh port#
An engine receives a MeshRequest (request.py), plain frozen data that holds the domain, the DEM grid, the cell size, the line families, the size field, a triangulation read from a file and, for a lumped engine only, the catchment outline. It reads no file and no config. build(request) returns a PlanarCellMesh (planar/planar_cell_mesh.py), which names its engine, its version and the hash of the request.
An engine declares engine_id, engine_version, capabilities and build. MeshEngineCapabilities (port.py) states what every mesh of the engine is: its cell types, structured, graded, exact faces, how a river becomes a chain, deterministic, cached, lumped, and the line families it ignores. Raise engine_version whenever the mesh of one request changes.
Four engines ship as dotted paths in _BUILTIN_PATHS of registry.py: regular (rectangular cells on the DEM grid, square when the extent is, the default), triangle (gmsh triangles that follow the lines), voronoi (polygons around generators) and catchment (the catchment as one polygon cell, holes filled, for a global model). A third party publishes an entry point named like its engine_id in the hydromodpy.mesh.engine group, and [mesh] engine = "<id>" selects it.
[mesh] engine is read by check_registered_name, which refuses a name nothing serves with the names served and the group. A request an engine cannot honour raises MeshRequestError, which names what it refuses. A family an engine ignores, written with a value other than none, is refused when the file is read. A solver that accepts *planar reads any planar engine that is not lumped, and engine_accepted of registry.py is that rule.
tests/ports/test_mesh.py runs on every engine the registry resolves, an installed one too, and holds the invariants a solver relies on.
Where the rest is told#
The page of the package in the developer docs gives the steps of a mesh, the two mesh objects and the rules between the folders. The developer pages of docs/source/contribute/architecture/engines/ cover the engines, and docs/source/theory/mesh.md explains the choice of an engine and of the cell size.
Modules#
cells/
Module |
What it does |
Lines |
|---|---|---|
barrier_faces.py |
Map a vector barrier line onto the faces of a cell mesh. |
214 |
boundary.py |
Cells of the outer sides, of the river and of a labeled family of a cell mesh. |
159 |
crossed_by_lines.py |
The cells a mapped linework crosses, on the face graph of any mesh. |
227 |
in_polygons.py |
Project a vector layer onto mesh cells (mesh-agnostic). |
192 |
locate.py |
Locate the cell that holds each point of a planar cell mesh. |
113 |
regions.py |
Regions of the cells of a mesh once the faces of a family are cut. |
66 |
zonal_stats.py |
Zonal reduction of a raster onto irregular mesh cells (mesh-agnostic). |
261 |
engines/catchment/
Module |
What it does |
Lines |
|---|---|---|
engine.py |
The |
119 |
engines/
Module |
What it does |
Lines |
|---|---|---|
face_families.py |
Face family rows the mesh engines share: oriented edges, the table, the zones. |
211 |
engines/regular/
Module |
What it does |
Lines |
|---|---|---|
engine.py |
The |
104 |
families.py |
Line families on the regular grid: cell chains, staircase faces and sides. |
345 |
grid.py |
Shape, pitch and vertices of the regular grid, bit for bit the flopy ones. |
107 |
engines/triangle/
Module |
What it does |
Lines |
|---|---|---|
engine.py |
The |
199 |
families.py |
Line families of a triangulation: oriented face rows, chains and zones. |
185 |
gmsh_triangulation.py |
Constrained Delaunay triangulation of a mesh request by gmsh. |
333 |
msh_groups.py |
Line families of a read triangulation, from the physical line groups of its |
198 |
engines/voronoi/
Module |
What it does |
Lines |
|---|---|---|
checks.py |
Blocking gates of the Voronoi generator. |
382 |
engine.py |
The |
105 |
families.py |
Families of a generated Voronoi mesh: river chains, family faces, sides, zones. |
259 |
free_seeds.py |
The free seeds of the Voronoi generator: count, draw and canonical order. |
190 |
from_seeds.py |
The generated Voronoi mesh: constraint seeds, free seeds, Lloyd, tessellation, gates. |
192 |
from_triangulation.py |
The Voronoi dual of a triangulation read from a file: its vertices are the generators. |
336 |
lloyd.py |
Weighted Lloyd relaxation of the free seeds, the constraint seeds held fixed. |
206 |
local_coords.py |
The local frame of the Voronoi generator and the size field sampled in it. |
120 |
tessellation.py |
Voronoi cells of the seeds, clipped to the domain, and their shared vertices. |
336 |
engines/voronoi/constraints/
Module |
What it does |
Lines |
|---|---|---|
boundary_seeds.py |
The seeds along each domain ring, between its corners and fixed positions. |
246 |
conflicts.py |
The conflict rules, applied until no constraint seed breaks one. |
369 |
face_pairs.py |
Mirror pairs of Berge (2019): the seed geometry that puts a mesh face on a line. |
493 |
lines.py |
The request lines in the local frame, the families in priority order. |
198 |
plan.py |
Constraint seeds of the Voronoi generator: river chains, mirror pairs, boundary. |
365 |
rivers.py |
The river chains: one seed per node of a cell line, and the crossings of the face lines. |
298 |
state.py |
The working state of the constraint stage: seeds, river chains, crossings, face graph. |
270 |
io/
Module |
What it does |
Lines |
|---|---|---|
bundle.py |
Read self-contained catchment mesh exchange bundles, and their cell mesh. |
721 |
meshio_bridge.py |
Round-trip conversion between |
190 |
msh.py |
Read, normalize and write simple 2D Gmsh meshes. |
526 |
vtu.py |
VTU read/write for |
107 |
top level
Module |
What it does |
Lines |
|---|---|---|
mesh_config.py |
The |
439 |
port.py |
The mesh port: what a mesh engine owes its callers. |
157 |
refinement_config.py |
The two tables of local mesh refinement, |
318 |
registry.py |
|
96 |
request.py |
The mesh request: everything a mesh engine reads, as plain frozen data. |
483 |
resample.py |
Resample the family lines at the local cell size, before any engine. |
286 |
stored.py |
The codec of the stored mesh product: |
219 |
planar/
Module |
What it does |
Lines |
|---|---|---|
cell_types.py |
Canonical cell-type enumeration for the unified mesh pivot. |
91 |
geometry.py |
Geometry derived once from a planar cell mesh: areas, edges, adjacency. |
403 |
hydro_mesh.py |
Unified mesh data container for HydroModPy. |
320 |
planar_cell_mesh.py |
The planar cell mesh: the one object a mesh engine returns and a consumer reads. |
418 |
quality.py |
Quality report of a planar cell mesh, computed by one routine for every engine. |
622 |
regular_grid.py |
A DEM-aligned regular grid, and its planar cell mesh built without a config. |
102 |
planning/
Module |
What it does |
Lines |
|---|---|---|
barrier_lines.py |
Barrier lines that run along a lake ring share its vertices, bit for bit. |
213 |
catchment_outline.py |
Smooth the staircase of the catchment outline before the planner reads it. |
346 |
families.py |
The line families of a mesh request, as |
246 |
gap_size.py |
The size terms that follow the gap between the face lines and the domain outline. |
245 |
geology_contacts.py |
Contacts between the zones of a vector geology map, as oriented lines. |
217 |
geometry_cleanup.py |
Clean the geometry of the project inputs before the planner sizes it. |
437 |
height_above_stream.py |
Height of each DEM pixel above the stream it drains to, on the DEM grid. |
155 |
input_triangulation.py |
Read a triangulation from a gmsh |
172 |
inputs.py |
Everything the planner reads besides |
62 |
lake_shores.py |
The lake shores a generated Voronoi mesh can follow, at its local cell size. |
287 |
lake_structures.py |
The size rules of the hydraulic structures of the lakes the mesh refines. |
137 |
plan.py |
Turn |
335 |
refinement_zones.py |
Read the |
261 |
size_field.py |
The target cell size |
192 |
size_terms.py |
The size terms of a graded mesh request. |
181 |
Inside the package#
A mesh is made in four steps, and each step has its own place in the package. The
planner of planning/ reads the [mesh] table and the project inputs. It cleans the
input lines, smooths the catchment outline, computes the target cell size at every point,
and returns a MeshRequest. The registry then finds the engine that [mesh] engine
names. The engine of engines/<name>/ builds the cells from the request, and it returns a
PlanarCellMesh of planar/. The solvers, the forcing, the property supports and the
exports all read this one object. Every engine declares in
MeshEngineCapabilities.ignored_families the line families its mesh cannot follow. The
catchment engine ignores them all, so the planner drops such a family with a named note
instead of failing on the default rivers = "cell".
The workflow step hydromodpy/workflow/steps/mesh.py runs these steps. It is the only
place that reads [mesh] and asks the product store of stored.py. Every mesh is stored
under <project>/.hmp/products/, and a later run reads the same cells back.
needs_cache only marks an engine that cannot rebuild the same cells bit for bit.
There are two mesh objects. PlanarCellMesh is the mesh of a run. It holds the cells
with their centres and CRS, the line families (river chains, lake shores, barriers, sides),
the zones, the engine identity and the quality report, and it computes areas, edges and
neighbours once, on first use. HydroMesh is the plain geometry inside it, vertices and
blocks of cells after the meshio data model. Plots and exchange files use it, it accepts 3D
cells, and a block of polygons pads the rows of its smaller cells with -1.
The imports go one way, from the request and the planar mesh up to the engines.
request.py, mesh_config.py and refinement_config.py import nothing else from
the package, and planar/ imports only itself. io/ and cells/ read planar/,
and cells/ also reads request.py. planning/ reads the two config modules,
request.py, resample.py and planar/, and it reads a triangulation file through
io/msh.py. The engines read request.py, resample.py, planar/ and cells/,
and never planning/ or the workflow. Nothing in the package imports the workflow or
hydromodpy/model/properties, because both read the mesh. gmsh is imported in one place,
engines/triangle/gmsh_triangulation.py, so a regular or a Voronoi mesh never loads it.
tests/unit/architecture/mesh_layout.yaml holds this order. No __init__.py
re-exports a name, so a search for a name finds its file.
What only a solver reads lives with that solver. The sgrid table of the MODFLOW
backends, the vertical part of a regular grid and the MODFLOW 6 DISV arguments are in
hydromodpy/solver/modflow_grid/. The cells a lake owns, the faces of its cutoff wall and
the share of a reach among the cells are in hydromodpy/solver/modflow6/builders/. The
package hydromodpy/model/properties samples its property supports on the cells of a
PlanarCellMesh.
The tests follow the layout of the code. A test file carries the name of the module it
tests and sits in the same folder under tests/unit/mesh/, so planning/size_field.py
is tested in tests/unit/mesh/planning/test_size_field.py. A few small modules are tested
through the module that calls them. A test builds its grid, request and meshes with the
helpers of tests/_helpers/mesh_cases.py. The port suite tests/ports/test_mesh.py
holds what every engine must do, and the mesh regression of tests/regression/mesh/
compares the meshes of fixed cases to committed references, so a change there must be
wanted. With HMP_REQUIRE_GMSH=1, a missing gmsh fails the triangle tests instead of
skipping them. A new [mesh] key is a field of the Pydantic model of mesh_config.py
with its description, and the parameter reference is built from it.
The developer notes Mesh engines and The mesh pivot and the cell mesh cover the mesh objects, the port, the engines and the product store.