26#ifndef PECLET_VORO_TESSELLATOR_HPP
27#define PECLET_VORO_TESSELLATOR_HPP
33#include <Kokkos_Core.hpp>
38#include "morton/morton.hpp"
39#include "peclet/core/common/view.hpp"
73 Kokkos::View<int*, peclet::core::MemSpace>
status;
98template <
class Cell,
int MAXF>
100 unsigned char* part,
unsigned char* pcnt,
bool& full) {
101 constexpr int kMaxFV = Cell::MAXFV;
102 signed char faceOf[256];
103 for (
int k = 0; k < c.np; ++k)
105 for (
int idx = 0; idx < nf; ++idx)
106 faceOf[faces[idx]] = (
signed char)idx;
107 for (
int idx = 0; idx < nf; ++idx)
110 for (
int t = 0; t < c.nt; ++t) {
113 const int tp[3] = {c.t0[t], c.t1[t], c.t2[t]};
114 for (
int a = 0; a < 3; ++a) {
115 const int fa = faceOf[tp[a]];
118 for (
int b = 0; b < 3; ++b) {
121 const int fb = faceOf[tp[b]];
125 for (
int q = 0; q < pcnt[fa]; ++q)
126 if (part[fa * kMaxFV + q] == (
unsigned char)fb) {
132 if (pcnt[fa] >= kMaxFV) {
136 part[fa * kMaxFV + pcnt[fa]] = (
unsigned char)fb;
142 for (
int idx = 0; idx < nf; ++idx)
143 total += 1 + pcnt[idx];
149template <
class Cell,
int MAXF,
class IView,
class RView>
150KOKKOS_INLINE_FUNCTION
void areaGradFill(
const Cell& c,
const int* faces,
int nf,
151 const unsigned char* part,
const unsigned char* pcnt,
152 int base,
int ebase,
const IView& oEdgeOff,
153 const IView& oEdgeCnt,
const IView& oEdgeFacet,
154 const RView& oEdgeGrad,
const RView& oEdgeLen) {
155 using Real =
typename RView::non_const_value_type;
156 constexpr int kMaxFV = Cell::MAXFV;
160 const bool wl = oEdgeLen.extent(0) > 0;
161 signed char faceOf[256];
162 for (
int k = 0; k < c.np; ++k)
164 for (
int idx = 0; idx < nf; ++idx)
165 faceOf[faces[idx]] = (
signed char)idx;
167 for (
int idx = 0; idx < nf; ++idx) {
168 oEdgeOff(base + idx) = run;
169 oEdgeCnt(base + idx) = 1 + pcnt[idx];
170 oEdgeFacet(run) = base + idx;
171 for (
int q = 0; q < pcnt[idx]; ++q)
172 oEdgeFacet(run + 1 + q) = base + part[idx * kMaxFV + q];
173 for (
int e = 0; e < 1 + pcnt[idx]; ++e) {
174 for (
int cc = 0; cc < 3; ++cc)
175 oEdgeGrad((
size_t)(run + e) * 3 + cc) = Real(0);
177 for (
int cc = 0; cc < 4; ++cc)
178 oEdgeLen((
size_t)(run + e) * 4 + cc) = Real(0);
180 run += 1 + pcnt[idx];
182 for (
int t = 0; t < c.nt; ++t) {
186 Real contrib[3], grad[3][3][3];
187 c.geomVolumeAreaGrad(t, pl, contrib, grad);
188 for (
int a = 0; a < 3; ++a) {
189 const int fa = faceOf[pl[a]];
192 const int eoff = oEdgeOff(base + fa);
193 for (
int b = 0; b < 3; ++b) {
194 const int fb = faceOf[pl[b]];
200 for (
int q = 0; q < pcnt[fa]; ++q)
201 if (part[fa * kMaxFV + q] == (
unsigned char)fb) {
208 const size_t o = (size_t)(eoff + slot) * 3;
209 oEdgeGrad(o + 0) += grad[a][b][0];
210 oEdgeGrad(o + 1) += grad[a][b][1];
211 oEdgeGrad(o + 2) += grad[a][b][2];
212 if (wl && slot > 0) {
213 const size_t l = (size_t)(eoff + slot) * 4;
214 oEdgeLen(l + 0) += c.vx[t];
215 oEdgeLen(l + 1) += c.vy[t];
216 oEdgeLen(l + 2) += c.vz[t];
217 oEdgeLen(l + 3) += c.vx[t] * c.vx[t] + c.vy[t] * c.vy[t] + c.vz[t] * c.vz[t];
223 for (
int e = ebase; e < run; ++e) {
224 const size_t l = (size_t)e * 4;
225 const Real S2 = oEdgeLen(l) * oEdgeLen(l) + oEdgeLen(l + 1) * oEdgeLen(l + 1) +
226 oEdgeLen(l + 2) * oEdgeLen(l + 2);
227 const Real d2 = Real(2) * oEdgeLen(l + 3) - S2;
228 oEdgeLen(l) = d2 > Real(0) ? Kokkos::sqrt(d2) : Real(0);
233template <
class Real,
bool Weighted,
class Sdf,
bool TrackAdj =
false,
int MAXP =
kMaxPlanes,
264 Kokkos::View<int*, MemSpace>
oNbr;
282 Kokkos::View<unsigned*, MemSpace>
oTri;
283 Kokkos::View<unsigned char*, MemSpace>
331 const int total = areaGradEnumerate<Cell, MAXF_TMP>(c, faces, nf, part, pcnt, full);
332 const int ebase = Kokkos::atomic_fetch_add(&
edgeCursor(0), total);
333 if (full || (
size_t)ebase + (
size_t)total >
edgeCap) {
335 for (
int idx = 0; idx < nf; ++idx) {
341 areaGradFill<Cell, MAXF_TMP>(c, faces, nf, part, pcnt, base, ebase,
oEdgeOff,
oEdgeCnt,
346 KOKKOS_INLINE_FUNCTION
void relVec(
int q, Real pix, Real piy, Real piz, Real pv[3])
const {
349 const Real Lxh = Real(0.5) *
Lx, Lyh = Real(0.5) *
Ly, Lzh = Real(0.5) *
Lz;
350 pv[0] = rx > Lxh ? rx -
Lx : (rx < -Lxh ? rx +
Lx : rx);
351 pv[1] = ry > Lyh ? ry -
Ly : (ry < -Lyh ? ry +
Ly : ry);
352 pv[2] = rz > Lzh ? rz -
Lz : (rz < -Lzh ? rz +
Lz : rz);
356 KOKKOS_INLINE_FUNCTION
int gridCell(
int rgx,
int rgy,
int rgz)
const {
357 int gx = rgx < 0 ? rgx +
dimx : (rgx >=
dimx ? rgx -
dimx : rgx);
358 int gy = rgy < 0 ? rgy +
dimy : (rgy >=
dimy ? rgy -
dimy : rgy);
359 int gz = rgz < 0 ? rgz +
dimz : (rgz >=
dimz ? rgz -
dimz : rgz);
364 KOKKOS_INLINE_FUNCTION
void homeCell(Real pix, Real piy, Real piz,
int& cx,
int& cy,
372 KOKKOS_INLINE_FUNCTION
int subBase(Real pix, Real piy, Real piz)
const {
373 const Real fxi = pix *
icx, fyi = piy *
icy, fzi = piz *
icz;
374 int sx = (int)((fxi - Kokkos::floor(fxi)) * Real(
wlS));
375 int sy = (int)((fyi - Kokkos::floor(fyi)) * Real(
wlS));
376 int sz = (int)((fzi - Kokkos::floor(fzi)) * Real(
wlS));
377 sx = sx < 0 ? 0 : (sx >=
wlS ?
wlS - 1 : sx);
378 sy = sy < 0 ? 0 : (sy >=
wlS ?
wlS - 1 : sy);
379 sz = sz < 0 ? 0 : (sz >=
wlS ?
wlS - 1 : sz);
384 KOKKOS_INLINE_FUNCTION
int worklistCell(
int base,
int g,
int cx,
int cy,
int cz)
const {
385 const int packed =
wlOff(base + g);
386 const int rgx = cx + ((packed & 0xFF) -
kWlOffBias);
387 const int rgy = cy + (((packed >> 8) & 0xFF) -
kWlOffBias);
388 const int rgz = cz + (((packed >> 16) & 0xFF) -
kWlOffBias);
398 for (
int k = 0; k < 6; ++k)
407 KOKKOS_INLINE_FUNCTION
void finishCell(
Cell& c,
int i, Real pix, Real piy, Real piz, Real covSq,
408 Real wSelf = Real(0))
const {
421 const bool incomplete =
424 if constexpr (!std::is_same_v<Sdf, NoSdf>) {
425 const Real seedW[3] = {pix, piy, piz};
426 const bool wallFD =
oWallDV.extent(0) > 0;
427 Real dV[3] = {Real(0), Real(0), Real(0)}, dA[3] = {Real(0), Real(0), Real(0)};
430 const Real phiC =
sdf.eval(pix, piy, piz);
432 if (phiC > Real(0) && phiC <= rad + Real(1e-8)) {
433 const Cell unclipped = c;
434 clipCellAgainstSdf<Real, kMaxP, kMaxT>(c, seedW,
sdf);
435 bool hasWall =
false;
436 for (
int k = 0; k < c.
np && !hasWall; ++k)
439 sdfWallFD<Real, kMaxP, kMaxT>(unclipped, seedW,
sdf, Real(1e-4) * rad, dV, dA);
441 clipCellAgainstSdf<Real, kMaxP, kMaxT>(c, seedW,
sdf);
443 for (
int d = 0; d < 3; ++d) {
444 oWallDV(3 * (
size_t)i + d) = dV[d];
445 oWallDA(3 * (
size_t)i + d) = dA[d];
448 clipCellAgainstSdf<Real, kMaxP, kMaxT>(c, seedW,
sdf);
461 Lmin = Lmin <
Lz ? Lmin :
Lz;
462 if (reachR > Real(0.5) * Lmin)
465 const bool empty = c.
empty();
470 if constexpr (Weighted) {
475 for (
int t = 0; t < c.
nt && !neg; ++t)
477 neg = c.
nn[c.
t0[t]] < Real(0) || c.
nn[c.
t1[t]] < Real(0) || c.
nn[c.
t2[t]] < Real(0);
489 for (
int k = 0; k < c.
np; ++k)
491 for (
int t = 0; t < c.
nt; ++t)
492 oTri[(
size_t)i *
kMaxT + t] = (unsigned)c.
t0[t] | ((
unsigned)c.
t1[t] << 8) |
493 ((
unsigned)c.
t2[t] << 16) | ((c.
alive[t] ? 1u : 0u) << 24);
494 if constexpr (TrackAdj)
507 for (
int k = 0; k < c.
np; ++k) {
509 for (
int t = 0; t < c.
nt; ++t)
510 if (c.
alive[t] && (c.
t0[t] == k || c.
t1[t] == k || c.
t2[t] == k))
522 const int base = Kokkos::atomic_fetch_add(&
facetCursor(0), nf);
523 if ((
size_t)base + (
size_t)nf >
facetCap) {
533 for (
int idx = 0; idx < nf; ++idx) {
534 const int k = faces[idx];
535 Real area[3] = {0, 0, 0}, dv[3] = {0, 0, 0}, conn[3];
536 conn[0] = Real(2) * c.
n[k][0];
537 conn[1] = Real(2) * c.
n[k][1];
538 conn[2] = Real(2) * c.
n[k][2];
539 if constexpr (Weighted) {
554 oNbr((
size_t)base + idx) = c.
pnbr[k];
555 const size_t o = ((size_t)base + idx) * 3;
556 for (
int cc = 0; cc < 3; ++cc) {
557 oArea(o + cc) = area[cc];
558 oDV(o + cc) = dv[cc];
559 oConn(o + cc) = conn[cc];
570 KOKKOS_INLINE_FUNCTION
bool gatherCell(
int pi,
Cell& c, Real& pix, Real& piy, Real& piz,
571 Real& wSelf,
int& ncRec,
int& nnRec)
const {
581 homeCell(pix, piy, piz, cx, cy, cz);
582 const int base =
subBase(pix, piy, piz);
585 if constexpr (Weighted)
587 bool emptied =
false;
594 if constexpr (!std::is_same_v<Sdf, NoSdf>) {
595 const Real phi0 =
sdf.eval(pix, piy, piz);
596 if (phi0 > Real(0)) {
599 const Real gl = Kokkos::sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);
605 const Real pdir[3] = {-g[0] / gl, -g[1] / gl, -g[2] / gl};
620 const bool emitNear =
oNear.extent(0) > 0 &&
nearCap > 0;
624 const Real nm = emitNear ?
nearMargin : Real(0);
625 for (
int g = 0; g <
nOff && !c.
overflow && !emptied; ++g) {
626 if constexpr (Weighted) {
627 const Real rr = Kokkos::sqrt(reachSq) + Real(2) * nm;
628 if (
wlRmin(base + g) > rr * rr)
633 const Real rr = Kokkos::sqrt(Real(2) * secR2) + Real(2) * nm;
634 if (
wlRmin(base + g) > rr * rr)
651 for (
int k = 6; k < c.
np && !dup; ++k)
652 dup = (c.
pnbr[k] ==
id);
661 relVec(q, pix, piy, piz, pv);
663 if constexpr (Weighted)
665 const Real off = PlanePolicy::template offsetFromRel<Real>(pv, wSelf, wNbr);
666 if constexpr (Weighted) {
670 const Real rho = pv[0] * pv[0] + pv[1] * pv[1] + pv[2] * pv[2];
671 if (!emitNear && off > Real(0) && rho * c.
maxVertexRsq() <= off * off)
674 if (c.
clip(pv, off,
binned(q), emitNear ? &gap :
nullptr)) {
680 }
else if (emitNear && gap > -nm) {
686 if (!emitNear && off >= secR2)
689 if (c.
clip(pv, off,
binned(q), emitNear ? &gap :
nullptr)) {
691 }
else if (emitNear && gap > -nm) {
708 Real pix, piy, piz, wSelf;
710 const bool nonEmpty =
gatherCell(pi, c, pix, piy, piz, wSelf, ncRec, nnRec);
712 const bool emitNear =
oNear.extent(0) > 0 &&
nearCap > 0;
713 if constexpr (Weighted) {
738 finishCell(c, i, pix, piy, piz, covSq, wSelf);
745template <
class D,
class S>
746void packFacetsByCell(
const char* label,
const Kokkos::View<D*, peclet::core::MemSpace>& dst,
747 const Kokkos::View<S*, peclet::core::MemSpace>& src,
int stride,
748 const Kokkos::View<int*, peclet::core::MemSpace>& from,
749 const Kokkos::View<int*, peclet::core::MemSpace>& to,
750 const Kokkos::View<int*, peclet::core::MemSpace>& count) {
751 using Exec = peclet::core::ExecSpace;
752 const int N =
static_cast<int>(count.extent(0));
753 Kokkos::parallel_for(
754 label, Kokkos::RangePolicy<Exec>(0, N), KOKKOS_LAMBDA(
const int i) {
755 const size_t o = (size_t)from(i) * stride, p = (size_t)to(i) * stride;
756 const size_t n = (size_t)count(i) * stride;
757 for (
size_t k = 0; k < n; ++k)
758 dst(p + k) =
static_cast<D
>(src(o + k));
770 Kokkos::View<int*, MemSpace>
oNbr;
781 facetCursor = Kokkos::View<int*, MemSpace>(
"facetCursor", 1);
787 edgeCursor = Kokkos::View<int*, MemSpace>(
"edgeCursor", 1);
793 using Kokkos::view_alloc;
794 using Kokkos::WithoutInitializing;
805 oEdgeOff = Kokkos::View<int*, MemSpace>(
807 oEdgeCnt = Kokkos::View<int*, MemSpace>(
811 oEdgeGrad = Kokkos::View<Real*, MemSpace>(
813 oEdgeLen = Kokkos::View<Real*, MemSpace>(
818 oMoment2 = Kokkos::View<Real*, MemSpace>(
826template <
class Real,
class Builder,
class BinnedView>
829 using MemSpace = peclet::core::MemSpace;
830 using Exec = peclet::core::ExecSpace;
832 int overBufferRebuilds = 0;
834 Kokkos::parallel_for(
835 "tess.build", Kokkos::RangePolicy<Exec>(0, N),
KOKKOS_LAMBDA(
const int pi) {
840 Kokkos::deep_copy(
nFacetsRaw, Kokkos::subview(
bufs.facetCursor, 0));
841 if (
bufs.withAreaGrad)
842 Kokkos::deep_copy(
nEdgesRaw, Kokkos::subview(
bufs.edgeCursor, 0));
854 ++overBufferRebuilds;
856 std::fprintf(
stderr,
"[tess.build] over-buffer exceeded (facets %d, edges %d): rebuilding\n",
861 op.oNbr =
bufs.oNbr = Kokkos::View<int*, MemSpace>();
862 op.oArea =
bufs.oArea = Kokkos::View<Real*, MemSpace>();
863 op.oDV =
bufs.oDV = Kokkos::View<Real*, MemSpace>();
864 op.oConn =
bufs.oConn = Kokkos::View<Real*, MemSpace>();
865 op.oEdgeOff =
bufs.oEdgeOff = Kokkos::View<int*, MemSpace>();
866 op.oEdgeCnt =
bufs.oEdgeCnt = Kokkos::View<int*, MemSpace>();
867 op.oEdgeFacet =
bufs.oEdgeFacet = Kokkos::View<int*, MemSpace>();
868 op.oEdgeGrad =
bufs.oEdgeGrad = Kokkos::View<Real*, MemSpace>();
869 op.oEdgeLen =
bufs.oEdgeLen = Kokkos::View<Real*, MemSpace>();
870 op.oMoment2 =
bufs.oMoment2 = Kokkos::View<Real*, MemSpace>();
873 op.oArea =
bufs.oArea;
875 op.oConn =
bufs.oConn;
876 op.facetCap =
bufs.facetCap;
877 op.oEdgeOff =
bufs.oEdgeOff;
878 op.oEdgeCnt =
bufs.oEdgeCnt;
879 op.oEdgeFacet =
bufs.oEdgeFacet;
880 op.oEdgeGrad =
bufs.oEdgeGrad;
881 op.oEdgeLen =
bufs.oEdgeLen;
882 op.edgeCap =
bufs.edgeCap;
883 op.oMoment2 =
bufs.oMoment2;
885 return overBufferRebuilds;
893 const Kokkos::View<int*, peclet::core::MemSpace>& cellFacetBase,
894 const Kokkos::View<int*, peclet::core::MemSpace>& facetCount,
895 const Kokkos::View<Real*, peclet::core::MemSpace>& cellVol,
896 const Kokkos::View<Real*, peclet::core::MemSpace>& oWallDV,
897 const Kokkos::View<Real*, peclet::core::MemSpace>& oWallDA,
int N) {
898 using MemSpace = peclet::core::MemSpace;
899 using Exec = peclet::core::ExecSpace;
900 using Kokkos::view_alloc;
901 using Kokkos::WithoutInitializing;
902 Kokkos::View<int*, MemSpace>
pubBase(
906 auto fc = facetCount;
908 Kokkos::parallel_scan(
909 "tess.facetScan", Kokkos::RangePolicy<Exec>(0, N),
924 Kokkos::parallel_for(
925 "tess.seedId", Kokkos::RangePolicy<Exec>(0, N),
940 bufs.oNbr = Kokkos::View<int*, MemSpace>();
942 view.
facetArea = Kokkos::View<Real*, MemSpace>(
947 bufs.oArea = Kokkos::View<Real*, MemSpace>();
954 bufs.oDV = Kokkos::View<Real*, MemSpace>();
960 if (oWallDV.extent(0) > 0) {
964 if (
bufs.withMoments) {
978 const Kokkos::View<int*, peclet::core::MemSpace>& cellFacetBase,
979 const Kokkos::View<int*, peclet::core::MemSpace>& facetCount,
int N,
981 using MemSpace = peclet::core::MemSpace;
982 using Exec = peclet::core::ExecSpace;
983 using Kokkos::view_alloc;
984 using Kokkos::WithoutInitializing;
985 Kokkos::View<int*, MemSpace>
pubEBase(
989 auto ob = cellFacetBase;
990 auto fc = facetCount;
993 Kokkos::parallel_scan(
994 "tess.edgeScan", Kokkos::RangePolicy<Exec>(0, N),
997 for (
int k = 0; k <
fc(
i); ++k)
1009 view.
edgeFacet = Kokkos::View<int*, MemSpace>(
1013 view.
edgeLength = Kokkos::View<Real*, MemSpace>(
1015 auto ob = cellFacetBase;
1017 auto fc = facetCount;
1023 Kokkos::parallel_for(
1024 "tess.packEdges", Kokkos::RangePolicy<Exec>(0, N),
KOKKOS_LAMBDA(
const int i) {
1025 const int n =
fc(
i);
1031 for (
int k = 0; k < n; ++k) {
1036 for (
int e = 0;
e <
ne; ++
e) {
1038 for (
int cc = 0;
cc < 3; ++
cc)
1047void printMaxFacets(
const Kokkos::View<int*, peclet::core::MemSpace>& facetCount,
int N) {
1048 using Exec = peclet::core::ExecSpace;
1052 auto fc = facetCount;
1053 Kokkos::parallel_reduce(
1054 "tess.maxFacets", Kokkos::RangePolicy<Exec>(0, N),
1056 std::fprintf(
stderr,
"[tess N=%d] maxFacets/cell=%d (=> maxVerts ~ %d; CAP=%d)\n", N,
maxF,
1057 2 *
maxF - 4, kMaxT);
1085template <
class Real,
bool Weighted,
class Sdf = NoSdf,
int MAXP =
kMaxPlanes,
1088 const Kokkos::View<Real*, peclet::core::MemSpace>& posFlat,
1089 const Kokkos::View<Real*, peclet::core::MemSpace>& weight,
int N,
const Real L[3],
1091 Kokkos::View<long*, peclet::core::MemSpace> gid = {}, Sdf sdf = {},
bool withForceGeom =
true,
1092 int nBuild = -1, Kokkos::View<int*, peclet::core::MemSpace> outNp = {},
1093 Kokkos::View<int*, peclet::core::MemSpace> outNt = {},
1094 Kokkos::View<int*, peclet::core::MemSpace> outPnbr = {},
1095 Kokkos::View<unsigned*, peclet::core::MemSpace> outTri = {},
1096 Kokkos::View<int*, peclet::core::MemSpace> outCand = {},
1097 Kokkos::View<int*, peclet::core::MemSpace> outCandCnt = {},
int candCap = 0,
1098 WorklistCache<Real>* wlc =
nullptr, WallStore<Real> outWall = {},
bool withAreaGrad =
false,
1099 Kokkos::View<int*, peclet::core::MemSpace> outNear = {},
1100 Kokkos::View<int*, peclet::core::MemSpace> outNearCnt = {},
int nearCap = 0,
1101 Real nearMargin = Real(0),
bool withWallFD =
false,
bool withMoments =
false,
1102 bool profile =
false) {
1103 using peclet::core::MemSpace;
1104 using Exec = peclet::core::ExecSpace;
1108 const bool emitTopo = outNp.extent(0) ==
static_cast<size_t>(N);
1109 const bool emitCand = outCand.extent(0) > 0 && candCap > 0;
1114 const bool haveGid = gid.extent(0) ==
static_cast<size_t>(N);
1116 const int nBuildEff = (nBuild >= 0 && nBuild < N) ? nBuild : N;
1120 const bool prof = profile;
1121 Kokkos::Timer ptimer;
1122 double tGrid = 0, tBuild = 0, tCsr = 0;
1127 auto grid = buildTessGrid<Real, Weighted>(posFlat, weight, N, L, sw, densityCount, gid, wlc);
1132 const Real Lx = grid.Lx, Ly = grid.Ly, Lz = grid.Lz;
1133 const Real icx = grid.icx, icy = grid.icy, icz = grid.icz, minCsz = grid.minCsz;
1134 const int dimx = grid.dimx, dimy = grid.dimy, dimz = grid.dimz;
1135 const int nOff = grid.nOff, wlS = grid.wlS;
1136 const bool useMorton = grid.useMorton;
1140 tGrid = ptimer.seconds();
1146 Kokkos::View<Real*, MemSpace> cellVol(
"cellVol", N);
1147 Kokkos::View<int*, MemSpace> facetCount(
"facetCount", N);
1148 Kokkos::View<int*, MemSpace> status(
"status", N);
1149 Kokkos::View<int*, MemSpace> cellFacetBase(
"cellFacetBase", N);
1170 constexpr size_t kMeanFacets = 18;
1171 const size_t facetCap = (size_t)N * kMeanFacets;
1173 bufs.init(facetCap, withAreaGrad, withMoments);
1175 Kokkos::View<Real*, MemSpace> oWallDV, oWallDA;
1176 if (withWallFD && !std::is_same_v<Sdf, NoSdf>) {
1177 oWallDV = Kokkos::View<Real*, MemSpace>(
"cellWallDV", (
size_t)N * 3);
1178 oWallDA = Kokkos::View<Real*, MemSpace>(
"cellWallDA", (
size_t)N * 3);
1181 std::fprintf(stderr,
"[worklist] sw=%d nOff=%d\n", sw, nOff);
1186 Kokkos::View<unsigned char*, MemSpace>
1190 Real wMaxAll = Real(0);
1191 if constexpr (Weighted) {
1192 if (weight.extent(0) ==
static_cast<size_t>(N)) {
1193 Kokkos::parallel_reduce(
1194 "wmax", Kokkos::RangePolicy<Exec>(0, N),
1195 KOKKOS_LAMBDA(
int idx, Real& m) { m = weight(idx) > m ? weight(idx) : m; },
1196 Kokkos::Max<Real>(wMaxAll));
1200 CellBuilder<Real, Weighted, Sdf, false, MAXP, MAXT> op{grid.binned,
1262 const int nBuildL = nBuildEff;
1263 auto binnedV0 = grid.binned;
1268 tBuild = ptimer.seconds();
1279 TessellationView<Real> view;
1286 bufs.oConn = Kokkos::View<Real*, MemSpace>();
1289 tCsr = ptimer.seconds();
1290 std::fprintf(stderr,
"[tess N=%d] grid=%.4fs build=%.4fs csr=%.4fs (build %.0f%%)\n", N, tGrid,
1291 tBuild, tCsr, 100.0 * tBuild / (tGrid + tBuild + tCsr));
1296 TessellatorResult<Real> res;
1298 res.status = status;
1299 res.overBufferRebuilds = overBufferRebuilds;
KOKKOS_INLINE_FUNCTION Dual< Real, K > dnum(Real c)
Definition convex_cell.hpp:53
void publishEdgeCsr(TessellationView< Real > &view, TessOverBuffers< Real > &bufs, const Kokkos::View< int *, peclet::core::MemSpace > &cellFacetBase, const Kokkos::View< int *, peclet::core::MemSpace > &facetCount, int N, int nFacets)
Definition tessellator.hpp:977
int publishFacetCsr(TessellationView< Real > &view, TessOverBuffers< Real > &bufs, const Kokkos::View< int *, peclet::core::MemSpace > &cellFacetBase, const Kokkos::View< int *, peclet::core::MemSpace > &facetCount, const Kokkos::View< Real *, peclet::core::MemSpace > &cellVol, const Kokkos::View< Real *, peclet::core::MemSpace > &oWallDV, const Kokkos::View< Real *, peclet::core::MemSpace > &oWallDA, int N)
Definition tessellator.hpp:892
void printMaxFacets(const Kokkos::View< int *, peclet::core::MemSpace > &facetCount, int N)
The profile's max published facets per cell (Max-reduce over the per-cell facet count).
Definition tessellator.hpp:1047
int runCellBuild(Builder &op, TessOverBuffers< Real > &bufs, BinnedView binnedV0, int nBuildL, int N, bool prof)
Definition tessellator.hpp:827
Device-native Voronoi tessellation, geometry and finite-volume physics.
Definition convex_cell.hpp:41
void packFacetsByCell(const char *label, const Kokkos::View< D *, peclet::core::MemSpace > &dst, const Kokkos::View< S *, peclet::core::MemSpace > &src, int stride, const Kokkos::View< int *, peclet::core::MemSpace > &from, const Kokkos::View< int *, peclet::core::MemSpace > &to, const Kokkos::View< int *, peclet::core::MemSpace > &count)
Definition tessellator.hpp:746
std::int32_t gid_t
Definition tessellation_view.hpp:41
StatusBit
Per-cell status bits written by the build pass.
Definition tessellator.hpp:48
@ kOverflow
Definition tessellator.hpp:50
@ kBuried
Definition tessellator.hpp:62
@ kReachExceeded
Definition tessellator.hpp:63
@ kNegOffset
Definition tessellator.hpp:56
@ kOk
Definition tessellator.hpp:49
@ kDegenerate
Definition tessellator.hpp:67
@ kIncomplete
Definition tessellator.hpp:52
@ kEmpty
Definition tessellator.hpp:51
constexpr int kMaxPlanes
Definition params.hpp:18
constexpr int kWlOffBias
Definition tess_grid.hpp:38
constexpr int kMaxTriangles
Definition params.hpp:19
constexpr int kBoundaryFacet
Definition sdf.hpp:36
KOKKOS_INLINE_FUNCTION void sdfGradient(const Sdf &s, Real x, Real y, Real z, Real g[3])
Central-difference gradient of any provider with eval() (matches peclet::core::geom::gradient).
Definition sdf.hpp:216
KOKKOS_INLINE_FUNCTION int areaGradEnumerate(const Cell &c, const int *faces, int nf, unsigned char *part, unsigned char *pcnt, bool &full)
Definition tessellator.hpp:99
constexpr int kSearchWindow
Definition params.hpp:28
KOKKOS_INLINE_FUNCTION int morton3(int x, int y, int z)
Definition tess_grid.hpp:46
KOKKOS_INLINE_FUNCTION void areaGradFill(const Cell &c, const int *faces, int nf, const unsigned char *part, const unsigned char *pcnt, int base, int ebase, const IView &oEdgeOff, const IView &oEdgeCnt, const IView &oEdgeFacet, const RView &oEdgeGrad, const RView &oEdgeLen)
Definition tessellator.hpp:150
TessellatorResult< Real > buildTessellation(const Kokkos::View< Real *, peclet::core::MemSpace > &posFlat, const Kokkos::View< Real *, peclet::core::MemSpace > &weight, int N, const Real L[3], int sw=kSearchWindow, int densityCount=-1, Kokkos::View< long *, peclet::core::MemSpace > gid={}, Sdf sdf={}, bool withForceGeom=true, int nBuild=-1, Kokkos::View< int *, peclet::core::MemSpace > outNp={}, Kokkos::View< int *, peclet::core::MemSpace > outNt={}, Kokkos::View< int *, peclet::core::MemSpace > outPnbr={}, Kokkos::View< unsigned *, peclet::core::MemSpace > outTri={}, Kokkos::View< int *, peclet::core::MemSpace > outCand={}, Kokkos::View< int *, peclet::core::MemSpace > outCandCnt={}, int candCap=0, WorklistCache< Real > *wlc=nullptr, WallStore< Real > outWall={}, bool withAreaGrad=false, Kokkos::View< int *, peclet::core::MemSpace > outNear={}, Kokkos::View< int *, peclet::core::MemSpace > outNearCnt={}, int nearCap=0, Real nearMargin=Real(0), bool withWallFD=false, bool withMoments=false, bool profile=false)
Definition tessellator.hpp:1087
Definition tessellator.hpp:235
bool emitTopo
Definition tessellator.hpp:287
int dimx
Definition tessellator.hpp:270
Kokkos::View< int *, MemSpace > binned
Definition tessellator.hpp:247
Kokkos::View< int *, MemSpace > oNp
Definition tessellator.hpp:281
bool withAreaGrad
Definition tessellator.hpp:301
Kokkos::View< Real *, MemSpace > oEdgeLen
Definition tessellator.hpp:298
Kokkos::View< Real *, MemSpace > oWallDV
Definition tessellator.hpp:313
KOKKOS_INLINE_FUNCTION bool gatherCell(int pi, Cell &c, Real &pix, Real &piy, Real &piz, Real &wSelf, int &ncRec, int &nnRec) const
Definition tessellator.hpp:570
Kokkos::View< Real *, MemSpace > oArea
Definition tessellator.hpp:265
int wlS
Definition tessellator.hpp:270
Kokkos::View< Real *, MemSpace > posSorted
Definition tessellator.hpp:248
Kokkos::View< Real *, MemSpace > oConn
Definition tessellator.hpp:265
Kokkos::View< int *, MemSpace > edgeCursor
Definition tessellator.hpp:299
Kokkos::View< int *, MemSpace > wlOff
Definition tessellator.hpp:257
Kokkos::View< Real *, MemSpace > oEdgeGrad
Definition tessellator.hpp:297
KOKKOS_INLINE_FUNCTION int worklistCell(int base, int g, int cx, int cy, int cz) const
Decode worklist entry g (relative to base) into a grid-cell index.
Definition tessellator.hpp:384
KOKKOS_INLINE_FUNCTION void finishCell(Cell &c, int i, Real pix, Real piy, Real piz, Real covSq, Real wSelf=Real(0)) const
Definition tessellator.hpp:407
int sw
Definition tessellator.hpp:270
bool useMorton
Definition tessellator.hpp:271
Kokkos::View< int *, MemSpace > oEdgeOff
Definition tessellator.hpp:296
Real minCsz
Definition tessellator.hpp:268
Real icx
Definition tessellator.hpp:268
Real Ly
Definition tessellator.hpp:268
Kokkos::View< Real *, MemSpace > wlRmin
Definition tessellator.hpp:258
Kokkos::View< int *, MemSpace > cellFacetBase
Definition tessellator.hpp:263
Real nearMargin
Definition tessellator.hpp:310
KOKKOS_INLINE_FUNCTION int subBase(Real pix, Real piy, Real piz) const
Flat base offset into the worklist tables for the wlS^3 sub-region the seed lands in.
Definition tessellator.hpp:372
KOKKOS_INLINE_FUNCTION void homeCell(Real pix, Real piy, Real piz, int &cx, int &cy, int &cz) const
Home grid cell index (cx,cy,cz) of seed (pix,piy,piz).
Definition tessellator.hpp:364
int nearCap
Definition tessellator.hpp:309
static constexpr int kMaxP
Definition tessellator.hpp:240
Real icz
Definition tessellator.hpp:268
size_t facetCap
Definition tessellator.hpp:272
int dimy
Definition tessellator.hpp:270
KOKKOS_INLINE_FUNCTION void buildCell(int pi) const
Definition tessellator.hpp:705
Kokkos::View< Real *, MemSpace > cellVol
Definition tessellator.hpp:261
KOKKOS_INLINE_FUNCTION int gridCell(int rgx, int rgy, int rgz) const
Linear/Morton index of the grid cell at raw (periodic-unwrapped) offset.
Definition tessellator.hpp:356
Kokkos::View< Real *, MemSpace > oWallDA
Definition tessellator.hpp:313
KOKKOS_INLINE_FUNCTION void relVec(int q, Real pix, Real piy, Real piz, Real pv[3]) const
Minimal-image relative vector from the seed at (pix,piy,piz) to sorted seed q.
Definition tessellator.hpp:346
Kokkos::View< int *, MemSpace > oNear
Definition tessellator.hpp:308
size_t edgeCap
Definition tessellator.hpp:300
KOKKOS_INLINE_FUNCTION void publishAreaGrad(const Cell &c, int i, int base, const int *faces, int nf) const
Definition tessellator.hpp:326
Kokkos::View< int *, MemSpace > cellStart
Definition tessellator.hpp:251
Kokkos::View< gid_t *, MemSpace > gidSorted
Definition tessellator.hpp:250
Kokkos::View< int *, MemSpace > status
Definition tessellator.hpp:260
int candCap
Definition tessellator.hpp:288
Kokkos::View< int *, MemSpace > oNt
Definition tessellator.hpp:281
bool haveGid
Definition tessellator.hpp:271
int nOff
Definition tessellator.hpp:270
Kokkos::View< int *, MemSpace > facetCursor
Definition tessellator.hpp:266
Kokkos::View< unsigned char *, MemSpace > oPoke4
Definition tessellator.hpp:284
Real wMaxAll
Definition tessellator.hpp:269
WallStore< Real > oWall
Definition tessellator.hpp:292
int dimz
Definition tessellator.hpp:270
Kokkos::View< int *, MemSpace > oNbr
Definition tessellator.hpp:264
Kokkos::View< Real *, MemSpace > oDV
Definition tessellator.hpp:265
Kokkos::View< int *, MemSpace > oEdgeCnt
Definition tessellator.hpp:296
Real Lz
Definition tessellator.hpp:268
Kokkos::View< int *, MemSpace > facetCount
Definition tessellator.hpp:262
Kokkos::View< int *, MemSpace > oNearCnt
Definition tessellator.hpp:308
Kokkos::View< Real *, MemSpace > wSorted
Definition tessellator.hpp:249
static constexpr int kMaxFV
Definition tessellator.hpp:295
Real icy
Definition tessellator.hpp:268
bool emitCand
Definition tessellator.hpp:287
Real Lx
Definition tessellator.hpp:268
std::conditional_t< Weighted, Power, Voronoi > PlanePolicy
Definition tessellator.hpp:244
KOKKOS_INLINE_FUNCTION void emitEmptyTopo(int i) const
Definition tessellator.hpp:395
bool withForceGeom
Definition tessellator.hpp:271
Kokkos::View< int *, MemSpace > oCandCnt
Definition tessellator.hpp:286
Kokkos::View< int *, MemSpace > oTopoPnbr
Definition tessellator.hpp:281
Kokkos::View< Real *, MemSpace > oMoment2
Definition tessellator.hpp:316
static constexpr int kMaxT
Definition tessellator.hpp:241
Kokkos::View< int *, MemSpace > oCand
Definition tessellator.hpp:286
peclet::core::MemSpace MemSpace
Definition tessellator.hpp:236
Kokkos::View< int *, MemSpace > oEdgeFacet
Definition tessellator.hpp:296
static constexpr int MAXF_TMP
Definition tessellator.hpp:242
Sdf sdf
Definition tessellator.hpp:273
Kokkos::View< unsigned *, MemSpace > oTri
Definition tessellator.hpp:282
Kokkos::View< int *, MemSpace > slotOf
Definition tessellator.hpp:320
Definition convex_cell.hpp:140
KOKKOS_INLINE_FUNCTION bool facetGeometryFrom(int k, const Real r[3], Real areaVec[3], Real dv[3]) const
Definition convex_cell.hpp:2258
KOKKOS_INLINE_FUNCTION Real volumePerVertex() const
Definition convex_cell.hpp:1585
static constexpr int MAXFV
Definition convex_cell.hpp:1307
unsigned char t1[MAXT]
Definition convex_cell.hpp:156
KOKKOS_INLINE_FUNCTION Real faceMoment2(int k) const
Definition convex_cell.hpp:578
KOKKOS_INLINE_FUNCTION void initBox(Real L0, Real L1, Real L2)
Definition convex_cell.hpp:182
bool overflow
set if MAXP/MAXT exceeded -> cell invalid, caller falls back
Definition convex_cell.hpp:160
int nt
triangle high-water mark
Definition convex_cell.hpp:159
Real nn[MAXP]
signed offset σ·|n|²: the half-space test is n·x <= nn
Definition convex_cell.hpp:153
Real n[MAXP][3]
Definition convex_cell.hpp:152
KOKKOS_INLINE_FUNCTION bool empty() const
Definition convex_cell.hpp:1287
int pnbr[MAXP]
neighbour seed id per plane (<0 => bounding box)
Definition convex_cell.hpp:154
KOKKOS_INLINE_FUNCTION Real maxVertexRsq() const
Largest squared dual-vertex radius over live triangles (drives the security radius).
Definition convex_cell.hpp:676
unsigned char t2[MAXT]
triangle = triple of plane indices
Definition convex_cell.hpp:156
int np
number of planes
Definition convex_cell.hpp:155
KOKKOS_INLINE_FUNCTION bool clip(const Real pdir[3], Real d, int nbr, Real *gapOut=nullptr)
Definition convex_cell.hpp:895
bool degenerate
Definition convex_cell.hpp:161
unsigned char t0[MAXT]
Definition convex_cell.hpp:156
KOKKOS_INLINE_FUNCTION void computePoke4(unsigned char *out) const
Definition convex_cell.hpp:474
bool alive[MAXT]
triangle live flag
Definition convex_cell.hpp:158
KOKKOS_INLINE_FUNCTION bool facetGeometry(int k, Real areaVec[3], Real dv[3], Real conn[3]) const
Definition convex_cell.hpp:2235
Definition tessellation_view.hpp:111
Kokkos::View< Real *, peclet::core::MemSpace > cellWallDV
Definition tessellation_view.hpp:151
Kokkos::View< Real *, peclet::core::MemSpace > facetConnect
Definition tessellation_view.hpp:123
Kokkos::View< Real *, peclet::core::MemSpace > facetArea
Definition tessellation_view.hpp:122
Kokkos::View< int *, peclet::core::MemSpace > facetEdgeOffset
Definition tessellation_view.hpp:138
Kokkos::View< int *, peclet::core::MemSpace > facetEdgeCount
Definition tessellation_view.hpp:139
Kokkos::View< Real *, peclet::core::MemSpace > cellVolume
Definition tessellation_view.hpp:120
Kokkos::View< Real *, peclet::core::MemSpace > facetMoment2
Definition tessellation_view.hpp:160
Kokkos::View< Real *, peclet::core::MemSpace > edgeAreaGrad
Definition tessellation_view.hpp:141
Kokkos::View< gid_t *, peclet::core::MemSpace > cellSeedId
Definition tessellation_view.hpp:119
Kokkos::View< Real *, peclet::core::MemSpace > cellWallDA
Definition tessellation_view.hpp:152
Kokkos::View< int *, peclet::core::MemSpace > cellFacetOffset
Definition tessellation_view.hpp:117
Kokkos::View< int *, peclet::core::MemSpace > edgeFacet
Definition tessellation_view.hpp:140
Kokkos::View< Real *, peclet::core::MemSpace > edgeLength
Definition tessellation_view.hpp:145
Kokkos::View< gid_t *, peclet::core::MemSpace > facetNeighbor
Definition tessellation_view.hpp:121
Kokkos::View< int *, peclet::core::MemSpace > cellFacetCount
Definition tessellation_view.hpp:118
Kokkos::View< Real *, peclet::core::MemSpace > facetConnVec
Definition tessellation_view.hpp:124
Definition tessellator.hpp:71
int overBufferRebuilds
Definition tessellator.hpp:77
Kokkos::View< int *, peclet::core::MemSpace > status
Definition tessellator.hpp:73
TessellationView< Real > view
Definition tessellator.hpp:72
Definition tessellator.hpp:766
Kokkos::View< int *, MemSpace > oEdgeFacet
Definition tessellator.hpp:773
Kokkos::View< Real *, MemSpace > oArea
Definition tessellator.hpp:771
Kokkos::View< int *, MemSpace > edgeCursor
Definition tessellator.hpp:773
Kokkos::View< Real *, MemSpace > oEdgeLen
Definition tessellator.hpp:774
size_t edgeCap
Definition tessellator.hpp:768
bool withAreaGrad
Definition tessellator.hpp:769
Kokkos::View< int *, MemSpace > oEdgeCnt
Definition tessellator.hpp:773
Kokkos::View< int *, MemSpace > oNbr
Definition tessellator.hpp:770
size_t facetCap
Definition tessellator.hpp:768
Kokkos::View< Real *, MemSpace > oEdgeGrad
Definition tessellator.hpp:774
Kokkos::View< int *, MemSpace > oEdgeOff
Definition tessellator.hpp:773
Kokkos::View< Real *, MemSpace > oConn
Definition tessellator.hpp:771
peclet::core::MemSpace MemSpace
Definition tessellator.hpp:767
bool withMoments
Definition tessellator.hpp:769
void init(size_t facetCap_, bool withAreaGrad_, bool withMoments_)
Definition tessellator.hpp:777
void alloc()
Definition tessellator.hpp:792
Kokkos::View< int *, MemSpace > facetCursor
Definition tessellator.hpp:772
Kokkos::View< Real *, MemSpace > oMoment2
Definition tessellator.hpp:775
Kokkos::View< Real *, MemSpace > oDV
Definition tessellator.hpp:771
Published, read-only device data layer for the tessellation (migration §2.1, §3).