pde_opt.numerics.shapes

This module contains the Shape class, which is used to set up a geometry/shape for solving PDE on with smoothed boundary method.

Classes

Shape(binary[, refine_factor, refine_edge, ...])

Sets up a geometry/shape for solving PDE on with smoothed boundary method.

class pde_opt.numerics.shapes.Shape(binary: jax.Array, refine_factor: float | None = None, refine_edge: float | None = None, dx: Tuple[float, float] | None = (1.0, 1.0), smooth_epsilon: float = 1.0, smooth_curvature: float = 0.0, smooth_dt: float = 0.1, smooth_tf: float = 1.0)[source]

Sets up a geometry/shape for solving PDE on with smoothed boundary method.

The user creates a shape by providing a binary representation and an optional smoothing parameter.

binary: jax.Array
refine_factor: float | None = None
refine_edge: float | None = None
dx: Tuple[float, float] | None = (1.0, 1.0)
smooth_epsilon: float = 1.0
smooth_curvature: float = 0.0
smooth_dt: float = 0.1
smooth_tf: float = 1.0
refine_binary_mask() jax.Array[source]

Refine the binary mask by upsampling and adding edge padding.

Parameters:
  • refine_factor – Factor by which to upsample the binary mask (e.g., 2.0 for 2x upsampling)

  • refine_edge – Percentage to increase the edges (e.g., 0.5 for 50% increase)

Returns:

Refined binary mask with upsampling and edge padding applied

smooth_shape() jax.Array[source]

Smooths the shape using the Allen-Cahn equation with curvature minimization.

laplacian_from_mask(periodic: bool = False)[source]

Unnormalized graph Laplacian (4-neighbour) from a 0/1 mask. Nodes are entries where mask==1. Two nodes connect if they are up/down/left/right neighbours and both are 1.

Returns:

(n_nodes, n_nodes) CSR Laplacian ids: (H, W) array, node index in [0, n_nodes) or -1 if not a node

Return type:

L

get_shape_modes(N: int | None = None)[source]

Get the first N eigenvectors of the graph Laplacian of the binary mask.

Creates a graph where nodes are the 1-valued pixels, with edges between adjacent pixels (left, right, top, bottom neighbors).

Parameters:
  • N – Number of eigenvectors to return. If None, returns all eigenvectors.

  • downsampling_factor – If provided, downsample binary by this factor before computing modes, then upsample results back to original size. This can significantly reduce memory usage and computation time for large binary masks.

Returns:

Array of shape (num_nodes, N) containing the first N eigenvectors

__init__(binary: jax.Array, refine_factor: float | None = None, refine_edge: float | None = None, dx: Tuple[float, float] | None = (1.0, 1.0), smooth_epsilon: float = 1.0, smooth_curvature: float = 0.0, smooth_dt: float = 0.1, smooth_tf: float = 1.0) None