The mesh pivot and the cell mesh#
HydroMesh is the exchange format of a mesh. PlanarCellMesh wraps a planar one, and it is
the mesh that every solver, every workflow step and every field support reads. The code is in
hydromodpy/mesh/planar/ and its package page is Package: mesh. The engines that
build it are on Mesh engines, and the grids of the solvers are on MODFLOW and Boussinesq backends.
The pivot#
HydroMesh (mesh/planar/hydro_mesh.py) is a frozen dataclass. It describes any 2D or 3D
mesh the same way, in the data model of meshio.
@dataclass(frozen=True)
class CellBlock:
"""One homogeneous block of cells sharing the same geometry type.
A mesh may contain several blocks (e.g. triangles + quads in a hybrid
mesh, or wedges across multiple physical groups). Most HydroModPy meshes
carry a single block.
``connectivity`` is one ``(n_cells, k)`` int table, one row of node indices
per cell. A ``POLYGON`` block has as many columns as its largest cell: the
nodes of a cell fill its row from the left and the free slots hold ``-1``.
One node array per cell is accepted for a polygon block and padded so.
"""
cell_type: CellType
connectivity: np.ndarray
CellType has TRIANGLE, QUADRILATERAL, POLYGON, WEDGE and HEXAHEDRON. A
POLYGON block is the one case of a block with cells of unequal size, so it pads its rows with
-1. flat_connectivity stacks the blocks into one table padded the same way. A
HydroMesh with a structured_shape is a regular grid, and only such a mesh gives the DIS
grid of MODFLOW-NWT (SolverMesh.to_dis_kwargs()). The class is frozen, so with_cell_data
and with_point_data return a new instance.
The disk formats live in mesh/io/, one module per format, and depend on the mesh model only.
VTU (
io/vtu.py) is the recommended format. It describes itself, opens in ParaView, PyVista and QGIS, and round-trips throughHydroMesh.write_vtualso writes whole-mesh values, such as the CRS of a run, in theFieldDataof the grid, andread_vtu_field_datareads them back.hmp exportwrites its VTU files through this module, with the cells in the order of the run store.io/meshio_bridge.pypasses aHydroMeshthrough meshio. A polygon block goes out as runs of consecutive cells of one node count, so the cell order is kept, and comes back as one block.io/msh.pyreads a Gmsh.mshfile.read_gmsh_2d_meshuses meshio, or a small reader of the ASCII 2.2 format when meshio fails, and returns the lines of the named physical groups. A.mshfile carries no CRS, sowrite_gmsh_2d_meshwrites the WKT of a givencrsto<name>.prjnext to the mesh, and the reader reads it back.io/bundle.pyreads a Boussinesq validation bundle, a folder of CSV files. The version isBUNDLE_SCHEMA_VERSIONandBUNDLE_COLUMNSgives the columns of each file.
xugrid handles 2D and layered pseudo-3D meshes but not fully 3D unstructured ones, so it cannot be
the pivot. meshio is the I/O dependency, but its meshio.Mesh is mutable and supports about
thirty cell types, most of them useless here. HydroMesh is a strict, frozen subset of that
model.
The cell mesh#
PlanarCellMesh (mesh/planar/planar_cell_mesh.py) is the mesh that the solvers and the
workflow steps read, as setup.cell_mesh. It wraps one planar HydroMesh block of
triangles, quadrilaterals or polygons, unchanged, and it holds what every consumer would
otherwise recompute.
@dataclass(frozen=True, eq=False)
class PlanarCellMesh:
"""A planar mesh of cells with its centers, CRS, families, identity and QA.
``centers`` is the only cell center rule for every consumer: the Voronoi
generator, the regular cell center, the triangle vertex mean or a point
inside a catchment cell, as ``center_kind`` says. Boundary sides are face families named
``boundary:<label>`` with ``cell_b = -1``. ``smoothed_catchment`` is the
outline of the catchment the planner smoothed before meshing, in the CRS of
the mesh, None when the mesh follows the delineated polygon as it is: a
cell is read against that outline, which its faces follow.
"""
mesh: HydroMesh
centers: np.ndarray
center_kind: CenterKind
crs_wkt: str
identity: MeshIdentity
qa: MeshQA
cell_chains: tuple[CellChain, ...] = ()
face_families: FaceFamilyTable | None = None
cell_zone: np.ndarray | None = None
zone_names: tuple[str, ...] = ()
structured_shape: tuple[int, int] | None = None
smoothed_catchment: Polygon | None = None
The cell centers follow one rule for every consumer. The center_kind says which, and it is
generator for Voronoi, cell_center for a regular grid, vertex_mean for triangles and
interior_point for the one cell of the catchment engine. A HydroMesh carries no
centers, so a consumer reads cell_mesh.centers.
cell_mesh.geometry is a DerivedGeometry (mesh/planar/geometry.py), derived once on
first use. It holds the -1 padded connectivity (the format of the Zarr store), the areas, the
area centroids, the edge table and the CSR adjacency of the interior edges. An edge shared by
more than two cells raises ValueError. cell_mesh.connectivity_rows returns the cells as
one array, which is the block itself for triangles and quadrilaterals and the padded rows as
32-bit ids for polygons.
MeshQA (mesh/planar/quality.py) holds the quantiles of area, size, center offset,
non-orthogonality and cell shape, the concave cells, the degenerate faces, the family coverage and
the engine declarations. quality_warnings turns the shape entries into build warnings and
never refuses a mesh.
Line families#
A family is a set of lines that the mesh honors, under one name. Its intent decides what the cell mesh keeps.
A cell family (
rivers) runs through the cells.cell_chainsholds one chain of cells per line, from upstream to downstream.CellChain.offset_mgives the distance from each cell center to the line. Thevoronoiengine fills it, and it isNonewhere the cell holds the line.A face family (
geology,lakes,barriers,watershed_boundary) lies on cell faces.face_familieshas one row per mesh edge on a line, with the two cells (-1outside the domain), the two vertices and the zone on each side. An edge on two families, such as a cutoff wall along a lake shore, has a row in each. The four sides of the domain are face families namedboundary:<side>.
The cell_families capability of an engine says how it forms a cell family, and the intent of
each family comes from [mesh.families]. Every engine gives a river as cell chains, and a
triangle mesh also keeps the faces of the river line as face rows.
Consumers read the cell mesh#
Every consumer reads the cell mesh, and none reads an engine-specific support. One function serves every engine for each question.
Question |
Where |
|---|---|
Centers, areas, adjacency |
|
Cells of a side, of the river, of a label |
|
Cell that holds a point |
|
Forcing grids and stations on the cells |
|
Zone fractions of a cell |
|
K, Sy and Ss on the cells |
|
HFB, lake and SFR cells |
|
The share of a cell inside a polygon |
|
The vertical layers, the grids and stations put on the cells, and the field supports live outside
mesh/, in model/domain/, model/forcing/ and model/properties/. They read the mesh
model and the mesh never imports them. The field package is on Package: model.
What the solvers read#
A solver reads the SolverMesh of solver/modflow_grid/solver_mesh.py. It is built from the
cell mesh and the vertical layers of [domain.layers], which both MODFLOW backends read.
layer_stack in model/domain/layering.py turns shares or surfaces into the layer bottoms
and the integer idomain (1, 0 or -1). discretization_spatial.py, next to
solver_mesh.py, samples the base rasters on the cell centers or resamples them to a regular
grid. SolverMesh checks the interfaces once it is built, and the run stores them with the
idomain (Storage layout). active_connectivity.py warns when some groups of
active cells have no connection to the rest. The MODFLOW 6 build and the MODFLOW-NWT solver call
it on the final grid, after the lake mask, and it counts a pass-through cell and a lake as
connections.
MODFLOW 6 reads a DISV grid for every engine. A regular grid is also written as DISV, through
SolverMesh.to_disv_kwargs(), which calls to_flopy_disv_args. MODFLOW-NWT reads a DIS grid,
which only the regular engine gives. Boussinesq reads the triangles of the cell mesh, and a
validation case gives a bundle that cell_mesh_from_bundle turns into a cell mesh. Each flow
adapter declares the engines it reads in accepted_meshes, as Mesh engines says.