19#ifndef DEM_SLEEPING_HPP
20#define DEM_SLEEPING_HPP
22#include <Kokkos_Core.hpp>
31inline void freezeAsleep(
int numReal, Kokkos::View<const unsigned char*, CpMem> asleep,
33 const Kokkos::View<const float* [3], CpMem> pos = bodies.
pos;
34 const Kokkos::View<const float* [4], CpMem> quat = bodies.
quat;
35 const Kokkos::View<float* [3], CpMem> posPred = pred.
posPred;
36 const Kokkos::View<float* [4], CpMem> quatPred = pred.
quatPred;
37 const Kokkos::View<float* [3], CpMem> velPred = pred.
velPred;
38 const Kokkos::View<float* [3], CpMem> angVelPred = pred.
angVelPred;
41 "peclet::dem::freeze_asleep", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
42 KOKKOS_LAMBDA(
int i) {
45 for (
int c = 0; c < 3; ++c) {
46 posPred(i, c) = pos(i, c);
48 angVelPred(i, c) = 0.0f;
50 for (
int c = 0; c < 4; ++c)
51 quatPred(i, c) = quat(i, c);
59inline void buildInvMassEff(
int numBodies, Kokkos::View<const unsigned char*, CpMem> asleep,
60 Kokkos::View<const int*, CpMem> realIdx,
61 Kokkos::View<const float*, CpMem> invMass,
62 Kokkos::View<float*, CpMem> invMassEff,
float sleeperFrac) {
65 "peclet::dem::inv_mass_eff", Kokkos::RangePolicy<CpExec>(space, 0, numBodies),
66 KOKKOS_LAMBDA(
int i) {
67 invMassEff(i) = asleep(realIdx(i)) ? sleeperFrac * invMass(i) : invMass(i);
76 Kokkos::View<const int*, CpMem> realIdx,
77 Kokkos::View<const unsigned char*, CpMem> asleep,
78 Kokkos::View<unsigned char*, CpMem> manifoldSleep) {
81 "peclet::dem::manifold_sleep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
82 KOKKOS_LAMBDA(
int idx) {
86 const bool aA = asleep(realIdx(m.
bodyA)) != 0;
87 const bool aB = (m.
bodyB < 0) ?
true : (asleep(realIdx(m.
bodyB)) != 0);
88 s = (aA && aB) ? 1 : 0;
90 manifoldSleep(idx) = s;
97 Kokkos::View<const int*, CpMem> realIdx,
98 Kokkos::View<const unsigned char*, CpMem> asleep,
99 Kokkos::View<unsigned char*, CpMem> contactSleep) {
101 Kokkos::parallel_for(
102 "peclet::dem::contact_sleep", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
103 KOKKOS_LAMBDA(
int idx) {
105 const bool aA = asleep(realIdx(c.
bodyA)) != 0;
106 const bool aB = (c.
bodyB < 0) ?
true : (asleep(realIdx(c.
bodyB)) != 0);
107 contactSleep(idx) = (aA && aB) ? 1 : 0;
119inline void wakeDisturbed(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
120 Kokkos::View<const int*, CpMem> realIdx,
121 Kokkos::View<
const float* [3],
CpMem> velPred,
float wakeSpeed,
123 const Kokkos::View<unsigned char*, CpMem> asleep = sleep.
asleep;
124 const Kokkos::View<unsigned char*, CpMem> sleepCounter = sleep.
sleepCounter;
125 const Kokkos::View<unsigned char*, CpMem> movingWall = sleep.
sleepMovingWall;
126 const Kokkos::View<int*, CpMem> curCount = sleep.
sleepCurCount;
128 Kokkos::parallel_for(
129 "peclet::dem::wake_reset", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
130 KOKKOS_LAMBDA(
int i) {
134 const float wakeSpeed2 = wakeSpeed * wakeSpeed;
135 Kokkos::parallel_for(
136 "peclet::dem::wake_disturbed", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
137 KOKKOS_LAMBDA(
int idx) {
141 const int ra = realIdx(m.
bodyA);
142 Kokkos::atomic_add(&curCount(ra), 1);
145 rb = realIdx(m.
bodyB);
148 Kokkos::atomic_add(&curCount(rb), 1);
153 if (wv.
x != 0.0f || wv.
y != 0.0f || wv.
z != 0.0f) {
157 sleepCounter(ra) = 0;
162 const bool sA = asleep(ra) != 0, sB = asleep(rb) != 0;
166 const int awake = sA ? rb : ra, sleeper = sA ? ra : rb;
167 const float vx = velPred(awake, 0), vy = velPred(awake, 1), vz = velPred(awake, 2);
168 if (vx * vx + vy * vy + vz * vz > wakeSpeed2) {
170 sleepCounter(sleeper) = 0;
180 const Kokkos::View<const int*, CpMem> curCount = sleep.
sleepCurCount;
182 const Kokkos::View<const unsigned char*, CpMem> movingWall = sleep.
sleepMovingWall;
183 const Kokkos::View<unsigned char*, CpMem> asleep = sleep.
asleep;
184 const Kokkos::View<unsigned char*, CpMem> sleepCounter = sleep.
sleepCounter;
186 Kokkos::parallel_for(
187 "peclet::dem::wake_contact_change", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
188 KOKKOS_LAMBDA(
int i) {
193 if (wakeOnChange && asleep(i) && curCount(i) < prevCount(i)) {
199 prevCount(i) = curCount(i);
210 Kokkos::View<const unsigned char*, CpMem> grounded,
const SleepState& sleep,
211 float sleepSpeed,
int K) {
212 const Kokkos::View<const float* [3], CpMem> vel = bodies.
vel;
213 const Kokkos::View<const float* [3], CpMem> angVel = bodies.
angVel;
214 const Kokkos::View<const unsigned char*, CpMem> movingWall = sleep.
sleepMovingWall;
215 const Kokkos::View<unsigned char*, CpMem> asleep = sleep.
asleep;
216 const Kokkos::View<unsigned char*, CpMem> sleepCounter = sleep.
sleepCounter;
217 const Kokkos::View<float* [3], CpMem> velOut = bodies.
vel;
218 const Kokkos::View<float* [3], CpMem> angVelOut = bodies.
angVel;
220 const float s2 = sleepSpeed * sleepSpeed;
221 Kokkos::parallel_for(
222 "peclet::dem::update_sleep", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
223 KOKKOS_LAMBDA(
int i) {
226 const float vx = vel(i, 0), vy = vel(i, 1), vz = vel(i, 2);
227 const float wx = angVel(i, 0), wy = angVel(i, 1), wz = angVel(i, 2);
228 const float r = rad(i);
229 const bool lowLin = (vx * vx + vy * vy + vz * vz) < s2;
230 const bool lowAng = (wx * wx + wy * wy + wz * wz) * r * r < s2;
231 if (lowLin && lowAng && grounded(i) > 0 && !movingWall(i)) {
232 int c =
static_cast<int>(sleepCounter(i)) + 1;
235 for (
int k = 0; k < 3; ++k) {
237 angVelOut(i, k) = 0.0f;
241 sleepCounter(i) =
static_cast<unsigned char>(c > 255 ? 255 : c);
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
void wakeDisturbed(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *[3], CpMem > velPred, float wakeSpeed, const SleepState &sleep, int numReal)
Wake pass (before the solve): a sleeper is woken when actually disturbed.
void wakeContactChange(int numReal, const SleepState &sleep, bool wakeOnChange)
Contact-set-change wake rule (b): wake any still-asleep body whose live contact count differs from th...
CpExec::memory_space CpMem
void computeManifoldSleep(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const unsigned char *, CpMem > asleep, Kokkos::View< unsigned char *, CpMem > manifoldSleep)
Per-manifold "both endpoints asleep" flag (a static wall, bodyB < 0, counts as asleep).
void freezeAsleep(int numReal, Kokkos::View< const unsigned char *, CpMem > asleep, const BodyState &bodies, const BodyPred &pred)
After predictVelocity: re-freeze the currently-asleep bodies so gravity/prediction do not move them —...
void computeContactSleep(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const unsigned char *, CpMem > asleep, Kokkos::View< unsigned char *, CpMem > contactSleep)
Per-contact twin of computeManifoldSleep for the position colouring.
Kokkos::DefaultExecutionSpace CpExec
void buildInvMassEff(int numBodies, Kokkos::View< const unsigned char *, CpMem > asleep, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< float *, CpMem > invMassEff, float sleeperFrac)
Effective inverse mass for the solve: a sleeping body (real, or a periodic ghost whose real is asleep...
void updateSleep(int numReal, const BodyState &bodies, Kokkos::View< const float *, CpMem > rad, Kokkos::View< const unsigned char *, CpMem > grounded, const SleepState &sleep, float sleepSpeed, int K)
Sleep detection (after the commit): an AWAKE, grounded body whose linear AND angular motion has staye...
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< float *[3], CpMem > posPred
Kokkos::View< float *[4], CpMem > quatPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > pos
Kokkos::View< float *[3], CpMem > angVel
Kokkos::View< float *[4], CpMem > quat
Kokkos::View< float *[3], CpMem > vel
Portable mirror of ManifoldConstraint.
Island-sleeping state per real body: asleep flag, low-motion counter, moving-wall flag,...
Kokkos::View< unsigned char *, CpMem > asleep
Kokkos::View< int *, CpMem > sleepPrevCount
Kokkos::View< int *, CpMem > sleepCurCount
Kokkos::View< unsigned char *, CpMem > sleepMovingWall
Kokkos::View< unsigned char *, CpMem > sleepCounter