Voronoi mesh generation for MODFLOW 6 using Gmsh and Geopandas.
vorflow is a Python package for creating 2D unstructured Voronoi cell meshes for groundwater modeling, particularly for MODFLOW 6. It leverages the power of Gmsh for robust triangular meshing and Shapely/Geopandas for geometric operations.
The process is designed to translate a conceptual model—defined by geometric features like polygons, lines, and points—into a high-quality Voronoi grid suitable for numerical simulation.
The library is built around three main classes that work in sequence:
-
ConceptualMesh: A blueprinting tool to define the model domain and its features. You can add polygons (e.g., model boundary, refinement zones), lines (rivers, faults), and points (wells) and specify the desired mesh density and refinement behavior for each. -
MeshGenerator: This is the engine that generates a triangular mesh based on the blueprint fromConceptualMesh. It usesGmshas its backend to create a quality-conforming Delaunay triangulation. -
VoronoiTessellator: This class takes the triangular mesh fromMeshGeneratorand computes its dual: the Voronoi diagram. The result is a grid of polygonal cells. It includes logic to clip the grid to the domain boundary and enforce barrier features by cutting through cells.
The typical workflow follows these steps:
- Define Geometry: Create
shapelyobjects for your model features (domain boundary, rivers, wells, etc.). - Create a Blueprint: Instantiate
ConceptualMeshand add your geometries, specifying parameters like mesh resolution, refinement distances, and feature types (e.g., barriers). - Generate Mesh: Instantiate
MeshGeneratorand call itsgenerate()method with the processed geometries from the blueprint. This produces a triangular mesh. - Tessellate to Voronoi: Instantiate
VoronoiTessellatorwith the generated mesh and the blueprint. Calling itsgenerate()method produces the finalGeoDataFrameof Voronoi cells. - Export: The resulting
GeoDataFramecan be easily saved to a shapefile or other formats.
The package dependencies are listed in pyproject.toml. You can install them using pip:
pip install numpy pandas geopandas shapely scipy gmsh matplotlibTo install vorflow itself, you can install it in editable mode from the root of the repository:
pip install -e .Here is a simple example of how to generate a grid:
from vorflow import ConceptualMesh, MeshGenerator, VoronoiTessellator
from shapely.geometry import box, Point, LineString
# 1. Define conceptual model features
domain = box(0, 0, 200, 200)
well_point = Point(25, 25)
fault_line = LineString([(100, 0), (100, 150)])
# 2. Create a blueprint
blueprint = ConceptualMesh(crs="EPSG:3857")
blueprint.add_polygon(domain, zone_id=1)
blueprint.add_point(well_point, point_id="Well-A", resolution=2, dist_max=300)
blueprint.add_line(fault_line, line_id="Fault-1", resolution=1, is_barrier=True)
clean_polys, clean_lines, clean_pts = blueprint.generate()
# 3. Generate the triangular mesh
mesher = MeshGenerator(background_lc=100)
mesher.generate(clean_polys, clean_lines, clean_pts)
# 4. Convert to Voronoi grid
tessellator = VoronoiTessellator(mesher, blueprint, clip_to_boundary=True)
grid_gdf = tessellator.generate()
# 5. Save the output
grid_gdf.to_file("mf6_grid.shp")
print("Grid generation complete.")