Mesh Generation#

The mesh generation modules provide tools for creating finite element meshes from boundary polygons and constraint features.

Generators Module#

The generators module contains the abstract base class for mesh generators.

Mesh generator base classes for IWFM models.

This module provides the abstract base class for mesh generators and the MeshResult class for holding generated meshes.

class pyiwfm.mesh_generation.generators.MeshResult(nodes, elements, node_markers=None, element_markers=None)[source]#

Bases: object

Result from mesh generation.

Variables:
nodes: ndarray[tuple[Any, ...], dtype[float64]]#
elements: ndarray[tuple[Any, ...], dtype[int32]]#
node_markers: ndarray[tuple[Any, ...], dtype[int32]] | None = None#
element_markers: ndarray[tuple[Any, ...], dtype[int32]] | None = None#
property n_nodes: int#

Return number of nodes.

property n_elements: int#

Return number of elements.

property n_triangles: int#

Return number of triangular elements.

property n_quads: int#

Return number of quadrilateral elements.

get_element_areas()[source]#

Calculate area of each element.

Returns:

Array of element areas

Return type:

ndarray[tuple[Any, …], dtype[float64]]

get_element_centroids()[source]#

Calculate centroid of each element.

Returns:

Array of element centroids (n_elements, 2)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

to_appgrid()[source]#

Convert mesh result to AppGrid.

Returns:

AppGrid instance

Return type:

AppGrid

__init__(nodes, elements, node_markers=None, element_markers=None)#
class pyiwfm.mesh_generation.generators.MeshGenerator[source]#

Bases: ABC

Abstract base class for mesh generators.

Subclasses must implement the generate() method to create a mesh from boundary and constraint definitions.

abstractmethod generate(boundary, max_area=None, min_angle=None, streams=None, refinement_zones=None, points=None)[source]#

Generate a mesh within the boundary.

Parameters:
  • boundary (Boundary) – Boundary polygon (with optional holes)

  • max_area (float | None) – Maximum element area

  • min_angle (float | None) – Minimum angle constraint (degrees)

  • streams (list[StreamConstraint] | None) – Stream line constraints

  • refinement_zones (list[RefinementZone] | None) – Zones requiring smaller elements

  • points (list[PointConstraint] | None) – Fixed point constraints

Returns:

MeshResult with generated mesh

Return type:

MeshResult

generate_from_shapely(polygon, max_area=None, min_angle=None)[source]#

Generate mesh from Shapely polygon.

Parameters:
  • polygon (Polygon) – Shapely Polygon object

  • max_area (float | None) – Maximum element area

  • min_angle (float | None) – Minimum angle constraint

Returns:

MeshResult with generated mesh

Return type:

MeshResult

Constraints Module#

The constraints module contains classes for representing mesh constraints such as boundaries, internal features, and refinement zones.

Mesh generation constraints for IWFM models.

This module provides classes for defining constraints used in mesh generation including boundaries, streams, refinement zones, and fixed points.

class pyiwfm.mesh_generation.constraints.BoundarySegment(start, end, marker=0)[source]#

Bases: object

A segment of a boundary polygon.

Variables:
start: ndarray[tuple[Any, ...], dtype[float64]]#
end: ndarray[tuple[Any, ...], dtype[float64]]#
marker: int = 0#
property length: float#

Return segment length.

property midpoint: tuple[float, float]#

Return segment midpoint.

__init__(start, end, marker=0)#
class pyiwfm.mesh_generation.constraints.Boundary(vertices, holes=<factory>, markers=None)[source]#

Bases: object

A boundary polygon for mesh generation.

Variables:
vertices: ndarray[tuple[Any, ...], dtype[float64]]#
holes: list[ndarray[tuple[Any, ...], dtype[float64]]]#
markers: ndarray[tuple[Any, ...], dtype[int32]] | None = None#
property n_vertices: int#

Return number of vertices.

property is_closed: bool#

Check if boundary is closed (first and last vertices connected).

property area: float#

Calculate boundary area using shoelace formula.

Returns:

Area (positive for counter-clockwise vertices)

property centroid: tuple[float, float]#

Calculate boundary centroid.

get_segments()[source]#

Get list of boundary segments.

get_hole_points()[source]#

Get interior points for each hole (for mesh generators).

Returns:

List of (x, y) points inside each hole

Return type:

list[tuple[float, float]]

__init__(vertices, holes=<factory>, markers=None)#
class pyiwfm.mesh_generation.constraints.StreamConstraint(vertices, stream_id=0, marker=0)[source]#

Bases: object

A stream line constraint for mesh generation.

Stream constraints ensure the mesh conforms to stream locations, with nodes placed along the stream path.

Variables:
  • vertices (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]) – Array of stream vertices (n, 2)

  • stream_id (int) – Identifier for this stream

  • marker (int) – Segment marker for stream edges

vertices: ndarray[tuple[Any, ...], dtype[float64]]#
stream_id: int = 0#
marker: int = 0#
property n_vertices: int#

Return number of vertices.

property length: float#

Calculate total stream length.

get_segments()[source]#

Get list of stream segments.

resample(spacing)[source]#

Resample stream at regular spacing.

Parameters:

spacing (float) – Distance between points

Returns:

New StreamConstraint with resampled vertices

Return type:

StreamConstraint

__init__(vertices, stream_id=0, marker=0)#
class pyiwfm.mesh_generation.constraints.RefinementZone(center=None, radius=None, polygon=None, max_area=100.0)[source]#

Bases: object

A zone requiring mesh refinement.

Can be defined as either a circular zone (center + radius) or a polygon.

Variables:
  • center (tuple[float, float] | None) – Center point for circular zone (x, y)

  • radius (float | None) – Radius for circular zone

  • polygon (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]] | None) – Polygon vertices for polygonal zone

  • max_area (float) – Maximum element area in this zone

center: tuple[float, float] | None = None#
radius: float | None = None#
polygon: ndarray[tuple[Any, ...], dtype[float64]] | None = None#
max_area: float = 100.0#
__post_init__()[source]#

Validate zone definition.

contains(x, y)[source]#

Check if point is inside refinement zone.

Parameters:
  • x (float) – X coordinate

  • y (float) – Y coordinate

Returns:

True if point is inside zone

Return type:

bool

__init__(center=None, radius=None, polygon=None, max_area=100.0)#
class pyiwfm.mesh_generation.constraints.PointConstraint(x, y, marker=0)[source]#

Bases: object

A fixed point constraint for mesh generation.

Ensures a node is placed at this exact location.

Variables:
  • x (float) – X coordinate

  • y (float) – Y coordinate

  • marker (int) – Point marker

x: float#
y: float#
marker: int = 0#
as_array()[source]#

Return point as numpy array.

__init__(x, y, marker=0)#

Triangle Wrapper#

The Triangle wrapper provides an interface to the Triangle mesh generation library for creating triangular meshes.

Triangle mesh generator wrapper.

This module wraps the Triangle library to provide mesh generation capabilities for IWFM models using triangular elements.

Triangle is a high-quality mesh generator and Delaunay triangulator.

class pyiwfm.mesh_generation.triangle_wrapper.TriangleMeshGenerator[source]#

Bases: MeshGenerator

Mesh generator using the Triangle library.

This generator creates triangular meshes using Jonathan Shewchuk’s Triangle library via the triangle Python package.

Features:
  • Quality mesh generation with angle constraints

  • Area constraints (global and regional)

  • Conforming Delaunay triangulation

  • Support for holes and internal boundaries

__init__()[source]#

Initialize the Triangle mesh generator.

generate(boundary, max_area=None, min_angle=None, streams=None, refinement_zones=None, points=None)[source]#

Generate a triangular mesh within the boundary.

Parameters:
  • boundary (Boundary) – Boundary polygon (with optional holes)

  • max_area (float | None) – Maximum element area

  • min_angle (float | None) – Minimum angle constraint (degrees)

  • streams (list[StreamConstraint] | None) – Stream line constraints

  • refinement_zones (list[RefinementZone] | None) – Zones requiring smaller elements

  • points (list[PointConstraint] | None) – Fixed point constraints

Returns:

MeshResult with generated triangular mesh

Return type:

MeshResult

Gmsh Wrapper#

The Gmsh wrapper provides an interface to the Gmsh mesh generation library for creating triangular, quadrilateral, or mixed meshes.

Gmsh mesh generator wrapper.

This module wraps the Gmsh library to provide mesh generation capabilities for IWFM models supporting triangular, quadrilateral, and mixed element meshes.

Gmsh is a powerful open-source mesh generator with CAD capabilities.

class pyiwfm.mesh_generation.gmsh_wrapper.GmshMeshGenerator(element_type='triangle')[source]#

Bases: MeshGenerator

Mesh generator using the Gmsh library.

This generator creates meshes using Gmsh, supporting triangular, quadrilateral, and mixed element types.

Variables:

element_type – Type of elements to generate - ‘triangle’: Only triangular elements - ‘quad’: Quadrilateral elements (recombined) - ‘mixed’: Mix of triangles and quads

__init__(element_type='triangle')[source]#

Initialize the Gmsh mesh generator.

Parameters:

element_type (Literal['triangle', 'quad', 'mixed']) – Type of elements to generate

generate(boundary, max_area=None, min_angle=None, streams=None, refinement_zones=None, points=None)[source]#

Generate a mesh within the boundary.

Parameters:
  • boundary (Boundary) – Boundary polygon (with optional holes)

  • max_area (float | None) – Maximum element area

  • min_angle (float | None) – Minimum angle constraint (degrees)

  • streams (list[StreamConstraint] | None) – Stream line constraints

  • refinement_zones (list[RefinementZone] | None) – Zones requiring smaller elements

  • points (list[PointConstraint] | None) – Fixed point constraints

Returns:

MeshResult with generated mesh

Return type:

MeshResult