Volume meshes with Meshers

Install the optional backend with pip install 'microgen[meshers]'. These examples export tetrahedral volume meshes as VTU files, retaining periodic node pairs and boundary facets.

For a Tpms with a constant offset, the mesh is periodic on the three axes by default, and geometry_tolerance is relative to the smallest cell_size (pass geometry_tolerance_mode="absolute" for a tolerance in length units). With density, the offset is fitted on the VTK grid, so the meshed volume fraction can differ from density by about 1 %; a larger algo_resolution reduces the gap.

The uniform Gyroid unit cell is periodic along all three axes:

"""Generate a periodic Gyroid volume mesh with microgen[meshers]."""

from pathlib import Path

from microgen import Tpms
from microgen.shape.surface_functions import gyroid

geometry = Tpms(surface_function=gyroid, offset=1.1)
mesh = geometry.generate_volume_mesh(
    backend="meshers",
    cells=24,
    order=1,
    geometry_tolerance=0.002,
    periodic=(True, True, True),
    threads=1,
)
mesh.save(Path(__file__).parent / "gyroid_meshers.vtu")
print(f"Gyroid: {mesh.n_cells} tetrahedra, {mesh.n_points} nodes")

A pointwise callable can grade the thickness offset. This example varies the offset along z, so only x and y remain periodic. A linear offset does not imply a linear density gradient. The callable must use operations supported by the Meshers expression compiler; sampled arrays and OffsetGrading objects are not supported by this backend.

"""Generate a Gyroid with callable thickness grading using microgen[meshers]."""

from pathlib import Path

from microgen import Tpms
from microgen.shape.surface_functions import gyroid


def thickness_offset(x, y, z):
    """Vary the offset from 0.9 to 1.3 over the unit cell's z extent."""
    return 1.1 + 0.4 * z


geometry = Tpms(surface_function=gyroid, offset=thickness_offset)
mesh = geometry.generate_volume_mesh(
    backend="meshers",
    cells=24,
    order=1,
    geometry_tolerance=0.002,
    periodic=(True, True, False),  # Thickness varies along z, so z is not periodic.
    threads=1,
)
mesh.save(Path(__file__).parent / "graded_gyroid_meshers.vtu")
print(f"Graded Gyroid: {mesh.n_cells} tetrahedra, {mesh.n_points} nodes")