34#ifndef DEM_SOLVE_DRIVER_FORCE_HPP
35#define DEM_SOLVE_DRIVER_FORCE_HPP
39#include <Kokkos_Core.hpp>
40#include <Kokkos_Sort.hpp>
57 const size_t need = P.
pairs.extent(0);
58 if (P.
hertzXi.extent(0) < need) {
59 P.
hertzXi = Kokkos::View<float* [3], CpMem>(
"hertzXi", need);
60 P.
hertzSnPair = Kokkos::View<float*, CpMem>(
"hertzSnPair", need);
61 P.
hertzKeys = Kokkos::View<unsigned long long*, CpMem>(
"hertzKeys", need);
62 P.
hertzPrevKeys = Kokkos::View<unsigned long long*, CpMem>(
"hertzPrevKeys", need);
63 P.
hertzPrevXi = Kokkos::View<float* [3], CpMem>(
"hertzPrevXi", need);
75 "peclet::dem::hertz_carry", Kokkos::RangePolicy<CpExec>(space, 0, np),
76 KOKKOS_LAMBDA(
int idx) {
85 const int a0 = pairs(idx, 0), b0 = pairs(idx, 1);
86 if (gid(a0) > gid(b0)) {
90 const unsigned long long k =
91 pairKeyFromGids((
unsigned)gid(pairs(idx, 0)), (
unsigned)gid(pairs(idx, 1)));
94 const bool hit = (l < pc && pk(l) == k);
95 xi(idx, 0) = hit ? px(l, 0) : 0.0f;
96 xi(idx, 1) = hit ? px(l, 1) : 0.0f;
97 xi(idx, 2) = hit ? px(l, 2) : 0.0f;
102 auto sn = Kokkos::subview(P.
hertzSnPair, Kokkos::pair<int, int>(0, np));
103 Kokkos::deep_copy(space, sn, 0.0f);
121 const auto rng = Kokkos::pair<int, int>(0, n);
123 Kokkos::deep_copy(space, kd, Kokkos::subview(P.
hertzKeys, rng));
124 Kokkos::View<int*, CpMem> perm(
125 Kokkos::view_alloc(space,
"peclet::dem::hertz_perm", Kokkos::WithoutInitializing), n);
126 Kokkos::parallel_for(
127 "peclet::dem::hertz_iota", Kokkos::RangePolicy<CpExec>(space, 0, n),
128 KOKKOS_LAMBDA(
int i) { perm(i) = i; });
129 Kokkos::Experimental::sort_by_key(space, kd, perm);
132 Kokkos::parallel_for(
133 "peclet::dem::hertz_gather", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
134 const int j = perm(i);
155 return HertzShapes{.quat = Kokkos::View<const float* [4], CpMem>(P.
quat),
156 .scale = Kokkos::View<const float*, CpMem>(P.
scale),
162 .hasShapes = hasShapes};
206 throw std::runtime_error(
"step_hertz: periodic domains not supported");
208 throw std::runtime_error(
"step_hertz: analytic planes not supported (use an SDF wall)");
210 throw std::runtime_error(
"step_hertz: too many SDF walls");
221 Kokkos::parallel_for(
222 "peclet::dem::force_zero_ghost", Kokkos::RangePolicy<CpExec>(space, lo, hi),
223 KOKKOS_LAMBDA(
int i) {
224 for (
int c = 0; c < 3; ++c) {
235 float allMax(
float v)
const {
return v; }
236 float allMin(
float v)
const {
return v; }
244template <
class Law,
class Hooks>
246 const Hooks& hooks) {
255 Kokkos::parallel_reduce(
256 "peclet::dem::hertz_minrad", Kokkos::RangePolicy<CpExec>(space, 0, P.
numReal),
257 KOKKOS_LAMBDA(
int i,
float& acc) {
261 Kokkos::Min<float>(m));
269 const float skin = skinFrac * hooks.allMin(minRad);
270 const float rebuildAt = 0.25f * skin * skin;
274 bool rebuild = hooks.allMax(P.
hertzNumPairs < 0 ? 1.0f : 0.0f) > 0.0f;
277 bool hasShapes =
false;
279 auto hs = Kokkos::create_mirror_view(P.
shapes);
280 Kokkos::deep_copy(hs, P.
shapes);
281 for (
size_t si = 0; si < hs.extent(0); ++si)
282 if (hs(si).numPoints > 0)
285 static const bool profile = std::getenv(
"PECLET_DEM_HERTZ_PROFILE") !=
nullptr;
286 double tPair = 0, tWall = 0, tInt = 0, tRebuild = 0, tCheck = 0;
289 for (
int k = 0; k < nsteps; ++k) {
291 law.commitHistory(P);
296 hooks.gatherGhosts(P);
297 law.rebuild(P, skin);
301 tRebuild += timer.seconds();
306 hooks.refreshGhostState(P, hasShapes);
308 hooks.clearGhostScratch(P);
309 law.pairForces(P, dt, hasShapes);
311 tPair += timer.seconds();
314 law.wallForces(P, dt, hasShapes);
316 tWall += timer.seconds();
321 tInt += timer.seconds();
324 if ((k & 15) == 15 || k == nsteps - 1) {
328 tCheck += timer.seconds();
335 "[hertz profile] steps=%d pairs=%d wallcand=%d rebuilds=%d | pair %.3fs wall %.3fs "
336 "integrate %.3fs check %.3fs rebuild %.3fs\n",
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.
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.
void hertzCommitHistory(Particles &P)
Save the current list (sorted by key) so the NEXT rebuild can carry the history.
void demStepForce(Particles &P, float dt, int nsteps, float skinFrac, const Law &law, const Hooks &hooks)
nsteps explicit force-based DEM steps of size dt with force law Law (see file comment).
Kokkos::View< const float *, CpMem > PairTableView
unsigned long long pairKeyFromGids(unsigned a, unsigned b)
The one 64-bit pair identity every gid-keyed ledger uses (the XPBD persistent-contact and warm-start ...
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...
Kokkos::View< float *[3], CpMem > V3
int findCollisionsGrow(Particles &P, float margin)
Broad phase with an automatically-grown pair buffer.
void hertzRebuildPairs(Particles &P, float skin)
Rebuild the Hertz cached pair list with margin skin, carrying the Mindlin shear history across by pai...
void zeroForceScratch(V3 dv, V3 dw, int lo, int hi)
Zero the force/torque accumulator rows [lo, hi) — under MPI the pair kernels atomically accumulate on...
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...
void demStepHertz(Particles &P, float dt, int nsteps, float skinFrac)
nsteps of the soft-sphere Hertz-Mindlin engine — the single-GPU instantiation of the force-based driv...
void fillWorldRadii(Vf scale, Vf rad, float gs, float bR, int n)
World radii rad(i) = scale(i) * globalScale * baseRadius over [0, n): the owned span before a step,...
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
int lowerBoundKey(const KeysView &keys, int count, unsigned long long key)
lower_bound over a ledger's sorted keys: the first index whose key is >= key (count when none).
dem — portable (Kokkos) particle SoA container: the storage the dem flip pivots on.
dem — the shared contact-solve driver: the full modern velocity + position solve sequence (warm-start...
Soft-sphere Hertz–Mindlin DEM (reference force model) for SPHERES.
The body state a Hertz force launcher reads.
The Hertz–Mindlin contact materials: each body's material id, the optional pair table of (e,...
Hertz–Mindlin force-law policy (the first force law; see file comment).
static HertzShapes shapes(const Particles &P, bool hasShapes)
The shape data a Hertz contact reads (hasShapes false: a sphere-only run).
void wallForces(Particles &P, float dt, bool hasShapes) const
void rebuild(Particles &P, float skin) const
static HertzBodies bodies(const Particles &P)
The body state a Hertz contact reads.
void commitHistory(Particles &P) const
void validate(const Particles &P) const
Engine-support restrictions of this law (probed once per call).
void pairForces(Particles &P, float dt, bool hasShapes) const
What a non-spherical Hertz contact reads: orientation, scale, shape id, the shape registry and its sh...
The SDF walls and their per-(particle, wall) contact history at slot i * maxWalls + wall: the Mindlin...
Kokkos::View< WallSdf *, CpMem > walls
Kokkos::View< float *, CpMem > hertzE
Kokkos::View< float *[3], CpMem > hertzXi
Kokkos::View< unsigned char *, CpMem > materialId
Kokkos::View< int, CpMem > hertzWallCandCount
Kokkos::View< int *[2], CpMem > pairs
Kokkos::View< int *, CpMem > hertzWallCand
Kokkos::View< float, CpMem > hertzDispMax
static constexpr int kHertzMaxWalls
Kokkos::View< float *, CpMem > pairMaterials
Kokkos::View< ShapeDesc *, CpMem > shapes
Kokkos::View< unsigned long long *, CpMem > hertzPrevKeys
float hertzContactRadiusFrac
Kokkos::View< float *, CpMem > hertzNu
Kokkos::View< unsigned long long *, CpMem > hertzKeys
BodyState bodyState() const
Kokkos::View< float *, CpMem > hertzSnWall
Kokkos::View< float *[3], CpMem > hertzRefPos
Kokkos::View< float *, CpMem > sdfGrid
Kokkos::View< float *, CpMem > hertzSnPair
Kokkos::View< float *, CpMem > wallGrid
Kokkos::View< float *[3], CpMem > shell
Kokkos::View< float *[3], CpMem > hertzXiWall
Kokkos::View< float *[3], CpMem > hertzPrevXi
Single-GPU hooks: no ghosts, reductions are already global. Everything inlines away.
static constexpr bool distributed
float allMin(float v) const
float allMax(float v) const
void clearGhostScratch(Particles &) const
void gatherGhosts(Particles &) const
void refreshGhostState(Particles &, bool) const