Skip to content

pyLSET logo

Quick Start Guide

The Level Set Representation

Every domain, for example a grain, a particle or a region bounded by a free surface, is represented by a level set function \phi. It is a signed distance function that is positive inside the domain, zero on its boundary and negative outside. Interfaces, such as grain boundaries, are the zero contours of these functions.

Isotropic interface motion with interface mobility \mu and interface energy \gamma follows

\[ \partial_t\phi = \mu\gamma\,|\nabla\phi|\,\nabla\cdot\frac{\nabla\phi}{|\nabla\phi|}, \]

which reduces to \partial_t\phi = \mu\gamma\Delta\phi while \phi is a signed distance function.

Setting Up the Geometry

The level sets are built by do_sdf() from a label array with one domain ID per grid point, for example a Voronoi tessellation or a grain map from tomography. Domains of a known shape, such as particles or parts from a CAD program, can instead be read from STL, VTK, Gmsh and CAD files with load_surface(). do_surface_sdf() then computes the exact signed distance to the surface, which do_sdf() uses in place of the distance found from the labels. This matters mainly for immobile domains that carry a condition of a scalar field on their surface. See the Quick Start and Geometry from Surface Files.

A Time Step

A time step in pyLSET consists of two parts.

  1. do_step_update() evolves all mobile level sets, every step.
  2. At intervals chosen by the user, typically every 10th or 20th step, do_reinitialization() restores the signed distance property, do_interaction_correction() removes voids and overlaps between neighboring level sets and do_reinitialization() is called again. With coloring, do_recoloring() and, with property functions, do_property_update() are called at the same interval.

A First Example

The following script generates a 2D Voronoi polycrystal, builds signed distance fields for its grains and evolves it by curvature-driven grain growth.

import pylset

# Simulation object, 2D since ndiv has two entries
sim = pylset.setup_simulation([128.0, 128.0], [128, 128],
                              device_type='CPU', bc_x='Periodic', bc_y='Mirror',
                              use_coloring=True)

# Label array with one grain ID per pixel, for example a grain map from experiments
labels = sim.do_voronoi_labels(60, seed=7)

# Signed distance fields, one per field (several grains per field with coloring)
phi = sim.do_sdf(labels, method='EDT')
sim.set_phi(phi)

# Time stepping
for step in range(1, 2001):
    sim.do_step_update()
    if step % 20 == 0:
        sim.do_reinitialization()      # the correction needs signed distance functions
        sim.do_interaction_correction()
        sim.do_reinitialization()
        sim.do_recoloring()            # keeps grains sharing a field from merging

# Output for ParaView and in HDF5 format
sim.write_vtk_image('grains', [sim.get_phi_max(), sim.get_domain_map()], ['phi_max', 'grain_id'])
sim.write_vtk_interfaces('grain_boundaries')
sim.save_hdf5('grains')

Domains whose level sets should not evolve, such as particles or free surfaces, are given with immobile_ids in do_sdf().

The shape of such a domain can also be read from a surface file with load_surface(), which returns line segments in 2D and triangles in 3D. do_surface_sdf() computes the exact signed distance to the surface, which is positive inside, and do_sdf() uses it in place of the distance found from the labels. In the script above, the call to do_sdf() is then replaced by

import numpy as np
V, C = sim.load_surface('particle.geo')      # STL, VTK, Gmsh and CAD files
sdf = sim.do_surface_sdf(V, C)
labels = np.where(sdf > 0, 99, labels)       # the particle gets ID 99
phi = sim.do_sdf(labels, method='EDT', immobile_ids=[99], exact_sdf={99: sdf})

The supported formats are listed in Geometry from Surface Files.

Interface energy and mobility can depend on the two domains and the interface normal.

import numpy as np
rng = np.random.default_rng(1)
orient = rng.uniform(0.0, np.pi / 2.0, size=len(sim.get_domain_ids()))
sim.set_domain_orientations(orient)

def energy(a, b, n):
    theta = sim.get_misorientation_angle(sim.get_domain_orientations(a), sim.get_domain_orientations(b), symmetry=4)
    return sim.get_read_shockley_energy(theta, energy_min=0.1)

sim.set_interface_energy(energy)   # evaluated by do_property_update(), call it with the reinitialization