peclet.voro 1.0.4
Device-native moving-particle Voronoi dynamics
Loading...
Searching...
No Matches
reeval_tessellation.hpp
Go to the documentation of this file.
1
24#ifndef PECLET_VORO_REEVAL_TESSELLATION_HPP
25#define PECLET_VORO_REEVAL_TESSELLATION_HPP
26
27#include <Kokkos_Core.hpp>
28#include <string>
29
30#include "peclet/core/common/view.hpp"
33#include "peclet/voro/sdf.hpp" // WallStore
35#include "peclet/voro/tessellator.hpp" // areaGradEnumerate / areaGradFill (rung A3)
37
38namespace peclet::voro {
39
54template <class Real, int MAXP, int MAXT, class Policy = Voronoi>
56 const Kokkos::View<Real*, peclet::core::MemSpace>& pos,
57 const Kokkos::View<Real*, peclet::core::MemSpace>& vol, int N,
58 const Real L[3], WallStore<Real> wall = {},
59 Kokkos::View<Real*, peclet::core::MemSpace> xRef = {},
60 bool withAreaGrad = false, bool withMoments = false,
61 Kokkos::View<Real*, peclet::core::MemSpace> weight = {}) {
62 using Mem = peclet::core::MemSpace;
63 using Exec = peclet::core::ExecSpace;
64 using Cell = ConvexCell<Real, MAXP, MAXT, false>;
65 using Kokkos::view_alloc;
66 using Kokkos::WithoutInitializing;
67 const Real Lx = L[0], Ly = L[1], Lz = L[2];
68 TopologyStore<MAXP, MAXT> st = store;
69 Kokkos::View<Real*, Mem> P = pos;
70
71 // ---- 1. per-cell live-face count (topology only: the >=3-incident-live-triangle criterion) ----
72 Kokkos::View<int*, Mem> facetCount("rp.facetCount", static_cast<std::size_t>(N));
73 Kokkos::View<int*, Mem> edgeCount("rp.edgeCount", withAreaGrad ? static_cast<std::size_t>(N) : 0);
74 constexpr int kMaxF = MAXP; // == CellBuilder::MAXF_TMP: what the cold build can publish
75 const bool wag = withAreaGrad;
76 Kokkos::parallel_for(
77 "reevalPublish.count", Kokkos::RangePolicy<Exec>(0, N), KOKKOS_LAMBDA(const int i) {
78 Cell c;
79 st.load(i, c, Lx, Ly, Lz);
80 int nf = 0;
81 int faces[kMaxF];
82 for (int k = 0; k < c.np; ++k) {
83 int cnt = 0;
84 for (int t = 0; t < c.nt; ++t)
85 if (c.alive[t] && (c.t0[t] == k || c.t1[t] == k || c.t2[t] == k))
86 ++cnt;
87 if (cnt >= 3) {
88 if (nf < kMaxF)
89 faces[nf] = k;
90 ++nf;
91 }
92 }
93 facetCount(i) = nf;
94 if (wag) {
95 unsigned char part[kMaxF * Cell::MAXFV], pcnt[kMaxF];
96 bool full = false;
97 edgeCount(i) = (nf > 0 && nf <= kMaxF)
98 ? areaGradEnumerate<Cell, kMaxF>(c, faces, nf, part, pcnt, full)
99 : 0;
100 }
101 });
102
103 // ---- 2. exclusive prefix sum -> per-cell facet base + total facet count ----
104 Kokkos::View<int*, Mem> base(view_alloc(std::string("rp.base"), WithoutInitializing),
105 static_cast<std::size_t>(N));
106 int nFacets = 0;
107 {
108 Kokkos::View<int*, Mem> fc = facetCount, bs = base;
109 Kokkos::parallel_scan(
110 "reevalPublish.scan", N,
111 KOKKOS_LAMBDA(const int i, int& upd, const bool final_pass) {
112 if (final_pass)
113 bs(i) = upd;
114 upd += fc(i);
115 },
116 nFacets);
117 }
118 Kokkos::View<int*, Mem> ebase(view_alloc(std::string("rp.ebase"), WithoutInitializing),
119 withAreaGrad ? static_cast<std::size_t>(N) : 0);
120 int nEdges = 0;
121 if (withAreaGrad) {
122 Kokkos::View<int*, Mem> ec = edgeCount, eb = ebase;
123 Kokkos::parallel_scan(
124 "reevalPublish.scanEdges", N,
125 KOKKOS_LAMBDA(const int i, int& upd, const bool final_pass) {
126 if (final_pass)
127 eb(i) = upd;
128 upd += ec(i);
129 },
130 nEdges);
131 }
132
133 // ---- 3. fill the facet CSR: reeval geometry, then facetGeometry per live face ----
134 const std::size_t nF = static_cast<std::size_t>(nFacets);
135 Kokkos::View<gid_t*, Mem> fNbr(view_alloc(std::string("rp.fNbr"), WithoutInitializing), nF);
136 Kokkos::View<Real*, Mem> fArea(view_alloc(std::string("rp.fArea"), WithoutInitializing), nF * 3);
137 Kokkos::View<Real*, Mem> fDV(view_alloc(std::string("rp.fDV"), WithoutInitializing), nF * 3);
138 Kokkos::View<Real*, Mem> fConn(view_alloc(std::string("rp.fConn"), WithoutInitializing), nF * 3);
139 const std::size_t nE = static_cast<std::size_t>(nEdges);
140 Kokkos::View<int*, Mem> eOff(view_alloc(std::string("rp.eOff"), WithoutInitializing),
141 withAreaGrad ? nF : 0);
142 Kokkos::View<int*, Mem> eCnt(view_alloc(std::string("rp.eCnt"), WithoutInitializing),
143 withAreaGrad ? nF : 0);
144 Kokkos::View<int*, Mem> eFacet(view_alloc(std::string("rp.eFacet"), WithoutInitializing),
145 withAreaGrad ? nE : 0);
146 Kokkos::View<Real*, Mem> eGrad(view_alloc(std::string("rp.eGrad"), WithoutInitializing),
147 withAreaGrad ? nE * 3 : 0);
148 Kokkos::View<Real*, Mem> eLen(view_alloc(std::string("rp.eLen"), WithoutInitializing),
149 withAreaGrad ? nE * 4 : 0);
150 Kokkos::View<Real*, Mem> fM2(view_alloc(std::string("rp.fM2"), WithoutInitializing),
151 withMoments ? nF : 0);
152 const bool wm = withMoments;
153 {
154 Kokkos::View<int*, Mem> bs = base, eb = ebase;
155 WallStore<Real> Wl = wall;
156 Kokkos::View<Real*, Mem> XR = xRef;
157 Kokkos::View<Real*, Mem> W = weight;
158 const Real Lxh = Real(0.5) * Lx, Lyh = Real(0.5) * Ly, Lzh = Real(0.5) * Lz;
159 Kokkos::parallel_for(
160 "reevalPublish.fill", Kokkos::RangePolicy<Exec>(0, N), KOKKOS_LAMBDA(const int i) {
161 Cell c;
162 st.load(i, c, Lx, Ly, Lz);
163 if (Wl.active()) { // restore the wall planes for the seed's displacement since save
164 Real dx = P(3 * i) - XR(3 * i), dy = P(3 * i + 1) - XR(3 * i + 1),
165 dz = P(3 * i + 2) - XR(3 * i + 2);
166 dx = dx > Lxh ? dx - Lx : (dx < -Lxh ? dx + Lx : dx);
167 dy = dy > Lyh ? dy - Ly : (dy < -Lyh ? dy + Ly : dy);
168 dz = dz > Lzh ? dz - Lz : (dz < -Lzh ? dz + Lz : dz);
169 Wl.load(i, c, dx, dy, dz);
170 }
171 const Real* wPtr = W.data(); // force-capture W OUTSIDE the constexpr-if (nvcc rule)
172 (void)wPtr;
173 if constexpr (Policy::kHasWeightDof)
174 c.template reevalGeometry<Policy>(P(3 * i), P(3 * i + 1), P(3 * i + 2), P.data(), Lx,
175 wPtr[i], wPtr);
176 else
177 c.reevalGeometry(P(3 * i), P(3 * i + 1), P(3 * i + 2), P.data(), Lx);
178 int idx = 0;
179 const int b = bs(i);
180 int faces[kMaxF];
181 for (int k = 0; k < c.np; ++k) {
182 int cnt = 0;
183 for (int t = 0; t < c.nt; ++t)
184 if (c.alive[t] && (c.t0[t] == k || c.t1[t] == k || c.t2[t] == k))
185 ++cnt;
186 if (cnt < 3)
187 continue; // not a polygon face — same criterion as the count pass
188 if (idx < kMaxF)
189 faces[idx] = k;
190 Real area[3] = {0, 0, 0}, dv[3] = {0, 0, 0}, conn[3];
191 conn[0] = Real(2) * c.n[k][0];
192 conn[1] = Real(2) * c.n[k][1];
193 conn[2] = Real(2) * c.n[k][2];
194 if constexpr (Policy::kHasWeightDof) {
195 // Power: the generator connector the radical plane was rebuilt from (the same
196 // cubic min-image as reevalGeometry), not 2·foot; box/wall planes keep 2·foot.
197 const int gk = c.pnbr[k];
198 if (gk >= 0) {
199 const Real Lh = Real(0.5) * Lx;
200 Real rx = P(3 * gk) - P(3 * i), ry = P(3 * gk + 1) - P(3 * i + 1),
201 rz = P(3 * gk + 2) - P(3 * i + 2);
202 conn[0] = rx > Lh ? rx - Lx : (rx < -Lh ? rx + Lx : rx);
203 conn[1] = ry > Lh ? ry - Lx : (ry < -Lh ? ry + Lx : ry);
204 conn[2] = rz > Lh ? rz - Lx : (rz < -Lh ? rz + Lx : rz);
205 }
206 c.facetGeometryFrom(k, conn, area, dv); // zeros for a degenerate face
207 } else {
208 // zeros for a degenerate face (matches buildCell)
209 c.facetGeometry(k, area, dv, conn);
210 }
211 const std::size_t g = static_cast<std::size_t>(b + idx);
212 fNbr(g) = static_cast<gid_t>(c.pnbr[k]);
213 for (int cc = 0; cc < 3; ++cc) {
214 fArea(3 * g + cc) = area[cc];
215 fDV(3 * g + cc) = dv[cc];
216 fConn(3 * g + cc) = conn[cc];
217 }
218 if (wm)
219 fM2(g) = c.faceMoment2(k);
220 ++idx;
221 }
222 if (wag && idx > 0 && idx <= kMaxF) {
223 unsigned char part[kMaxF * Cell::MAXFV], pcnt[kMaxF];
224 bool full = false;
225 areaGradEnumerate<Cell, kMaxF>(c, faces, idx, part, pcnt, full);
226 areaGradFill<Cell, kMaxF>(c, faces, idx, part, pcnt, b, eb(i), eOff, eCnt, eFacet,
227 eGrad, eLen);
228 }
229 });
230 }
231
232 // ---- 4. assemble the published view (cellVolume reuses the repair's volumes) ----
233 TessellationView<Real> view;
234 view.cellFacetOffset = base;
235 view.cellFacetCount = facetCount;
236 view.cellVolume = vol;
237 view.cellSeedId = Kokkos::View<gid_t*, Mem>(
238 view_alloc(std::string("rp.seedId"), WithoutInitializing), static_cast<std::size_t>(N));
239 {
240 Kokkos::View<gid_t*, Mem> vSeed = view.cellSeedId;
241 Kokkos::parallel_for(
242 "reevalPublish.seedId", Kokkos::RangePolicy<Exec>(0, N),
243 KOKKOS_LAMBDA(const int i) { vSeed(i) = static_cast<gid_t>(i); });
244 }
245 view.facetNeighbor = fNbr;
246 view.facetArea = fArea;
247 view.facetConnect = fDV;
248 view.facetConnVec = fConn;
249 if (withMoments)
250 view.facetMoment2 = fM2;
251 if (withAreaGrad) {
252 view.facetEdgeOffset = eOff;
253 view.facetEdgeCount = eCnt;
254 view.edgeFacet = eFacet;
255 view.edgeAreaGrad = eGrad;
256 view.edgeLength =
257 Kokkos::View<Real*, Mem>(view_alloc(std::string("rp.edgeLength"), WithoutInitializing), nE);
258 {
259 auto el = view.edgeLength;
260 Kokkos::parallel_for(
261 "reevalPublish.edgeLength", Kokkos::RangePolicy<Exec>(0, nE),
262 KOKKOS_LAMBDA(const int e) { el(e) = eLen((size_t)e * 4); });
263 }
264 }
265 return view;
266}
267
268} // namespace peclet::voro
269
270#endif // PECLET_VORO_REEVAL_TESSELLATION_HPP
peclet::core::MemSpace Mem
Definition voro_bindings_common.hpp:131
Device-native Voronoi tessellation, geometry and finite-volume physics.
Definition convex_cell.hpp:41
std::int32_t gid_t
Definition tessellation_view.hpp:41
TessellationView< Real > reevalPublish(const TopologyStore< MAXP, MAXT > &store, const Kokkos::View< Real *, peclet::core::MemSpace > &pos, const Kokkos::View< Real *, peclet::core::MemSpace > &vol, int N, const Real L[3], WallStore< Real > wall={}, Kokkos::View< Real *, peclet::core::MemSpace > xRef={}, bool withAreaGrad=false, bool withMoments=false, Kokkos::View< Real *, peclet::core::MemSpace > weight={})
Definition reeval_tessellation.hpp:55
Definition tessellation_view.hpp:111
Definition topology_store.hpp:39
Definition sdf.hpp:352
Published, read-only device data layer for the tessellation (migration §2.1, §3).