Examples Gallery¶
This page showcases complete examples from the mmgpy repository.
3D Volume Meshing (mmg3d)¶
Mesh Quality Improvement¶
Improve mesh quality without changing topology.
"""Mesh quality improvement with mean edge length preservation."""
import mmgpy
# Load mesh
mesh = mmgpy.Mesh("input.mesh")
# Optimize quality only (no vertex insertion/removal)
result = mesh.remesh_optimize()
print(f"Quality: {result.quality_mean_before:.3f} -> {result.quality_mean_after:.3f}")
Open Boundary Remeshing¶
Remesh volumetric mesh with open boundaries.
"""Remeshing with open boundary handling."""
import mmgpy
mesh = mmgpy.Mesh("domain_with_holes.mesh")
result = mesh.remesh(
hmax=0.1,
hausd=0.001,
)
Lagrangian Motion¶
Remesh while applying mesh displacement.
"""Lagrangian mesh motion remeshing."""
import mmgpy
import numpy as np
mesh = mmgpy.Mesh("input.mesh")
vertices = mesh.get_vertices()
# Define displacement field
displacement = np.zeros_like(vertices)
displacement[:, 0] = 0.1 * np.sin(vertices[:, 1] * np.pi)
# Remesh with motion
result = mesh.remesh_lagrangian(displacement)
Level-Set Discretization¶
Extract isosurface from implicit function.
"""Level-set based surface extraction."""
import mmgpy
import numpy as np
mesh = mmgpy.Mesh("background.mesh")
vertices = mesh.get_vertices()
# Sphere level-set
levelset = np.linalg.norm(vertices - [0.5, 0.5, 0.5], axis=1) - 0.3
result = mesh.remesh_levelset(levelset)
2D Meshing (mmg2d)¶
Local Sizing Control¶
Apply regional mesh refinement.
"""Per-region mesh density control."""
import mmgpy
mesh = mmgpy.Mesh("domain.mesh")
# Fine mesh in center
mesh.set_size_sphere(center=[0.5, 0.5], radius=0.2, size=0.01)
# Coarser mesh elsewhere
result = mesh.remesh(hmax=0.1)
Solution-Based Adaptation¶
Adapt mesh to solution field.
"""Mesh adaptation to solution gradients."""
import mmgpy
import mmgpy.metrics as metrics
import numpy as np
mesh = mmgpy.Mesh("domain.mesh")
vertices = mesh.get_vertices()
# Solution field
solution = np.sin(vertices[:, 0] * 4 * np.pi) * np.cos(vertices[:, 1] * 4 * np.pi)
# Create metric from solution gradients
# (simplified - full implementation computes Hessian)
sizes = 0.01 + 0.1 * np.abs(solution)
metric = metrics.create_isotropic_metric(sizes)
mesh["metric"] = metric
result = mesh.remesh()
Anisotropic Mesh Adaptation¶
Directional mesh refinement.
"""Anisotropic mesh adaptation."""
import mmgpy
import mmgpy.metrics as metrics
import numpy as np
mesh = mmgpy.Mesh("domain.mesh")
n_vertices = mesh.get_mesh_size()["vertices"]
# Create anisotropic metric (stretch in x direction)
directions = np.tile(np.eye(2), (n_vertices, 1, 1))
sizes = np.tile([0.1, 0.02], (n_vertices, 1)) # Larger in x, smaller in y
metric = metrics.create_anisotropic_metric(directions, sizes)
mesh["metric"] = metric
result = mesh.remesh()
Implicit 2D Domain Meshing¶
Generate mesh from implicit function.
"""Generate 2D mesh from implicit domain definition."""
import mmgpy
import numpy as np
mesh = mmgpy.Mesh("background.mesh")
vertices = mesh.get_vertices()
# Circle level-set
levelset = np.linalg.norm(vertices[:, :2] - [0.5, 0.5], axis=1) - 0.3
result = mesh.remesh_levelset(levelset)
Surface Meshing (mmgs)¶
Mechanical Piece Remeshing¶
Industrial part surface remeshing.
"""Mechanical part surface optimization."""
import mmgpy
mesh = mmgpy.Mesh("part.stl")
result = mesh.remesh(
hmax=0.05,
hausd=0.001,
angle=30.0, # Preserve sharp edges
)

Smooth Surface Remeshing¶
Surface smoothing and refinement.
"""Smooth surface mesh optimization."""
import mmgpy
mesh = mmgpy.Mesh("surface.mesh")
result = mesh.remesh(
hmax=0.1,
hausd=0.0001, # Tight approximation
hgrad=1.1, # Smooth gradation
)

Implicit Surface Meshing¶
Generate surface from implicit function.
"""Generate surface mesh from implicit function."""
import mmgpy
import numpy as np
mesh = mmgpy.Mesh("background_surface.mesh")
vertices = mesh.get_vertices()
# Torus level-set
R, r = 0.5, 0.15
x, y, z = vertices[:, 0] - 0.5, vertices[:, 1] - 0.5, vertices[:, 2] - 0.5
q = np.sqrt(x**2 + y**2) - R
levelset = np.sqrt(q**2 + z**2) - r
result = mesh.remesh_levelset(levelset)
Running Examples¶
Clone the repository and run examples:
git clone https://github.com/kmarchais/mmgpy.git
cd mmgpy
# Install with examples dependencies
pip install -e ".[dev]"
# Run an example
python examples/mmgs/mechanical_piece_remeshing.py
Each example includes detailed comments and visualization using PyVista.