/home/runner/work/spades/spades/Source/ParticleOps.H Source File

SPADES API: /home/runner/work/spades/spades/Source/ParticleOps.H Source File
SPADES API
ParticleOps.H
Go to the documentation of this file.
1#ifndef PARTICLEOPS_H
2#define PARTICLEOPS_H
3
4#include <AMReX.H>
5#include <AMReX_Array4.H>
6#include <AMReX_Print.H>
7#include "ParticleData.H"
8
9namespace spades::particles {
10
12template <size_t NRealT, size_t NIntT, class P, class RT, class IT>
14{
15 static constexpr size_t NREAL = NRealT;
16 static constexpr size_t NINT = NIntT;
17
25 P* aos, std::array<RT, NRealT>& rdata, std::array<IT, NIntT>& idata)
26 : m_aos(aos)
27 {
28 for (size_t i = 0; i < NRealT; i++) {
29 m_rdata[i] = rdata[i].data();
30 }
31 for (size_t i = 0; i < NIntT; i++) {
32 m_idata[i] = idata[i].data();
33 }
34 }
35
37 P* m_aos = nullptr;
38
40 amrex::GpuArray<amrex::Real*, NRealT> m_rdata;
41
43 amrex::GpuArray<int*, NIntT> m_idata;
44};
45
47struct Print
48{
54 template <class PArrs>
55 void operator()(const amrex::Long n, PArrs& parrs) const
56 {
57 auto& p = parrs.m_aos[n];
58 amrex::Print() << "Particle data: \n"
59 << " id = " << p.id() << "\n"
60 << " cpu = " << p.cpu() << "\n"
61 << " type = "
62 << parrs.m_idata[CommonIntData::type_id][n] << "\n";
63 for (size_t i = 0; i < PArrs::NINT; i++) {
64 amrex::Print() << " int comp(" << i
65 << ") = " << parrs.m_idata[i][n] << "\n";
66 }
67 for (size_t i = 0; i < PArrs::NREAL; i++) {
68 amrex::Print() << " real comp(" << i
69 << ") = " << parrs.m_rdata[i][n] << "\n";
70 }
71 }
72};
73
76{
82 template <class PArrs>
83 AMREX_GPU_DEVICE void operator()(const amrex::Long n, PArrs& parrs) const
84 {
85 auto& p = parrs.m_aos[n];
86 printf(
87 "Particle data: id = %ld, cpu = %ld, type = %d, timestamp = %.8e\n",
88 static_cast<amrex::Long>(p.id()), static_cast<amrex::Long>(p.cpu()),
89 parrs.m_idata[CommonIntData::type_id][n],
90 parrs.m_rdata[CommonRealData::timestamp][n]);
91 }
92};
93
95struct Copy
96{
103 template <class PArrs>
104 AMREX_GPU_DEVICE AMREX_FORCE_INLINE void
105 operator()(const amrex::Long p1, const amrex::Long p2, PArrs& parrs) const
106 {
107 for (size_t i = 0; i < PArrs::NREAL; i++) {
108 parrs.m_rdata[i][p2] = parrs.m_rdata[i][p1];
109 }
110 for (size_t i = 0; i < PArrs::NINT; i++) {
111 parrs.m_idata[i][p2] = parrs.m_idata[i][p1];
112 }
113 }
114};
115
117template <class PArrs>
118struct Get
119{
127 AMREX_GPU_DEVICE AMREX_FORCE_INLINE
128 Get(const amrex::IntVect& iv,
129 const amrex::Array4<const int>& counts,
130 const amrex::Array4<const int>& offsets,
131 PArrs& parrs)
132 : m_iv(iv), m_counts(counts), m_offsets(offsets), m_parrs(parrs)
133 {}
134
141 AMREX_GPU_DEVICE AMREX_FORCE_INLINE int
142 operator()(const int n, const int typ) const
143 {
144
145 AMREX_ASSERT(m_counts(m_iv, typ) > n);
146 const int idx = m_offsets(m_iv, typ) + n;
147
148 AMREX_ASSERT(m_parrs.m_aos[idx].id() >= 0);
149 AMREX_ASSERT(m_parrs.m_idata[CommonIntData::type_id][idx] == typ);
150
151 return idx;
152 }
153
160 AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool
161 check(const int n, const int typ) const
162 {
163 AMREX_ASSERT(m_counts(m_iv, typ) > n);
164 const int idx = m_offsets(m_iv, typ) + n;
165 return m_parrs.m_idata[CommonIntData::type_id][idx] == typ;
166 }
167
181 AMREX_GPU_DEVICE AMREX_FORCE_INLINE void
182 assert_different(const int n, const int typ, const int expected_type) const
183 {
184#ifdef AMREX_DEBUG
185 const int idx = m_offsets(m_iv, typ) + n;
186 AMREX_ASSERT(
187 m_parrs.m_idata[CommonIntData::type_id][idx] == expected_type);
188#else
189 amrex::ignore_unused(n, typ, expected_type);
190#endif
191 }
192
194 const amrex::IntVect& m_iv;
195
197 const amrex::Array4<const int>& m_counts;
198
200 const amrex::Array4<const int>& m_offsets;
201
203 PArrs& m_parrs;
204};
205
208{
213 template <class P>
214 AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool
215 operator()(const P& /*p1*/, const P& /*p2*/) const
216 {
217 return false;
218 }
219};
220
228{
236 template <class PArrs>
237 AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool
238 operator()(const amrex::Long p1, const amrex::Long p2, PArrs& parrs) const
239 {
240 // sort by iv, then particle type then by timestamp
241 const amrex::IntVect piv1(AMREX_D_DECL(
242 parrs.m_idata[CommonIntData::i][p1],
243 parrs.m_idata[CommonIntData::j][p1],
244 parrs.m_idata[CommonIntData::k][p1]));
245 const amrex::IntVect piv2(AMREX_D_DECL(
246 parrs.m_idata[CommonIntData::i][p2],
247 parrs.m_idata[CommonIntData::j][p2],
248 parrs.m_idata[CommonIntData::k][p2]));
249
250 const auto m1 = parrs.m_idata[CommonIntData::type_id][p1];
251 const auto m2 = parrs.m_idata[CommonIntData::type_id][p2];
252 const auto t1 = parrs.m_rdata[CommonRealData::timestamp][p1];
253 const auto t2 = parrs.m_rdata[CommonRealData::timestamp][p2];
254 return (piv1 < piv2) ||
255 (piv1 == piv2 && ((m1 < m2) || (m1 == m2 && t1 < t2)));
256 }
257};
258
259} // namespace spades::particles
260#endif
SPADES particles.
Definition EntityData.H:7
Functor for pairing particles.
Definition ParticleOps.H:208
AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool operator()(const P &, const P &) const
Compare particles for pairing.
Definition ParticleOps.H:215
@ type_id
Definition ParticleData.H:18
@ timestamp
Definition ParticleData.H:12
Functor for comparing particles.
Definition ParticleOps.H:228
AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool operator()(const amrex::Long p1, const amrex::Long p2, PArrs &parrs) const
Compare particles.
Definition ParticleOps.H:238
Functor for copying a particle's data to another.
Definition ParticleOps.H:96
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void operator()(const amrex::Long p1, const amrex::Long p2, PArrs &parrs) const
Copy particle data.
Definition ParticleOps.H:105
Functor for printing particle data on device.
Definition ParticleOps.H:76
AMREX_GPU_DEVICE void operator()(const amrex::Long n, PArrs &parrs) const
Print particle data to screen.
Definition ParticleOps.H:83
Functor for accessing a particle in a cell.
Definition ParticleOps.H:119
const amrex::Array4< const int > & m_offsets
Cell offsets of particle types.
Definition ParticleOps.H:200
PArrs & m_parrs
Particle array.
Definition ParticleOps.H:203
const amrex::IntVect & m_iv
Cell index.
Definition ParticleOps.H:194
AMREX_GPU_DEVICE AMREX_FORCE_INLINE Get(const amrex::IntVect &iv, const amrex::Array4< const int > &counts, const amrex::Array4< const int > &offsets, PArrs &parrs)
Constructor.
Definition ParticleOps.H:128
AMREX_GPU_DEVICE AMREX_FORCE_INLINE int operator()(const int n, const int typ) const
Get a particle in a cell.
Definition ParticleOps.H:142
AMREX_GPU_DEVICE AMREX_FORCE_INLINE bool check(const int n, const int typ) const
Check validity of the particle type.
Definition ParticleOps.H:161
const amrex::Array4< const int > & m_counts
Cell counts of particle types.
Definition ParticleOps.H:197
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void assert_different(const int n, const int typ, const int expected_type) const
Assert that the particle is of a different but expected type.
Definition ParticleOps.H:182
Particle operations.
Definition ParticleOps.H:14
amrex::GpuArray< amrex::Real *, NRealT > m_rdata
Pointers to real data.
Definition ParticleOps.H:40
ParticleArrays(P *aos, std::array< RT, NRealT > &rdata, std::array< IT, NIntT > &idata)
Constructor.
Definition ParticleOps.H:24
static constexpr size_t NREAL
Definition ParticleOps.H:15
P * m_aos
AOS data.
Definition ParticleOps.H:37
static constexpr size_t NINT
Definition ParticleOps.H:16
amrex::GpuArray< int *, NIntT > m_idata
Pointers to int data.
Definition ParticleOps.H:43
Functor for printing particle data on host.
Definition ParticleOps.H:48
void operator()(const amrex::Long n, PArrs &parrs) const
Print particle data to screen.
Definition ParticleOps.H:55