1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/* Copyright 2021 Andrew Myers
*
* This file is part of WarpX.
*
* License: BSD-3-Clause-LBNL
*/
#include "WarpX.H"
#include "EmbeddedBoundary/DistanceToEB.H"
#include "Particles/ParticleBoundaryBuffer.H"
#include "Particles/MultiParticleContainer.H"
#include "Utils/TextMsg.H"
#include "Utils/WarpXProfilerWrapper.H"
#include <ablastr/particles/NodalFieldGather.H>
#include <AMReX_Geometry.H>
#include <AMReX_ParmParse.H>
#include <AMReX_Reduce.H>
#include <AMReX_Tuple.H>
#include <AMReX.H>
struct IsOutsideDomainBoundary {
amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> m_plo;
amrex::GpuArray<amrex::Real, AMREX_SPACEDIM> m_phi;
int m_idim;
int m_iside;
template <typename SrcData>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
int operator() (const SrcData& src,
int ip, const amrex::RandomEngine& /*engine*/) const noexcept
{
const auto& p = src.getSuperParticle(ip);
if (m_iside == 0) {
if (p.pos(m_idim) < m_plo[m_idim]) { return 1; }
} else {
if (p.pos(m_idim) >= m_phi[m_idim]) { return 1; }
}
return 0;
}
};
struct CopyAndTimestamp {
int m_index;
int m_step;
template <typename DstData, typename SrcData>
AMREX_GPU_HOST_DEVICE
void operator() (const DstData& dst, const SrcData& src,
int src_i, int dst_i) const noexcept
{
dst.m_aos[dst_i] = src.m_aos[src_i];
for (int j = 0; j < SrcData::NAR; ++j)
dst.m_rdata[j][dst_i] = src.m_rdata[j][src_i];
for (int j = 0; j < src.m_num_runtime_real; ++j)
dst.m_runtime_rdata[j][dst_i] = src.m_runtime_rdata[j][src_i];
for (int j = 0; j < src.m_num_runtime_int; ++j)
dst.m_runtime_idata[j][dst_i] = src.m_runtime_idata[j][src_i];
dst.m_runtime_idata[m_index][dst_i] = m_step;
}
};
ParticleBoundaryBuffer::ParticleBoundaryBuffer ()
{
m_particle_containers.resize(numBoundaries());
m_do_boundary_buffer.resize(numBoundaries());
m_do_any_boundary.resize(numBoundaries(), 0);
m_boundary_names.resize(numBoundaries());
for (int i = 0; i < numBoundaries(); ++i)
{
m_particle_containers[i].resize(numSpecies());
m_do_boundary_buffer[i].resize(numSpecies(), 0);
}
for (int ispecies = 0; ispecies < numSpecies(); ++ispecies)
{
amrex::ParmParse pp_species(getSpeciesNames()[ispecies]);
#if defined(WARPX_DIM_1D_Z)
pp_species.query("save_particles_at_zlo", m_do_boundary_buffer[0][ispecies]);
pp_species.query("save_particles_at_zhi", m_do_boundary_buffer[1][ispecies]);
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
pp_species.query("save_particles_at_xlo", m_do_boundary_buffer[0][ispecies]);
pp_species.query("save_particles_at_xhi", m_do_boundary_buffer[1][ispecies]);
pp_species.query("save_particles_at_zlo", m_do_boundary_buffer[2][ispecies]);
pp_species.query("save_particles_at_zhi", m_do_boundary_buffer[3][ispecies]);
#else
pp_species.query("save_particles_at_xlo", m_do_boundary_buffer[0][ispecies]);
pp_species.query("save_particles_at_xhi", m_do_boundary_buffer[1][ispecies]);
pp_species.query("save_particles_at_ylo", m_do_boundary_buffer[2][ispecies]);
pp_species.query("save_particles_at_yhi", m_do_boundary_buffer[3][ispecies]);
pp_species.query("save_particles_at_zlo", m_do_boundary_buffer[4][ispecies]);
pp_species.query("save_particles_at_zhi", m_do_boundary_buffer[5][ispecies]);
#endif
#ifdef AMREX_USE_EB
pp_species.query("save_particles_at_eb", m_do_boundary_buffer[AMREX_SPACEDIM*2][ispecies]);
#endif
// Set the flag whether the boundary is active or any species
for (int i = 0; i < numBoundaries(); ++i) {
if (m_do_boundary_buffer[i][ispecies]) m_do_any_boundary[i] = 1;
}
}
#if defined(WARPX_DIM_1D_Z)
m_boundary_names[0] = "zlo";
m_boundary_names[1] = "zhi";
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
m_boundary_names[0] = "xlo";
m_boundary_names[1] = "xhi";
m_boundary_names[2] = "zlo";
m_boundary_names[3] = "zhi";
#else
m_boundary_names[0] = "xlo";
m_boundary_names[1] = "xhi";
m_boundary_names[2] = "ylo";
m_boundary_names[3] = "yhi";
m_boundary_names[4] = "zlo";
m_boundary_names[5] = "zhi";
#endif
#ifdef AMREX_USE_EB
m_boundary_names[AMREX_SPACEDIM*2] = "eb";
#endif
}
void ParticleBoundaryBuffer::printNumParticles () const {
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim)
{
for (int iside = 0; iside < 2; ++iside)
{
auto& buffer = m_particle_containers[2*idim+iside];
for (int i = 0; i < numSpecies(); ++i)
{
int np = buffer[i].isDefined() ? buffer[i].TotalNumberOfParticles(false) : 0;
amrex::Print() << Utils::TextMsg::Info(
"Species " + getSpeciesNames()[i] + " has "
+ std::to_string(np) + " particles in the boundary buffer "
+ "for side " + std::to_string(iside) + " of dim " + std::to_string(idim)
);
}
}
}
#ifdef AMREX_USE_EB
auto& buffer = m_particle_containers[2*AMREX_SPACEDIM];
for (int i = 0; i < numSpecies(); ++i)
{
int np = buffer[i].isDefined() ? buffer[i].TotalNumberOfParticles(false) : 0;
amrex::Print() << Utils::TextMsg::Info(
"Species " + getSpeciesNames()[i] + " has "
+ std::to_string(np) + " particles in the EB boundary buffer"
);
}
#endif
}
void ParticleBoundaryBuffer::redistribute () {
for (int i = 0; i < numBoundaries(); ++i)
{
auto& buffer = m_particle_containers[i];
for (int ispecies = 0; ispecies < numSpecies(); ++ispecies)
{
auto& species_buffer = buffer[ispecies];
if (species_buffer.isDefined()) {
species_buffer.Redistribute();
}
}
}
}
void ParticleBoundaryBuffer::clearParticles () {
for (int i = 0; i < numBoundaries(); ++i)
{
clearParticles(i);
}
}
void ParticleBoundaryBuffer::clearParticles (int const i) {
auto& buffer = m_particle_containers[i];
for (int ispecies = 0; ispecies < numSpecies(); ++ispecies)
{
auto& species_buffer = buffer[ispecies];
if (species_buffer.isDefined()) species_buffer.clearParticles();
}
}
void ParticleBoundaryBuffer::gatherParticles (MultiParticleContainer& mypc,
const amrex::Vector<const amrex::MultiFab*>& distance_to_eb)
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles");
using PIter = amrex::ParConstIter<0,0,PIdx::nattribs>;
const auto& warpx_instance = WarpX::GetInstance();
const amrex::Geometry& geom = warpx_instance.Geom(0);
auto plo = geom.ProbLoArray();
auto phi = geom.ProbHiArray();
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim)
{
if (geom.isPeriodic(idim)) continue;
for (int iside = 0; iside < 2; ++iside)
{
auto& buffer = m_particle_containers[2*idim+iside];
for (int i = 0; i < numSpecies(); ++i)
{
if (!m_do_boundary_buffer[2*idim+iside][i]) continue;
const WarpXParticleContainer& pc = mypc.GetParticleContainer(i);
if (!buffer[i].isDefined())
{
buffer[i] = pc.make_alike<amrex::PinnedArenaAllocator>();
buffer[i].AddIntComp("timestamp", false);
}
auto& species_buffer = buffer[i];
for (int lev = 0; lev < pc.numLevels(); ++lev)
{
const auto& plevel = pc.GetParticles(lev);
for(PIter pti(pc, lev); pti.isValid(); ++pti)
{
auto index = std::make_pair(pti.index(), pti.LocalTileIndex());
if(plevel.find(index) == plevel.end()) continue;
auto& ptile_buffer = species_buffer.DefineAndReturnParticleTile(
lev, pti.index(), pti.LocalTileIndex());
const auto& ptile = plevel.at(index);
auto np = ptile.numParticles();
if (np == 0) continue;
auto predicate = IsOutsideDomainBoundary{plo, phi, idim, iside};
const auto ptile_data = ptile.getConstParticleTileData();
amrex::ReduceOps<amrex::ReduceOpSum> reduce_op;
amrex::ReduceData<int> reduce_data(reduce_op);
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::count_out_of_bounds");
amrex::RandomEngine rng{};
reduce_op.eval(np, reduce_data, [=] AMREX_GPU_HOST_DEVICE (int ip)
{ return predicate(ptile_data, ip, rng) ? 1 : 0; });
}
auto dst_index = ptile_buffer.numParticles();
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::resize");
ptile_buffer.resize(dst_index + amrex::get<0>(reduce_data.value()));
}
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::filterAndTransform");
int timestamp_index = ptile_buffer.NumRuntimeIntComps()-1;
int timestep = warpx_instance.getistep(0);
amrex::filterAndTransformParticles(ptile_buffer, ptile,
predicate,
CopyAndTimestamp{timestamp_index, timestep},
0, dst_index);
}
}
}
}
}
}
#ifdef AMREX_USE_EB
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::EB");
auto& buffer = m_particle_containers[m_particle_containers.size()-1];
for (int i = 0; i < numSpecies(); ++i)
{
if (!m_do_boundary_buffer[AMREX_SPACEDIM*2][i]) continue;
const auto& pc = mypc.GetParticleContainer(i);
if (!buffer[i].isDefined())
{
buffer[i] = pc.make_alike<amrex::PinnedArenaAllocator>();
buffer[i].AddIntComp("timestamp", false);
}
auto& species_buffer = buffer[i];
for (int lev = 0; lev < pc.numLevels(); ++lev)
{
const auto& plevel = pc.GetParticles(lev);
auto dxi = warpx_instance.Geom(lev).InvCellSizeArray();
for(PIter pti(pc, lev); pti.isValid(); ++pti)
{
auto phiarr = (*distance_to_eb[lev])[pti].array(); // signed distance function
auto index = std::make_pair(pti.index(), pti.LocalTileIndex());
if(plevel.find(index) == plevel.end()) continue;
const auto getPosition = GetParticlePosition(pti);
auto& ptile_buffer = species_buffer.DefineAndReturnParticleTile(lev, pti.index(),
pti.LocalTileIndex());
const auto& ptile = plevel.at(index);
auto np = ptile.numParticles();
if (np == 0) continue;
using SrcData = WarpXParticleContainer::ParticleTileType::ConstParticleTileDataType;
auto predicate = [=] AMREX_GPU_HOST_DEVICE (const SrcData& /*src*/, const int ip)
/* NVCC 11.3.109 chokes in C++17 on this: noexcept */
{
amrex::ParticleReal xp, yp, zp;
getPosition(ip, xp, yp, zp);
amrex::Real phi_value = ablastr::particles::doGatherScalarFieldNodal(
xp, yp, zp, phiarr, dxi, plo
);
return phi_value < 0.0 ? 1 : 0;
};
const auto ptile_data = ptile.getConstParticleTileData();
amrex::ReduceOps<amrex::ReduceOpSum> reduce_op;
amrex::ReduceData<int> reduce_data(reduce_op);
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::count_out_of_boundsEB");
reduce_op.eval(np, reduce_data, [=] AMREX_GPU_HOST_DEVICE (int ip)
{ return predicate(ptile_data, ip) ? 1 : 0; });
}
auto dst_index = ptile_buffer.numParticles();
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::resize_eb");
ptile_buffer.resize(dst_index + amrex::get<0>(reduce_data.value()));
}
int timestamp_index = ptile_buffer.NumRuntimeIntComps()-1;
int timestep = warpx_instance.getistep(0);
{
WARPX_PROFILE("ParticleBoundaryBuffer::gatherParticles::filterTransformEB");
amrex::filterAndTransformParticles(ptile_buffer, ptile, predicate,
CopyAndTimestamp{timestamp_index, timestep}, 0, dst_index);
}
}
}
}
#else
amrex::ignore_unused(distance_to_eb);
#endif
}
int ParticleBoundaryBuffer::getNumParticlesInContainer(
const std::string species_name, int boundary) {
auto& buffer = m_particle_containers[boundary];
auto index = WarpX::GetInstance().GetPartContainer().getSpeciesID(species_name);
if (buffer[index].isDefined()) return buffer[index].TotalNumberOfParticles(false);
else return 0;
}
PinnedMemoryParticleContainer &
ParticleBoundaryBuffer::getParticleBuffer(const std::string species_name, int boundary) {
auto& buffer = m_particle_containers[boundary];
auto index = WarpX::GetInstance().GetPartContainer().getSpeciesID(species_name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(m_do_boundary_buffer[boundary][index],
"Attempted to get particle buffer for boundary "
+ std::to_string(boundary) + ", which is not used!");
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(buffer[index].isDefined(),
"Tried to get a buffer that is not defined!");
return buffer[index];
}
PinnedMemoryParticleContainer *
ParticleBoundaryBuffer::getParticleBufferPointer(const std::string species_name, int boundary) {
auto& buffer = m_particle_containers[boundary];
auto index = WarpX::GetInstance().GetPartContainer().getSpeciesID(species_name);
return &buffer[index];
}
|