9#ifndef DEM_INTEGRATION_HPP
10#define DEM_INTEGRATION_HPP
12#include <Kokkos_Core.hpp>
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>;
30KOKKOS_INLINE_FUNCTION
void st3(
const V3& v,
int i,
F3 a) {
35KOKKOS_INLINE_FUNCTION
void st4(
const V4& v,
int i,
F4 a) {
48 const V3 pos = bodies.
pos, vel = bodies.
vel, angVel = bodies.
angVel,
51 const V4 quat = bodies.
quat;
59 "peclet::dem::predict_velocity", Kokkos::RangePolicy<CpExec>(space, 0, n),
60 KOKKOS_LAMBDA(
int i) {
61 const float invM = invMass(i);
71 F3 wpred =
ldF3(angVel, i);
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);
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};
87 const F3 alpha{invI.
x * (tb.
x - wxL.
x), invI.
y * (tb.
y - wxL.
y),
88 invI.
z * (tb.
z - wxL.
z)};
105 constraintCounts(i) = 0;
114 Kokkos::parallel_for(
115 "peclet::dem::apply_velocity_deltas", Kokkos::RangePolicy<CpExec>(space, 0, n),
116 KOKKOS_LAMBDA(
int i) {
129 const V3 pos = bodies.
pos, vel = bodies.
vel, angVel = bodies.
angVel;
131 const V4 quat = bodies.
quat;
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);
143 const float invM = invMass(i);
149 const F4 q =
ldF4(quat, i);
150 const F3 omega =
ldF3(angVelPred, i);
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);
160 const float s = 1.0f / len;
161 qp =
F4{qp.
x * s, qp.
y * s, qp.
z * s, qp.
w * s};
171 Vi constraintCounts) {
173 Kokkos::parallel_for(
174 "peclet::dem::contact_counts", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
175 KOKKOS_LAMBDA(
int i) {
179 Kokkos::atomic_add(&constraintCounts(c.
bodyA), 1);
181 Kokkos::atomic_add(&constraintCounts(c.
bodyB), 1);
192 bool averaged =
true) {
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);
202 const float f = averaged ? 1.0f /
static_cast<float>(count) : 1.0f;
207 constraintCounts(i) = 0;
219 Kokkos::parallel_for(
220 "peclet::dem::final_commit", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
225 else if (p.
x >= dom.
max.
x)
231 else if (p.
y >= dom.
max.
y)
237 else if (p.
z >= dom.
max.
z)
251 double t = 0.0,
r = 0.0;
260 double Ttarget,
float dt) {
263 const V4 quat = bodies.
quat;
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);
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) {
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);
284 const double ndof = 3.0 * numReal;
285 auto lambda = [&](
double ke) {
286 if (ndof <= 0 || kB <= 0)
288 const double Tcur = (2.0 * ke) / (ndof * kB);
291 double f = 1.0 + (dt / tau) * (Ttarget / Tcur - 1.0);
294 return static_cast<float>(Kokkos::sqrt(f));
296 const float lt = lambda(sum.
t), lr = lambda(sum.
r);
298 Kokkos::parallel_for(
299 "peclet::dem::thermo_scale", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
300 KOKKOS_LAMBDA(
int i) {
309 if (!(factor > 0.0f))
312 Kokkos::parallel_for(
313 "peclet::dem::growth", Kokkos::RangePolicy<CpExec>(space, 0, n),
314 KOKKOS_LAMBDA(
int i) { scale(i) = targetScale(i) * factor; });
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).
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.
Kokkos::View< float *[4], CpMem > V4
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
Berendsen thermostat: reduce translational + rotational KE over real particles, compute the two scali...
KE2 & operator+=(const KE2 &o)