from __future__ import annotations
import math
from typing import Any
import numpy as np
from qewton.config.variables import Variable
from qewton.geometries.base import Geometry, DiscreteGeometry, BoundaryGeometry, Structure
from qewton.geometries.discrete.mesh import Mesh
from qewton.backends.base import TensorType, ComputingBackend
from qewton.backends import DEFAULT_DL_BACKEND
from qewton.config.devices import Device, cpu
from qewton.config.dtypes import Bool, Int32
[docs]
class MeshGeometry(DiscreteGeometry[TensorType]):
"""A geometry object representing a simplex mesh.
Args:
variable (Variable): The variable connected to this geometry.
mesh (Mesh): The underlying mesh object.
discretization_of (Geometry | None, optional): The geometry this
mesh is a discretization of. Defaults to None.
backend (type[ComputingBackend[TensorType]], optional):
Defaults to DEFAULT_DL_BACKEND.
"""
def __init__(
self,
variable: Variable,
mesh: Mesh,
discretization_of: Geometry | None = None,
backend: type[ComputingBackend[TensorType]] = DEFAULT_DL_BACKEND,
):
assert (
len(mesh.vertices[0]) == variable.dim
), "Dimension of variable must match dimension of mesh vertices."
self.mesh = mesh
super().__init__(
variable=variable,
shape=(mesh.vertices.shape[0],),
discretization_points=mesh.vertices,
backend=backend,
)
if discretization_of is not None:
self.discretization_of = discretization_of
# For checking points inside the mesh:
self.inv_A: TensorType | None = None # Inverse matrix for bary. coords.
self.v0: TensorType | None = None # Origin of each simplex
self.bbox_min: TensorType | None = None # bounding box of each simplex
self.bbox_max: TensorType | None = None
self.contains_tol = 1.0e-5
[docs]
@classmethod
def load_mesh(
cls,
variable: Variable,
file_path: str,
backend: type[ComputingBackend[TensorType]] = DEFAULT_DL_BACKEND,
prune_z: bool = False,
) -> MeshGeometry:
"""Loads a *volume* mesh from a path.
Args:
variable (Variable): The variable connected to this geometry.
file_path (str): The path to the mesh file. This uses the meshio library
load the mesh. Supported formats are .msh/.vtk/.vtu/.xdmf/.inp and
more, see the meshio documentation.
backend (type[ComputingBackend[TensorType]], optional):
Defaults to DEFAULT_DL_BACKEND.
prune_z (bool, optional): If True, the z-coordinate of the mesh will be
removed. This is useful for 2D meshes that are stored in 3D.
Returns:
MeshGeometry: _description_
"""
return cls(
variable=variable,
mesh=Mesh.load_mesh(file_path, backend=backend, prune_z=prune_z),
backend=backend,
)
def __and__(self, other):
raise NotImplementedError("Mesh combinations are not supported yet.")
def __sub__(self, other):
raise NotImplementedError("Mesh combinations are not supported yet.")
[docs]
def create_mesh(
self, max_vertex_distance: float | None = None, device: Device = cpu
) -> MeshGeometry:
return self # TODO: max_vertex_distance is currently ignored
@property
def structure(self) -> Structure:
return Structure.MESH
@property
def topological_dim(self) -> int:
# A simplex with k vertices has dimension k - 1.
return self.mesh.cells.shape[1] - 1
@property
def cells(self) -> np.ndarray:
return np.asarray(self.backend.to_numpy(self.mesh.cells))
[docs]
def bounding_box(self):
bounding_box = []
for i in range(self.variable.dim):
min_val = self.backend.math.min(self.mesh.vertices[:, i])
max_val = self.backend.math.max(self.mesh.vertices[:, i])
bounding_box.append(min_val)
bounding_box.append(max_val)
return self._bounds(bounding_box)
def _get_volume(self):
cell_volumes = self.mesh.compute_cell_volumes()
return self.backend.math.sum(cell_volumes)
[docs]
def get_marker(self, marker):
return self.get_submesh(marker=marker)
[docs]
def mesh_info(self):
"""Print out some general information about the mesh."""
print("Number of vertices:", self.mesh.vertex_count)
print("Number of cells:", len(self.mesh.cells))
if len(self.mesh.marker_labels) > 0:
print("The mesh has the markers:", self.mesh.marker_labels)
elif self.mesh.cell_markers is not None:
print(
"The mesh has the markers:",
self.backend.math.unique(self.mesh.cell_markers),
)
else:
print("No markers are known in the mesh.")
@property
def boundary(self) -> MeshBoundaryGeometry:
return super().boundary # type: ignore
[docs]
def create_boundary(self) -> MeshBoundaryGeometry:
return MeshBoundaryGeometry(self)
def _move_data(self, device: Device | str) -> None:
self.mesh.to(device)
self.discretization_points = self.mesh.vertices
for name in ("bbox_min", "bbox_max", "v0", "inv_A"):
if getattr(self, name) is not None:
setattr(self, name, self.backend.to(getattr(self, name), device=device))
[docs]
def sample_grid_from_discretization(
self, n_points: int, device: Device | str = cpu
) -> TensorType:
points = self.mesh.sample_grid_from_vertices(n_points, device=self.device)[0]
return self._transfer(points, device)
[docs]
def sample_grid(self, n_points: int, device: Device | str = cpu) -> TensorType:
return self._transfer(self._sample_grid_here(n_points), device)
def _sample_grid_here(self, n_points: int) -> TensorType:
"""Grid points on this geometry's own device."""
device = self.device
mins = self.bounding_box()[::2]
maxs = self.bounding_box()[1::2]
dim = len(mins)
bbox_lengths = maxs - mins
bbox_volume = self.backend.math.prod(bbox_lengths)
# Sample first in bounding box
scaled_points = int(math.ceil(bbox_volume / self.volume() * n_points))
# Resolution per axis proportional to side length
points_per_axis = self.backend.math.clip(
self.backend.math.floor(
scaled_points ** (1 / dim) * bbox_lengths / (bbox_volume ** (1 / dim))
),
2,
1.0e8,
)
points_per_axis = self.backend.cast_dtype(points_per_axis, Int32)
axes = [
self.backend.math.linspace(lo, hi, n, device=device)
for lo, hi, n in zip(mins, maxs, points_per_axis)
]
meshgrid = self.backend.math.meshgrid(*axes, indexing="ij")
grid_points = self.backend.math.stack(
[g.ravel() for g in meshgrid],
axis=-1,
)
# Check what points are inside
grid_points = grid_points[self._locate_here(grid_points)[2]]
# Add random points
missing_n = n_points - len(grid_points)
if missing_n > 0:
random_points = self.mesh.sample_random_inside(missing_n, device=device)[0]
grid_points = self.backend.math.concatenate(
[grid_points, random_points],
axis=0,
)
elif missing_n < 0: # or remove some if we have to many
idx = self.backend.random.permutation(
self.backend.math.arange(len(grid_points)), device=device
)[:n_points]
grid_points = grid_points[idx]
return grid_points
[docs]
def contains(self, points):
_, _, found = self.locate(points)
return found
[docs]
def interpolate_to(self, points, values):
"""Interpolates a per-vertex field onto arbitrary target points, via
linear (barycentric) interpolation inside whichever simplex contains
each point. Generic over the points, not plane-specific - a plane
slice is just one choice of `points`, a regular 3D resampling grid or
a scattered comparison set are others equally valid.
Args:
points: Target points, shape (n_points, dim).
values: Per-vertex field, shape (n_vertices,) or
(n_vertices, *feature_shape).
Returns:
Interpolated values, shape (n_points,) or (n_points, *feature_shape)
matching `values`'s trailing shape. Points outside the mesh are
NaN rather than extrapolated, so callers can render/ignore them as
missing (e.g. Scale.observe() already uses nanmin/nanmax).
"""
points_device = self._device_of(points)
cell_idx, weights, found = self._locate_here(self._transfer(points, self.device))
values = self._transfer(values, self.device)
vertex_idx = self.mesh.cells[cell_idx] # (n_points, dim + 1)
vertex_values = values[vertex_idx] # (n_points, dim + 1, *feature_shape)
w = weights
while w.ndim < vertex_values.ndim:
w = w[..., None]
interpolated = self.backend.math.sum(w * vertex_values, axis=1)
not_found = ~found
while not_found.ndim < interpolated.ndim:
not_found = not_found[..., None]
nan = self.backend.build_tensor(float("nan"))
return self._transfer(
self.backend.math.where(not_found, nan, interpolated), points_device, warn=False
)
[docs]
def locate(self, points):
"""For each point, finds a containing simplex and its barycentric
weights - the same search `contains()` does, but keeping the cell
index and weights it computes along the way instead of collapsing
them to a boolean. Used by interpolation onto arbitrary target points
(e.g. a plane slice), where those weights double as the interpolation
weights - no separate lookup needed.
Args:
points: Points to locate, shape (n_points, dim).
Returns:
cell_idx: int array (n_points,) - index into self.mesh.cells of a
simplex containing each point. Arbitrary (0) where not found.
weights: float array (n_points, dim + 1) - barycentric weights
against self.mesh.cells[cell_idx[i]]'s vertices, in vertex
order (v0 first). Rows for points not found are all zero.
found: bool array (n_points,) - whether a containing simplex was
found for each point.
"""
points_device = self._device_of(points)
results = self._locate_here(self._transfer(points, self.device))
return tuple(self._transfer(r, points_device, warn=False) for r in results)
def _locate_here(self, points):
""":meth:`locate` for points on this geometry's own device."""
self._build_barycentric_cache()
if len(points) < len(self.mesh.cells):
return self._locate_point_based(points)
return self._locate_cell_based(points)
def _build_barycentric_cache(self):
if self.inv_A is None or self.v0 is None:
vertices = self.mesh.vertices[self.mesh.cells]
self.bbox_min = self.backend.math.min(vertices, axis=1)
self.bbox_max = self.backend.math.max(vertices, axis=1)
self.v0 = vertices[:, 0]
mat_A = vertices[:, 1:] - self.v0[:, None] # type: ignore
self.inv_A = self.backend.linalg.inv(mat_A)
def _locate_point_based(self, points):
device = self.device
n_points = len(points)
dim = self.variable.dim
cell_idx = self.backend.math.zeros(
n_points, dtype=self.backend.dtypes[Int32], device=device
)
weights = self.backend.math.zeros((n_points, dim + 1), device=device) # type: ignore
found = self.backend.math.zeros(
n_points, dtype=self.backend.dtypes[Bool], device=device
)
for i, p in enumerate(points):
candidates = self.backend.math.where(
self.backend.math.all(p >= self.bbox_min, axis=1)
& self.backend.math.all(p <= self.bbox_max, axis=1)
)[0]
for cell in candidates:
u = (p - self.v0[cell]) @ self.inv_A[cell] # type: ignore
l0 = 1.0 - u.sum()
if l0 >= -self.contains_tol and self.backend.math.all(
u >= -self.contains_tol
):
found[i] = True
cell_idx[i] = cell
weights[i, 0] = l0
weights[i, 1:] = u
break
return cell_idx, weights, found
def _locate_cell_based(self, points):
device = self.device
n_points = len(points)
dim = self.variable.dim
cell_idx = self.backend.math.zeros(
n_points, dtype=self.backend.dtypes[Int32], device=device
)
weights = self.backend.math.zeros((n_points, dim + 1), device=device) # type: ignore
found = self.backend.math.zeros(
n_points, dtype=self.backend.dtypes[Bool], device=device
)
for cell in range(len(self.mesh.cells)):
# bbox filter
mask = (
~found
& self.backend.math.all(
points >= self.bbox_min[cell], axis=1 # type: ignore
)
& self.backend.math.all(
points <= self.bbox_max[cell], axis=1 # type: ignore
)
)
idx = self.backend.math.where(mask)[0]
if len(idx) == 0:
continue
# barycentric test
u = (points[idx] - self.v0[cell]) @ self.inv_A[cell] # type: ignore
l0 = 1.0 - self.backend.math.sum(u, axis=1)
bary_mask = self.backend.math.logical_and(
l0 >= -self.contains_tol,
self.backend.math.all(u >= -self.contains_tol, axis=1),
)
good = idx[bary_mask]
found[good] = True
cell_idx[good] = cell
weights[good, 0] = l0[bary_mask]
weights[good, 1:] = u[bary_mask]
return cell_idx, weights, found
[docs]
def get_submesh(self, marker: int | str) -> MeshGeometry:
"""Returns a submesh of the main mesh that only contains the provided
marker.
Args:
marker (int | str): The marker of the submesh
Returns:
MeshGeometry: A new mesh geometry that only consists of the
mesh with the marker.
"""
return MeshGeometry(
variable=self.variable,
mesh=self.mesh.get_submesh(marker),
discretization_of=self.discretization_of,
backend=self.backend,
)
[docs]
class MeshBoundaryGeometry(BoundaryGeometry[TensorType]):
def __init__(self, geometry: MeshGeometry):
assert isinstance(geometry, MeshGeometry)
super().__init__(geometry)
self.mesh = geometry.mesh.get_boundary_mesh()
self.geometry: MeshGeometry = geometry # type: ignore
self.face_bbox_min: TensorType | None = None # bounding box of each face
self.face_bbox_max: TensorType | None = None
self.v0: TensorType | None = None # Origin of each face simplex
def _move_data(self, device: Device | str) -> None:
self.mesh.to(device)
for name in ("face_bbox_min", "face_bbox_max", "v0"):
if getattr(self, name) is not None:
setattr(self, name, self.backend.to(getattr(self, name), device=device))
[docs]
def bounding_box(self):
return self.geometry.bounding_box()
[docs]
def contains(self, points):
points_device = self._device_of(points)
inside, _ = self._search_here(self._transfer(points, self.device))
return self._transfer(inside, points_device, warn=False)
def _search_here(self, points):
"""The boundary faces containing `points` (on this geometry's own
device): ``(point_inside, cell_idx)``."""
self._build_face_bbox()
if len(points) < len(self.mesh.cells):
return self._contains_point_based_search(points)
return self._contains_cell_based_search(points)
[docs]
def create_mesh(
self, max_vertex_distance: float | None = None, device: Device = cpu
) -> MeshBoundaryGeometry:
return self
def _contains_point_based_search(self, points):
point_inside = self.backend.math.zeros(
len(points), dtype=self.backend.dtypes[Bool], device=self.device
)
cell_idx = self.backend.math.zeros(
len(points), dtype=self.backend.dtypes[Int32], device=self.device
)
for i, p in enumerate(points):
# Check if points are close to face
candidates = self.backend.math.where(
self.backend.math.all(p >= self.face_bbox_min, axis=1)
& self.backend.math.all(p <= self.face_bbox_max, axis=1)
)
# Do concrete distance check via normal computation
for cell in candidates[0]:
cell_mapped = self.mesh.map_cells_to_original(cell)
dist = self.backend.math.dot(
p - self.v0[cell], # type: ignore
self.geometry.mesh.boundary_normals[cell_mapped],
)
if abs(dist) <= self.geometry.contains_tol:
point_inside[i] = True
cell_idx[i] = cell
break
return point_inside, cell_idx
def _contains_cell_based_search(self, points):
point_inside = self.backend.math.zeros(
len(points), dtype=self.backend.dtypes[Bool], device=self.device
)
cell_idx = self.backend.math.zeros(
len(points), dtype=self.backend.dtypes[Int32], device=self.device
)
for cell in range(len(self.mesh.cells)):
mask = (
~point_inside
& self.backend.math.all(
points >= self.face_bbox_min[cell], axis=1 # type: ignore
)
& self.backend.math.all(
points <= self.face_bbox_max[cell], axis=1 # type: ignore
)
)
idx = self.backend.math.where(mask)[0]
if len(idx) == 0:
continue
cell_mapped = self.mesh.map_cells_to_original(cell)
distance = self.backend.math.dot(
(points[idx] - self.v0[cell]), # type: ignore
self.geometry.mesh.boundary_normals[cell_mapped],
)
bary_mask = self.backend.math.abs(distance) <= self.geometry.contains_tol
point_inside[idx[bary_mask]] = True
cell_idx[idx[bary_mask]] = cell
return point_inside, cell_idx
def __and__(self, other):
raise NotImplementedError("Mesh combinations are not supported yet.")
def __sub__(self, other):
raise NotImplementedError("Mesh combinations are not supported yet.")
def _get_volume(self):
cell_volumes = self.mesh.compute_cell_volumes()
return self.backend.math.sum(cell_volumes)
[docs]
def sample_grid_from_discretization(
self, n_points: int, device: Device | str = cpu, include_normals: bool = False
):
points, idx = self.mesh.sample_grid_from_vertices(n_points, device=self.device)
if include_normals:
normals = self.geometry.mesh.boundary_normals_at_vertex[idx]
return self._transfer(points, device), self._transfer(normals, device, warn=False)
return self._transfer(points, device)
[docs]
def sample_grid(
self, n_points: int, device: Device | str = cpu, include_normals: bool = False
):
result = self._sample_grid_here(n_points, include_normals)
if include_normals:
points, normals = result
return self._transfer(points, device), self._transfer(normals, device, warn=False)
return self._transfer(result, device)
def _sample_grid_here(self, n_points: int, include_normals: bool):
"""Grid points (and normals) on this geometry's own device."""
device = self.device
# Allocate points based on area:
n_areas, local_n = self._compute_local_distribution(n_points)
# Build grid over all faces
all_points = []
face_idx = []
face_vertices = self.mesh.vertices[self.mesh.cells]
if face_vertices.shape[1] == 3:
grid_fn = self._face_grid
elif face_vertices.shape[1] == 2:
grid_fn = self._line_grid
else:
raise NotImplementedError(
f"No boundary sampling implemented for dimension {face_vertices.shape[1]}"
)
for area_counter in range(n_areas):
new_points = grid_fn(local_n[area_counter], face_vertices[area_counter])
all_points.extend(new_points)
face_idx.extend([area_counter] * len(new_points))
points = self.backend.math.vstack(all_points)
face_idx = self.backend.build_tensor(
face_idx, dtype=self.backend.dtypes[Int32], device=device
)
# Check how many points we have and either cut them or add some more
random_normals = None
if len(points) > n_points:
idx = self.backend.random.permutation(
self.backend.math.arange(len(points)), device=device
)[:n_points]
points = points[idx]
face_idx = face_idx[idx]
elif len(points) < n_points:
missing_n = n_points - len(points)
random = self.sample_random_uniform(
missing_n, include_normals=include_normals, device=device
)
random_points, random_normals = random if include_normals else (random, None)
points = self.backend.math.concatenate([points, random_points], axis=0)
if include_normals:
face_idx = self.mesh.map_cells_to_original(face_idx)
normals = self.geometry.mesh.boundary_normals[face_idx]
if random_normals is not None:
normals = self.backend.math.concatenate([normals, random_normals], axis=0)
return points, normals
return points
def _compute_local_distribution(self, n_points):
total_area = self.volume()
local_area: TensorType = self.mesh.cell_volumes # type: ignore
n_areas = len(local_area)
local_n = self.backend.math.maximum(
self.backend.build_tensor(1),
self.backend.math.round(local_area / total_area * n_points), # type: ignore
)
local_n = self.backend.cast_dtype(local_n, Int32)
# check if we have enough points or not:
diff = n_points - self.backend.math.sum(local_n)
if diff != 0:
# Fix number by adding or removing points, starting from the biggest area
idx = self.backend.math.argsort(local_area)
idx = self.backend.math.flip(idx, axis=0)
for i in range(abs(diff)):
local_n[idx[i % n_areas]] += self.backend.math.sign(diff)
return n_areas, local_n
def _face_grid(self, n_points: int, face_vertices):
n_i = int(math.ceil(math.sqrt(2 * n_points)))
all_points = []
for i in range(n_i):
for j in range(n_i - i):
u = (i + 1) / (n_i + 2) # dont include 1 and 0
v = (j + 1) / (n_i + 2)
w = 1 - u - v
point = u * face_vertices[0] + v * face_vertices[1] + w * face_vertices[2]
all_points.append(point)
return all_points
def _line_grid(self, n_points: int, face_vertices):
all_points = []
for i in range(n_points):
u = (i + 1) / (n_points + 2)
v = 1 - u
point = u * face_vertices[0] + v * face_vertices[1]
all_points.append(point)
return all_points
def _build_face_bbox(self):
if self.face_bbox_min is None or self.face_bbox_max is None:
vertices = self.mesh.vertices[self.mesh.cells]
self.v0 = vertices[:, 0]
self.face_bbox_min = (
self.backend.math.min(vertices, axis=1) - self.geometry.contains_tol
)
self.face_bbox_max = (
self.backend.math.max(vertices, axis=1) + self.geometry.contains_tol
)
[docs]
def normal(self, points, device: Device | str = cpu):
points_device = self._device_of(points)
points = self._transfer(points, self.device)
point_found, cell_idx = self._search_here(points)
normals = self.backend.math.zeros_like(points)
mapped_idx = self.mesh.map_cells_to_original(cell_idx[point_found])
normals[point_found] = self.geometry.mesh.boundary_normals[mapped_idx]
return self._transfer(normals, points_device, warn=False)
[docs]
def get_submesh(self, marker: int | str) -> MeshBoundaryGeometry:
sub_mesh_geo = MeshBoundaryGeometry(self.geometry)
sub_mesh_geo.mesh = self.mesh.get_submesh(marker)
return sub_mesh_geo