peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
solve_driver.hpp File Reference

dem — the shared contact-solve driver: the full modern velocity + position solve sequence (warm-started colored PGS with persistent contacts, gravity statics / stabilization passes, friction, colored-GS overlap projection, adaptive stops), extracted from demStep so the single-GPU and the distributed (MPI) steps run ONE driver instead of drifting copies. More...

#include <algorithm>
#include <cstdlib>
#include <Kokkos_Core.hpp>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "broadphase_arborx.hpp"
#include "contact_preprocessing.hpp"
#include "particles.hpp"
#include "solve_copies.hpp"
#include "solver_friction.hpp"
#include "solver_multilevel.hpp"
#include "solver_position.hpp"
#include "solver_velocity.hpp"

Go to the source code of this file.

Classes

struct  peclet::dem::CudaIterGraph
struct  peclet::dem::RankK
 Rank-level M (docs/contact_solve_framework.md §13.3) as the hooks expose it to the driver: whether this rank reconciles copies with neighbours at all (it sends or receives ghosts). More...
struct  peclet::dem::SoloSolveHooks
 Single-GPU hooks: no ghost refresh, residuals are already global. Everything inlines away. More...
struct  peclet::dem::ColoringInvariant
 The colouring invariant (§4.2 item 4, §12 S3): the failing phase's edges, the accumulated message, and whether the distributed vote is still owed. More...
struct  peclet::dem::PositionSolveContext
 The overlap projection's loop context: what its iterations read that solvePositionPhase settles before them – the units and colour schedule, the stop tolerance and the phase's switches. More...
struct  peclet::dem::VelocitySolveContext
 The velocity phase's solve context: everything the velocity-phase loops read that demSolveContacts settles before them – the colour schedule, the solve views and slot override (true masses, mass-split copies or rank-level M), the Poisson restitution views, gravity, the rank-level X gate and the phase's switches. More...
struct  peclet::dem::WarmStartContext
 The warm start's inputs that demSolveContacts settles before it: the pair-key map, gravity, the visible manifold count and the phase's switches. More...
struct  peclet::dem::SweepPlan
 One coloured phase's sweep plan: owns the dense-bucket offsets and the fused context that its ColorSchedule points at, so it is built in place and never copied or moved. More...
struct  peclet::dem::VelocitySolveViews
 The velocity phase's solve views and slot override: true masses; the hub copies' split masses; or each slot's global k_vel (rank-level M). More...

Namespaces

namespace  peclet
namespace  peclet::dem

Macros

#define PECLET_DEM_GRAPH_LOOP(useVar, graphVar, emitVar, slotVar)

Functions

int peclet::dem::readInt (Kokkos::View< int, CpMem > v)
float peclet::dem::readFloat (Kokkos::View< float, CpMem > v)
void peclet::dem::fillGidBase (Vi gid, int n, int base)
 gid(i) = base + i over [0, n) — the per-rank global-id re-base of the distributed step (namespace scope: nvcc forbids KOKKOS_LAMBDA in member functions).
float peclet::dem::maxOwnedRadius (const Particles &P)
 Largest effective particle radius over the owned set (= max scale × globalScale, growth included).
void peclet::dem::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, owned + ghosts after periodic ghost generation or a halo gather.
int peclet::dem::findCollisionsGrow (Particles &P, float margin)
 Broad phase with an automatically-grown pair buffer.
int peclet::dem::findCollisionsVerlet (Particles &P, float margin, float maxRad)
 Verlet-cached impulse broadphase (single-GPU, non-periodic).
int * peclet::dem::demIterCountSlot (Particles &P, int slot)
 Diagnostics (docs/contact_solve_framework.md §12 S12): the device scalar a fused main loop writes its iteration count to (slot 0 velocity, 1 position), allocated on first use.
int peclet::dem::demReadIterCount (const Particles &P, int slot)
 The count a fused main loop wrote (one small readback, only with P.iterCounters on); -1 with the counters off (no fence, no copy).
void peclet::dem::growCopySlots (Particles &P, int slotBase, int nCopies)
 Room for nCopies copy slots appended at slotBase (the colour phases' hub copies).
template<class Hooks>
int peclet::dem::colorVelocityPhase (Particles &P, int nm, int nBodies, bool usePersistPre, bool incrColorOff, const Kokkos::View< const unsigned char *, CpMem > mSleep, PhaseCopies &vcs, int slotBase, ColoringInvariant &inv)
 Velocity-phase colouring (docs/contact_solve_framework.md §4.2 item 4, §4.4), the PGS paths only: colour the manifold graph – incrementally or in full – and, if an edge found no free colour, split the hubs into copies and recolour.
template<class Hooks>
int peclet::dem::colorPositionPhase (Particles &P, int nc, int nm, bool usePersistPre, bool incrColorOff, const Kokkos::View< const unsigned char *, CpMem > cSleep, PhaseCopies &pcs, int slotBase, ColoringInvariant &inv, int &numPosUnits, PosUnits &posUnits)
 Position-phase colouring (docs/contact_solve_framework.md §4.2 item 3, §4.3, §4.4), the PGS paths only: build the position units, colour them – incrementally or in full – with hub copies when an edge found no free colour (and fold groups for periodic images), then give every contact its unit's colour.
float peclet::dem::withConsensus (Particles &P, float res, bool cons)
 §12 S14: a stop vote's residual with the largest consensus correction since the previous vote folded in (read, then zeroed) when the phase has copies (cons); res unchanged otherwise.
template<class Hooks, class EmitPosIter, class SyncPos>
void peclet::dem::runOverlapProjection (Particles &P, int nc, const Hooks &hooks, CpExec &space, const PositionSolveContext &ctx, EmitPosIter &&emitPosIter, SyncPos &&syncPos, ColoringInvariant &inv)
 The overlap projection's iterations to their adaptive stop (WO-12): the fused device loop (CUDA, single-rank), CUDA-graph replay or host submission of one iteration (emitPosIter), or mass-split Jacobi; the position syncs and the final owner->ghost refresh.
template<class Hooks>
void peclet::dem::solvePositionPhase (Particles &P, int nc, int nm, const Hooks &hooks, CpExec &space, PhaseCopies &pcs, int slotBase, bool rankM, bool usePGS, const PosUnits posUnits, int numPosUnits, const ColorSchedule &posSched, bool stopsOn, float fusedOff, ColoringInvariant &inv)
 The position phase (docs/contact_solve_framework.md §4.4, §4.5, §13.3 step 9; WO-12): seed the position-phase copies, run the accumulated overlap projection to its adaptive stop, and carry the position-channel Coulomb bound to the next substep.
VelocitySweepBodies peclet::dem::velocitySweepBodies (Particles &P, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia, SlotOverride ov={})
 The velocity sweep's bodies: the masses invMass/invInertia (true masses or the solve copies'), P's orientation, predicted velocities and velocity slots, the slot overrides ov.
PoissonBankViews peclet::dem::poissonBankViews (Particles &P, bool on)
 The solve's Poisson views: the banks and the orphan accounts when the model is on, else empty.
void peclet::dem::foldVelocityCopies (CpExec &space, Particles &P, const VelocitySolveContext &ctx)
 The velocity-phase copies' fold, run in every iteration after the sweep (none without copies).
template<class Hooks>
void peclet::dem::syncVelocityPhase (Particles &P, const Hooks &hooks, const VelocitySolveContext &ctx)
 A rank sync inside the velocity phase: orphan accounts back to balances, the raw reverse + forward, then the copies' seeds re-marked from the reconciled bases.
template<class Hooks>
bool peclet::dem::launchFusedVelocityLoop (Particles &P, int nm, float vRest, const VelocitySolveContext &ctx)
 The main velocity phase's device-side iteration loop (CUDA, single-rank PGS with a fused schedule and no copies): the whole adaptive loop as ONE kernel.
void peclet::dem::oneShotVelocityIteration (Particles &P, int nm, int it, float vRest, CpExec &space, const VelocitySolveContext &ctx)
 One iteration of the g = 0 one-shot coloured GS velocity solve (velocityUseGS on, PGS off): the residual zero, the rank-level X gate for sync interval it, the coloured sweep, the copy fold.
template<class Hooks>
void peclet::dem::massSplitVelocityIteration (Particles &P, int nm, const Hooks &hooks, float vRest)
 Mass-split Jacobi (velocityUseGS off, a global setting; docs/contact_solve_framework.md §3.1): every manifold is its own copy.
template<class Hooks>
void peclet::dem::solveVelocityPhase (Particles &P, int nc, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, ColoringInvariant &inv)
 The main velocity phase (docs/contact_solve_framework.md §4.4, §4.5, §13.3): the warm-started PGS iterations (|g| > 0; fused device loop, CUDA-graph replay or host submission), the g = 0 one-shot coloured GS with rank-level X, or mass-split Jacobi, each to its adaptive stop, and the final owner->ghost refresh.
template<class Hooks>
void peclet::dem::stabilizeOneSided (Particles &P, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
 The ONE-SIDED grounded stabilization pass (stabilization mode 1, the default): held-lower-side impulses, to the adaptive stop at vRestS.
Kokkos::View< const float *, CpMem > peclet::dem::coarseVertexInvMass (Particles &P, int nBodies, bool velM)
 The MULTILEVEL (GraphMG) stabilization pass (stabilization mode 2): fine sweeps plus a coarse cycle over rigid aggregates of the quasi-static contact graph, to the adaptive stop.
Kokkos::View< const float *[3], CpMem > peclet::dem::coarseVertexInvInertia (Particles &P, int nBodies, bool velM)
 The coarse vertices' inverse inertias for the multilevel pass (the same fraction).
void peclet::dem::allocateMultilevelPools (Particles &P, int nBodies)
 The multilevel pass's rigid-aggregate pools and coarse accumulator, sized for nBodies.
void peclet::dem::recordMultilevelHierarchy (Particles &P, int nm, int nBodies, const ContactHierarchy &H, const MlScratch &S, bool velM, bool velCopiesOn)
 The multilevel hierarchy's diagnostics: the debug-build inactive-vertex check, the hub aggregation count and the last hierarchy's shape (P.mlLast).
template<class Hooks>
void peclet::dem::stabilizeMultilevel (Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
template<class Hooks>
void peclet::dem::stabilizeOrdered (Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
 The ORDERED stabilization pass (stabilization mode 4, measurement mode): level-ordered symmetric sweeps, to the adaptive stop.
template<class Hooks>
void peclet::dem::stabilizeEscalation (Particles &P, int nm, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx, const float vRestS)
 The ESCALATION stabilization pass (stabilization mode 3, diagnostic/fallback): plain symmetric coloured sweeps, to the adaptive stop or the 256-sweep cap.
template<class Hooks>
void peclet::dem::stabilizeVelocityPhase (Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const VelocitySolveContext &ctx)
 The stabilization pass after the main velocity phase (Guendelman staging, Phase B): when the symmetric sweeps leave a residual above 2 g dt, the one-sided grounded pass (default), the multilevel (GraphMG) pass, the ordered or the escalation pass, each to its adaptive stop, and the final owner->ghost refresh.
template<class Hooks>
void peclet::dem::closeVelocityPhase (Particles &P, int nm, const Hooks &hooks, const VelocitySolveContext &ctx)
 The close of the velocity phase (§13.3 step 8): the orphan accounts back to balances, the event-level (Poisson) restitution bookkeeping on the final velocity state, and the converged force network saved for the next substep's warm start.
template<class Hooks>
void peclet::dem::openVelocityWarmStart (Particles &P, int nm, int nBodies, const Hooks &hooks, CpExec &space, const WarmStartContext &ws)
 The velocity phase's warm start and opening (docs/contact_solve_framework.md §13.3 step 5): on the PGS path the gather of the previous substep's impulses by pair key, the Poisson orphan transfer, the persistence and grounding marks, the pre-solve approach, the warm impulses and the opening; on the g = 0 one-shot path the position-count opening.
void peclet::dem::countRankLevelActivity (Particles &P, int nm, bool usePersistPre, const PosUnits &posUnits, int numPosUnits)
 Rank-level M's activity pass (docs/contact_solve_framework.md §13.3) on a rank that exchanges: size the per-slot count views (and, on the g = 0 one-shot path, rank-level X's masks and gates), then give each slot its local active copy count a per phase from the final colourings.
void peclet::dem::planColorSweeps (SweepPlan &plan, CpExec &space, Particles &P, Kokkos::View< int *, CpMem > color, int n, int numColors, Kokkos::View< int *, CpMem > perm, Kokkos::View< int *, CpMem > offsDev, bool wantFused)
 Dense colour buckets for one phase's PGS sweeps (bit-identical: colour classes are body-disjoint) over its n items, the fused context where wanted, and the schedule over both.
VelocitySolveViews peclet::dem::openVelocityCopies (Particles &P, int slotBase, bool velM, bool velCopiesOn, const PoissonBankViews &pv)
bool peclet::dem::gravityOn (const Particles &P)
 Whether gravity is on (any component non-zero): it gates persistent contacts, warm-started PGS and island sleeping.
template<class Hooks>
bool peclet::dem::islandSleepingOn (const Particles &P, bool gravity)
 Island sleeping: single-GPU statics, gravity on, no external drag.
bool peclet::dem::frictionOn (const Particles &P)
 A frictional wall drives friction even when the body-body material is frictionless.
template<class Hooks>
void peclet::dem::demSolveContacts (Particles &P, int nc, int nm, int nBodies, Kokkos::View< const int *, CpMem > keyIdx, const Hooks &hooks)
 One full velocity + position contact solve over the already-built contacts/manifolds (see file comment).

Detailed Description

dem — the shared contact-solve driver: the full modern velocity + position solve sequence (warm-started colored PGS with persistent contacts, gravity statics / stabilization passes, friction, colored-GS overlap projection, adaptive stops), extracted from demStep so the single-GPU and the distributed (MPI) steps run ONE driver instead of drifting copies.

The two callers differ only through the Hooks policy:

  • SoloSolveHooks (single-GPU): every hook is a no-op / identity — the driver compiles to exactly the pre-extraction demStep sequence (validated bit-for-bit on the Serial backend).
  • MpiSolveHooks (step_solve_mpi.hpp, PECLET_DEM_MPI): processor-block Gauss–Seidel with a SINGLE owner per contact (docs/mpi_momentum_conservation.md) — nc / nm are the counts of the contacts / manifolds this rank owns, and only they are swept, over owned + ghost body slots; the partner half of every impulse lands in the ghost slot. beginSolve marks the ghosts' velocity baselines; syncVelocities / syncPositions reverse-accumulate each ghost's change since its baseline onto its owner, then refresh owner->ghost (reverse, then forward), every syncEvery iterations plus once after every solve phase; publishPositions opens the position phase with a forward only (the integration of a ghost is not an interaction); syncFrictionCounts makes the legacy-friction counts and syncContactCounts the Jacobi count-averaging counts the serial ones. visibleManifolds(nm) is the whole visible range, read only by the label passes (warm-ledger match, grounded / height levels). allMax turns each adaptive-stop residual into a global MPI_Allreduce(MAX) so all ranks take the same break (a rank-local break would desynchronise the collective syncs and deadlock); allMaxAny folds a rank-local vote (the colouring-invariant check, §12 S3) into that same Allreduce.

nBodies is the body-slot span of the solve graph: numReal on the single-GPU path (ghost slots are realIndices-mapped onto their owners), numReal + numGhost under MPI (ghosts are self-mapped slots solved in place). keyIdx maps a body slot to the identity used in the persistent-pair keys: realIndices on the single-GPU path, the global particle id under MPI (local slots are not stable across halo rebuilds / migration).

Definition in file solve_driver.hpp.

Macro Definition Documentation

◆ PECLET_DEM_GRAPH_LOOP

#define PECLET_DEM_GRAPH_LOOP ( useVar,
graphVar,
emitVar,
slotVar )