CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pygmsh

Python frontend for Gmsh mesh generator providing intuitive abstractions for creating complex geometric models and generating high-quality meshes

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

PyGMSH

PyGMSH is a comprehensive Python frontend for Gmsh, providing intuitive abstractions for creating complex geometric models and generating high-quality meshes programmatically. It enables developers and researchers to create sophisticated 3D finite element meshes through two powerful geometry engines: the built-in geo module for traditional CAD-style operations and the occ module for OpenCASCADE-based modeling with advanced geometric operations.

Package Information

  • Package Name: pygmsh
  • Language: Python
  • Installation: pip install pygmsh
  • Dependencies: gmsh, meshio, numpy
  • License: GPL-3.0-or-later

Core Imports

import pygmsh

For built-in geometry kernel:

import pygmsh.geo

For OpenCASCADE geometry kernel:

import pygmsh.occ

For utility functions:

from pygmsh import write, optimize, rotation_matrix, orient_lines

Basic Usage

Built-in Geometry Kernel

import pygmsh

# Create geometry using built-in kernel
with pygmsh.geo.Geometry() as geom:
    # Add basic shapes
    circle = geom.add_circle([0.0, 0.0, 0.0], 1.0, mesh_size=0.1)
    rectangle = geom.add_rectangle(
        xmin=-2.0, xmax=2.0, ymin=-1.0, ymax=1.0, z=0.0, mesh_size=0.2
    )
    
    # Generate mesh
    mesh = geom.generate_mesh(dim=2)

# Write mesh to file
pygmsh.write("output.msh")

OpenCASCADE Geometry Kernel

import pygmsh

# Create geometry using OpenCASCADE kernel  
with pygmsh.occ.Geometry() as geom:
    # Add primitive shapes
    ball = geom.add_ball([0, 0, 0], 1.0)
    box = geom.add_box([1, 1, 1], [2, 2, 2])
    
    # Perform boolean operations
    result = geom.boolean_union([ball, box])
    
    # Generate mesh
    mesh = geom.generate_mesh(dim=3)

# Optimize mesh quality
optimized_mesh = pygmsh.optimize(mesh)

Architecture

PyGMSH provides a dual-kernel architecture enabling flexible geometric modeling approaches:

Geometry Kernels

  • geo kernel (pygmsh.geo): Built-in Gmsh geometry kernel for traditional CAD-style modeling with parametric shapes, extrusions, and revolutions
  • occ kernel (pygmsh.occ): OpenCASCADE-based kernel enabling advanced solid modeling with boolean operations, BREP import/export, and complex surface operations
  • common base (pygmsh.common): Shared functionality including mesh generation, transformations, physical groups, and size field management

Design Patterns

  • Context managers: All geometry operations use with statements for proper resource management
  • Method chaining: Geometry entities can be transformed and combined using fluent interfaces
  • Mesh integration: Seamless integration with meshio for data processing and format conversion
  • Size control: Comprehensive mesh sizing through point-based sizing, size fields, and callback functions

This architecture supports maximum reusability across scientific computing applications, finite element analysis, computational fluid dynamics, and any domain requiring mesh generation.

Capabilities

Built-in Geometry Operations

Traditional CAD-style geometric modeling with parametric shapes, extrusions, revolutions, and structured mesh generation. Ideal for engineering applications requiring precise control over mesh topology.

class Geometry(CommonGeometry):
    def add_circle(self, x0, radius, mesh_size=None, R=None, compound=False, 
                   num_sections=3, holes=None, make_surface=True): ...
    def add_rectangle(self, xmin, xmax, ymin, ymax, z, mesh_size=None, 
                      holes=None, make_surface=True): ...
    def add_ellipsoid(self, x0, radii, mesh_size=None, with_volume=True, holes=None): ...
    def add_ball(self, x0, radius, **kwargs): ...
    def add_box(self, x0, x1, y0, y1, z0, z1, mesh_size=None, with_volume=True, holes=None): ...
    def add_torus(self, irad, orad, mesh_size=None, R=np.eye(3), x0=np.array([0,0,0]), 
                  variant="extrude_lines"): ...
    def add_pipe(self, outer_radius, inner_radius, length, R=np.eye(3), x0=np.array([0,0,0]), 
                 mesh_size=None, variant="rectangle_rotation"): ...
    def revolve(self, *args, **kwargs): ...  # angle < π constraint
    def twist(self, input_entity, translation_axis, rotation_axis, point_on_axis, 
              angle, num_layers=None, heights=None, recombine=False): ...
    def in_surface(self, input_entity, surface): ...
    def in_volume(self, input_entity, volume): ...

Built-in Geometry Operations

OpenCASCADE Solid Modeling

Advanced solid modeling capabilities with boolean operations, primitive shapes, BREP operations, and CAD file import/export. Provides the full power of OpenCASCADE for complex geometric operations.

class Geometry(CommonGeometry):
    # Properties
    characteristic_length_min: float
    characteristic_length_max: float
    
    # Shape creation
    def add_ball(self, *args, mesh_size=None, **kwargs): ...
    def add_box(self, *args, mesh_size=None, **kwargs): ...
    def add_cone(self, *args, mesh_size=None, **kwargs): ...
    def add_cylinder(self, *args, mesh_size=None, **kwargs): ...
    def add_disk(self, *args, mesh_size=None, **kwargs): ...
    def add_ellipsoid(self, center, radii, mesh_size=None): ...
    def add_rectangle(self, *args, mesh_size=None, **kwargs): ...
    def add_torus(self, *args, mesh_size=None, **kwargs): ...
    def add_wedge(self, *args, mesh_size=None, **kwargs): ...
    
    # Boolean operations
    def boolean_intersection(self, entities, delete_first=True, delete_other=True): ...
    def boolean_union(self, entities, delete_first=True, delete_other=True): ...
    def boolean_difference(self, d0, d1, delete_first=True, delete_other=True): ...
    def boolean_fragments(self, d0, d1, delete_first=True, delete_other=True): ...
    
    # Import/export and transformations
    def import_shapes(self, filename): ...
    def revolve(self, *args, **kwargs): ...  # angle < 2π constraint
    def force_outward_normals(self, tag): ...

OpenCASCADE Solid Modeling

Helper Functions and Utilities

Essential utility functions for mesh processing, geometric transformations, file I/O, and mesh optimization that complement the core geometry operations.

# File I/O operations
def write(filename: str): ...

# Geometric transformations
def rotation_matrix(u, theta): ...
def orient_lines(lines): ...

# Mesh optimization
def optimize(mesh, method="", verbose=False): ...
def extract_to_meshio(): ...

# Command line interface
def optimize_cli(argv=None): ...

Helper Functions and Utilities

Mesh Control and Size Fields

Advanced mesh generation control with size fields, boundary layers, transfinite meshing, and callback-based sizing for precise mesh quality management.

# Size field management
def set_mesh_size_callback(fun, ignore_other_mesh_sizes=True): ...
def add_boundary_layer(*args, **kwargs): ...
def set_background_mesh(fields, operator): ...

# Transfinite meshing
def set_transfinite_curve(curve, num_nodes, mesh_type, coeff): ...
def set_transfinite_surface(surface, arrangement, corner_pts): ...
def set_transfinite_volume(volume, corner_pts): ...
def set_recombined_surfaces(surfaces): ...

Mesh Control and Size Fields

Common Base Types

class CommonGeometry:
    """Base class providing fundamental geometry operations for both kernels."""
    
    # Basic shape creation
    def add_point(self, x, mesh_size=None): ...
    def add_line(self, p0, p1): ...
    def add_circle_arc(self, *args, **kwargs): ...
    def add_ellipse_arc(self, *args, **kwargs): ...
    def add_spline(self, *args, **kwargs): ...
    def add_bspline(self, *args, **kwargs): ...
    def add_bezier(self, *args, **kwargs): ...
    def add_curve_loop(self, *args, **kwargs): ...
    def add_plane_surface(self, *args, **kwargs): ...
    def add_surface(self, *args, **kwargs): ...
    def add_surface_loop(self, *args, **kwargs): ...
    def add_volume(self, *args, **kwargs): ...
    def add_polygon(self, *args, **kwargs): ...
    
    # Physical groups and properties
    def add_physical(self, entities, label=None): ...
    
    # Mesh generation and control
    def generate_mesh(self, dim=3, order=None, algorithm=None, verbose=False): ...
    def set_mesh_size_callback(self, fun, ignore_other_mesh_sizes=True): ...
    def add_boundary_layer(self, *args, **kwargs): ...
    def set_background_mesh(self, *args, **kwargs): ...
    
    # Transformations
    def extrude(self, input_entity, translation_axis, num_layers=None, 
                heights=None, recombine=False): ...
    def translate(self, obj, vector): ...
    def rotate(self, obj, point, angle, axis): ...
    def copy(self, obj): ...
    def symmetrize(self, obj, coefficients): ...
    def dilate(self, obj, x0, abc): ...
    def mirror(self, obj, abcd): ...
    def remove(self, obj, recursive=False): ...
    
    # Transfinite meshing
    def set_transfinite_curve(self, curve, num_nodes, mesh_type, coeff): ...
    def set_transfinite_surface(self, surface, arrangement, corner_pts): ...
    def set_transfinite_volume(self, volume, corner_pts): ...
    def set_recombined_surfaces(self, surfaces): ...
    
    # Embedding operations
    def in_surface(self, input_entity, surface): ...
    def in_volume(self, input_entity, volume): ...
    
    # Context manager
    def __enter__(self): ...
    def __exit__(self, *args): ...
    def synchronize(self): ...
    def save_geometry(self, filename): ...

class Point:
    """Elementary point in 3D space."""
    def __init__(self, env, x, mesh_size=None): ...
    # Common attributes: x, dim=0, _id, dim_tag, dim_tags

class Line:
    """Line segment between two points."""
    def __init__(self, env, p0, p1): ...
    # Common attributes: points, dim=1, _id, dim_tag, dim_tags

class Polygon:
    """Polygon with optional holes and surface generation."""
    def __init__(self, host, points, mesh_size=None, holes=None, make_surface=True): ...
    # Common attributes: points, curves, lines, curve_loop, surface, dim=2, _id, dim_tag, dim_tags

class Surface:
    """Surface generated from curve loop."""  
    def __init__(self, env, curve_loop): ...
    # Common attributes: dim=2, _id, dim_tag, dim_tags

class Volume:
    """Volume from surface loop."""
    def __init__(self, env, surface_loop): ...
    # Common attributes: dim=3, _id, dim_tag, dim_tags

# Size field classes
class BoundaryLayer:
    """Boundary layer size field for mesh refinement near surfaces."""
    def __init__(self, lcmin, lcmax, distmin, distmax, edges_list=None, 
                 faces_list=None, nodes_list=None, num_points_per_curve=None): ...

class SetBackgroundMesh:
    """Background mesh configuration with multiple size fields."""
    def __init__(self, fields, operator): ...

Shape Classes and Entity Types

Comprehensive documentation of all shape classes used in pygmsh, covering both geometric entities and primitive shape classes with their constructors and attributes.

# Basic geometric entities
class Point:
    def __init__(self, env, x, mesh_size=None): ...
    # Common attributes: x, dim=0, _id, dim_tag, dim_tags

class Line:
    def __init__(self, env, p0, p1): ...
    # Common attributes: points, dim=1, _id, dim_tag, dim_tags

class Polygon:
    def __init__(self, host, points, mesh_size=None, holes=None, make_surface=True): ...
    # Common attributes: points, curves, curve_loop, surface, dim, _id, dim_tags

# OpenCASCADE primitive shapes
class Ball:
    def __init__(self, center, radius, angle1=-π/2, angle2=π/2, angle3=2π): ...

class Box:
    def __init__(self, x0, extents, char_length=None): ...

class Cylinder:
    def __init__(self, x0, axis, radius, angle=2π): ...

class Cone:
    def __init__(self, center, axis, radius0, radius1, angle=2π): ...

class Disk:
    def __init__(self, x0, radius0, radius1=None): ...

class Torus:
    def __init__(self, center, radius0, radius1, alpha=2π): ...

class Wedge:
    def __init__(self, x0, extents, top_extent=None): ...

class Rectangle:
    def __init__(self, x0, a, b, corner_radius=None): ...

Shape Classes and Entity Types

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pygmsh@7.1.x
Publish Source
CLI
Badge
tessl/pypi-pygmsh badge