Tutorials

This page provides comprehensive tutorials for all Microgen features.

Basic Shapes

Microgen provides several basic geometric shapes that can be used as building blocks.

Box

import microgen

box = microgen.Box(
    center=(0, 0, 0),
    dim=(1.0, 0.6, 0.4),
    orientation=(0, 0, 0)
)
box.generate_vtk().plot(color='white')
_images/tutorials_1_0.png

Sphere

sphere = microgen.Sphere(
    center=(0, 0, 0),
    radius=0.5
)
sphere.generate_vtk().plot(color='white')
_images/tutorials_2_0.png

Cylinder

cylinder = microgen.Cylinder(
    center=(0, 0, 0),
    height=1.0,
    radius=0.3,
    orientation=(0, 0, 0)
)
cylinder.generate_vtk().plot(color='white')
_images/tutorials_3_0.png

Ellipsoid

ellipsoid = microgen.Ellipsoid(
    center=(0, 0, 0),
    radii=(0.5, 0.3, 0.2),
    orientation=(0, 0, 0)
)
ellipsoid.generate_vtk().plot(color='white')
_images/tutorials_4_0.png

Capsule

capsule = microgen.Capsule(
    center=(0, 0, 0),
    height=1.0,
    radius=0.2,
    orientation=(0, 0, 0)
)
capsule.generate_vtk().plot(color='white')
_images/tutorials_5_0.png

Extruded Polygon

# Define polygon corners
corners = [(0, 0), (1, 0), (1, 1), (0.5, 1.5), (0, 1)]

polygon = microgen.ExtrudedPolygon(
    center=(0, 0, 0),
    listCorners=corners,
    height=0.5,
    orientation=(0, 0, 0)
)
polygon.generate_vtk().plot(color='white')
_images/tutorials_6_0.png

TPMS Surfaces

Microgen supports many TPMS (Triply Periodic Minimal Surfaces) functions.

Available Surface Functions

The following TPMS surface functions are available in microgen.surface_functions:

3D TPMS Functions

Function

Description

gyroid

Gyroid surface

schwarz_p

Schwarz P surface

schwarz_d

Schwarz D surface

neovius

Neovius surface

schoen_iwp

Schoen IWP surface

schoen_frd

Schoen FRD surface

fischer_koch_s

Fischer-Koch S surface

pmy

PMY surface

honeycomb

Honeycomb surface

lidinoid

Lidinoid surface

split_p

Split P surface

2D Honeycomb Functions

Function

Description

honeycomb_gyroid

2D Honeycomb Gyroid

honeycomb_schwarz_p

2D Honeycomb Schwarz P

honeycomb_schwarz_d

2D Honeycomb Schwarz D

honeycomb_schoen_iwp

2D Honeycomb Schoen IWP

honeycomb_lidinoid

2D Honeycomb Lidinoid

TPMS Examples

Schwarz P:

schwarz_p = microgen.Tpms(
    surface_function=microgen.surface_functions.schwarz_p,
    offset=0.3,
    cell_size=1.0,
    repeat_cell=2,
    resolution=20
)
schwarz_p.sheet.plot(color='white')
_images/tutorials_7_0.png

Schwarz D:

schwarz_d = microgen.Tpms(
    surface_function=microgen.surface_functions.schwarz_d,
    offset=0.3,
    cell_size=1.0,
    repeat_cell=2,
    resolution=20
)
schwarz_d.sheet.plot(color='white')
_images/tutorials_8_0.png

Neovius:

neovius = microgen.Tpms(
    surface_function=microgen.surface_functions.neovius,
    offset=0.3,
    cell_size=1.0,
    repeat_cell=2,
    resolution=20
)
neovius.sheet.plot(color='white')
_images/tutorials_9_0.png

Using Density Instead of Offset

You can specify a target density instead of an offset value:

gyroid_50 = microgen.Tpms(
    surface_function=microgen.surface_functions.gyroid,
    density=0.5,  # 50% density
    cell_size=1.0,
    repeat_cell=2,
    resolution=20
)
gyroid_50.sheet.plot(color='white')
_images/tutorials_10_0.png

Spherical TPMS

Create TPMS on a spherical coordinate system:

spherical_gyroid = microgen.SphericalTpms(
    radius=2.0,
    surface_function=microgen.surface_functions.gyroid,
    offset=0.3,
    cell_size=0.5,
    repeat_cell=(3, 0, 0),  # 0 = auto-fill to complete sphere
    resolution=20
)
spherical_gyroid.sheet.plot(color='white')
_images/tutorials_11_2.png

Cylindrical TPMS

Create TPMS on a cylindrical coordinate system:

cylindrical_gyroid = microgen.CylindricalTpms(
    radius=1.5,
    surface_function=microgen.surface_functions.gyroid,
    offset=0.3,
    cell_size=0.5,
    repeat_cell=(2, 0, 3),  # 0 = auto-fill circumference
    resolution=20
)
cylindrical_gyroid.sheet.plot(color='white')
_images/tutorials_12_1.png

3D Operations

Repeat

Repeat a shape in a grid pattern:

import microgen
import pyvista as pv
import cadquery as cq

# Create a sphere
sphere = microgen.Sphere(center=(0, 0, 0), radius=0.15)
sphere_cad = sphere.generate()

# Create RVE (Representative Volume Element)
rve = microgen.Rve(dim=(0.5, 0.5, 0.5))

# Repeat the sphere in a 3x3x3 grid
repeated = microgen.repeat_shape(
    unit_geom=sphere_cad,
    rve=rve,
    grid=(3, 3, 3)
)

# Export to STL
cq.exporters.export(repeated, "repeated.stl")

# Visualize
plotter = pv.Plotter()
plotter.add_mesh(pv.read("repeated.stl"))
plotter.show()
_images/tutorials_13_0.png

Raster

Raster a phase to create a periodic pattern:

import cadquery as cq

# Create ellipsoid and convert to CadQuery shape
ellipsoid = microgen.Ellipsoid(
    center=(0, 0, 0),
    radii=(0.15, 0.1, 0.08)
)
ellipsoid_cad = ellipsoid.generate()
phase = microgen.Phase(shape=ellipsoid_cad)

# Define RVE
rve = microgen.Rve(dim=(0.5, 0.5, 0.5))

# Raster the phase
rastered = microgen.raster_phase(
    phase=phase,
    rve=rve,
    grid=(2, 2, 2)
)

# Create compound from all solids and export to STL
compound = cq.Compound.makeCompound(
    [solid for p in rastered for solid in p.solids]
)
cq.exporters.export(compound, "rastered.stl")

# Visualize
plotter = pv.Plotter()
plotter.add_mesh(pv.read("rastered.stl"), color='white')
plotter.show()
_images/tutorials_14_0.png

Meshing

Regular Mesh with Gmsh

Generate a tetrahedral mesh using Gmsh:

import microgen
import cadquery as cq

# Create a TPMS geometry
gyroid = microgen.Tpms(
    surface_function=microgen.surface_functions.gyroid,
    offset=0.3,
    resolution=20
)
shape = gyroid.generate(type_part='sheet')

# Export to STEP file first
cq.exporters.export(cq.Workplane().add(shape), 'gyroid.step')

# Create mesh from STEP file
microgen.mesh(
    mesh_file='gyroid.step',
    size=0.1,
    order=1,
    output_file='gyroid_mesh.msh'
)

Periodic Mesh

Generate a periodic mesh suitable for homogenization:

import cadquery as cq

# Create geometry
gyroid = microgen.Tpms(
    surface_function=microgen.surface_functions.gyroid,
    offset=0.3,
    cell_size=1.0,
    resolution=20
)
shape = gyroid.generate(type_part='sheet')

# Create a phase from the shape
phase = microgen.Phase(shape=shape)

# Export to STEP file first
cq.exporters.export(cq.Workplane().add(shape), 'gyroid.step')

# Create periodic mesh
microgen.mesh_periodic(
    mesh_file='gyroid.step',
    rve=microgen.Rve(dim=(1, 1, 1)),
    list_phases=[phase],
    size=0.05,
    order=1,
    output_file='gyroid_periodic.msh'
)

Remeshing with Mmg

Refine or adapt an existing mesh using Mmg:

Note

Mmg must be installed separately to use remeshing features.

# Use Mmg for remeshing
mmg = microgen.Mmg(
    input_mesh='input.mesh',
    output_mesh='output.mesh',
    hausd=0.01,  # Hausdorff distance
    hmin=0.01,   # Minimum edge size
    hmax=0.1     # Maximum edge size
)
mmg.run()

Further Resources