peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
integration.hpp
Go to the documentation of this file.
1
9#ifndef DEM_INTEGRATION_HPP
10#define DEM_INTEGRATION_HPP
11
12#include <Kokkos_Core.hpp>
13
14#include "contact_preprocessing.hpp" // ContactC, CpExec/CpMem
15#include "dem_portable.hpp"
16
17namespace peclet::dem {
18
23
24using V3 = Kokkos::View<float* [3], CpMem>;
25using V4 = Kokkos::View<float* [4], CpMem>;
26using Vf = Kokkos::View<float*, CpMem>;
27using Vi = Kokkos::View<int*, CpMem>;
28
29namespace detail {
30KOKKOS_INLINE_FUNCTION void st3(const V3& v, int i, F3 a) {
31 v(i, 0) = a.x;
32 v(i, 1) = a.y;
33 v(i, 2) = a.z;
34}
35KOKKOS_INLINE_FUNCTION void st4(const V4& v, int i, F4 a) {
36 v(i, 0) = a.x;
37 v(i, 1) = a.y;
38 v(i, 2) = a.z;
39 v(i, 3) = a.w;
40}
41} // namespace detail
42
45inline void predictVelocity(int n, const BodyState& bodies, const BodyPred& pred,
46 const BodyDelta& delta, F3 gravity, float dt, V3 extForce,
47 V3 extTorque) {
48 const V3 pos = bodies.pos, vel = bodies.vel, angVel = bodies.angVel,
49 invInertia = bodies.invInertia;
50 const Vf invMass = bodies.invMass;
51 const V4 quat = bodies.quat;
52 const V3 posPred = pred.posPred, velPred = pred.velPred, angVelPred = pred.angVelPred;
53 const V4 quatPred = pred.quatPred;
54 const V3 deltaPos = delta.deltaPos, deltaVel = delta.deltaVel, deltaAngVel = delta.deltaAngVel;
55 const V4 deltaQuat = delta.deltaQuat;
56 const Vi constraintCounts = delta.constraintCounts;
57 CpExec space;
58 Kokkos::parallel_for(
59 "peclet::dem::predict_velocity", Kokkos::RangePolicy<CpExec>(space, 0, n),
60 KOKKOS_LAMBDA(int i) {
61 const float invM = invMass(i);
62 F3 v = ldF3(vel, i);
63 if (invM > 0.0f) {
64 v = add3(v, scale3(gravity, dt));
65 // external force (fluid drag, etc.): F=ma => dv = F*invMass*dt (extForce is a FORCE, so
66 // it scales with 1/mass; gravity above is already an acceleration).
67 v = add3(v, scale3(ldF3(extForce, i), invM * dt));
68 }
69 detail::st3(velPred, i, v);
70
71 F3 wpred = ldF3(angVel, i);
72 if (invM > 0.0f) {
73 const F3 invI = ldF3(invInertia, i);
74 if (invI.x > 0.0f || invI.y > 0.0f || invI.z > 0.0f) {
75 const F4 q = ldF4(quat, i);
76 F3 wb = invRotateVector(q, ldF3(angVel, i));
77 const F3 Ib{(invI.x > 1e-9f) ? 1.0f / invI.x : 0.0f,
78 (invI.y > 1e-9f) ? 1.0f / invI.y : 0.0f,
79 (invI.z > 1e-9f) ? 1.0f / invI.z : 0.0f};
80 const F3 Lb{Ib.x * wb.x, Ib.y * wb.y, Ib.z * wb.z};
81 const F3 wxL = cross3v(wb, Lb);
82 // Euler's equations in the body (principal) frame:
83 // I dw/dt = tau_body - w x (I w) => dw = invI (tau_body - w x I w) dt.
84 // The external torque arrives in the WORLD frame and is rotated in here, the same
85 // way the angular velocity above is.
86 const F3 tb = invRotateVector(q, ldF3(extTorque, i));
87 const F3 alpha{invI.x * (tb.x - wxL.x), invI.y * (tb.y - wxL.y),
88 invI.z * (tb.z - wxL.z)};
89 wb = add3(wb, scale3(alpha, dt));
90 wpred = rotateVector(q, wb);
91 }
92 }
93 detail::st3(angVelPred, i, wpred);
94
95 F3 x = ldF3(pos, i);
96 if (invM > 0.0f)
97 x = add3(x, scale3(v, dt));
98 detail::st3(posPred, i, x);
99 detail::st4(quatPred, i, ldF4(quat, i));
100
101 detail::st3(deltaVel, i, F3{0, 0, 0});
102 detail::st3(deltaAngVel, i, F3{0, 0, 0});
103 detail::st3(deltaPos, i, F3{0, 0, 0});
104 detail::st4(deltaQuat, i, F4{0, 0, 0, 0});
105 constraintCounts(i) = 0;
106 });
107 space.fence();
108}
109
112inline void applyVelocityDeltas(int n, V3 velPred, V3 angVelPred, V3 deltaVel, V3 deltaAngVel) {
113 CpExec space;
114 Kokkos::parallel_for(
115 "peclet::dem::apply_velocity_deltas", Kokkos::RangePolicy<CpExec>(space, 0, n),
116 KOKKOS_LAMBDA(int i) {
117 detail::st3(velPred, i, add3(ldF3(velPred, i), ldF3(deltaVel, i)));
118 detail::st3(angVelPred, i, add3(ldF3(angVelPred, i), ldF3(deltaAngVel, i)));
119 detail::st3(deltaVel, i, F3{0, 0, 0});
120 detail::st3(deltaAngVel, i, F3{0, 0, 0});
121 });
122 space.fence();
123}
124
127inline void applyVelocityAndPredictPosition(int n, const BodyState& bodies, const BodyPred& pred,
128 float dt) {
129 const V3 pos = bodies.pos, vel = bodies.vel, angVel = bodies.angVel;
130 const Vf invMass = bodies.invMass;
131 const V4 quat = bodies.quat;
132 const V3 velPred = pred.velPred, angVelPred = pred.angVelPred, posPred = pred.posPred;
133 const V4 quatPred = pred.quatPred;
134 CpExec space;
135 Kokkos::parallel_for(
136 "peclet::dem::apply_vel_predict_pos", Kokkos::RangePolicy<CpExec>(space, 0, n),
137 KOKKOS_LAMBDA(int i) {
138 const F3 vStart = ldF3(vel, i);
139 const F3 vFinal = ldF3(velPred, i); // already includes deltas
140 detail::st3(velPred, i, vFinal);
141 detail::st3(vel, i, vFinal); // persist v_{n+1}
142
143 const float invM = invMass(i);
144 F3 x = ldF3(pos, i);
145 if (invM > 0.0f)
146 x = add3(x, scale3(add3(vStart, vFinal), 0.5f * dt));
147 detail::st3(posPred, i, x);
148
149 const F4 q = ldF4(quat, i);
150 const F3 omega = ldF3(angVelPred, i);
151 detail::st3(angVel, i, omega);
152 F4 qp = q;
153 if (invM > 0.0f) {
154 qp.x += 0.5f * dt * (omega.x * q.w + omega.y * q.z - omega.z * q.y);
155 qp.y += 0.5f * dt * (omega.y * q.w + omega.z * q.x - omega.x * q.z);
156 qp.z += 0.5f * dt * (omega.z * q.w + omega.x * q.y - omega.y * q.x);
157 qp.w += 0.5f * dt * (-omega.x * q.x - omega.y * q.y - omega.z * q.z);
158 const float len = Kokkos::sqrt(qp.x * qp.x + qp.y * qp.y + qp.z * qp.z + qp.w * qp.w);
159 if (len > 1e-9f) {
160 const float s = 1.0f / len;
161 qp = F4{qp.x * s, qp.y * s, qp.z * s, qp.w * s};
162 }
163 }
164 detail::st4(quatPred, i, qp);
165 });
166 space.fence();
167}
168
170inline void computeContactCounts(Kokkos::View<const ContactC*, CpMem> contacts, int numContacts,
171 Vi constraintCounts) {
172 CpExec space;
173 Kokkos::parallel_for(
174 "peclet::dem::contact_counts", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
175 KOKKOS_LAMBDA(int i) {
176 const ContactC c = contacts(i);
177 if (c.dist > 0.0f)
178 return;
179 Kokkos::atomic_add(&constraintCounts(c.bodyA), 1);
180 if (c.bodyB >= 0)
181 Kokkos::atomic_add(&constraintCounts(c.bodyB), 1);
182 });
183 space.fence();
184}
185
191inline void applyUpdates(int n, const BodyPred& pred, const BodyDelta& delta,
192 bool averaged = true) {
193 const V3 posPred = pred.posPred, velPred = pred.velPred;
194 const V3 deltaPos = delta.deltaPos, deltaVel = delta.deltaVel;
195 const Vi constraintCounts = delta.constraintCounts;
196 CpExec space;
197 Kokkos::parallel_for(
198 "peclet::dem::apply_updates", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
199 const int count = constraintCounts(i);
200 if (count <= 0)
201 return;
202 const float f = averaged ? 1.0f / static_cast<float>(count) : 1.0f;
203 detail::st3(posPred, i, add3(ldF3(posPred, i), scale3(ldF3(deltaPos, i), f)));
204 detail::st3(velPred, i, add3(ldF3(velPred, i), scale3(ldF3(deltaVel, i), f)));
205 detail::st3(deltaPos, i, F3{0, 0, 0});
206 detail::st3(deltaVel, i, F3{0, 0, 0});
207 constraintCounts(i) = 0;
208 });
209 space.fence();
210}
211
214inline void finalCommit(int n, const BodyState& bodies, const BodyPred& pred, Domain dom) {
215 const V3 pos = bodies.pos, posPred = pred.posPred;
216 const Vf invMass = bodies.invMass;
217 const V4 quat = bodies.quat, quatPred = pred.quatPred;
218 CpExec space;
219 Kokkos::parallel_for(
220 "peclet::dem::final_commit", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(int i) {
221 F3 p = ldF3(posPred, i);
222 if (dom.periodic_x) {
223 if (p.x < dom.min.x)
224 p.x += dom.size.x;
225 else if (p.x >= dom.max.x)
226 p.x -= dom.size.x;
227 }
228 if (dom.periodic_y) {
229 if (p.y < dom.min.y)
230 p.y += dom.size.y;
231 else if (p.y >= dom.max.y)
232 p.y -= dom.size.y;
233 }
234 if (dom.periodic_z) {
235 if (p.z < dom.min.z)
236 p.z += dom.size.z;
237 else if (p.z >= dom.max.z)
238 p.z -= dom.size.z;
239 }
240 detail::st3(pos, i, p);
241 detail::st4(quat, i, ldF4(quatPred, i));
242 });
243 space.fence();
244}
245
250struct KE2 {
251 double t = 0.0, r = 0.0;
252 KOKKOS_INLINE_FUNCTION KE2& operator+=(const KE2& o) {
253 t += o.t;
254 r += o.r;
255 return *this;
256 }
257};
258
259inline void applyThermostat(int numReal, const BodyState& bodies, double kB, double tau,
260 double Ttarget, float dt) {
261 const V3 vel = bodies.vel, angVel = bodies.angVel, invInertia = bodies.invInertia;
262 const Vf invMass = bodies.invMass;
263 const V4 quat = bodies.quat;
264 CpExec space;
265 KE2 sum;
266 Kokkos::parallel_reduce(
267 "peclet::dem::thermo_energy", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
268 KOKKOS_LAMBDA(int i, KE2& acc) {
269 const float invM = invMass(i);
270 const F3 v = ldF3(vel, i);
271 if (invM > 0.0f)
272 acc.t += 0.5 * (1.0 / invM) * (v.x * v.x + v.y * v.y + v.z * v.z);
273 const F3 invI = ldF3(invInertia, i);
274 if (invI.x > 0.0f || invI.y > 0.0f || invI.z > 0.0f) {
275 const F3 wb = invRotateVector(ldF4(quat, i), ldF3(angVel, i));
276 const double Ix = (invI.x > 0.0f) ? 1.0 / invI.x : 0.0;
277 const double Iy = (invI.y > 0.0f) ? 1.0 / invI.y : 0.0;
278 const double Iz = (invI.z > 0.0f) ? 1.0 / invI.z : 0.0;
279 acc.r += 0.5 * (Ix * wb.x * wb.x + Iy * wb.y * wb.y + Iz * wb.z * wb.z);
280 }
281 },
282 sum);
283
284 const double ndof = 3.0 * numReal;
285 auto lambda = [&](double ke) {
286 if (ndof <= 0 || kB <= 0)
287 return 1.0f;
288 const double Tcur = (2.0 * ke) / (ndof * kB);
289 if (Tcur < 1e-6)
290 return 1.0f;
291 double f = 1.0 + (dt / tau) * (Ttarget / Tcur - 1.0);
292 if (f < 0.0)
293 f = 0.0;
294 return static_cast<float>(Kokkos::sqrt(f));
295 };
296 const float lt = lambda(sum.t), lr = lambda(sum.r);
297
298 Kokkos::parallel_for(
299 "peclet::dem::thermo_scale", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
300 KOKKOS_LAMBDA(int i) {
301 detail::st3(vel, i, scale3(ldF3(vel, i), lt));
302 detail::st3(angVel, i, scale3(ldF3(angVel, i), lr));
303 });
304 space.fence();
305}
306
308inline void updateGrowthScales(int n, Vf scale, Vf targetScale, float factor) {
309 if (!(factor > 0.0f))
310 return;
311 CpExec space;
312 Kokkos::parallel_for(
313 "peclet::dem::growth", Kokkos::RangePolicy<CpExec>(space, 0, n),
314 KOKKOS_LAMBDA(int i) { scale(i) = targetScale(i) * factor; });
315 space.fence();
316}
317
318} // namespace peclet::dem
319
320#endif // DEM_INTEGRATION_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 st3(const V3 &v, int i, F3 a)
void st4(const V4 &v, int i, F4 a)
void applyThermostat(int numReal, const BodyState &bodies, double kB, double tau, double Ttarget, float dt)
Reads bodies (vel, invMass, angVel, invInertia, quat); rescales vel and angVel in place.
Kokkos::View< float *, CpMem > Vf
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 updateGrowthScales(int n, Vf scale, Vf targetScale, float factor)
Growth mode: scale = target * factor (when active).
F3 cross3v(F3 a, F3 b)
Kokkos::View< int *, CpMem > Vi
void applyVelocityAndPredictPosition(int n, const BodyState &bodies, const BodyPred &pred, float dt)
Re-integration: persist solved velocity, trapezoidal position predict, quaternion integrate.
F3 invRotateVector(F4 q, F3 v)
void finalCommit(int n, const BodyState &bodies, const BodyPred &pred, Domain dom)
Final commit: periodic wrap of the predicted position into the domain, commit position + quat.
F3 ldF3(const V &v, int i)
F3 rotateVector(F4 q, F3 v)
Kokkos::DefaultExecutionSpace CpExec
Kokkos::View< float *[3], CpMem > V3
F4 ldF4(const V &v, int i)
void predictVelocity(int n, const BodyState &bodies, const BodyPred &pred, const BodyDelta &delta, F3 gravity, float dt, V3 extForce, V3 extTorque)
Predict velocity (gravity + gyroscopic precession), speculative position, and clear all deltas.
F3 scale3(F3 a, float s)
Kokkos::View< float *[4], CpMem > V4
F3 add3(F3 a, F3 b)
void computeContactCounts(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, Vi constraintCounts)
Per-contact constraint-count pre-pass (active contacts, dist <= 0).
The solve's per-body accumulators: position/orientation and velocity corrections and the per-body con...
Kokkos::View< float *[4], CpMem > deltaQuat
Kokkos::View< float *[3], CpMem > deltaVel
Kokkos::View< float *[3], CpMem > deltaPos
Kokkos::View< float *[3], CpMem > deltaAngVel
Kokkos::View< int *, CpMem > constraintCounts
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< float *[3], CpMem > posPred
Kokkos::View< float *[4], CpMem > quatPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > pos
Kokkos::View< float *[3], CpMem > invInertia
Kokkos::View< float *, CpMem > invMass
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).
Berendsen thermostat: reduce translational + rotational KE over real particles, compute the two scali...
KE2 & operator+=(const KE2 &o)