from refined_quads import write_ugrid
write_ugrid(quad_mesh, workdir / "mesh_net.nc")Use the mesh in D-HYDRO
Write a net file
The file is UGRID-1.0 with the structure of the D-HYDRO mesh generator export: the Mesh2d topology with nodes, edges and faces, edge and face coordinates and face bounds. For EPSG:28992 the CRS variable has exactly the attributes of the D-Flow FM plugin; for another projected CRS they are derived with pyproj; a mesh without CRS gets no CRS variable.
variables: projected_coordinate_system, Mesh2d, Mesh2d_node_x, Mesh2d_node_y, Mesh2d_face_nodes, Mesh2d_edge_nodes, Mesh2d_edge_x, Mesh2d_edge_y, Mesh2d_face_x, Mesh2d_face_y, Mesh2d_face_x_bnd, Mesh2d_face_y_bnd
CRS: Amersfoort / RD New (EPSG 28992)
Bed levels
The net file contains the mesh geometry. Use D-HyDAMO to add bed levels from a raster.
With D-HyDAMO
from pathlib import Path
import refined_quads
from hydrolib.dhydamo.geometry import mesh
quad_mesh = refined_quads.generate(
domain,
cell_size=32.0,
refinement_areas=refinement_areas,
transition_rows=1,
water_areas=water_areas,
crs="EPSG:28992",
)
net_file = Path(output_path) / "refined_quads_net.nc"
refined_quads.write_ugrid(quad_mesh, net_file)
network = fm.geometry.netfile.network
mesh.mesh2d_from_netcdf(network, net_file)
mesh.mesh2d_altitude_from_raster(network, rasterpath, "face", "mean", fill_value=-999)
fm.geometry.bedlevtype = 1Use quad_mesh as the variable name so that mesh still refers to the D-HyDAMO module. Complete any clipping or refinement before assigning bed levels.
Bed levels on nodes
To assign bed levels to nodes instead of cells, use "node" in the same function and choose a matching bedLevType (3 to 6), for example:
mesh.mesh2d_altitude_from_raster(network, rasterpath, "node", "mean", fill_value=-999)
fm.geometry.bedlevtype = 3Which bedLevType
In the net file a “face” is a cell. In the MDU, “at face” means at a cell side (a flow link). The table uses cell, cell side and node.
bedLevType |
Input | Level of a cell side | Bed level of the cell |
|---|---|---|---|
| 1 | per cell: mesh2d_face_z |
the higher of the two cells | its own value |
| 2 | per cell side: samples | the sample | |
| 3 | per node: mesh2d_node_z |
mean of its two nodes | lowest of its cell sides |
| 4 | per node | lower of its two nodes | lowest of its cell sides, so its lowest node |
| 5 | per node | higher of its two nodes | lowest of its cell sides |
| 6 | per node | mean of its nodes |
Only cell sides shared with another cell count.
Read a net file
read_ugrid reads only the 2D mesh of a net file; a 1D network and other variables in it are ignored. The lattice is derived from the square cells, so you can check or refine a mesh from any program:
from refined_quads import check, read_ugrid
quad_mesh = read_ugrid(workdir / "mesh_net.nc")
print(check(quad_mesh).summary())lattice: origin (154976, 462976), base cell 32, 1 levels, unit 16
nodes: 183, cells: 187
cells per level: 0 (32): 115, 1 (16): 72
nodes off the lattice: 0
duplicate nodes (information): 0
split edges: 0
hanging nodes: 0
2:1 balance violations: 0
non-orthogonal edges (> 0.30): 0 (worst 0.000)
clockwise cells: 0
unrecognised cells: 0
holes MeshKernel closes (information): 0
valid: yes
If the orthogonality repair does not close
D-Flow FM rejects a network whose edges are not orthogonal enough. The generator repairs such edges; if that does not close, generate raises OrthogonalityRepairError with the raw mesh (exc.mesh) and the problem locations (exc.bad_points), and the command line writes <output>_raw.nc and <output>_problems.gpkg next to the output so you can look at them in QGIS.
Practical restrictions
- Assign bed levels after all changes to the mesh. With
"face", the values belong to the current cells in their current order. Further clipping or rebuilding can change those cells; assign the bed levels again if that happens. An extra call to the private_set_mesh2dmethod is not part of the standard workflow above. - MeshKernel derives cells from nodes and edges. Small holes can become cells when the mesh is read again. Refined-quads keeps cells where removing them would leave such a hole; see Water areas. Additional clipping in D-HyDAMO can introduce new holes.
fill_value=-999fills locations without raster data with that value. Check raster coverage and choose a fill value or fill option appropriate for your model.- Some HYDROLIB/pandas combinations can raise a read-only-array error when filling missing raster values. This is a dependency compatibility issue; check the versions in your environment if it occurs.
- For raster import in the D-HYDRO GUI, use
.tifor.asc.