Skip to main content

Refine an existing mesh

refine makes an existing mesh finer in new refinement areas. It needs only the mesh, which may come from another program. Cells outside the affected area keep their nodes and coordinates exactly.

In Python

from refined_quads import check, read_ugrid, refine

quad_mesh = read_ugrid(workdir / "model_net.nc")
new_area = Point(155_340, 463_170).buffer(45)

result = refine(quad_mesh, [(new_area, 2)], transition_rows=1, crs="EPSG:28992")

print(result.mesh)
print("valid:", check(result.mesh).is_valid)
Mesh(nodes=535, faces=604, cell_size=32, levels=2, crs=EPSG:28992)
valid: True

Before and after; the new or changed cells are highlighted:

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

transition_rows works as for generate: one value per level step, or one value for all. A level finer than the mesh has adds levels to its lattice.

Carry cell data over: the replacement map

result.replacement_map relates the new cells to the old ones:

  • source[i]: the old cell that new cell i lies in (for an unchanged cell, itself);
  • unchanged[i]: whether new cell i is an old cell with the same nodes;
  • removed: the old cells that are not in the new mesh.

Per-cell values of the old mesh, such as an initial water level, carry over in one line:

old_values = np.linspace(10.0, 12.0, len(quad_mesh.faces))  # one value per old cell
rm = result.replacement_map

new_values = old_values[rm.source]

print(
    f"{rm.unchanged.sum()} cells unchanged, {(~rm.unchanged).sum()} new, "
    f"{len(rm.removed)} old cells replaced"
)
239 cells unchanged, 365 new, 44 old cells replaced

On the command line

Put the new areas in a TOML file:

transition_rows = 1

[[refinement_areas]]
path = "extra.gpkg"
layer = "verfijning_nieuw"
level = 2
refined-quads refine model_net.nc extra.toml -o model_v2_net.nc

The options and exit codes are those of generate. A mesh that cannot be recognised exits with 2 and writes model_v2_net_recognition.gpkg with the problem locations.

Domain and water areas

You may pass a domain and water areas to refine, or put them in the TOML file. They only remove new cells, and the domain’s holes are clipped like water. New cells only replace existing ones, inside the footprint of the mesh, so the mesh never grows. See Why refine starts from the mesh.