Python frontend for Gmsh mesh generator providing intuitive abstractions for creating complex geometric models and generating high-quality meshes
—
The built-in geometry kernel (pygmsh.geo) provides traditional CAD-style geometric modeling with parametric shapes, extrusions, revolutions, and structured mesh generation. This kernel excels at engineering applications requiring precise control over mesh topology and structured meshing approaches.
Create circular geometries with optional hole patterns and surface generation. Supports compound circles for complex topologies and configurable mesh sizing.
def add_circle(x0, radius, mesh_size=None, R=None, compound=False,
num_sections=3, holes=None, make_surface=True):
"""
Create a circle with optional holes and surface.
Parameters:
- x0: list[float], center point coordinates [x, y, z]
- radius: float, circle radius
- mesh_size: float, optional mesh size at circle boundary
- R: array, optional rotation matrix for circle orientation
- compound: bool, create compound circle for complex topologies
- num_sections: int, number of sections for circle discretization (default: 3)
- holes: list, optional holes to subtract from circle surface
- make_surface: bool, create surface from circle curve (default: True)
Returns:
Circle object with attributes: x0, radius, R, compound, num_sections,
holes, curve_loop, plane_surface, mesh_size
"""Create rectangular surfaces with precise boundary control and optional hole patterns. Ideal for domain decomposition and structured mesh generation.
def add_rectangle(xmin, xmax, ymin, ymax, z, mesh_size=None, holes=None, make_surface=True):
"""
Create a rectangle in the XY plane at specified Z coordinate.
Parameters:
- xmin, xmax: float, X-direction bounds
- ymin, ymax: float, Y-direction bounds
- z: float, Z coordinate of rectangle plane
- mesh_size: float, optional mesh size at rectangle boundary
- holes: list, optional holes to subtract from rectangle surface
- make_surface: bool, create surface from rectangle boundary (default: True)
Returns:
Rectangle surface object
"""Create ellipsoids and spheres with configurable radii and volume generation. Supports both surface-only and volumetric representations.
def add_ellipsoid(x0, radii, mesh_size=None, with_volume=True, holes=None):
"""
Create an ellipsoid with specified radii in each direction.
Parameters:
- x0: list[float], center point coordinates [x, y, z]
- radii: list[float], radii in [x, y, z] directions
- mesh_size: float, optional mesh size at ellipsoid surface
- with_volume: bool, create volume (True) or surface only (False)
- holes: list, optional holes to subtract from ellipsoid volume
Returns:
Ellipsoid object (surface or volume based on with_volume parameter)
"""
def add_ball(x0, radius, **kwargs):
"""
Create a sphere (special case of ellipsoid with equal radii).
Parameters:
- x0: list[float], center point coordinates [x, y, z]
- radius: float, sphere radius
- **kwargs: additional arguments passed to add_ellipsoid
Returns:
Sphere object
"""Create rectangular boxes with precise dimensional control and volume generation capabilities.
def add_box(x0, x1, y0, y1, z0, z1, mesh_size=None, with_volume=True, holes=None):
"""
Create a rectangular box defined by coordinate bounds.
Parameters:
- x0, x1: float, X-direction bounds
- y0, y1: float, Y-direction bounds
- z0, z1: float, Z-direction bounds
- mesh_size: float, optional mesh size at box surfaces
- with_volume: bool, create volume (True) or surface only (False)
- holes: list, optional holes to subtract from box volume
Returns:
Box object (surface or volume based on with_volume parameter)
"""Create torus geometries with inner and outer radii, supporting multiple construction variants for different meshing strategies.
def add_torus(irad, orad, mesh_size=None, R=np.eye(3), x0=np.array([0,0,0]),
variant="extrude_lines"):
"""
Create a torus with inner and outer radii.
Parameters:
- irad: float, inner radius (minor radius)
- orad: float, outer radius (major radius)
- mesh_size: float, optional mesh size at torus surface
- R: array, rotation matrix for torus orientation (default: identity)
- x0: array, center point coordinates (default: origin)
- variant: str, construction method ("extrude_lines" or other variants)
Returns:
Torus object
"""Create hollow cylindrical pipes with configurable wall thickness and construction variants.
def add_pipe(outer_radius, inner_radius, length, R=np.eye(3), x0=np.array([0,0,0]),
mesh_size=None, variant="rectangle_rotation"):
"""
Create a hollow cylindrical pipe.
Parameters:
- outer_radius: float, pipe outer radius
- inner_radius: float, pipe inner radius (wall thickness = outer - inner)
- length: float, pipe length
- R: array, rotation matrix for pipe orientation (default: identity)
- x0: array, center point coordinates (default: origin)
- mesh_size: float, optional mesh size at pipe surfaces
- variant: str, construction method ("rectangle_rotation" or other variants)
Returns:
Pipe object with hollow cylindrical volume
"""Create surfaces and volumes by revolving curves and surfaces around specified axes with angle constraints.
def revolve(*args, **kwargs):
"""
Revolve entity around axis to create surfaces or volumes.
Note: For geo kernel, revolution angle must be < π (180 degrees).
Parameters depend on input entity type:
- input_entity: curve, surface, or volume to revolve
- point_on_axis: point on revolution axis
- axis_direction: revolution axis direction vector
- angle: revolution angle in radians (must be < π)
Returns:
Revolved entity (surface from curve, volume from surface)
"""Create complex geometries by extruding entities along a path with simultaneous rotation, enabling helical and twisted shapes.
def twist(input_entity, translation_axis, rotation_axis, point_on_axis, angle,
num_layers=None, heights=None, recombine=False):
"""
Create twisted extrusion of entity with simultaneous translation and rotation.
Parameters:
- input_entity: curve or surface to twist
- translation_axis: translation direction vector
- rotation_axis: rotation axis direction vector
- point_on_axis: point on rotation axis
- angle: total rotation angle in radians
- num_layers: int, number of extrusion layers for structured meshing
- heights: list[float], layer heights for non-uniform extrusion
- recombine: bool, recombine tetrahedra into hexahedra where possible
Returns:
Twisted entity (surface from curve, volume from surface)
"""Embed geometric entities within higher-dimensional entities for mesh generation control and physical group management.
def in_surface(input_entity, surface):
"""
Embed entity in surface for mesh generation.
Embedded entities constrain mesh generation without creating physical boundaries.
Parameters:
- input_entity: point or curve to embed
- surface: target surface for embedding
Returns:
None (modifies geometry in-place)
"""
def in_volume(input_entity, volume):
"""
Embed entity in volume for mesh generation.
Embedded entities constrain mesh generation without creating physical boundaries.
Parameters:
- input_entity: point, curve, or surface to embed
- volume: target volume for embedding
Returns:
None (modifies geometry in-place)
"""import pygmsh
import numpy as np
with pygmsh.geo.Geometry() as geom:
# Create main circle
outer_circle = geom.add_circle([0, 0, 0], radius=2.0, mesh_size=0.2)
# Create holes
hole1 = geom.add_circle([-0.8, 0, 0], radius=0.3, mesh_size=0.1, make_surface=False)
hole2 = geom.add_circle([0.8, 0, 0], radius=0.3, mesh_size=0.1, make_surface=False)
# Subtract holes from main circle
surface_with_holes = geom.add_circle(
[0, 0, 0], radius=2.0, mesh_size=0.2,
holes=[hole1.curve_loop, hole2.curve_loop]
)
mesh = geom.generate_mesh(dim=2)import pygmsh
import numpy as np
with pygmsh.geo.Geometry() as geom:
# Create profile curve
points = [
geom.add_point([1.0, 0.0, 0.0]),
geom.add_point([1.5, 0.5, 0.0]),
geom.add_point([1.2, 1.0, 0.0]),
geom.add_point([1.0, 1.0, 0.0])
]
lines = [
geom.add_line(points[i], points[i+1])
for i in range(len(points)-1)
]
# Revolve around Z-axis (angle < π for geo kernel)
revolved_surface = geom.revolve(
lines,
point_on_axis=[0, 0, 0],
axis_direction=[0, 0, 1],
angle=np.pi * 0.8 # Must be < π
)
mesh = geom.generate_mesh(dim=3)import pygmsh
import numpy as np
with pygmsh.geo.Geometry() as geom:
# Create base rectangle
rectangle = geom.add_rectangle(-0.5, 0.5, -0.3, 0.3, z=0.0, make_surface=False)
# Twist extrude along Z-axis with rotation around Z
twisted_volume = geom.twist(
rectangle.curve_loop,
translation_axis=[0, 0, 1], # Extrude in Z
rotation_axis=[0, 0, 1], # Rotate around Z
point_on_axis=[0, 0, 0],
angle=np.pi/2, # 90 degree twist
num_layers=10,
heights=[0.1] * 10 # Uniform layer heights
)
mesh = geom.generate_mesh(dim=3)class Circle:
"""Result object from add_circle() containing circle geometry and mesh data."""
def __init__(self, x0, radius, mesh_size, R, compound, num_sections,
holes, curve_loop, plane_surface):
"""
Initialize circle object.
Attributes:
- x0: list[float], center coordinates
- radius: float, circle radius
- mesh_size: float, mesh size at boundary
- R: array, rotation matrix
- compound: bool, compound circle flag
- num_sections: int, discretization sections
- holes: list, hole curve loops
- curve_loop: CurveLoop, circle boundary
- plane_surface: PlaneSurface, circle surface (if created)
"""Install with Tessl CLI
npx tessl i tessl/pypi-pygmsh