Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changelog.d/polyhedra-no-kernel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Fixed

- A polyhedron with no kernel (tangled, inside out, or with a face whose own fan
folds over, which no point inside sees whole) held no points: a point inside
one was found in a neighbour or nowhere, and a particle reaching one stopped,
with a warning. Such a cell now holds a point by its winding number (the
solid angles of its face triangles), and point data there is interpolated by
mean value coordinates, which are exact for a linear field, as VTK's
`vtkPolyhedron` interpolates. Probes, `CellLocator.find`, `advect` and
streamlines take it. On AhmedML's volume 12 of 21.5M cells are such (#334).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design doc says 10 of the 12 are read as polyhedra; the changelog says all 12 are handled.

50 changes: 37 additions & 13 deletions docs/vis-guide/design/polyhedra.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,19 +796,43 @@ the points from which every triangle of its face fans is seen from inside.
evaluate with, and probe's evaluation from the cells
(`_evaluate_in_moved_tets`, `tet_value_from`).
- A mesh without such cells compiles the path out.
- **Cells with no kernel are skipped, with a warning.** These are tangled or
inside-out cells, cells with a face whose own fan folds over, and cells with
no volume. `starts` is -2 for them, and both searches leave them out.
- A point inside one is found in a neighbour it overlaps, or nowhere, and a
particle reaching one stops.
- On the AhmedML volume there were 12: wall cells 2–3 mm across at the
nose's four corners, which none of 338 streamlines aimed at those edges
entered.
- VTK's `vtkStreamTracer` traces through such cells without a word: in 9 of
the 12 its `FindCell` returns a neighbour for the cell's centroid.
- Locating in them properly would need a solid-angle (winding number) test
and another interpolant, mean value coordinates. That is on the roadmap
for 0.9.
- **Cells with no kernel hold points by their winding number.** These are
tangled or inside-out cells, and cells with a face whose own fan folds over.
`starts` is -2 for them (`_SKIPPED`), and their sub-tetrahedra overlap, so
neither the fan's locating nor its interpolant holds there (#334).
- **Inside test.** `locate_by_winding` sums the solid angles of the cell's
face triangles (each face fanned from its face point, as its neighbour fans
it, so the cells' surfaces meet) over 4π, by Van Oosterom and Strackee's
formula. A point is held where that is 1 or -1, so an inside-out cell holds
its points too; a cell with no volume winds round nothing. A point outside
but within the slack of a face triangle is near, as in other cells.
- **The location.** Its sub-tetrahedron is `views.NO_KERNEL_TET` (positive
and past any fan's, so callers testing `t >= 0` need no change), and its
`pc` is the point less the cell point (the mean).
- **Interpolant.** `mvc_value`: mean value coordinates over the same
triangles (Ju, Schaefer and Warren, 2005, in the robust form
`vtkMeanValueCoordinatesInterpolator` uses), defined in any closed
polyhedron and exact for a linear field. A face point takes the mean of
its face's values. A point on a triangle takes its barycentric weights;
the angle tolerance is VTK's 1e-8, since the arcs' shortfall from 2π
shrinks as the square of the distance (1e-5 snapped points 0.3% of a
triangle away onto it).
- **Who reads it.** The search from the cells (`_test_points_in_polyhedra`),
the by-id `_PolyhedralMesh` (`locate_held`, `tet_value_of`), which probe
and advection use, and probe's evaluation from the cells
(`_evaluate_in_moved_tets`). Advection's shortcut, a cell that holds the
particle well inside (`_well_inside`), does not stop at such a cell, which
may overlap its neighbours.
- **Not done.** Gradients there: nothing reads a polyhedral location's
gradient yet.
- **On the AhmedML volume** there are 12: wall cells 2–3 mm across at the
nose's four corners, 10 of them read as polyhedra. Until #334 they held no
points. Now each of those 10 holds its centre and the points a third of
the way from it to each of its points (80 in all), none found in a
neighbour, and every interpolated `UMean` is within the range of the
cell's own point values (2026-12-05). VTK's `vtkStreamTracer` traces
through such cells without a word: in 9 of the 12 its `FindCell` returns a
neighbour for the cell's centroid.

**Tests, and why not VTK.** The proposal planned exact agreement with
`vtkProbeFilter` and `vtkGradientFilter` on linear fields. On VTK 9.7.1
Expand Down
2 changes: 1 addition & 1 deletion packages/tack-dataset/CLAUDE.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions packages/tack-dataset/src/tack/dataset/algorithms/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import tack
from tack import arrays
from tack.dataset import ids, units
from tack.dataset import ids, units, views
from tack.dataset.algorithms._common import _field, _stand_in
from tack.dataset.algorithms._extract import _vertices
from tack.dataset.buckets import bucket_groups
Expand Down Expand Up @@ -42,7 +42,8 @@ def _evaluate_in_tets(cells, u, links, link_points, tets, pcs, out):
def _evaluate_in_moved_tets(cells, u, starts, weights, links, link_points, tets, pcs, out):
# As _evaluate_in_tets, point data in polyhedra whose cell points the locator
# moved (CellLocator.moved_cell_points): there, the cell point's value is the
# sum of the points' values by its weights.
# sum of the points' values by its weights. In a cell with no kernel
# (views.NO_KERNEL_TET), the value is its mean value coordinates' sum.
for c in cells:
e = cells.entity_id(c)
if links[e + 1] > links[e]:
Expand All @@ -55,7 +56,10 @@ def _evaluate_in_moved_tets(cells, u, starts, weights, links, link_points, tets,
u0 += (u.corner_value(c, j) - first) * weights[s + j]
for n in range(links[e], links[e + 1]):
q = link_points[n]
out[q] = u.tet_value_from(c, tets[q], pcs[q], u0)
if tets[q] == views.NO_KERNEL_TET:
out[q] = cells.mvc_value(c, cells.cell_point(c) + pcs[q], u)
else:
out[q] = u.tet_value_from(c, tets[q], pcs[q], u0)


@tack.kernel
Expand Down
80 changes: 53 additions & 27 deletions packages/tack-dataset/src/tack/dataset/by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,28 @@ def cell_type(self, e):

@tack.func
def locate(self, c, x, slack):
"""``_PolyhedralLocation.locate`` from its cell point; a cell the locator found
no kernel for (``moved_cell_points``' -2) holds nothing."""
"""``_PolyhedralLocation.locate`` from its cell point; in a cell the locator
found no kernel for (``moved_cell_points``' -2), by its winding number
(``locate_by_winding``)."""
t, pc, inside = self.locate_held(c, x, slack, 1)
return t, pc, inside

@tack.func
def locate_held(self, c, x, slack, near):
"""``locate``, seeking the nearest sub-tetrahedron only where ``near`` is 1
(``_PolyhedralLocation.locate_held_from``)."""
t, pc, inside = self.locate_held_from(c, self.cell_point(c), x, slack, near)
(``_PolyhedralLocation.locate_held_from``); in a cell with no kernel, one
within the slack only where ``near`` is 1."""
p = self.cell_point(c)
skipped = self.moved_starts[c] if self.MOVED == 1 else -1
t = -1
pc = x - x
inside = 0
if skipped == -2:
t = -1
t, pc, inside = self.locate_by_winding(c, p, x, slack)
if inside == 0 and near == 0:
t = -1
else:
t, pc, inside = self.locate_held_from(c, p, x, slack, near)
return t, pc, inside

@tack.func
Expand All @@ -220,28 +229,37 @@ def cell_point(self, c):
@tack.func
def tet_value_of(self, field, e, t, pc):
"""Point data at ``pc`` in sub-tetrahedron ``t``: linear there, the cell point
taking its points' values as its position takes their positions."""
k, j = self.tet_face(e, t)
n = self.side_num_points(e, k)
nxt = j + 1 if j + 1 < n else 0
count = self.num_points(e)
s = self.moved_starts[e] if self.MOVED == 1 else -1
v0 = field.values[self.point_id(e, 0)]
if s >= 0:
first = v0
for i in range(1, count):
v0 += (field.values[self.point_id(e, i)] - first) * self.moved_weights[s + i]
taking its points' values as its position takes their positions. In a cell
with no kernel (``views.NO_KERNEL_TET``), ``pc`` is the point less the cell
point, and the value its mean value coordinates' sum (``mvc_value``)."""
result = field.values[self.point_id(e, 0)]
# MOVED is compiled in: a mesh whose cells all have kernels (most) compiles
# the mean value coordinates out.
if self.MOVED == 1 and t == views.NO_KERNEL_TET:
result = self.mvc_value(e, self.cell_point(e) + pc, field)
else:
for i in range(1, count):
v0 += field.values[self.point_id(e, i)]
v0 = v0 / count
vf = field.values[self.side_point_id(e, k, 0)]
for i in range(1, n):
vf += field.values[self.side_point_id(e, k, i)]
vf = vf / n
return (v0 + pc[0] * (vf - v0)
+ pc[1] * (field.values[self.side_point_id(e, k, j)] - v0)
+ pc[2] * (field.values[self.side_point_id(e, k, nxt)] - v0))
k, j = self.tet_face(e, t)
n = self.side_num_points(e, k)
nxt = j + 1 if j + 1 < n else 0
count = self.num_points(e)
s = self.moved_starts[e] if self.MOVED == 1 else -1
v0 = field.values[self.point_id(e, 0)]
if s >= 0:
first = v0
for i in range(1, count):
v0 += (field.values[self.point_id(e, i)] - first) * self.moved_weights[s + i]
else:
for i in range(1, count):
v0 += field.values[self.point_id(e, i)]
v0 = v0 / count
vf = field.values[self.side_point_id(e, k, 0)]
for i in range(1, n):
vf += field.values[self.side_point_id(e, k, i)]
vf = vf / n
result = (v0 + pc[0] * (vf - v0)
+ pc[1] * (field.values[self.side_point_id(e, k, j)] - v0)
+ pc[2] * (field.values[self.side_point_id(e, k, nxt)] - v0))
return result


@tack.device_class
Expand Down Expand Up @@ -737,7 +755,10 @@ def _well_inside_fan(self, p, e):
kind = 0
pc = p - p
t = -1
if (held == 1 and tet >= 0 and min(tpc[0], tpc[1], tpc[2]) >= shapes.WELL_INSIDE
# A cell with no kernel (views.NO_KERNEL_TET) may overlap its neighbours, so
# holding p is not enough to stop the search there.
if (held == 1 and tet >= 0 and tet != views.NO_KERNEL_TET
and min(tpc[0], tpc[1], tpc[2]) >= shapes.WELL_INSIDE
and tpc[0] + tpc[1] + tpc[2] <= 1.0 - shapes.WELL_INSIDE):
found = e
kind = self.cells.cell_type(e)
Expand Down Expand Up @@ -1127,6 +1148,11 @@ class _PointValues:
def __init__(self, values):
self.values = values

@tack.func
def get(self, p):
"""Point ``p``'s value (as a field view's ``get``, for ``mvc_value``)."""
return self.values[p]

@tack.func
def value(self, mesh, e, kind, pc, tet):
v = self.values[mesh.cells.point_id(e, 0)]
Expand Down
32 changes: 15 additions & 17 deletions packages/tack-dataset/src/tack/dataset/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"""

import copy
import warnings
from typing import NamedTuple

import numpy as np
Expand Down Expand Up @@ -77,8 +76,9 @@ class Locations(NamedTuple):
_TOLERANCE = 1e-3

#: ``moved_cell_points``' start for a polyhedron with no kernel: no point inside it
#: sees all of its faces, so it holds no points (found, if anywhere, in a neighbour
#: it overlaps).
#: sees all of its faces, so its sub-tetrahedra overlap. It holds a point by its
#: winding number (``views._PolyhedralLocation.locate_by_winding``), the location's
#: sub-tetrahedron ``views.NO_KERNEL_TET``.
_SKIPPED = tack.constant(-2)


Expand Down Expand Up @@ -374,7 +374,7 @@ def _unmoved(cells, starts):

@tack.kernel
def _skip(bad, starts):
# A cell still not star-shaped from its point holds no points.
# A cell still not star-shaped from its point is located by its winding number.
for e in range(bad.shape[0]):
if bad[e] == 1:
starts[e] = _SKIPPED
Expand Down Expand Up @@ -416,8 +416,6 @@ def _test_points_in_polyhedra(cells, points, lo, hi, bin_offsets, bin_points, ox
j1 = min(tack.i64(floor((b[1] + slack - oy) * iy)), ny - 1)
k1 = min(tack.i64(floor((b[2] + slack - oz) * iz)), nz - 1)
skipped = starts[e] if moved == 1 else -1
if skipped == _SKIPPED:
k1 = k0 - 1 # no kernel: it holds no points
if hidden == 1 and (tack.i32(marks[e]) & _MASKED) != 0:
k1 = k0 - 1 # left out: it holds no points
for k in range(k0, k1 + 1):
Expand All @@ -430,7 +428,13 @@ def _test_points_in_polyhedra(cells, points, lo, hi, bin_offsets, bin_points, ox
if (x[0] >= a[0] - slack and x[0] <= b[0] + slack
and x[1] >= a[1] - slack and x[1] <= b[1] + slack
and x[2] >= a[2] - slack and x[2] <= b[2] + slack):
t, pc, inside = cells.locate_from(c, middle, x, slack)
t = -1
pc = x - x
inside = 0
if skipped == _SKIPPED:
t, pc, inside = cells.locate_by_winding(c, middle, x, slack)
else:
t, pc, inside = cells.locate_from(c, middle, x, slack)
if t >= 0:
rank = e if inside == 1 else e + ncells
if write == 0:
Expand Down Expand Up @@ -694,10 +698,10 @@ def __init__(self, data, density=1.0, levels=None):
#: sum of its points by ``weights[starts[c] + j]`` (``starts[c]`` -1 for a cell
#: that keeps the mean). ``find``'s ``tets`` and ``pcs`` there are relative to
#: that point; ``probe`` and advection evaluate there. A polyhedron with no
#: kernel (tangled, inside out, a face whose fan folds, no volume) has
#: ``starts[c]`` -2 and holds no points: one inside it is found in a
#: neighbour it overlaps, or nowhere, and a particle reaching it stops as at
#: the mesh's edge. Making it is warned about.
#: kernel (tangled, inside out, a face whose fan folds) has ``starts[c]`` -2:
#: it holds a point by its winding number, and point data there is
#: interpolated by mean value coordinates (``views.NO_KERNEL_TET``). One with
#: no volume winds round nothing, so holds nothing.
self.moved_cell_points = None
if n and self.polyhedral:
bad = tack.zeros((n,), tack.i32)
Expand All @@ -714,12 +718,6 @@ def __init__(self, data, density=1.0, levels=None):
count = int(bad.numpy().sum())
if count and dimension == 3:
_skip(bad, self.moved_cell_points[0])
warnings.warn(
f"{count} of the {n} cells are not star-shaped from any point found "
"inside them -- none sees all of their faces (tangled or inside-out "
"cells) -- or have no volume: they hold no points. A point inside "
"one is found in a neighbour it overlaps, or nowhere; a particle "
"reaching one stops.", stacklevel=2)
elif count:
raise NotImplementedError(
f"{count} of the {n} cells are not star-shaped from their cell point "
Expand Down
Loading
Loading