Generate#
Mesh generation from implicit representations: isosurface extraction and dimension-generic volume meshing.
Choosing a Mesher#
PhysicsNeMo-Mesh has three generation tools with distinct contracts:
Tool |
Input |
Output |
Use When |
|---|---|---|---|
|
closed boundary |
volume |
the boundary discretization is the contract (coupling, per-boundary BCs, guaranteed worst-element quality) |
|
implicit function \(\varphi(x)\) (SDF, level set, neural field) |
volume |
geometry is implicit, you need 3D/ND volumes, GPU scale, or gradients with regard to shape |
|
3D scalar field on a grid |
surface |
the boundary surface itself is the deliverable (visualization, B-rep export) |
Volume Meshing of Implicit Domains#
mesh_implicit_domain() generates a simplex volume mesh of
{x : phi(x) < 0} for an arbitrary implicit function phi (a signed
distance function, or any level set with a usable gradient, including
neural implicit fields), in any spatial dimension, entirely in PyTorch
tensor ops on CPU or CUDA. The meshed set is {phi < 0} intersected
with the bounding box. Where the domain reaches the box, the generator
treats its faces as a boundary, so external-flow “box minus obstacle”
domains work directly. The generator is structurally robust. Every
optimization step is validity-gated, so it always returns a positively
oriented mesh with a closed-manifold boundary. Difficult inputs degrade
element quality (reported in diagnostics), but never prevent the mesh
from being generated. A coverage guard raises an error, rather than
silently dropping geometry, when the domain
has features below the target edge length h or when coverage cannot
be certified at all. The latter can happen, for example, if a neural field
returns NaN inside the box when queried outside its training range. You can
interpolate sharp corners exactly through feature_points.
refit_mesh_to_implicit() is the differentiable companion: it
re-projects a mesh’s boundary onto phi = 0 with graph-preserving
Newton steps at fixed topology, so gradients flow from mesh coordinates to
shape parameters inside phi. This enables meshing as a
differentiable layer for shape optimization.
import torch
from physicsnemo.mesh.generate import (
mesh_implicit_domain,
refit_mesh_to_implicit,
sdf_difference,
sdf_sphere,
)
# A spherical shell, meshed with tetrahedra on the GPU:
shell = sdf_difference(sdf_sphere([0.0] * 3, 0.8), sdf_sphere([0.0] * 3, 0.35))
mesh = mesh_implicit_domain(shell, ([-1] * 3, [1] * 3), h=0.05, device="cuda")
# Differentiable geometry at fixed topology:
r = torch.tensor(0.8, requires_grad=True)
refit = refit_mesh_to_implicit(mesh.to("cpu"), lambda x: x.norm(dim=-1) - r)
The following signed-distance building blocks are provided for convenience:
sdf_sphere()sdf_box()sdf_polygon_2d()sdf_union()(CSG combinator)sdf_intersection()(CSG combinator)sdf_difference()(CSG combinator)project_to_zero_set()(Newton projection ontophi = 0)
Any callable with the signature phi(x: (..., d)) -> (...) also works.
Isosurface Extraction#
marching_cubes() extracts a triangle isosurface from a 3D scalar
field.
API Reference#
Mesh generation from implicit representations.
This module provides functions for generating meshes from scalar fields
and implicit functions. It supports isosurface extraction using marching
cubes and volume mesh generation for implicit domains in any spatial
dimension through mesh_implicit_domain. It also includes a
differentiable geometry refit (refit_mesh_to_implicit) and
signed-distance building blocks.