peclet-dem
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
25inline int calculateGhostCapacity(int nReal, Domain dom, float skin) {
26 const float sx = dom.periodic_x ? skin : 0.0f;
27 const float sy = dom.periodic_y ? skin : 0.0f;
28 const float sz = dom.periodic_z ? skin : 0.0f;
29 const double innerX = (dom.size.x - 2.0f * sx > 0.0f) ? (dom.size.x - 2.0f * sx) : 0.0;
30 const double innerY = (dom.size.y - 2.0f * sy > 0.0f) ? (dom.size.y - 2.0f * sy) : 0.0;
31 const double innerZ = (dom.size.z - 2.0f * sz > 0.0f) ? (dom.size.z - 2.0f * sz) : 0.0;
32 const double volTotal = static_cast<double>(dom.size.x) * dom.size.y * dom.size.z;
33 if (volTotal <= 0.0)
34 return nReal * 8; // degenerate box (domain not set yet) -> generous fallback
35 const double ghostFraction = (volTotal - innerX * innerY * innerZ) / volTotal;
36 const double estGhosts = (ghostFraction > 0.5) ? nReal * 32.0 : nReal * ghostFraction * 4.0;
37 return nReal + static_cast<int>(estGhosts) + 4096; // + fixed buffer (matches CUDA)
38}
39
43inline void generateGhostsKokkos(int numReal, int capacity, Domain dom, float skin, V3 pos,
44 Vf invMass, V3 posPred, V3 vel, V3 velPred, V4 quat, V4 quatPred,
45 V3 angVel, V3 angVelPred, Vf scale, Vi shapeId, Vi realIndices,
46 Kokkos::View<int, CpMem> topGhost) {
47 using detail::st3;
48 using detail::st4;
49 CpExec space;
50 Kokkos::parallel_for(
51 "peclet::dem::generate_ghosts", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
52 KOKKOS_LAMBDA(int i) {
53 const F3 p = ldF3(pos, i);
54 const int sx = dom.periodic_x
55 ? ((p.x < dom.min.x + skin) ? 1 : ((p.x > dom.max.x - skin) ? -1 : 0))
56 : 0;
57 const int sy = dom.periodic_y
58 ? ((p.y < dom.min.y + skin) ? 1 : ((p.y > dom.max.y - skin) ? -1 : 0))
59 : 0;
60 const int sz = dom.periodic_z
61 ? ((p.z < dom.min.z + skin) ? 1 : ((p.z > dom.max.z - skin) ? -1 : 0))
62 : 0;
63 const int ax[2] = {0, sx}, ay[2] = {0, sy}, az[2] = {0, sz};
64 const int nx = (sx == 0) ? 1 : 2, ny = (sy == 0) ? 1 : 2, nz = (sz == 0) ? 1 : 2;
65
66 const F3 pPred = ldF3(posPred, i);
67 for (int a = 0; a < nx; ++a)
68 for (int b = 0; b < ny; ++b)
69 for (int c = 0; c < nz; ++c) {
70 const int ix = ax[a], iy = ay[b], iz = az[c];
71 if (ix == 0 && iy == 0 && iz == 0)
72 continue;
73 const int slot = Kokkos::atomic_fetch_add(&topGhost(), 1);
74 if (slot >= capacity) {
75 Kokkos::atomic_add(&topGhost(), -1);
76 continue;
77 }
78 const F3 shift{ix * dom.size.x, iy * dom.size.y, iz * dom.size.z};
79 st3(pos, slot, add3(p, shift));
80 st3(posPred, slot, add3(pPred, shift));
81 invMass(slot) = invMass(i);
82 st3(vel, slot, ldF3(vel, i));
83 st3(velPred, slot, ldF3(velPred, i));
84 st4(quat, slot, ldF4(quat, i));
85 st4(quatPred, slot, ldF4(quatPred, i));
86 st3(angVel, slot, ldF3(angVel, i));
87 st3(angVelPred, slot, ldF3(angVelPred, i));
88 scale(slot) = scale(i);
89 shapeId(slot) = shapeId(i);
90 realIndices(slot) = i;
91 }
92 });
93 space.fence();
94}
95
96} // namespace peclet::dem
97
98#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
Kokkos::View< float *[3], CpMem > V3
F3 ldF3(const V &v, int i)
F4 ldF4(const V &v, int i)
Kokkos::View< float *[4], CpMem > V4
F3 add3(F3 a, F3 b)
void generateGhostsKokkos(int numReal, int capacity, Domain dom, float skin, V3 pos, Vf invMass, V3 posPred, V3 vel, V3 velPred, V4 quat, V4 quatPred, V3 angVel, V3 angVelPred, Vf scale, Vi shapeId, Vi realIndices, Kokkos::View< int, CpMem > topGhost)
Generate periodic ghosts for particles [0,numReal).
Kokkos::DefaultExecutionSpace CpExec
int calculateGhostCapacity(int nReal, Domain dom, float skin)
Padded particle-array capacity that leaves room for the periodic ghosts generateGhostsKokkos will emi...