peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
solver_hertz.hpp
Go to the documentation of this file.
1#pragma once
15
16#include <Kokkos_Core.hpp>
17#include <Kokkos_Sort.hpp>
18
20#include "narrowphase.hpp"
21#include "particles.hpp"
22
23namespace peclet::dem {
24
27KOKKOS_INLINE_FUNCTION void hertzPairEMu(int matA, int matB, PairTableView pairTable, float eGlobal,
28 float muGlobal, float& e, float& mu) {
29 if (pairTable.extent(0) > 0) {
30 const int t = (matA * kMaxMaterials + matB) * 2;
31 e = pairTable(t);
32 mu = pairTable(t + 1);
33 } else {
34 e = eGlobal;
35 mu = muGlobal;
36 }
37}
38
45 float eGlobal;
46 float muGlobal;
47 Kokkos::View<const float*, CpMem> hertzE;
48 Kokkos::View<const float*, CpMem> hertzNu;
49};
50
53 Kokkos::View<const float* [3], CpMem> pos;
54 Kokkos::View<const float* [3], CpMem> vel;
55 Kokkos::View<const float* [3], CpMem> angVel;
56 Kokkos::View<const float*, CpMem> rad;
57 Kokkos::View<const float*, CpMem> invMass;
58};
59
64 Kokkos::View<const float* [4], CpMem> quat;
65 Kokkos::View<const float*, CpMem> scale;
67 Kokkos::View<const ShapeDesc*, CpMem> shapes;
69 float globalScale = 1.0f;
70 float contactRadiusFrac = 0.5f;
71 bool hasShapes = false;
72};
73
77 Kokkos::View<const WallSdf*, CpMem> walls;
79 Kokkos::View<float* [3], CpMem> xiWall;
81 Kokkos::View<float*, CpMem> snWall;
82};
83
86KOKKOS_INLINE_FUNCTION float hertzBetaD(float e) {
87 if (e <= 0.0f)
88 return -1.0f;
89 if (e >= 1.0f)
90 return 0.0f;
91 const float le = Kokkos::log(e);
92 return le / Kokkos::sqrt(le * le + 9.8696044f);
93}
94
98KOKKOS_INLINE_FUNCTION F3 hertzForce(float delta, F3 nhat, F3 vrel, float rStar, float mStar,
99 float eStar, float gStar, float e, float mu, float dt,
100 F3& xi) {
101 const float sq = Kokkos::sqrt(rStar * delta);
102 const float sn = 2.0f * eStar * sq;
103 const float st = 8.0f * gStar * sq;
104 const float kn = (4.0f / 3.0f) * eStar * sq; // Fn_spring = kn * delta
105 const float bd = hertzBetaD(e);
106 const float etaN = -2.0f * 0.9128709f * bd * Kokkos::sqrt(sn * mStar); // sqrt(5/6)
107 const float etaT = -2.0f * 0.9128709f * bd * Kokkos::sqrt(st * mStar);
108
109 const float vn = dot3(vrel, nhat); // > 0: separating (A moving away from B)
110 const float fnSpring = kn * delta;
111 const float fn = fnSpring - etaN * vn; // + etaN * ddelta/dt: damping opposes approach
112 const F3 vt = sub3(vrel, scale3(nhat, vn));
113
114 // Mindlin history spring: accumulate tangential displacement, keep it in the tangent plane.
115 xi = add3(xi, scale3(vt, dt));
116 xi = sub3(xi, scale3(nhat, dot3(xi, nhat)));
117 F3 ft = add3(scale3(xi, -st), scale3(vt, -etaT));
118 const float ftLen = Kokkos::sqrt(dot3(ft, ft));
119 const float ftMax = mu * Kokkos::fabs(fnSpring);
120 if (ftLen > ftMax) {
121 // sliding: clamp AND rescale the history so the spring alone carries the clamped force
122 // (the LIGGGHTS shear-history rescale).
123 const float s = (ftLen > 1e-20f) ? ftMax / ftLen : 0.0f;
124 ft = scale3(ft, s);
125 if (st > 1e-20f)
126 xi = scale3(add3(ft, scale3(vt, etaT)), -1.0f / st);
127 }
128 return add3(scale3(nhat, fn), ft);
129}
130
136KOKKOS_INLINE_FUNCTION float hertzSphereRadius(int i, Kokkos::View<const float*, CpMem> rad,
137 Kokkos::View<const float*, CpMem> scale,
138 ScalarI shapeId,
139 Kokkos::View<const ShapeDesc*, CpMem> shapes,
140 float globalScale) {
141 if (shapes.extent(0) > 0 && scale.extent(0) > 0) {
142 const ShapeDesc d = shapes(shapeId(i));
143 if (d.type == SPHERE && d.params.x > 0.0f)
144 return scale(i) * globalScale * d.params.x;
145 }
146 return rad(i);
147}
148
153inline void hertzPairForces(Kokkos::View<const int* [2], CpMem> pairs, int numPairs,
154 const HertzBodies& bodies, const HertzMaterials& mat, float dt,
155 Kokkos::View<float* [3], CpMem> xi,
156 Kokkos::View<float* [3], CpMem> force,
157 Kokkos::View<float* [3], CpMem> torque, const HertzShapes& shp = {}) {
158 const Kokkos::View<const float* [3], CpMem> pos = bodies.pos, vel = bodies.vel,
159 angVel = bodies.angVel;
160 const Kokkos::View<const float*, CpMem> rad = bodies.rad, invMass = bodies.invMass;
161 const Kokkos::View<const float*, CpMem> scale = shp.scale;
162 const ScalarI shapeId = shp.shapeId;
163 const Kokkos::View<const ShapeDesc*, CpMem> shapes = shp.shapes;
164 const float globalScale = shp.globalScale;
165 const MatIdView matId = mat.matId;
166 const PairTableView pairTable = mat.pairTable;
167 const float eGlobal = mat.eGlobal, muGlobal = mat.muGlobal;
168 const Kokkos::View<const float*, CpMem> hertzE = mat.hertzE, hertzNu = mat.hertzNu;
169 CpExec space;
170 Kokkos::parallel_for(
171 "peclet::dem::hertz_pairs", Kokkos::RangePolicy<CpExec>(space, 0, numPairs),
172 KOKKOS_LAMBDA(int idx) {
173 const int i = pairs(idx, 0), j = pairs(idx, 1);
174 const F3 pi = loadF3(pos, i), pj = loadF3(pos, j);
175 const F3 dx = sub3(pi, pj);
176 const float d = Kokkos::sqrt(dot3(dx, dx));
177 const float ri = hertzSphereRadius(i, rad, scale, shapeId, shapes, globalScale),
178 rj = hertzSphereRadius(j, rad, scale, shapeId, shapes, globalScale);
179 const float delta = ri + rj - d;
180 if (delta <= 0.0f || d < 1e-12f) {
181 xi(idx, 0) = xi(idx, 1) = xi(idx, 2) = 0.0f; // contact open: history resets
182 return;
183 }
184 const F3 nhat = scale3(dx, 1.0f / d); // B(j) -> A(i)
185 const F3 rA = scale3(nhat, -(ri - 0.5f * delta));
186 const F3 rB = scale3(nhat, (rj - 0.5f * delta));
187 const F3 vrel = sub3(add3(loadF3(vel, i), cross3v(loadF3(angVel, i), rA)),
188 add3(loadF3(vel, j), cross3v(loadF3(angVel, j), rB)));
189 const int ma = matId(i), mb = matId(j);
190 float e, mu;
191 hertzPairEMu(ma, mb, pairTable, eGlobal, muGlobal, e, mu);
192 const float Ei = hertzE(ma), Ej = hertzE(mb);
193 const float ni = hertzNu(ma), nj = hertzNu(mb);
194 const float eStar = 1.0f / ((1.0f - ni * ni) / Ei + (1.0f - nj * nj) / Ej);
195 const float gStar =
196 1.0f / (2.0f * (2.0f - ni) * (1.0f + ni) / Ei + 2.0f * (2.0f - nj) * (1.0f + nj) / Ej);
197 const float rStar = ri * rj / (ri + rj);
198 const float imSum = invMass(i) + invMass(j);
199 const float mStar = (imSum > 0.0f) ? 1.0f / imSum : 0.0f;
200 F3 x{xi(idx, 0), xi(idx, 1), xi(idx, 2)};
201 const F3 f = hertzForce(delta, nhat, vrel, rStar, mStar, eStar, gStar, e, mu, dt, x);
202 xi(idx, 0) = x.x;
203 xi(idx, 1) = x.y;
204 xi(idx, 2) = x.z;
205 const F3 tA = cross3v(rA, f), tB = cross3v(rB, scale3(f, -1.0f));
206 for (int c = 0; c < 3; ++c) {
207 Kokkos::atomic_add(&force(i, c), (&f.x)[c]);
208 Kokkos::atomic_add(&force(j, c), -(&f.x)[c]);
209 Kokkos::atomic_add(&torque(i, c), (&tA.x)[c]);
210 Kokkos::atomic_add(&torque(j, c), (&tB.x)[c]);
211 }
212 });
213 space.fence();
214}
215
218KOKKOS_FORCEINLINE_FUNCTION void hertzWallEMu(int ma, const WallSdf& w, PairTableView pairTable,
219 float eGlobal, float muGlobal, float& e, float& mu) {
220 if (w.materialId >= 0 && pairTable.extent(0) > 0) {
221 hertzPairEMu(ma, w.materialId, pairTable, eGlobal, muGlobal, e, mu);
222 } else {
223 e = w.restitution;
224 mu = w.friction;
225 }
226}
227
229KOKKOS_FORCEINLINE_FUNCTION F3 wallSdfGradient(F3 q, const WallSdf& w, GridView wallGrid) {
230 const float hx = 0.5f / w.grid.invSpacing.x;
231 return F3{(sampleWallSdf(F3{q.x + hx, q.y, q.z}, w, wallGrid) -
232 sampleWallSdf(F3{q.x - hx, q.y, q.z}, w, wallGrid)),
233 (sampleWallSdf(F3{q.x, q.y + hx, q.z}, w, wallGrid) -
234 sampleWallSdf(F3{q.x, q.y - hx, q.z}, w, wallGrid)),
235 (sampleWallSdf(F3{q.x, q.y, q.z + hx}, w, wallGrid) -
236 sampleWallSdf(F3{q.x, q.y, q.z - hx}, w, wallGrid))};
237}
238
241inline void hertzWallForces(Kokkos::View<const int*, CpMem> candSlots, int numCand,
242 const HertzWallState& wall, const HertzBodies& bodies,
243 const HertzMaterials& mat, float dt,
244 Kokkos::View<float* [3], CpMem> force,
245 Kokkos::View<float* [3], CpMem> torque, const HertzShapes& shp = {}) {
246 const Kokkos::View<const WallSdf*, CpMem> walls = wall.walls;
247 const GridView wallGrid = wall.wallGrid;
248 const Kokkos::View<float* [3], CpMem> xiWall = wall.xiWall;
249 const int maxWalls = wall.maxWalls;
250 const Kokkos::View<float*, CpMem> snWall = wall.snWall;
251 const Kokkos::View<const float* [3], CpMem> pos = bodies.pos, vel = bodies.vel,
252 angVel = bodies.angVel;
253 const Kokkos::View<const float*, CpMem> rad = bodies.rad, invMass = bodies.invMass;
254 const Kokkos::View<const float* [4], CpMem> quat = shp.quat;
255 const Kokkos::View<const float*, CpMem> scale = shp.scale;
256 const ScalarI shapeId = shp.shapeId;
257 const Kokkos::View<const ShapeDesc*, CpMem> shapes = shp.shapes;
258 const ShellView shell = shp.shell;
259 const float globalScale = shp.globalScale, contactRadiusFrac = shp.contactRadiusFrac;
260 const bool hasShapes = shp.hasShapes;
261 const MatIdView matId = mat.matId;
262 const PairTableView pairTable = mat.pairTable;
263 const float eGlobal = mat.eGlobal, muGlobal = mat.muGlobal;
264 const Kokkos::View<const float*, CpMem> hertzE = mat.hertzE, hertzNu = mat.hertzNu;
265 CpExec space;
266 Kokkos::parallel_for(
267 "peclet::dem::hertz_walls", Kokkos::RangePolicy<CpExec>(space, 0, numCand),
268 KOKKOS_LAMBDA(int ci) {
269 const int enc = candSlots(ci);
270 const int i = enc / 8;
271 const int wi = enc % 8;
272 const F3 p = loadF3(pos, i);
273 const float r = hertzSphereRadius(i, rad, scale, shapeId, shapes, globalScale);
274 const WallSdf w = walls(wi);
275 const int slot = i * maxWalls + wi;
276 const int ma = matId(i);
277 if (hasShapes && shapes(shapeId(i)).numPoints > 0) {
278 // Non-spherical: per-point Hertz springs of the shell against the wall SDF, one
279 // Mindlin patch history per (particle, wall).
280 const ShapeDesc dS = shapes(shapeId(i));
281 const F4 qP = F4{quat(i, 0), quat(i, 1), quat(i, 2), quat(i, 3)};
282 const float effScale = scale(i) * globalScale;
283 const float rc = contactRadiusFrac * dS.params.x * effScale;
284 float e, mu;
285 hertzWallEMu(ma, w, pairTable, eGlobal, muGlobal, e, mu);
286 const int mw = (w.materialId >= 0) ? w.materialId : ma;
287 const float Ei = hertzE(ma), Ew = hertzE(mw);
288 const float ni = hertzNu(ma), nw = hertzNu(mw);
289 const float eStar = 1.0f / ((1.0f - ni * ni) / Ei + (1.0f - nw * nw) / Ew);
290 const float gStar = 1.0f / (2.0f * (2.0f - ni) * (1.0f + ni) / Ei +
291 2.0f * (2.0f - nw) * (1.0f + nw) / Ew);
292 const float mStar = (invMass(i) > 0.0f) ? 1.0f / invMass(i) : 0.0f;
293 const float bd = hertzBetaD(e);
294 const F3 vP = loadF3(vel, i), wP = loadF3(angVel, i);
295 const float snTotPrev = (snWall.extent(0) > 0) ? snWall(slot) : 0.0f;
296 const float dampC = (mStar > 0.0f) ? -2.0f * 0.9128709f * bd * Kokkos::sqrt(mStar) : 0.0f;
297 float fnSum = 0.0f, snSum = 0.0f, dMax = 0.0f;
298 F3 cSum{0, 0, 0}, nSum{0, 0, 0}, fAcc{0, 0, 0}, tAcc{0, 0, 0};
299 for (int k = 0; k < dS.numPoints; ++k) {
300 const int sIdx = dS.shellOffset + k;
301 const F3 pW = add3(
302 p, rotateVector(
303 qP, scale3(F3{shell(sIdx, 0), shell(sIdx, 1), shell(sIdx, 2)}, effScale)));
304 const float sd = sampleWallSdf(pW, w, wallGrid);
305 if (sd >= 0.0f)
306 continue;
307 const float delta = -sd;
308 F3 g = wallSdfGradient(pW, w, wallGrid);
309 const float gl = len3(g);
310 if (gl < 1e-12f)
311 continue;
312 const F3 nhat = scale3(g, 1.0f / gl);
313 const F3 rA = sub3(pW, p);
314 const F3 vWall = add3(w.linVel, cross3v(w.angVel, sub3(pW, w.center)));
315 const F3 vrelP = sub3(add3(vP, cross3v(wP, rA)), vWall);
316 const float vnP = dot3(vrelP, nhat);
317 const float sq = Kokkos::sqrt(rc * delta);
318 const float fnSpring = (4.0f / 3.0f) * eStar * sq * delta;
319 const float snI = 2.0f * eStar * sq;
320 const float snTot = (snTotPrev > snI) ? snTotPrev : snI;
321 const float etaI = dampC * snI / Kokkos::sqrt(snTot);
322 const F3 f = scale3(nhat, fnSpring - etaI * vnP);
323 fAcc = add3(fAcc, f);
324 tAcc = add3(tAcc, cross3v(rA, f));
325 fnSum += fnSpring;
326 snSum += 2.0f * eStar * sq;
327 cSum = add3(cSum, scale3(pW, fnSpring));
328 nSum = add3(nSum, scale3(nhat, fnSpring));
329 if (delta > dMax)
330 dMax = delta;
331 }
332 if (fnSum <= 0.0f) {
333 xiWall(slot, 0) = xiWall(slot, 1) = xiWall(slot, 2) = 0.0f;
334 if (snWall.extent(0) > 0)
335 snWall(slot) = 0.0f;
336 return;
337 }
338 const F3 cpt = scale3(cSum, 1.0f / fnSum);
339 F3 nhat = nSum;
340 const float nl = len3(nhat);
341 if (nl > 1e-9f)
342 nhat = scale3(nhat, 1.0f / nl);
343 const F3 rA = sub3(cpt, p);
344 const F3 vWall = add3(w.linVel, cross3v(w.angVel, sub3(cpt, w.center)));
345 const F3 vrel = sub3(add3(vP, cross3v(wP, rA)), vWall);
346 if (snWall.extent(0) > 0)
347 snWall(slot) = snSum;
348 const F3 vt = sub3(vrel, scale3(nhat, dot3(vrel, nhat)));
349 const float st = 8.0f * gStar * Kokkos::sqrt(rc * dMax);
350 const float etaT = -2.0f * 0.9128709f * bd * Kokkos::sqrt(st * mStar);
351 F3 x{xiWall(slot, 0), xiWall(slot, 1), xiWall(slot, 2)};
352 x = add3(x, scale3(vt, dt));
353 x = sub3(x, scale3(nhat, dot3(x, nhat)));
354 F3 ft = add3(scale3(x, -st), scale3(vt, -etaT));
355 const float ftLen = len3(ft);
356 const float ftMax = mu * fnSum;
357 if (ftLen > ftMax) {
358 const float sc2 = (ftLen > 1e-20f) ? ftMax / ftLen : 0.0f;
359 ft = scale3(ft, sc2);
360 if (st > 1e-20f)
361 x = scale3(add3(ft, scale3(vt, etaT)), -1.0f / st);
362 }
363 xiWall(slot, 0) = x.x;
364 xiWall(slot, 1) = x.y;
365 xiWall(slot, 2) = x.z;
366 fAcc = add3(fAcc, ft);
367 tAcc = add3(tAcc, cross3v(rA, ft));
368 for (int c = 0; c < 3; ++c) {
369 Kokkos::atomic_add(&force(i, c), (&fAcc.x)[c]);
370 Kokkos::atomic_add(&torque(i, c), (&tAcc.x)[c]);
371 }
372 return;
373 }
374 {
375 const float s = sampleWallSdf(p, w, wallGrid);
376 const float delta = r - s;
377 if (delta <= 0.0f) {
378 xiWall(slot, 0) = xiWall(slot, 1) = xiWall(slot, 2) = 0.0f;
379 return;
380 }
381 F3 g = wallSdfGradient(p, w, wallGrid);
382 const float gl = Kokkos::sqrt(dot3(g, g));
383 if (gl < 1e-12f)
384 return;
385 const F3 nhat = scale3(g, 1.0f / gl); // into the void = push direction on the grain
386 const F3 rA = scale3(nhat, -(r - 0.5f * delta));
387 const F3 cpt = add3(p, rA);
388 const F3 vWall = add3(w.linVel, cross3v(w.angVel, sub3(cpt, w.center)));
389 const F3 vrel = sub3(add3(loadF3(vel, i), cross3v(loadF3(angVel, i), rA)), vWall);
390 float e, mu;
391 hertzWallEMu(ma, w, pairTable, eGlobal, muGlobal, e, mu);
392 const int mw = (w.materialId >= 0) ? w.materialId : ma;
393 const float Ei = hertzE(ma), Ew = hertzE(mw);
394 const float ni = hertzNu(ma), nw = hertzNu(mw);
395 const float eStar = 1.0f / ((1.0f - ni * ni) / Ei + (1.0f - nw * nw) / Ew);
396 const float gStar = 1.0f / (2.0f * (2.0f - ni) * (1.0f + ni) / Ei +
397 2.0f * (2.0f - nw) * (1.0f + nw) / Ew);
398 const float mStar = (invMass(i) > 0.0f) ? 1.0f / invMass(i) : 0.0f;
399 F3 x{xiWall(slot, 0), xiWall(slot, 1), xiWall(slot, 2)};
400 const F3 f = hertzForce(delta, nhat, vrel, r, mStar, eStar, gStar, e, mu, dt, x);
401 xiWall(slot, 0) = x.x;
402 xiWall(slot, 1) = x.y;
403 xiWall(slot, 2) = x.z;
404 const F3 tA = cross3v(rA, f);
405 for (int c = 0; c < 3; ++c) {
406 Kokkos::atomic_add(&force(i, c), (&f.x)[c]);
407 Kokkos::atomic_add(&torque(i, c), (&tA.x)[c]);
408 }
409 }
410 });
411 space.fence();
412}
413
417inline int hertzBuildWallCandidates(int numReal, int numWalls,
418 Kokkos::View<const WallSdf*, CpMem> walls, GridView wallGrid,
419 Kokkos::View<const float* [3], CpMem> pos,
420 Kokkos::View<const float*, CpMem> rad, float skin,
421 Kokkos::View<int*, CpMem> outSlots,
422 Kokkos::View<int, CpMem> outCount) {
423 CpExec space;
424 Kokkos::deep_copy(space, outCount, 0);
425 const int cap = static_cast<int>(outSlots.extent(0));
426 Kokkos::parallel_for(
427 "peclet::dem::hertz_wall_cand", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
428 KOKKOS_LAMBDA(int i) {
429 const F3 p{pos(i, 0), pos(i, 1), pos(i, 2)};
430 for (int wi = 0; wi < numWalls; ++wi) {
431 const float s = sampleWallSdf(p, walls(wi), wallGrid);
432 if (s < rad(i) + skin) {
433 const int at = Kokkos::atomic_fetch_add(&outCount(), 1);
434 if (at < cap)
435 outSlots(at) = i * 8 + wi;
436 }
437 }
438 });
439 space.fence();
440 int n = 0;
441 Kokkos::deep_copy(n, outCount);
442 return n < cap ? n : cap;
443}
444
449inline void hertzIntegrate(int numReal, Kokkos::View<float* [3], CpMem> force,
450 Kokkos::View<float* [3], CpMem> torque, const BodyState& bodies,
451 F3 gravity, float dt, bool integrateOrientation = false) {
452 const Kokkos::View<const float*, CpMem> invMass = bodies.invMass;
453 const Kokkos::View<const float* [3], CpMem> invInertia = bodies.invInertia;
454 const Kokkos::View<float* [3], CpMem> vel = bodies.vel, angVel = bodies.angVel, pos = bodies.pos;
455 const Kokkos::View<float* [4], CpMem> quat = bodies.quat;
456 CpExec space;
457 Kokkos::parallel_for(
458 "peclet::dem::hertz_integrate", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
459 KOKKOS_LAMBDA(int i) {
460 const float im = invMass(i);
461 if (im > 0.0f) {
462 const F3 iI{invInertia(i, 0), invInertia(i, 1), invInertia(i, 2)};
463 F3 dw;
464 if (iI.x == iI.y && iI.y == iI.z) { // isotropic: no frame rotation needed
465 dw = F3{torque(i, 0) * iI.x, torque(i, 1) * iI.x, torque(i, 2) * iI.x};
466 } else { // world torque -> principal frame -> back
467 const F4 q = loadF4(quat, i);
468 const F3 tl = invRotateVector(q, F3{torque(i, 0), torque(i, 1), torque(i, 2)});
469 dw = rotateVector(q, F3{tl.x * iI.x, tl.y * iI.y, tl.z * iI.z});
470 }
471 for (int c = 0; c < 3; ++c) {
472 vel(i, c) += (force(i, c) * im + (&gravity.x)[c]) * dt;
473 angVel(i, c) += (&dw.x)[c] * dt;
474 pos(i, c) += vel(i, c) * dt;
475 }
476 if (integrateOrientation) {
477 F4 q = loadF4(quat, i);
478 const F3 w{angVel(i, 0), angVel(i, 1), angVel(i, 2)};
479 // q += dt/2 * (0, w) x q, renormalized
480 F4 dq{0.5f * dt * (w.x * q.w + w.y * q.z - w.z * q.y),
481 0.5f * dt * (w.y * q.w + w.z * q.x - w.x * q.z),
482 0.5f * dt * (w.z * q.w + w.x * q.y - w.y * q.x),
483 0.5f * dt * (-w.x * q.x - w.y * q.y - w.z * q.z)};
484 q = F4{q.x + dq.x, q.y + dq.y, q.z + dq.z, q.w + dq.w};
485 const float qn = Kokkos::sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
486 if (qn > 1e-12f) {
487 quat(i, 0) = q.x / qn;
488 quat(i, 1) = q.y / qn;
489 quat(i, 2) = q.z / qn;
490 quat(i, 3) = q.w / qn;
491 }
492 }
493 }
494 force(i, 0) = force(i, 1) = force(i, 2) = 0.0f;
495 torque(i, 0) = torque(i, 1) = torque(i, 2) = 0.0f;
496 });
497 space.fence();
498}
499
502inline float hertzMaxDisp2(int numReal, Kokkos::View<const float* [3], CpMem> pos,
503 Kokkos::View<const float* [3], CpMem> refPos) {
504 CpExec space;
505 float m = 0.0f;
506 Kokkos::parallel_reduce(
507 "peclet::dem::hertz_maxdisp", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
508 KOKKOS_LAMBDA(int i, float& acc) {
509 const F3 d = sub3(loadF3(pos, i), loadF3(refPos, i));
510 const float dd = dot3(d, d);
511 if (dd > acc)
512 acc = dd;
513 },
514 Kokkos::Max<float>(m));
515 space.fence();
516 return m;
517}
518
527inline void hertzShapePairForces(Kokkos::View<const int* [2], CpMem> pairs, int numPairs,
528 const HertzBodies& bodies, const HertzShapes& shp,
529 GridView sdfGrid, const HertzMaterials& mat, float dt,
530 Kokkos::View<float* [3], CpMem> xi,
531 Kokkos::View<float*, CpMem> snPrev,
532 Kokkos::View<float* [3], CpMem> force,
533 Kokkos::View<float* [3], CpMem> torque) {
534 const Kokkos::View<const float* [3], CpMem> pos = bodies.pos, vel = bodies.vel,
535 angVel = bodies.angVel;
536 const Kokkos::View<const float*, CpMem> invMass = bodies.invMass;
537 const Kokkos::View<const float* [4], CpMem> quat = shp.quat;
538 const Kokkos::View<const float*, CpMem> scale = shp.scale;
539 const ScalarI shapeId = shp.shapeId;
540 const Kokkos::View<const ShapeDesc*, CpMem> shapes = shp.shapes;
541 const ShellView shell = shp.shell;
542 const float globalScale = shp.globalScale, contactRadiusFrac = shp.contactRadiusFrac;
543 const MatIdView matId = mat.matId;
544 const PairTableView pairTable = mat.pairTable;
545 const float eGlobal = mat.eGlobal, muGlobal = mat.muGlobal;
546 const Kokkos::View<const float*, CpMem> hertzE = mat.hertzE, hertzNu = mat.hertzNu;
547 CpExec space;
548 Kokkos::parallel_for(
549 "peclet::dem::hertz_shape_pairs", Kokkos::RangePolicy<CpExec>(space, 0, numPairs),
550 KOKKOS_LAMBDA(int idx) {
551 const int idA = pairs(idx, 0), idB = pairs(idx, 1);
552 const ShapeDesc dA = shapes(shapeId(idA));
553 const ShapeDesc dB = shapes(shapeId(idB));
554 const F3 posA = loadF3(pos, idA), posB = loadF3(pos, idB);
555 const F4 qA = loadF4(quat, idA), qB = loadF4(quat, idB);
556 const float effScaleA = scale(idA) * globalScale, effScaleB = scale(idB) * globalScale;
557 const int countA = dA.numPoints;
558 const bool sphereA = (dA.type == SPHERE);
559 const int iter = (countA > 0) ? countA : 1;
560
561 const int ma = matId(idA), mb = matId(idB);
562 float e, mu;
563 hertzPairEMu(ma, mb, pairTable, eGlobal, muGlobal, e, mu);
564 const float Ei = hertzE(ma), Ej = hertzE(mb);
565 const float ni = hertzNu(ma), nj = hertzNu(mb);
566 const float eStar = 1.0f / ((1.0f - ni * ni) / Ei + (1.0f - nj * nj) / Ej);
567 const float gStar =
568 1.0f / (2.0f * (2.0f - ni) * (1.0f + ni) / Ei + 2.0f * (2.0f - nj) * (1.0f + nj) / Ej);
569 // contact radii: spheres exact, shapes = frac * bounding radius
570 // params.x = sphere radius or (grid SDF) canonical bounding radius
571 const float rcA = (sphereA ? 1.0f : contactRadiusFrac) * dA.params.x * effScaleA;
572 const float rcB =
573 ((dB.type == SPHERE) ? 1.0f : contactRadiusFrac) * dB.params.x * effScaleB;
574 const float rStar = rcA * rcB / (rcA + rcB);
575 const float imSum = invMass(idA) + invMass(idB);
576 const float mStar = (imSum > 0.0f) ? 1.0f / imSum : 0.0f;
577 const float bd = hertzBetaD(e);
578
579 const F3 vA = loadF3(vel, idA), wA = loadF3(angVel, idA);
580 const F3 vB = loadF3(vel, idB), wB = loadF3(angVel, idB);
581
582 float fnSum = 0.0f, snSum = 0.0f;
583 F3 cSum{0, 0, 0}, nSum{0, 0, 0};
584 float dMax = 0.0f;
585 F3 fAcc{0, 0, 0}, tAccA{0, 0, 0}, tAccB{0, 0, 0};
586 // Per-point dashpots weighted by stiffness against LAST step's patch stiffness sum:
587 // eta_i = 2 beta sn_i sqrt(m*/snTot). Sums to the exact two-body damping AND gives every
588 // patch mode (incl. face rocking) a uniform damping ratio; the patch-level dashpot alone
589 // left rocking modes undamped (measured: cubes tumbling uphill on an incline).
590 const float snTotPrev = snPrev(idx);
591 const float dampC = (mStar > 0.0f) ? -2.0f * 0.9128709f * bd * Kokkos::sqrt(mStar) : 0.0f;
592
593 for (int k = 0; k < iter; ++k) {
594 F3 pLocalA{0, 0, 0};
595 float pointRadius = 0.0f;
596 if (countA > 0) {
597 const int sIdx = dA.shellOffset + k;
598 pLocalA = F3{shell(sIdx, 0), shell(sIdx, 1), shell(sIdx, 2)};
599 } else if (sphereA) {
600 pointRadius = dA.params.x * effScaleA;
601 }
602 const F3 pWorld = add3(posA, rotateVector(qA, scale3(pLocalA, effScaleA)));
603 const F3 pCanB = scale3(invRotateVector(qB, sub3(pWorld, posB)), 1.0f / effScaleB);
604 const float dist = sdfEvalShape(pCanB, dB, sdfGrid) * effScaleB;
605 const float effDist = dist - pointRadius;
606 if (effDist >= 0.0f)
607 continue;
608 const float delta = -effDist;
609 F3 nLoc = sdfGradShape(pCanB, dB, sdfGrid);
610 const float len = len3(nLoc);
611 nLoc = (len > 1e-9f) ? scale3(nLoc, 1.0f / len) : F3{0, 1, 0};
612 const F3 nW = rotateVector(qB, nLoc); // out of B = push direction on A
613 const F3 pSurfA = sub3(pWorld, scale3(nW, pointRadius));
614 const F3 rA = sub3(pSurfA, posA);
615 const F3 rB = sub3(add3(pSurfA, scale3(nW, delta)), posB);
616
617 const F3 vrelP = sub3(add3(vA, cross3v(wA, rA)), add3(vB, cross3v(wB, rB)));
618 const float vnP = dot3(vrelP, nW);
619 const float sq = Kokkos::sqrt(rStar * delta);
620 const float fnSpring = (4.0f / 3.0f) * eStar * sq * delta;
621 const float snI = 2.0f * eStar * sq;
622 const float snTot = (snTotPrev > snI) ? snTotPrev : snI; // first-contact guard
623 const float etaI = dampC * snI / Kokkos::sqrt(snTot);
624 const F3 f = scale3(nW, fnSpring - etaI * vnP);
625 fAcc = add3(fAcc, f);
626 tAccA = add3(tAccA, cross3v(rA, f));
627 tAccB = add3(tAccB, cross3v(rB, scale3(f, -1.0f)));
628 fnSum += fnSpring;
629 snSum += 2.0f * eStar * sq; // patch tangent stiffness = sum of point stiffnesses
630 cSum = add3(cSum, scale3(pSurfA, fnSpring));
631 nSum = add3(nSum, scale3(nW, fnSpring));
632 if (delta > dMax)
633 dMax = delta;
634 }
635
636 if (fnSum <= 0.0f) {
637 xi(idx, 0) = xi(idx, 1) = xi(idx, 2) = 0.0f;
638 snPrev(idx) = 0.0f;
639 return;
640 }
641 // Patch level: ONE normal dashpot and ONE Mindlin tangential spring at the
642 // load-weighted centroid.
643 const F3 cpt = scale3(cSum, 1.0f / fnSum);
644 F3 nhat = nSum;
645 const float nl = len3(nhat);
646 if (nl > 1e-9f)
647 nhat = scale3(nhat, 1.0f / nl);
648 const F3 rA = sub3(cpt, posA), rB = sub3(cpt, posB);
649 const F3 vrel = sub3(add3(vA, cross3v(wA, rA)), add3(vB, cross3v(wB, rB)));
650 snPrev(idx) = snSum;
651 const F3 vt = sub3(vrel, scale3(nhat, dot3(vrel, nhat)));
652 const float st = 8.0f * gStar * Kokkos::sqrt(rStar * dMax);
653 const float etaT = -2.0f * 0.9128709f * bd * Kokkos::sqrt(st * mStar);
654 F3 x{xi(idx, 0), xi(idx, 1), xi(idx, 2)};
655 x = add3(x, scale3(vt, dt));
656 x = sub3(x, scale3(nhat, dot3(x, nhat)));
657 F3 ft = add3(scale3(x, -st), scale3(vt, -etaT));
658 const float ftLen = len3(ft);
659 const float ftMax = mu * fnSum;
660 if (ftLen > ftMax) {
661 const float sc = (ftLen > 1e-20f) ? ftMax / ftLen : 0.0f;
662 ft = scale3(ft, sc);
663 if (st > 1e-20f)
664 x = scale3(add3(ft, scale3(vt, etaT)), -1.0f / st);
665 }
666 xi(idx, 0) = x.x;
667 xi(idx, 1) = x.y;
668 xi(idx, 2) = x.z;
669 fAcc = add3(fAcc, ft);
670 tAccA = add3(tAccA, cross3v(rA, ft));
671 tAccB = add3(tAccB, cross3v(rB, scale3(ft, -1.0f)));
672
673 for (int c = 0; c < 3; ++c) {
674 Kokkos::atomic_add(&force(idA, c), (&fAcc.x)[c]);
675 Kokkos::atomic_add(&force(idB, c), -(&fAcc.x)[c]);
676 Kokkos::atomic_add(&torque(idA, c), (&tAccA.x)[c]);
677 Kokkos::atomic_add(&torque(idB, c), (&tAccB.x)[c]);
678 }
679 });
680 space.fence();
681}
682
683} // namespace peclet::dem
dem — portable (Kokkos) contact->manifold reduction, replacing the thrust-based reduce_contacts_to_ma...
float hertzBetaD(float e)
Restitution -> damping ratio beta_d = ln e / sqrt(ln^2 e + pi^2) (e in (0, 1]; e <= 0 -> -1,...
void hertzWallForces(Kokkos::View< const int *, CpMem > candSlots, int numCand, const HertzWallState &wall, const HertzBodies &bodies, const HertzMaterials &mat, float dt, Kokkos::View< float *[3], CpMem > force, Kokkos::View< float *[3], CpMem > torque, const HertzShapes &shp={})
SDF-wall forces (per particle x wall).
int hertzBuildWallCandidates(int numReal, int numWalls, Kokkos::View< const WallSdf *, CpMem > walls, GridView wallGrid, Kokkos::View< const float *[3], CpMem > pos, Kokkos::View< const float *, CpMem > rad, float skin, Kokkos::View< int *, CpMem > outSlots, Kokkos::View< int, CpMem > outCount)
Build the wall candidate list: particles within (radius + skin) of any wall's zero level.
F3 sdfGradShape(F3 p, const ShapeDesc &d, GridView grid)
Outward gradient of a shape's SDF at canonical point p, NOT normalised (callers normalise,...
F3 cross3v(F3 a, F3 b)
void hertzIntegrate(int numReal, Kokkos::View< float *[3], CpMem > force, Kokkos::View< float *[3], CpMem > torque, const BodyState &bodies, F3 gravity, float dt, bool integrateOrientation=false)
Symplectic-Euler kick-drift (MUSEN-style) + displacement tracking for the Verlet rebuild.
CpExec::memory_space CpMem
float sampleWallSdf(F3 p, const WallSdf &w, GridView grid)
Trilinearly sample a static world-space wall SDF at world point p.
F3 invRotateVector(F4 q, F3 v)
F3 loadF3(PosView v, int i)
float sdfEvalShape(F3 p, const ShapeDesc &d, GridView grid)
Canonical-space SDF of a shape: analytic dispatch, or a trilinear grid sample for an imported grid SD...
void hertzWallEMu(int ma, const WallSdf &w, PairTableView pairTable, float eGlobal, float muGlobal, float &e, float &mu)
A particle-wall pair's (e, mu): the wall's material id through the pair table when both are set,...
F3 rotateVector(F4 q, F3 v)
void hertzPairEMu(int matA, int matB, PairTableView pairTable, float eGlobal, float muGlobal, float &e, float &mu)
Pairwise (e, mu) lookup shared with the narrowphase convention: pair table if present,...
float dot3(F3 a, F3 b)
F3 sub3(F3 a, F3 b)
F3 hertzForce(float delta, F3 nhat, F3 vrel, float rStar, float mStar, float eStar, float gStar, float e, float mu, float dt, F3 &xi)
One Hertz–Mindlin force evaluation for a sphere-sphere or sphere-wall contact.
Kokkos::View< const float *, CpMem > PairTableView
float hertzSphereRadius(int i, Kokkos::View< const float *, CpMem > rad, Kokkos::View< const float *, CpMem > scale, ScalarI shapeId, Kokkos::View< const ShapeDesc *, CpMem > shapes, float globalScale)
A body's Hertz sphere radius: a SPHERE shape's own radius, scale * globalScale * params....
Kokkos::DefaultExecutionSpace CpExec
void hertzPairForces(Kokkos::View< const int *[2], CpMem > pairs, int numPairs, const HertzBodies &bodies, const HertzMaterials &mat, float dt, Kokkos::View< float *[3], CpMem > xi, Kokkos::View< float *[3], CpMem > force, Kokkos::View< float *[3], CpMem > torque, const HertzShapes &shp={})
Cached-pair forces: overlap from current positions; history resets when a cached pair is currently se...
F4 loadF4(QuatView v, int i)
void hertzShapePairForces(Kokkos::View< const int *[2], CpMem > pairs, int numPairs, const HertzBodies &bodies, const HertzShapes &shp, GridView sdfGrid, const HertzMaterials &mat, float dt, Kokkos::View< float *[3], CpMem > xi, Kokkos::View< float *, CpMem > snPrev, Kokkos::View< float *[3], CpMem > force, Kokkos::View< float *[3], CpMem > torque)
Non-spherical pairs: per-point Hertz springs over A's point shell against B's SDF (the same one-sided...
Kokkos::View< const int *, CpMem > ScalarI
F3 scale3(F3 a, float s)
float len3(F3 v)
Kokkos::View< const float *[3], CpMem > ShellView
F3 add3(F3 a, F3 b)
float hertzMaxDisp2(int numReal, Kokkos::View< const float *[3], CpMem > pos, Kokkos::View< const float *[3], CpMem > refPos)
Max squared displacement since the last pair build (called only at rebuild checks – keeping this out ...
Kokkos::View< const unsigned char *, CpMem > MatIdView
F3 wallSdfGradient(F3 q, const WallSdf &w, GridView wallGrid)
The wall SDF's gradient at q by central differences at half a grid cell (unnormalized).
Kokkos::View< const float *, CpMem > GridView
constexpr int kMaxMaterials
Pair-material lookup: flat [K*K*2] table, entry ((a*K + b)*2) = restitution, +1 = friction.
dem — portable (Kokkos) narrow-phase: SDF point-shell collision + boundary planes.
dem — portable (Kokkos) particle SoA container: the storage the dem flip pivots on.
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
The body state a Hertz force launcher reads.
Kokkos::View< const float *[3], CpMem > vel
Kokkos::View< const float *[3], CpMem > angVel
Kokkos::View< const float *[3], CpMem > pos
Kokkos::View< const float *, CpMem > invMass
Kokkos::View< const float *, CpMem > rad
The Hertz–Mindlin contact materials: each body's material id, the optional pair table of (e,...
Kokkos::View< const float *, CpMem > hertzNu
Kokkos::View< const float *, CpMem > hertzE
What a non-spherical Hertz contact reads: orientation, scale, shape id, the shape registry and its sh...
Kokkos::View< const float *[4], CpMem > quat
Kokkos::View< const float *, CpMem > scale
Kokkos::View< const ShapeDesc *, CpMem > shapes
The SDF walls and their per-(particle, wall) contact history at slot i * maxWalls + wall: the Mindlin...
Kokkos::View< float *, CpMem > snWall
Kokkos::View< const WallSdf *, CpMem > walls
Kokkos::View< float *[3], CpMem > xiWall
Portable mirror of ShapeDescriptor (analytic fields + a flat-array point shell).
Static, world-space SDF container/geometry the particles collide against (a drum barrel,...
peclet::core::geom::GridDesc< float > grid