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:
objectResult from mesh generation.
- Variables:
nodes (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]) – Array of node coordinates (n_nodes, 2)
elements (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.int32]]) – Array of element vertex indices (n_elements, 3 or 4) Triangles use -1 for 4th vertex
node_markers (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.int32]] | None) – Optional node boundary markers
element_markers (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.int32]] | None) – Optional element region markers
- __init__(nodes, elements, node_markers=None, element_markers=None)#
- class pyiwfm.mesh_generation.generators.MeshGenerator[source]#
Bases:
ABCAbstract 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:
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:
objectA segment of a boundary polygon.
- Variables:
start (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]) – Start point coordinates (x, y)
end (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]) – End point coordinates (x, y)
marker (int) – Boundary marker for identifying segment type
- __init__(start, end, marker=0)#
- class pyiwfm.mesh_generation.constraints.Boundary(vertices, holes=<factory>, markers=None)[source]#
Bases:
objectA boundary polygon for mesh generation.
- Variables:
vertices (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]) – Array of boundary vertices (n, 2) in counter-clockwise order
holes (list[numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.float64]]]) – List of hole polygons (each is array of vertices)
markers (numpy.ndarray[tuple[Any, ...], numpy.dtype[numpy.int32]] | None) – Optional array of segment markers
- property area: float#
Calculate boundary area using shoelace formula.
- Returns:
Area (positive for counter-clockwise vertices)
- __init__(vertices, holes=<factory>, markers=None)#
- class pyiwfm.mesh_generation.constraints.StreamConstraint(vertices, stream_id=0, marker=0)[source]#
Bases:
objectA 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
- resample(spacing)[source]#
Resample stream at regular spacing.
- Parameters:
spacing (float) – Distance between points
- Returns:
New StreamConstraint with resampled vertices
- Return type:
- __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:
objectA 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
- __init__(center=None, radius=None, polygon=None, max_area=100.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:
MeshGeneratorMesh 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
- 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:
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:
MeshGeneratorMesh 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: