peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
contact_preprocessing.hpp
Go to the documentation of this file.
1
16#ifndef DEM_CONTACT_PREPROCESSING_HPP
17#define DEM_CONTACT_PREPROCESSING_HPP
18
19#include <cstddef>
20#include <cstdint>
21#include <Kokkos_Core.hpp>
22#include <Kokkos_Sort.hpp>
23#include <utility>
24
25#include "dem_portable.hpp" // F4, cross3
26
27namespace peclet::dem {
28
29using CpExec = Kokkos::DefaultExecutionSpace;
30using CpMem = CpExec::memory_space;
31
32// Launcher parameter bundles (playbook launch-params-struct): Views held BY VALUE (trivially
33// copyable), named as the Particles fields they alias. A launcher unpacks the members it uses into
34// by-value locals before its kernel, so the kernel captures exactly what it captured before.
35
37struct BodyState {
38 Kokkos::View<float* [3], CpMem> pos;
39 Kokkos::View<float* [3], CpMem> vel;
40 Kokkos::View<float* [4], CpMem> quat;
41 Kokkos::View<float* [3], CpMem> angVel;
42 Kokkos::View<float*, CpMem> invMass;
43 Kokkos::View<float* [3], CpMem> invInertia;
44 Kokkos::View<float*, CpMem> scale;
45 Kokkos::View<int*, CpMem> shapeId;
46};
47
49struct BodyPred {
50 Kokkos::View<float* [3], CpMem> posPred;
51 Kokkos::View<float* [3], CpMem> velPred;
52 Kokkos::View<float* [4], CpMem> quatPred;
53 Kokkos::View<float* [3], CpMem> angVelPred;
54};
55
58struct BodyDelta {
59 Kokkos::View<float* [3], CpMem> deltaPos;
60 Kokkos::View<float* [4], CpMem> deltaQuat;
61 Kokkos::View<float* [3], CpMem> deltaVel;
62 Kokkos::View<float* [3], CpMem> deltaAngVel;
63 Kokkos::View<int*, CpMem> constraintCounts;
64};
65
69 Kokkos::View<const unsigned long long*, CpMem> keys;
70 Kokkos::View<const int*, CpMem> color;
71 int count = 0;
72};
73
76struct SleepState {
77 Kokkos::View<unsigned char*, CpMem> asleep;
78 Kokkos::View<unsigned char*, CpMem> sleepCounter;
79 Kokkos::View<unsigned char*, CpMem> sleepMovingWall;
80 Kokkos::View<int*, CpMem> sleepCurCount;
81 Kokkos::View<int*, CpMem> sleepPrevCount;
82};
83
87 Kokkos::View<long long*, CpMem> bodyWinner;
88 Kokkos::View<std::uint64_t*, CpMem> bodyColorMask;
89};
90
94struct PairLedger {
95 Kokkos::View<unsigned long long*, CpMem> keys;
96 Kokkos::View<float*, CpMem> lambda;
97 Kokkos::View<float* [3], CpMem> lambdaT;
98 Kokkos::View<float*, CpMem> restBank;
99 Kokkos::View<float*, CpMem> restVPeak;
100};
101
103struct ContactC {
104 int bodyA;
105 int bodyB; // < 0 => boundary/static: -1 - wallIndex (wallContactId)
106 F4 normal; // .xyz = world normal
107 F4 rA; // lever arm on A
108 F4 rB; // lever arm on B
109 float dist; // signed penetration (>0 => inactive)
111 float weight;
112 // --- moving / per-material boundary (idB<0) extension; sentinels for body-body & static planes
113 // so the solvers fall back to the global material and a zero wall velocity (unchanged behaviour).
114 // ---
115 F4 boundaryVel{0.0f, 0.0f, 0.0f, 0.0f}; // wall surface velocity at the contact point (xyz)
116 float boundaryRestitution{-1.0f}; // per-wall normal restitution; < 0 => use the global one
117 float boundaryFriction{-1.0f}; // per-wall Coulomb friction; < 0 => use the global one
118};
119
124KOKKOS_INLINE_FUNCTION bool normalOfA(const ContactC& c) {
125 return c.normal.w != 0.0f;
126}
127
133KOKKOS_INLINE_FUNCTION constexpr int wallContactId(int wallIndex) {
134 return -1 - wallIndex;
135}
136KOKKOS_INLINE_FUNCTION constexpr int wallIndexOf(int bodyB) {
137 return -1 - bodyB;
138}
139
141struct ManifoldC {
142 int bodyA;
143 int bodyB;
150 // Σ over the manifold's active contacts of the boundary (idB<0) extension; the velocity solve
151 // averages by num_points. wallVel_sum -> the wall's surface velocity seen by this contact patch;
152 // restitution_sum -> per-wall restitution (a < 0 average keeps the global material).
153 F4 wallVel_sum{0.0f, 0.0f, 0.0f, 0.0f};
154 float restitution_sum{0.0f};
155 float friction_sum{0.0f}; // per-contact mu (pair table / wall); a < 0 average = global material
156};
157
162KOKKOS_INLINE_FUNCTION unsigned long long pairKeyFromGids(unsigned a, unsigned b) {
163 const unsigned hi = a < b ? a : b, lo = a < b ? b : a;
164 return (static_cast<unsigned long long>(hi) << 32) | lo;
165}
166
170template <class KeysView>
171KOKKOS_INLINE_FUNCTION int lowerBoundKey(const KeysView& keys, int count, unsigned long long key) {
172 int lo = 0, hi = count;
173 while (lo < hi) {
174 const int mid = (lo + hi) >> 1;
175 if (keys(mid) < key)
176 lo = mid + 1;
177 else
178 hi = mid;
179 }
180 return lo;
181}
182
188KOKKOS_INLINE_FUNCTION unsigned long long pairKeyOf(const ManifoldC& m,
189 Kokkos::View<const int*, CpMem> realIdx) {
190 const unsigned a = static_cast<unsigned>(realIdx(m.bodyA));
191 const unsigned b = (m.bodyB >= 0) ? static_cast<unsigned>(realIdx(m.bodyB)) : 0xFFFFFFFFu;
192 return pairKeyFromGids(a, b);
193}
194
198inline void markPersistentManifolds(Kokkos::View<const ManifoldC*, CpMem> manifolds,
199 int numManifolds, Kokkos::View<const int*, CpMem> realIdx,
200 Kokkos::View<const int*, CpMem> keyIdx,
201 Kokkos::View<const unsigned long long*, CpMem> prevKeys,
202 int prevCount, Kokkos::View<unsigned long long*, CpMem> outKeys,
203 Kokkos::View<unsigned char*, CpMem> outFlags) {
204 (void)realIdx;
205 CpExec space;
206 Kokkos::parallel_for(
207 "peclet::dem::mark_persistent", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
208 KOKKOS_LAMBDA(int idx) {
209 const ManifoldC m = manifolds(idx);
210 if (m.num_points <= 0) {
211 outKeys(idx) = ~0ull; // hi = 0xFFFFFFFF: can never match a live manifold's key
212 outFlags(idx) = 0;
213 return;
214 }
215 const unsigned long long k = pairKeyOf(m, keyIdx);
216 outKeys(idx) = k;
217 const int lo = lowerBoundKey(prevKeys, prevCount, k);
218 outFlags(idx) = (lo < prevCount && prevKeys(lo) == k) ? 1 : 0;
219 });
220}
221
228inline void gatherWarmLambda(Kokkos::View<const ManifoldC*, CpMem> manifolds, int numManifolds,
229 Kokkos::View<const int*, CpMem> realIdx,
230 Kokkos::View<const int*, CpMem> keyIdx, const PairLedger& prev,
231 Kokkos::View<const float*, CpMem> prevPosImpulse, int prevCount,
232 const PairLedger& out, Kokkos::View<float*, CpMem> outPosImpulse,
233 Kokkos::View<unsigned char*, CpMem> outMatched = {},
234 bool dedupTwins = true) {
235 const Kokkos::View<const unsigned long long*, CpMem> prevKeys = prev.keys;
236 const Kokkos::View<const float*, CpMem> prevLambda = prev.lambda;
237 const Kokkos::View<const float* [3], CpMem> prevLambdaT = prev.lambdaT;
238 const Kokkos::View<const float*, CpMem> prevRestBank = prev.restBank,
239 prevRestVPeak = prev.restVPeak;
240 const Kokkos::View<unsigned long long*, CpMem> outKeys = out.keys;
241 const Kokkos::View<float*, CpMem> outWarm = out.lambda;
242 const Kokkos::View<float* [3], CpMem> outWarmT = out.lambdaT;
243 const Kokkos::View<float*, CpMem> outRestBank = out.restBank, outRestVPeak = out.restVPeak;
244 CpExec space;
245 Kokkos::parallel_for(
246 "peclet::dem::gather_warm", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
247 KOKKOS_LAMBDA(int idx) {
248 const ManifoldC m = manifolds(idx);
249 bool dup = false;
250 if (dedupTwins && m.num_points > 0 && m.bodyB >= 0 && realIdx(m.bodyA) > realIdx(m.bodyB))
251 dup = true; // periodic dedup (single rank; §4.2 item 2)
252 if (m.num_points <= 0 || dup) {
253 outKeys(idx) = ~0ull;
254 outWarm(idx) = 0.0f;
255 outWarmT(idx, 0) = outWarmT(idx, 1) = outWarmT(idx, 2) = 0.0f;
256 outPosImpulse(idx) = 0.0f;
257 outRestBank(idx) = 0.0f;
258 outRestVPeak(idx) = 0.0f;
259 return;
260 }
261 const unsigned long long k = pairKeyOf(m, keyIdx);
262 outKeys(idx) = k;
263 const int lo = lowerBoundKey(prevKeys, prevCount, k);
264 const bool hit = (lo < prevCount && prevKeys(lo) == k);
265 if (hit && outMatched.extent(0) > 0)
266 outMatched(lo) = 1; // prev entry survives; unmatched entries orphan their bank
267 outWarm(idx) = hit ? prevLambda(lo) : 0.0f;
268 outWarmT(idx, 0) = hit ? prevLambdaT(lo, 0) : 0.0f;
269 outWarmT(idx, 1) = hit ? prevLambdaT(lo, 1) : 0.0f;
270 outWarmT(idx, 2) = hit ? prevLambdaT(lo, 2) : 0.0f;
271 outPosImpulse(idx) = hit ? prevPosImpulse(lo) : 0.0f;
272 outRestBank(idx) = hit ? prevRestBank(lo) : 0.0f;
273 outRestVPeak(idx) = hit ? prevRestVPeak(lo) : 0.0f;
274 });
275}
276
280inline void commitPairKeysLambda(const PairLedger& cur, const PairLedger& prev,
281 Kokkos::View<int*, CpMem> perm, // pooled scratch, >= n
282 int numManifolds,
283 // Optional: carry the per-manifold colour by pair key too
284 // (single-GPU incremental colouring; empty views = skip).
285 Kokkos::View<const int*, CpMem> color = {},
286 Kokkos::View<int*, CpMem> prevColor = {}) {
287 const Kokkos::View<const unsigned long long*, CpMem> keys = cur.keys;
288 const Kokkos::View<const float*, CpMem> lambda = cur.lambda;
289 const Kokkos::View<const float* [3], CpMem> lambdaT = cur.lambdaT;
290 const Kokkos::View<const float*, CpMem> restBank = cur.restBank, restVPeak = cur.restVPeak;
291 const Kokkos::View<unsigned long long*, CpMem> prevKeys = prev.keys;
292 const Kokkos::View<float*, CpMem> prevLambda = prev.lambda;
293 const Kokkos::View<float* [3], CpMem> prevLambdaT = prev.lambdaT;
294 const Kokkos::View<float*, CpMem> prevRestBank = prev.restBank, prevRestVPeak = prev.restVPeak;
295 if (numManifolds <= 0)
296 return;
297 CpExec space;
298 const int n = numManifolds;
299 const auto rng = Kokkos::pair<int, int>(0, n);
300 auto kd = Kokkos::subview(prevKeys, rng);
301 Kokkos::deep_copy(space, kd, Kokkos::subview(keys, rng));
302 Kokkos::parallel_for(
303 "peclet::dem::commit_iota", Kokkos::RangePolicy<CpExec>(space, 0, n),
304 KOKKOS_LAMBDA(int i) { perm(i) = i; });
305 {
306 auto pd = Kokkos::subview(perm, rng);
307 Kokkos::Experimental::sort_by_key(space, kd, pd);
308 }
309 Kokkos::View<float*, CpMem> pl = prevLambda;
310 Kokkos::View<float* [3], CpMem> plt = prevLambdaT;
311 Kokkos::View<float*, CpMem> prb = prevRestBank;
312 Kokkos::View<float*, CpMem> prv = prevRestVPeak;
313 const bool carryColor = color.extent(0) > 0 && prevColor.extent(0) > 0;
314 Kokkos::View<const int*, CpMem> col = color;
315 Kokkos::View<int*, CpMem> pcol = prevColor;
316 Kokkos::parallel_for(
317 "peclet::dem::commit_gather", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
318 const int j = perm(i);
319 pl(i) = lambda(j);
320 plt(i, 0) = lambdaT(j, 0);
321 plt(i, 1) = lambdaT(j, 1);
322 plt(i, 2) = lambdaT(j, 2);
323 prb(i) = restBank(j);
324 prv(i) = restVPeak(j);
325 if (carryColor)
326 pcol(i) = col(j);
327 });
328 space.fence();
329}
330
332inline void commitPairKeys(Kokkos::View<const unsigned long long*, CpMem> keys,
333 Kokkos::View<unsigned long long*, CpMem> prevKeys, int numManifolds) {
334 if (numManifolds <= 0)
335 return;
336 CpExec space;
337 auto src = Kokkos::subview(keys, Kokkos::pair<int, int>(0, numManifolds));
338 auto dst = Kokkos::subview(prevKeys, Kokkos::pair<int, int>(0, numManifolds));
339 Kokkos::deep_copy(space, dst, src);
340 Kokkos::sort(space, dst);
341 space.fence();
342}
343
349inline void updateGroundedLevels(Kokkos::View<const ManifoldC*, CpMem> manifolds, int numManifolds,
350 Kokkos::View<const int*, CpMem> realIdx,
351 Kokkos::View<const float* [3], CpMem> posPred, F3 gHat,
352 Kokkos::View<unsigned char*, CpMem> grounded, int numReal,
353 int sweeps, int decay) {
354 CpExec space;
355 Kokkos::parallel_for(
356 "peclet::dem::grounded_decay", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
357 KOKKOS_LAMBDA(int i) {
358 const int g = static_cast<int>(grounded(i)) - decay;
359 grounded(i) = static_cast<unsigned char>(g > 0 ? g : 0);
360 });
361 for (int s = 0; s < sweeps; ++s) {
362 Kokkos::parallel_for(
363 "peclet::dem::grounded_sweep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
364 KOKKOS_LAMBDA(int idx) {
365 const ManifoldC m = manifolds(idx);
366 if (m.num_points <= 0)
367 return;
368 const int realA = realIdx(m.bodyA);
369 if (m.bodyB < 0) {
370 Kokkos::atomic_max(&grounded(realA), static_cast<unsigned char>(255));
371 return;
372 }
373 const int realB = realIdx(m.bodyB);
374 const F3 dx = sub3(ldF3(posPred, m.bodyA), ldF3(posPred, m.bodyB));
375 const float up = -(dx.x * gHat.x + dx.y * gHat.y + dx.z * gHat.z); // >0: A above B
376 const float thr = 0.3f * Kokkos::sqrt(dot3(dx, dx));
377 if (up > thr) { // B supports A
378 const int lvl = static_cast<int>(grounded(realB)) - 1;
379 if (lvl > 0)
380 Kokkos::atomic_max(&grounded(realA), static_cast<unsigned char>(lvl));
381 } else if (up < -thr) { // A supports B
382 const int lvl = static_cast<int>(grounded(realA)) - 1;
383 if (lvl > 0)
384 Kokkos::atomic_max(&grounded(realB), static_cast<unsigned char>(lvl));
385 }
386 });
387 }
388}
389
397inline constexpr int kLevelInf = 1 << 28;
398inline void computeHeightLevels(Kokkos::View<const ManifoldC*, CpMem> manifolds, int numManifolds,
399 Kokkos::View<const int*, CpMem> realIdx,
400 Kokkos::View<const float* [3], CpMem> posPred, F3 gHat,
401 Kokkos::View<int*, CpMem> heights, int numReal) {
402 CpExec space;
403 Kokkos::parallel_for(
404 "peclet::dem::height_init", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
405 KOKKOS_LAMBDA(int i) { heights(i) = kLevelInf; });
406 const int maxSweeps = 1024; // >= deepest supported column; early exit ends real runs sooner
407 for (int s = 0; s < maxSweeps; ++s) {
408 int changed = 0;
409 Kokkos::parallel_reduce(
410 "peclet::dem::height_sweep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
411 KOKKOS_LAMBDA(int idx, int& acc) {
412 const ManifoldC m = manifolds(idx);
413 if (m.num_points <= 0)
414 return;
415 const int realA = realIdx(m.bodyA);
416 if (m.bodyB < 0) {
417 if (Kokkos::atomic_fetch_min(&heights(realA), 0) > 0)
418 acc += 1;
419 return;
420 }
421 const int realB = realIdx(m.bodyB);
422 const F3 dx = sub3(ldF3(posPred, m.bodyA), ldF3(posPred, m.bodyB));
423 const float up = -(dx.x * gHat.x + dx.y * gHat.y + dx.z * gHat.z); // >0: A above B
424 const float thr = 0.3f * Kokkos::sqrt(dot3(dx, dx));
425 if (up > thr) { // B supports A
426 const int cand = heights(realB);
427 if (cand < kLevelInf && Kokkos::atomic_fetch_min(&heights(realA), cand + 1) > cand + 1)
428 acc += 1;
429 } else if (up < -thr) { // A supports B
430 const int cand = heights(realA);
431 if (cand < kLevelInf && Kokkos::atomic_fetch_min(&heights(realB), cand + 1) > cand + 1)
432 acc += 1;
433 }
434 },
435 changed);
436 if (changed == 0)
437 break;
438 }
439 space.fence();
440}
441
449inline void buildColorBuckets(Kokkos::View<const int*, CpMem> colorOf, int n, int numColors,
450 Kokkos::View<int*, CpMem> perm,
451 Kokkos::View<int*, CpMem> cursor, // scratch, >= 64 ints
452 std::vector<int>& offs) {
453 offs.assign(static_cast<std::size_t>(numColors) + 1, 0);
454 if (n <= 0 || numColors <= 0)
455 return;
456 CpExec space;
457 auto cnt = Kokkos::subview(cursor, Kokkos::pair<int, int>(0, numColors));
458 Kokkos::deep_copy(space, cnt, 0);
459 Kokkos::parallel_for(
460 "peclet::dem::bucket_hist", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
461 const int c = colorOf(i);
462 if (c >= 0)
463 Kokkos::atomic_add(&cursor(c), 1);
464 });
465 auto hCnt = Kokkos::create_mirror_view(cnt);
466 Kokkos::deep_copy(space, hCnt, cnt);
467 space.fence();
468 for (int c = 0; c < numColors; ++c)
469 offs[static_cast<std::size_t>(c) + 1] = offs[static_cast<std::size_t>(c)] + hCnt(c);
470 for (int c = 0; c < numColors; ++c)
471 hCnt(c) = offs[static_cast<std::size_t>(c)];
472 Kokkos::deep_copy(space, cnt, hCnt);
473 Kokkos::parallel_for(
474 "peclet::dem::bucket_scatter", Kokkos::RangePolicy<CpExec>(space, 0, n),
475 KOKKOS_LAMBDA(int i) {
476 const int c = colorOf(i);
477 if (c >= 0)
478 perm(Kokkos::atomic_fetch_add(&cursor(c), 1)) = i;
479 });
480}
481
485KOKKOS_INLINE_FUNCTION long long colorKey(int idx) {
486 unsigned int z = static_cast<unsigned>(idx) + 0x9e3779b9u;
487 z = (z ^ (z >> 16)) * 0x21f0aaadu;
488 z = (z ^ (z >> 15)) * 0x735a2d97u;
489 z ^= (z >> 15);
490 // priority in the high word, unique index in the low word (unique key per edge; >= 0)
491 return (static_cast<long long>(z & 0x7fffffffu) << 32) | static_cast<unsigned>(idx);
492}
493
495KOKKOS_INLINE_FUNCTION std::uint64_t pairKey(const ContactC& c) {
496 const int idA = c.bodyA, idB = c.bodyB;
497 if (idB < 0)
498 return (static_cast<std::uint64_t>(static_cast<unsigned>(idA)) << 32) | 0xFFFFFFFFu;
499 const unsigned u = (idA < idB) ? idA : idB;
500 const unsigned v = (idA < idB) ? idB : idA;
501 return (static_cast<std::uint64_t>(u) << 32) | v;
502}
503
507inline constexpr std::uint64_t kNonOwnedPairBit = 1ull << 63;
508
513inline constexpr int kColorPalette = 64;
514inline constexpr int kColorUncolourable = -3;
515
523 Kokkos::View<const int*, CpMem> a, b;
524 Kokkos::View<const unsigned char*, CpMem> split; // per slot: 1 = its body has k > 1
525 float omega = 1.0f; // the phase's split relaxation (§4.5)
526 KOKKOS_INLINE_FUNCTION int slotA(int e, int def) const {
527 if (a.extent(0) > 0) {
528 const int s = a(e);
529 if (s >= 0)
530 return s;
531 }
532 return def;
533 }
534 KOKKOS_INLINE_FUNCTION int slotB(int e, int def) const {
535 if (b.extent(0) > 0) {
536 const int s = b(e);
537 if (s >= 0)
538 return s;
539 }
540 return def;
541 }
542
543 KOKKOS_INLINE_FUNCTION bool relax(int sA, int sB) const {
544 return split.extent(0) > 0 && (split(sA) != 0 || (sB >= 0 && split(sB) != 0));
545 }
546};
547
550struct ColorEdge {
551 int a, b;
552 long long key;
553};
554
557 const char* initBodies;
558 const char* initEdges;
559 const char* resetWinner;
560 const char* contend;
561 const char* commit;
562 const char* max;
563 const char* leftover;
564};
565
578template <class EdgeAccessor>
579int colorEdges(const EdgeAccessor& edges, int numEdges, int numVertices, int vertexSpan,
580 Kokkos::View<int*, CpMem> color, const ColorScratch& scratch, int& leftover) {
581 const Kokkos::View<long long*, CpMem> bodyWinner = scratch.bodyWinner;
582 const Kokkos::View<std::uint64_t*, CpMem> bodyMask = scratch.bodyColorMask;
583 constexpr ColorLabels labels = EdgeAccessor::labels();
584 leftover = 0;
585 CpExec space;
586 if (numEdges <= 0 || numVertices <= 0)
587 return 0;
588 const int nV = vertexSpan > 0 ? vertexSpan : numVertices;
589 Kokkos::parallel_for(
590 labels.initBodies, Kokkos::RangePolicy<CpExec>(space, 0, nV),
591 KOKKOS_LAMBDA(int i) { bodyMask(i) = 0; });
592 Kokkos::parallel_for(
593 labels.initEdges, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
594 KOKKOS_LAMBDA(int idx) { color(idx) = edges.initialColor(idx); });
595
596 int remaining = 1, prevRemaining = -1;
597 // Round cap (docs/contact_solve_framework.md §12 S7): the bound is the edge count + 2. Every
598 // round commits at least the globally highest-key uncoloured edge (keys are unique: colorKey
599 // carries the index in its low word), so the loop terminates within numEdges rounds; O(log n)
600 // in practice.
601 const int maxRounds = numEdges + 2;
602 for (int round = 0; round < maxRounds && remaining > 0; ++round) {
603 Kokkos::parallel_for(
604 labels.resetWinner, Kokkos::RangePolicy<CpExec>(space, 0, nV),
605 KOKKOS_LAMBDA(int i) { bodyWinner(i) = -1; });
606 Kokkos::parallel_for(
607 labels.contend, Kokkos::RangePolicy<CpExec>(space, 0, numEdges), KOKKOS_LAMBDA(int idx) {
608 if (color(idx) != -1)
609 return;
610 const ColorEdge e = edges.edge(idx);
611 Kokkos::atomic_max(&bodyWinner(e.a), e.key);
612 if (e.b >= 0)
613 Kokkos::atomic_max(&bodyWinner(e.b), e.key);
614 });
615 int rem = 0;
616 Kokkos::parallel_reduce(
617 labels.commit, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
618 KOKKOS_LAMBDA(int idx, int& acc) {
619 if (color(idx) != -1)
620 return;
621 const ColorEdge e = edges.edge(idx);
622 // Winner iff it holds BOTH its endpoints -> no uncoloured conflict, sole writer of a/b.
623 if (bodyWinner(e.a) != e.key || (e.b >= 0 && bodyWinner(e.b) != e.key)) {
624 acc += 1;
625 return;
626 }
627 std::uint64_t forbidden = bodyMask(e.a);
628 if (e.b >= 0)
629 forbidden |= bodyMask(e.b);
630 int c = 0; // lowest free colour of 64; never forced (§4.2 item 1)
631 while (c < kColorPalette && ((forbidden >> c) & 1))
632 ++c;
633#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 3
634 if (c > 62)
635 c = 62; // G13 mutant 3: the old forced colour 62
636#endif
637 if (c == kColorPalette) {
638 color(idx) = kColorUncolourable; // no free colour: neither coloured nor remaining
639 return;
640 }
641 color(idx) = c;
642 const std::uint64_t bit = std::uint64_t(1) << c;
643 bodyMask(e.a) |= bit;
644 if (e.b >= 0)
645 bodyMask(e.b) |= bit;
646 },
647 rem);
648 space.fence();
649 // Stall break: a safety bound only -- every round commits (or marks -3) the highest-key
650 // contender, so rem strictly decreases (§12 S7). Anything left -1 is a leftover.
651 if (rem == prevRemaining)
652 break;
653 prevRemaining = rem;
654 remaining = rem;
655 }
656
657 int maxc = -1;
658 Kokkos::parallel_reduce(
659 labels.max, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
660 KOKKOS_LAMBDA(int idx, int& mx) {
661 if (color(idx) > mx)
662 mx = color(idx);
663 },
664 Kokkos::Max<int>(maxc));
665 Kokkos::parallel_reduce(
666 labels.leftover, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
667 KOKKOS_LAMBDA(int idx, int& acc) {
668 if (color(idx) == -1 || color(idx) == kColorUncolourable)
669 acc += 1;
670 },
671 leftover);
672 space.fence();
673 return maxc + 1;
674}
675
678KOKKOS_INLINE_FUNCTION void decodeKey(std::uint64_t key, int& bodyA, int& bodyB) {
679 const unsigned v = static_cast<unsigned>(key & 0xFFFFFFFFu);
680 bodyA = static_cast<int>((key >> 32) & 0x7FFFFFFFu);
681 bodyB = (v == 0xFFFFFFFFu) ? -1 : static_cast<int>(v);
682}
683
687KOKKOS_INLINE_FUNCTION ManifoldC transformContact(const ContactC& c) {
688 ManifoldC m{};
689 const int idA = c.bodyA, idB = c.bodyB;
690 if (c.dist > 0.0f) {
691 m.num_points = 0;
692 return m; // inactive (sums already zero)
693 }
694 const bool flip = (idB >= 0 && idB < idA);
695 m.num_points = 1;
696
697 const F4 n_vec = c.normal;
698 F4 n_aligned = flip ? n_vec : F4{-n_vec.x, -n_vec.y, -n_vec.z, 0.0f};
699
700 const F4 shift{c.normal.x * c.dist * 0.5f, c.normal.y * c.dist * 0.5f, c.normal.z * c.dist * 0.5f,
701 0.0f};
702 const F4 rA_mid{c.rA.x - shift.x, c.rA.y - shift.y, c.rA.z - shift.z, 0.0f};
703 const F4 rB_mid{c.rB.x + shift.x, c.rB.y + shift.y, c.rB.z + shift.z, 0.0f};
704
705 const F4 r_can = flip ? rB_mid : rA_mid;
706 const F4 r_other = flip ? rA_mid : rB_mid;
707
708 const F4 tau_can = cross3(r_can, n_aligned);
709 const F4 tau_other = cross3(r_other, F4{-n_aligned.x, -n_aligned.y, -n_aligned.z, 0.0f});
710
711 m.normal_sum = F4{n_aligned.x, n_aligned.y, n_aligned.z, 0.0f};
712 m.torque_armA_sum = F4{tau_can.x, tau_can.y, tau_can.z, 0.0f};
713 m.torque_armB_sum = F4{tau_other.x, tau_other.y, tau_other.z, 0.0f};
714 m.rA_sum = F4{r_can.x, r_can.y, r_can.z, 0.0f};
715 m.rB_sum = F4{r_other.x, r_other.y, r_other.z, 0.0f};
716 // Boundary (idB<0) moving-wall extension: carry the wall velocity + per-wall restitution through
717 // to the (count-averaged) velocity solve. Zero / -1 sentinel for body-body & static planes.
721 return m;
722}
723
729template <class Owns>
730inline int partitionContacts(Kokkos::View<ContactC*, CpMem> contacts, int n, const Owns& owns) {
731 if (n == 0)
732 return 0;
733 CpExec space;
734 Kokkos::View<int*, CpMem> before(
735 Kokkos::view_alloc(space, "peclet::dem::cp::partBefore", Kokkos::WithoutInitializing), n);
736 Kokkos::View<ContactC*, CpMem> ct = contacts;
737 int ncOwned = 0;
738 Kokkos::parallel_scan(
739 "peclet::dem::cp::partScan", Kokkos::RangePolicy<CpExec>(space, 0, n),
740 KOKKOS_LAMBDA(int i, int& run, const bool final) {
741 const int f = owns(ct(i)) ? 1 : 0;
742 if (final)
743 before(i) = run; // exclusive
744 run += f;
745 },
746 ncOwned);
747 Kokkos::View<ContactC*, CpMem> part(
748 Kokkos::view_alloc(space, "peclet::dem::cp::part", Kokkos::WithoutInitializing), n);
749 const int no = ncOwned;
750 Kokkos::parallel_for(
751 "peclet::dem::cp::partScatter", Kokkos::RangePolicy<CpExec>(space, 0, n),
752 KOKKOS_LAMBDA(int i) {
753 const bool f = owns(ct(i));
754 part(f ? before(i) : no + (i - before(i))) = ct(i);
755 });
756 Kokkos::deep_copy(space,
757 Kokkos::subview(contacts, std::pair<std::size_t, std::size_t>(
758 0, static_cast<std::size_t>(n))),
759 part);
760 space.fence();
761 return ncOwned;
762}
763
772inline int reduceContactsToManifolds(Kokkos::View<const ContactC*, CpMem> contacts, int n,
773 Kokkos::View<ManifoldC*, CpMem> outManifolds,
774 Kokkos::View<int, CpMem> outCount,
775 Kokkos::View<int*, CpMem> contactSlot = {},
776 int numOwnedContacts = -1, int* numOwnedManifolds = nullptr) {
777 CpExec space;
778 if (n == 0) {
779 Kokkos::deep_copy(space, outCount, 0);
780 if (numOwnedManifolds)
781 *numOwnedManifolds = 0;
782 return 0;
783 }
784
785 // 1. Key every contact and seed an identity permutation.
786 Kokkos::View<std::uint64_t*, CpMem> keys(
787 Kokkos::view_alloc(space, "peclet::dem::cp::keys", Kokkos::WithoutInitializing), n);
788 Kokkos::View<int*, CpMem> perm(
789 Kokkos::view_alloc(space, "peclet::dem::cp::perm", Kokkos::WithoutInitializing), n);
790 const int nOwnedC = numOwnedContacts;
791 Kokkos::parallel_for(
792 "peclet::dem::cp::key", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
793 keys(i) = pairKey(contacts(i)) |
794 ((nOwnedC >= 0 && i >= nOwnedC) ? kNonOwnedPairBit : std::uint64_t{0});
795 perm(i) = i;
796 });
797
798 // 2. Sort the permutation by key (groups equal pairs contiguously).
799 Kokkos::Experimental::sort_by_key(space, keys, perm);
800
801 // 3. Segment id per sorted position: inclusive scan of "key changed" minus 1.
802 Kokkos::View<int*, CpMem> segId(
803 Kokkos::view_alloc(space, "peclet::dem::cp::segId", Kokkos::WithoutInitializing), n);
804 int numSeg = 0;
805 Kokkos::parallel_scan(
806 "peclet::dem::cp::segscan", Kokkos::RangePolicy<CpExec>(space, 0, n),
807 KOKKOS_LAMBDA(int p, int& run, const bool final) {
808 const bool isNew = (p == 0) || (keys(p) != keys(p - 1));
809 if (isNew)
810 ++run;
811 if (final)
812 segId(p) = run - 1; // 0-based segment index
813 },
814 numSeg);
815 if (numOwnedManifolds) { // owned segment leaders (their keys carry no kNonOwnedPairBit)
816 Kokkos::View<std::uint64_t*, CpMem> kk = keys;
817 int nOwnedM = 0;
818 Kokkos::parallel_reduce(
819 "peclet::dem::cp::ownedSegs", Kokkos::RangePolicy<CpExec>(space, 0, n),
820 KOKKOS_LAMBDA(int p, int& cnt) {
821 const bool leader = (p == 0) || (kk(p) != kk(p - 1));
822 if (leader && (kk(p) & kNonOwnedPairBit) == 0)
823 ++cnt;
824 },
825 nOwnedM);
826 *numOwnedManifolds = nOwnedM;
827 }
828
829 // 4. Initialise one manifold per segment (canonical ids from the key, sums zero).
830 Kokkos::View<std::uint64_t*, CpMem> k = keys;
831 Kokkos::View<int*, CpMem> sid = segId;
832 Kokkos::View<ManifoldC*, CpMem> out = outManifolds;
833 Kokkos::parallel_for(
834 "peclet::dem::cp::init", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int p) {
835 const bool leader = (p == 0) || (k(p) != k(p - 1));
836 if (leader) {
837 ManifoldC m{};
838 decodeKey(k(p), m.bodyA, m.bodyB);
839 out(sid(p)) = m;
840 }
841 });
842
843 // 5. Accumulate each contact's transformed manifold into its segment (atomic; order-independent).
844 Kokkos::View<const ContactC*, CpMem> ct = contacts;
845 Kokkos::View<int*, CpMem> pm = perm;
846 Kokkos::parallel_for(
847 "peclet::dem::cp::accum", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int p) {
848 const ManifoldC m = transformContact(ct(pm(p)));
849 const int s = sid(p);
850 Kokkos::atomic_add(&out(s).num_points, m.num_points);
851 Kokkos::atomic_add(&out(s).normal_sum.x, m.normal_sum.x);
852 Kokkos::atomic_add(&out(s).normal_sum.y, m.normal_sum.y);
853 Kokkos::atomic_add(&out(s).normal_sum.z, m.normal_sum.z);
854 Kokkos::atomic_add(&out(s).torque_armA_sum.x, m.torque_armA_sum.x);
855 Kokkos::atomic_add(&out(s).torque_armA_sum.y, m.torque_armA_sum.y);
856 Kokkos::atomic_add(&out(s).torque_armA_sum.z, m.torque_armA_sum.z);
857 Kokkos::atomic_add(&out(s).torque_armB_sum.x, m.torque_armB_sum.x);
858 Kokkos::atomic_add(&out(s).torque_armB_sum.y, m.torque_armB_sum.y);
859 Kokkos::atomic_add(&out(s).torque_armB_sum.z, m.torque_armB_sum.z);
860 Kokkos::atomic_add(&out(s).rA_sum.x, m.rA_sum.x);
861 Kokkos::atomic_add(&out(s).rA_sum.y, m.rA_sum.y);
862 Kokkos::atomic_add(&out(s).rA_sum.z, m.rA_sum.z);
863 Kokkos::atomic_add(&out(s).rB_sum.x, m.rB_sum.x);
864 Kokkos::atomic_add(&out(s).rB_sum.y, m.rB_sum.y);
865 Kokkos::atomic_add(&out(s).rB_sum.z, m.rB_sum.z);
866 Kokkos::atomic_add(&out(s).wallVel_sum.x, m.wallVel_sum.x);
867 Kokkos::atomic_add(&out(s).wallVel_sum.y, m.wallVel_sum.y);
868 Kokkos::atomic_add(&out(s).wallVel_sum.z, m.wallVel_sum.z);
869 Kokkos::atomic_add(&out(s).restitution_sum, m.restitution_sum);
870 Kokkos::atomic_add(&out(s).friction_sum, m.friction_sum);
871 });
872 // Optional contact -> manifold slot map (PGS friction bound reads lambdaAcc through it).
873 if (contactSlot.extent(0) >= (size_t)n) {
874 Kokkos::View<int*, CpMem> cs = contactSlot;
875 Kokkos::parallel_for(
876 "peclet::dem::cp::slotmap", Kokkos::RangePolicy<CpExec>(space, 0, n),
877 KOKKOS_LAMBDA(int p) { cs(pm(p)) = sid(p); });
878 }
879 space.fence();
880
881 Kokkos::deep_copy(space, outCount, numSeg);
882 return numSeg;
883}
884
889KOKKOS_INLINE_FUNCTION std::uint64_t unitKey(const ContactC& c) {
890 if (c.bodyB < 0)
891 return (static_cast<std::uint64_t>(static_cast<unsigned>(c.bodyA)) << 32) |
892 static_cast<unsigned>(c.bodyB);
893 return pairKey(c);
894}
895
901struct PosUnits {
902 Kokkos::View<const int*, CpMem> start; // numUnits + 1
903 Kokkos::View<const int*, CpMem> list; // numContacts
904 KOKKOS_INLINE_FUNCTION bool on() const { return start.extent(0) > 0; }
905 KOKKOS_INLINE_FUNCTION int begin(int u) const { return on() ? start(u) : u; }
906 KOKKOS_INLINE_FUNCTION int end(int u) const { return on() ? start(u + 1) : u + 1; }
907 KOKKOS_INLINE_FUNCTION int contact(int k) const { return on() ? list(k) : k; }
908 KOKKOS_INLINE_FUNCTION int leader(int u) const { return on() ? list(start(u)) : u; }
909};
910
920inline int buildPositionUnits(Kokkos::View<const ContactC*, CpMem> contacts, int n,
921 Kokkos::View<int*, CpMem> unitStart,
922 Kokkos::View<int*, CpMem> unitContacts, int numManifolds = -1) {
923 CpExec space;
924 if (n <= 0) {
925 Kokkos::deep_copy(space, Kokkos::subview(unitStart, 0), 0);
926 space.fence();
927 return 0;
928 }
929 auto writeIdentity = [&]() {
930 Kokkos::View<int*, CpMem> us = unitStart;
931 Kokkos::View<int*, CpMem> uc = unitContacts;
932 Kokkos::parallel_for(
933 "peclet::dem::cp::uident", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int p) {
934 us(p) = p;
935 uc(p) = p;
936 });
937 Kokkos::deep_copy(space, Kokkos::subview(unitStart, n), n);
938 space.fence();
939 return n;
940 };
941 if (numManifolds == n)
942 return writeIdentity();
943 using Kokkos::view_alloc;
944 using Kokkos::WithoutInitializing;
945 Kokkos::View<std::uint64_t*, CpMem> keys(
946 view_alloc(space, "peclet::dem::cp::ukeys", WithoutInitializing), n);
947 Kokkos::View<int*, CpMem> perm(view_alloc(space, "peclet::dem::cp::uperm", WithoutInitializing),
948 n);
949 Kokkos::parallel_for(
950 "peclet::dem::cp::ukey", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
951 keys(i) = unitKey(contacts(i));
952 perm(i) = i;
953 });
954 Kokkos::Experimental::sort_by_key(space, keys, perm);
955 Kokkos::View<int*, CpMem> segId(view_alloc(space, "peclet::dem::cp::useg", WithoutInitializing),
956 n);
957 int numSeg = 0;
958 Kokkos::parallel_scan(
959 "peclet::dem::cp::usegscan", Kokkos::RangePolicy<CpExec>(space, 0, n),
960 KOKKOS_LAMBDA(int p, int& run, const bool final) {
961 if (p == 0 || keys(p) != keys(p - 1))
962 ++run;
963 if (final)
964 segId(p) = run - 1;
965 },
966 numSeg);
967 if (numSeg == n) {
968 // Every unit is one contact (spheres): the leader is the contact itself and the canonical
969 // order is the contact order, so the CSR is the identity -- written directly, without the
970 // second sort. The sweeps then take the empty PosUnits (no indirection; bitwise identical).
971 return writeIdentity();
972 }
973 Kokkos::View<int*, CpMem> segLead(
974 view_alloc(space, "peclet::dem::cp::ulead", WithoutInitializing), numSeg);
975 Kokkos::deep_copy(space, segLead, n);
976 Kokkos::parallel_for(
977 "peclet::dem::cp::uleadmin", Kokkos::RangePolicy<CpExec>(space, 0, n),
978 KOKKOS_LAMBDA(int p) { Kokkos::atomic_min(&segLead(segId(p)), perm(p)); });
979 // Second key: (leader << 32) | contact index -- unique, so the order does not depend on the
980 // sort's stability: units by ascending leader, contacts ascending inside each unit. perm(p)
981 // already holds the key's low word (the contact index), so it is the value the sort carries.
982 Kokkos::parallel_for(
983 "peclet::dem::cp::ukey2", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int p) {
984 const int i = perm(p);
985 keys(p) = (static_cast<std::uint64_t>(static_cast<unsigned>(segLead(segId(p)))) << 32) |
986 static_cast<unsigned>(i);
987 });
988 Kokkos::Experimental::sort_by_key(space, keys, perm);
989 Kokkos::View<int*, CpMem> us = unitStart;
990 Kokkos::View<int*, CpMem> uc = unitContacts;
991 int nu = 0;
992 Kokkos::parallel_scan(
993 "peclet::dem::cp::ustart", Kokkos::RangePolicy<CpExec>(space, 0, n),
994 KOKKOS_LAMBDA(int p, int& run, const bool final) {
995 const bool isNew = (p == 0) || ((keys(p) >> 32) != (keys(p - 1) >> 32));
996 if (final) {
997 uc(p) = perm(p);
998 if (isNew)
999 us(run) = p;
1000 }
1001 if (isNew)
1002 ++run;
1003 },
1004 nu);
1005 Kokkos::deep_copy(space, Kokkos::subview(unitStart, nu), n);
1006 space.fence();
1007 return nu;
1008}
1009
1014inline void frictionBoundFromLambda(Kokkos::View<ContactC*, CpMem> contacts, int numContacts,
1015 Kokkos::View<const int*, CpMem> contactSlot,
1016 Kokkos::View<const ManifoldC*, CpMem> manifolds,
1017 Kokkos::View<const float*, CpMem> lambdaAcc) {
1018 CpExec space;
1019 Kokkos::parallel_for(
1020 "peclet::dem::cp::pgs_friction_bound", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
1021 KOKKOS_LAMBDA(int i) {
1022 const int s = contactSlot(i);
1023 const ManifoldC m = manifolds(s);
1024 const int np = (m.num_points > 0) ? m.num_points : 1;
1025 contacts(i).friction_lambda_n = lambdaAcc(s) / static_cast<float>(np);
1026 });
1027 space.fence();
1028}
1029
1036inline void commitPosImpulse(Kokkos::View<const float*, CpMem> posLambdaContact, int numContacts,
1037 Kokkos::View<const int*, CpMem> contactSlot,
1038 Kokkos::View<const ManifoldC*, CpMem> manifolds, int numManifolds,
1039 Kokkos::View<const unsigned long long*, CpMem> keys,
1040 Kokkos::View<const unsigned long long*, CpMem> prevKeysSorted,
1041 int prevCount, float dt, Kokkos::View<float*, CpMem> scratchManifold,
1042 Kokkos::View<float*, CpMem> prevPosImpulse) {
1043 CpExec space;
1044 auto sm = Kokkos::subview(scratchManifold, Kokkos::pair<int, int>(0, numManifolds));
1045 Kokkos::deep_copy(space, sm, 0.0f);
1046 Kokkos::parallel_for(
1047 "peclet::dem::pos_load_reduce", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
1048 KOKKOS_LAMBDA(int i) {
1049 const float l = posLambdaContact(i);
1050 if (l != 0.0f)
1051 Kokkos::atomic_add(&scratchManifold(contactSlot(i)), l / dt);
1052 });
1053 Kokkos::parallel_for(
1054 "peclet::dem::pos_load_scatter", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
1055 KOKKOS_LAMBDA(int idx) {
1056 const unsigned long long k = keys(idx);
1057 if (k == ~0ull)
1058 return;
1059 int lo = 0, hi = prevCount;
1060 while (lo < hi) {
1061 const int m = (lo + hi) >> 1;
1062 if (prevKeysSorted(m) < k)
1063 lo = m + 1;
1064 else
1065 hi = m;
1066 }
1067 if (lo < prevCount && prevKeysSorted(lo) == k)
1068 prevPosImpulse(lo) = scratchManifold(idx);
1069 });
1070 space.fence();
1071}
1072
1073} // namespace peclet::dem
1074
1075#endif // DEM_CONTACT_PREPROCESSING_HPP
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
std::uint64_t pairKey(const ContactC &c)
Canonical pair key: (min<<32)|max, or (idA<<32)|0xFFFFFFFF for a boundary (idB<0) contact.
int colorEdges(const EdgeAccessor &edges, int numEdges, int numVertices, int vertexSpan, Kokkos::View< int *, CpMem > color, const ColorScratch &scratch, int &leftover)
Greedy graph colouring of an edge set: no two edges sharing a colouring vertex get the same colour.
void commitPosImpulse(Kokkos::View< const float *, CpMem > posLambdaContact, int numContacts, Kokkos::View< const int *, CpMem > contactSlot, Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< const unsigned long long *, CpMem > prevKeysSorted, int prevCount, float dt, Kokkos::View< float *, CpMem > scratchManifold, Kokkos::View< float *, CpMem > prevPosImpulse)
After the position solve: convert the per-contact positional lambdas into an impulse- equivalent per ...
ManifoldC transformContact(const ContactC &c)
Per-contact transform to a single-point manifold, aligned to the canonical pair.
std::uint64_t unitKey(const ContactC &c)
Position-unit key (docs/contact_solve_framework.md §4.3, §12 S6): the canonical pair key for a body-b...
constexpr int wallContactId(int wallIndex)
Wall identity of a boundary contact (docs/contact_solve_framework.md §12 S6): a contact with a wall c...
CpExec::memory_space CpMem
void decodeKey(std::uint64_t key, int &bodyA, int &bodyB)
Decode the canonical (bodyA, bodyB) from a pair key (bodyB = -1 for boundary).
void buildColorBuckets(Kokkos::View< const int *, CpMem > colorOf, int n, int numColors, Kokkos::View< int *, CpMem > perm, Kokkos::View< int *, CpMem > cursor, std::vector< int > &offs)
Dense colour buckets (numColors <= 64): perm[offs[c] .
void frictionBoundFromLambda(Kokkos::View< ContactC *, CpMem > contacts, int numContacts, Kokkos::View< const int *, CpMem > contactSlot, Kokkos::View< const ManifoldC *, CpMem > manifolds, Kokkos::View< const float *, CpMem > lambdaAcc)
PGS friction bound: overwrite each contact's friction_lambda_n with its manifold's converged PGS push...
constexpr std::uint64_t kNonOwnedPairBit
Distributed step: bit 63 of a manifold-reduction key marks a contact this rank sees but does not own ...
F3 ldF3(const V &v, int i)
constexpr int wallIndexOf(int bodyB)
float dot3(F3 a, F3 b)
F3 sub3(F3 a, F3 b)
int reduceContactsToManifolds(Kokkos::View< const ContactC *, CpMem > contacts, int n, Kokkos::View< ManifoldC *, CpMem > outManifolds, Kokkos::View< int, CpMem > outCount, Kokkos::View< int *, CpMem > contactSlot={}, int numOwnedContacts=-1, int *numOwnedManifolds=nullptr)
Reduce n contacts to manifolds (one per unique canonical pair).
unsigned long long pairKeyFromGids(unsigned a, unsigned b)
The one 64-bit pair identity every gid-keyed ledger uses (the XPBD persistent-contact and warm-start ...
Kokkos::DefaultExecutionSpace CpExec
void commitPairKeysLambda(const PairLedger &cur, const PairLedger &prev, Kokkos::View< int *, CpMem > perm, int numManifolds, Kokkos::View< const int *, CpMem > color={}, Kokkos::View< int *, CpMem > prevColor={})
Save this substep's keys + converged impulses (normal AND tangential) and key-sort them for next subs...
int buildPositionUnits(Kokkos::View< const ContactC *, CpMem > contacts, int n, Kokkos::View< int *, CpMem > unitStart, Kokkos::View< int *, CpMem > unitContacts, int numManifolds=-1)
Build the position units of contacts [0, n) (docs/contact_solve_framework.md §4.3,...
int partitionContacts(Kokkos::View< ContactC *, CpMem > contacts, int n, const Owns &owns)
Stable partition of contacts [0, n) by owns(contact): the owned ones first, [0, ncOwned),...
void markPersistentManifolds(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > keyIdx, Kokkos::View< const unsigned long long *, CpMem > prevKeys, int prevCount, Kokkos::View< unsigned long long *, CpMem > outKeys, Kokkos::View< unsigned char *, CpMem > outFlags)
keyIdx maps a body slot to the identity the pair key is built from: the REAL index map on the single-...
long long colorKey(int idx)
splitmix32 finalizer: a well-mixed pseudo-random priority per edge index.
void updateGroundedLevels(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *[3], CpMem > posPred, F3 gHat, Kokkos::View< unsigned char *, CpMem > grounded, int numReal, int sweeps, int decay)
Guendelman support levels, warm-started: decay every body's level by decay, re-seed 255 at wall/plane...
constexpr int kLevelInf
Height-from-floor BFS levels for the level-ordered ("multilevel") stabilization pass: 0 at a wall/pla...
void gatherWarmLambda(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > keyIdx, const PairLedger &prev, Kokkos::View< const float *, CpMem > prevPosImpulse, int prevCount, const PairLedger &out, Kokkos::View< float *, CpMem > outPosImpulse, Kokkos::View< unsigned char *, CpMem > outMatched={}, bool dedupTwins=true)
Warm-start gather for the PGS velocity solve: per manifold, write its pair key and look up the previo...
void commitPairKeys(Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< unsigned long long *, CpMem > prevKeys, int numManifolds)
Copy this substep's keys into prevKeys and sort them for next substep's binary search.
constexpr int kColorUncolourable
void computeHeightLevels(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *[3], CpMem > posPred, F3 gHat, Kokkos::View< int *, CpMem > heights, int numReal)
constexpr int kColorPalette
Colouring palette (docs/contact_solve_framework.md §4.2 item 1): the greedy takes the lowest colour f...
int lowerBoundKey(const KeysView &keys, int count, unsigned long long key)
lower_bound over a ledger's sorted keys: the first index whose key is >= key (count when none).
bool normalOfA(const ContactC &c)
Which body's surface a contact's normal belongs to.
F4 cross3(F4 a, F4 b)
unsigned long long pairKeyOf(const ManifoldC &m, Kokkos::View< const int *, CpMem > realIdx)
Persistent-contact detection for the gravity-gated restitution rule.
The solve's per-body accumulators: position/orientation and velocity corrections and the per-body con...
Kokkos::View< float *[4], CpMem > deltaQuat
Kokkos::View< float *[3], CpMem > deltaVel
Kokkos::View< float *[3], CpMem > deltaPos
Kokkos::View< float *[3], CpMem > deltaAngVel
Kokkos::View< int *, CpMem > constraintCounts
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< float *[3], CpMem > posPred
Kokkos::View< float *[4], CpMem > quatPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > pos
Kokkos::View< float *[3], CpMem > invInertia
Kokkos::View< float *, CpMem > scale
Kokkos::View< float *, CpMem > invMass
Kokkos::View< int *, CpMem > shapeId
Kokkos::View< float *[3], CpMem > angVel
Kokkos::View< float *[4], CpMem > quat
Kokkos::View< float *[3], CpMem > vel
The previous substep's colour ledger for an incremental colouring: the key-sorted pair keys,...
Kokkos::View< const unsigned long long *, CpMem > keys
Kokkos::View< const int *, CpMem > color
One colouring edge as the arbitration sees it: the colouring vertices of its A / B ends (b = -1 for a...
Kernel labels of one colorEdges instantiation (each caller keeps its own profiling names).
Per-vertex scratch of the Jones-Plassmann colourings: the round's winning edge key and the committed-...
Kokkos::View< long long *, CpMem > bodyWinner
Kokkos::View< std::uint64_t *, CpMem > bodyColorMask
Portable mirror of ParticleSystem.cuh ContactConstraint (the fields this reduction touches).
Portable mirror of ManifoldConstraint.
The pair-keyed warm-start ledger: pair key, converged normal and tangential impulses,...
Kokkos::View< float *[3], CpMem > lambdaT
Kokkos::View< float *, CpMem > restVPeak
Kokkos::View< float *, CpMem > restBank
Kokkos::View< unsigned long long *, CpMem > keys
Kokkos::View< float *, CpMem > lambda
The position units of a contact list as a CSR (§4.3): unit u owns the contacts list(start(u)) .
Kokkos::View< const int *, CpMem > start
Kokkos::View< const int *, CpMem > list
Island-sleeping state per real body: asleep flag, low-motion counter, moving-wall flag,...
Kokkos::View< unsigned char *, CpMem > asleep
Kokkos::View< int *, CpMem > sleepPrevCount
Kokkos::View< int *, CpMem > sleepCurCount
Kokkos::View< unsigned char *, CpMem > sleepMovingWall
Kokkos::View< unsigned char *, CpMem > sleepCounter
Per-edge slot overrides of a phase's hub copies (docs/contact_solve_framework.md §4....
Kokkos::View< const unsigned char *, CpMem > split
int slotA(int e, int def) const
bool relax(int sA, int sB) const
True iff the edge touches a mass-split slot (its step is scaled by omega before the clamp).
int slotB(int e, int def) const
Kokkos::View< const int *, CpMem > a
Kokkos::View< const int *, CpMem > b