Skip to content

Class pylset_pre

Provides label maps, graph coloring of domains and the construction of signed distance fields with the 'EDT', 'FMM', 'FSM' and 'Brute' methods.

Bases: setup_base

Methods:

__init__

__init__(domain_size: ndarray, ndiv: ndarray, config: Dict[str, Any]) -> None

Initialize the pre-processing class.

Parameters:

Name Type Description Default
domain_size ndarray of float, shape (ndim,)

Physical size of the domain along each axis.

required
ndiv ndarray of int, shape (ndim,)

Number of grid points along each axis.

required
config dict

Configuration parameters as key-value pairs. See the pyLSET overview for the complete list.

required

do_coloring

do_coloring(labels: ndarray, separation: Optional[int] = None, immobile_ids: Optional[Sequence[int]] = None) -> Tuple[np.ndarray, np.ndarray]

Assign domains to a small number of level set fields by graph coloring.

Two domains may share a field only if their distance in the adjacency graph exceeds separation. With separation = 1 neighbors get different fields, with separation = 2 also neighbors of neighbors do, and so on. For mobile domains the distance is counted through mobile domains only, and mobile domains that face each other across an immobile region at less than about a band width count as neighbors. Mobile and immobile domains are colored separately so that they never share a field. Greedy coloring in order of decreasing degree is used.

Parameters:

Name Type Description Default
labels ndarray of int, shape ndiv

Label array.

required
separation int

Minimum graph distance between domains sharing a field minus one. Defaults to the configuration value coloring_separation.

None
immobile_ids sequence of int

IDs of immobile domains.

None

Returns:

Name Type Description
domain_ids ndarray of int

Sorted domain IDs.

domain_field ndarray of int

Field index for each domain. Fields of mobile domains come first.

Notes

During the evolution, topological changes can bring two domains of the same field close to each other, and they would merge if they came into contact. do_recoloring() detects such pairs and moves one of the two domains to another field. Call it at regular intervals. A larger separation detects the pairs earlier.

do_sdf

do_sdf(labels: ndarray, method: Optional[str] = None, band_width: Optional[float] = None, immobile_ids: Optional[Sequence[int]] = None, use_coloring: Optional[bool] = None, pinned_labels: Optional[ndarray] = None, exact_sdf: Optional[Dict[int, ndarray]] = None) -> np.ndarray

Generate signed distance fields from a pixel (2D) or voxel (3D) label array.

Each distinct value in labels defines one domain (grain, particle or other region). The returned fields are positive inside, zero on and negative outside the domain boundary. Values are taken at pixel or voxel centers and boundaries lie on the faces between pixels or voxels with different labels, so a voxel next to a boundary gets the value +h/2 or -h/2 for a boundary normal to a grid axis.

Calling this function also registers the domains (IDs, field assignment and mobile flags) in the simulation object.

Parameters:

Name Type Description Default
labels ndarray of int, shape ndiv

Domain ID of each pixel or voxel, for example a grain map from diffraction contrast tomography.

required
method str

'EDT' (exact Euclidean distance transform), 'FMM' (fast marching, requires scikit-fmm), 'FSM' (fast sweeping) or 'Brute' (exact distance to the extracted boundary, 2D only so far). Defaults to the configuration value sdf_method.

None
band_width float

Half width, in units of the largest grid spacing, beyond which values are clamped. Defaults to the configuration value band_width.

None
immobile_ids sequence of int

IDs of domains whose level sets are immobile, for example free surfaces, particles or inclusions.

None
use_coloring bool

Group domains that are not neighbors in shared fields (do_coloring()). Defaults to the configuration value use_coloring.

None
pinned_labels ndarray of int, shape ndiv

Continuation of the mobile domains into the immobile domains, for the 'Pinned' contact condition. Inside immobile domains, the mobile level sets are computed from these labels instead of labels, so that interfaces between mobile domains continue into the immobile domains and stay fixed where they meet them. Values outside immobile domains are not used.

None
exact_sdf dict of int to ndarray

Signed distance fields of shape ndiv, positive inside, for some of the domains, for example from do_surface_sdf() or from an analytic expression. They are used in place of the distance computed from the labels, which puts the boundary on the cell faces. This matters mainly for immobile domains with a condition of a scalar field on their surface. The labels still register the domains and should agree with the sign of each field, for example labels = np.where(sdf > 0, domain_id, labels). A warning is printed where they differ farther than one grid spacing from the surface.

None

Returns:

Name Type Description
phi ndarray of float, shape (n_fields, *ndiv)

Signed distance fields, one per field.

Raises:

Type Description
ValueError

If the shape of labels does not match the grid, or if exact_sdf holds a domain that is not in labels or a field of the wrong shape.

do_sphere_labels

do_sphere_labels(radius: float, center: Optional[Sequence[float]] = None, inside_id: int = 1, outside_id: int = 2) -> np.ndarray

Generate a label array with a disk (2D) or sphere (3D) embedded in a matrix.

Parameters:

Name Type Description Default
radius float

Radius of the disk or sphere.

required
center sequence of float

Center coordinates. Defaults to the center of the domain.

None
inside_id int

Label of the disk or sphere.

1
outside_id int

Label of the surrounding matrix.

2

Returns:

Name Type Description
labels ndarray of int, shape ndiv

Label array.

do_surface_sdf

do_surface_sdf(vertices: ndarray, cells: ndarray, band_width: Optional[float] = None) -> np.ndarray

Signed distance on the grid to a closed surface given as a mesh.

The surface is made of line segments in 2D or triangles in 3D, for example read by load_surface(). Within the band, the value at each grid point is the exact distance to the nearest segment or triangle. Outside the band it is plus or minus the band half width. The value is positive inside the surface. The inside is found by counting the crossings of the surface along the grid lines in each axis direction, so the orientation of the cells does not matter, and the result is the majority over the axes. A grid line that crosses the surface an odd number of times gives no result, and a point is outside when no axis gives a result. A surface that is not closed gives a warning. Small holes are thereby bridged by the other axes, while the inside of a surface with a large opening is not defined. Nested closed surfaces enclose the region between them, as in the even-odd rule. Repeated cells are removed with a warning, since they would cancel in the count.

The result can be given to do_sdf() through exact_sdf, for example for an immobile domain with a condition of a scalar field on its surface. Periodic axes wrap the surface around, so it may extend beyond the domain along them. Along other axes, parts of the surface outside the domain still count for the inside.

Parameters:

Name Type Description Default
vertices ndarray of float, shape (n_vertices, ndim)

Vertex coordinates, in the coordinates of the grid where the cell centers lie at (i + 1/2)*ddiv.

required
cells ndarray of int, shape (n_cells, ndim)

Vertex indices of the segments (2D) or triangles (3D).

required
band_width float

Half width, in units of the largest grid spacing, of the band in which the distance is computed. Defaults to the configuration value band_width.

None

Returns:

Name Type Description
phi ndarray of float, shape ndiv

Signed distance to the surface, positive inside.

Raises:

Type Description
ValueError

If the arrays have the wrong shape or no band width is set.

do_voronoi_labels

do_voronoi_labels(n_grains: int, seed: Optional[int] = None) -> np.ndarray

Generate a Voronoi polycrystal as a label array.

Seed points are placed at random. Periodic axes are handled by periodic copies of the seeds, so grains wrap across periodic boundaries.

Parameters:

Name Type Description Default
n_grains int

Number of grains.

required
seed int

Seed for the random number generator.

None

Returns:

Name Type Description
labels ndarray of int, shape ndiv

Grain IDs from 1 to n_grains.

get_band_width

get_band_width() -> Optional[float]

Get the band half width in units of the largest grid spacing.

Returns:

Name Type Description
band_width float or None

Band half width, or None if clamping is disabled.

get_domain_field

get_domain_field() -> np.ndarray

Get the index of the level set field that holds each domain.

Without coloring this is 0, 1, 2, .... With coloring, several domains share one field.

Returns:

Name Type Description
domain_field ndarray of int

Field index for each domain, in the order of get_domain_ids().

get_domain_ids

get_domain_ids() -> np.ndarray

Get the IDs of the domains registered by do_sdf().

Returns:

Name Type Description
domain_ids ndarray of int

One ID per domain.

get_label_adjacency

get_label_adjacency(labels: ndarray) -> np.ndarray

Find pairs of labels that touch, honoring the boundary conditions.

Two labels touch if they occupy neighboring pixels or voxels, including diagonal neighbors (8 neighbors in 2D and 26 in 3D). Diagonal contacts are included because level set fields of domains touching at a corner interact, so such domains must not share a field.

Parameters:

Name Type Description Default
labels ndarray of int, shape ndiv

Label array.

required

Returns:

Name Type Description
pairs ndarray of int, shape (n_pairs, 2)

Unique pairs (a, b) with a < b of labels that touch.

get_sdf_from_field

get_sdf_from_field(phi0: Union[ndarray, Tensor], method: str = 'FSM', band: Optional[float] = None) -> np.ndarray

Compute the signed distance function to the zero contour of a field.

The zero contour is located by linear interpolation between neighboring grid points, so subgrid information in phi0 is kept. This function is used both for initialization and for reinitialization.

Parameters:

Name Type Description Default
phi0 ndarray or torch.Tensor, shape ndiv

Field whose zero contour defines the interface. Positive inside.

required
method str

'FMM', 'FSM' or 'Brute'.

'FSM'
band float

Distance beyond which values are clamped. None gives full fields.

None

Returns:

Name Type Description
phi ndarray of float, shape ndiv

Signed distance function.

get_sdf_method

get_sdf_method() -> str

Get the default method used by do_sdf().

Returns:

Name Type Description
method str

Current default method.

set_band_width

set_band_width(band_width: Optional[float]) -> None

Set the half width of the band in which level sets are kept as signed distance functions.

Outside the band, level set values are clamped to plus or minus the band half width. None disables clamping.

Parameters:

Name Type Description Default
band_width float or None

Band half width in units of the largest grid spacing.

required

set_sdf_method

set_sdf_method(method: str) -> None

Set the default method used by do_sdf().

Parameters:

Name Type Description Default
method str

'EDT', 'FMM', 'FSM' or 'Brute'.

required