Skip to main content

Your first mesh

This tutorial generates a small refined quad mesh from shapely geometries, looks at it, checks it and writes it as a UGRID net file.

The input

A mesh needs a domain, the area it covers, and a base cell, the size of its coarsest squares. Refinement areas say where the mesh must be finer: each is a polygon paired with a refinement level, the number of times the base cell is halved there. Coordinates are in RD New (EPSG:28992).

from shapely import Point, box

domain = box(155_000, 463_000, 155_640, 463_480)
refinement_areas = [
    (Point(155_250, 463_240).buffer(60), 2),  # 32 m / 2**2 = 8 m cells
    (box(155_450, 463_080, 155_600, 463_160), 1),  # 16 m cells
]

Generate

from refined_quads import generate

quad_mesh = generate(
    domain,
    cell_size=32.0,
    refinement_areas=refinement_areas,
    transition_rows=[1, 1],
    crs="EPSG:28992",
)
print(quad_mesh)
Mesh(nodes=844, faces=916, cell_size=32, levels=2, crs=EPSG:28992)

transition_rows gives, per step from coarse to fine, how many rows of finer cells surround a finer area before the mesh coarsens. Here one row for the step from 32 m to 16 m and one for 16 m to 8 m.

The mesh, coloured by refinement level, with the refinement areas in red:

2026-11-26T12:18:52.119405 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Where levels meet, a lattice square is split into transition cells: triangles that connect the finer cells on one side to the coarser square, without hanging nodes.

Check

check reports everything D-Flow FM and refining rely on:

from refined_quads import check

report = check(quad_mesh)
print(report.summary())
lattice: origin (154976, 462976), base cell 32, 2 levels, unit 8
nodes: 844, cells: 916
cells per level: 0 (32): 356, 1 (16): 256, 2 (8): 304
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

Write it for D-HYDRO

from refined_quads import read_ugrid, write_ugrid

write_ugrid(quad_mesh, workdir / "mesh_net.nc")
print(read_ugrid(workdir / "mesh_net.nc"))
Mesh(nodes=844, faces=916, cell_size=32, levels=2, crs=EPSG:28992)

The file has the structure of the D-HYDRO mesh export, with the RD New attributes the D-Flow FM plugin writes; see Use the mesh in D-HYDRO.

Next