peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
solver_friction.hpp
Go to the documentation of this file.
1
15#ifndef DEM_SOLVER_FRICTION_HPP
16#define DEM_SOLVER_FRICTION_HPP
17
18#include <Kokkos_Core.hpp>
19
20#include "dem_portable.hpp"
21#include "solver_position.hpp" // ContactC, detail::computeW, CpExec/CpMem
22#include "solver_velocity.hpp" // detail::genInvMass (the world-frame inverse inertia)
23
24namespace peclet::dem {
25
26using FrManifoldCounts = Kokkos::View<float* [2], CpMem>; // per body: .x = plane load, .y = count
27
33KOKKOS_INLINE_FUNCTION void contactArmsMid(const ContactC& c, F3& rA, F3& rB) {
34 const F4 shift{c.normal.x * c.dist * 0.5f, c.normal.y * c.dist * 0.5f, c.normal.z * c.dist * 0.5f,
35 0.0f};
36 rA = F3{c.rA.x - shift.x, c.rA.y - shift.y, c.rA.z - shift.z};
37 rB = F3{c.rB.x + shift.x, c.rB.y + shift.y, c.rB.z + shift.z};
38}
39
45inline void accumulateNormalImpulse(Kokkos::View<ContactC*, CpMem> contacts, int numContacts,
46 const BodyState& bodies, const BodyPred& pred,
47 Kokkos::View<const int*, CpMem> realIdx, float growthRate,
48 bool dedupTwins = true) {
49 const Kokkos::View<const float*, CpMem> invMass = bodies.invMass;
50 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.invInertia;
51 const Kokkos::View<const float* [3], CpMem> velPred = pred.velPred;
52 const Kokkos::View<const float* [3], CpMem> angVelPred = pred.angVelPred;
53 using detail::computeW;
54 CpExec space;
55 Kokkos::parallel_for(
56 "peclet::dem::fric_accum", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
57 KOKKOS_LAMBDA(int idx) {
58 ContactC c = contacts(idx);
59 if (c.bodyB < 0) {
60 // Wall (boundary): accumulate the transmitted normal load on the owned grain (immovable
61 // wall -> A term only), relative to the wall's surface velocity. Indexed like
62 // computePlaneLoad (c.bodyA directly reads the ghost-augmented velPred slot).
63 const int idA = c.bodyA;
64 const float invMA = invMass(idA);
65 if (invMA <= 0.0f)
66 return;
67 const F3 invIA = ldF3(invInertia, idA);
68 const F3 rA{c.rA.x, c.rA.y, c.rA.z}, n{c.normal.x, c.normal.y, c.normal.z};
69 const F3 vAc = add3(ldF3(velPred, idA), cross3v(ldF3(angVelPred, idA), rA));
70 const F3 vWall{c.boundaryVel.x, c.boundaryVel.y, c.boundaryVel.z};
71 const float approach = -dot3(sub3(vAc, vWall), n);
72 if (approach <= 0.0f)
73 return;
74 const float w_n = computeW(rA, n, invMA, invIA);
75 if (w_n > 1e-6f)
76 contacts(idx).friction_lambda_n += approach / w_n;
77 return;
78 }
79 const int realA = realIdx(c.bodyA), realB = realIdx(c.bodyB);
80 if (dedupTwins && realA > realB)
81 return; // periodic dedup (single rank; §4.2 item 2)
82 const float invMA = invMass(realA), invMB = invMass(realB);
83 const F3 invIA = ldF3(invInertia, realA), invIB = ldF3(invInertia, realB);
84 F3 rA, rB;
85 contactArmsMid(c, rA, rB); // single application point
86 const F3 n{c.normal.x, c.normal.y, c.normal.z};
87 const F3 vAc = add3(ldF3(velPred, realA), cross3v(ldF3(angVelPred, realA), rA));
88 const F3 vBc = add3(ldF3(velPred, realB), cross3v(ldF3(angVelPred, realB), rB));
89 const F3 vrel = add3(sub3(vAc, vBc), scale3(sub3(rA, rB), growthRate));
90 const float approach = -dot3(vrel, n);
91 if (approach <= 0.0f)
92 return;
93 const float w_n = computeW(rA, n, invMA, invIA) + computeW(rB, n, invMB, invIB);
94 if (w_n > 1e-6f)
95 contacts(idx).friction_lambda_n += approach / w_n;
96 });
97 space.fence();
98}
99
102inline void computePlaneLoad(Kokkos::View<ContactC*, CpMem> contacts, int numContacts,
103 const BodyState& bodies, const BodyPred& pred,
104 FrManifoldCounts planeFriction) {
105 const Kokkos::View<const float*, CpMem> invMass = bodies.invMass;
106 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.invInertia;
107 const Kokkos::View<const float* [3], CpMem> velPred = pred.velPred;
108 const Kokkos::View<const float* [3], CpMem> angVelPred = pred.angVelPred;
109 using detail::computeW;
110 CpExec space;
111 Kokkos::deep_copy(space, planeFriction, 0.0f);
112 Kokkos::parallel_for(
113 "peclet::dem::fric_plane_load", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
114 KOKKOS_LAMBDA(int idx) {
115 ContactC c = contacts(idx);
116 if (c.bodyB >= 0)
117 return;
118 const int idA = c.bodyA;
119 const float invMA = invMass(idA);
120 if (invMA <= 0.0f)
121 return;
122 const F3 invIA = ldF3(invInertia, idA);
123 const F3 rA{c.rA.x, c.rA.y, c.rA.z}, n{c.normal.x, c.normal.y, c.normal.z};
124 const F3 vAc = add3(ldF3(velPred, idA), cross3v(ldF3(angVelPred, idA), rA));
125 // Relative to the wall's surface velocity (0 for a static plane), so a wall pressing into a
126 // grain along its normal (a vibrating tray) registers the normal load that bounds friction.
127 const F3 vWall{c.boundaryVel.x, c.boundaryVel.y, c.boundaryVel.z};
128 const float approach = -dot3(sub3(vAc, vWall), n);
129 if (approach <= 0.0f)
130 return;
131 const float w_n = computeW(rA, n, invMA, invIA);
132 contacts(idx).friction_lambda_n = (w_n > 1e-6f) ? approach / w_n : 0.0f;
133 Kokkos::atomic_max(&planeFriction(idA, 0), approach / invMA);
134 });
135 space.fence();
136}
137
139inline void countFrictionContacts(Kokkos::View<const ContactC*, CpMem> contacts, int numContacts,
140 Kokkos::View<const int*, CpMem> realIdx,
141 FrManifoldCounts planeFriction) {
142 CpExec space;
143 Kokkos::parallel_for(
144 "peclet::dem::fric_count", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
145 KOKKOS_LAMBDA(int idx) {
146 const ContactC c = contacts(idx);
147 if (c.friction_lambda_n <= 0.0f)
148 return;
149 Kokkos::atomic_add(&planeFriction(realIdx(c.bodyA), 1), 1.0f);
150 if (c.bodyB >= 0)
151 Kokkos::atomic_add(&planeFriction(realIdx(c.bodyB), 1), 1.0f);
152 });
153 space.fence();
154}
155
170inline void solveContactFriction(Kokkos::View<const ContactC*, CpMem> contacts, int numContacts,
171 const BodyState& bodies, const BodyPred& pred,
172 Kokkos::View<const int*, CpMem> realIdx,
173 FrManifoldCounts planeFriction, float frictionDynamic,
174 const BodyDelta& delta) {
175 const Kokkos::View<const float*, CpMem> invMass = bodies.invMass;
176 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.invInertia;
177 const Kokkos::View<const float* [4], CpMem> quat = bodies.quat;
178 const Kokkos::View<const float* [3], CpMem> velPred = pred.velPred;
179 const Kokkos::View<const float* [3], CpMem> angVelPred = pred.angVelPred;
180 const Kokkos::View<float* [3], CpMem> deltaVel = delta.deltaVel;
181 const Kokkos::View<float* [3], CpMem> deltaAngVel = delta.deltaAngVel;
182 CpExec space;
183 Kokkos::deep_copy(space, deltaVel, 0.0f);
184 Kokkos::deep_copy(space, deltaAngVel, 0.0f);
185 Kokkos::parallel_for(
186 "peclet::dem::fric_solve", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
187 KOKKOS_LAMBDA(int idx) {
188 const ContactC c = contacts(idx);
189 const float lambda_n = c.friction_lambda_n;
190 if (lambda_n <= 0.0f)
191 return;
192 const int idA = c.bodyA, idB = c.bodyB;
193 const int realA = realIdx(idA);
194 const int realB = (idB >= 0) ? realIdx(idB) : -1;
195 const float invMA = invMass(realA);
196 const float invMB = (idB >= 0) ? invMass(realB) : 0.0f;
197 const F3 invIA = ldF3(invInertia, realA);
198 const F3 invIB = (idB >= 0) ? ldF3(invInertia, realB) : F3{0, 0, 0};
199 F3 rA{c.rA.x, c.rA.y, c.rA.z}, rB{c.rB.x, c.rB.y, c.rB.z};
200 if (idB >= 0)
201 contactArmsMid(c, rA, rB); // single application point; a wall keeps its arm
202 const F3 n{c.normal.x, c.normal.y, c.normal.z};
203
204 const F3 vAc = add3(ldF3(velPred, realA), cross3v(ldF3(angVelPred, realA), rA));
205 F3 vBc{0, 0, 0};
206 if (idB >= 0)
207 vBc = add3(ldF3(velPred, realB), cross3v(ldF3(angVelPred, realB), rB));
208 else
209 vBc =
210 F3{c.boundaryVel.x, c.boundaryVel.y, c.boundaryVel.z}; // moving wall drags the grain
211 const F3 vrel = sub3(vAc, vBc);
212 const float vn = dot3(vrel, n);
213 const F3 vt = sub3(vrel, scale3(n, vn));
214 const float vt_len = Kokkos::sqrt(dot3(vt, vt));
215 if (vt_len < 1e-7f)
216 return;
217 const F3 t = scale3(vt, 1.0f / vt_len);
218
219 const F3 rnA = cross3v(rA, t), rnB = cross3v(rB, t);
220 // Isotropic inverse inertia at an end (spheres; a wall end is 0): no principal frame.
221 const bool isoA = invIA.x == invIA.y && invIA.y == invIA.z;
222 const bool isoB = invIB.x == invIB.y && invIB.y == invIB.z;
223 F4 qA{0, 0, 0, 1}, qB{0, 0, 0, 1};
224 if (!isoA)
225 qA = F4{quat(realA, 0), quat(realA, 1), quat(realA, 2), quat(realA, 3)};
226 if (!isoB)
227 qB = F4{quat(realB, 0), quat(realB, 1), quat(realB, 2), quat(realB, 3)};
228 const float w_t = (isoA && isoB)
229 ? invMA + invMB + rnA.x * rnA.x * invIA.x + rnA.y * rnA.y * invIA.y +
230 rnA.z * rnA.z * invIA.z + rnB.x * rnB.x * invIB.x +
231 rnB.y * rnB.y * invIB.y + rnB.z * rnB.z * invIB.z
232 : invMA + invMB + detail::genInvMass(rnA, invIA, qA) +
233 detail::genInvMass(rnB, invIB, qB);
234 if (w_t < 1e-6f)
235 return;
236
237 // friction_lambda_n now carries the accumulated force-chain load for BOTH body-body and
238 // wall contacts (see accumulateNormalImpulse), so it is the Coulomb bound in both
239 // cases.
240 const float bound = lambda_n;
241 const float nA = planeFriction(realA, 1);
242 const float nB = (idB >= 0) ? planeFriction(realB, 1) : 0.0f;
243 const float inv_n = 1.0f / Kokkos::fmax(Kokkos::fmax(nA, nB), 1.0f);
244 float lt = -vt_len / w_t;
245 // Per-wall AND per-pair friction (a < 0 sentinel keeps the global material — planes and
246 // body-body contacts without a pair table).
247 const float mu = (c.boundaryFriction >= 0.0f) ? c.boundaryFriction : frictionDynamic;
248 const float maxf = mu * bound;
249 if (lt < -maxf)
250 lt = -maxf;
251 lt *= inv_n;
252
253 Kokkos::atomic_add(&deltaVel(realA, 0), t.x * lt * invMA);
254 Kokkos::atomic_add(&deltaVel(realA, 1), t.y * lt * invMA);
255 Kokkos::atomic_add(&deltaVel(realA, 2), t.z * lt * invMA);
256 if (isoA) {
257 Kokkos::atomic_add(&deltaAngVel(realA, 0), rnA.x * invIA.x * lt);
258 Kokkos::atomic_add(&deltaAngVel(realA, 1), rnA.y * invIA.y * lt);
259 Kokkos::atomic_add(&deltaAngVel(realA, 2), rnA.z * invIA.z * lt);
260 } else { // I_world^-1 (r x t) lt = R diag(invI) R^T (r x t) lt
261 const F3 Jl = invRotateVector(qA, scale3(rnA, lt));
262 const F3 dww = rotateVector(qA, F3{Jl.x * invIA.x, Jl.y * invIA.y, Jl.z * invIA.z});
263 Kokkos::atomic_add(&deltaAngVel(realA, 0), dww.x);
264 Kokkos::atomic_add(&deltaAngVel(realA, 1), dww.y);
265 Kokkos::atomic_add(&deltaAngVel(realA, 2), dww.z);
266 }
267 if (idB >= 0) {
268 Kokkos::atomic_add(&deltaVel(realB, 0), -t.x * lt * invMB);
269 Kokkos::atomic_add(&deltaVel(realB, 1), -t.y * lt * invMB);
270 Kokkos::atomic_add(&deltaVel(realB, 2), -t.z * lt * invMB);
271 if (isoB) {
272 Kokkos::atomic_add(&deltaAngVel(realB, 0), -rnB.x * invIB.x * lt);
273 Kokkos::atomic_add(&deltaAngVel(realB, 1), -rnB.y * invIB.y * lt);
274 Kokkos::atomic_add(&deltaAngVel(realB, 2), -rnB.z * invIB.z * lt);
275 } else {
276 const F3 Jl = invRotateVector(qB, scale3(rnB, -lt));
277 const F3 dww = rotateVector(qB, F3{Jl.x * invIB.x, Jl.y * invIB.y, Jl.z * invIB.z});
278 Kokkos::atomic_add(&deltaAngVel(realB, 0), dww.x);
279 Kokkos::atomic_add(&deltaAngVel(realB, 1), dww.y);
280 Kokkos::atomic_add(&deltaAngVel(realB, 2), dww.z);
281 }
282 }
283 });
284 space.fence();
285}
286
287} // namespace peclet::dem
288
289#endif // DEM_SOLVER_FRICTION_HPP
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
float genInvMass(F3 tau, F3 invIlocal, F4 q)
float computeW(F3 r, F3 dir, float invM, F3 invI)
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.
F3 cross3v(F3 a, F3 b)
void contactArmsMid(const ContactC &c, F3 &rA, F3 &rB)
The single application point of a body-body contact: both lever arms moved to the contact midpoint,...
F3 invRotateVector(F4 q, F3 v)
F3 ldF3(const V &v, int i)
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).
F3 rotateVector(F4 q, F3 v)
float dot3(F3 a, F3 b)
F3 sub3(F3 a, F3 b)
void computePlaneLoad(Kokkos::View< ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, FrManifoldCounts planeFriction)
Plane (idB<0) one-shot loads.
Kokkos::DefaultExecutionSpace CpExec
F3 scale3(F3 a, float s)
F3 add3(F3 a, F3 b)
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 +=...
Kokkos::View< float *[2], CpMem > FrManifoldCounts
dem — portable (Kokkos) XPBD position solve (pure overlap removal).
dem — portable (Kokkos) manifold velocity solve (normal restitution impulse).
The solve's per-body accumulators: position/orientation and velocity corrections and the per-body con...
Kokkos::View< float *[3], CpMem > deltaVel
Kokkos::View< float *[3], CpMem > deltaAngVel
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > invInertia
Kokkos::View< float *, CpMem > invMass
Kokkos::View< float *[4], CpMem > quat
Portable mirror of ParticleSystem.cuh ContactConstraint (the fields this reduction touches).