peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
periodicity.hpp
Go to the documentation of this file.
1
9#ifndef DEM_PERIODICITY_HPP
10#define DEM_PERIODICITY_HPP
11
12#include <Kokkos_Core.hpp>
13
14#include "dem_portable.hpp"
15#include "integration.hpp" // Domain, V3/V4/Vf/Vi, detail::st3/st4
16
17namespace peclet::dem {
18
26inline int calculateGhostCapacity(int nReal, Domain dom, float skin) {
27 const float sx = dom.periodic_x ? skin : 0.0f;
28 const float sy = dom.periodic_y ? skin : 0.0f;
29 const float sz = dom.periodic_z ? skin : 0.0f;
30 const double innerX = (dom.size.x - 2.0f * sx > 0.0f) ? (dom.size.x - 2.0f * sx) : 0.0;
31 const double innerY = (dom.size.y - 2.0f * sy > 0.0f) ? (dom.size.y - 2.0f * sy) : 0.0;
32 const double innerZ = (dom.size.z - 2.0f * sz > 0.0f) ? (dom.size.z - 2.0f * sz) : 0.0;
33 const double volTotal = static_cast<double>(dom.size.x) * dom.size.y * dom.size.z;
34 if (volTotal <= 0.0)
35 return nReal * 8; // degenerate box (domain not set yet) -> generous fallback
36 const double ghostFraction = (volTotal - innerX * innerY * innerZ) / volTotal;
37 const double estGhosts = (ghostFraction > 0.5) ? nReal * 32.0 : nReal * ghostFraction * 4.0;
38 return nReal + static_cast<int>(estGhosts) + 4096; // + fixed buffer (matches CUDA)
39}
40
45inline void generateGhosts(int numReal, int capacity, Domain dom, float skin,
46 const BodyState& bodies, const BodyPred& pred, Vi realIndices,
47 Kokkos::View<int, CpMem> topGhost, Vi gid = Vi{},
48 Kokkos::View<unsigned char*, CpMem> materialId = {}) {
49 const V3 pos = bodies.pos, vel = bodies.vel, angVel = bodies.angVel;
50 const Vf invMass = bodies.invMass, scale = bodies.scale;
51 const V4 quat = bodies.quat;
52 const Vi shapeId = bodies.shapeId;
53 const V3 posPred = pred.posPred, velPred = pred.velPred, angVelPred = pred.angVelPred;
54 const V4 quatPred = pred.quatPred;
55 using detail::st3;
56 using detail::st4;
57 CpExec space;
58 Kokkos::parallel_for(
59 "peclet::dem::generate_ghosts", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
60 KOKKOS_LAMBDA(int i) {
61 const F3 p = ldF3(pos, i);
62 const int sx = dom.periodic_x
63 ? ((p.x < dom.min.x + skin) ? 1 : ((p.x > dom.max.x - skin) ? -1 : 0))
64 : 0;
65 const int sy = dom.periodic_y
66 ? ((p.y < dom.min.y + skin) ? 1 : ((p.y > dom.max.y - skin) ? -1 : 0))
67 : 0;
68 const int sz = dom.periodic_z
69 ? ((p.z < dom.min.z + skin) ? 1 : ((p.z > dom.max.z - skin) ? -1 : 0))
70 : 0;
71 const int ax[2] = {0, sx}, ay[2] = {0, sy}, az[2] = {0, sz};
72 const int nx = (sx == 0) ? 1 : 2, ny = (sy == 0) ? 1 : 2, nz = (sz == 0) ? 1 : 2;
73
74 const F3 pPred = ldF3(posPred, i);
75 for (int a = 0; a < nx; ++a)
76 for (int b = 0; b < ny; ++b)
77 for (int c = 0; c < nz; ++c) {
78 const int ix = ax[a], iy = ay[b], iz = az[c];
79 if (ix == 0 && iy == 0 && iz == 0)
80 continue;
81 const int slot = Kokkos::atomic_fetch_add(&topGhost(), 1);
82 if (slot >= capacity) {
83 Kokkos::atomic_add(&topGhost(), -1);
84 continue;
85 }
86 const F3 shift{ix * dom.size.x, iy * dom.size.y, iz * dom.size.z};
87 st3(pos, slot, add3(p, shift));
88 st3(posPred, slot, add3(pPred, shift));
89 invMass(slot) = invMass(i);
90 st3(vel, slot, ldF3(vel, i));
91 st3(velPred, slot, ldF3(velPred, i));
92 st4(quat, slot, ldF4(quat, i));
93 st4(quatPred, slot, ldF4(quatPred, i));
94 st3(angVel, slot, ldF3(angVel, i));
95 st3(angVelPred, slot, ldF3(angVelPred, i));
96 scale(slot) = scale(i);
97 shapeId(slot) = shapeId(i);
98 realIndices(slot) = i;
99 if (gid.extent(0) > 0)
100 gid(slot) = gid(i);
101 // Ghost slots must carry the owner's material id: the narrowphase reads matId by RAW
102 // slot index, so a stale/zero ghost entry silently applied material 0's pair row to
103 // every cross-periodic-boundary contact.
104 if (materialId.extent(0) > 0)
105 materialId(slot) = materialId(i);
106 }
107 });
108 space.fence();
109}
110
111} // namespace peclet::dem
112
113#endif // DEM_PERIODICITY_HPP
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
dem — portable (Kokkos) time integration kernels (integration.cu).
void st3(const V3 &v, int i, F3 a)
void st4(const V4 &v, int i, F4 a)
Kokkos::View< float *, CpMem > Vf
Kokkos::View< int *, CpMem > Vi
void generateGhosts(int numReal, int capacity, Domain dom, float skin, const BodyState &bodies, const BodyPred &pred, Vi realIndices, Kokkos::View< int, CpMem > topGhost, Vi gid=Vi{}, Kokkos::View< unsigned char *, CpMem > materialId={})
Generate periodic ghosts for particles [0,numReal).
F3 ldF3(const V &v, int i)
Kokkos::DefaultExecutionSpace CpExec
Kokkos::View< float *[3], CpMem > V3
F4 ldF4(const V &v, int i)
Kokkos::View< float *[4], CpMem > V4
F3 add3(F3 a, F3 b)
int calculateGhostCapacity(int nReal, Domain dom, float skin)
Padded particle-array capacity that leaves room for the periodic ghosts generateGhosts will emit: the...
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 *, CpMem > scale
Kokkos::View< float *, CpMem > invMass
Kokkos::View< int *, CpMem > shapeId
Kokkos::View< float *[3], CpMem > angVel
Kokkos::View< float *[4], CpMem > quat
Kokkos::View< float *[3], CpMem > vel