31#ifndef DEM_SOLVE_DRIVER_HPP
32#define DEM_SOLVE_DRIVER_HPP
36#include <Kokkos_Core.hpp>
53inline int readInt(Kokkos::View<int, CpMem> v) {
55 Kokkos::deep_copy(h, v);
58inline float readFloat(Kokkos::View<float, CpMem> v) {
60 Kokkos::deep_copy(h, v);
68 "peclet::dem::gid_base", Kokkos::RangePolicy<CpExec>(0, n),
69 KOKKOS_LAMBDA(
int i) { gid(i) = base + i; });
83 Kokkos::parallel_reduce(
84 "peclet::dem::max_scale", Kokkos::RangePolicy<CpExec>(0, P.
numReal),
85 KOKKOS_LAMBDA(
int i,
float& m) { m = sc(i) > m ? sc(i) : m; }, Kokkos::Max<float>(mx));
97 "peclet::dem::world_radii", Kokkos::RangePolicy<CpExec>(space, 0, n),
98 KOKKOS_LAMBDA(
int i) { rad(i) = scale(i) * gs * bR; });
116 if (np >
static_cast<int>(P.
pairs.extent(0))) {
117 const int grown = np + np / 2 + 64;
118 Kokkos::realloc(Kokkos::WithoutInitializing, P.
pairs, grown);
123 return std::min(np,
static_cast<int>(P.
pairs.extent(0)));
141 Kokkos::parallel_reduce(
142 "peclet::dem::verlet_disp", Kokkos::RangePolicy<CpExec>(0, P.
numReal),
143 KOKKOS_LAMBDA(
int i,
float& acc) {
144 const float dx = pp(i, 0) - rp(i, 0), dy = pp(i, 1) - rp(i, 1), dz = pp(i, 2) - rp(i, 2);
145 const float d = dx * dx + dy * dy + dz * dz;
149 Kokkos::Max<float>(md2));
153 const float grow = std::max(0.0f, maxRad - P.
impRefMaxRad);
154 if (std::sqrt(md2) + 2.0f * grow > 0.5f * skin)
159 const auto rng = Kokkos::pair<int, int>(0, P.
numReal);
160 Kokkos::deep_copy(Kokkos::subview(P.
impRefPos, rng, Kokkos::ALL),
161 Kokkos::subview(P.
posPred, rng, Kokkos::ALL));
169#ifdef KOKKOS_ENABLE_CUDA
177struct CudaIterGraph {
178 cudaGraphExec_t exec =
nullptr;
181 cudaStream_t str = space.cuda_stream();
182 if (cudaStreamBeginCapture(str, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
183 (void)cudaGetLastError();
187 cudaGraph_t g =
nullptr;
188 if (cudaStreamEndCapture(str, &g) != cudaSuccess || g ==
nullptr) {
189 (void)cudaGetLastError();
195 if (cacheSlot !=
nullptr) {
196 exec =
static_cast<cudaGraphExec_t
>(cacheSlot);
197 cudaGraphExecUpdateResultInfo ri;
198 if (cudaGraphExecUpdate(exec, g, &ri) != cudaSuccess) {
199 (void)cudaGetLastError();
200 cudaGraphExecDestroy(exec);
205 if (exec ==
nullptr) {
206 if (cudaGraphInstantiate(&exec, g,
nullptr,
nullptr, 0) != cudaSuccess) {
207 (void)cudaGetLastError();
213 return exec !=
nullptr;
215 void launch(
CpExec& space) { cudaGraphLaunch(exec, space.cuda_stream()); }
220#define PECLET_DEM_GRAPH_LOOP(useVar, graphVar, emitVar, slotVar) \
221 if constexpr (!Hooks::distributed) { \
223 useVar = graphVar.capture(space, emitVar, slotVar); \
233#define PECLET_DEM_GRAPH_LOOP(useVar, graphVar, emitVar, slotVar) (void)graphVar;
253 float allMax(
float v)
const {
return v; }
276 P.
iterCountDev = Kokkos::View<int*, CpMem>(
"peclet::dem::iter_count", 2);
285 Kokkos::deep_copy(v, Kokkos::subview(P.
iterCountDev, slot));
297 void note(
const char* phase, Kokkos::View<const int*, CpMem> color,
int n,
300 auto hc = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), color);
301 auto ha = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
edgeA);
302 auto hb = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
edgeB);
303 auto hd = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
vertexDegree);
304 int bad = 0, first = -1;
305 for (
int e = 0; e < n; ++e)
311 std::string m = std::string(
"colouring invariant violated: ") + std::to_string(bad) +
" " +
312 phase +
" uncolourable edges after hub splitting";
314 m +=
" (first: vertex " + std::to_string(ha(first)) +
", degree " +
315 std::to_string(ha(first) >= 0 ? hd(ha(first)) : 0);
317 m +=
"; vertex " + std::to_string(hb(first)) +
", degree " + std::to_string(hd(hb(first)));
320 msg += (
msg.empty() ?
"" :
"; ") + m;
326 template <
class Hooks>
329 if constexpr (!Hooks::distributed) {
331 throw std::runtime_error(
msg);
335 template <
class Hooks>
336 float vote(
const Hooks& hooks,
float res) {
339 const float r = hooks.allMaxAny(res, bad);
341 throw std::runtime_error(
342 msg.empty() ? std::string(
"colouring invariant violated on another rank") :
msg);
349 if (slotBase + nCopies > P.
capacity)
357template <
class Hooks>
360 const Kokkos::View<const unsigned char*, CpMem> mSleep,
362 Kokkos::View<int*, CpMem>& edgeA = inv.
edgeA;
363 Kokkos::View<int*, CpMem>& edgeB = inv.
edgeB;
371 const bool incrColor = usePersistPre && P.
velocityUseGS && !Hooks::distributed && !incrColorOff;
377 velLeftover, didFull, mSleep);
389 !Hooks::distributed);
391 if (velLeftover > 0 && nm > 0) {
393 edgeA = Kokkos::View<int*, CpMem>(
"peclet::dem::copies_eA", nm);
394 edgeB = Kokkos::View<int*, CpMem>(
"peclet::dem::copies_eB", nm);
396 Kokkos::View<const int*, CpMem>(P.
manifoldColor), edgeA, edgeB);
404 !Hooks::distributed, slotBase + vcs.
nCopies);
410 Kokkos::View<const float*, CpMem>(P.
invMass));
423template <
class Hooks>
425 const Kokkos::View<const unsigned char*, CpMem> cSleep,
427 int& numPosUnits,
PosUnits& posUnits) {
428 Kokkos::View<int*, CpMem>& edgeA = inv.
edgeA;
429 Kokkos::View<int*, CpMem>& edgeB = inv.
edgeB;
431 int numPosColors = 0;
433 const bool incrPosColor =
434 usePersistPre && P.
velocityUseGS && !Hooks::distributed && !incrColorOff;
435 bool posDidFull =
false;
440 if (numPosUnits != nc)
441 posUnits =
PosUnits{Kokkos::subview(Kokkos::View<const int*, CpMem>(P.
unitStart),
442 Kokkos::pair<int, int>(0, numPosUnits + 1)),
443 Kokkos::subview(Kokkos::View<const int*, CpMem>(P.
unitContacts),
444 Kokkos::pair<int, int>(0, nc))};
449 P.
colorScratch(), posLeftover, posDidFull, cSleep, posUnits);
465 if ((posLeftover > 0 || images) && numPosUnits > 0) {
466 edgeA = Kokkos::View<int*, CpMem>(
"peclet::dem::copies_eA", numPosUnits);
467 edgeB = Kokkos::View<int*, CpMem>(
"peclet::dem::copies_eB", numPosUnits);
469 Kokkos::View<const int*, CpMem>(P.
unitColor), edgeA, edgeB);
471 "peclet::dem::vertexDegree");
473 if (posLeftover > 0) {
481 posLeftover, cSleep, posUnits, ovColor, slotBase + pcs.
nCopies);
487 inv.
note(
"position", Kokkos::View<const int*, CpMem>(P.
unitColor), numPosUnits,
492 : Kokkos::View<
const float* [3],
CpMem>(),
493 pcs, Hooks::distributed);
516 return std::max(res, c);
537template <
class Hooks,
class EmitPosIter,
class SyncPos>
541 const auto& posUnits = ctx.
posUnits;
543 const auto& posSched = ctx.
posSched;
544 const auto& posTol = ctx.
posTol;
545 const auto& fusedOff = ctx.
fusedOff;
546 const auto& stopsOn = ctx.
stopsOn;
548 const auto& posCons = ctx.
posCons;
552 bool posLoopDone =
false;
553 if constexpr (!Hooks::distributed) {
565 bool graphPos =
false;
586 if (stopsOn && posRes < posTol)
591 hooks.syncContactCounts(P);
596 if constexpr (Hooks::distributed) {
597 if (hooks.syncPoint(it))
601 if constexpr (Hooks::distributed)
608template <
class Hooks>
610 PhaseCopies& pcs,
int slotBase,
bool rankM,
bool usePGS,
611 const PosUnits posUnits,
int numPosUnits,
617 const bool posCopiesOn = pcs.
nGroups > 0;
618 Kokkos::View<const float*, CpMem> invMassPos = P.
invMass;
619 Kokkos::View<const float* [3], CpMem> invInertiaPos = P.
invInertia;
635#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 7
639 }
else if (posCopiesOn) {
648#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 7
652 Kokkos::Profiling::popRegion();
653 Kokkos::Profiling::pushRegion(
"dem::solve::pos");
655 if constexpr (Hooks::distributed) {
656 hooks.syncPositions(P);
665 auto pc = Kokkos::subview(P.
posLambdaContact, Kokkos::pair<int, int>(0, nc));
666 Kokkos::deep_copy(pc, 0.0f);
673 auto emitPosIter = [&] {
684 const bool posCons = posCopiesOn || posM;
686 .numPosUnits = numPosUnits,
687 .posSched = posSched,
689 .fusedOff = fusedOff,
691 .posCopiesOn = posCopiesOn,
698 if (usePGS && nm > 0)
701 Kokkos::Profiling::popRegion();
707 Kokkos::View<const float*, CpMem> invMass,
708 Kokkos::View<
const float* [3],
CpMem> invInertia,
710 return VelocitySweepBodies{.invMass = invMass,
711 .invInertia = invInertia,
722 .rel = on ? P.
restRel : Kokkos::View<float*, CpMem>(),
723 .vPeak = on ? Kokkos::View<const float*, CpMem>(P.
restVPeak)
724 : Kokkos::View<const float*, CpMem>(),
725 .orphan = on ? P.
bodyOrphan : Kokkos::View<float*, CpMem>(),
773template <
class Hooks>
775 if constexpr (Hooks::distributed) {
777 hooks.syncVelocities(P);
785 hooks.syncVelocities(P);
795template <
class Hooks>
798 const auto& velSched = ctx.
velSched;
800 const auto& usePGS = ctx.
usePGS;
802 const auto& stopsOn = ctx.
stopsOn;
803 const auto& fusedOff = ctx.
fusedOff;
804 bool velLoopDone =
false;
805 if constexpr (!Hooks::distributed) {
806 if (usePGS && P.
velocityUseGS && !velCopiesOn && velFusedP) {
817 nm, velSched, &spec);
830 const auto& velOv = ctx.
velOv;
831 const auto& rk = ctx.
rk;
832 const auto& xOn = ctx.
xOn;
838 const long long t = P.
solveEpoch + it / rk.syncInterval;
839 xGate =
XGateSpec{Kokkos::View<const unsigned long long*, CpMem>(P.
velMask), rk.color,
843 Kokkos::View<const unsigned long long*, CpMem>(P.
velMask), P.
xGate, rk.color,
851 .numColors = numColors},
853 .invInertia = invInertiaVel,
867template <
class Hooks>
870 !Hooks::distributed);
871 hooks.syncContactCounts(P);
875 !Hooks::distributed);
885template <
class Hooks>
888 const auto& velSched = ctx.
velSched;
891 const auto& velOv = ctx.
velOv;
892 const auto& gMagP = ctx.
gMagP;
893 const auto& usePGS = ctx.
usePGS;
895 const auto& velCons = ctx.
velCons;
896 const auto& stopsOn = ctx.
stopsOn;
899 const float vRest = 2.0f * P.
dt * gMagP;
905 auto emitVelIter = [&] {
920 bool graphVel =
false;
928 Kokkos::Profiling::popRegion();
929 Kokkos::Profiling::pushRegion(
"dem::solve::vel");
935 !Hooks::distributed);
955 if (stopsOn && velRes <= (usePGS ? 0.02f * vRest : vRest))
961 if constexpr (Hooks::distributed) {
962 if (hooks.syncPoint(it))
966 if constexpr (Hooks::distributed)
972template <
class Hooks>
975 const auto& velSched = ctx.
velSched;
979 const auto& velOv = ctx.
velOv;
980 const auto& gHat = ctx.
gHat;
981 const auto& gMagP = ctx.
gMagP;
983 const auto& velCons = ctx.
velCons;
984 const auto& stopsOn = ctx.
stopsOn;
985 const auto& fusedOff = ctx.
fusedOff;
988 .posPred = P.posPred,
990 .grounded = P.groundedLevel},
992 !Hooks::distributed);
995 auto emitOsIter = [&] {
1006 bool osLoopDone =
false;
1007 if constexpr (!Hooks::distributed) {
1008 if (!velCopiesOn && velFusedP) {
1017 nm, velSched, &spec);
1020 bool graphOs =
false;
1033 if (stopsOn && osRes <= vRestS)
1035 if constexpr (Hooks::distributed) {
1036 if (hooks.syncPoint(it))
1054#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 6
1055 Kokkos::View<const float*, CpMem> invMassCoarse = P.
invMassSolve;
1057 Kokkos::View<const float*, CpMem> invMassCoarse = P.
invMass;
1065 return invMassCoarse;
1074 Kokkos::View<const float* [3], CpMem> invInertiaCoarse = P.
invInertia;
1077 "peclet::dem::invInertiaCoarse");
1082 return invInertiaCoarse;
1089 const std::size_t ng = P.
mlInvMassG.extent(0);
1090 const auto wi = Kokkos::WithoutInitializing;
1091 P.
mlOriginG = Kokkos::View<float* [3], CpMem>(Kokkos::view_alloc(wi,
"mlOriginG"), ng);
1092 P.
mlComOffG = Kokkos::View<float* [3], CpMem>(Kokkos::view_alloc(wi,
"mlComOffG"), ng);
1093 P.
mlAngG = Kokkos::View<float* [3], CpMem>(Kokkos::view_alloc(wi,
"mlAngG"), ng);
1094 P.
mlAngG0 = Kokkos::View<float* [3], CpMem>(Kokkos::view_alloc(wi,
"mlAngG0"), ng);
1095 P.
mlInvIG = Kokkos::View<float* [6], CpMem>(Kokkos::view_alloc(wi,
"mlInvIG"), ng);
1103 const MlScratch& S,
bool velM,
bool velCopiesOn) {
1109 H.
numGroups[0], Kokkos::View<const int*, CpMem>(P.
aVel)) > 0)
1110 throw std::logic_error(
"rank-level M: a coarse group holds a vertex with a = 0");
1112 if (H.
numLevels > 0 && (velCopiesOn || velM))
1117 Kokkos::View<const unsigned char*, CpMem>(P.
splitSlot)));
1124#ifdef KOKKOS_ENABLE_CUDA
1127template <
class Hooks>
1128inline bool launchFusedMultilevelLoop(Particles& P,
int nm,
CpExec& space,
1129 const VelocitySolveContext& ctx,
const float vRestS,
1130 const ContactHierarchy& H, MlScratch& S,
1131 const MlBodyViews& mlBody,
const MlFusedCtx* mlFusedP) {
1132 const auto& velSched = ctx.velSched;
1133 const auto& numColors = ctx.numColors;
1134 const auto& velPermC = ctx.velPermC;
1135 const auto& velFusedP = ctx.velFusedP;
1136 const auto& relV = ctx.poissonViews.rel;
1137 const auto& velCopiesOn = ctx.velCopiesOn;
1138 const auto& stopsOn = ctx.stopsOn;
1139 const auto& fusedOff = ctx.fusedOff;
1140 bool mlLoopDone =
false;
1141 if constexpr (!Hooks::distributed) {
1142 if (!velCopiesOn && velFusedP) {
1143 if (H.numLevels > 0 && mlFusedP) {
1146 P.restitutionNormal, vRestS, P.maxApproach, P.maxApproachQS, P.lambdaAcc, P.vn0,
1147 Kokkos::View<const unsigned char*, CpMem>(P.sideFlags), P.lambdaT, P.frictionDynamic,
1148 P.vt0, P.restitutionTangent, Kokkos::View<const float*, CpMem>(P.posImpulse),
1150 mlLoopDone = demLaunchFusedMlLoop(
1151 space, fStab, velPermC, *velFusedP, numColors,
1152 MlManifoldViews{P.manifolds, P.realIndices, P.lambdaAcc, P.maxApproachQS,
1153 Kokkos::View<const float*, CpMem>(relV)},
1154 Kokkos::View<const float*, CpMem>(P.invMass), P.velPred, S,
1155 Kokkos::View<const int*, CpMem>(P.mlBucketPerm), *mlFusedP, 2 * P.velocityIterations,
1156 stopsOn ? vRestS : fusedOff, mlBody);
1157 }
else if (H.numLevels == 0) {
1159 const FusedLoopSpec spec{2 * P.velocityIterations, stopsOn ? vRestS : fusedOff,
false};
1162 P.growthRate, P.restitutionNormal, vRestS, P.maxApproach,
1163 P.maxApproachQS, P.lambdaAcc, P.vn0,
1164 Kokkos::View<const unsigned char*, CpMem>(P.sideFlags), P.lambdaT,
1165 P.frictionDynamic, P.vt0, P.restitutionTangent,
1166 Kokkos::View<const float*, CpMem>(P.posImpulse), ctx.poissonViews),
1167 nm, velSched, &spec);
1175template <
class Hooks>
1178 const float vRestS) {
1180 const auto& velSched = ctx.
velSched;
1183 const auto& velOv = ctx.
velOv;
1185 const auto& gHat = ctx.
gHat;
1186 const auto& gMagP = ctx.
gMagP;
1187 const auto& sleepOn = ctx.
sleepOn;
1190 const auto& velM = ctx.
velM;
1191 const auto& velCons = ctx.
velCons;
1192 const auto& stopsOn = ctx.
stopsOn;
1205 const float qsThr = 8.0f * P.
dt * gMagP;
1216 Kokkos::View<const float* [4], CpMem>(P.
quat), invInertiaCoarse};
1220 Kokkos::View<const float*, CpMem>(P.
vn0),
1221 Kokkos::View<const float* [3], CpMem>(P.
vt0),
1230 sleepOn ? Kokkos::View<const unsigned char*, CpMem>(P.
asleep)
1231 : Kokkos::View<const unsigned char*, CpMem>(),
1235 std::vector<std::vector<int>> mlOffs;
1258 auto emitMlIter = [&] {
1271 Kokkos::View<const float*, CpMem>(relV)},
1272 nm, invMassCoarse, P.
velPred, nBodies, H, S,
1273 2, mlBody, &mlOffs, Kokkos::View<const int*, CpMem>(P.
mlBucketPerm),
1281 bool mlLoopDone =
false;
1282#ifdef KOKKOS_ENABLE_CUDA
1283 mlLoopDone = launchFusedMultilevelLoop<Hooks>(P, nm, space, ctx, vRestS, H, S, mlBody, mlFusedP);
1285 bool graphMl =
false;
1298 if (stopsOn && mlRes <= vRestS)
1300 if constexpr (Hooks::distributed) {
1301 if (hooks.syncPoint(it))
1309template <
class Hooks>
1314 const auto& velOv = ctx.
velOv;
1320 const auto& gHat = ctx.
gHat;
1322 const auto& velCons = ctx.
velCons;
1323 const auto& stopsOn = ctx.
stopsOn;
1331 std::vector<std::pair<int, int>> buckets;
1349 Kokkos::View<const unsigned char*, CpMem>(P.
sideFlags),
1354 Kokkos::View<const float*, CpMem>(P.
posImpulse),
1371 if (stopsOn && orRes <= vRestS)
1373 if constexpr (Hooks::distributed) {
1374 if (hooks.syncPoint(it))
1382template <
class Hooks>
1385 const auto& velSched = ctx.
velSched;
1388 const auto& velOv = ctx.
velOv;
1389 const auto& velCons = ctx.
velCons;
1390 const auto& stopsOn = ctx.
stopsOn;
1396 for (
int it = 0; it < 256; ++it) {
1407 if (stopsOn && esRes <= vRestS)
1409 if constexpr (Hooks::distributed) {
1410 if (hooks.syncPoint(it))
1420template <
class Hooks>
1423 const auto& gMagP = ctx.
gMagP;
1424 const auto& usePGS = ctx.
usePGS;
1431 const float vRestS = 2.0f * P.
dt * gMagP;
1436 }
else if (smode == 2) {
1438 }
else if (smode == 4) {
1440 }
else if (smode == 3) {
1443 if constexpr (Hooks::distributed)
1452template <
class Hooks>
1457 const auto& gMagP = ctx.
gMagP;
1458 const auto& usePGS = ctx.
usePGS;
1459 const auto& poisson = ctx.
poisson;
1461 const auto& velM = ctx.
velM;
1468 hooks.restoreOrphanBalance(P);
1469 }
else if (velCopiesOn) {
1477 Kokkos::View<const float*, CpMem>(P.
restRel),
1478 !Hooks::distributed);
1483 Hooks::distributed ? Kokkos::View<const int*, CpMem>()
1507template <
class Hooks>
1510 const auto& keyIdx = ws.
keyIdx;
1511 const auto& gHat = ws.
gHat;
1512 const auto& gMagP = ws.
gMagP;
1514 const auto& usePGS = ws.
usePGS;
1515 const auto& poisson = ws.
poisson;
1516 const auto& rankM = ws.
rankM;
1520 Kokkos::deep_copy(mt,
static_cast<unsigned char>(0));
1524 poisson ? P.
prevMatched : Kokkos::View<unsigned char*, CpMem>(),
1525 !Hooks::distributed);
1528 auto rr = Kokkos::subview(P.
restRel, Kokkos::pair<int, int>(0, nm));
1529 Kokkos::deep_copy(rr, 0.0f);
1538 Kokkos::View<const int*, CpMem> gidSorted, slotSorted;
1539 if constexpr (Hooks::distributed) {
1540 Kokkos::View<int*, CpMem> gs(
1541 Kokkos::view_alloc(space,
"peclet::dem::orphan_gids", Kokkos::WithoutInitializing),
1543 Kokkos::View<int*, CpMem> ss(
1544 Kokkos::view_alloc(space,
"peclet::dem::orphan_slots", Kokkos::WithoutInitializing),
1547 Kokkos::parallel_for(
1548 "peclet::dem::orphan_gid_map", Kokkos::RangePolicy<CpExec>(space, 0, nBodies),
1549 KOKKOS_LAMBDA(
int i) {
1553 Kokkos::Experimental::sort_by_key(space, gs, ss);
1573 auto flags = Kokkos::subview(P.
sideFlags, Kokkos::pair<int, int>(0, nm));
1574 Kokkos::deep_copy(flags,
static_cast<unsigned char>(0));
1577 !Hooks::distributed);
1579 P.
lambdaT, !Hooks::distributed);
1584 if constexpr (Hooks::distributed)
1585 hooks.openVelocityPhase(P, poisson);
1591 if constexpr (Hooks::distributed)
1592 hooks.openPositionCounts(P, rankM);
1600 const PosUnits& posUnits,
int numPosUnits) {
1609 if (!usePersistPre) {
1611 growCopyView(P.
xGate,
static_cast<std::size_t
>(nm > 0 ? nm : 1),
"peclet::dem::xGate");
1643 Kokkos::View<int*, CpMem> color,
int n,
int numColors,
1644 Kokkos::View<int*, CpMem> perm, Kokkos::View<int*, CpMem> offsDev,
1646 const bool dense = P.
velocityUseGS && n > 0 && numColors > 0;
1650 const Kokkos::View<const int*, CpMem> permC(perm);
1668 Kokkos::View<const float*, CpMem> invMassVel = P.
invMass;
1669 Kokkos::View<const float* [3], CpMem> invInertiaVel = P.
invInertia;
1682 }
else if (velCopiesOn) {
1690 return VelocitySolveViews{.invMass = invMassVel, .invInertia = invInertiaVel, .ov = velOv};
1700template <
class Hooks>
1714template <
class Hooks>
1716 Kokkos::View<const int*, CpMem> keyIdx,
const Hooks& hooks) {
1718 hooks.beginSolve(P);
1723 Kokkos::Profiling::pushRegion(
"dem::solve::prep");
1724 const int nmVisible = hooks.visibleManifolds(nm);
1727 const bool usePersistPre =
gravityOn(P);
1737 const Kokkos::View<const unsigned char*, CpMem> mSleep =
1738 sleepOn ? Kokkos::View<const unsigned char*, CpMem>(P.
manifoldSleep)
1739 : Kokkos::View<const unsigned char*, CpMem>();
1740 const Kokkos::View<const unsigned char*, CpMem> cSleep =
1741 sleepOn ? Kokkos::View<const unsigned char*, CpMem>(P.
contactSleep)
1742 : Kokkos::View<const unsigned char*, CpMem>();
1743 const bool legacyFriction = friction && !(usePersistPre && P.
velocityUseGS);
1791 int numPosColors = 0;
1792 int numPosUnits = 0;
1797 slotBase, inv, numPosUnits, posUnits);
1804 const bool rankM = Hooks::distributed && hooks.rankK().exchanges;
1831 const RankK rk = hooks.rankK();
1832#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 4
1833 const bool xOn =
false;
1849 .nmVisible = nmVisible,
1858 const bool velCopiesOn = vcs.
nGroups > 0;
1862 const bool velM = rankM && usePGS;
1869 const bool velCons = velCopiesOn || velM;
1872 const float fusedOff = -1.0f;
1884 .nmVisible = nmVisible,
1887 .legacyFriction = legacyFriction,
1889 .wantFused = wantFused,
1891 .velCopiesOn = velCopiesOn,
1895 .fusedOff = fusedOff};
1897 Kokkos::Profiling::popRegion();
1898 Kokkos::Profiling::pushRegion(
"dem::solve::stab_fric");
1901 if (legacyFriction) {
1903 hooks.syncFrictionCounts(P);
1908 if constexpr (Hooks::distributed)
1909 hooks.syncVelocities(P);
1913 if constexpr (Hooks::distributed)
1914 hooks.publishPositions(P);
1916 solvePositionPhase(P, nc, nm, hooks, space, pcs, slotBase, rankM, usePGS, posUnits, numPosUnits,
1917 posPlan.sched, stopsOn, fusedOff, inv);
dem — portable (ArborX) broad-phase, the Kokkos-native replacement for the CUDA-only cuBQL broad-phas...
int countLightHubs(const PhaseCopies &H, Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, Kokkos::View< const float *, CpMem > invMass)
Light hubs (split_stats, R-P3): velocity hubs whose mass is below 10x the mean mass of their partners...
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 stabilizeOrdered(Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
The ORDERED stabilization pass (stabilization mode 4, measurement mode): level-ordered symmetric swee...
void vertexDegree(Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, Kokkos::View< int *, CpMem > deg)
Per-vertex active degree over [0, nV) (§4.4 step 1). Wall edges have one endpoint.
Kokkos::View< float *, CpMem > Vf
void finishActivity(Kokkos::View< const unsigned char *, CpMem > hit, int nSlots, PhaseCopies &H, Kokkos::View< int *, CpMem > a)
a(q) from the pass (§13.3): hit(q) for a slot outside any group; at a hub base the number of its grou...
void stabilizeVelocityPhase(Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx)
The stabilization pass after the main velocity phase (Guendelman staging, Phase B): when the symmetri...
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...
void activityHitVelocity(Kokkos::View< const ManifoldC *, CpMem > manifolds, int nm, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > color, SlotOverride ov, Kokkos::View< unsigned char *, CpMem > hit, int nSlots)
Rank-level activity pass (docs/contact_solve_framework.md §13.3 C4; the distributed step only),...
bool frictionOn(const Particles &P)
A frictional wall drives friction even when the body-body material is frictionless.
bool solvePositionColoredGS(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int numContacts, const ColorSchedule &sched, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< float *[3], CpMem > posPred, Kokkos::View< const float *[4], CpMem > quatPred, Kokkos::View< const float *[4], CpMem > quatStatic, Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< float, CpMem > maxOverlap, Kokkos::View< float *, CpMem > posLambdaAcc={}, const FusedLoopSpec *loop=nullptr, SlotOverride ov={}, Kokkos::View< float, CpMem > posResidual={}, float omega=1.0f)
Colored Gauss–Seidel XPBD overlap solve: sweep the numColors colour classes in order,...
void solveContactFriction(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, FrManifoldCounts planeFriction, float frictionDynamic, const BodyDelta &delta)
One count-averaged Coulomb friction sweep.
void markCopySeeds(const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w, Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanPeak, bool shareFromBase=false)
Mark the phase seed sigma of every group and re-seed its members from the base (§4....
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 ...
float maxOwnedRadius(const Particles &P)
Largest effective particle radius over the owned set (= max scale × globalScale, growth included).
void applyVelocityDeltas(int n, V3 velPred, V3 angVelPred, V3 deltaVel, V3 deltaAngVel)
Add accumulated velocity/angular deltas onto the predicted velocity, then clear the delta buffers.
void applyUpdates(int n, const BodyPred &pred, const BodyDelta &delta, bool averaged=true)
Jacobi count-averaged apply of position/velocity deltas, then clear deltas + counts.
void positionEdgeEnds(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int nu, Kokkos::View< const int *, CpMem > color, Kokkos::View< int *, CpMem > eA, Kokkos::View< int *, CpMem > eB)
Position-phase colouring edges: one per unit, the leader contact's raw (bodyA, bodyB); active = the u...
bool gravityOn(const Particles &P)
Whether gravity is on (any component non-zero): it gates persistent contacts, warm-started PGS and is...
void velocityEdgeEnds(Kokkos::View< const ManifoldC *, CpMem > manifolds, int nm, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > color, Kokkos::View< int *, CpMem > eA, Kokkos::View< int *, CpMem > eB)
Velocity-phase colouring edges: the vertices realIdx(bodyA / bodyB) of each manifold (-1: a wall end,...
void solvePositionPhase(Particles &P, int nc, int nm, const Hooks &hooks, CpExec &space, PhaseCopies &pcs, int slotBase, bool rankM, bool usePGS, const PosUnits posUnits, int numPosUnits, const ColorSchedule &posSched, bool stopsOn, float fusedOff, ColoringInvariant &inv)
The position phase (docs/contact_solve_framework.md §4.4, §4.5, §13.3 step 9; WO-12): seed the positi...
Kokkos::View< int *, CpMem > Vi
void buildSolveViewsRankK(Particles &P, const PhaseCopies &H, int nSlots, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< const int *, CpMem > kRank)
Solve views under rank-level M (§13.3, §4.5): invMassSolve / invInertiaSolve = k x the body's masses ...
void demSolveContacts(Particles &P, int nc, int nm, int nBodies, Kokkos::View< const int *, CpMem > keyIdx, const Hooks &hooks)
One full velocity + position contact solve over the already-built contacts/manifolds (see file commen...
int colorContacts(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, int numBodies, Kokkos::View< int *, CpMem > cColor, const ColorScratch &scratch, int &leftover, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, PosUnits units={}, SlotOverride ov={}, int vertexSpan=0)
Greedy graph-colour the contacts (raw bodyA/bodyB): no two contacts sharing a body get the same colou...
void planColorSweeps(SweepPlan &plan, CpExec &space, Particles &P, Kokkos::View< int *, CpMem > color, int n, int numColors, Kokkos::View< int *, CpMem > perm, Kokkos::View< int *, CpMem > offsDev, bool wantFused)
Dense colour buckets for one phase's PGS sweeps (bit-identical: colour classes are body-disjoint) ove...
void applyVelocityAndPredictPosition(int n, const BodyState &bodies, const BodyPred &pred, float dt)
Re-integration: persist solved velocity, trapezoidal position predict, quaternion integrate.
int colorVelocityPhase(Particles &P, int nm, int nBodies, bool usePersistPre, bool incrColorOff, const Kokkos::View< const unsigned char *, CpMem > mSleep, PhaseCopies &vcs, int slotBase, ColoringInvariant &inv)
Velocity-phase colouring (docs/contact_solve_framework.md §4.2 item 4, §4.4), the PGS paths only: col...
CpExec::memory_space CpMem
int * demIterCountSlot(Particles &P, int slot)
Diagnostics (docs/contact_solve_framework.md §12 S12): the device scalar a fused main loop writes its...
Kokkos::View< const float *[3], CpMem > coarseVertexInvInertia(Particles &P, int nBodies, bool velM)
The coarse vertices' inverse inertias for the multilevel pass (the same fraction).
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).
void buildCoarseBuckets(const ContactHierarchy &H, MlScratch &S, int numManifolds, Kokkos::View< int *, CpMem > colorScratch, Kokkos::View< int *, CpMem > perm, Kokkos::View< int *, CpMem > cursor, std::vector< std::vector< int > > &offs)
One multilevel stabilization cycle over an already-built hierarchy: fine colored smoothing is the cal...
void solveVelocityPhase(Particles &P, int nc, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, ColoringInvariant &inv)
The main velocity phase (docs/contact_solve_framework.md §4.4, §4.5, §13.3): the warm-started PGS ite...
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 closeVelocityPhase(Particles &P, int nm, const Hooks &hooks, const VelocitySolveContext &ctx)
The close of the velocity phase (§13.3 step 8): the orphan accounts back to balances,...
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...
void countFrictionContacts(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, Kokkos::View< const int *, CpMem > realIdx, FrManifoldCounts planeFriction)
Per-body active-contact count into planeFriction(:,1).
bool demFusedWanted(bool graphReplayAvailable, int mode)
Fused-sweep policy.
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.
void activityHitPosition(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int nu, Kokkos::View< const int *, CpMem > color, SlotOverride ov, Kokkos::View< unsigned char *, CpMem > hit, int nSlots)
The same over the position phase's units (raw slots of the leader contact, through the overrides).
void reseedCopies(CpExec space, const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w)
Re-seed every member from its base, sigma unchanged (the multilevel pass, after the coarse cycle has ...
int colorContactsIncremental(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, int numBodies, const CarriedColors &carried, Kokkos::View< int *, CpMem > cColor, Kokkos::View< unsigned long long *, CpMem > keysOut, const ColorScratch &scratch, int &leftover, bool forceFull, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, PosUnits units={})
Incremental (warm-started) contact colouring for the single-GPU PGS position solve.
VelocitySolveViews openVelocityCopies(Particles &P, int slotBase, bool velM, bool velCopiesOn, const PoissonBankViews &pv)
void stabilizeOneSided(Particles &P, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
The ONE-SIDED grounded stabilization pass (stabilization mode 1, the default): held-lower-side impuls...
void buildHubCopies(Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, int slotBase, Kokkos::View< const int *, CpMem > deg, PhaseCopies &H)
Build one phase's hub copies (§4.4 steps 2, 3 and 5): every vertex with d > kHubEdgeBudget active edg...
int readInt(Kokkos::View< int, CpMem > v)
float readFloat(Kokkos::View< float, CpMem > v)
void openVelocityWarmStart(Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const WarmStartContext &ws)
The velocity phase's warm start and opening (docs/contact_solve_framework.md §13.3 step 5): on the PG...
void buildInvInertiaCoarse(Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< const int *, CpMem > k, Kokkos::View< const int *, CpMem > a, int n, Kokkos::View< float *[3], CpMem > out)
The multilevel coarse vertex's inverse inertia (docs/contact_physics_followups.md §2....
void countRankLevelActivity(Particles &P, int nm, bool usePersistPre, const PosUnits &posUnits, int numPosUnits)
Rank-level M's activity pass (docs/contact_solve_framework.md §13.3) on a rank that exchanges: size t...
void computePlaneLoad(Kokkos::View< ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, FrManifoldCounts planeFriction)
Plane (idB<0) one-shot loads.
void buildSolveViews(Particles &P, const PhaseCopies &H, int nSlots, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia)
Solve views of a phase (§4.5): invMassSolve / invInertiaSolve = the phase's masses on every slot in [...
float withConsensus(Particles &P, float res, bool cons)
§12 S14: a stop vote's residual with the largest consensus correction since the previous vote folded ...
void commitContactColor(Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< const int *, CpMem > color, Kokkos::View< unsigned long long *, CpMem > prevKeys, Kokkos::View< int *, CpMem > prevColor, Kokkos::View< int *, CpMem > perm, int numContacts)
Commit this substep's per-contact (key, colour) sorted by key, for next substep's warm gather.
void stabilizeEscalation(Particles &P, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
The ESCALATION stabilization pass (stabilization mode 3, diagnostic/fallback): plain symmetric colour...
void seedCopySlotState(Particles &P, const PhaseCopies &H)
Seed the copy slots' read-only state from their hub vertex (§4.4 step 6): orientation,...
Kokkos::DefaultExecutionSpace CpExec
void growCopySlots(Particles &P, int slotBase, int nCopies)
Room for nCopies copy slots appended at slotBase (the colour phases' hub copies).
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...
int demReadIterCount(const Particles &P, int slot)
The count a fused main loop wrote (one small readback, only with P.iterCounters on); -1 with the coun...
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 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...
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...
int findCollisionsGrow(Particles &P, float margin)
Broad phase with an automatically-grown pair buffer.
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,...
void fillGidBase(Vi gid, int n, int base)
gid(i) = base + i over [0, n) — the per-rank global-id re-base of the distributed step (namespace sco...
constexpr float kSplitOmegaVelocity
Split relaxation (§4.5, §13.1): the PGS normal step on an edge touching a mass-split slot is scaled b...
void stabilizeMultilevel(Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
void syncVelocityPhase(Particles &P, const Hooks &hooks, const VelocitySolveContext &ctx)
A rank sync inside the velocity phase: orphan accounts back to balances, the raw reverse + forward,...
void massSplitVelocityIteration(Particles &P, int nm, const Hooks &hooks, float vRest)
Mass-split Jacobi (velocityUseGS off, a global setting; docs/contact_solve_framework....
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...
bool launchFusedVelocityLoop(Particles &P, int nm, float vRest, const VelocitySolveContext &ctx)
The main velocity phase's device-side iteration loop (CUDA, single-rank PGS with a fused schedule and...
void oneShotVelocityIteration(Particles &P, int nm, int it, float vRest, CpExec &space, const VelocitySolveContext &ctx)
One iteration of the g = 0 one-shot coloured GS velocity solve (velocityUseGS on, PGS off): the resid...
void foldVelocityCopies(CpExec &space, Particles &P, const VelocitySolveContext &ctx)
The velocity-phase copies' fold, run in every iteration after the sweep (none without copies).
void fillWorldRadii(Vf scale, Vf rad, float gs, float bR, int n)
World radii rad(i) = scale(i) * globalScale * baseRadius over [0, n): the owned span before a step,...
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-...
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...
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...
int countSplitBodies(const PhaseCopies &H)
Count the bodies whose group has k > 1 (split_stats).
int countSplitAggregated(Kokkos::View< const int *, CpMem > parent, int off, int nV, int nGroups1, Kokkos::View< const unsigned char *, CpMem > split)
split_stats.mlHubAggregated (§13.2's positive control): the vertices v in [0, nV) with split(v) !...
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.
int findCollisionsVerlet(Particles &P, float margin, float maxRad)
Verlet-cached impulse broadphase (single-GPU, non-periodic).
void recordMultilevelHierarchy(Particles &P, int nm, int nBodies, const ContactHierarchy &H, const MlScratch &S, bool velM, bool velCopiesOn)
The multilevel hierarchy's diagnostics: the debug-build inactive-vertex check, the hub aggregation co...
long long countUnfired(Kokkos::View< const unsigned char *, CpMem > gate, int nm)
Diagnostics (iterCounters): the number of gated-off manifolds of one sweep.
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)
int colorPositionPhase(Particles &P, int nc, int nm, bool usePersistPre, bool incrColorOff, const Kokkos::View< const unsigned char *, CpMem > cSleep, PhaseCopies &pcs, int slotBase, ColoringInvariant &inv, int &numPosUnits, PosUnits &posUnits)
Position-phase colouring (docs/contact_solve_framework.md §4.2 item 3, §4.3, §4.4),...
void multilevelCoarseCycle(const MlManifoldViews &mv, int numManifolds, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< float *[3], CpMem > velPred, int numReal, const ContactHierarchy &H, MlScratch &S, int coarseSweeps, const MlBodyViews &body, const std::vector< std::vector< int > > *bkOffs=nullptr, Kokkos::View< const int *, CpMem > bkPerm={}, const MlFusedCtx *fused=nullptr)
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...
void solvePosition(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, const BodyDelta &delta, Kokkos::View< float, CpMem > maxOverlap, Kokkos::View< const int *, CpMem > onlyColor={}, int colorFilter=0, bool massSplit=false)
Accumulate XPBD position corrections for numContacts contacts.
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),...
PoissonBankViews poissonBankViews(Particles &P, bool on)
The solve's Poisson views: the banks and the orphan accounts when the model is on,...
MlFusedCtx demMakeMlFusedCtx(CpExec &space, const ContactHierarchy &H, const std::vector< std::vector< int > > &bkOffs, int numManifolds, int numReal, int coarseSweeps, Kokkos::View< int *, CpMem > offsDev, Kokkos::View< unsigned *, CpMem > bar)
Build the fused-coarse-cycle context from an already-built hierarchy + its dense buckets: flatten the...
void growCopyView(V &v, std::size_t n, const char *label)
Grow-only (re)allocation of a copy-machinery view to at least n entries.
void countPositionJacobi(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, Kokkos::View< int *, CpMem > counts)
Mass-split Jacobi count pass (docs/contact_solve_framework.md §3.1, D3): per body slot,...
void runOverlapProjection(Particles &P, int nc, const Hooks &hooks, CpExec &space, const PositionSolveContext &ctx, EmitPosIter &&emitPosIter, SyncPos &&syncPos, ColoringInvariant &inv)
The overlap projection's iterations to their adaptive stop (WO-12): the fused device loop (CUDA,...
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 ...
void buildCopyGroups(Kokkos::View< const int *, CpMem > realIdx, int span, int numReal, Kokkos::View< const int *, CpMem > deg, Kokkos::View< const float *[3], CpMem > imageShift, PhaseCopies &H, bool identityBodies=false)
Build the fold groups of a phase (§4.4 "local fold", §WO-4 item 2): every body with more than one slo...
void allocateMultilevelPools(Particles &P, int nBodies)
The multilevel pass's rigid-aggregate pools and coarse accumulator, sized for nBodies.
void expandUnitColors(const PosUnits &units, int numUnits, Kokkos::View< const int *, CpMem > unitColor, Kokkos::View< int *, CpMem > contactColor)
Copy each position unit's colour onto its contacts (contactColor(i) = unitColor(unit of i)).
void foldCopies(CpExec space, const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w, Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanPeak, Kokkos::View< float, CpMem > consensus={})
The local fold (§4.4), once per iteration after the sweep: per group T = sum over the ACTIVE members ...
int findCollisionsArborX(PosV pos, RadV rad, int numParticles, int numReal, float margin, PairsV outPairs, CountV outCount, float boxCap=0.0f)
Emit candidate collision pairs (i<j) for real particles into outPairs/outCount.
void accumulateNormalImpulse(Kokkos::View< ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, Kokkos::View< const int *, CpMem > realIdx, float growthRate, bool dedupTwins=true)
Force-chain normal load, accumulated over the velocity iterations: contacts(idx).friction_lambda_n +=...
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...
bool islandSleepingOn(const Particles &P, bool gravity)
Island sleeping: single-GPU statics, gravity on, no external drag.
int countInactiveAggregated(Kokkos::View< const int *, CpMem > parent, int off, int nV, int nGroups1, Kokkos::View< const int *, CpMem > a)
Debug-build check (§13.2, §13.5 WO-5 item 4): the vertices with a = 0 that sit in a level-1 multileve...
FusedSweepCtx demMakeFusedCtx(CpExec &space, const std::vector< int > &offs, Kokkos::View< int *, CpMem > offsDev, Kokkos::View< unsigned *, CpMem > bar)
Fill a FusedSweepCtx from buildColorBuckets's host offsets: async-upload them into the pooled device ...
void buildInvMassCoarse(Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const int *, CpMem > k, Kokkos::View< const int *, CpMem > a, int n, Kokkos::View< float *, CpMem > out)
The multilevel coarse vertex's inverse mass (§13.2): invMass(q) (k(q) / max(1, a(q))),...
VelocitySweepBodies velocitySweepBodies(Particles &P, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia, SlotOverride ov={})
The velocity sweep's bodies: the masses invMass/invInertia (true masses or the solve copies'),...
Kokkos::View< const float *, CpMem > coarseVertexInvMass(Particles &P, int nBodies, bool velM)
The MULTILEVEL (GraphMG) stabilization pass (stabilization mode 2): fine sweeps plus a coarse cycle o...
ContactHierarchy buildContactHierarchy(const MlEligibility &elig, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *, CpMem > invMass, int numReal, MlScratch &S, const ColorScratch &scratch, bool excludeImmovable=false, Kokkos::View< const unsigned char *, CpMem > asleep={}, const MlBodyViews &body={})
Build the aggregation hierarchy + per-level crossing-manifold colorings.
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") ...
void unfoldOrphan(const PhaseCopies &H, Kokkos::View< float *, CpMem > orphan)
Orphan accounts back from share form (before a rank sync and at the end of the velocity phase): the b...
dem — portable (Kokkos) particle SoA container: the storage the dem flip pivots on.
dem — body copies of the contact solve (docs/contact_solve_framework.md §4.4, §4.5,...
#define PECLET_DEM_GRAPH_LOOP(useVar, graphVar, emitVar, slotVar)
dem — portable (Kokkos) Coulomb friction cluster (the single dissipative friction path).
dem — multilevel (GraphMG-style) momentum-conserving contact stabilization.
dem — portable (Kokkos) XPBD position solve (pure overlap removal).
dem — portable (Kokkos) manifold velocity solve (normal restitution impulse).
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
The colour classes one coloured sweep visits: manifolds [0, numManifolds), labelled by mColor in [0,...
The colouring invariant (§4.2 item 4, §12 S3): the failing phase's edges, the accumulated message,...
float vote(const Hooks &hooks, float res)
Kokkos::View< int *, CpMem > edgeA
Kokkos::View< int *, CpMem > edgeB
void note(const char *phase, Kokkos::View< const int *, CpMem > color, int n, Kokkos::View< const int *, CpMem > vertexDegree)
bool capture(CpExec &, F &&, void *&)
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...
The per-body inputs of the rigid coarse space (§2.3-§2.5): the predicted positions (the member offset...
The manifolds of a hierarchy build and the inputs of mldetail::eligible (constant over the build): th...
Fused-coarse-cycle context: flat per-level colour offsets on device + barrier + meta.
The manifold side of a coarse sweep: the manifolds, their real-body map, the shared impulse accumulat...
Device scratch for the multilevel pass, sized once (see Particles::allocate).
Kokkos::View< int *, CpMem > parent
One full colored PGS sweep.
std::vector< int > parentOff
std::vector< int > numGroups
Kokkos::View< float, CpMem > posResidual
Kokkos::View< int *, CpMem > commitPerm
Kokkos::View< unsigned *, CpMem > fusedBar
Kokkos::View< unsigned char *, CpMem > asleep
Kokkos::View< int *, CpMem > bucketCursor
Kokkos::View< unsigned char *, CpMem > sideFlags
Kokkos::View< unsigned long long *, CpMem > prevContactKeys
Kokkos::View< int *, CpMem > mlGrp
Kokkos::View< unsigned char *, CpMem > xGate
Kokkos::View< unsigned char *, CpMem > manifoldPersistent
Kokkos::View< int *, CpMem > aPos
Kokkos::View< float *[3], CpMem > impRefPos
Kokkos::View< int *, CpMem > posOffsDev
Kokkos::View< float *, CpMem > bodyOrphanVPeak
Kokkos::View< int *, CpMem > velPerm
PairLedger pairLedger() const
Kokkos::View< unsigned char *, CpMem > contactSleep
void ensureCapacity(int newCap)
Kokkos::View< int *, CpMem > unitStart
Kokkos::View< ManifoldC *, CpMem > manifolds
Kokkos::View< float *[3], CpMem > mlOriginG
Kokkos::View< float *, CpMem > vn0
Kokkos::View< float *, CpMem > mlMassG
Kokkos::View< int *, CpMem > levelKey
Kokkos::View< int *[2], CpMem > pairs
Kokkos::View< float *[3], CpMem > invInertiaSolve
Kokkos::View< float *[3], CpMem > imageShift
BodyPred bodyPred() const
Kokkos::View< float *[3], CpMem > mlAngG0
Kokkos::View< float *[3], CpMem > vt0
Kokkos::View< int *, CpMem > vertexDegree
Kokkos::View< float *, CpMem > restRel
Kokkos::View< int *, CpMem > levelPerm
Kokkos::View< int *, CpMem > prevManifoldColor
Kokkos::View< float *[3], CpMem > mlAngG
bool velCopiesLastSubstep
Kokkos::View< unsigned char *, CpMem > splitSlot
Kokkos::View< int *, CpMem > kVel
Kokkos::View< float *[3], CpMem > mlComOffG
Kokkos::View< unsigned long long *, CpMem > contactKeys
Kokkos::View< int *, CpMem > prevContactColor
Kokkos::View< unsigned char *, CpMem > prevMatched
Kokkos::View< unsigned char *, CpMem > activityHit
Kokkos::View< float, CpMem > maxApproach
Kokkos::View< float *, CpMem > restVPeak
Kokkos::View< float *, CpMem > restBank
CarriedColors prevManifoldColors() const
Kokkos::View< int *, CpMem > posCommitPerm
Kokkos::View< int *, CpMem > velOffsDev
Kokkos::View< int *, CpMem > mlOffsDev
Kokkos::View< unsigned long long *, CpMem > pairKeys
BodyState bodyState() const
struct peclet::dem::Particles::MlLast mlLast
Kokkos::View< int *, CpMem > aVel
Kokkos::View< float, CpMem > maxConsensus
Kokkos::View< float *, CpMem > bodyOrphan
Kokkos::View< unsigned long long *, CpMem > prevPairKeys
Kokkos::View< int *, CpMem > posPerm
Kokkos::View< float *, CpMem > prevPosImpulse
ColorScratch colorScratch() const
Kokkos::View< int *, CpMem > contactColor
Kokkos::View< float *[3], CpMem > mlVelG
Kokkos::View< float *, CpMem > invMassSolve
Kokkos::View< float *[3], CpMem > mlVelG0
Kokkos::View< int *, CpMem > mlBucketPerm
Kokkos::View< float *[3], CpMem > invInertiaCoarse
Kokkos::View< int *, CpMem > mlParent
Kokkos::View< float *[3], CpMem > lambdaT
CarriedColors prevContactColors() const
Kokkos::View< int *, CpMem > unitColor
Kokkos::View< int *, CpMem > kPos
Kokkos::View< float, CpMem > maxOverlap
Kokkos::View< float *, CpMem > posLambdaContact
Kokkos::View< unsigned long long *, CpMem > velMask
Kokkos::View< float *, CpMem > posImpulse
Kokkos::View< int *, CpMem > contactSlot
PairLedger prevPairLedger() const
Kokkos::View< float *, CpMem > mlInvMassG
Kokkos::View< int *, CpMem > heightLevel
BodyDelta bodyDelta() const
Kokkos::View< float *, CpMem > lambdaAcc
Kokkos::View< double *[7], CpMem > mlAccD
Kokkos::View< long long *, CpMem > mlColorPacked
Kokkos::View< float *, CpMem > invMassCoarse
Kokkos::View< int *, CpMem > iterCountDev
Kokkos::View< float, CpMem > maxApproachQS
Kokkos::View< unsigned char *, CpMem > manifoldSleep
Kokkos::View< const float *, CpMem > crad() const
Kokkos::View< float *[2], CpMem > planeFriction
Kokkos::View< int *, CpMem > mlMate
Kokkos::View< ContactC *, CpMem > contacts
Kokkos::View< int, CpMem > pairCount
Kokkos::View< unsigned char *, CpMem > groundedLevel
Kokkos::View< int *, CpMem > unitContacts
Kokkos::View< int *, CpMem > manifoldColor
Kokkos::View< float *[6], CpMem > mlInvIG
One solve phase's body copies (docs/contact_solve_framework.md §4.4, §4.5, §WO-4 item 2): the hub cop...
Kokkos::View< int *, CpMem > slotB
Kokkos::View< int *, CpMem > slotA
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 position units of a contact list as a CSR (§4.3): unit u owns the contacts list(start(u)) .
The overlap projection's loop context: what its iterations read that solvePositionPhase settles befor...
Rank-level M (docs/contact_solve_framework.md §13.3) as the hooks expose it to the driver: whether th...
Shock propagation for persistent loaded contacts (see solveVelocity): the persistence flags,...
Per-edge slot overrides of a phase's hub copies (docs/contact_solve_framework.md §4....
Single-GPU hooks: no ghost refresh, residuals are already global. Everything inlines away.
float allMaxAny(float v, bool &) const
static constexpr bool distributed
void beginSolve(Particles &) const
void restoreOrphanBalance(Particles &) const
void publishPositions(Particles &) const
void syncContactCounts(Particles &) const
int visibleManifolds(int nm) const
float allMax(float v) const
void openPositionCounts(Particles &, bool) const
void syncVelocities(Particles &) const
bool syncPoint(int) const
void syncFrictionCounts(Particles &) const
void syncPositions(Particles &) const
void openVelocityPhase(Particles &, bool) const
diagnostics.split_stats() (docs/contact_solve_framework.md §WO-4 item 4, §13.5 WO-4b): the last subst...
long long driftMigrations
long long unfiredSplitContacts
One coloured phase's sweep plan: owns the dense-bucket offsets and the fused context that its ColorSc...
SweepPlan & operator=(const SweepPlan &)=delete
SweepPlan(const SweepPlan &)=delete
The velocity phase's solve context: everything the velocity-phase loops read that demSolveContacts se...
const FusedSweepCtx * velFusedP
Kokkos::View< const float *, CpMem > invMassVel
Kokkos::View< const int *, CpMem > velPermC
PoissonBankViews poissonViews
Kokkos::View< const float *[3], CpMem > invInertiaVel
The velocity phase's solve views and slot override: true masses; the hub copies' split masses; or eac...
Kokkos::View< const float *, CpMem > invMass
Kokkos::View< const float *[3], CpMem > invInertia
The bodies a velocity sweep reads and updates in place: mass data and orientation,...
The warm start's inputs that demSolveContacts settles before it: the pair-key map,...
Kokkos::View< const int *, CpMem > keyIdx
Rank-level X evaluated inline in the one-shot sweep (only for the manifolds of the colour being swept...