29#ifndef PECLET_DEM_MPI_HALO_HPP
30#define PECLET_DEM_MPI_HALO_HPP
40#include <Kokkos_Core.hpp>
45#include <unordered_map>
50#include "peclet/core/common/types.hpp"
51#include "peclet/core/common/view.hpp"
52#include "peclet/core/decomp/block_decomposer.hpp"
53#include "peclet/core/halo/particle_halo.hpp"
54#include "peclet/core/halo/particle_halo_topology.hpp"
55#include "peclet/core/halo/particle_migrator.hpp"
56#include "peclet/core/halo/particle_rebalance.hpp"
66 F3 vel, velPred, angVel, angVelPred, invInertia;
76 unsigned char material, grounded;
79 float orphan, orphanVPeak;
86 unsigned long long key;
95inline constexpr int kWarmCarryMax = 14;
100struct HertzPairEntry {
101 unsigned long long key;
116 F3 vel, angVel, invInertia;
117 float invMass, scale, targetScale;
119 float planeFric0, planeFric1;
122 unsigned char materialId, groundedLevel, numWarm, numHertz;
123 float orphan, orphanVPeak;
126 F3 extForce, extTorque;
127 WarmPairEntry warm[kWarmCarryMax];
130 HertzPairEntry hertz[kWarmCarryMax];
131 float hertzXiWall[Particles::kHertzMaxWalls][3];
132 float hertzSnWall[Particles::kHertzMaxWalls];
137KOKKOS_INLINE_FUNCTION
int imagePack(
int x,
int y,
int z) {
138 return (x + 1) + 4 * (y + 1) + 16 * (z + 1);
140KOKKOS_INLINE_FUNCTION
int imageNegate(
int p) {
141 const int x = p % 4 - 1, y = (p / 4) % 4 - 1, z = (p / 16) % 4 - 1;
142 return imagePack(-x, -y, -z);
153struct ContactOwnership {
156 Kokkos::View<const int*, CpMem> gid, ghostSource, copyOffsets, copyRanks;
160 Kokkos::View<const int*, CpMem> ghostImage, copyImage;
163 Kokkos::View<const int*, CpMem> selfOffsets, selfImage;
164 KOKKOS_INLINE_FUNCTION
bool operator()(
const ContactC& c)
const {
165 const int a = c.bodyA, b = c.bodyB;
168 const bool ao = a < numReal, bo = b < numReal;
173#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 1
177 const int o = ao ? a : b, g = ao ? b : a;
178 if (gid(o) == gid(g))
180 const int s = ghostSource(g - numReal);
186 const int want = imageNegate(ghostImage(g - numReal));
188 for (
int k = selfOffsets(o); k < selfOffsets(o + 1); ++k)
189 if (selfImage(k) == want) {
196 bool partnerSees =
false;
197 const int want = imageNegate(ghostImage(g - numReal));
198 for (
int k = copyOffsets(o); k < copyOffsets(o + 1); ++k)
199 if (copyRanks(k) == s && copyImage(k) == want) {
206 return gid(o) < gid(g);
216struct VelocityIncrement {
222KOKKOS_INLINE_FUNCTION VelocityIncrement operator+(
const VelocityIncrement& a,
223 const VelocityIncrement& b) {
225 for (
int d = 0; d < 3; ++d) {
226 r.v[d] = a.v[d] + b.v[d];
227 r.w[d] = a.w[d] + b.w[d];
229 r.orphan = a.orphan + b.orphan;
230 r.orphanPeak = a.orphanPeak > b.orphanPeak ? a.orphanPeak : b.orphanPeak;
235struct PositionIncrement {
238KOKKOS_INLINE_FUNCTION PositionIncrement operator+(
const PositionIncrement& a,
239 const PositionIncrement& b) {
241 for (
int d = 0; d < 3; ++d)
242 r.x[d] = a.x[d] + b.x[d];
249inline void haloPackF3(
V3 field, peclet::core::View<F3> owned,
int n) {
250 Kokkos::parallel_for(
251 "peclet::dem::halo::packF3", Kokkos::RangePolicy<CpExec>(0, n),
252 KOKKOS_LAMBDA(
int i) { owned(i) =
F3{field(i, 0), field(i, 1), field(i, 2)}; });
254inline void haloPackF4(
V4 field, peclet::core::View<F4> owned,
int n) {
255 Kokkos::parallel_for(
256 "peclet::dem::halo::packF4", Kokkos::RangePolicy<CpExec>(0, n),
257 KOKKOS_LAMBDA(
int i) { owned(i) =
F4{field(i, 0), field(i, 1), field(i, 2), field(i, 3)}; });
270inline void haloUnpackF3(
V3 field, peclet::core::View<F3> ghost, peclet::core::View<F3> shift,
271 const GhostRange& ghosts,
bool doShift) {
272 const Vi slot = ghosts.slot;
273 const int no = ghosts.no;
274 const int ng = ghosts.ng;
275 Kokkos::parallel_for(
276 "peclet::dem::halo::unpackF3", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
283 const int s = no + slot(g);
289inline void haloUnpackF4(
V4 field, peclet::core::View<F4> ghost,
const GhostRange& ghosts) {
290 const Vi slot = ghosts.slot;
291 const int no = ghosts.no;
292 const int ng = ghosts.ng;
293 Kokkos::parallel_for(
294 "peclet::dem::halo::unpackF4", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
295 const int s = no + slot(g);
296 field(s, 0) = ghost(g).x;
297 field(s, 1) = ghost(g).y;
298 field(s, 2) = ghost(g).z;
299 field(s, 3) = ghost(g).w;
304 Kokkos::View<unsigned char*, CpMem> materialId,
305 Kokkos::View<unsigned char*, CpMem> grounded,
Vf orphan,
Vf orphanVPeak,
306 peclet::core::View<MpiGatherPack> owned,
int n) {
307 const V3 vel = body.vel;
308 const V3 velPred = pred.velPred;
309 const V3 angVel = body.angVel;
310 const V3 angVelPred = pred.angVelPred;
311 const V3 invInertia = body.invInertia;
312 const V4 quat = body.quat;
313 const V4 quatPred = pred.quatPred;
314 const Vf scale = body.scale;
315 const Vf invMass = body.invMass;
316 const Vi shapeId = body.shapeId;
317 Kokkos::parallel_for(
318 "peclet::dem::halo::packGather", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(
int i) {
320 g.vel =
F3{vel(i, 0), vel(i, 1), vel(i, 2)};
321 g.velPred =
F3{velPred(i, 0), velPred(i, 1), velPred(i, 2)};
322 g.angVel =
F3{angVel(i, 0), angVel(i, 1), angVel(i, 2)};
323 g.angVelPred =
F3{angVelPred(i, 0), angVelPred(i, 1), angVelPred(i, 2)};
324 g.invInertia =
F3{invInertia(i, 0), invInertia(i, 1), invInertia(i, 2)};
325 g.quat =
F4{quat(i, 0), quat(i, 1), quat(i, 2), quat(i, 3)};
326 g.quatPred =
F4{quatPred(i, 0), quatPred(i, 1), quatPred(i, 2), quatPred(i, 3)};
328 g.invMass = invMass(i);
329 g.shape = shapeId(i);
331 g.material = materialId(i);
332 g.grounded = grounded(i);
333 g.orphan = orphan(i);
334 g.orphanVPeak = orphanVPeak(i);
342 Kokkos::View<unsigned char*, CpMem> materialId,
343 Kokkos::View<unsigned char*, CpMem> grounded,
Vf orphan,
344 Vf orphanVPeak, peclet::core::View<MpiGatherPack> ghost,
345 const GhostRange& ghosts) {
346 const Vi slot = ghosts.slot;
347 const int no = ghosts.no;
348 const int ng = ghosts.ng;
349 const V3 vel = body.vel;
350 const V3 velPred = pred.velPred;
351 const V3 angVel = body.angVel;
352 const V3 angVelPred = pred.angVelPred;
353 const V3 invInertia = body.invInertia;
354 const V4 quat = body.quat;
355 const V4 quatPred = pred.quatPred;
356 const Vf scale = body.scale;
357 const Vf invMass = body.invMass;
358 const Vi shapeId = body.shapeId;
359 Kokkos::parallel_for(
360 "peclet::dem::halo::unpackGather", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
361 const MpiGatherPack p = ghost(g);
362 const int s = no + slot(g);
366 velPred(s, 0) = p.velPred.x;
367 velPred(s, 1) = p.velPred.y;
368 velPred(s, 2) = p.velPred.z;
369 angVel(s, 0) = p.angVel.x;
370 angVel(s, 1) = p.angVel.y;
371 angVel(s, 2) = p.angVel.z;
372 angVelPred(s, 0) = p.angVelPred.x;
373 angVelPred(s, 1) = p.angVelPred.y;
374 angVelPred(s, 2) = p.angVelPred.z;
375 invInertia(s, 0) = p.invInertia.x;
376 invInertia(s, 1) = p.invInertia.y;
377 invInertia(s, 2) = p.invInertia.z;
378 quat(s, 0) = p.quat.x;
379 quat(s, 1) = p.quat.y;
380 quat(s, 2) = p.quat.z;
381 quat(s, 3) = p.quat.w;
382 quatPred(s, 0) = p.quatPred.x;
383 quatPred(s, 1) = p.quatPred.y;
384 quatPred(s, 2) = p.quatPred.z;
385 quatPred(s, 3) = p.quatPred.w;
387 invMass(s) = p.invMass;
388 shapeId(s) = p.shape;
391 materialId(s) = p.material;
392 grounded(s) = p.grounded;
393 orphan(s) = p.orphan;
394 orphanVPeak(s) = p.orphanVPeak;
405struct VelocityPhaseViews {
413inline void haloMarkVelocityBaseline(
const VelocityPhaseViews& vel,
const VelocityPhaseViews& base,
414 const GhostRange& ghosts) {
415 const Vi slot = ghosts.slot;
416 const int no = ghosts.no;
417 const int ng = ghosts.ng;
418 const V3 velPred = vel.velPred;
419 const V3 angVelPred = vel.angVelPred;
420 const Vf orphan = vel.orphan;
421 const Vf orphanPeak = vel.orphanPeak;
422 const V3 baseV = base.velPred;
423 const V3 baseW = base.angVelPred;
424 const Vf baseO = base.orphan;
425 const Vf basePk = base.orphanPeak;
426 Kokkos::parallel_for(
427 "peclet::dem::halo::markVelBase", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
428 const int s = no + slot(g);
429 for (
int d = 0; d < 3; ++d) {
430 baseV(g, d) = velPred(s, d);
431 baseW(g, d) = angVelPred(s, d);
433 baseO(g) = orphan(s);
434 basePk(g) = orphanPeak(s);
439inline void haloPackVelocityIncrement(
const VelocityPhaseViews& vel,
const VelocityPhaseViews& base,
440 peclet::core::View<VelocityIncrement> inc,
441 const GhostRange& ghosts) {
442 const Vi slot = ghosts.slot;
443 const int no = ghosts.no;
444 const int ng = ghosts.ng;
445 const V3 velPred = vel.velPred;
446 const V3 angVelPred = vel.angVelPred;
447 const Vf orphan = vel.orphan;
448 const Vf orphanPeak = vel.orphanPeak;
449 const V3 baseV = base.velPred;
450 const V3 baseW = base.angVelPred;
451 const Vf baseO = base.orphan;
452 const Vf basePk = base.orphanPeak;
453 Kokkos::parallel_for(
454 "peclet::dem::halo::packVelInc", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
455 const int s = no + slot(g);
457 for (
int d = 0; d < 3; ++d) {
458 r.v[d] = velPred(s, d) - baseV(g, d);
459 r.w[d] = angVelPred(s, d) - baseW(g, d);
461 r.orphan = orphan(s) - baseO(g);
462 r.orphanPeak = orphanPeak(s) > basePk(g) ? orphanPeak(s) : 0.0f;
467inline void haloApplyVelocityIncrement(
const VelocityPhaseViews& vel,
468 peclet::core::View<VelocityIncrement> ownedInc,
int no) {
469 const V3 velPred = vel.velPred;
470 const V3 angVelPred = vel.angVelPred;
471 const Vf orphan = vel.orphan;
472 const Vf orphanPeak = vel.orphanPeak;
473 Kokkos::parallel_for(
474 "peclet::dem::halo::applyVelInc", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int i) {
475 const VelocityIncrement r = ownedInc(i);
476 for (
int d = 0; d < 3; ++d) {
477 velPred(i, d) += r.v[d];
478 angVelPred(i, d) += r.w[d];
480 orphan(i) = Kokkos::fmax(0.0f, orphan(i) + r.orphan);
481 orphanPeak(i) = Kokkos::fmax(orphanPeak(i), r.orphanPeak);
484inline void haloMarkPositionBaseline(
V3 posPred,
V3 baseX,
const GhostRange& ghosts) {
485 const Vi slot = ghosts.slot;
486 const int no = ghosts.no;
487 const int ng = ghosts.ng;
488 Kokkos::parallel_for(
489 "peclet::dem::halo::markPosBase", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
490 const int s = no + slot(g);
491 for (
int d = 0; d < 3; ++d)
492 baseX(g, d) = posPred(s, d);
497inline void haloPackPositionIncrement(
V3 posPred,
V3 baseX,
498 peclet::core::View<PositionIncrement> inc,
499 const GhostRange& ghosts) {
500 const Vi slot = ghosts.slot;
501 const int no = ghosts.no;
502 const int ng = ghosts.ng;
503 Kokkos::parallel_for(
504 "peclet::dem::halo::packPosInc", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
505 const int s = no + slot(g);
507 for (
int d = 0; d < 3; ++d)
508 r.x[d] = posPred(s, d) - baseX(g, d);
512inline void haloApplyPositionIncrement(
V3 posPred, peclet::core::View<PositionIncrement> ownedInc,
514 Kokkos::parallel_for(
515 "peclet::dem::halo::applyPosInc", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int i) {
516 const PositionIncrement r = ownedInc(i);
517 for (
int d = 0; d < 3; ++d)
518 posPred(i, d) += r.x[d];
522inline void haloPackGhostColumn(Kokkos::View<
float* [2],
CpMem> pf,
523 peclet::core::View<float> ghostVal,
const GhostRange& ghosts) {
524 const Vi slot = ghosts.slot;
525 const int no = ghosts.no;
526 const int ng = ghosts.ng;
527 Kokkos::parallel_for(
528 "peclet::dem::halo::packGhostCol", Kokkos::RangePolicy<CpExec>(0, ng),
529 KOKKOS_LAMBDA(
int g) { ghostVal(g) = pf(no + slot(g), 1); });
531inline void haloAddOwnedColumn(Kokkos::View<
float* [2],
CpMem> pf,
532 peclet::core::View<float> ownedVal,
int no) {
533 Kokkos::parallel_for(
534 "peclet::dem::halo::addOwnedCol", Kokkos::RangePolicy<CpExec>(0, no),
535 KOKKOS_LAMBDA(
int i) { pf(i, 1) += ownedVal(i); });
537inline void haloPackOwnedColumn(Kokkos::View<
float* [2],
CpMem> pf,
538 peclet::core::View<float> ownedVal,
int no) {
539 Kokkos::parallel_for(
540 "peclet::dem::halo::packOwnedCol", Kokkos::RangePolicy<CpExec>(0, no),
541 KOKKOS_LAMBDA(
int i) { ownedVal(i) = pf(i, 1); });
543inline void haloUnpackGhostColumn(Kokkos::View<
float* [2],
CpMem> pf,
544 peclet::core::View<float> ghostVal,
const GhostRange& ghosts) {
545 const Vi slot = ghosts.slot;
546 const int no = ghosts.no;
547 const int ng = ghosts.ng;
548 Kokkos::parallel_for(
549 "peclet::dem::halo::unpackGhostCol", Kokkos::RangePolicy<CpExec>(0, ng),
550 KOKKOS_LAMBDA(
int g) { pf(no + slot(g), 1) = ghostVal(g); });
554inline void haloPackGhostCount(
Vi counts, peclet::core::View<int> ghostCnt,
555 const GhostRange& ghosts) {
556 const Vi slot = ghosts.slot;
557 const int no = ghosts.no;
558 const int ng = ghosts.ng;
559 Kokkos::parallel_for(
560 "peclet::dem::halo::packGhostCount", Kokkos::RangePolicy<CpExec>(0, ng),
561 KOKKOS_LAMBDA(
int g) { ghostCnt(g) = counts(no + slot(g)); });
563inline void haloAddPackOwnedCount(
Vi counts, peclet::core::View<int> ownedCnt,
int no) {
564 Kokkos::parallel_for(
565 "peclet::dem::halo::addPackOwnedCount", Kokkos::RangePolicy<CpExec>(0, no),
566 KOKKOS_LAMBDA(
int i) {
567 counts(i) += ownedCnt(i);
568 ownedCnt(i) = counts(i);
571inline void haloUnpackGhostCount(
Vi counts, peclet::core::View<int> ghostCnt,
572 const GhostRange& ghosts) {
573 const Vi slot = ghosts.slot;
574 const int no = ghosts.no;
575 const int ng = ghosts.ng;
576 Kokkos::parallel_for(
577 "peclet::dem::halo::unpackGhostCount", Kokkos::RangePolicy<CpExec>(0, ng),
578 KOKKOS_LAMBDA(
int g) { counts(no + slot(g)) = ghostCnt(g); });
584struct VelocityState {
588struct PositionState {
595inline void haloPackVelocityState(
const BodyPred& pred, peclet::core::View<VelocityState> owned,
596 int n,
Vi rows = {}) {
597 const V3 velPred = pred.velPred;
598 const V3 angVelPred = pred.angVelPred;
599 const bool list = rows.extent(0) > 0;
600 Kokkos::parallel_for(
601 "peclet::dem::halo::packVelState", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(
int j) {
602 const int i = list ? rows(j) : j;
604 for (
int d = 0; d < 3; ++d) {
605 r.v[d] = velPred(i, d);
606 r.w[d] = angVelPred(i, d);
611inline void haloUnpackVelocityState(
const BodyPred& pred, peclet::core::View<VelocityState> ghost,
612 const GhostRange& ghosts) {
613 const Vi slot = ghosts.slot;
614 const int no = ghosts.no;
615 const int ng = ghosts.ng;
616 const V3 velPred = pred.velPred;
617 const V3 angVelPred = pred.angVelPred;
618 Kokkos::parallel_for(
619 "peclet::dem::halo::unpackVelState", Kokkos::RangePolicy<CpExec>(0, ng),
620 KOKKOS_LAMBDA(
int g) {
621 const VelocityState r = ghost(g);
622 const int s = no + slot(g);
623 for (
int d = 0; d < 3; ++d) {
624 velPred(s, d) = r.v[d];
625 angVelPred(s, d) = r.w[d];
629inline void haloPackPositionState(
const BodyPred& pred, peclet::core::View<PositionState> owned,
630 int n,
Vi rows = {}) {
631 const V3 posPred = pred.posPred;
632 const V4 quatPred = pred.quatPred;
633 const bool list = rows.extent(0) > 0;
634 Kokkos::parallel_for(
635 "peclet::dem::halo::packPosState", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(
int j) {
636 const int i = list ? rows(j) : j;
638 for (
int d = 0; d < 3; ++d)
639 r.x[d] = posPred(i, d);
640 for (
int d = 0; d < 4; ++d)
641 r.q[d] = quatPred(i, d);
646inline void haloUnpackPositionState(
const BodyPred& pred, peclet::core::View<PositionState> ghost,
647 peclet::core::View<F3> shift,
const GhostRange& ghosts) {
648 const Vi slot = ghosts.slot;
649 const int no = ghosts.no;
650 const int ng = ghosts.ng;
651 const V3 posPred = pred.posPred;
652 const V4 quatPred = pred.quatPred;
653 Kokkos::parallel_for(
654 "peclet::dem::halo::unpackPosState", Kokkos::RangePolicy<CpExec>(0, ng),
655 KOKKOS_LAMBDA(
int g) {
656 const PositionState r = ghost(g);
657 const F3 sh = shift(g);
658 const int s = no + slot(g);
659 posPred(s, 0) = r.x[0] + sh.x;
660 posPred(s, 1) = r.x[1] + sh.y;
661 posPred(s, 2) = r.x[2] + sh.z;
662 for (
int d = 0; d < 4; ++d)
663 quatPred(s, d) = r.q[d];
675struct OpeningIncrement {
683KOKKOS_INLINE_FUNCTION OpeningIncrement operator+(
const OpeningIncrement& a,
684 const OpeningIncrement& b) {
686 for (
int d = 0; d < 3; ++d) {
687 r.v[d] = a.v[d] + b.v[d];
688 r.w[d] = a.w[d] + b.w[d];
690 r.orphan = a.orphan + b.orphan;
691 r.orphanPeak = a.orphanPeak > b.orphanPeak ? a.orphanPeak : b.orphanPeak;
692 r.aVel = a.aVel + b.aVel;
693 r.aPos = a.aPos + b.aPos;
706struct OpeningStateNoRot {
715struct VelocityStateB {
721struct VelocityStateBNoRot {
727inline void haloPackOpeningIncrement(
const VelocityPhaseViews& vel,
const VelocityPhaseViews& base,
728 Vi aVel,
Vi aPos, peclet::core::View<OpeningIncrement> inc,
729 const GhostRange& ghosts) {
730 const Vi slot = ghosts.slot;
731 const int no = ghosts.no;
732 const int ng = ghosts.ng;
733 const V3 velPred = vel.velPred;
734 const V3 angVelPred = vel.angVelPred;
735 const Vf orphan = vel.orphan;
736 const Vf orphanPeak = vel.orphanPeak;
737 const V3 baseV = base.velPred;
738 const V3 baseW = base.angVelPred;
739 const Vf baseO = base.orphan;
740 const Vf basePk = base.orphanPeak;
741 Kokkos::parallel_for(
742 "peclet::dem::halo::packOpenInc", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
743 const int s = no + slot(g);
745 for (
int d = 0; d < 3; ++d) {
746 r.v[d] = velPred(s, d) - baseV(g, d);
747 r.w[d] = angVelPred(s, d) - baseW(g, d);
749 r.orphan = orphan(s) - baseO(g);
750 r.orphanPeak = orphanPeak(s) > basePk(g) ? orphanPeak(s) : 0.0f;
757inline void haloApplyOpening(
const VelocityPhaseViews& vel,
Vi aVel,
Vi aPos,
Vi kVel,
Vi kPos,
758 peclet::core::View<OpeningIncrement> ownedInc,
int no) {
759 const V3 velPred = vel.velPred;
760 const V3 angVelPred = vel.angVelPred;
761 const Vf orphan = vel.orphan;
762 const Vf orphanPeak = vel.orphanPeak;
763 Kokkos::parallel_for(
764 "peclet::dem::halo::applyOpen", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int i) {
765 const OpeningIncrement r = ownedInc(i);
766 for (
int d = 0; d < 3; ++d) {
767 velPred(i, d) += r.v[d];
768 angVelPred(i, d) += r.w[d];
770 orphan(i) = Kokkos::fmax(0.0f, orphan(i) + r.orphan);
771 orphanPeak(i) = Kokkos::fmax(orphanPeak(i), r.orphanPeak);
772 const int kv = aVel(i) + r.aVel, kp = aPos(i) + r.aPos;
773 kVel(i) = kv > 1 ? kv : 1;
774 kPos(i) = kp > 1 ? kp : 1;
778inline void haloPackOpeningState(
const VelocityPhaseViews& vel,
Vi kVel,
Vi kPos,
779 peclet::core::View<S> owned,
int no) {
780 const V3 velPred = vel.velPred;
781 const V3 angVelPred = vel.angVelPred;
782 const Vf orphan = vel.orphan;
783 const Vf orphanPeak = vel.orphanPeak;
784 Kokkos::parallel_for(
785 "peclet::dem::halo::packOpenState", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int i) {
786 const V3& wv = angVelPred;
788 for (
int d = 0; d < 3; ++d)
789 r.v[d] = velPred(i, d);
790 if constexpr (std::is_same_v<S, OpeningState>) {
791 for (
int d = 0; d < 3; ++d)
795 r.peak = orphanPeak(i);
802inline void haloUnpackOpeningState(
const VelocityPhaseViews& vel,
Vi kVel,
Vi kPos,
803 peclet::core::View<S> ghost,
const GhostRange& ghosts) {
804 const Vi slot = ghosts.slot;
805 const int no = ghosts.no;
806 const int ng = ghosts.ng;
807 const V3 velPred = vel.velPred;
808 const V3 angVelPred = vel.angVelPred;
809 const Vf orphan = vel.orphan;
810 const Vf orphanPeak = vel.orphanPeak;
811 Kokkos::parallel_for(
812 "peclet::dem::halo::unpackOpenState", Kokkos::RangePolicy<CpExec>(0, ng),
813 KOKKOS_LAMBDA(
int g) {
814 const V3& wv = angVelPred;
815 const S r = ghost(g);
816 const int s = no + slot(g);
817 for (
int d = 0; d < 3; ++d)
818 velPred(s, d) = r.v[d];
819 if constexpr (std::is_same_v<S, OpeningState>) {
820 for (
int d = 0; d < 3; ++d)
824 orphanPeak(s) = r.peak;
830inline void haloPackVelocityStateB(
const VelocityPhaseViews& vel, peclet::core::View<S> owned,
831 int no,
Vi rows = {}) {
832 const V3 velPred = vel.velPred;
833 const V3 angVelPred = vel.angVelPred;
834 const Vf orphan = vel.orphan;
835 const Vf orphanPeak = vel.orphanPeak;
836 const bool list = rows.extent(0) > 0;
837 Kokkos::parallel_for(
838 "peclet::dem::halo::packVelStateB", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int j) {
839 const int i = list ? rows(j) : j;
840 const V3& wv = angVelPred;
842 for (
int d = 0; d < 3; ++d)
843 r.v[d] = velPred(i, d);
844 if constexpr (std::is_same_v<S, VelocityStateB>) {
845 for (
int d = 0; d < 3; ++d)
849 r.peak = orphanPeak(i);
854inline void haloUnpackVelocityStateB(
const VelocityPhaseViews& vel, peclet::core::View<S> ghost,
855 const GhostRange& ghosts) {
856 const Vi slot = ghosts.slot;
857 const int no = ghosts.no;
858 const int ng = ghosts.ng;
859 const V3 velPred = vel.velPred;
860 const V3 angVelPred = vel.angVelPred;
861 const Vf orphan = vel.orphan;
862 const Vf orphanPeak = vel.orphanPeak;
863 Kokkos::parallel_for(
864 "peclet::dem::halo::unpackVelStateB", Kokkos::RangePolicy<CpExec>(0, ng),
865 KOKKOS_LAMBDA(
int g) {
866 const V3& wv = angVelPred;
867 const S r = ghost(g);
868 const int s = no + slot(g);
869 for (
int d = 0; d < 3; ++d)
870 velPred(s, d) = r.v[d];
871 if constexpr (std::is_same_v<S, VelocityStateB>) {
872 for (
int d = 0; d < 3; ++d)
876 orphanPeak(s) = r.peak;
883inline void haloMarkOwnerVelocitySeeds(
V3 velPred,
V3 angVelPred,
Vf orphan,
V3 seedV,
V3 seedW,
884 Vf seedO,
int no,
Vi rows = {}) {
885 const bool list = rows.extent(0) > 0;
886 Kokkos::parallel_for(
887 "peclet::dem::halo::markOwnerVelSeed", Kokkos::RangePolicy<CpExec>(0, no),
888 KOKKOS_LAMBDA(
int j) {
889 const int i = list ? rows(j) : j;
890 for (
int d = 0; d < 3; ++d) {
891 seedV(i, d) = velPred(i, d);
892 seedW(i, d) = angVelPred(i, d);
894 seedO(i) = orphan(i);
897inline void haloMarkOwnerPositionSeeds(
V3 posPred,
V3 seedX,
int no,
Vi rows = {}) {
898 const bool list = rows.extent(0) > 0;
899 Kokkos::parallel_for(
900 "peclet::dem::halo::markOwnerPosSeed", Kokkos::RangePolicy<CpExec>(0, no),
901 KOKKOS_LAMBDA(
int j) {
902 const int i = list ? rows(j) : j;
903 for (
int d = 0; d < 3; ++d)
904 seedX(i, d) = posPred(i, d);
909inline void haloSetOrphanShares(
Vf orphan,
Vi kVel,
int n) {
910 Kokkos::parallel_for(
911 "peclet::dem::halo::orphanShares", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(
int q) {
912 const int k = kVel(q);
914 orphan(q) = orphan(q) /
static_cast<float>(k);
919inline int haloBuildMRows(
Vi k, Kokkos::View<const unsigned char*, CpMem> mark,
Vi rows,
int no) {
921 Kokkos::parallel_scan(
922 "peclet::dem::halo::mRows", Kokkos::RangePolicy<CpExec>(0, no),
923 KOKKOS_LAMBDA(
int i,
int& run,
const bool final) {
924 if (mark(i) != 0 || k(i) > 1) {
947KOKKOS_INLINE_FUNCTION
float payloadLin(
const F3& r,
int d) {
948 return d == 0 ? r.x : (d == 1 ? r.y : r.z);
950KOKKOS_INLINE_FUNCTION
void setPayloadLin(
F3& r,
int d,
float x) {
951 (d == 0 ? r.x : (d == 1 ? r.y : r.z)) = x;
954KOKKOS_INLINE_FUNCTION
float payloadLin(
const S& r,
int d) {
955 if constexpr (std::is_same_v<S, PositionState>)
961KOKKOS_INLINE_FUNCTION
void setPayloadLin(S& r,
int d,
float x) {
962 if constexpr (std::is_same_v<S, PositionState>)
968inline constexpr bool kPayloadW =
969 std::is_same_v<S, VelocityState> || std::is_same_v<S, VelocityStateB>;
971inline constexpr bool kPayloadB =
972 std::is_same_v<S, VelocityStateB> || std::is_same_v<S, VelocityStateBNoRot>;
977inline void haloVelocityMPackZero(
const VelocityPhaseViews& vel,
const VelocityPhaseViews& base,
978 Vi aVel, peclet::core::View<VelocityIncrement> inc,
979 const GhostRange& ghosts,
980 peclet::core::View<VelocityIncrement> ownedInc,
Vi rows,
982 const Vi slot = ghosts.slot;
983 const int no = ghosts.no;
984 const int ng = ghosts.ng;
985 const V3 velPred = vel.velPred;
986 const V3 angVelPred = vel.angVelPred;
987 const Vf orphan = vel.orphan;
988 const Vf orphanPeak = vel.orphanPeak;
989 const V3 baseV = base.velPred;
990 const V3 baseW = base.angVelPred;
991 const Vf baseO = base.orphan;
992 const Vf basePk = base.orphanPeak;
993 Kokkos::parallel_for(
994 "peclet::dem::halo::velMPackZero", Kokkos::RangePolicy<CpExec>(0, ng + nRows),
995 KOKKOS_LAMBDA(
int j) {
997 ownedInc(rows(j - ng)) = VelocityIncrement{};
1001 const int s = no + slot(g);
1002 const float f =
static_cast<float>(aVel(s));
1003 VelocityIncrement r;
1004 for (
int d = 0; d < 3; ++d) {
1005 r.v[d] = f * (velPred(s, d) - baseV(g, d));
1006 r.w[d] = f * (angVelPred(s, d) - baseW(g, d));
1008 r.orphan = f * (orphan(s) - baseO(g));
1009 r.orphanPeak = (f > 0.0f && orphanPeak(s) > basePk(g)) ? orphanPeak(s) : 0.0f;
1012 bool moved = orphan(s) != baseO(g);
1013 for (
int d = 0; d < 3; ++d)
1014 moved = moved || velPred(s, d) != baseV(g, d) || angVelPred(s, d) != baseW(g, d);
1017 "peclet::dem rank-level M: an inactive velocity slot (a = 0) was written");
1030inline void haloVelocityMApply(
const VelocityPhaseViews& vel,
V3 seedV,
V3 seedW,
Vf seedO,
Vi aVel,
1031 Vi kVel, peclet::core::View<VelocityIncrement> ownedInc,
1032 Kokkos::View<int, CpMem> clamps,
1033 Kokkos::View<float, CpMem> consensus,
Vi rows,
int nRows,
1034 Kokkos::View<const unsigned char*, CpMem> iface,
1035 peclet::core::View<S> out,
bool shares) {
1036 const V3 velPred = vel.velPred;
1037 const V3 angVelPred = vel.angVelPred;
1038 const Vf orphan = vel.orphan;
1039 const Vf orphanPeak = vel.orphanPeak;
1040 Kokkos::parallel_for(
1041 "peclet::dem::halo::velMApply", Kokkos::RangePolicy<CpExec>(0, nRows), KOKKOS_LAMBDA(
int j) {
1042 const V3& wv = angVelPred;
1043 const Vf& ov = orphan;
1044 const Vf& pv = orphanPeak;
1045 const int i = rows(j);
1046 const VelocityIncrement r = ownedInc(i);
1047 const int k = kVel(i);
1050 for (
int d = 0; d < 3; ++d) {
1051 velPred(i, d) += r.v[d];
1052 angVelPred(i, d) += r.w[d];
1054 pre = orphan(i) + r.orphan;
1056 const float kf =
static_cast<float>(k), af =
static_cast<float>(aVel(i));
1058 for (
int d = 0; d < 3; ++d) {
1059#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 2
1060 const float own = velPred(i, d) - seedV(i, d),
1061 mean = af * own + r.v[d];
1063 const float own = velPred(i, d) - seedV(i, d), mean = (af * own + r.v[d]) / kf;
1065 c2 += (mean - own) * (mean - own);
1066 velPred(i, d) = seedV(i, d) + mean;
1067 angVelPred(i, d) = seedW(i, d) + (af * (angVelPred(i, d) - seedW(i, d)) + r.w[d]) / kf;
1069 if (af > 0.0f && c2 > 0.0f)
1070 Kokkos::atomic_max(&consensus(), Kokkos::sqrt(c2));
1071 const float B = seedO(i);
1072 pre = B + af * (orphan(i) - B / kf) + r.orphan;
1074 if (!(pre >= -1e-6f * Kokkos::fmax(B, 1e-30f)))
1075 Kokkos::abort(
"peclet::dem rank-level M: the orphan balance was overdrawn");
1079 Kokkos::atomic_add(&clamps(), 1);
1080 orphan(i) = Kokkos::fmax(0.0f, pre);
1081 orphanPeak(i) = Kokkos::fmax(orphanPeak(i), r.orphanPeak);
1082 if (iface(i) != 0) {
1084 for (
int d = 0; d < 3; ++d)
1085 setPayloadLin(p, d, velPred(i, d));
1086 if constexpr (kPayloadW<S>) {
1087 for (
int d = 0; d < 3; ++d)
1090 if constexpr (kPayloadB<S>) {
1096 for (
int d = 0; d < 3; ++d) {
1097 seedV(i, d) = velPred(i, d);
1098 seedW(i, d) = angVelPred(i, d);
1100 seedO(i) = orphan(i);
1101 if (shares && k > 1)
1102 orphan(i) = orphan(i) /
static_cast<float>(k);
1109inline void haloVelocityMUnpack(
const VelocityPhaseViews& vel, peclet::core::View<S> ghost,
1110 const VelocityPhaseViews& base,
Vi aVel,
Vi kVel,
1111 peclet::core::View<VelocityIncrement> inc,
1112 Kokkos::View<float, CpMem> consensus,
const GhostRange& ghosts,
1114 const Vi slot = ghosts.slot;
1115 const int no = ghosts.no;
1116 const int ng = ghosts.ng;
1117 const V3 velPred = vel.velPred;
1118 const V3 angVelPred = vel.angVelPred;
1119 const Vf orphan = vel.orphan;
1120 const Vf orphanPeak = vel.orphanPeak;
1121 const V3 baseV = base.velPred;
1122 const V3 baseW = base.angVelPred;
1123 const Vf baseO = base.orphan;
1124 const Vf basePk = base.orphanPeak;
1125 Kokkos::parallel_for(
1126 "peclet::dem::halo::velMUnpack", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
1127 const V3& wv = angVelPred;
1128 const Vf& ov = orphan;
1129 const Vf& pv = orphanPeak;
1130 const S p = ghost(g);
1131 const int s = no + slot(g);
1132 for (
int d = 0; d < 3; ++d)
1133 velPred(s, d) = payloadLin(p, d);
1134 if constexpr (kPayloadW<S>) {
1135 for (
int d = 0; d < 3; ++d)
1138 if constexpr (kPayloadB<S>) {
1142 const int k = kVel(s);
1143 if (aVel(s) > 0 && k > 1) {
1144 const float af =
static_cast<float>(aVel(s));
1145 const VelocityIncrement r = inc(g);
1147 for (
int d = 0; d < 3; ++d) {
1148 const float e = (velPred(s, d) - baseV(g, d)) - r.v[d] / af;
1152 Kokkos::atomic_max(&consensus(), Kokkos::sqrt(c2));
1154 if (shares && k > 1)
1155 orphan(s) = orphan(s) /
static_cast<float>(k);
1156 for (
int d = 0; d < 3; ++d) {
1157 baseV(g, d) = velPred(s, d);
1158 baseW(g, d) = angVelPred(s, d);
1160 baseO(g) = orphan(s);
1161 basePk(g) = orphanPeak(s);
1166inline void haloRestoreOrphanBalance(
Vf orphan,
Vf seedO,
Vi aVel,
Vi kVel,
int no,
Vi rows = {}) {
1167 const bool list = rows.extent(0) > 0;
1168 Kokkos::parallel_for(
1169 "peclet::dem::halo::restoreOrphan", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int j) {
1170 const int i = list ? rows(j) : j;
1171 const int k = kVel(i);
1174 const float kf =
static_cast<float>(k), B = seedO(i);
1175 orphan(i) = B +
static_cast<float>(aVel(i)) * (orphan(i) - B / kf);
1179inline void haloPositionMPackZero(
V3 posPred,
V3 baseX,
Vi aPos,
1180 peclet::core::View<PositionIncrement> inc,
1181 const GhostRange& ghosts,
1182 peclet::core::View<PositionIncrement> ownedInc,
Vi rows,
1184 const Vi slot = ghosts.slot;
1185 const int no = ghosts.no;
1186 const int ng = ghosts.ng;
1187 Kokkos::parallel_for(
1188 "peclet::dem::halo::posMPackZero", Kokkos::RangePolicy<CpExec>(0, ng + nRows),
1189 KOKKOS_LAMBDA(
int j) {
1191 ownedInc(rows(j - ng)) = PositionIncrement{};
1195 const int s = no + slot(g);
1196 const float f =
static_cast<float>(aPos(s));
1197 PositionIncrement r;
1198 for (
int d = 0; d < 3; ++d)
1199 r.x[d] = f * (posPred(s, d) - baseX(g, d));
1201 if (aPos(s) == 0 && (posPred(s, 0) != baseX(g, 0) || posPred(s, 1) != baseX(g, 1) ||
1202 posPred(s, 2) != baseX(g, 2)))
1203 Kokkos::abort(
"peclet::dem rank-level M: an inactive position slot (a = 0) was written");
1210inline void haloPositionMApply(
const BodyPred& pred,
V3 seedX,
Vi aPos,
Vi kPos,
1211 peclet::core::View<PositionIncrement> ownedInc,
1212 Kokkos::View<float, CpMem> consensus,
Vi rows,
int nRows,
1213 Kokkos::View<const unsigned char*, CpMem> iface,
1214 peclet::core::View<S> out) {
1215 const V3 posPred = pred.posPred;
1216 const V4 quatPred = pred.quatPred;
1217 Kokkos::parallel_for(
1218 "peclet::dem::halo::posMApply", Kokkos::RangePolicy<CpExec>(0, nRows), KOKKOS_LAMBDA(
int j) {
1219 const V4& qv = quatPred;
1220 const int i = rows(j);
1221 const PositionIncrement r = ownedInc(i);
1222 const int k = kPos(i);
1224 for (
int d = 0; d < 3; ++d)
1225 posPred(i, d) += r.x[d];
1227 const float kf =
static_cast<float>(k), af =
static_cast<float>(aPos(i));
1229 for (
int d = 0; d < 3; ++d) {
1230 const float own = posPred(i, d) - seedX(i, d), mean = (af * own + r.x[d]) / kf;
1231 c2 += (mean - own) * (mean - own);
1232 posPred(i, d) = seedX(i, d) + mean;
1234 if (af > 0.0f && c2 > 0.0f)
1235 Kokkos::atomic_max(&consensus(), Kokkos::sqrt(c2));
1237 if (iface(i) != 0) {
1239 for (
int d = 0; d < 3; ++d)
1240 setPayloadLin(p, d, posPred(i, d));
1241 if constexpr (std::is_same_v<S, PositionState>) {
1242 for (
int d = 0; d < 4; ++d)
1247 for (
int d = 0; d < 3; ++d)
1248 seedX(i, d) = posPred(i, d);
1254inline void haloPositionMUnpack(
const BodyPred& pred, peclet::core::View<S> ghost,
1255 peclet::core::View<F3> shift,
V3 baseX,
Vi aPos,
Vi kPos,
1256 peclet::core::View<PositionIncrement> inc,
1257 Kokkos::View<float, CpMem> consensus,
const GhostRange& ghosts) {
1258 const Vi slot = ghosts.slot;
1259 const int no = ghosts.no;
1260 const int ng = ghosts.ng;
1261 const V3 posPred = pred.posPred;
1262 const V4 quatPred = pred.quatPred;
1263 Kokkos::parallel_for(
1264 "peclet::dem::halo::posMUnpack", Kokkos::RangePolicy<CpExec>(0, ng), KOKKOS_LAMBDA(
int g) {
1265 const V4& qv = quatPred;
1266 const S p = ghost(g);
1267 const F3 sh = shift(g);
1268 const int s = no + slot(g);
1269 posPred(s, 0) = payloadLin(p, 0) + sh.x;
1270 posPred(s, 1) = payloadLin(p, 1) + sh.y;
1271 posPred(s, 2) = payloadLin(p, 2) + sh.z;
1272 if constexpr (std::is_same_v<S, PositionState>) {
1273 for (
int d = 0; d < 4; ++d)
1276 if (aPos(s) > 0 && kPos(s) > 1) {
1277 const float af =
static_cast<float>(aPos(s));
1278 const PositionIncrement r = inc(g);
1280 for (
int d = 0; d < 3; ++d) {
1281 const float e = (posPred(s, d) - baseX(g, d)) - r.x[d] / af;
1285 Kokkos::atomic_max(&consensus(), Kokkos::sqrt(c2));
1287 for (
int d = 0; d < 3; ++d)
1288 baseX(g, d) = posPred(s, d);
1292inline void haloPackGhostInt(
Vi src, peclet::core::View<int> ghost,
const GhostRange& ghosts) {
1293 const Vi slot = ghosts.slot;
1294 const int no = ghosts.no;
1295 const int ng = ghosts.ng;
1296 Kokkos::parallel_for(
1297 "peclet::dem::halo::packGhostInt", Kokkos::RangePolicy<CpExec>(0, ng),
1298 KOKKOS_LAMBDA(
int g) { ghost(g) = src(no + slot(g)); });
1300inline void haloOwnerCountK(
Vi a,
Vi k, peclet::core::View<int> owned,
int no) {
1301 Kokkos::parallel_for(
1302 "peclet::dem::halo::ownerCountK", Kokkos::RangePolicy<CpExec>(0, no), KOKKOS_LAMBDA(
int i) {
1303 const int kk = a(i) + owned(i);
1304 k(i) = kk > 1 ? kk : 1;
1308inline void haloUnpackGhostInt(
Vi dst, peclet::core::View<int> ghost,
const GhostRange& ghosts) {
1309 const Vi slot = ghosts.slot;
1310 const int no = ghosts.no;
1311 const int ng = ghosts.ng;
1312 Kokkos::parallel_for(
1313 "peclet::dem::halo::unpackGhostInt", Kokkos::RangePolicy<CpExec>(0, ng),
1314 KOKKOS_LAMBDA(
int g) { dst(no + slot(g)) = ghost(g); });
1320struct PositionCountsMask {
1321 unsigned long long mask;
1325KOKKOS_INLINE_FUNCTION PositionCountsMask operator+(
const PositionCountsMask& a,
1326 const PositionCountsMask& b) {
1327 return PositionCountsMask{a.mask | b.mask, a.aPos + b.aPos, 0};
1329inline void haloPackGhostCountsMask(
Vi aVel,
Vi aPos, peclet::core::View<PositionCountsMask> ghost,
1330 const GhostRange& ghosts,
unsigned long long bit) {
1331 const Vi slot = ghosts.slot;
1332 const int no = ghosts.no;
1333 const int ng = ghosts.ng;
1334 Kokkos::parallel_for(
1335 "peclet::dem::halo::packCountsMask", Kokkos::RangePolicy<CpExec>(0, ng),
1336 KOKKOS_LAMBDA(
int g) {
1337 const int s = no + slot(g);
1338 ghost(g) = PositionCountsMask{aVel(s) > 0 ? bit : 0ull, aPos(s), 0};
1341inline void haloOwnerCountsMask(
Vi aVel,
Vi aPos,
Vi kPos,
1342 Kokkos::View<unsigned long long*, CpMem> velMask,
1343 peclet::core::View<PositionCountsMask> owned,
1344 unsigned long long bit,
int no) {
1345 Kokkos::parallel_for(
1346 "peclet::dem::halo::ownerCountsMask", Kokkos::RangePolicy<CpExec>(0, no),
1347 KOKKOS_LAMBDA(
int i) {
1348 const PositionCountsMask r = owned(i);
1349 const int kk = aPos(i) + r.aPos;
1350 kPos(i) = kk > 1 ? kk : 1;
1351 const unsigned long long m = r.mask | (aVel(i) > 0 ? bit : 0ull);
1353 owned(i) = PositionCountsMask{m, kPos(i), 0};
1356inline void haloUnpackCountsMask(
Vi kPos, Kokkos::View<unsigned long long*, CpMem> velMask,
1357 peclet::core::View<PositionCountsMask> ghost,
1358 const GhostRange& ghosts) {
1359 const Vi slot = ghosts.slot;
1360 const int no = ghosts.no;
1361 const int ng = ghosts.ng;
1362 Kokkos::parallel_for(
1363 "peclet::dem::halo::unpackCountsMask", Kokkos::RangePolicy<CpExec>(0, ng),
1364 KOKKOS_LAMBDA(
int g) {
1365 const int s = no + slot(g);
1366 kPos(s) = ghost(g).aPos;
1367 velMask(s) = ghost(g).mask;
1373inline void haloMapVelocitySlots(
Vi realIndices,
Vi canon,
int no,
int ng) {
1374 Kokkos::parallel_for(
1375 "peclet::dem::halo::mapVelSlots", Kokkos::RangePolicy<CpExec>(0, ng),
1376 KOKKOS_LAMBDA(
int j) { realIndices(no + j) = canon(j); });
1392 void initMpi(std::array<double, 3> origin, std::array<double, 3> size, std::array<long, 3> gsize,
1393 std::array<bool, 3> periodic, MPI_Comm comm) {
1396 MPI_Comm_rank(comm_, &rank_);
1397 MPI_Comm_size(comm_, &sz);
1398 dec_.init(
static_cast<std::size_t
>(sz), peclet::core::IVec<3>{gsize[0], gsize[1], gsize[2]});
1399 peclet::core::halo::DomainMap<3> map;
1400 for (
int i = 0; i < 3; ++i) {
1401 map.origin[i] = origin[i];
1402 map.cellSize[i] = size[i] /
static_cast<double>(gsize[i]);
1403 map.periodic[i] = periodic[i];
1406 mig_.init(dec_, rank_, map, comm_);
1413 void initMpi(
const peclet::core::decomp::BlockDecomposer<3>& dec, std::array<double, 3> origin,
1414 std::array<double, 3> size, std::array<bool, 3> periodic, MPI_Comm comm) {
1416 MPI_Comm_rank(comm_, &rank_);
1418 const auto& gs = dec.globalSize();
1419 peclet::core::halo::DomainMap<3> map;
1420 for (
int i = 0; i < 3; ++i) {
1421 map.origin[i] = origin[i];
1422 map.cellSize[i] = size[i] /
static_cast<double>(gs[i]);
1423 map.periodic[i] = periodic[i];
1426 mig_.init(dec_, rank_, map, comm_);
1430 const peclet::core::decomp::BlockDecomposer<3>& decomposer()
const {
return dec_; }
1435 int rankColor()
const {
return rankColor_; }
1436 int numRankColors()
const {
return numRankColors_; }
1438 bool inited()
const {
return inited_; }
1439 int rank()
const {
return rank_; }
1440 int numGhost()
const {
return numGhost_; }
1449 void setVerletSkin(
float skin) { verletSkin_ = skin < 0.0f ? 0.0f : skin; }
1450 float verletSkin()
const {
return verletSkin_; }
1454 void invalidateTopology() { haveTopo_ =
false; }
1457 long numRebuilds()
const {
return nRebuild_; }
1458 long numGathers()
const {
return nGather_; }
1463 int gather(Particles& P,
double rcut) {
1464 const int no = P.numReal;
1474 const double band = rcut +
static_cast<double>(verletSkin_);
1475 ensureRankColoring(band);
1477 velM_ = posM_ = orphanShares_ =
false;
1478 bool rebuild = (verletSkin_ <= 0.0f) || !haveTopo_ || (no != lastNumReal_) || band != lastBand_;
1479 if (!rebuild && maxOwnedDisplacement(P.pos, no) >= verletSkin_)
1486 if (verletSkin_ > 0.0f) {
1487 int mine = rebuild ? 1 : 0, any = 0;
1496 auto hpos = Kokkos::create_mirror_view(P.pos);
1497 Kokkos::deep_copy(hpos, P.pos);
1498 std::vector<peclet::core::Vec<3>> pv(
static_cast<std::size_t
>(no));
1505 const auto& gsz = dec_.globalSize();
1506 for (
int i = 0; i < no; ++i) {
1507 double q[3] = {hpos(i, 0), hpos(i, 1), hpos(i, 2)};
1508 for (
int d = 0; d < 3; ++d)
1509 if (!map_.periodic[d])
1510 q[d] = std::clamp(q[d], map_.origin[d],
1511 map_.origin[d] +
static_cast<double>(gsz[d]) * map_.cellSize[d]);
1512 pv[i] = peclet::core::Vec<3>{q[0], q[1], q[2]};
1522 halo_.build(pv, band,
true,
true);
1524 const int ng =
static_cast<int>(halo_.numGhost());
1531 if (no + ng > P.capacity)
1532 throw std::runtime_error(
1533 "ParticleHalo::gather: ghost overflow -- need capacity >= " + std::to_string(no + ng) +
1534 " (numReal=" + std::to_string(no) +
" + numGhost=" + std::to_string(ng) +
"), have " +
1535 std::to_string(P.capacity) +
"; increase the Simulation capacity.");
1536 const auto topo = halo_.flatten();
1538 numSend_ =
static_cast<int>(topo.sendIdx.size());
1539 allocBuffers(no, ng);
1541 const std::vector<int> hSlot = uploadGhostSlots(topo, ng);
1542 buildOwnershipMaps(topo, no, ng, hSlot);
1543 numReceived_ =
static_cast<int>(topo.numReceived);
1544 selfIdx_.assign(topo.selfIdx.begin(), topo.selfIdx.end());
1545 buildInterfaceRows(topo, no);
1547 if (verletSkin_ > 0.0f) {
1548 if (refPos_.extent(0) <
static_cast<std::size_t
>(no))
1549 refPos_ =
V3(
"peclet::dem::halo::refPos",
static_cast<std::size_t
>(P.capacity));
1550 Kokkos::deep_copy(Kokkos::subview(refPos_, std::pair<int, int>(0, no), Kokkos::ALL),
1551 Kokkos::subview(P.pos, std::pair<int, int>(0, no), Kokkos::ALL));
1559 const int ng = numGhost_;
1560 P.numParticles = no + ng;
1563 selfMapReals(P.realIndices, no);
1570 forwardPositions(P.pos);
1571 forwardPositions(P.posPred);
1575 haloPackGather(P.bodyState(), P.bodyPred(), P.gid, P.materialId, P.groundedLevel, P.bodyOrphan,
1576 P.bodyOrphanVPeak, ownedPack_, no);
1577 dev_.forward(ownedPack_, ghostPack_);
1578 haloUnpackGather(P.bodyState(), P.bodyPred(), P.realIndices, P.gid, P.materialId,
1579 P.groundedLevel, P.bodyOrphan, P.bodyOrphanVPeak, ghostPack_,
1580 GhostRange{ghostSlot_, no, ng});
1584 MPI_Comm comm()
const {
return comm_; }
1588 bool exchanges()
const {
return numGhost_ > 0 || numSend_ > 0; }
1598 int rebalance(Particles& P) {
1599 std::vector<peclet::core::Vec<3>> pos;
1600 std::vector<char> payload;
1601 packState(P, pos, payload);
1602 const std::size_t newN = peclet::core::halo::rebalanceByParticleCount(
1603 dec_, mig_, pos, payload,
sizeof(MigratePack), comm_);
1604 orderArrivalsBySource(pos, payload, newN);
1605 if ((
int)newN > P.capacity)
1606 throw std::runtime_error(
"ParticleHalo::rebalance: owned overflow -- rank received " +
1607 std::to_string(newN) +
" particles, capacity " +
1608 std::to_string(P.capacity));
1609 unpackState(P, pos, payload, newN);
1618 int migrateToBlocks(Particles& P) {
1619 std::vector<peclet::core::Vec<3>> pos;
1620 std::vector<char> payload;
1621 packState(P, pos, payload);
1622 const std::size_t newN = mig_.migrate(pos, payload,
sizeof(MigratePack));
1623 orderArrivalsBySource(pos, payload, newN);
1624 if ((
int)newN > P.capacity)
1625 throw std::runtime_error(
"ParticleHalo::migrateToBlocks: owned overflow -- rank received " +
1626 std::to_string(newN) +
" particles, capacity " +
1627 std::to_string(P.capacity));
1628 unpackState(P, pos, payload, newN);
1637 BlockBox blockBox()
const {
1638 const auto blk = dec_.block(
static_cast<std::size_t
>(rank_));
1639 const auto& gs = dec_.globalSize();
1640 float lo[3], hi[3], L[3];
1641 for (
int d = 0; d < 3; ++d) {
1642 lo[d] =
static_cast<float>(map_.origin[d] +
1643 static_cast<double>(blk.origin[d]) * map_.cellSize[d]);
1644 hi[d] =
static_cast<float>(map_.origin[d] +
static_cast<double>(blk.origin[d] + blk.size[d]) *
1646 L[d] = map_.periodic[d] ?
static_cast<float>(
static_cast<double>(gs[d]) * map_.cellSize[d])
1650 if (!map_.periodic[d]) {
1651 if (blk.origin[d] == 0)
1652 lo[d] = -std::numeric_limits<float>::infinity();
1653 if (blk.origin[d] + blk.size[d] == gs[d])
1654 hi[d] = std::numeric_limits<float>::infinity();
1657 return BlockBox{F3{lo[0], lo[1], lo[2]}, F3{hi[0], hi[1], hi[2]}, F3{L[0], L[1], L[2]}};
1662 int migrateTo(Particles& P,
const peclet::core::decomp::BlockDecomposer<3>& newDec) {
1663 std::vector<peclet::core::Vec<3>> pos;
1664 std::vector<char> payload;
1665 packState(P, pos, payload);
1669 const std::size_t newN = mig_.migrate(pos, payload,
sizeof(MigratePack));
1670 orderArrivalsBySource(pos, payload, newN);
1671 if ((
int)newN > P.capacity)
1672 throw std::runtime_error(
"ParticleHalo::migrateTo: owned overflow -- rank received " +
1673 std::to_string(newN) +
" particles, capacity " +
1674 std::to_string(P.capacity));
1675 unpackState(P, pos, payload, newN);
1686 int migrateToWeights(Particles& P,
const std::vector<peclet::core::Real>& w,
int align = 1) {
1688 MPI_Comm_size(comm_, &size);
1690 peclet::core::decomp::BlockDecomposer<3> newDec((std::size_t)size, dec_.globalSize(), w);
1691 return migrateTo(P, newDec);
1693 peclet::core::decomp::BlockDecomposer<3> newDec;
1694 newDec.init((std::size_t)size, dec_.globalSize(), w,
1695 peclet::core::IVec<3>{align, align, align});
1696 return migrateTo(P, newDec);
1702 std::vector<MigratePack> packParticleRecords(
1703 const Particles& P, std::vector<peclet::core::Vec<3>>& pos, std::vector<char>& payload,
1704 std::unordered_map<unsigned, int>& gidToLocal)
const {
1705 const int no = P.numReal;
1706 auto h_pos = Kokkos::create_mirror_view(P.pos);
1707 auto h_quat = Kokkos::create_mirror_view(P.quat);
1708 auto h_vel = Kokkos::create_mirror_view(P.vel);
1709 auto h_angVel = Kokkos::create_mirror_view(P.angVel);
1710 auto h_invI = Kokkos::create_mirror_view(P.invInertia);
1711 auto h_invM = Kokkos::create_mirror_view(P.invMass);
1712 auto h_scale = Kokkos::create_mirror_view(P.scale);
1713 auto h_tScale = Kokkos::create_mirror_view(P.targetScale);
1714 auto h_shape = Kokkos::create_mirror_view(P.shapeId);
1715 auto h_pf = Kokkos::create_mirror_view(P.planeFriction);
1716 auto h_gid = Kokkos::create_mirror_view(P.gid);
1717 auto h_mat = Kokkos::create_mirror_view(P.materialId);
1718 auto h_grd = Kokkos::create_mirror_view(P.groundedLevel);
1719 Kokkos::deep_copy(h_pos, P.pos);
1720 Kokkos::deep_copy(h_quat, P.quat);
1721 Kokkos::deep_copy(h_vel, P.vel);
1722 Kokkos::deep_copy(h_angVel, P.angVel);
1723 Kokkos::deep_copy(h_invI, P.invInertia);
1724 Kokkos::deep_copy(h_invM, P.invMass);
1725 Kokkos::deep_copy(h_scale, P.scale);
1726 Kokkos::deep_copy(h_tScale, P.targetScale);
1727 Kokkos::deep_copy(h_shape, P.shapeId);
1728 Kokkos::deep_copy(h_pf, P.planeFriction);
1729 Kokkos::deep_copy(h_gid, P.gid);
1730 Kokkos::deep_copy(h_mat, P.materialId);
1731 Kokkos::deep_copy(h_grd, P.groundedLevel);
1732 auto h_orp = Kokkos::create_mirror_view(P.bodyOrphan);
1733 auto h_ovp = Kokkos::create_mirror_view(P.bodyOrphanVPeak);
1734 Kokkos::deep_copy(h_orp, P.bodyOrphan);
1735 Kokkos::deep_copy(h_ovp, P.bodyOrphanVPeak);
1736 auto h_ef = Kokkos::create_mirror_view(P.extForce);
1737 auto h_et = Kokkos::create_mirror_view(P.extTorque);
1738 Kokkos::deep_copy(h_ef, P.extForce);
1739 Kokkos::deep_copy(h_et, P.extTorque);
1740 pos.assign((std::size_t)no, peclet::core::Vec<3>{});
1741 payload.assign((std::size_t)no *
sizeof(MigratePack), 0);
1742 std::vector<MigratePack> packs((std::size_t)no);
1743 gidToLocal.reserve((std::size_t)no * 2);
1744 for (
int i = 0; i < no; ++i) {
1745 pos[(std::size_t)i] = peclet::core::Vec<3>{h_pos(i, 0), h_pos(i, 1), h_pos(i, 2)};
1746 MigratePack& m = packs[(std::size_t)i];
1747 m.quat = F4{h_quat(i, 0), h_quat(i, 1), h_quat(i, 2), h_quat(i, 3)};
1748 m.vel = F3{h_vel(i, 0), h_vel(i, 1), h_vel(i, 2)};
1749 m.angVel = F3{h_angVel(i, 0), h_angVel(i, 1), h_angVel(i, 2)};
1750 m.invInertia = F3{h_invI(i, 0), h_invI(i, 1), h_invI(i, 2)};
1751 m.invMass = h_invM(i);
1752 m.scale = h_scale(i);
1753 m.targetScale = h_tScale(i);
1754 m.shapeId = h_shape(i);
1755 m.planeFric0 = h_pf(i, 0);
1756 m.planeFric1 = h_pf(i, 1);
1759 m.materialId = h_mat(i);
1760 m.groundedLevel = h_grd(i);
1761 m.orphan = h_orp(i);
1762 m.orphanVPeak = h_ovp(i);
1763 m.extForce = F3{h_ef(i, 0), h_ef(i, 1), h_ef(i, 2)};
1764 m.extTorque = F3{h_et(i, 0), h_et(i, 1), h_et(i, 2)};
1766 gidToLocal.emplace(
static_cast<unsigned>(h_gid(i)), i);
1774 static void carryWarmLedger(
const Particles& P, std::vector<MigratePack>& packs,
1775 const std::unordered_map<unsigned, int>& gidToLocal) {
1776 if (P.prevPairCount > 0) {
1777 const int pc = P.prevPairCount;
1780 auto h_k = Kokkos::create_mirror_view(P.prevPairKeys);
1781 auto h_l = Kokkos::create_mirror_view(P.prevLambda);
1782 auto h_lt = Kokkos::create_mirror_view(P.prevLambdaT);
1783 auto h_pi = Kokkos::create_mirror_view(P.prevPosImpulse);
1784 auto h_rb = Kokkos::create_mirror_view(P.prevRestBank);
1785 auto h_rv = Kokkos::create_mirror_view(P.prevRestVPeak);
1786 Kokkos::deep_copy(h_k, P.prevPairKeys);
1787 Kokkos::deep_copy(h_l, P.prevLambda);
1788 Kokkos::deep_copy(h_lt, P.prevLambdaT);
1789 Kokkos::deep_copy(h_pi, P.prevPosImpulse);
1790 Kokkos::deep_copy(h_rb, P.prevRestBank);
1791 Kokkos::deep_copy(h_rv, P.prevRestVPeak);
1792 auto attach = [&](
int i,
const WarmPairEntry& e,
float w) {
1793 MigratePack& m = packs[(std::size_t)i];
1794 if (m.numWarm < kWarmCarryMax) {
1795 m.warm[m.numWarm++] = e;
1799 float worstW = 1e30f;
1800 for (
int s = 0; s < kWarmCarryMax; ++s) {
1801 const float ws = std::fabs(m.warm[s].lambda) + std::fabs(m.warm[s].posImpulse);
1810 for (
int e = 0; e < pc; ++e) {
1811 const unsigned long long k = h_k(e);
1817 we.lambdaT[0] = h_lt(e, 0);
1818 we.lambdaT[1] = h_lt(e, 1);
1819 we.lambdaT[2] = h_lt(e, 2);
1820 we.posImpulse = h_pi(e);
1821 we.restBank = h_rb(e);
1822 we.restVPeak = h_rv(e);
1823 if (we.lambda == 0.0f && we.posImpulse == 0.0f && we.lambdaT[0] == 0.0f &&
1824 we.lambdaT[1] == 0.0f && we.lambdaT[2] == 0.0f && we.restBank == 0.0f)
1826 const float w = std::fabs(we.lambda) + std::fabs(we.posImpulse) + std::fabs(we.restBank);
1827 const unsigned hi =
static_cast<unsigned>(k >> 32);
1828 const unsigned lo =
static_cast<unsigned>(k & 0xFFFFFFFFu);
1829 if (
auto it = gidToLocal.find(hi); it != gidToLocal.end())
1830 attach(it->second, we, w);
1831 if (lo != 0xFFFFFFFFu)
1832 if (
auto it = gidToLocal.find(lo); it != gidToLocal.end())
1833 attach(it->second, we, w);
1839 static void carryHertzLedger(
const Particles& P, std::vector<MigratePack>& packs,
1840 const std::unordered_map<unsigned, int>& gidToLocal) {
1841 if (P.hertzNumPairs > 0) {
1842 const int hn = P.hertzNumPairs;
1843 auto h_hk = Kokkos::create_mirror_view(P.hertzKeys);
1844 auto h_hx = Kokkos::create_mirror_view(P.hertzXi);
1845 Kokkos::deep_copy(h_hk, P.hertzKeys);
1846 Kokkos::deep_copy(h_hx, P.hertzXi);
1847 auto attachHertz = [&](
int i,
const HertzPairEntry& e,
float w) {
1848 MigratePack& m = packs[(std::size_t)i];
1849 if (m.numHertz < kWarmCarryMax) {
1850 m.hertz[m.numHertz++] = e;
1854 float worstW = 1e30f;
1855 for (
int s = 0; s < kWarmCarryMax; ++s) {
1856 const float ws = std::fabs(m.hertz[s].xi[0]) + std::fabs(m.hertz[s].xi[1]) +
1857 std::fabs(m.hertz[s].xi[2]);
1866 for (
int e = 0; e < hn; ++e) {
1869 he.xi[0] = h_hx(e, 0);
1870 he.xi[1] = h_hx(e, 1);
1871 he.xi[2] = h_hx(e, 2);
1872 if (he.xi[0] == 0.0f && he.xi[1] == 0.0f && he.xi[2] == 0.0f)
1874 const float w = std::fabs(he.xi[0]) + std::fabs(he.xi[1]) + std::fabs(he.xi[2]);
1875 const unsigned hi =
static_cast<unsigned>(he.key >> 32);
1876 const unsigned lo =
static_cast<unsigned>(he.key & 0xFFFFFFFFu);
1877 if (
auto it = gidToLocal.find(hi); it != gidToLocal.end())
1878 attachHertz(it->second, he, w);
1879 if (
auto it = gidToLocal.find(lo); it != gidToLocal.end())
1880 attachHertz(it->second, he, w);
1885 static void packWallHistory(
const Particles& P, std::vector<MigratePack>& packs,
int no) {
1886 auto h_xw = Kokkos::create_mirror_view(P.hertzXiWall);
1887 auto h_sw = Kokkos::create_mirror_view(P.hertzSnWall);
1888 Kokkos::deep_copy(h_xw, P.hertzXiWall);
1889 Kokkos::deep_copy(h_sw, P.hertzSnWall);
1890 for (
int i = 0; i < no; ++i)
1891 for (
int wi = 0; wi < Particles::kHertzMaxWalls; ++wi) {
1892 const int slot = i * Particles::kHertzMaxWalls + wi;
1893 MigratePack& m = packs[(std::size_t)i];
1894 m.hertzXiWall[wi][0] = h_xw(slot, 0);
1895 m.hertzXiWall[wi][1] = h_xw(slot, 1);
1896 m.hertzXiWall[wi][2] = h_xw(slot, 2);
1897 m.hertzSnWall[wi] = h_sw(slot);
1902 void packState(Particles& P, std::vector<peclet::core::Vec<3>>& pos, std::vector<char>& payload) {
1903 const int no = P.numReal;
1904 std::unordered_map<unsigned, int> gidToLocal;
1905 std::vector<MigratePack> packs = packParticleRecords(P, pos, payload, gidToLocal);
1906 carryWarmLedger(P, packs, gidToLocal);
1907 carryHertzLedger(P, packs, gidToLocal);
1908 packWallHistory(P, packs, no);
1909 for (
int i = 0; i < no; ++i)
1910 std::memcpy(&payload[(std::size_t)i *
sizeof(MigratePack)], &packs[(std::size_t)i],
1911 sizeof(MigratePack));
1916 void unpackState(Particles& P,
const std::vector<peclet::core::Vec<3>>& pos,
1917 const std::vector<char>& payload, std::size_t newN) {
1918 auto h_pos = Kokkos::create_mirror_view(P.pos);
1919 auto h_quat = Kokkos::create_mirror_view(P.quat);
1920 auto h_vel = Kokkos::create_mirror_view(P.vel);
1921 auto h_angVel = Kokkos::create_mirror_view(P.angVel);
1922 auto h_invI = Kokkos::create_mirror_view(P.invInertia);
1923 auto h_invM = Kokkos::create_mirror_view(P.invMass);
1924 auto h_scale = Kokkos::create_mirror_view(P.scale);
1925 auto h_tScale = Kokkos::create_mirror_view(P.targetScale);
1926 auto h_shape = Kokkos::create_mirror_view(P.shapeId);
1927 auto h_pf = Kokkos::create_mirror_view(P.planeFriction);
1928 auto h_gid = Kokkos::create_mirror_view(P.gid);
1929 auto h_mat = Kokkos::create_mirror_view(P.materialId);
1930 auto h_grd = Kokkos::create_mirror_view(P.groundedLevel);
1931 auto h_orp = Kokkos::create_mirror_view(P.bodyOrphan);
1932 auto h_ovp = Kokkos::create_mirror_view(P.bodyOrphanVPeak);
1933 Kokkos::deep_copy(h_orp, P.bodyOrphan);
1934 Kokkos::deep_copy(h_ovp, P.bodyOrphanVPeak);
1935 auto h_ef = Kokkos::create_mirror_view(P.extForce);
1936 auto h_et = Kokkos::create_mirror_view(P.extTorque);
1937 Kokkos::deep_copy(h_ef, P.extForce);
1938 Kokkos::deep_copy(h_et, P.extTorque);
1939 std::vector<WarmPairEntry> ledger;
1940 ledger.reserve(newN * 4);
1941 std::vector<HertzPairEntry> hertzLedger;
1942 hertzLedger.reserve(newN * 4);
1943 auto h_xw = Kokkos::create_mirror_view(P.hertzXiWall);
1944 auto h_sw = Kokkos::create_mirror_view(P.hertzSnWall);
1945 Kokkos::deep_copy(h_xw, P.hertzXiWall);
1946 Kokkos::deep_copy(h_sw, P.hertzSnWall);
1947 for (std::size_t i = 0; i < newN; ++i) {
1948 h_pos((
int)i, 0) = pos[i][0];
1949 h_pos((
int)i, 1) = pos[i][1];
1950 h_pos((
int)i, 2) = pos[i][2];
1952 std::memcpy(&m, &payload[i *
sizeof(MigratePack)],
sizeof(MigratePack));
1953 h_quat((
int)i, 0) = m.quat.x;
1954 h_quat((
int)i, 1) = m.quat.y;
1955 h_quat((
int)i, 2) = m.quat.z;
1956 h_quat((
int)i, 3) = m.quat.w;
1957 h_vel((
int)i, 0) = m.vel.x;
1958 h_vel((
int)i, 1) = m.vel.y;
1959 h_vel((
int)i, 2) = m.vel.z;
1960 h_angVel((
int)i, 0) = m.angVel.x;
1961 h_angVel((
int)i, 1) = m.angVel.y;
1962 h_angVel((
int)i, 2) = m.angVel.z;
1963 h_invI((
int)i, 0) = m.invInertia.x;
1964 h_invI((
int)i, 1) = m.invInertia.y;
1965 h_invI((
int)i, 2) = m.invInertia.z;
1966 h_invM((
int)i) = m.invMass;
1967 h_scale((
int)i) = m.scale;
1968 h_tScale((
int)i) = m.targetScale;
1969 h_shape((
int)i) = m.shapeId;
1970 h_pf((
int)i, 0) = m.planeFric0;
1971 h_pf((
int)i, 1) = m.planeFric1;
1972 h_gid((
int)i) = m.gid;
1973 h_mat((
int)i) = m.materialId;
1974 h_grd((
int)i) = m.groundedLevel;
1975 h_orp((
int)i) = m.orphan;
1976 h_ovp((
int)i) = m.orphanVPeak;
1977 h_ef((
int)i, 0) = m.extForce.x;
1978 h_ef((
int)i, 1) = m.extForce.y;
1979 h_ef((
int)i, 2) = m.extForce.z;
1980 h_et((
int)i, 0) = m.extTorque.x;
1981 h_et((
int)i, 1) = m.extTorque.y;
1982 h_et((
int)i, 2) = m.extTorque.z;
1983 for (
int s = 0; s < (int)m.numWarm && s < kWarmCarryMax; ++s)
1984 ledger.push_back(m.warm[s]);
1985 for (
int s = 0; s < (int)m.numHertz && s < kWarmCarryMax; ++s)
1986 hertzLedger.push_back(m.hertz[s]);
1987 for (
int wi = 0; wi < Particles::kHertzMaxWalls; ++wi) {
1988 const int slot = (int)i * Particles::kHertzMaxWalls + wi;
1989 h_xw(slot, 0) = m.hertzXiWall[wi][0];
1990 h_xw(slot, 1) = m.hertzXiWall[wi][1];
1991 h_xw(slot, 2) = m.hertzXiWall[wi][2];
1992 h_sw(slot) = m.hertzSnWall[wi];
1995 Kokkos::deep_copy(P.pos, h_pos);
1996 Kokkos::deep_copy(P.quat, h_quat);
1997 Kokkos::deep_copy(P.vel, h_vel);
1998 Kokkos::deep_copy(P.angVel, h_angVel);
1999 Kokkos::deep_copy(P.invInertia, h_invI);
2000 Kokkos::deep_copy(P.invMass, h_invM);
2001 Kokkos::deep_copy(P.scale, h_scale);
2002 Kokkos::deep_copy(P.targetScale, h_tScale);
2003 Kokkos::deep_copy(P.shapeId, h_shape);
2004 Kokkos::deep_copy(P.planeFriction, h_pf);
2005 Kokkos::deep_copy(P.gid, h_gid);
2006 Kokkos::deep_copy(P.materialId, h_mat);
2007 Kokkos::deep_copy(P.groundedLevel, h_grd);
2008 Kokkos::deep_copy(P.bodyOrphan, h_orp);
2009 Kokkos::deep_copy(P.bodyOrphanVPeak, h_ovp);
2010 Kokkos::deep_copy(P.extForce, h_ef);
2011 Kokkos::deep_copy(P.extTorque, h_et);
2016 std::sort(ledger.begin(), ledger.end(),
2017 [](
const WarmPairEntry& a,
const WarmPairEntry& b) { return a.key < b.key; });
2019 std::unique(ledger.begin(), ledger.end(),
2020 [](
const WarmPairEntry& a,
const WarmPairEntry& b) { return a.key == b.key; }),
2022 const int nl = std::min<int>((
int)ledger.size(), (
int)P.prevPairKeys.extent(0));
2024 auto hk = Kokkos::create_mirror_view(P.prevPairKeys);
2025 auto hl = Kokkos::create_mirror_view(P.prevLambda);
2026 auto hlt = Kokkos::create_mirror_view(P.prevLambdaT);
2027 auto hpi = Kokkos::create_mirror_view(P.prevPosImpulse);
2028 auto hrb = Kokkos::create_mirror_view(P.prevRestBank);
2029 auto hrv = Kokkos::create_mirror_view(P.prevRestVPeak);
2030 Kokkos::deep_copy(hk, P.prevPairKeys);
2031 Kokkos::deep_copy(hl, P.prevLambda);
2032 Kokkos::deep_copy(hlt, P.prevLambdaT);
2033 Kokkos::deep_copy(hpi, P.prevPosImpulse);
2034 Kokkos::deep_copy(hrb, P.prevRestBank);
2035 Kokkos::deep_copy(hrv, P.prevRestVPeak);
2036 for (
int e = 0; e < nl; ++e) {
2037 hk(e) = ledger[(std::size_t)e].key;
2038 hl(e) = ledger[(std::size_t)e].lambda;
2039 hlt(e, 0) = ledger[(std::size_t)e].lambdaT[0];
2040 hlt(e, 1) = ledger[(std::size_t)e].lambdaT[1];
2041 hlt(e, 2) = ledger[(std::size_t)e].lambdaT[2];
2042 hpi(e) = ledger[(std::size_t)e].posImpulse;
2043 hrb(e) = ledger[(std::size_t)e].restBank;
2044 hrv(e) = ledger[(std::size_t)e].restVPeak;
2046 Kokkos::deep_copy(P.prevPairKeys, hk);
2047 Kokkos::deep_copy(P.prevLambda, hl);
2048 Kokkos::deep_copy(P.prevLambdaT, hlt);
2049 Kokkos::deep_copy(P.prevPosImpulse, hpi);
2050 Kokkos::deep_copy(P.prevRestBank, hrb);
2051 Kokkos::deep_copy(P.prevRestVPeak, hrv);
2053 P.prevPairCount = nl;
2060 Kokkos::deep_copy(P.hertzXiWall, h_xw);
2061 Kokkos::deep_copy(P.hertzSnWall, h_sw);
2062 std::sort(hertzLedger.begin(), hertzLedger.end(),
2063 [](
const HertzPairEntry& a,
const HertzPairEntry& b) { return a.key < b.key; });
2064 hertzLedger.erase(std::unique(hertzLedger.begin(), hertzLedger.end(),
2065 [](
const HertzPairEntry& a,
const HertzPairEntry& b) {
2066 return a.key == b.key;
2069 const int nh = (int)hertzLedger.size();
2070 if ((
int)P.hertzPrevKeys.extent(0) < nh) {
2071 P.hertzPrevKeys = Kokkos::View<unsigned long long*, CpMem>(
"hertzPrevKeys", nh);
2072 P.hertzPrevXi = Kokkos::View<float* [3], CpMem>(
"hertzPrevXi", nh);
2075 auto hk = Kokkos::create_mirror_view(P.hertzPrevKeys);
2076 auto hx = Kokkos::create_mirror_view(P.hertzPrevXi);
2077 Kokkos::deep_copy(hk, P.hertzPrevKeys);
2078 Kokkos::deep_copy(hx, P.hertzPrevXi);
2079 for (
int e = 0; e < nh; ++e) {
2080 hk(e) = hertzLedger[(std::size_t)e].key;
2081 hx(e, 0) = hertzLedger[(std::size_t)e].xi[0];
2082 hx(e, 1) = hertzLedger[(std::size_t)e].xi[1];
2083 hx(e, 2) = hertzLedger[(std::size_t)e].xi[2];
2085 Kokkos::deep_copy(P.hertzPrevKeys, hk);
2086 Kokkos::deep_copy(P.hertzPrevXi, hx);
2088 P.hertzPrevCount = nh;
2089 P.hertzNumPairs = -1;
2091 P.numReal = (int)newN;
2092 P.numParticles = (int)newN;
2104 void forward(V3 field) {
2107 haloPackF3(field, ownedF3_, numReal_);
2108 dev_.forward(ownedF3_, ghostF3_);
2109 haloUnpackF3(field, ghostF3_, shiftDev_, ghosts(),
false);
2112 void forwardPositions(V3 field) {
2115 haloPackF3(field, ownedF3_, numReal_);
2116 dev_.forward(ownedF3_, ghostF3_);
2117 haloUnpackF3(field, ghostF3_, shiftDev_, ghosts(),
true);
2120 void forward4(V4 field) {
2123 haloPackF4(field, ownedF4_, numReal_);
2124 dev_.forward(ownedF4_, ghostF4_);
2125 haloUnpackF4(field, ghostF4_, ghosts());
2129 ContactOwnership contactOwnership(
const Particles& P)
const {
2130 return ContactOwnership{rank_, numReal_, P.gid, ghostSource_, copyOffsets_,
2131 copyRanks_, ghostImage_, copyImage_, selfOffsets_, selfImage_};
2137 std::vector<float> debugGhostShiftsBySlot()
const {
2138 std::vector<float> out(3 *
static_cast<std::size_t
>(numGhost_ > 0 ? numGhost_ : 0), 0.0f);
2141 auto hs = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), shiftDev_);
2142 auto hk = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), ghostSlot_);
2143 for (
int g = 0; g < numGhost_; ++g) {
2144 const int s = hk(g);
2145 out[3 * s] = hs(g).x;
2146 out[3 * s + 1] = hs(g).y;
2147 out[3 * s + 2] = hs(g).z;
2158 void markVelocityBaseline(Particles& P) {
2161 haloMarkVelocityBaseline(
2162 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2163 velocityBaselines(), ghosts());
2172 void syncVelocities(Particles& P,
bool rotation) {
2176 syncVelocitiesM(P, rotation);
2179 haloPackVelocityIncrement(
2180 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2181 velocityBaselines(), ghostVelInc_, ghosts());
2183 Kokkos::subview(ownedVelInc_, std::pair<std::size_t, std::size_t>(0, numReal_)),
2184 VelocityIncrement{});
2185 dev_.reverse(ghostVelInc_, ownedVelInc_);
2186 haloApplyVelocityIncrement(
2187 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, ownedVelInc_,
2190 haloPackVelocityState(P.bodyPred(), ownedVelState_, nIface_, iface_);
2191 dev_.forward(ownedVelState_, ghostVelState_);
2192 haloUnpackVelocityState(P.bodyPred(), ghostVelState_, ghosts());
2196 markVelocityBaseline(P);
2200 void publishPositions(Particles& P,
bool rotation) {
2203 forwardPositionState(P, rotation);
2204 markPositionPublished(P);
2207 void forwardPositionState(Particles& P,
bool rotation) {
2209 haloPackPositionState(P.bodyPred(), ownedPosState_, nIface_, iface_);
2210 dev_.forward(ownedPosState_, ghostPosState_);
2211 haloUnpackPositionState(P.bodyPred(), ghostPosState_, shiftDev_, ghosts());
2213 forwardPositions(P.posPred);
2217 void markPositionPublished(Particles& P) {
2218 haloMarkPositionBaseline(P.posPred, basePos_, ghosts());
2220 haloMarkOwnerPositionSeeds(P.posPred, ownerSeedPos_, nPosRows_, posRows_);
2227 void syncPositions(Particles& P,
bool rotation) {
2231 haloPositionMPackZero(P.posPred, basePos_, P.aPos, ghostPosInc_, ghosts(), ownedPosInc_,
2232 posRows_, nPosRows_);
2233 dev_.reverse(ghostPosInc_, ownedPosInc_);
2235 positionMRound(P, ownedPosState_, ghostPosState_);
2237 positionMRound(P, ownedF3_, ghostF3_);
2240 haloPackPositionIncrement(P.posPred, basePos_, ghostPosInc_, ghosts());
2242 Kokkos::subview(ownedPosInc_, std::pair<std::size_t, std::size_t>(0, numReal_)),
2243 PositionIncrement{});
2244 dev_.reverse(ghostPosInc_, ownedPosInc_);
2245 haloApplyPositionIncrement(P.posPred, ownedPosInc_, numReal_);
2246 publishPositions(P, rotation);
2251 bool velocityMassSplit()
const {
return velM_; }
2252 bool positionMassSplit()
const {
return posM_; }
2261 void openVelocityPhase(Particles& P,
bool rotation,
bool shares) {
2264 ensureClampCounter(P);
2265 const int no = numReal_, ng = numGhost_;
2266 haloPackOpeningIncrement(
2267 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2268 velocityBaselines(), P.aVel, P.aPos, ghostOpenInc_, GhostRange{ghostSlot_, no, ng});
2269 Kokkos::deep_copy(Kokkos::subview(ownedOpenInc_, std::pair<std::size_t, std::size_t>(0, no)),
2270 OpeningIncrement{});
2271 dev_.reverse(ghostOpenInc_, ownedOpenInc_);
2272 haloApplyOpening(VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2273 P.aVel, P.aPos, P.kVel, P.kPos, ownedOpenInc_, no);
2275 haloPackOpeningState(
2276 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, P.kVel,
2277 P.kPos, ownedOpenState_, no);
2278 dev_.forward(ownedOpenState_, ghostOpenState_);
2279 haloUnpackOpeningState(
2280 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, P.kVel,
2281 P.kPos, ghostOpenState_, GhostRange{ghostSlot_, no, ng});
2283 haloPackOpeningState(
2284 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, P.kVel,
2285 P.kPos, ownedOpenStateNR_, no);
2286 dev_.forward(ownedOpenStateNR_, ghostOpenStateNR_);
2287 haloUnpackOpeningState(
2288 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, P.kVel,
2289 P.kPos, ghostOpenStateNR_, GhostRange{ghostSlot_, no, ng});
2291 buildMRows(P.kVel, velRows_, nVelRows_);
2292 buildMRows(P.kPos, posRows_, nPosRows_);
2293 velM_ = posM_ =
true;
2294 orphanShares_ = shares;
2295 markVelocityReconciled(P);
2304 void openPositionCounts(Particles& P,
bool velocityMask) {
2307 const int no = numReal_, ng = numGhost_;
2309 const unsigned long long bit = 1ull << rankColor_;
2310 haloPackGhostCountsMask(P.aVel, P.aPos, ghostCM_, GhostRange{ghostSlot_, no, ng}, bit);
2311 Kokkos::deep_copy(Kokkos::subview(ownedCM_, std::pair<std::size_t, std::size_t>(0, no)),
2312 PositionCountsMask{0ull, 0, 0});
2313 dev_.reverse(ghostCM_, ownedCM_);
2314 haloOwnerCountsMask(P.aVel, P.aPos, P.kPos, P.velMask, ownedCM_, bit, no);
2315 dev_.forward(ownedCM_, ghostCM_);
2316 haloUnpackCountsMask(P.kPos, P.velMask, ghostCM_, GhostRange{ghostSlot_, no, ng});
2317 buildMRows(P.kPos, posRows_, nPosRows_);
2321 haloPackGhostInt(P.aPos, ghostCnt_, GhostRange{ghostSlot_, no, ng});
2322 Kokkos::deep_copy(Kokkos::subview(ownedCnt_, std::pair<std::size_t, std::size_t>(0, no)), 0);
2323 dev_.reverse(ghostCnt_, ownedCnt_);
2324 haloOwnerCountK(P.aPos, P.kPos, ownedCnt_, no);
2325 dev_.forward(ownedCnt_, ghostCnt_);
2326 haloUnpackGhostInt(P.kPos, ghostCnt_, GhostRange{ghostSlot_, no, ng});
2327 buildMRows(P.kPos, posRows_, nPosRows_);
2331 void restoreOrphanBalance(Particles& P) {
2332 if (!velM_ || !orphanShares_)
2334 haloRestoreOrphanBalance(P.bodyOrphan, ownerSeedOrphan_, P.aVel, P.kVel, nVelRows_, velRows_);
2345 void mapVelocitySlots(Particles& P) {
2346 const int no = numReal_, ng = numGhost_;
2349 if (canonStale_ || ghostCanon_.extent(0) <
static_cast<std::size_t
>(ng)) {
2350 const int nr = numReceived_;
2352 Kokkos::create_mirror_view(Kokkos::subview(P.gid, std::pair<int, int>(no, no + nr)));
2353 Kokkos::deep_copy(hg, Kokkos::subview(P.gid, std::pair<int, int>(no, no + nr)));
2354 std::vector<std::pair<int, int>> key(
static_cast<std::size_t
>(nr));
2355 for (
int g = 0; g < nr; ++g)
2356 key[
static_cast<std::size_t
>(g)] = {hg(g), g};
2357 std::sort(key.begin(), key.end());
2358 std::vector<int> canon(
static_cast<std::size_t
>(ng));
2359 for (std::size_t p = 0; p < key.size();) {
2361 while (e < key.size() && key[e].first == key[p].first)
2363 const int c = no + key[p].second;
2364 for (std::size_t t = p; t < e; ++t)
2365 canon[
static_cast<std::size_t
>(key[t].second)] = c;
2368 for (
int g = nr; g < ng; ++g)
2369 canon[
static_cast<std::size_t
>(g)] = selfIdx_[
static_cast<std::size_t
>(g - nr)];
2370 ghostCanon_ = peclet::core::toDevice(canon,
"peclet::dem::halo::ghostCanon");
2371 canonStale_ =
false;
2373 haloMapVelocitySlots(P.realIndices, ghostCanon_, no, ng);
2385 void ensureRankColoring(
double band) {
2388 const int nb =
static_cast<int>(dec_.numBlocks());
2389 unsigned long long fp = 1469598103934665603ull;
2390 for (
int b = 0; b < nb; ++b) {
2391 const auto blk = dec_.block(
static_cast<std::size_t
>(b));
2392 for (
int d = 0; d < 3; ++d) {
2393 fp = (fp ^
static_cast<unsigned long long>(blk.origin[d])) * 1099511628211ull;
2394 fp = (fp ^
static_cast<unsigned long long>(blk.size[d])) * 1099511628211ull;
2397 if (band == colorBand_ && fp == colorFingerprint_)
2400 colorFingerprint_ = fp;
2401 const auto& gs = dec_.globalSize();
2402 std::vector<std::array<double, 6>> box(
static_cast<std::size_t
>(nb));
2403 for (
int b = 0; b < nb; ++b) {
2404 const auto blk = dec_.block(
static_cast<std::size_t
>(b));
2405 for (
int d = 0; d < 3; ++d) {
2406 box[b][d] = map_.origin[d] +
static_cast<double>(blk.origin[d]) * map_.cellSize[d];
2407 box[b][3 + d] = box[b][d] +
static_cast<double>(blk.size[d]) * map_.cellSize[d];
2410 const double lim2 = 4.0 * band * band;
2411 auto adjacent = [&](
int a,
int b) {
2413 for (
int d = 0; d < 3; ++d) {
2414 const double L =
static_cast<double>(gs[d]) * map_.cellSize[d];
2415 auto gap = [&](
double shift) {
2416 const double loB = box[b][d] + shift, hiB = box[b][3 + d] + shift;
2417 return std::max(0.0, std::max(box[a][d] - hiB, loB - box[a][3 + d]));
2419 double g = gap(0.0);
2420 if (map_.periodic[d])
2421 g = std::min(g, std::min(gap(L), gap(-L)));
2426 std::vector<int> col(
static_cast<std::size_t
>(nb), 0);
2428 for (
int r = 0; r < nb; ++r) {
2429 unsigned long long used = 0ull;
2430 for (
int q = 0; q < r; ++q)
2432 used |= 1ull << col[q];
2434 while (c < 64 && ((used >> c) & 1ull))
2437 throw std::runtime_error(
2438 "ParticleHalo: the rank graph needs more than 64 colours for rank-level X "
2439 "(docs/contact_solve_framework.md §1.4); the blocks are too small for the band");
2441 maxc = std::max(maxc, c);
2443 rankColor_ = col[
static_cast<std::size_t
>(rank_)];
2444 numRankColors_ = maxc + 1;
2448 void buildInterfaceRows(
const peclet::core::halo::ParticleHaloTopology<3>::FlatTopo& t,
int no) {
2449 std::vector<unsigned char> mark(
static_cast<std::size_t
>(std::max(no, 1)), 0);
2450 for (
const auto i : t.sendIdx)
2451 mark[
static_cast<std::size_t
>(i)] = 1;
2452 for (
const auto i : t.selfIdx)
2453 mark[
static_cast<std::size_t
>(i)] = 1;
2454 std::vector<int> rows;
2455 for (
int i = 0; i < no; ++i)
2456 if (mark[
static_cast<std::size_t
>(i)])
2458 nIface_ =
static_cast<int>(rows.size());
2461 iface_ = peclet::core::toDevice(rows,
"peclet::dem::halo::ifaceRows");
2462 ifaceMark_ = peclet::core::toDevice(mark,
"peclet::dem::halo::ifaceMark");
2468 void buildMRows(Vi k, Vi& rows,
int& nRows) {
2469 const int no = numReal_;
2470 if (rows.extent(0) <
static_cast<std::size_t
>(std::max(no, 1)))
2471 rows =
Vi(
"peclet::dem::halo::mRows",
static_cast<std::size_t
>(std::max(no, 1)));
2472 nRows = haloBuildMRows(k, ifaceMark_, rows, no);
2474 void ensureClampCounter(Particles& P) {
2475 if (P.orphanClampCount.data() ==
nullptr)
2476 P.orphanClampCount = Kokkos::View<int, CpMem>(
"peclet::dem::orphanClampCount");
2480 void markVelocityReconciled(Particles& P) {
2481 haloMarkOwnerVelocitySeeds(P.velPred, P.angVelPred, P.bodyOrphan, ownerSeedVel_,
2482 ownerSeedAngVel_, ownerSeedOrphan_, nVelRows_, velRows_);
2484 haloSetOrphanShares(P.bodyOrphan, P.kVel, numReal_ + numGhost_);
2485 markVelocityBaseline(P);
2489 void syncVelocitiesM(Particles& P,
bool rotation) {
2490 haloVelocityMPackZero(
2491 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2492 velocityBaselines(), P.aVel, ghostVelInc_, ghosts(), ownedVelInc_, velRows_, nVelRows_);
2493 dev_.reverse(ghostVelInc_, ownedVelInc_);
2494 if (orphanShares_) {
2496 velocityMRound(P, ownedVelStateB_, ghostVelStateB_);
2498 velocityMRound(P, ownedVelStateBNR_, ghostVelStateBNR_);
2499 }
else if (rotation) {
2500 velocityMRound(P, ownedVelState_, ghostVelState_);
2502 velocityMRound(P, ownedF3_, ghostF3_);
2507 void velocityMRound(Particles& P, peclet::core::View<S> owned, peclet::core::View<S> ghost) {
2508 haloVelocityMApply(VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak},
2509 ownerSeedVel_, ownerSeedAngVel_, ownerSeedOrphan_, P.aVel, P.kVel,
2510 ownedVelInc_, P.orphanClampCount, P.maxConsensus, velRows_, nVelRows_,
2511 ifaceMark_, owned, orphanShares_);
2512 dev_.forward(owned, ghost);
2513 haloVelocityMUnpack(
2514 VelocityPhaseViews{P.velPred, P.angVelPred, P.bodyOrphan, P.bodyOrphanVPeak}, ghost,
2515 velocityBaselines(), P.aVel, P.kVel, ghostVelInc_, P.maxConsensus, ghosts(), orphanShares_);
2519 void positionMRound(Particles& P, peclet::core::View<S> owned, peclet::core::View<S> ghost) {
2520 haloPositionMApply(P.bodyPred(), ownerSeedPos_, P.aPos, P.kPos, ownedPosInc_, P.maxConsensus,
2521 posRows_, nPosRows_, ifaceMark_, owned);
2522 dev_.forward(owned, ghost);
2523 haloPositionMUnpack(P.bodyPred(), ghost, shiftDev_, basePos_, P.aPos, P.kPos, ghostPosInc_,
2524 P.maxConsensus, ghosts());
2531 void syncFrictionCounts(Particles& P) {
2534 haloPackGhostColumn(P.planeFriction, ghostVal_, ghosts());
2535 Kokkos::deep_copy(Kokkos::subview(ownedVal_, std::pair<std::size_t, std::size_t>(0, numReal_)),
2537 dev_.reverse(ghostVal_, ownedVal_);
2538 haloAddOwnedColumn(P.planeFriction, ownedVal_, numReal_);
2539 haloPackOwnedColumn(P.planeFriction, ownedVal_, numReal_);
2540 dev_.forward(ownedVal_, ghostVal_);
2541 haloUnpackGhostColumn(P.planeFriction, ghostVal_, ghosts());
2548 void syncContactCounts(Particles& P) {
2551 haloPackGhostCount(P.constraintCounts, ghostCnt_, ghosts());
2552 Kokkos::deep_copy(Kokkos::subview(ownedCnt_, std::pair<std::size_t, std::size_t>(0, numReal_)),
2554 dev_.reverse(ghostCnt_, ownedCnt_);
2555 haloAddPackOwnedCount(P.constraintCounts, ownedCnt_, numReal_);
2556 dev_.forward(ownedCnt_, ghostCnt_);
2557 haloUnpackGhostCount(P.constraintCounts, ghostCnt_, ghosts());
2560 void selfMapReals(Vi realIndices,
int no) {
2561 Kokkos::parallel_for(
2562 "peclet::dem::halo::selfMapReals", Kokkos::RangePolicy<CpExec>(0, no),
2563 KOKKOS_LAMBDA(
int i) { realIndices(i) = i; });
2567 void allocBuffers(
int no,
int ng) {
2570 ownedF3_ = peclet::core::View<F3>(
"peclet::dem::halo::ownedF3", no);
2571 ghostF3_ = peclet::core::View<F3>(
"peclet::dem::halo::ghostF3", ng);
2572 ownedF4_ = peclet::core::View<F4>(
"peclet::dem::halo::ownedF4", no);
2573 ghostF4_ = peclet::core::View<F4>(
"peclet::dem::halo::ghostF4", ng);
2574 ownedPack_ = peclet::core::View<MpiGatherPack>(
"peclet::dem::halo::ownedPack", no);
2575 ghostPack_ = peclet::core::View<MpiGatherPack>(
"peclet::dem::halo::ghostPack", ng);
2578 const std::size_t g1 =
static_cast<std::size_t
>(std::max(ng, 1)),
2579 o1 =
static_cast<std::size_t
>(std::max(no, 1));
2580 if (baseVel_.extent(0) < g1) {
2581 baseVel_ =
V3(
"peclet::dem::halo::baseVel", g1);
2582 baseAngVel_ =
V3(
"peclet::dem::halo::baseAngVel", g1);
2583 basePos_ =
V3(
"peclet::dem::halo::basePos", g1);
2584 baseOrphan_ =
Vf(
"peclet::dem::halo::baseOrphan", g1);
2585 baseOrphanPeak_ =
Vf(
"peclet::dem::halo::baseOrphanPeak", g1);
2586 ghostVelInc_ = peclet::core::View<VelocityIncrement>(
"peclet::dem::halo::ghostVelInc", g1);
2587 ghostPosInc_ = peclet::core::View<PositionIncrement>(
"peclet::dem::halo::ghostPosInc", g1);
2588 ghostVal_ = peclet::core::View<float>(
"peclet::dem::halo::ghostVal", g1);
2589 ghostCnt_ = peclet::core::View<int>(
"peclet::dem::halo::ghostCnt", g1);
2590 ghostCM_ = peclet::core::View<PositionCountsMask>(
"peclet::dem::halo::ghostCM", g1);
2591 ghostVelState_ = peclet::core::View<VelocityState>(
"peclet::dem::halo::ghostVelState", g1);
2592 ghostPosState_ = peclet::core::View<PositionState>(
"peclet::dem::halo::ghostPosState", g1);
2593 ghostOpenInc_ = peclet::core::View<OpeningIncrement>(
"peclet::dem::halo::ghostOpenInc", g1);
2594 ghostOpenState_ = peclet::core::View<OpeningState>(
"peclet::dem::halo::ghostOpenState", g1);
2596 peclet::core::View<OpeningStateNoRot>(
"peclet::dem::halo::ghostOpenStateNR", g1);
2597 ghostVelStateB_ = peclet::core::View<VelocityStateB>(
"peclet::dem::halo::ghostVelStateB", g1);
2599 peclet::core::View<VelocityStateBNoRot>(
"peclet::dem::halo::ghostVelStateBNR", g1);
2601 if (ownedVelInc_.extent(0) < o1) {
2602 ownedVelInc_ = peclet::core::View<VelocityIncrement>(
"peclet::dem::halo::ownedVelInc", o1);
2603 ownedPosInc_ = peclet::core::View<PositionIncrement>(
"peclet::dem::halo::ownedPosInc", o1);
2604 ownedVal_ = peclet::core::View<float>(
"peclet::dem::halo::ownedVal", o1);
2605 ownedCnt_ = peclet::core::View<int>(
"peclet::dem::halo::ownedCnt", o1);
2606 ownedCM_ = peclet::core::View<PositionCountsMask>(
"peclet::dem::halo::ownedCM", o1);
2607 ownedVelState_ = peclet::core::View<VelocityState>(
"peclet::dem::halo::ownedVelState", o1);
2608 ownedPosState_ = peclet::core::View<PositionState>(
"peclet::dem::halo::ownedPosState", o1);
2609 ownedOpenInc_ = peclet::core::View<OpeningIncrement>(
"peclet::dem::halo::ownedOpenInc", o1);
2610 ownedOpenState_ = peclet::core::View<OpeningState>(
"peclet::dem::halo::ownedOpenState", o1);
2612 peclet::core::View<OpeningStateNoRot>(
"peclet::dem::halo::ownedOpenStateNR", o1);
2613 ownedVelStateB_ = peclet::core::View<VelocityStateB>(
"peclet::dem::halo::ownedVelStateB", o1);
2615 peclet::core::View<VelocityStateBNoRot>(
"peclet::dem::halo::ownedVelStateBNR", o1);
2616 ownerSeedVel_ =
V3(
"peclet::dem::halo::ownerSeedVel", o1);
2617 ownerSeedAngVel_ =
V3(
"peclet::dem::halo::ownerSeedAngVel", o1);
2618 ownerSeedOrphan_ =
Vf(
"peclet::dem::halo::ownerSeedOrphan", o1);
2619 ownerSeedPos_ =
V3(
"peclet::dem::halo::ownerSeedPos", o1);
2628 void buildOwnershipMaps(
const peclet::core::halo::ParticleHaloTopology<3>::FlatTopo& t,
int no,
2629 int ng,
const std::vector<int>& hSlot) {
2630 const std::size_t g1 =
static_cast<std::size_t
>(std::max(ng, 1));
2631 std::vector<int> src(g1, rank_);
2632 for (std::size_t k = 0; k < t.recvRanks.size(); ++k)
2633 for (
int j = 0; j < t.recvCounts[k]; ++j)
2634 src[
static_cast<std::size_t
>(hSlot[
static_cast<std::size_t
>(t.recvOffsets[k]) + j])] =
2636 std::vector<int> off(
static_cast<std::size_t
>(no) + 1, 0);
2637 for (
const auto i : t.sendIdx)
2638 ++off[
static_cast<std::size_t
>(i) + 1];
2639 for (
int i = 0; i < no; ++i)
2640 off[
static_cast<std::size_t
>(i) + 1] += off[
static_cast<std::size_t
>(i)];
2642 const auto& gsz = dec_.globalSize();
2643 auto imageOf = [&](
const peclet::core::Vec<3>& sh) {
2644 int c[3] = {0, 0, 0};
2645 for (
int d = 0; d < 3; ++d)
2646 if (map_.periodic[d]) {
2647 const double L =
static_cast<double>(gsz[d]) * map_.cellSize[d];
2648 c[d] =
static_cast<int>(std::lround(sh[d] / L));
2650 return imagePack(c[0], c[1], c[2]);
2652 std::vector<int> gimg(g1, imagePack(0, 0, 0));
2653 for (std::size_t j = 0; j < t.shift.size(); ++j)
2654 gimg[
static_cast<std::size_t
>(hSlot[j])] = imageOf(t.shift[j]);
2655 std::vector<int> ranks(std::max<std::size_t>(t.sendIdx.size(), 1), -1);
2656 std::vector<int> imgs(std::max<std::size_t>(t.sendIdx.size(), 1), imagePack(0, 0, 0));
2657 std::vector<int> cur(off.begin(), off.end() - 1);
2658 for (std::size_t k = 0; k < t.sendRanks.size(); ++k)
2659 for (
int j = t.sendOffsets[k]; j < t.sendOffsets[k + 1]; ++j) {
2660 const std::size_t at =
2661 static_cast<std::size_t
>(cur[
static_cast<std::size_t
>(t.sendIdx[j])]++);
2662 ranks[at] = t.sendRanks[k];
2663 imgs[at] = imageOf(t.sendShift[
static_cast<std::size_t
>(j)]);
2666 std::vector<int> soff(
static_cast<std::size_t
>(no) + 1, 0);
2667 for (
const auto i : t.selfIdx)
2668 ++soff[
static_cast<std::size_t
>(i) + 1];
2669 for (
int i = 0; i < no; ++i)
2670 soff[
static_cast<std::size_t
>(i) + 1] += soff[
static_cast<std::size_t
>(i)];
2671 std::vector<int> simg(std::max<std::size_t>(t.selfIdx.size(), 1), imagePack(0, 0, 0));
2673 std::vector<int> scur(soff.begin(), soff.end() - 1);
2674 for (std::size_t j = 0; j < t.selfIdx.size(); ++j)
2675 simg[
static_cast<std::size_t
>(scur[
static_cast<std::size_t
>(t.selfIdx[j])]++)] =
2676 imageOf(t.shift[
static_cast<std::size_t
>(t.numReceived) + j]);
2678 selfOffsets_ = peclet::core::toDevice(soff,
"peclet::dem::halo::selfOffsets");
2679 selfImage_ = peclet::core::toDevice(simg,
"peclet::dem::halo::selfImage");
2680 ghostSource_ = peclet::core::toDevice(src,
"peclet::dem::halo::ghostSource");
2681 ghostImage_ = peclet::core::toDevice(gimg,
"peclet::dem::halo::ghostImage");
2682 copyOffsets_ = peclet::core::toDevice(off,
"peclet::dem::halo::copyOffsets");
2683 copyRanks_ = peclet::core::toDevice(ranks,
"peclet::dem::halo::copyRanks");
2684 copyImage_ = peclet::core::toDevice(imgs,
"peclet::dem::halo::copyImage");
2691 float maxOwnedDisplacement(
const V3& pos,
int no)
const {
2697 if (refPos_.extent(0) <
static_cast<std::size_t
>(no))
2700 V3 p = pos, r = refPos_;
2701 Kokkos::parallel_reduce(
2702 "peclet::dem::halo::maxdisp", Kokkos::RangePolicy<CpExec>(0, no),
2703 KOKKOS_LAMBDA(
const int i,
float& m) {
2704 const float dx = p(i, 0) - r(i, 0), dy = p(i, 1) - r(i, 1), dz = p(i, 2) - r(i, 2);
2705 const float d = Kokkos::sqrt(dx * dx + dy * dy + dz * dz);
2709 Kokkos::Max<float>(md));
2716 GhostRange ghosts()
const {
return {ghostSlot_, numReal_, numGhost_}; }
2717 VelocityPhaseViews velocityBaselines()
const {
2718 return {baseVel_, baseAngVel_, baseOrphan_, baseOrphanPeak_};
2723 std::vector<int> uploadGhostSlots(
const peclet::core::halo::ParticleHaloTopology<3>::FlatTopo& t,
2725 std::vector<std::size_t> blocks(t.recvRanks.size());
2726 for (std::size_t k = 0; k < blocks.size(); ++k)
2728 std::sort(blocks.begin(), blocks.end(),
2729 [&](std::size_t a, std::size_t b) { return t.recvRanks[a] < t.recvRanks[b]; });
2730 if (ghostSlot_.extent(0) <
static_cast<std::size_t
>(std::max(ng, 1)))
2731 ghostSlot_ =
Vi(
"peclet::dem::halo::ghostSlot",
static_cast<std::size_t
>(std::max(ng, 1)));
2732 auto h = Kokkos::create_mirror_view(ghostSlot_);
2734 for (std::size_t k : blocks)
2735 for (
int j = 0; j < t.recvCounts[k]; ++j)
2736 h(
static_cast<int>(t.recvOffsets[k]) + j) = next++;
2737 for (
int g = next; g < ng; ++g)
2739 Kokkos::deep_copy(ghostSlot_, h);
2740 std::vector<int> hs(
static_cast<std::size_t
>(std::max(ng, 0)));
2741 for (
int g = 0; g < ng; ++g)
2742 hs[
static_cast<std::size_t
>(g)] = h(g);
2748 void orderArrivalsBySource(std::vector<peclet::core::Vec<3>>& pos, std::vector<char>& payload,
2750 const std::size_t nArr = mig_.lastReceived();
2751 if (nArr < 2 || nArr > newN)
2753 const std::size_t first = newN - nArr;
2754 std::vector<int> src(nArr);
2755 for (std::size_t k = 0; k < nArr; ++k)
2756 std::memcpy(&src[k],
2757 &payload[(first + k) *
sizeof(MigratePack) + offsetof(MigratePack, srcRank)],
2759 if (std::is_sorted(src.begin(), src.end()))
2761 std::vector<std::size_t> perm(nArr);
2762 for (std::size_t k = 0; k < nArr; ++k)
2764 std::stable_sort(perm.begin(), perm.end(),
2765 [&](std::size_t a, std::size_t b) { return src[a] < src[b]; });
2766 std::vector<peclet::core::Vec<3>> p2(nArr);
2767 std::vector<char> b2(nArr *
sizeof(MigratePack));
2768 for (std::size_t k = 0; k < nArr; ++k) {
2769 p2[k] = pos[first + perm[k]];
2770 std::memcpy(&b2[k *
sizeof(MigratePack)], &payload[(first + perm[k]) *
sizeof(MigratePack)],
2771 sizeof(MigratePack));
2773 std::copy(p2.begin(), p2.end(), pos.begin() +
static_cast<std::ptrdiff_t
>(first));
2774 std::memcpy(&payload[first *
sizeof(MigratePack)], b2.data(), b2.size());
2777 void uploadShift(
const peclet::core::halo::ParticleHaloTopology<3>::FlatTopo& t) {
2778 std::vector<F3> hs(t.shift.size());
2779 for (std::size_t i = 0; i < t.shift.size(); ++i)
2780 hs[i] = F3{
static_cast<float>(t.shift[i][0]),
static_cast<float>(t.shift[i][1]),
2781 static_cast<float>(t.shift[i][2])};
2782 shiftDev_ = peclet::core::toDevice(hs,
"peclet::dem::halo::shift");
2785 bool inited_ =
false;
2786 int rank_ = 0, numReal_ = 0, numGhost_ = 0;
2790 float verletSkin_ = 0.0f;
2791 bool haveTopo_ =
false;
2792 int lastNumReal_ = -1;
2793 double lastBand_ = -1.0;
2794 long nRebuild_ = 0, nGather_ = 0;
2796 MPI_Comm comm_ = MPI_COMM_NULL;
2797 peclet::core::decomp::BlockDecomposer<3> dec_;
2798 peclet::core::halo::DomainMap<3> map_;
2799 peclet::core::halo::ParticleMigrator<3> mig_;
2800 peclet::core::halo::ParticleHaloTopology<3> halo_;
2801 peclet::core::halo::ParticleHalo<3> dev_;
2802 peclet::core::View<F3> ownedF3_, ghostF3_, shiftDev_;
2804 peclet::core::View<F4> ownedF4_, ghostF4_;
2805 peclet::core::View<MpiGatherPack> ownedPack_, ghostPack_;
2808 V3 baseVel_, baseAngVel_, basePos_;
2809 Vf baseOrphan_, baseOrphanPeak_;
2810 peclet::core::View<VelocityIncrement> ghostVelInc_, ownedVelInc_;
2811 peclet::core::View<PositionIncrement> ghostPosInc_, ownedPosInc_;
2812 peclet::core::View<float> ghostVal_, ownedVal_;
2813 peclet::core::View<int> ghostCnt_, ownedCnt_;
2814 peclet::core::View<PositionCountsMask> ghostCM_, ownedCM_;
2817 int rankColor_ = 0, numRankColors_ = 1;
2818 double colorBand_ = -1.0;
2819 unsigned long long colorFingerprint_ = 0ull;
2820 peclet::core::View<VelocityState> ghostVelState_, ownedVelState_;
2821 peclet::core::View<PositionState> ghostPosState_, ownedPosState_;
2823 Kokkos::View<int*, CpMem> ghostSource_, copyOffsets_, copyRanks_, ghostImage_, copyImage_,
2824 selfOffsets_, selfImage_;
2828 peclet::core::View<OpeningIncrement> ghostOpenInc_, ownedOpenInc_;
2829 peclet::core::View<OpeningState> ghostOpenState_, ownedOpenState_;
2830 peclet::core::View<OpeningStateNoRot> ghostOpenStateNR_, ownedOpenStateNR_;
2831 peclet::core::View<VelocityStateB> ghostVelStateB_, ownedVelStateB_;
2832 peclet::core::View<VelocityStateBNoRot> ghostVelStateBNR_, ownedVelStateBNR_;
2833 V3 ownerSeedVel_, ownerSeedAngVel_, ownerSeedPos_;
2834 Vf ownerSeedOrphan_;
2835 bool velM_ =
false, posM_ =
false, orphanShares_ =
false;
2836 bool canonStale_ =
true;
2837 int numReceived_ = 0;
2839 Kokkos::View<unsigned char*, CpMem> ifaceMark_;
2841 Vi velRows_, posRows_;
2842 int nVelRows_ = 0, nPosRows_ = 0;
2843 std::vector<int> selfIdx_;
2844 Kokkos::View<int*, CpMem> ghostCanon_;
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
Kokkos::View< float *, CpMem > Vf
Kokkos::View< int *, CpMem > Vi
CpExec::memory_space CpMem
Kokkos::View< float *[3], CpMem > V3
Kokkos::View< float *[4], CpMem > V4
dem — portable (Kokkos) particle SoA container: the storage the dem flip pivots on.
int MPI_Allreduce(const void *a, void *b, int c, MPI_Datatype d, MPI_Op e, MPI_Comm f)
The substep's predicted body state (Particles::bodyPred()).
A body's state, one row per particle slot (Particles::bodyState()).