peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
sleeping.hpp
Go to the documentation of this file.
1
19#ifndef DEM_SLEEPING_HPP
20#define DEM_SLEEPING_HPP
21
22#include <Kokkos_Core.hpp>
23
24#include "contact_preprocessing.hpp" // ManifoldC, CpExec/CpMem
25#include "dem_portable.hpp"
26
27namespace peclet::dem {
28
31inline void freezeAsleep(int numReal, Kokkos::View<const unsigned char*, CpMem> asleep,
32 const BodyState& bodies, const BodyPred& pred) {
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;
39 CpExec space;
40 Kokkos::parallel_for(
41 "peclet::dem::freeze_asleep", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
42 KOKKOS_LAMBDA(int i) {
43 if (!asleep(i))
44 return;
45 for (int c = 0; c < 3; ++c) {
46 posPred(i, c) = pos(i, c);
47 velPred(i, c) = 0.0f;
48 angVelPred(i, c) = 0.0f;
49 }
50 for (int c = 0; c < 4; ++c)
51 quatPred(i, c) = quat(i, c);
52 });
53 space.fence();
54}
55
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) {
63 CpExec space;
64 Kokkos::parallel_for(
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);
68 });
69 space.fence();
70}
71
75inline void computeManifoldSleep(Kokkos::View<const ManifoldC*, CpMem> manifolds, int numManifolds,
76 Kokkos::View<const int*, CpMem> realIdx,
77 Kokkos::View<const unsigned char*, CpMem> asleep,
78 Kokkos::View<unsigned char*, CpMem> manifoldSleep) {
79 CpExec space;
80 Kokkos::parallel_for(
81 "peclet::dem::manifold_sleep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
82 KOKKOS_LAMBDA(int idx) {
83 const ManifoldC m = manifolds(idx);
84 unsigned char s = 0;
85 if (m.num_points > 0) {
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;
89 }
90 manifoldSleep(idx) = s;
91 });
92 space.fence();
93}
94
96inline void computeContactSleep(Kokkos::View<const ContactC*, CpMem> contacts, int numContacts,
97 Kokkos::View<const int*, CpMem> realIdx,
98 Kokkos::View<const unsigned char*, CpMem> asleep,
99 Kokkos::View<unsigned char*, CpMem> contactSleep) {
100 CpExec space;
101 Kokkos::parallel_for(
102 "peclet::dem::contact_sleep", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
103 KOKKOS_LAMBDA(int idx) {
104 const ContactC c = contacts(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;
108 });
109 space.fence();
110}
111
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,
122 const SleepState& sleep, int numReal) {
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;
127 CpExec space;
128 Kokkos::parallel_for(
129 "peclet::dem::wake_reset", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
130 KOKKOS_LAMBDA(int i) {
131 movingWall(i) = 0;
132 curCount(i) = 0;
133 });
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) {
138 const ManifoldC m = manifolds(idx);
139 if (m.num_points <= 0)
140 return;
141 const int ra = realIdx(m.bodyA);
142 Kokkos::atomic_add(&curCount(ra), 1);
143 int rb = -1;
144 if (m.bodyB >= 0) {
145 rb = realIdx(m.bodyB);
146 if (rb == ra)
147 return; // periodic self-pair
148 Kokkos::atomic_add(&curCount(rb), 1);
149 }
150 // Moving wall (boundary manifold with nonzero wall velocity): never sleeps.
151 if (m.bodyB < 0) {
152 const F4 wv = m.wallVel_sum;
153 if (wv.x != 0.0f || wv.y != 0.0f || wv.z != 0.0f) {
154 movingWall(ra) = 1;
155 if (asleep(ra)) {
156 asleep(ra) = 0;
157 sleepCounter(ra) = 0;
158 }
159 }
160 return;
161 }
162 const bool sA = asleep(ra) != 0, sB = asleep(rb) != 0;
163 if (sA == sB)
164 return; // both awake or both asleep: no cross-wake here
165 // Exactly one asleep: wake it if the awake side moves faster than wakeSpeed.
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) {
169 asleep(sleeper) = 0;
170 sleepCounter(sleeper) = 0;
171 }
172 });
173 space.fence();
174}
175
179inline void wakeContactChange(int numReal, const SleepState& sleep, bool wakeOnChange) {
180 const Kokkos::View<const int*, CpMem> curCount = sleep.sleepCurCount;
181 const Kokkos::View<int*, CpMem> prevCount = sleep.sleepPrevCount;
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;
185 CpExec space;
186 Kokkos::parallel_for(
187 "peclet::dem::wake_contact_change", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
188 KOKKOS_LAMBDA(int i) {
189 // Rule (b): a change in the live contact count. In a near-threshold settled bed contacts
190 // FLICKER in and out every substep, so waking on any change stops the bed ever freezing;
191 // waking only on a LOST contact (support removed), and only when enabled, is the robust
192 // form. A gained contact is extra support and never a reason to wake.
193 if (wakeOnChange && asleep(i) && curCount(i) < prevCount(i)) {
194 asleep(i) = 0;
195 sleepCounter(i) = 0;
196 }
197 if (movingWall(i))
198 sleepCounter(i) = 0;
199 prevCount(i) = curCount(i);
200 });
201 space.fence();
202}
203
209inline void updateSleep(int numReal, const BodyState& bodies, Kokkos::View<const float*, CpMem> rad,
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;
219 CpExec space;
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) {
224 if (asleep(i))
225 return; // stays asleep until an explicit wake
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;
233 if (c >= K) {
234 asleep(i) = 1;
235 for (int k = 0; k < 3; ++k) {
236 velOut(i, k) = 0.0f;
237 angVelOut(i, k) = 0.0f;
238 }
239 c = K; // saturate the counter
240 }
241 sleepCounter(i) = static_cast<unsigned char>(c > 255 ? 255 : c);
242 } else {
243 sleepCounter(i) = 0;
244 }
245 });
246 space.fence();
247}
248
249} // namespace peclet::dem
250
251#endif // DEM_SLEEPING_HPP
dem — portable (Kokkos) contact->manifold reduction, replacing the thrust-based reduce_contacts_to_ma...
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.
Definition sleeping.hpp:119
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...
Definition sleeping.hpp:179
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).
Definition sleeping.hpp:75
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 —...
Definition sleeping.hpp:31
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.
Definition sleeping.hpp:96
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...
Definition sleeping.hpp:59
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...
Definition sleeping.hpp:209
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 ParticleSystem.cuh ContactConstraint (the fields this reduction touches).
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