9#ifndef DEM_SOLVER_VELOCITY_HPP
10#define DEM_SOLVER_VELOCITY_HPP
13#include <Kokkos_Core.hpp>
25KOKKOS_INLINE_FUNCTION
F3 ld3(Kokkos::View<
const float* [3],
CpMem> v,
int i) {
26 return F3{v(i, 0), v(i, 1), v(i, 2)};
31 if (invIlocal.
x == invIlocal.
y && invIlocal.
y == invIlocal.
z)
32 return dot3(tau, tau) * invIlocal.
x;
34 return t.
x * t.
x * invIlocal.
x + t.
y * t.
y * invIlocal.
y + t.
z * t.
z * invIlocal.
z;
47 Kokkos::View<const int*, CpMem> realIdx,
48 Kokkos::View<int*, CpMem> counts,
bool dedupTwins =
true) {
51 "peclet::dem::count_velocity_jacobi", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
52 KOKKOS_LAMBDA(
int idx) {
56 const int realA = realIdx(m.
bodyA);
58 const int realB = realIdx(m.
bodyB);
59 if (dedupTwins && realA > realB)
61 Kokkos::atomic_add(&counts(realB), 1);
63 Kokkos::atomic_add(&counts(realA), 1);
72 Kokkos::View<const float* [3], CpMem>
posPred;
74 Kokkos::View<const unsigned char*, CpMem>
grounded;
91 KOKKOS_INLINE_FUNCTION
ShockGate(
F3 dx,
F3 gHat,
float restVelThreshold)
92 :
up(-(dx.x * gHat.x + dx.y * gHat.y + dx.z * gHat.z))
93 ,
thr(0.3f * Kokkos::sqrt(
dot3(dx, dx)))
94 ,
riseThr(4.0f * restVelThreshold) {}
96 KOKKOS_INLINE_FUNCTION
bool lowerB(
F3 vB,
F3 gHat)
const {
115 Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
const BodyState& bodies,
116 const BodyPred& pred, Kokkos::View<const int*, CpMem> realIdx,
float growthRate,
117 float restitutionNormal,
float restVelThreshold, Kokkos::View<
float* [3],
CpMem> deltaVel,
118 Kokkos::View<
float* [3],
CpMem> deltaAngVel, Kokkos::View<int*, CpMem> velCounts,
119 Kokkos::View<const int*, CpMem> onlyColor = {},
int colorFilter = 0,
120 const ShockPropagation& shock = {},
bool massSplit =
false,
bool dedupTwins =
true) {
121 const Kokkos::View<const float*, CpMem> invMass = bodies.
invMass;
122 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.
invInertia;
123 const Kokkos::View<const float* [4], CpMem> quat = bodies.
quat;
124 const Kokkos::View<const float* [3], CpMem> velPred = pred.
velPred, angVelPred = pred.
angVelPred;
125 const Kokkos::View<const unsigned char*, CpMem> persistent = shock.persistent,
126 grounded = shock.grounded;
127 const Kokkos::View<const float* [3], CpMem> posPred = shock.posPred;
128 const F3 gHat = shock.gHat;
132 const bool filt = onlyColor.extent(0) > 0;
133 const bool usePersist = persistent.extent(0) > 0;
134 Kokkos::parallel_for(
135 "peclet::dem::solve_velocity", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
136 KOKKOS_LAMBDA(
int idx) {
137 if (filt && onlyColor(idx) != colorFilter)
139 const ManifoldC m = manifolds(idx);
140 if (m.num_points <= 0)
143 const int idA = m.bodyA, idB = m.bodyB;
144 const int realA = realIdx(idA);
147 realB = realIdx(idB);
148 if (dedupTwins && realA > realB)
152 const float invMassA = invMass(realA);
153 const float invMassB = (idB >= 0) ? invMass(realB) : 0.0f;
154 const F3 invIA =
ld3(invInertia, realA);
155 const F3 invIB = (idB >= 0) ?
ld3(invInertia, realB) :
F3{0, 0, 0};
156 const F4 qA =
F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
157 const F4 qB = (idB >= 0)
158 ?
F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)}
161 const F3 vA =
ld3(velPred, realA), wA =
ld3(angVelPred, realA);
162 F3 vB{0, 0, 0}, wB{0, 0, 0};
164 vB =
ld3(velPred, realB);
165 wB =
ld3(angVelPred, realB);
168 const F3 Nsum{m.normal_sum.x, m.normal_sum.y, m.normal_sum.z};
169 const F3 TauA{m.torque_armA_sum.x, m.torque_armA_sum.y, m.torque_armA_sum.z};
170 const F3 TauB{m.torque_armB_sum.x, m.torque_armB_sum.y, m.torque_armB_sum.z};
172 const float invN = 1.0f /
static_cast<float>(m.num_points);
177 float restitution = restitutionNormal;
179 vB =
scale3(
F3{m.wallVel_sum.x, m.wallVel_sum.y, m.wallVel_sum.z}, invN);
181 const float ra = m.restitution_sum * invN;
185 const F3 rAavg =
scale3(
F3{m.rA_sum.x, m.rA_sum.y, m.rA_sum.z}, invN);
186 const F3 rBavg =
scale3(
F3{m.rB_sum.x, m.rB_sum.y, m.rB_sum.z}, invN);
188 const float lenN = Kokkos::sqrt(
dot3(Nsum, Nsum));
200 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
201 const F3 vGrowth =
scale3(diffCenters, growthRate);
203 float vn =
dot3(vA, Nsum) +
dot3(wA, TauA) +
dot3(vB,
F3{-Nsum.x, -Nsum.y, -Nsum.z}) +
205 vn +=
dot3(vGrowth, Nsum);
207 const float alignment =
dot3(Nsum, diffCenters);
208 if (alignment > 0.0f) {
216 const float Nsq =
dot3(Nsum, Nsum);
217 float wA_n = Nsq * invMassA +
genInvMass(TauA, invIA, qA);
218 float wB_n = Nsq * invMassB +
genInvMass(TauB, invIB, qB);
228 bool applyA =
true, applyB =
true;
229 if (usePersist && persistent(idx) != 0 && idB >= 0) {
232 if (gate.lowerB(vB, gHat) && grounded(realB) > 0) {
237 }
else if (gate.lowerA(vA, gHat) && grounded(realA) > 0) {
243 const float wTotal = wA_n + wB_n;
248 massSplit ?
static_cast<float>(velCounts(realA)) * wA_n +
249 ((idB >= 0) ?
static_cast<float>(velCounts(realB)) * wB_n : 0.0f)
259 if (Kokkos::fabs(vn) < restVelThreshold * lenN)
262 const float lambda = (-restitution * vn - vn) / wSolve;
264 const F3 Jlin =
scale3(Nsum, lambda);
265 const F3 JangA =
scale3(TauA, lambda);
266 const F3 JangB =
scale3(TauB, lambda);
270 Kokkos::atomic_add(&deltaVel(realA, 0), Jlin.x * invMassA);
271 Kokkos::atomic_add(&deltaVel(realA, 1), Jlin.y * invMassA);
272 Kokkos::atomic_add(&deltaVel(realA, 2), Jlin.z * invMassA);
276 const F3 dwl{Jl.x * invIA.x, Jl.y * invIA.y, Jl.z * invIA.z};
278 Kokkos::atomic_add(&deltaAngVel(realA, 0), dww.x);
279 Kokkos::atomic_add(&deltaAngVel(realA, 1), dww.y);
280 Kokkos::atomic_add(&deltaAngVel(realA, 2), dww.z);
283 if (idB >= 0 && applyB) {
284 Kokkos::atomic_add(&deltaVel(realB, 0), -Jlin.x * invMassB);
285 Kokkos::atomic_add(&deltaVel(realB, 1), -Jlin.y * invMassB);
286 Kokkos::atomic_add(&deltaVel(realB, 2), -Jlin.z * invMassB);
288 const F3 dwl{Jl.x * invIB.x, Jl.y * invIB.y, Jl.z * invIB.z};
290 Kokkos::atomic_add(&deltaAngVel(realB, 0), dww.x);
291 Kokkos::atomic_add(&deltaAngVel(realB, 1), dww.y);
292 Kokkos::atomic_add(&deltaAngVel(realB, 2), dww.z);
294 Kokkos::atomic_add(&velCounts(realB), 1);
296 if (applyA && !massSplit)
297 Kokkos::atomic_add(&velCounts(realA), 1);
309template <
class V3,
class Vi>
311 V3 deltaAngVel,
Vi velCounts,
bool averaged =
true) {
313 Kokkos::parallel_for(
314 "peclet::dem::apply_vel_avg", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
315 const int count = velCounts(i);
321 const float f = averaged ? Kokkos::fmin(1.0f, 2.0f /
static_cast<float>(count)) : 1.0f;
322 for (
int c = 0; c < 3; ++c) {
323 velPred(i, c) += deltaVel(i, c) * f;
324 angVelPred(i, c) += deltaAngVel(i, c) * f;
325 deltaVel(i, c) = 0.0f;
326 deltaAngVel(i, c) = 0.0f;
356 return {
"peclet::dem::color_init_bodies",
"peclet::dem::color_init_manifolds",
357 "peclet::dem::color_reset_winner",
"peclet::dem::color_contend",
358 "peclet::dem::color_commit",
"peclet::dem::color_max",
359 "peclet::dem::color_leftover"};
383inline int colorManifolds(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
384 Kokkos::View<const int*, CpMem> realIdx,
int numReal,
385 Kokkos::View<int*, CpMem> mColor,
const ColorScratch& scratch,
386 int& leftover, Kokkos::View<const unsigned char*, CpMem> sleepMask = {},
387 SlotOverride ov = {},
bool dedupTwins =
true,
int vertexSpan = 0) {
388 const ManifoldEdges edges{manifolds, realIdx, sleepMask, ov, sleepMask.extent(0) > 0, dedupTwins};
389 return colorEdges(edges, numManifolds, numReal, vertexSpan, mColor, scratch, leftover);
409 int numManifolds, Kokkos::View<const int*, CpMem> realIdx,
411 Kokkos::View<int*, CpMem> mColor,
const ColorScratch& scratch,
412 int& leftover,
bool forceFull,
413 Kokkos::View<const unsigned char*, CpMem> sleepMask = {},
414 bool dedupTwins =
true) {
415 const Kokkos::View<const unsigned long long*, CpMem> prevKeys = carried.
keys;
416 const Kokkos::View<const int*, CpMem> prevColor = carried.
color;
417 const int prevCount = carried.
count;
418 const Kokkos::View<long long*, CpMem> bodyWinner = scratch.
bodyWinner;
419 const Kokkos::View<std::uint64_t*, CpMem> bodyMask = scratch.
bodyColorMask;
422 if (numManifolds <= 0 || numReal <= 0)
424 Kokkos::parallel_for(
425 "peclet::dem::icolor_init_bodies", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
426 KOKKOS_LAMBDA(
int i) { bodyMask(i) = 0; });
427 const bool full = forceFull || prevCount <= 0;
428 const bool sleepOn = sleepMask.extent(0) > 0;
431 Kokkos::parallel_for(
432 "peclet::dem::icolor_seed", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
433 KOKKOS_LAMBDA(
int idx) {
434 const ManifoldC m = manifolds(idx);
435 if (m.num_points <= 0 || (sleepOn && sleepMask(idx))) {
439 if (dedupTwins && m.bodyB >= 0 && realIdx(m.bodyA) > realIdx(m.bodyB)) {
445 const unsigned long long k =
pairKeyOf(m, realIdx);
446 int lo = 0, hi = prevCount;
448 const int mid = (lo + hi) >> 1;
449 if (prevKeys(mid) < k)
454 if (lo < prevCount && prevKeys(lo) == k) {
455 const int pc = prevColor(lo);
464 Kokkos::parallel_for(
465 "peclet::dem::icolor_seed_mask", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
466 KOKKOS_LAMBDA(
int idx) {
467 const int c = mColor(idx);
471 const std::uint64_t bit = std::uint64_t(1) << c;
472 Kokkos::atomic_or(&bodyMask(realIdx(m.bodyA)), bit);
474 Kokkos::atomic_or(&bodyMask(realIdx(m.bodyB)), bit);
478 int remaining = 1, prevRemaining = -1;
479 const int maxRounds = numManifolds + 2;
480 for (
int round = 0; round < maxRounds && remaining > 0; ++round) {
481 Kokkos::parallel_for(
482 "peclet::dem::icolor_reset_winner", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
483 KOKKOS_LAMBDA(
int i) { bodyWinner(i) = -1; });
484 Kokkos::parallel_for(
485 "peclet::dem::icolor_contend", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
486 KOKKOS_LAMBDA(
int idx) {
487 if (mColor(idx) != -1)
490 const long long key =
colorKey(idx);
491 Kokkos::atomic_max(&bodyWinner(realIdx(m.bodyA)), key);
493 Kokkos::atomic_max(&bodyWinner(realIdx(m.bodyB)), key);
496 Kokkos::parallel_reduce(
497 "peclet::dem::icolor_commit", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
498 KOKKOS_LAMBDA(
int idx,
int& acc) {
499 if (mColor(idx) != -1)
502 const int ea = realIdx(m.bodyA);
503 const int eb = (m.bodyB >= 0) ? realIdx(m.bodyB) : -1;
504 const long long key =
colorKey(idx);
505 if (bodyWinner(ea) != key || (eb >= 0 && bodyWinner(eb) != key)) {
509 std::uint64_t forbidden = bodyMask(ea);
511 forbidden |= bodyMask(eb);
515#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 3
524 const std::uint64_t bit = std::uint64_t(1) << c;
531 if (rem == prevRemaining)
537 Kokkos::parallel_reduce(
538 "peclet::dem::icolor_max", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
539 KOKKOS_LAMBDA(
int idx,
int& mx) {
540 if (mColor(idx) > mx)
543 Kokkos::Max<int>(maxc));
544 Kokkos::parallel_reduce(
545 "peclet::dem::icolor_leftover", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
546 KOKKOS_LAMBDA(
int idx,
int& acc) {
569inline void computeVn0(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
570 const BodyPred& pred, Kokkos::View<const int*, CpMem> realIdx,
571 float growthRate, Kokkos::View<float*, CpMem> vn0,
572 Kokkos::View<
float* [3],
CpMem> vt0,
bool dedupTwins =
true) {
573 const Kokkos::View<const float* [3], CpMem> velPred = pred.
velPred, angVelPred = pred.
angVelPred;
576 Kokkos::parallel_for(
577 "peclet::dem::pgs_vn0", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
578 KOKKOS_LAMBDA(
int idx) {
583 const int realA = realIdx(idA);
584 if (dedupTwins && idB >= 0 && realA > realIdx(idB))
586 const float invN = 1.0f /
static_cast<float>(m.
num_points);
590 const F3 vA = ld3(velPred, realA), wA = ld3(angVelPred, realA);
591 F3 vB{0, 0, 0}, wB{0, 0, 0};
593 vB = ld3(velPred, realIdx(idB));
594 wB = ld3(angVelPred, realIdx(idB));
600 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
601 const F3 vGrowth =
scale3(diffCenters, growthRate);
602 float vn =
dot3(vA, Nsum) +
dot3(wA, TauA) +
dot3(vB,
F3{-Nsum.
x, -Nsum.
y, -Nsum.
z}) +
604 vn +=
dot3(vGrowth, Nsum);
609 const float lenN = Kokkos::sqrt(
dot3(Nsum, Nsum));
612 const F3 nhat =
scale3(Nsum, 1.0f / lenN);
613 const int realB2 = (idB >= 0) ? realIdx(idB) : -1;
614 const F3 wAv = ld3(angVelPred, realA);
615 const F3 wBv = (realB2 >= 0) ? ld3(angVelPred, realB2) :
F3{0, 0, 0};
643 Kokkos::View<const int*, CpMem> realIdx,
float growthRate,
644 float restitutionNormal,
float restVelThreshold,
645 Kokkos::View<const float*, CpMem> vn0,
const PairLedger& ledger,
646 Kokkos::View<const float*, CpMem> restRel,
647 bool dedupTwins =
true) {
648 const Kokkos::View<const float*, CpMem> lambdaAcc = ledger.
lambda;
649 const Kokkos::View<float*, CpMem> restBank = ledger.
restBank, restVPeak = ledger.
restVPeak;
650 const Kokkos::View<const float*, CpMem> invMass = bodies.
invMass;
651 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.
invInertia;
652 const Kokkos::View<const float* [4], CpMem> quat = bodies.
quat;
653 const Kokkos::View<const float* [3], CpMem> velPred = pred.
velPred, angVelPred = pred.
angVelPred;
657 Kokkos::parallel_for(
658 "peclet::dem::rest_bank_update", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
659 KOKKOS_LAMBDA(
int idx) {
664 const int realA = realIdx(idA);
665 const int realB = (idB >= 0) ? realIdx(idB) : idB;
666 if (dedupTwins && idB >= 0 && realA > realB)
668 const float invN = 1.0f /
static_cast<float>(m.
num_points);
670 const float lenN = Kokkos::sqrt(
dot3(Nsum, Nsum));
675 const F3 vA = ld3(velPred, realA), wA = ld3(angVelPred, realA);
676 F3 vB{0, 0, 0}, wB{0, 0, 0};
678 vB = ld3(velPred, realB);
679 wB = ld3(angVelPred, realB);
685 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
686 const F3 vGrowth =
scale3(diffCenters, growthRate);
687 float vn =
dot3(vA, Nsum) +
dot3(wA, TauA) +
dot3(vB,
F3{-Nsum.
x, -Nsum.
y, -Nsum.
z}) +
689 vn +=
dot3(vGrowth, Nsum);
690 const float alignment =
dot3(Nsum, diffCenters);
691 const float sgn = (alignment > 0.0f) ? 1.0f : -1.0f;
694 const F3 invIA = ld3(invInertia, realA);
695 const F3 invIB = (idB >= 0) ? ld3(invInertia, realB) :
F3{0, 0, 0};
696 const F4 qA =
F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
697 const F4 qB = (idB >= 0)
698 ?
F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)}
700 const float Nsq =
dot3(Nsum, Nsum);
701 const float wTotal = Nsq * invMass(realA) + genInvMass(TauA, invIA, qA) +
702 Nsq * ((idB >= 0) ? invMass(realB) : 0.0f) +
703 genInvMass(TauB, invIB, qB);
706 float e = restitutionNormal;
712 const float v0til = sgn * vn0(idx);
713 const float vtilEnd = sgn * vn;
714 float owed = restBank(idx);
716 float vPeak = restVPeak(idx) * (1.0f - 1.0f / 256.0f);
717 if (v0til > restVelThreshold * lenN)
718 vPeak = Kokkos::fmax(vPeak, v0til / lenN);
719 if (vPeak > restVelThreshold) {
725 const float pR = Kokkos::fmax(0.0f, -vtilEnd) / wTotal * lenN;
726 const float pTot = Kokkos::fmax(lambdaAcc(idx), 0.0f) * lenN;
727 const float pC = Kokkos::fmax(0.0f, pTot - pR);
728 owed += e * pC - pR - Kokkos::fmax(restRel(idx), 0.0f) * lenN;
729 restBank(idx) = Kokkos::fmax(owed, 0.0f);
730 restVPeak(idx) = vPeak;
732 restBank(idx) = 0.0f;
733 restVPeak(idx) = 0.0f;
745 Kokkos::View<float*, CpMem> orphanVPeak,
int numOwned,
746 float restVelThreshold) {
748 Kokkos::parallel_for(
749 "peclet::dem::rest_orphan_decay", Kokkos::RangePolicy<CpExec>(space, 0, numOwned),
750 KOKKOS_LAMBDA(
int i) {
751 const float decayed = orphanVPeak(i) * (1.0f - 1.0f / 64.0f);
752 if (decayed <= restVelThreshold || orphan(i) <= 0.0f) {
754 orphanVPeak(i) = 0.0f;
756 orphan(i) *= (1.0f - 1.0f / 64.0f);
757 orphanVPeak(i) = decayed;
775 const PairLedger& prev, Kokkos::View<const unsigned char*, CpMem> matched,
int prevCount,
776 Kokkos::View<const float*, CpMem> invMass, Kokkos::View<float*, CpMem> orphan,
777 Kokkos::View<float*, CpMem> orphanVPeak, Kokkos::View<const int*, CpMem> gidSorted = {},
778 Kokkos::View<const int*, CpMem> slotSorted = {},
int ownedLimit = -1) {
779 const Kokkos::View<const unsigned long long*, CpMem> prevKeys = prev.
keys;
780 const Kokkos::View<const float*, CpMem> prevRestBank = prev.
restBank;
781 const Kokkos::View<const float*, CpMem> prevRestVPeak = prev.
restVPeak;
787 const int nMap =
static_cast<int>(gidSorted.extent(0));
788 const int nBody =
static_cast<int>(orphan.extent(0));
789 Kokkos::parallel_for(
790 "peclet::dem::rest_orphan_scatter", Kokkos::RangePolicy<CpExec>(space, 0, prevCount),
791 KOKKOS_LAMBDA(
int e) {
794 const float owed = prevRestBank(e);
797 const unsigned long long k = prevKeys(e);
800 const unsigned hi =
static_cast<unsigned>(k >> 32);
801 const unsigned lo =
static_cast<unsigned>(k & 0xFFFFFFFFu);
802 auto resolve = [&](
unsigned id) ->
int {
804 return (
static_cast<int>(
id) < nBody) ?
static_cast<int>(id) : -1;
807 const int m = (a + b) >> 1;
808 if (gidSorted(m) <
static_cast<int>(
id))
813 return (a < nMap && gidSorted(a) ==
static_cast<int>(
id)) ? slotSorted(a) : -1;
815 const int sA = resolve(hi);
816 if (ownedLimit >= 0 && !(sA >= 0 && sA < ownedLimit))
818 const int sB = (lo != 0xFFFFFFFFu) ? resolve(lo) : -1;
819 const float vpk = prevRestVPeak(e);
821 if (lo != 0xFFFFFFFFu) {
822 if (sB >= 0 && sA >= 0) {
823 const float wA = invMass(sA), wB = invMass(sB);
825 shareA = wB / (wA + wB);
830 if (sA >= 0 && shareA > 0.0f) {
831 Kokkos::atomic_add(&orphan(sA), owed * shareA);
832 Kokkos::atomic_max(&orphanVPeak(sA), vpk);
834 if (sB >= 0 && shareA < 1.0f) {
835 Kokkos::atomic_add(&orphan(sB), owed * (1.0f - shareA));
836 Kokkos::atomic_max(&orphanVPeak(sB), vpk);
843inline std::tuple<double, float, int>
restBankStats(Kokkos::View<const float*, CpMem> bank,
int n) {
848 Kokkos::parallel_reduce(
849 "peclet::dem::rest_bank_stats", Kokkos::RangePolicy<CpExec>(0, n),
850 KOKKOS_LAMBDA(
int i,
double& ls,
float& lm,
int& lc) {
851 const float v = bank(i);
858 s, Kokkos::Max<float>(mx), Kokkos::Sum<int>(cnt));
871inline void computeSideFlags(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
873 Kokkos::View<
const float* [3],
CpMem> velPred,
float riseThr,
874 Kokkos::View<unsigned char*, CpMem> sideFlag,
875 Kokkos::View<const float*, CpMem> vn0,
float approachThr,
876 bool dedupTwins =
true) {
877 const Kokkos::View<const unsigned char*, CpMem> persistent = shock.
persistent,
879 const Kokkos::View<const float* [3], CpMem> posPred = shock.
posPred;
880 const F3 gHat = shock.
gHat;
883 Kokkos::parallel_for(
884 "peclet::dem::pgs_side_flags", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
885 KOKKOS_LAMBDA(
int idx) {
886 const unsigned char wasPersistent = persistent(idx);
896 if (Kokkos::fabs(vn0(idx)) > approachThr)
898 const int realA = realIdx(m.
bodyA), realB = realIdx(m.
bodyB);
899 if (dedupTwins && realA > realB)
902 const float up = -(dx.
x * gHat.
x + dx.
y * gHat.
y + dx.
z * gHat.
z);
903 const float thr = 0.3f * Kokkos::sqrt(
dot3(dx, dx));
904 if (up > thr && -
dot3(ld3(velPred, realB), gHat) <= riseThr && grounded(realB) > 0) {
906 }
else if (up < -thr && -
dot3(ld3(velPred, realA), gHat) <= riseThr &&
907 grounded(realA) > 0) {
915inline void warmStartApply(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
917 Kokkos::View<const int*, CpMem> realIdx,
918 Kokkos::View<const float*, CpMem> warmP,
919 Kokkos::View<
float* [3],
CpMem> warmT,
bool dedupTwins =
true) {
920 const Kokkos::View<const float*, CpMem> invMass = bodies.
invMass;
921 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.
invInertia;
922 const Kokkos::View<const float* [4], CpMem> quat = bodies.
quat;
923 const Kokkos::View<float* [3], CpMem> velPred = pred.
velPred, angVelPred = pred.
angVelPred;
926 Kokkos::parallel_for(
927 "peclet::dem::pgs_warm_apply", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
928 KOKKOS_LAMBDA(
int idx) {
929 const float p = warmP(idx);
930 F3 lt{warmT(idx, 0), warmT(idx, 1), warmT(idx, 2)};
931 const bool hasT = (lt.
x != 0.0f || lt.
y != 0.0f || lt.
z != 0.0f);
932 if (p == 0.0f && !hasT)
938 const int realA = realIdx(idA);
939 const int realB = (idB >= 0) ? realIdx(idB) : idB;
940 if (dedupTwins && idB >= 0 && realA > realB)
942 const float invN = 1.0f /
static_cast<float>(m.
num_points);
948 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
949 const float alignment =
dot3(Nsum, diffCenters);
950 const float sgn = (alignment > 0.0f) ? 1.0f : -1.0f;
951 const float lambda = -sgn * p;
952 const F3 Jlin =
scale3(Nsum, lambda);
953 const float invMassA = invMass(realA);
954 const float invMassB = (idB >= 0) ? invMass(realB) : 0.0f;
955 Kokkos::atomic_add(&velPred(realA, 0), Jlin.
x * invMassA);
956 Kokkos::atomic_add(&velPred(realA, 1), Jlin.
y * invMassA);
957 Kokkos::atomic_add(&velPred(realA, 2), Jlin.
z * invMassA);
959 const F4 qA =
F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
961 const F3 invIA = ld3(invInertia, realA);
962 const F3 dwl{Jl.
x * invIA.
x, Jl.
y * invIA.
y, Jl.
z * invIA.
z};
964 Kokkos::atomic_add(&angVelPred(realA, 0), dww.
x);
965 Kokkos::atomic_add(&angVelPred(realA, 1), dww.
y);
966 Kokkos::atomic_add(&angVelPred(realA, 2), dww.
z);
969 Kokkos::atomic_add(&velPred(realB, 0), -Jlin.
x * invMassB);
970 Kokkos::atomic_add(&velPred(realB, 1), -Jlin.
y * invMassB);
971 Kokkos::atomic_add(&velPred(realB, 2), -Jlin.
z * invMassB);
972 const F4 qB =
F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)};
974 const F3 invIB = ld3(invInertia, realB);
975 const F3 dwl{Jl.
x * invIB.
x, Jl.
y * invIB.
y, Jl.
z * invIB.
z};
977 Kokkos::atomic_add(&angVelPred(realB, 0), dww.
x);
978 Kokkos::atomic_add(&angVelPred(realB, 1), dww.
y);
979 Kokkos::atomic_add(&angVelPred(realB, 2), dww.
z);
985 const float lenN2 = Kokkos::sqrt(
dot3(Nsum, Nsum));
987 const F3 nhat =
scale3(Nsum, 1.0f / lenN2);
990 warmT(idx, 0) = lt.
x;
991 warmT(idx, 1) = lt.
y;
992 warmT(idx, 2) = lt.
z;
993 Kokkos::atomic_add(&velPred(realA, 0), lt.
x * invMassA);
994 Kokkos::atomic_add(&velPred(realA, 1), lt.
y * invMassA);
995 Kokkos::atomic_add(&velPred(realA, 2), lt.
z * invMassA);
997 const F4 qA =
F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
999 const F3 invIA = ld3(invInertia, realA);
1000 const F3 dwl{Jl.
x * invIA.
x, Jl.
y * invIA.
y, Jl.
z * invIA.
z};
1002 Kokkos::atomic_add(&angVelPred(realA, 0), dww.
x);
1003 Kokkos::atomic_add(&angVelPred(realA, 1), dww.
y);
1004 Kokkos::atomic_add(&angVelPred(realA, 2), dww.
z);
1007 Kokkos::atomic_add(&velPred(realB, 0), -lt.
x * invMassB);
1008 Kokkos::atomic_add(&velPred(realB, 1), -lt.
y * invMassB);
1009 Kokkos::atomic_add(&velPred(realB, 2), -lt.
z * invMassB);
1010 const F4 qB =
F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)};
1012 const F3 invIB = ld3(invInertia, realB);
1013 const F3 dwl{Jl.
x * invIB.
x, Jl.
y * invIB.
y, Jl.
z * invIB.
z};
1015 Kokkos::atomic_add(&angVelPred(realB, 0), dww.
x);
1016 Kokkos::atomic_add(&angVelPred(realB, 1), dww.
y);
1017 Kokkos::atomic_add(&angVelPred(realB, 2), dww.
z);
1037 Kokkos::View<const float* [4], CpMem>
quat;
1050 Kokkos::View<const float*, CpMem>
vn0;
1054 Kokkos::View<const float* [3], CpMem>
vt0;
1127 const F3 dwl{Jl.
x * invI.
x, Jl.
y * invI.
y, Jl.
z * invI.
z};
1144 F3 TauB,
F3 vGrowth) {
1147 vn +=
dot3(vGrowth, Nsum);
1157 const int realA =
ov.slotA(idx,
realIdx(idA));
1158 const int realB = (idB >= 0) ?
ov.slotB(idx,
realIdx(idB)) : idB;
1159 const float invMassA =
invMass(realA);
1160 const float invMassB = (idB >= 0) ?
invMass(realB) : 0.0f;
1162 const F3 invIB = (idB >= 0) ? ld3(
invInertia, realB) :
F3{0, 0, 0};
1167 F3 vB{0, 0, 0}, wB{0, 0, 0};
1168 const float invN = 1.0f /
static_cast<float>(m.
num_points);
1181 const float eMat = restitution;
1187 const float lenN = Kokkos::sqrt(
dot3(nSum, nSum));
1192 .invMassA = invMassA,
1193 .invMassB = invMassB,
1198 .vel =
PairVelocity{.vA = vA, .wA = wA, .vB = vB, .wB = wB},
1200 .restitution = restitution,
1213 const int idB = r.
idB;
1216 const F4 qA = r.
qA, qB = r.
qB;
1219 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
1222 const float alignment =
dot3(nSum, diffCenters);
1223 const float sgn = (alignment > 0.0f) ? 1.0f : -1.0f;
1224 const float nSq =
dot3(nSum, nSum);
1225 float wAn = nSq * invMassA + genInvMass(tauA, invIA, qA);
1226 float wBn = nSq * invMassB + genInvMass(tauB, invIB, qB);
1231 bool applyA =
true, applyB =
true;
1232 const unsigned char sf =
sideFlag(idx);
1237 }
else if (sf == 2) {
1242 const float wTotal = wAn + wBn;
1256 float vtil3)
const {
1260 const F4 qA = r.
qA, qB = r.
qB;
1264 float owed = b.
owed;
1269 const bool relA =
true, relB = (idB >= 0);
1270 const float wRel = wTotal;
1277 const float vTphys = eMat * vPeak;
1278 const float targetR = -vTphys * lenN;
1279 const float dpR = (vtil3 - targetR) / wRel;
1280 float cap = owed / lenN;
1281 const float rOld =
restRel(idx);
1282 float rNew = rOld + dpR;
1289 if (rNew > cap && (orphA > 0.0f || orphB > 0.0f)) {
1290 const float draw =
drawOrphans(realA, realB, (rNew - cap) * lenN, orphA, orphB);
1299 const float dR = rNew - rOld;
1303 const float lambdaR = -sgn * dR;
1304 const F3 JlinR =
scale3(nSum, lambdaR);
1319 KOKKOS_FORCEINLINE_FUNCTION
float drawOrphans(
int realA,
int realB,
float need,
float orphA,
1320 float orphB)
const {
1323 const float d = Kokkos::fmin(need, orphA);
1328 if (need > 0.0f && orphB > 0.0f) {
1329 const float d = Kokkos::fmin(need, orphB);
1348 const float v0til = r.
sgn *
vn0(idx);
1353 const F4 qA = r.
qA, qB = r.
qB;
1356 const float invN = r.
invN, eMat = r.
eMat;
1358 const float lenN = r.
lenN;
1360 const float sgn = r.
sgn, wTotal = r.
wTotal;
1361 const unsigned char sf = r.
sf;
1364 const float vn = r.
vn;
1381 const float target =
1383 ? ((v0til > 0.0f) ? -restitution * v0til : 0.0f)
1385 const float vtil = sgn * vn;
1386 float dp = (vtil - target) / wTotal;
1387 if (
ov.relax(realA, realB))
1390 float pNew = pOld + dp;
1393 const float dApplied = pNew - pOld;
1394 if (dApplied != 0.0f) {
1397 const float lambda = -sgn * dApplied;
1398 const F3 Jlin =
scale3(Nsum, lambda);
1399 const F3 JangA =
scale3(TauA, lambda);
1400 const F3 JangB =
scale3(TauB, lambda);
1405 if (idB >= 0 && applyB) {
1430 float orphA = 0.0f, orphB = 0.0f;
1441 if ((owed > 0.0f || orphA > 0.0f || orphB > 0.0f) && vPeak > 0.0f) {
1443 F3 vB3{0, 0, 0}, wB3{0, 0, 0};
1450 float vn3 =
dot3(vA3, Nsum) +
dot3(wA3, TauA) +
dot3(vB3,
F3{-Nsum.
x, -Nsum.
y, -Nsum.
z}) +
1452 vn3 +=
dot3(vGrowth, Nsum);
1453 const float vtil3 = sgn * vn3;
1455 idx, r,
ReleaseBudget{.owed = owed, .vPeak = vPeak, .orphA = orphA, .orphB = orphB},
1473 const bool haveOld = (ltOld.
x != 0.0f || ltOld.
y != 0.0f || ltOld.
z != 0.0f);
1474 if (mu > 0.0f || haveOld) {
1475 const F3 nhat =
scale3(Nsum, 1.0f / lenN);
1477 F3 vB2{0, 0, 0}, wB2{0, 0, 0};
1495 if (beta != 0.0f &&
vt0.extent(0) > 0) {
1500 const float vtLen = Kokkos::sqrt(
dot3(vtErr, vtErr));
1502 float wT = invMassA + invMassB;
1503 if (vtLen > 1e-9f) {
1504 const F3 that =
scale3(vtErr, 1.0f / vtLen);
1505 float wAt = (sf == 2) ? 0.0f : invMassA + genInvMass(
cross3v(rAavg, that), invIA, qA);
1506 float wBt = (sf == 1 || idB < 0)
1508 : invMassB + genInvMass(
cross3v(rBavg, that), invIB, qB);
1511 ltNew =
add3(ltOld,
scale3(that, -vtLen / wT));
1517 float nTot = Kokkos::fmax(
lambdaAcc(idx), 0.0f) * lenN;
1523 const float bound = mu * nTot;
1524 const float ltLen = Kokkos::sqrt(
dot3(ltNew, ltNew));
1526 ltNew = (bound > 0.0f) ?
scale3(ltNew, bound / ltLen) :
F3{0, 0, 0};
1527 const F3 dApp =
sub3(ltNew, ltOld);
1528 if (dApp.
x != 0.0f || dApp.
y != 0.0f || dApp.
z != 0.0f) {
1532 const float dLen = Kokkos::sqrt(
dot3(dApp, dApp));
1539 if (idB >= 0 && applyB) {
1556 Kokkos::View<const float* [4], CpMem>
quat;
1566 Kokkos::View<const float*, CpMem>
vPeak;
1576 float growthRate,
float restitutionNormal,
float restVelThreshold,
1577 Kokkos::View<float, CpMem> maxApproach, Kokkos::View<float, CpMem> maxApproachQS,
1578 Kokkos::View<float*, CpMem> lambdaAcc, Kokkos::View<const float*, CpMem> vn0,
1579 Kokkos::View<const unsigned char*, CpMem> sideFlag, Kokkos::View<
float* [3],
CpMem> lambdaT,
1580 float frictionDynamic, Kokkos::View<
const float* [3],
CpMem> vt0,
float restitutionTangent,
1581 Kokkos::View<const float*, CpMem> posImpulse,
const PoissonBankViews& bank) {
1586 restitutionNormal, restVelThreshold,
1587 maxApproach, maxApproachQS.data() ? maxApproachQS : maxApproach,
1590 frictionDynamic, vt0,
1591 restitutionTangent, posImpulse,
1603 const Kokkos::View<const int*, CpMem> mColor = sched.
color;
1605 const Kokkos::View<const int*, CpMem> colorPerm = sched.
perm;
1606 const std::vector<int>* colorOffs = sched.
offs;
1613#ifdef KOKKOS_ENABLE_CUDA
1615 if (fused && fused->
maxBucket > 0 && colorOffs)
1616 return demLaunchFusedSweepLoop(space, f, colorPerm, *fused, numColors, *loop,
1620 if (fused && fused->
maxBucket > 0 && colorOffs &&
1621 demLaunchFusedColorSweep(space, f, colorPerm, *fused, numColors))
1632 for (
int color = 0; color < numColors; ++color) {
1634 const int b = (*colorOffs)[color], e = (*colorOffs)[color + 1];
1637 Kokkos::parallel_for(
1638 "peclet::dem::solve_velocity_pgs", Kokkos::RangePolicy<CpExec>(space, b, e),
1639 KOKKOS_LAMBDA(
int i2) { f.
solveOne(colorPerm(i2)); });
1641 Kokkos::parallel_for(
1642 "peclet::dem::solve_velocity_pgs", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
1643 KOKKOS_LAMBDA(
int idx) {
1644 if (mColor(idx) == color)
1661 int numManifolds, Kokkos::View<const int*, CpMem> realIdx,
1662 Kokkos::View<const int*, CpMem> mColor,
1663 Kokkos::View<const int*, CpMem> heights,
1664 Kokkos::View<int*, CpMem> keys, Kokkos::View<int*, CpMem> perm,
1665 std::vector<std::pair<int, int>>& buckets) {
1667 if (numManifolds <= 0)
1670 Kokkos::parallel_for(
1671 "peclet::dem::level_keys", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
1672 KOKKOS_LAMBDA(
int idx) {
1674 const int c = mColor(idx);
1677 keys(idx) = INT_MAX;
1680 int h = heights(realIdx(m.
bodyA));
1682 const int hB = heights(realIdx(m.
bodyB));
1688 keys(idx) = h * 64 + c;
1690 const auto rng = Kokkos::pair<int, int>(0, numManifolds);
1691 auto kd = Kokkos::subview(keys, rng);
1692 auto pd = Kokkos::subview(perm, rng);
1693 Kokkos::Experimental::sort_by_key(space, kd, pd);
1694 auto hk = Kokkos::create_mirror_view(kd);
1695 Kokkos::deep_copy(space, hk, kd);
1696 for (
int b = 0; b < numManifolds && hk(b) != INT_MAX;) {
1698 while (e < numManifolds && hk(e) == hk(b))
1700 buckets.emplace_back(b, e);
1712 const std::vector<std::pair<int, int>>& buckets,
bool topDown) {
1714 const int nb =
static_cast<int>(buckets.size());
1715 for (
int i = 0; i < nb; ++i) {
1716 const auto [b, e] = buckets[topDown ? nb - 1 - i : i];
1717 Kokkos::parallel_for(
1718 "peclet::dem::solve_velocity_pgs_lvl", Kokkos::RangePolicy<CpExec>(space, b, e),
1719 KOKKOS_LAMBDA(
int i2) { f.
solveOne(perm(i2)); });
1736KOKKOS_INLINE_FUNCTION
int xHolder(
unsigned long long mask,
long long t,
int numColors) {
1737 const int tc =
static_cast<int>(t % numColors);
1738 if ((mask >> tc) & 1ull)
1740 int j =
static_cast<int>(t %
xPopcount(mask));
1741 for (
int b = 0; b < 64; ++b)
1742 if ((mask >> b) & 1ull) {
1753 Kokkos::View<const unsigned long long*, CpMem>
mask;
1756 KOKKOS_INLINE_FUNCTION
bool fires(
int slotA,
int slotB)
const {
1757 if (
mask.extent(0) == 0)
1759 const unsigned long long mA =
mask(slotA);
1763 const unsigned long long mB =
mask(slotB);
1773inline void computeXGate(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int nm,
1774 Kokkos::View<const int*, CpMem> realIdx,
1775 Kokkos::View<const unsigned long long*, CpMem> velMask,
1776 Kokkos::View<unsigned char*, CpMem> gate,
int color,
int numColors,
1778 Kokkos::parallel_for(
1779 "peclet::dem::x_gate", Kokkos::RangePolicy<CpExec>(0, nm), KOKKOS_LAMBDA(
int idx) {
1782 const unsigned long long mA = velMask(realIdx(m.
bodyA));
1786 const unsigned long long mB = velMask(realIdx(m.
bodyB));
1790 gate(idx) = fire ? 1 : 0;
1794inline long long countUnfired(Kokkos::View<const unsigned char*, CpMem> gate,
int nm) {
1796 Kokkos::parallel_reduce(
1797 "peclet::dem::x_unfired", Kokkos::RangePolicy<CpExec>(0, nm),
1798 KOKKOS_LAMBDA(
int idx,
long long& acc) { acc += gate(idx) == 0 ? 1 : 0; }, n);
1815 const F3 dwl{Jl.
x * invI.
x, Jl.
y * invI.
y, Jl.
z * invI.
z};
1828 float restitutionNormal,
float restVelThreshold,
1829 Kokkos::View<float, CpMem> maxApproach,
XGateSpec xg = {},
1830 const ShockPropagation& shock = {}) {
1831 const Kokkos::View<const ManifoldC*, CpMem> manifolds = colored.
manifolds;
1833 const Kokkos::View<const int*, CpMem> mColor = colored.
mColor;
1834 const Kokkos::View<const float*, CpMem> invMass = bodies.
invMass;
1835 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.
invInertia;
1836 const Kokkos::View<const float* [4], CpMem> quat = bodies.
quat;
1837 const Kokkos::View<float* [3], CpMem> velPred = bodies.
velPred, angVelPred = bodies.
angVelPred;
1838 const Kokkos::View<const int*, CpMem> realIdx = bodies.
realIdx;
1839 const SlotOverride ov = bodies.
ov;
1840 const Kokkos::View<const unsigned char*, CpMem> persistent = shock.persistent,
1841 grounded = shock.grounded;
1842 const Kokkos::View<const float* [3], CpMem> posPred = shock.posPred;
1843 const F3 gHat = shock.gHat;
1847 const bool usePersist = persistent.extent(0) > 0;
1848 for (
int color = 0; color < numColors; ++color) {
1849 Kokkos::parallel_for(
1850 "peclet::dem::solve_velocity_gs", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
1851 KOKKOS_LAMBDA(
int idx) {
1852 if (mColor(idx) != color)
1854 const ManifoldC m = manifolds(idx);
1856 const int idA = m.bodyA, idB = m.bodyB;
1858 const int realA = ov.slotA(idx, realIdx(idA));
1859 const int realB = (idB >= 0) ? ov.slotB(idx, realIdx(idB)) : idB;
1861 const float invMassA = invMass(realA);
1862 const float invMassB = (idB >= 0) ? invMass(realB) : 0.0f;
1863 const F3 invIA = ld3(invInertia, realA);
1864 const F3 invIB = (idB >= 0) ? ld3(invInertia, realB) : F3{0, 0, 0};
1865 const F4 qA =
F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
1866 const F4 qB = (idB >= 0)
1867 ?
F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)}
1870 const F3 vA =
ld3(velPred, realA), wA =
ld3(angVelPred, realA);
1871 F3 vB{0, 0, 0}, wB{0, 0, 0};
1873 vB =
ld3(velPred, realB);
1874 wB =
ld3(angVelPred, realB);
1877 const F3 Nsum{m.normal_sum.x, m.normal_sum.y, m.normal_sum.z};
1878 const F3 TauA{m.torque_armA_sum.x, m.torque_armA_sum.y, m.torque_armA_sum.z};
1879 const F3 TauB{m.torque_armB_sum.x, m.torque_armB_sum.y, m.torque_armB_sum.z};
1881 const float invN = 1.0f /
static_cast<float>(m.num_points);
1883 float restitution = restitutionNormal;
1885 vB =
scale3(
F3{m.wallVel_sum.x, m.wallVel_sum.y, m.wallVel_sum.z}, invN);
1887 const float ra = m.restitution_sum * invN;
1891 const F3 rAavg =
scale3(
F3{m.rA_sum.x, m.rA_sum.y, m.rA_sum.z}, invN);
1892 const F3 rBavg =
scale3(
F3{m.rB_sum.x, m.rB_sum.y, m.rB_sum.z}, invN);
1894 const float lenN = Kokkos::sqrt(
dot3(Nsum, Nsum));
1898 const F3 diffCenters = (idB < 0) ? rAavg :
sub3(rAavg, rBavg);
1899 const F3 vGrowth =
scale3(diffCenters, growthRate);
1901 float vn =
dot3(vA, Nsum) +
dot3(wA, TauA) +
dot3(vB,
F3{-Nsum.x, -Nsum.y, -Nsum.z}) +
1903 vn +=
dot3(vGrowth, Nsum);
1905 const float alignment =
dot3(Nsum, diffCenters);
1906 if (alignment > 0.0f) {
1914 const float Nsq =
dot3(Nsum, Nsum);
1915 float wA_n = Nsq * invMassA +
genInvMass(TauA, invIA, qA);
1916 float wB_n = Nsq * invMassB +
genInvMass(TauB, invIB, qB);
1918 bool applyA =
true, applyB =
true;
1919 if (usePersist && persistent(idx) != 0 && idB >= 0) {
1922 if (gate.lowerB(vB, gHat) && grounded(realB) > 0) {
1926 }
else if (gate.lowerA(vA, gHat) && grounded(realA) > 0) {
1932 const float wTotal = wA_n + wB_n;
1939 Kokkos::atomic_max(&maxApproach(), Kokkos::fabs(vn) / lenN);
1943 if (!xg.fires(realIdx(idA), idB >= 0 ? realIdx(idB) : -1))
1946 if (Kokkos::fabs(vn) < restVelThreshold * lenN)
1949 const float lambda = (-restitution * vn - vn) / wTotal;
1951 const F3 Jlin =
scale3(Nsum, lambda);
1952 const F3 JangA =
scale3(TauA, lambda);
1953 const F3 JangB =
scale3(TauB, lambda);
1957 velPred(realA, 0) += Jlin.x * invMassA;
1958 velPred(realA, 1) += Jlin.y * invMassA;
1959 velPred(realA, 2) += Jlin.z * invMassA;
1962 angVelPred(realA, 0) += dww.x;
1963 angVelPred(realA, 1) += dww.y;
1964 angVelPred(realA, 2) += dww.z;
1967 if (idB >= 0 && applyB) {
1968 velPred(realB, 0) += -Jlin.x * invMassB;
1969 velPred(realB, 1) += -Jlin.y * invMassB;
1970 velPred(realB, 2) += -Jlin.z * invMassB;
1972 angVelPred(realB, 0) += dww.x;
1973 angVelPred(realB, 1) += dww.y;
1974 angVelPred(realB, 2) += dww.z;
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
float genInvMass(F3 tau, F3 invIlocal, F4 q)
F3 ld3(Kokkos::View< const float *[3], CpMem > v, int i)
int xHolder(unsigned long long mask, long long t, int numColors)
void solveVelocityPGSBuckets(const PGSManifoldSweep &f, Kokkos::View< const int *, CpMem > perm, const std::vector< std::pair< int, int > > &buckets, bool topDown)
Level-ordered symmetric sweep: launch one PGS kernel per (level, colour) bucket, ascending (bottom-up...
void solveVelocityColoredGS(const ColoredManifolds &colored, const VelocitySweepBodies &bodies, float growthRate, float restitutionNormal, float restVelThreshold, Kokkos::View< float, CpMem > maxApproach, XGateSpec xg={}, const ShockPropagation &shock={})
Colored Gauss–Seidel normal-restitution solve: sweep the numColors colour classes in order,...
PGSManifoldSweep makePGSManifoldSweep(Kokkos::View< const ManifoldC *, CpMem > manifolds, const VelocitySweepBodies &bodies, float growthRate, float restitutionNormal, float restVelThreshold, Kokkos::View< float, CpMem > maxApproach, Kokkos::View< float, CpMem > maxApproachQS, Kokkos::View< float *, CpMem > lambdaAcc, Kokkos::View< const float *, CpMem > vn0, Kokkos::View< const unsigned char *, CpMem > sideFlag, Kokkos::View< float *[3], CpMem > lambdaT, float frictionDynamic, Kokkos::View< const float *[3], CpMem > vt0, float restitutionTangent, Kokkos::View< const float *, CpMem > posImpulse, const PoissonBankViews &bank)
Build the shared per-manifold sweep functor (the colored launch loop, the fused kernels and the fused...
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.
int xPopcount(unsigned long long m)
Rank-level X (docs/contact_solve_framework.md §1.4): the holder of a body whose velocity activity mas...
Kokkos::View< int *, CpMem > Vi
CpExec::memory_space CpMem
void computeSideFlags(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, const ShockPropagation &shock, Kokkos::View< const float *[3], CpMem > velPred, float riseThr, Kokkos::View< unsigned char *, CpMem > sideFlag, Kokkos::View< const float *, CpMem > vn0, float approachThr, bool dedupTwins=true)
Decide each persistent contact's treatment ONCE per substep (before any impulse is applied): 0 = symm...
void warmStartApply(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, const BodyState &bodies, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *, CpMem > warmP, Kokkos::View< float *[3], CpMem > warmT, bool dedupTwins=true)
Apply the warm-start impulses up front (order-independent: fixed impulses, atomic adds).
F3 invRotateVector(F4 q, F3 v)
void scatterOrphanBanks(const PairLedger &prev, Kokkos::View< const unsigned char *, CpMem > matched, int prevCount, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanVPeak, Kokkos::View< const int *, CpMem > gidSorted={}, Kokkos::View< const int *, CpMem > slotSorted={}, int ownedLimit=-1)
Orphan transfer: previous-ledger entries NOT matched by any current manifold (their pair died this su...
F3 ldF3(const V &v, int i)
F3 rotateVector(F4 q, F3 v)
void solveVelocity(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, const BodyState &bodies, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, float growthRate, float restitutionNormal, float restVelThreshold, Kokkos::View< float *[3], CpMem > deltaVel, Kokkos::View< float *[3], CpMem > deltaAngVel, Kokkos::View< int *, CpMem > velCounts, Kokkos::View< const int *, CpMem > onlyColor={}, int colorFilter=0, const ShockPropagation &shock={}, bool massSplit=false, bool dedupTwins=true)
Accumulate normal-restitution velocity deltas for numManifolds manifolds.
std::tuple< double, float, int > restBankStats(Kokkos::View< const float *, CpMem > bank, int n)
Poisson-restitution diagnostics: (sum, max, count>0) over the committed owed-impulse store (namespace...
Kokkos::DefaultExecutionSpace CpExec
Kokkos::View< float *[3], CpMem > V3
void computeVn0(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, float growthRate, Kokkos::View< float *, CpMem > vn0, Kokkos::View< float *[3], CpMem > vt0, bool dedupTwins=true)
-— Warm-started projected Gauss-Seidel (PGS) velocity solve -— Nonsmooth contact dynamics (Moreau-Jea...
void updateRestitutionBank(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, const BodyState &bodies, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, float growthRate, float restitutionNormal, float restVelThreshold, Kokkos::View< const float *, CpMem > vn0, const PairLedger &ledger, Kokkos::View< const float *, CpMem > restRel, bool dedupTwins=true)
Event-level (Poisson) restitution bookkeeping, once per substep AFTER all velocity phases (restitutio...
void applyVelocityDeltasAveraged(int n, V3 velPred, V3 angVelPred, V3 deltaVel, V3 deltaAngVel, Vi velCounts, bool averaged=true)
Apply the accumulated velocity deltas AVERAGED by the per-body manifold count — the velocity- solve t...
void computeXGate(Kokkos::View< const ManifoldC *, CpMem > manifolds, int nm, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const unsigned long long *, CpMem > velMask, Kokkos::View< unsigned char *, CpMem > gate, int color, int numColors, long long t)
The per-manifold fire gate of sync interval t: 1 iff this rank (colour color) holds every endpoint wh...
F3 angularDelta(F3 jang, F3 invI, F4 q)
World-frame angular velocity change of an angular impulse jang on a body with diagonal local inverse ...
long long colorKey(int idx)
splitmix32 finalizer: a well-mixed pseudo-random priority per edge index.
constexpr int kColorUncolourable
int colorManifoldsIncremental(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, int numReal, const CarriedColors &carried, Kokkos::View< int *, CpMem > mColor, const ColorScratch &scratch, int &leftover, bool forceFull, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, bool dedupTwins=true)
Incremental (warm-started) manifold colouring for the single-GPU PGS path.
long long countUnfired(Kokkos::View< const unsigned char *, CpMem > gate, int nm)
Diagnostics (iterCounters): the number of gated-off manifolds of one sweep.
int colorManifolds(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, int numReal, Kokkos::View< int *, CpMem > mColor, const ColorScratch &scratch, int &leftover, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, SlotOverride ov={}, bool dedupTwins=true, int vertexSpan=0)
Greedy graph-colour the manifolds: no two manifolds sharing a real body get the same colour (colorEdg...
constexpr int kColorPalette
Colouring palette (docs/contact_solve_framework.md §4.2 item 1): the greedy takes the lowest colour f...
void countVelocityJacobi(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< int *, CpMem > counts, bool dedupTwins=true)
Mass-split Jacobi count pass (docs/contact_solve_framework.md §3.1, D3): per body (REAL index),...
void decayBodyOrphan(Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanVPeak, int numOwned, float restVelThreshold)
Orphan-account aging, once per substep over the OWNED bodies: both the balance and the carried event ...
bool solveVelocityPGS(const PGSManifoldSweep &f, int numManifolds, const ColorSchedule &sched, const FusedLoopSpec *loop=nullptr)
Returns true when the sweep (or, with loop, the whole iteration loop) was submitted; false ONLY in lo...
unsigned long long pairKeyOf(const ManifoldC &m, Kokkos::View< const int *, CpMem > realIdx)
Persistent-contact detection for the gravity-gated restitution rule.
void buildLevelColorBuckets(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > mColor, Kokkos::View< const int *, CpMem > heights, Kokkos::View< int *, CpMem > keys, Kokkos::View< int *, CpMem > perm, std::vector< std::pair< int, int > > &buckets)
Bucket the active coloured manifolds by (support level, colour) for the level-ordered ("multilevel") ...
dem — fused colour sweeps: one persistent kernel per sweep instead of one kernel launch per colour.
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > invInertia
Kokkos::View< float *, CpMem > invMass
Kokkos::View< float *[4], CpMem > quat
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).
The colour schedule of one coloured Gauss–Seidel sweep: each item's colour and the colour count,...
Kokkos::View< const int *, CpMem > perm
const FusedSweepCtx * fused
const std::vector< int > * offs
Kokkos::View< const int *, CpMem > color
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
The colour classes one coloured sweep visits: manifolds [0, numManifolds), labelled by mColor in [0,...
Kokkos::View< const ManifoldC *, CpMem > manifolds
Kokkos::View< const int *, CpMem > mColor
Device-side iteration loop: run up to maxIters sweeps of the colour classes inside ONE kernel,...
Device-side context for a fused colour sweep: the colour offsets (numColors+1, uploaded from buildCol...
Portable mirror of ManifoldConstraint.
The manifold edge source of colorEdges: vertex realIdx(body) or its hub copy slot through ov,...
int initialColor(int idx) const
Kokkos::View< const unsigned char *, CpMem > sleepMask
ColorEdge edge(int idx) const
static constexpr ColorLabels labels()
Kokkos::View< const ManifoldC *, CpMem > manifolds
Kokkos::View< const int *, CpMem > realIdx
The endpoints' velocities (B on a wall: the wall's average velocity, zero spin).
The Poisson release's budget: the pair's owed impulse, the release speed scale and the endpoints' orp...
One manifold's constraint row as the three impulse channels share it: the manifold,...
One full colored PGS sweep.
Kokkos::View< float, CpMem > maxApproach
Kokkos::View< float, CpMem > maxApproachQS
Kokkos::View< float *[3], CpMem > lambdaT
Kokkos::View< const float *, CpMem > restOrphanVPeak
void applyLinear(int real, F3 dLin, float invMass) const
Linear velocity update of body slot real by the world-frame impulse dLin.
Kokkos::View< const float *, CpMem > invMass
Kokkos::View< const float *[4], CpMem > quat
Kokkos::View< const int *, CpMem > realIdx
Kokkos::View< const float *[3], CpMem > vt0
static float normalVelocity(const PairVelocity &v, F3 Nsum, F3 TauA, F3 TauB, F3 vGrowth)
The pair's normal approach velocity along Nsum (arm sums TauA/TauB), growth included.
Kokkos::View< const float *[3], CpMem > invInertia
Kokkos::View< const unsigned char *, CpMem > sideFlag
void gatherRow(int idx, Row &r) const
Row, part 1: the manifold, its endpoints' slots, mass data and velocities, the averaged normal and ar...
Kokkos::View< float *, CpMem > lambdaAcc
void solveOne(int idx) const
void recordCorrection(int idx, float lenN, float corr) const
Residual record of one applied correction: the adaptive stop's maxApproach and, on quasi-static conta...
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< const float *, CpMem > restVPeak
Kokkos::View< const float *, CpMem > vn0
Kokkos::View< float *, CpMem > restOrphan
Kokkos::View< const float *, CpMem > posImpulse
Kokkos::View< const ManifoldC *, CpMem > manifolds
void weighRow(int idx, Row &r) const
Row, part 2: the approach velocity, the sign, the side gates and the effective mass.
void releaseIncrement(int idx, const Row &r, const ReleaseBudget &b, float vtil3) const
One release increment: the budget-capped push toward the event's separation-velocity target,...
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< float *, CpMem > restRel
float drawOrphans(int realA, int realB, float need, float orphA, float orphB) const
Draw up to need from the endpoints' orphan balances (A first, then B); returns the amount drawn.
void applyAngular(int real, F3 dAngWorld, F3 invI, F4 q) const
Angular velocity update of body slot real by the world-frame angular impulse dAngWorld (rotated into ...
Kokkos::View< float *, CpMem > restBank
The pair-keyed warm-start ledger: pair key, converged normal and tangential impulses,...
Kokkos::View< float *, CpMem > restVPeak
Kokkos::View< float *, CpMem > restBank
Kokkos::View< unsigned long long *, CpMem > keys
Kokkos::View< float *, CpMem > lambda
Event-level (Poisson) restitution views, all empty when the model is off.
Kokkos::View< float *, CpMem > orphan
Kokkos::View< float *, CpMem > rel
Kokkos::View< const float *, CpMem > vPeak
Kokkos::View< float *, CpMem > bank
Kokkos::View< float *, CpMem > orphanPeak
The geometric and rise gate of shock propagation (see solveVelocity) for one persistent body-body pai...
bool lowerB(F3 vB, F3 gHat) const
B is the lower, non-rising body (velocity vB).
ShockGate(F3 dx, F3 gHat, float restVelThreshold)
bool lowerA(F3 vA, F3 gHat) const
A is the lower, non-rising body (velocity vA).
Shock propagation for persistent loaded contacts (see solveVelocity): the persistence flags,...
Kokkos::View< const float *[3], CpMem > posPred
Kokkos::View< const unsigned char *, CpMem > grounded
Kokkos::View< const unsigned char *, CpMem > persistent
Per-edge slot overrides of a phase's hub copies (docs/contact_solve_framework.md §4....
The bodies a velocity sweep reads and updates in place: mass data and orientation,...
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< const int *, CpMem > realIdx
Kokkos::View< const float *[4], CpMem > quat
Kokkos::View< const float *[3], CpMem > invInertia
Kokkos::View< const float *, CpMem > invMass
Rank-level X evaluated inline in the one-shot sweep (only for the manifolds of the colour being swept...
Kokkos::View< const unsigned long long *, CpMem > mask
bool fires(int slotA, int slotB) const