Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_ParticleContainerI.H
Go to the documentation of this file.
2
3#include <algorithm>
4#include <iterator>
5#include <set>
6#include <stdexcept>
7#include <string>
8#include <type_traits>
9#include <utility>
10#include <vector>
11
12namespace amrex {
13
14template <typename ParticleType, int NArrayReal, int NArrayInt,
15 template<class> class Allocator, class CellAssignor>
16void
18{
19 num_real_comm_comps = 0;
20 int comm_comps_start = 0;
21 if constexpr (!ParticleType::is_soa_particle) {
22 comm_comps_start += AMREX_SPACEDIM + NStructReal;
23 }
24 for (int i = comm_comps_start; i < comm_comps_start + NumRealComps(); ++i) {
25 if (h_redistribute_real_comp[i]) {++num_real_comm_comps;}
26 }
27
28 num_int_comm_comps = 0;
29 comm_comps_start = 2 + NStructInt;
30 for (int i = comm_comps_start; i < comm_comps_start + NumIntComps(); ++i) {
31 if (h_redistribute_int_comp[i]) {++num_int_comm_comps;}
32 }
33
34 if constexpr (ParticleType::is_soa_particle) {
35 particle_size = sizeof(uint64_t); // idcpu
36 } else {
37 particle_size = sizeof(ParticleType);
38 }
39 superparticle_size = particle_size +
40 num_real_comm_comps*sizeof(ParticleReal) + num_int_comm_comps*sizeof(int);
41}
42
43template <typename ParticleType, int NArrayReal, int NArrayInt,
44 template<class> class Allocator, class CellAssignor>
45void
47{
48 levelDirectoriesCreated = false;
49 usePrePost = false;
50 doUnlink = true;
51
52 // by default communicate all components
53 if constexpr (ParticleType::is_soa_particle)
54 {
55 h_redistribute_real_comp.resize(NArrayReal, true);
56 } else {
57 h_redistribute_real_comp.resize(AMREX_SPACEDIM + NStructReal + NArrayReal, true);
58 }
59 h_redistribute_int_comp.resize(2 + NStructInt + NArrayInt, true);
60
61 SetParticleSize();
62
63 // add default names for SoA Real and Int compile-time arguments
64 m_soa_rdata_names.clear();
65 for (int i=0; i<NArrayReal; ++i)
66 {
67 m_soa_rdata_names.push_back(getDefaultCompNameReal<ParticleType>(i));
68 }
69 m_soa_idata_names.clear();
70 for (int i=0; i<NArrayInt; ++i)
71 {
72 m_soa_idata_names.push_back(getDefaultCompNameInt<ParticleType>(i));
73 }
74
75 static_assert(sizeof(ParticleType)%sizeof(RealType) == 0,
76 "sizeof ParticleType is not a multiple of sizeof RealType");
77 static_assert(std::is_standard_layout_v<ParticleType>,
78 "Particle type must be standard layout");
79
80 // Process-wide parameters are read only once, so that building another
81 // container type does not override values that were changed in code.
82 ReadParameters();
83
84 // usePrePost and doUnlink belong to this container, so read them for every one.
85 ParmParse pp("particles");
86 pp.query("use_prepost", usePrePost);
87 pp.query("do_unlink", doUnlink);
88}
89
90template<
91 typename ParticleType,
92 int NArrayReal,
93 int NArrayInt,
94 template<class> class Allocator,
95 class CellAssignor
96>
97void
99 std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name
100)
101{
102 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(std::ssize(rdata_name) == NArrayReal, "rdata_name must be equal to NArrayReal");
103 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(std::ssize(idata_name) == NArrayInt, "idata_name must be equal to NArrayInt");
104
105 // ensure names for components are unique
106 std::set<std::string> const unique_r_names(rdata_name.begin(), rdata_name.end());
107 std::set<std::string> const unique_i_names(idata_name.begin(), idata_name.end());
108 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == unique_r_names.size(), "SetSoACompileTimeNames: Provided names in rdata_name are not unique!");
109 AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == unique_i_names.size(), "SetSoACompileTimeNames: Provided names in idata_name are not unique!");
110
111 for (int i=0; i<NArrayReal; ++i)
112 {
113 m_soa_rdata_names.at(i) = rdata_name.at(i);
114 }
115 for (int i=0; i<NArrayInt; ++i)
116 {
117 m_soa_idata_names.at(i) = idata_name.at(i);
118 }
119}
120
121template<
122 typename ParticleType,
123 int NArrayReal,
124 int NArrayInt,
125 template<class> class Allocator,
126 class CellAssignor
127>
128bool
130{
131 return std::ranges::find(m_soa_rdata_names, name) != std::end(m_soa_rdata_names);
132}
133
134template <typename ParticleType, int NArrayReal, int NArrayInt,
135 template<class> class Allocator, class CellAssignor>
136bool
138{
139 return std::ranges::find(m_soa_idata_names, name) != std::end(m_soa_idata_names);
140}
141
142template<
143 typename ParticleType,
144 int NArrayReal,
145 int NArrayInt,
146 template<class> class Allocator,
147 class CellAssignor
148>
149int
151{
152 const auto it = std::ranges::find(m_soa_rdata_names, name);
153
154 if (it == m_soa_rdata_names.end())
155 {
156 throw std::runtime_error("GetRealCompIndex: Component " + name + " does not exist!");
157 }
158 else
159 {
160 return std::distance(m_soa_rdata_names.begin(), it);
161 }
162}
163
164template<
165 typename ParticleType,
166 int NArrayReal,
167 int NArrayInt,
168 template<class> class Allocator,
169 class CellAssignor
170>
171int
173{
174 const auto it = std::ranges::find(m_soa_idata_names, name);
175
176 if (it == m_soa_idata_names.end())
177 {
178 throw std::runtime_error("GetIntCompIndex: Component " + name + " does not exist!");
179 }
180 else
181 {
182 return std::distance(m_soa_idata_names.begin(), it);
183 }
184}
185
186template <typename ParticleType, int NArrayReal, int NArrayInt,
187 template<class> class Allocator, class CellAssignor>
188template <typename P, typename Assignor>
191{
192 const Geometry& geom = Geom(lev);
193 const auto& domain = geom.Domain();
194 const auto& plo = geom.ProbLoArray();
195 const auto& dxi = geom.InvCellSizeArray();
196
197 return Assignor{}(p, plo, dxi, domain);
198}
199
200template <typename ParticleType, int NArrayReal, int NArrayInt,
201 template<class> class Allocator, class CellAssignor>
202template <typename P>
203bool
205::Where (const P& p,
206 ParticleLocData& pld,
207 int lev_min,
208 int lev_max,
209 IntVect nGrow,
210 int local_grid) const
211{
212
213 AMREX_ASSERT(m_gdb != nullptr);
214
215 if (lev_max == -1) {
216 lev_max = finestLevel();
217 }
218
219 AMREX_ASSERT(lev_max <= finestLevel());
220
221 AMREX_ASSERT(nGrow == 0 || (nGrow.allGE(0) && lev_min == lev_max));
222
223 std::vector< std::pair<int, Box> > isects;
224
225 for (int lev = lev_max; lev >= lev_min; lev--) {
226 const IntVect& iv = Index(p, lev);
227 if (lev == pld.m_lev) {
228 // The fact that we are here means this particle does not belong to any finer grids.
229 if (pld.m_grid >= 0) {
230 if (pld.m_grown_gridbox.contains(iv)) {
231 pld.m_cell = iv;
232 if (!pld.m_tilebox.contains(iv)) {
234 }
235 return true;
236 }
237 }
238 }
239
240 int grid;
241 const BoxArray& ba = ParticleBoxArray(lev);
243
244 if (local_grid < 0) {
245 bool findfirst = (nGrow == 0) ? true : false;
246 ba.intersections(Box(iv, iv), isects, findfirst, nGrow);
247 grid = isects.empty() ? -1 : isects[0].first;
248 if (nGrow != 0 && std::ssize(isects) > 1) {
249 for (auto & isect : isects) {
250 Box bx = ba.getCellCenteredBox(isect.first);
251 for (int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
252 Box gbx = bx;
254 gr[dir] = nGrow[dir];
255 gbx.grow(gr);
256 if (gbx.contains(iv)) {
257 grid = isect.first;
258 }
259 }
260 }
261 }
262 } else {
263 grid = (*redistribute_mask_ptr)[local_grid](iv, 0);
264 }
265
266 if (grid >= 0) {
267 const Box& bx = ba.getCellCenteredBox(grid);
268 pld.m_lev = lev;
269 pld.m_grid = grid;
270 pld.m_tile = getTileIndex(iv, bx, do_tiling, tile_size, pld.m_tilebox);
271 pld.m_cell = iv;
272 pld.m_gridbox = bx;
273 pld.m_grown_gridbox = amrex::grow(bx, nGrow);
274 return true;
275 }
276 }
277
278 return false;
279}
280
281template <typename ParticleType, int NArrayReal, int NArrayInt,
282 template<class> class Allocator, class CellAssignor>
283template <typename P>
284bool
287 ParticleLocData& pld,
288 int lev_min,
289 int lev_max,
290 int local_grid) const
291{
292
293 AMREX_ASSERT(m_gdb != nullptr);
294
295 if (!Geom(0).isAnyPeriodic()) { return false; }
296
297 if (lev_max == -1) {
298 lev_max = finestLevel();
299 }
300
301 AMREX_ASSERT(lev_max <= finestLevel());
302
303 // Shift the particle itself, so that the container's cell assignor, which may read more
304 // than the position, places it; the position is restored if no grid takes it.
306 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { pos_unshifted[idim] = p.pos(idim); }
307 if (PeriodicShift(p)) {
308 std::vector< std::pair<int,Box> > isects;
309 for (int lev = lev_max; lev >= lev_min; lev--) {
310
311 int grid;
312 IntVect iv;
313 const BoxArray& ba = ParticleBoxArray(lev);
315
316 if (local_grid < 0) {
317 iv = Index(p, lev);
318 ba.intersections(Box(iv, iv), isects, true, 0);
319 grid = isects.empty() ? -1 : isects[0].first;
320 } else {
321 iv = Index(p, lev);
322 if (ba[local_grid].contains(iv))
323 {
324 grid = local_grid;
325 }
326 else
327 {
328 ba.intersections(Box(iv, iv), isects, true, 0);
329 grid = isects.empty() ? -1 : isects[0].first;
330 if(grid == -1)
331 {
332 // the mask is indexed by the unshifted cell
333 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
334 std::swap(p.pos(idim), pos_unshifted[idim]);
335 }
336 grid = (*redistribute_mask_ptr)[local_grid](Index(p, lev), 0);
337 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
338 std::swap(p.pos(idim), pos_unshifted[idim]);
339 }
340 }
341 }
342 }
343
344 if (grid >= 0) {
345 const Box& bx = ba.getCellCenteredBox(grid);
346
347 pld.m_lev = lev;
348 pld.m_grid = grid;
349 pld.m_tile = getTileIndex(iv, bx, do_tiling, tile_size, pld.m_tilebox);
350 pld.m_cell = iv;
351 pld.m_gridbox = bx;
352 pld.m_grown_gridbox = bx;
353 return true;
354 }
355 }
356 }
357
358 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { p.pos(idim) = pos_unshifted[idim]; }
359 return false;
360}
361
362
363template <typename ParticleType, int NArrayReal, int NArrayInt,
364 template<class> class Allocator, class CellAssignor>
365template <typename P>
366bool
368::OutsidePeriodic (const P& p) const
369{
370 if (!Geom(0).isAnyPeriodic()) { return false; }
371 Particle<0, 0> pos_only;
372 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { pos_only.pos(idim) = p.pos(idim); }
373 return PeriodicShift(pos_only);
374}
375
376template <typename ParticleType, int NArrayReal, int NArrayInt,
377 template<class> class Allocator, class CellAssignor>
378template <typename P>
379bool
382{
383 const auto& geom = Geom(0);
384 const auto plo = geom.ProbLoArray();
385 const auto phi = geom.ProbHiArray();
386 const auto rlo = geom.ProbLoArrayInParticleReal();
387 const auto rhi = geom.ProbHiArrayInParticleReal();
388 const auto is_per = geom.isPeriodicArray();
389
390 return enforcePeriodic(p, plo, phi, rlo, rhi, is_per);
391}
392
393template <typename ParticleType, int NArrayReal, int NArrayInt,
394 template<class> class Allocator, class CellAssignor>
398 bool /*update*/,
399 bool verbose,
400 ParticleLocData pld) const
401{
402 AMREX_ASSERT(m_gdb != nullptr);
403
404 bool ok = Where(p, pld);
405
406 if (!ok && Geom(0).isAnyPeriodic())
407 {
408 // Attempt to shift the particle back into the domain if it
409 // crossed a periodic boundary.
411 ok = Where(p, pld);
412 }
413
414 if (!ok) {
415 // invalidate the particle.
416 if (verbose) {
417 amrex::AllPrint()<< "Invalidating out-of-domain particle: " << p << '\n';
418 }
419
420 AMREX_ASSERT(p.id().is_valid());
421
422 p.id().make_invalid();
423 }
424
425 return pld;
426}
427
428template <typename ParticleType, int NArrayReal, int NArrayInt,
429 template<class> class Allocator, class CellAssignor>
430void
436
437template <typename ParticleType, int NArrayReal, int NArrayInt,
438 template<class> class Allocator, class CellAssignor>
439void
446
447template <typename ParticleType, int NArrayReal, int NArrayInt,
448 template<class> class Allocator, class CellAssignor>
449template <typename P>
450void
451ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::locateParticle (P& p, ParticleLocData& pld,
452 int lev_min, int lev_max, IntVect nGrow, int local_grid) const
453{
454 // The same order as the GPU path (partitionParticlesByDest): a position outside the
455 // domain in a periodic direction is wrapped first, then the particle is placed with the
456 // container's cell assignor, and it has left the domain if that cell is outside. With the
457 // default assignor this is the same as testing the position against the roundoff domain,
458 // but a custom assignor may place a particle in the domain whatever its position, e.g.
459 // from its vertical cell index on a terrain-following grid.
460 bool success;
461 if (OutsidePeriodic(p))
462 {
463 // Note that EnforcePeriodicWhere may shift the particle if it is successful.
464 success = EnforcePeriodicWhere(p, pld, lev_min, lev_max, local_grid);
465 if (!success && lev_min == 0)
466 {
467 // The particle has left the domain; invalidate it.
468 p.id().make_invalid();
469 success = true;
470 }
471 }
472 else if (!Geom(0).Domain().contains(Index(p, 0)))
473 {
474 success = false;
475 if (lev_min == 0)
476 {
477 // The particle has left the domain; invalidate it.
478 p.id().make_invalid();
479 success = true;
480 }
481 }
482 else
483 {
484 success = Where(p, pld, lev_min, lev_max, IntVect(0), local_grid);
485 }
486
487 if (!success)
488 {
489 success = (nGrow != 0) && Where(p, pld, lev_min, lev_min, nGrow);
490 pld.m_grown_gridbox = pld.m_gridbox; // reset grown box for subsequent calls.
491 }
492
493 if (!success)
494 {
495 amrex::Abort("ParticleContainer::locateParticle(): invalid particle.");
496 }
497}
498
499template <typename ParticleType, int NArrayReal, int NArrayInt,
500 template<class> class Allocator, class CellAssignor>
501Long
503{
504 Long nparticles = 0;
505 for (int lev = 0; lev <= finestLevel(); lev++) {
506 nparticles += NumberOfParticlesAtLevel(lev,only_valid,true);
507 }
508 if (!only_local) {
510 }
511 return nparticles;
512}
513
514template <typename ParticleType, int NArrayReal, int NArrayInt,
515 template<class> class Allocator, class CellAssignor>
518{
519 AMREX_ASSERT(lev >= 0 && lev < std::ssize(m_particles));
520
521 LayoutData<Long> np_per_grid_local(ParticleBoxArray(lev),
523
524 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
525 {
526 int gid = pti.index();
527 if (only_valid)
528 {
529 const auto& ptile = ParticlesAt(lev, pti);
530 const int np = ptile.numParticles();
531 auto const ptd = ptile.getConstParticleTileData();
532
533 ReduceOps<ReduceOpSum> reduce_op;
534 ReduceData<int> reduce_data(reduce_op);
535 using ReduceTuple = typename decltype(reduce_data)::Type;
536
537 reduce_op.eval(np, reduce_data,
538 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
539 {
540 return (ptd.id(i).is_valid()) ? 1 : 0;
541 });
542
543 int np_valid = amrex::get<0>(reduce_data.value(reduce_op));
544 np_per_grid_local[gid] += np_valid;
545 } else
546 {
547 np_per_grid_local[gid] += pti.numParticles();
548 }
549 }
550
551 Vector<Long> nparticles(np_per_grid_local.size(), 0);
552 if (only_local)
554 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
555 {
556 nparticles[pti.index()] = np_per_grid_local[pti.index()];
557 }
558 }
559 else
560 {
561 ParallelDescriptor::GatherLayoutDataToVector(np_per_grid_local, nparticles,
563 ParallelDescriptor::Bcast(nparticles.data(), nparticles.size(),
566 }
567
568 return nparticles;
570
571template <typename ParticleType, int NArrayReal, int NArrayInt,
572 template<class> class Allocator, class CellAssignor>
575 Long nparticles = 0;
576
577 if (level < 0 || level >= std::ssize(m_particles)) { return nparticles; }
578
579 if (only_valid) {
580 ReduceOps<ReduceOpSum> reduce_op;
581 ReduceData<unsigned long long> reduce_data(reduce_op);
582 using ReduceTuple = typename decltype(reduce_data)::Type;
583
584 for (const auto& kv : GetParticles(level)) {
585 const auto& ptile = kv.second;
586 auto const ptd = ptile.getConstParticleTileData();
587
588 reduce_op.eval(ptile.numParticles(), reduce_data,
589 [=] AMREX_GPU_DEVICE (int i) -> ReduceTuple
590 {
591 return (ptd.id(i).is_valid()) ? 1 : 0;
592 });
593 }
595 nparticles = static_cast<Long>(amrex::get<0>(reduce_data.value(reduce_op)));
597 else {
598 for (const auto& kv : GetParticles(level)) {
599 const auto& ptile = kv.second;
600 nparticles += ptile.numParticles();
601 }
602 }
603
604 if (!only_local) {
606 }
607
608 return nparticles;
609}
610
611template <typename ParticleType, int NArrayReal, int NArrayInt,
612 template<class> class Allocator, class CellAssignor>
613template <std::integral Int>
614requires (sizeof(Int) >= sizeof(Long))
615void
617{
618 AMREX_ASSERT(lev >= 0 && lev < std::ssize(m_particles));
619
622
623 [[maybe_unused]] Gpu::NoSyncRegion no_sync{};
624 for (ParConstIterType pti(*this, lev); pti.isValid(); ++pti)
625 {
626 int gid = pti.index();
627 mem[gid] += static_cast<Int>(pti.capacity());
628 }
629}
630
632// This includes both valid and invalid particles since invalid particles still take up space.
633//
634
635template <typename ParticleType, int NArrayReal, int NArrayInt,
636 template<class> class Allocator, class CellAssignor>
637std::array<Long, 3>
640{
641 Long cnt = 0;
642
643 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
644 const auto& pmap = m_particles[lev];
645 for (const auto& kv : pmap) {
646 const auto& ptile = kv.second;
647 cnt += ptile.numParticles();
648 }
649 }
650
651 Long mn = cnt, mx = mn;
653 const int IOProc = ParallelContext::IOProcessorNumberSub();
654 const Long sz = sizeof(ParticleType)+NumRealComps()*sizeof(ParticleReal)+NumIntComps()*sizeof(int);
655
656#ifdef AMREX_LAZY
657 Lazy::QueueReduction( [=] () mutable {
658#endif
663 amrex::Print() << "ParticleContainer spread across MPI nodes - bytes (num particles): [Min: "
664 << mn*sz
665 << " (" << mn << ")"
666 << ", Max: "
667 << mx*sz
668 << " (" << mx << ")"
669 << ", Total: "
670 << cnt*sz
671 << " (" << cnt << ")]\n";
672#ifdef AMREX_LAZY
673 });
674#endif
675
676 return {mn*sz, mx*sz, cnt*sz};
677}
678
679template <typename ParticleType, int NArrayReal, int NArrayInt,
680 template<class> class Allocator, class CellAssignor>
681std::array<Long, 3>
684{
685 Long cnt = 0;
686
687 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
688 const auto& pmap = m_particles[lev];
689 for (const auto& kv : pmap) {
690 const auto& ptile = kv.second;
691 cnt += ptile.capacity();
692 }
693 }
694
695 Long mn = cnt, mx = mn;
696
697 const int IOProc = ParallelContext::IOProcessorNumberSub();
698
699#ifdef AMREX_LAZY
700 Lazy::QueueReduction( [=] () mutable {
701#endif
706 amrex::Print() << "ParticleContainer spread across MPI nodes - bytes: [Min: "
707 << mn
708 << ", Max: "
709 << mx
710 << ", Total: "
711 << cnt
712 << "]\n";
713#ifdef AMREX_LAZY
714 });
715#endif
716
717 return {mn, mx, cnt};
719
720template <typename ParticleType, int NArrayReal, int NArrayInt,
721 template<class> class Allocator, class CellAssignor>
722void
724{
725 for (unsigned lev = 0; lev < m_particles.size(); lev++) {
726 auto& pmap = m_particles[lev];
727 for (auto& kv : pmap) {
728 auto& ptile = kv.second;
729 ptile.shrink_to_fit();
730 }
731 }
732}
733
738
739
740template <typename ParticleType, int NArrayReal, int NArrayInt,
741 template<class> class Allocator, class CellAssignor>
742void
744{
745 BL_PROFILE("ParticleContainer::Increment");
746
747 AMREX_ASSERT(OK());
748 if (m_particles.empty()) { return; }
749 AMREX_ASSERT(lev >= 0 && lev < std::ssize(m_particles));
750 AMREX_ASSERT(numParticlesOutOfRange(*this, 0) == 0);
751
752 const auto& geom = Geom(lev);
753 const auto plo = geom.ProbLoArray();
754 const auto dxi = geom.InvCellSizeArray();
755 const auto domain = geom.Domain();
756 amrex::ParticleToMesh(*this, mf, lev,
757 [=] AMREX_GPU_DEVICE (const typename ParticleTileType::ConstParticleTileDataType& ptd, int ip,
758 amrex::Array4<amrex::Real> const& count)
759 {
760 const auto p = ptd[ip];
761 CellAssignor assignor;
762 IntVect iv = assignor(p, plo, dxi, domain);
763 amrex::Gpu::Atomic::AddNoRet(&count(iv), 1.0_rt);
764 }, false);
765}
766
767template <typename ParticleType, int NArrayReal, int NArrayInt,
768 template<class> class Allocator, class CellAssignor>
769Long
771{
772 BL_PROFILE("ParticleContainer::IncrementWithTotal(lev)");
773 Increment(mf, lev);
774 return TotalNumberOfParticles(true, local);
775}
776
777template <typename ParticleType, int NArrayReal, int NArrayInt,
778 template<class> class Allocator, class CellAssignor>
779void
781{
782 BL_PROFILE("ParticleContainer::RemoveParticlesAtLevel()");
783 if (level >= std::ssize(this->m_particles)) { return; }
784
785 if (!this->m_particles[level].empty())
786 {
787 ParticleLevel().swap(this->m_particles[level]);
788 }
789}
790
791template <typename ParticleType, int NArrayReal, int NArrayInt,
792 template<class> class Allocator, class CellAssignor>
793void
795{
796 BL_PROFILE("ParticleContainer::RemoveParticlesNotAtFinestLevel()");
797 AMREX_ASSERT(this->finestLevel()+1 == std::ssize(this->m_particles));
798
799 Long cnt = 0;
800
801 for (unsigned lev = 0; lev < m_particles.size() - 1; ++lev) {
802 auto& pmap = m_particles[lev];
803 if (!pmap.empty()) {
804 for (auto& kv : pmap) {
805 const auto& pbx = kv.second;
806 cnt += pbx.numParticles();
807 }
808 ParticleLevel().swap(pmap);
809 }
810 }
811
812 //
813 // Print how many particles removed on each processor if any were removed.
814 //
815 if (this->m_verbose > 1 && cnt > 0) {
816 amrex::AllPrint() << "Processor " << ParallelContext::MyProcSub() << " removed " << cnt
817 << " particles not in finest level\n";
818 }
819}
820
822{
823
827
829 const GpuArray<Real, AMREX_SPACEDIM> & dxi, const Box& domain)
830 : m_assign_buffer_grid(assign_buffer_grid), m_plo(plo), m_dxi(dxi), m_domain(domain)
831 {}
832
833 template <typename SrcData>
835 int operator() (const SrcData& src, int src_i) const noexcept
836 {
837 auto iv = getParticleCell(src, src_i, m_plo, m_dxi, m_domain);
838 return (m_assign_buffer_grid(iv).first != -1);
839 }
840};
841
843{
844 template <typename DstData, typename SrcData>
846 void operator() (DstData& dst, const SrcData& src,
847 int src_i, int dst_i) const noexcept
848 {
849 copyParticle(dst, src, src_i, dst_i);
850
852 dst.cpu(dst_i) = 0;
853 }
854};
855
856
857template <typename ParticleType, int NArrayReal, int NArrayInt,
858 template<class> class Allocator, class CellAssignor>
859void
860ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
861::CreateVirtualParticles (int level, AoS& virts) const
862{
863 ParticleTileType ptile;
864 CreateVirtualParticles(level, ptile);
865 ptile.GetArrayOfStructs().swap(virts);
866}
867
868template <typename ParticleType, int NArrayReal, int NArrayInt,
869 template<class> class Allocator, class CellAssignor>
870void
873{
874 BL_PROFILE("ParticleContainer::CreateVirtualParticles()");
875 AMREX_ASSERT(level > 0);
876 AMREX_ASSERT(virts.empty());
877
878 if (level >= std::ssize(m_particles)) {
879 return;
880 }
881
882 std::string const& aggregation_type = AggregationType();
883 int aggregation_buffer = AggregationBuffer();
884
885 if (aggregation_type == "None")
886 {
887 auto virts_offset = virts.numParticles();
888 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
889 {
890 const auto& src_tile = ParticlesAt(level, pti);
891
892 auto np = src_tile.numParticles();
893 virts.resize(virts_offset+np);
894 transformParticles(virts, src_tile, 0, virts_offset, np, TransformerVirt());
895 virts_offset += np;
896 }
897 }
898 if (aggregation_type == "Cell")
899 {
900 //Components would be based on
901 int nComp = AMREX_SPACEDIM + NStructReal + NArrayReal;
902 // NArrayReal, NStructInt, NArrayInt behavior as before
903 int nGhost = 0;
904 MultiFab mf(ParticleBoxArray(level), ParticleDistributionMap(level), nComp, nGhost);
905
906 nComp = 1 + NStructInt + NArrayInt;
907 iMultiFab imf(ParticleBoxArray(level), ParticleDistributionMap(level), nComp, nGhost);
908
909 const auto& geom = Geom(level);
910 const auto plo = geom.ProbLoArray();
911 const auto dxi = geom.InvCellSizeArray();
912 const auto domain = geom.Domain();
913
914 BoxList bl_buffer;
915 bl_buffer.complementIn(Geom(level).Domain(), ParticleBoxArray(level));
916 BoxArray buffer(std::move(bl_buffer));
917 buffer.grow(aggregation_buffer);
918
920 locator.build(buffer, geom, do_tiling, tile_size);
921 AssignGrid<DenseBinIteratorFactory<Box>> assign_buffer_grid = locator.getGridAssignor();
922
923 amrex::ParticleToMesh(*this, mf, level,
924 [=] AMREX_GPU_DEVICE (const ParticleType& p,
925 amrex::Array4<amrex::Real> const& partData,
928 {
929 auto iv = getParticleCell(p, plo_loc, dxi_loc, domain);
930 if(assign_buffer_grid(iv).first == -1)
931 {
932 //Ordering may make this not unique
933 for (int i = 0; i < NArrayReal; ++i)
934 {
935 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+NStructReal+i), partData(iv,AMREX_SPACEDIM)!=0.0 ? static_cast<Real>(0) : static_cast<Real>(p.rdata(NStructReal+i)));
936 }
937 //Add the rdata(0)-weighted sum of position
938 for (int i = 0; i < AMREX_SPACEDIM; ++i)
939 {
940 amrex::Gpu::Atomic::AddNoRet(&partData(iv,i), static_cast<Real>((p.rdata(0)*p.pos(i))));
941 }
942 //Add the rdata(0)-weighted sum of other rdata fields
943 for (int i = 1; i < NStructReal; ++i)
944 {
945 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+i), static_cast<Real>((p.rdata(0)*p.rdata(i))));
946 }
947 //Add the rdata(0) sum
948 for (int i = 0; i < 1; ++i)
949 {
950 amrex::Gpu::Atomic::AddNoRet(&partData(iv,AMREX_SPACEDIM+i), static_cast<Real>(p.rdata(0)));
951 }
952 }
953
954 }); //skipping extra false argument, doing mf.setVal(0) at beginning
955
956 amrex::ParticleToMesh(*this, imf, level,
957 [=] AMREX_GPU_DEVICE (const ParticleType& p,
958 amrex::Array4<int> const& partData,
961 {
962
963 auto iv = getParticleCell(p, plo_loc, dxi_loc, domain);
964 if(assign_buffer_grid(iv).first == -1)
965 {
966 //if this cell has no particle id info, do a straight copy to store idata
967 if(partData(iv,0)==0)
968 {
969 //Add 1 to indicate at least 1 particle at cell iv
970 amrex::Gpu::Atomic::AddNoRet(&partData(iv,0), 1);
971 for (int i = 0; i < NStructInt; ++i)
972 {
973 amrex::Gpu::Atomic::AddNoRet(&partData(iv,1+i), p.idata(i));
974 }
975 for (int i = 0; i < NArrayInt; ++i)
976 {
977 amrex::Gpu::Atomic::AddNoRet(&partData(iv,1+NStructInt+i), p.idata(NStructInt+i));
978 }
979 }
980 }
981 });
982
983 //There may be a better way to ensure virts is the right length
984 virts.resize(imf.sum(0));
985
986 int last_offset = 0;
987 for (MFIter mfi(mf); mfi.isValid(); ++mfi)
988 {
989 const auto bx = mfi.tilebox();
990 const auto partData = mf.array(mfi);
991 const auto imf_arr = imf.array(mfi);
992
993 Gpu::DeviceVector<int> offsets(bx.numPts());
994 auto *offsets_ptr = offsets.dataPtr();
995 int next_offset = Scan::ExclusiveSum((int) bx.numPts(),(imf_arr.ptr(bx.smallEnd(),0)),(offsets.dataPtr()),Scan::retSum);
996 auto dst = virts.getParticleTileData();
997 ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
998 {
999 if(imf_arr(i,j,k,0)!=0)
1000 {
1001 const auto idx = last_offset + offsets_ptr[
1002 imf_arr.get_offset(IntVectND<3>{i,j,k})
1003 ];
1004
1005 dst.cpu(idx) = 0;
1007
1008 auto& p = dst[idx];
1009 //Set rdata(0) first so we can normalize the weighted fields
1010 //Note that this does not work for soa PC
1011 p.rdata(0) = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM));;
1012 //Set pos with the normalized weighted field
1013 for (int n = 0; n < AMREX_SPACEDIM; ++n)
1014 {
1015 p.pos(n) = static_cast<ParticleReal>(partData(i,j,k,n) / p.rdata(0));
1016 }
1017 //Set rdata(n>0) with the normalized weighted field for NStructReal
1018 for (int n = 1; n < NStructReal; ++n)
1019 {
1020 p.rdata(n) = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM+n) / p.rdata(0));
1021 }
1022 //Set rdata(n>0) with the normalized weighted field for NArrayReal
1023 for (int n = 0; n < NArrayReal; ++n)
1024 {
1025 dst.rdata(n)[idx] = static_cast<ParticleReal>(partData(i,j,k,AMREX_SPACEDIM+NStructReal+n));
1026 }
1027 //Set idata with the "first" particles idata field for NStructInt
1028 for (int n = 0; n < NStructInt; ++n)
1029 {
1030 p.idata(n) = imf_arr(i,j,k,1+n);
1031 }
1032 //Set idata with the "first" particles idata field for NArrayInt
1033 for (int n = 0; n < NArrayInt; ++n)
1034 {
1035 dst.idata(n)[idx] = imf_arr(i,j,k,1+NStructInt+n);
1036 }
1037 }
1038
1039 });
1040 last_offset+=next_offset;
1042 }
1043
1044 // last_offset should equal virts.numParticles()
1045 auto virts_offset = last_offset;
1046 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
1047 {
1048 const auto& src_tile = ParticlesAt(level, pti);
1049
1050 auto np = src_tile.numParticles();
1051 virts.resize(virts_offset+np);
1052 virts_offset += filterAndTransformParticles(virts, src_tile, FilterVirt(assign_buffer_grid,plo,dxi,domain), TransformerVirt(),0,virts_offset);
1054 }
1055 virts.resize(virts_offset);
1057 }
1058}
1059
1061{
1062
1065
1071 : m_lev_min(level), m_lev_max(level+1), m_nGrow(nGrow), m_gid(gid), m_assign_grid(assign_grid)
1072 {}
1073
1074 template <typename SrcData>
1076 int operator() (const SrcData& src, int src_i) const noexcept
1077 {
1078 const auto tup_min = (m_assign_grid)(src[src_i], m_lev_min, m_lev_max, m_nGrow, DefaultAssignor{});
1079 const auto tup_max = (m_assign_grid)(src[src_i], m_lev_max, m_lev_max, m_nGrow, DefaultAssignor{});
1080 const auto p_boxes = amrex::get<0>(tup_min);
1081 const auto p_boxes_max = amrex::get<0>(tup_max);
1082 const auto p_levs_max = amrex::get<2>(tup_max);
1083 return p_boxes_max >=0 && p_boxes == m_gid && p_levs_max == m_lev_max;
1084 }
1085};
1086
1088{
1089
1090 template <typename DstData, typename SrcData>
1092 void operator() (DstData& dst, const SrcData& src,
1093 int src_i, int dst_i) const noexcept
1094 {
1095 copyParticle(dst, src, src_i, dst_i);
1096
1097 dst.id(dst_i) = LongParticleIds::GhostParticleID;
1098 dst.cpu(dst_i) = 0;
1099 }
1100};
1101
1102template <typename ParticleType, int NArrayReal, int NArrayInt,
1103 template<class> class Allocator, class CellAssignor>
1104void
1105ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
1106::CreateGhostParticles (int level, int nGrow, AoS& ghosts) const
1107{
1108 ParticleTileType ptile;
1109 CreateGhostParticles(level, nGrow, ptile);
1110 ptile.GetArrayOfStructs().swap(ghosts);
1111}
1112
1113template <typename ParticleType, int NArrayReal, int NArrayInt,
1114 template<class> class Allocator, class CellAssignor>
1115void
1117::CreateGhostParticles (int level, int nGrow, ParticleTileType& ghosts) const
1118{
1119 BL_PROFILE("ParticleContainer::CreateGhostParticles()");
1120 AMREX_ASSERT(ghosts.empty());
1121 AMREX_ASSERT(level < finestLevel());
1122
1123 if (level >= std::ssize(m_particles)) {
1124 return;
1125 }
1126
1127 if (! m_particle_locator.isValid(GetParGDB(), do_tiling, tile_size)) {
1129 }
1130
1131 m_particle_locator.setGeometry(GetParGDB());
1132 AmrAssignGrid<DenseBinIteratorFactory<Box>> assign_grid = m_particle_locator.getGridAssignor();
1133 auto ghost_offset = ghosts.numParticles();
1134 for(ParConstIterType pti(*this, level); pti.isValid(); ++pti)
1135 {
1136 const auto& src_tile = ParticlesAt(level, pti);
1137 int gid = pti.index();
1138
1139 auto np = src_tile.numParticles();
1140 ghosts.resize(ghost_offset+np);
1141 ghost_offset += filterAndTransformParticles(ghosts, src_tile, AssignGridFilter(assign_grid,gid,level,nGrow), TransformerGhost(),0,ghost_offset);
1142 }
1143 ghosts.resize(ghost_offset);
1145}
1146
1147template <typename ParticleType, int NArrayReal, int NArrayInt,
1148 template<class> class Allocator, class CellAssignor>
1149void
1152{
1153 BL_PROFILE("ParticleContainer::clearParticles()");
1154
1155 for (int lev = 0; lev < std::ssize(m_particles); ++lev)
1156 {
1157 for (auto& kv : m_particles[lev]) { kv.second.resize(0); }
1158 particle_detail::clearEmptyEntries(m_particles[lev]);
1159 }
1160}
1161
1162template <typename ParticleType, int NArrayReal, int NArrayInt,
1163 template<class> class Allocator, class CellAssignor>
1164template <class PCType>
1166void
1168copyParticles (const PCType& other, bool local)
1169{
1170 using PData = typename ParticleTileType::ConstParticleTileDataType;
1171 copyParticles(other, [] AMREX_GPU_HOST_DEVICE (const PData& /*data*/, int /*i*/) { return 1; }, local);
1172}
1173
1174template <typename ParticleType, int NArrayReal, int NArrayInt,
1175 template<class> class Allocator, class CellAssignor>
1176template <class PCType>
1178void
1180addParticles (const PCType& other, bool local)
1181{
1182 using PData = typename ParticleTileType::ConstParticleTileDataType;
1183 addParticles(other, [] AMREX_GPU_HOST_DEVICE (const PData& /*data*/, int /*i*/) { return 1; }, local);
1184}
1185
1186template <typename ParticleType, int NArrayReal, int NArrayInt,
1187 template<class> class Allocator, class CellAssignor>
1188template <class F, class PCType>
1189requires ((IsParticleContainer<PCType>::value) && (!std::is_integral_v<F>))
1190void
1192copyParticles (const PCType& other, F&& f, bool local)
1193{
1194 BL_PROFILE("ParticleContainer::copyParticles");
1196 addParticles(other, std::forward<F>(f), local);
1197}
1198
1199template <typename ParticleType, int NArrayReal, int NArrayInt,
1200 template<class> class Allocator, class CellAssignor>
1201template <class F, class PCType>
1202requires ((IsParticleContainer<PCType>::value) && (!std::is_integral_v<F>))
1203void
1205addParticles (const PCType& other, F const& f, bool local)
1206{
1207 BL_PROFILE("ParticleContainer::addParticles");
1208
1209 // touch all tiles in serial
1210 for (int lev = 0; lev < other.numLevels(); ++lev)
1211 {
1212 [[maybe_unused]] Gpu::NoSyncRegion no_sync{};
1213 const auto& plevel_other = other.GetParticles(lev);
1214 for(MFIter mfi = other.MakeMFIter(lev); mfi.isValid(); ++mfi)
1215 {
1216 auto index = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1217 if(!plevel_other.contains(index)) { continue; }
1218
1219 DefineAndReturnParticleTile(lev, mfi.index(), mfi.LocalTileIndex());
1220 }
1221 }
1222
1223#ifdef AMREX_USE_OMP
1224#pragma omp parallel if (Gpu::notInLaunchRegion())
1225#endif
1226 for (int lev = 0; lev < other.numLevels(); ++lev)
1227 {
1228 const auto& plevel_other = other.GetParticles(lev);
1229 for(MFIter mfi = other.MakeMFIter(lev); mfi.isValid(); ++mfi)
1230 {
1231 auto index = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1232 if(!plevel_other.contains(index)) { continue; }
1233
1234 // this has already had define() called above
1235 auto& ptile = ParticlesAt(lev, mfi.index(), mfi.LocalTileIndex());
1236 const auto& ptile_other = plevel_other.at(index);
1237 auto np = ptile_other.numParticles();
1238 if (np == 0) { continue; }
1239
1240 auto dst_index = ptile.numParticles();
1241 ptile.resize(dst_index + np);
1242
1243 auto count = amrex::filterParticles(ptile, ptile_other, f,
1244 static_cast<decltype(np)>(0), dst_index, np);
1245
1246 ptile.resize(dst_index + count);
1247 }
1248 }
1249
1250 if (! local) { Redistribute(); }
1251}
1252
1253//
1254// This redistributes valid particles and discards invalid ones.
1255//
1256template <typename ParticleType, int NArrayReal, int NArrayInt,
1257 template<class> class Allocator, class CellAssignor>
1258void
1260::Redistribute (int lev_min, int lev_max, IntVect nGrow,
1261 bool local, IntVect max_cells_moved, bool remove_negative)
1263 BL_PROFILE_SYNC_START_TIMED("SyncBeforeComms: Redist");
1264
1266 !is_rtsoa_pc || NumRuntimeRealComps() >= AMREX_SPACEDIM,
1267 "ParticleContainer with RTSoA requires at least AMREX_SPACEDIM "
1268 "runtime real components for positions"
1269 );
1270
1271 Redistribute_impl(lev_min, lev_max, nGrow, local, max_cells_moved,
1272 remove_negative);
1275}
1276
1277template <typename ParticleType, int NArrayReal, int NArrayInt,
1278 template<class> class Allocator, class CellAssignor>
1279template <class index_type>
1280void
1282::ReorderParticles (int lev, const MFIter& mfi, const index_type* permutations)
1283{
1284 auto& ptile = ParticlesAt(lev, mfi);
1285 const size_t np = ptile.numParticles();
1286 const size_t np_total = np + ptile.numNeighborParticles();
1287
1288 if (memEfficientSort) {
1289 amrex::ReorderParticles(ptile, permutations);
1290 } else {
1292 ptile_tmp.define(m_num_runtime_real, m_num_runtime_int,
1293 &m_soa_rdata_names, &m_soa_idata_names, arena());
1294 ptile_tmp.resize(np_total);
1295 // copy re-ordered particles
1296 gatherParticles(ptile_tmp, ptile, np, permutations);
1297 // copy neighbor particles
1298 amrex::copyParticles(ptile_tmp, ptile, np, np, np_total-np);
1299 ptile.swap(ptile_tmp);
1300 }
1301}
1302
1303template <typename ParticleType, int NArrayReal, int NArrayInt,
1304 template<class> class Allocator, class CellAssignor>
1305void
1310
1311template <typename ParticleType, int NArrayReal, int NArrayInt,
1312 template<class> class Allocator, class CellAssignor>
1313void
1316{
1317 BL_PROFILE("ParticleContainer::SortParticlesByBin()");
1318
1319 if (bin_size == IntVect::TheZeroVector()) { return; }
1320
1321 for (int lev = 0; lev < numLevels(); ++lev)
1322 {
1323 const Geometry& geom = Geom(lev);
1324 const auto dxi = geom.InvCellSizeArray();
1325 const auto plo = geom.ProbLoArray();
1326 const auto domain = geom.Domain();
1327
1328 for(MFIter mfi = MakeMFIter(lev); mfi.isValid(); ++mfi)
1329 {
1330 auto& ptile = ParticlesAt(lev, mfi);
1331 const size_t np = ptile.numParticles();
1332
1333 const Box& box = mfi.validbox();
1334
1335 int ntiles = numTilesInBox(box, true, bin_size);
1336
1337 m_bins.build(np, ptile.getParticleTileData(), ntiles,
1338 GetParticleBin{.plo = plo, .dxi = dxi, .domain = domain,
1339 .bin_size = bin_size, .box = box});
1340 ReorderParticles(lev, mfi, m_bins.permutationPtr());
1341 }
1342 }
1343}
1344
1345template <typename ParticleType, int NArrayReal, int NArrayInt,
1346 template<class> class Allocator, class CellAssignor>
1347void
1350{
1351 BL_PROFILE("ParticleContainer::SortParticlesForDeposition()");
1352
1353 for (int lev = 0; lev < numLevels(); ++lev)
1354 {
1355 const Geometry& geom = Geom(lev);
1356
1357 for(MFIter mfi = MakeMFIter(lev); mfi.isValid(); ++mfi)
1358 {
1359 const auto& ptile = ParticlesAt(lev, mfi);
1360 const size_t np = ptile.numParticles();
1361
1362 const Box& box = mfi.validbox();
1363
1364 using index_type = typename decltype(m_bins)::index_type;
1366 PermutationForDeposition<index_type>(perm, np, ptile, box, geom, idx_type);
1367 ReorderParticles(lev, mfi, perm.dataPtr());
1368 }
1369 }
1370}
1371
1372template <typename ParticleType, int NArrayReal, int NArrayInt,
1373 template<class> class Allocator, class CellAssignor>
1375int
1377::hostPartitionTile (ParticleTileType& src_tile,
1378 int lev, int gid, int tid,
1379 int lev_min, int lev_max, IntVect nGrow, bool local,
1380 bool remove_negative, int myproc,
1382 Gpu::DeviceVector<int>& levels,
1384 Gpu::DeviceVector<int>& src_indices,
1385 Gpu::DeviceVector<IntVect>& periodic_shift)
1386{
1387 ParticleLocData pld;
1388 auto ptd = src_tile.getParticleTileData();
1389 int num_move = 0;
1390
1391 Long last = src_tile.numParticles() - 1;
1392 Long pindex = 0;
1393 int who = -1;
1394 while (pindex <= last) {
1395 decltype(auto) p = ptd[pindex];
1396
1397 // remove invalid if needed
1398 if (!ptd.id(pindex).is_valid()) {
1399 if (remove_negative) {
1400 copyParticle(ptd, ptd, last, pindex);
1401 correctCellVectors(last, pindex, gid, p);
1402 --last;
1403 } else {
1404 ++pindex;
1405 }
1406 continue;
1408
1409 // Fast path: if on the finest level, try the last good location first
1410 // If not on finest level, we need to do the full locate because even if
1411 // the last assignment works, the particle might belong on a finer level.
1412 // A particle past a periodic boundary is wrapped first, as on the GPU path, even if
1413 // the cell assignor still puts it in this tile.
1414 if (lev == lev_max && pld.m_tile == tid && pld.m_grid == gid && pld.m_lev == lev && who == myproc
1415 && !OutsidePeriodic(p)) {
1416 const auto iv = Index(p, lev);
1417 if (pld.m_tilebox.contains(iv)) {
1418 ++pindex;
1419 continue; // don't need correctCellVectors or particlePostLocate if it stays where it is
1420 }
1421 }
1422
1423 // full locate
1424 locateParticle(p, pld, lev_min, lev_max, nGrow, local ? gid : -1);
1425 particlePostLocate(p, pld, lev);
1426
1427 // particle may have been invalidated, so check if we need to remove again.
1428 if (!ptd.id(pindex).is_valid()) {
1429 copyParticle(ptd, ptd, last, pindex);
1430 correctCellVectors(last, pindex, gid, p);
1431 --last;
1432 continue;
1433 }
1434
1435 // flag particle for move if needed
1436 who = BufferMap().procID(pld.m_grid, pld.m_tile, pld.m_lev);
1437 if (pld.m_lev != lev || pld.m_grid != gid || pld.m_tile != tid || who != myproc) {
1438 // the particle is valid but needs to be sent somewhere else
1439 swapParticle(ptd, ptd, last, pindex);
1440 correctCellVectors(last, pindex, gid, p);
1441 boxes[num_move] = pld.m_grid;
1442 levels[num_move] = pld.m_lev;
1443 tiles[num_move] = pld.m_tile;
1444 src_indices[num_move] = static_cast<int>(last);
1445 ++num_move;
1446 --last;
1447 continue;
1448 }
1449
1450 ++pindex; // if here, particle stays
1451 }
1452
1453 boxes.resize(num_move);
1454 levels.resize(num_move);
1455 tiles.resize(num_move);
1456 src_indices.resize(num_move);
1457 periodic_shift.resize(num_move);
1458
1459 return static_cast<int>(pindex);
1460}
1461
1462template <typename ParticleType, int NArrayReal, int NArrayInt,
1463 template<class> class Allocator, class CellAssignor>
1464bool
1466::RedistributeInPlace (int lev_min, int lev_max, IntVect nGrow, bool remove_negative)
1467{
1468 if (!redistribute_early_out) { return false; }
1469
1470 // a single process, level, box and tile: there is nowhere else a particle could go
1471 if (ParallelContext::NProcsSub() != 1) { return false; }
1472 if (lev_min != 0 || lev_max != 0 || std::ssize(m_particles) != 1) { return false; }
1473
1474 const BoxArray& ba = ParticleBoxArray(0);
1475 if (ba.size() != 1) { return false; }
1476 const Box box = ba.getCellCenteredBox(0);
1477 if (numTilesInBox(box, do_tiling, tile_size) != 1) { return false; }
1478
1479 const int gid = 0;
1480 const int tid = 0;
1481 ParticleTileType* ptile = nullptr;
1482 for (auto& kv : m_particles[0]) {
1483 // the full Redistribute drops neighbor particles
1484 if (kv.second.numNeighborParticles() > 0) { return false; }
1485 if (kv.second.numParticles() == 0) { continue; }
1486 if (kv.first != std::make_pair(gid, tid)) { return false; }
1487 ptile = &kv.second;
1488 }
1489
1490 BL_PROFILE("ParticleContainer::RedistributeInPlace()");
1491
1492 if (ptile != nullptr)
1493 {
1494 // A tile filled before it was defined has no runtime components yet. Define
1495 // it and size them to the particle count, with zeros for the new values.
1496 // This is rarely needed, so check first.
1497 if (!particle_detail::soaIsSized(*ptile, NumRealComps(), NumIntComps())) {
1498 ptile = &DefineAndReturnParticleTile(0, gid, tid);
1499 particle_detail::resizeAndZeroNewSoA(*ptile, ptile->numParticles(), true);
1500 }
1501
1502#ifdef AMREX_USE_GPU
1503 // the same particles stay, shifted the same way and in the same order, as in
1504 // partitionParticlesByDest()
1505 const particle_detail::SingleBoxLocator<CellAssignor> loc(Geom(0), box, nGrow);
1506 Gpu::DeviceVector<uint8_t> stays(ptile->numParticles());
1507 const auto counts = particle_detail::singleBoxRedistributeLocate(
1508 *ptile, loc, remove_negative, stays.dataPtr());
1509 const int num_removed = amrex::get<0>(counts);
1510 const int num_invalid = amrex::get<1>(counts);
1511 const int num_kept_unshifted = amrex::get<2>(counts);
1512
1513 // the shifts are the same in the full Redistribute
1514 if (num_kept_unshifted > 0) { return false; }
1515
1516 if (num_removed > 0 || (remove_negative && num_invalid > 0)) {
1517 ptile->resize(particle_detail::partitionParticlesByFlag(*ptile, stays.dataPtr()));
1519 }
1520#else
1521 // the same steps, in the same order, as hostPartitionTile() with locateParticle()
1522 const Geometry& geom = Geom(0);
1523 const Box grown_box = amrex::grow(box, nGrow);
1524 auto ptd = ptile->getParticleTileData();
1525 Long last = ptile->numParticles() - 1;
1526 Long pindex = 0;
1527 bool located = false; // after the first locate, the box is tested first
1528 while (pindex <= last) {
1529 decltype(auto) p = ptd[pindex];
1530
1531 bool stays = true;
1532 if (!ptd.id(pindex).is_valid()) {
1533 stays = !remove_negative;
1534 } else if (OutsidePeriodic(p)) {
1535 // wrapped first, as on the GPU path; stays if then in the box
1536 stays = false;
1538 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { pos_unshifted[idim] = p.pos(idim); }
1539 if (PeriodicShift(p) && box.contains(Index(p, 0))) {
1540 stays = true;
1541 located = true;
1542 } else {
1543 for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) { p.pos(idim) = pos_unshifted[idim]; }
1544 }
1545 } else if (located && box.contains(Index(p, 0))) {
1546 stays = true;
1547 } else if (!geom.Domain().contains(Index(p, 0))) {
1548 // left the domain
1549 stays = false;
1550 } else if (grown_box.contains(Index(p, 0))) {
1551 located = true;
1552 } else {
1553 // in no box: the full Redistribute aborts
1554 return false;
1555 }
1556
1557 if (!stays) {
1558 copyParticle(ptd, ptd, last, pindex);
1559 correctCellVectors(static_cast<int>(last), static_cast<int>(pindex), gid, p);
1560 --last;
1561 continue;
1562 }
1563 ++pindex;
1564 }
1565 ptile->resize(pindex);
1566#endif
1567 }
1568
1569 // leave the same tiles as the full Redistribute: none but the local one, also if empty
1570 particle_detail::clearEmptyEntries(m_particles[0]);
1571 DefineAndReturnParticleTile(0, gid, tid);
1572
1573 AMREX_ASSERT(numParticlesOutOfRange(*this, lev_min, lev_max, nGrow) == 0);
1574
1575 return true;
1576}
1577
1578//
1579// Shared implementation of Redistribute
1580//
1581template <typename ParticleType, int NArrayReal, int NArrayInt,
1582 template<class> class Allocator, class CellAssignor>
1583void
1585::Redistribute_impl (int lev_min, int lev_max, IntVect nGrow,
1586 bool local, IntVect max_cells_moved, bool remove_negative)
1588 BL_PROFILE("ParticleContainer::Redistribute_impl()");
1589 BL_PROFILE_VAR_NS("Redistribute_partition", blp_partition);
1590
1591 int theEffectiveFinestLevel = m_gdb->finestLevel();
1592 while (!m_gdb->LevelDefined(theEffectiveFinestLevel)) { theEffectiveFinestLevel--; }
1593
1594 if (std::ssize(m_particles) < theEffectiveFinestLevel+1) {
1595 if (Verbose()) {
1596 amrex::Print() << "ParticleContainer::Redistribute() resizing containers from "
1597 << m_particles.size() << " to "
1598 << theEffectiveFinestLevel + 1 << '\n';
1599 }
1600 m_particles.resize(theEffectiveFinestLevel+1);
1601 m_dummy_mf.resize(theEffectiveFinestLevel+1);
1602 }
1603
1604 for (int lev = 0; lev < theEffectiveFinestLevel+1; ++lev) { RedefineDummyMF(lev); }
1605
1606 int finest_lev_particles;
1607 if (lev_max == -1) {
1608 lev_max = theEffectiveFinestLevel;
1609 finest_lev_particles = m_particles.size() - 1;
1610 } else {
1611 finest_lev_particles = lev_max;
1612 }
1613 AMREX_ASSERT(lev_max <= finestLevel());
1614
1615 if (local) {
1616 AMREX_ASSERT(max_cells_moved.allGE(0));
1617 AMREX_ASSERT(numParticlesOutOfRange(*this, lev_min, lev_max,
1618 max_cells_moved) == 0);
1619 }
1620
1621 this->defineBufferMap();
1622
1623 if (RedistributeInPlace(lev_min, lev_max, nGrow, remove_negative)) { return; }
1624
1625#ifndef AMREX_USE_GPU
1626 const bool use_mask_lookup = local && lev_min == 0 && lev_max == 0
1627 && RedistributeMaskLooksCheap(0, max_cells_moved);
1628 if (use_mask_lookup) { BuildRedistributeMask(0, max_cells_moved); }
1629#else
1631 m_particle_locator.setGeometry(GetParGDB());
1632#endif
1633
1634 BL_PROFILE_VAR_START(blp_partition);
1635 ParticleCopyOp op;
1636 int num_levels = finest_lev_particles + 1;
1637 op.setNumLevels(num_levels);
1638 Vector<std::map<std::pair<int, int>, int> > new_sizes(num_levels);
1639#ifndef AMREX_USE_GPU
1640 const int myproc = ParallelContext::MyProcSub();
1641#endif
1642#ifdef AMREX_USE_GPU
1643 auto assign_grid = m_particle_locator.getGridAssignor();
1644 const auto plo = Geom(0).ProbLoArray();
1645 const auto phi = Geom(0).ProbHiArray();
1646 const auto rlo = Geom(0).ProbLoArrayInParticleReal();
1647 const auto rhi = Geom(0).ProbHiArrayInParticleReal();
1648 const auto is_per = Geom(0).isPeriodicArray();
1649#endif
1650
1651#if defined(AMREX_USE_OMP) || defined(AMREX_USE_GPU)
1652 Vector<std::pair<int, int> > grid_tile_ids;
1653 Vector<ParticleTileType*> ptile_ptrs;
1654 Vector<int> plevs;
1655 Vector<int*> new_size_ptrs;
1656#ifndef AMREX_USE_GPU
1658 Vector<Gpu::DeviceVector<int>*> level_ptrs;
1660 Vector<Gpu::DeviceVector<int>*> src_index_ptrs;
1661 Vector<Gpu::DeviceVector<IntVect>*> periodic_shift_ptrs;
1662#endif
1663 std::size_t num_tiles = 0;
1664 for (int lev = lev_min; lev <= finest_lev_particles; ++lev) {
1665 for (auto const& kv : m_particles[lev]) {
1666 if (kv.second.numParticles() != 0) {
1667 ++num_tiles;
1668 }
1669 }
1670 }
1671 grid_tile_ids.reserve(num_tiles);
1672 ptile_ptrs.reserve(num_tiles);
1673 plevs.reserve(num_tiles);
1674 new_size_ptrs.reserve(num_tiles);
1675#ifndef AMREX_USE_GPU
1676 box_ptrs.reserve(num_tiles);
1677 level_ptrs.reserve(num_tiles);
1678 tile_ptrs.reserve(num_tiles);
1679 src_index_ptrs.reserve(num_tiles);
1680 periodic_shift_ptrs.reserve(num_tiles);
1681#endif
1682 for (int lev = lev_min; lev <= finest_lev_particles; lev++) {
1683 auto& pmap = m_particles[lev];
1684 for (auto& kv : pmap)
1685 {
1686 const auto np = kv.second.numParticles();
1687 if (np == 0) { continue; }
1688
1689 grid_tile_ids.push_back(kv.first);
1690 ptile_ptrs.push_back(&(kv.second));
1691 plevs.push_back(lev);
1692 auto index = std::make_pair(kv.first.first, kv.first.second);
1693 auto& new_size = new_sizes[lev][index];
1694 new_size = 0;
1695 new_size_ptrs.push_back(&new_size);
1696#ifndef AMREX_USE_GPU
1697 op.resize(kv.first.first, kv.first.second, lev, static_cast<int>(np));
1698 auto& boxes = op.m_boxes[lev][index];
1699 auto& levels = op.m_levels[lev][index];
1700 auto& tiles = op.m_tiles[lev][index];
1701 auto& src_indices = op.m_src_indices[lev][index];
1702 auto& periodic_shift = op.m_periodic_shift[lev][index];
1703 box_ptrs.push_back(&boxes);
1704 level_ptrs.push_back(&levels);
1705 tile_ptrs.push_back(&tiles);
1706 src_index_ptrs.push_back(&src_indices);
1707 periodic_shift_ptrs.push_back(&periodic_shift);
1708#endif
1709 }
1710 }
1711#endif
1712
1713#if defined(AMREX_USE_OMP) || defined(AMREX_USE_GPU)
1714#ifdef AMREX_USE_OMP
1715#pragma omp parallel for
1716#endif
1717 for (int pmap_it = 0; pmap_it < static_cast<int>(ptile_ptrs.size()); ++pmap_it)
1718 {
1719 int lev = plevs[pmap_it];
1720 int gid = grid_tile_ids[pmap_it].first;
1721 int tid = grid_tile_ids[pmap_it].second;
1722 auto& src_tile = *ptile_ptrs[pmap_it];
1723 const size_t np = src_tile.numParticles();
1724 if (np == 0) {continue;}
1725
1726#ifndef AMREX_USE_GPU
1727 auto& boxes = *box_ptrs[pmap_it];
1728 auto& levels = *level_ptrs[pmap_it];
1729 auto& tiles = *tile_ptrs[pmap_it];
1730 auto& src_indices = *src_index_ptrs[pmap_it];
1731 auto& periodic_shift = *periodic_shift_ptrs[pmap_it];
1732 *new_size_ptrs[pmap_it] = hostPartitionTile(src_tile,
1733 lev, gid, tid,
1734 lev_min, lev_max, nGrow, use_mask_lookup,
1735 remove_negative, myproc,
1736 boxes, levels, tiles,
1737 src_indices, periodic_shift);
1738#else // GPU algorithm
1739 int num_stay = partitionParticlesByDest(src_tile, assign_grid,
1740 std::forward<CellAssignor>(CellAssignor{}),
1741 BufferMap(),
1742 plo, phi, rlo, rhi, is_per, lev, gid, tid,
1743 lev_min, lev_max, nGrow, remove_negative);
1744
1745 int num_move = np - num_stay;
1746 *new_size_ptrs[pmap_it] = num_stay;
1747 op.resize(gid, tid, lev, num_move);
1748
1749 auto index = std::make_pair(gid, tid);
1750 auto* p_boxes = op.m_boxes[lev][index].dataPtr();
1751 auto* p_levs = op.m_levels[lev][index].dataPtr();
1752 auto* p_tiles = op.m_tiles[lev][index].dataPtr();
1753 auto* p_src_indices = op.m_src_indices[lev][index].dataPtr();
1754 auto* p_periodic_shift = op.m_periodic_shift[lev][index].dataPtr();
1755 auto ptd = src_tile.getParticleTileData();
1756
1757 amrex::ParallelFor(num_move, [=] AMREX_GPU_DEVICE (int i)
1758 {
1759 const auto p = ptd[i + num_stay];
1760
1761 if (!p.id().is_valid())
1762 {
1763 p_boxes[i] = -1;
1764 p_tiles[i] = -1;
1765 p_levs[i] = -1;
1766 }
1767 else
1768 {
1769 const auto tup = assign_grid(p, lev_min, lev_max, nGrow,
1770 std::forward<CellAssignor>(CellAssignor{}));
1771 p_boxes[i] = amrex::get<0>(tup);
1772 p_tiles[i] = amrex::get<1>(tup);
1773 p_levs[i] = amrex::get<2>(tup);
1774 }
1775 p_periodic_shift[i] = IntVect(AMREX_D_DECL(0,0,0));
1776 p_src_indices[i] = i+num_stay;
1777 });
1778#endif
1779 }
1780#else
1781 for (int lev = lev_min; lev <= finest_lev_particles; ++lev) {
1782 auto& pmap = m_particles[lev];
1783 for (auto& kv : pmap)
1784 {
1785 auto& src_tile = kv.second;
1786 const auto np = src_tile.numParticles();
1787 if (np == 0) { continue; }
1788
1789 int gid = kv.first.first;
1790 int tid = kv.first.second;
1791 auto index = std::make_pair(gid, tid);
1792 auto& new_size = new_sizes[lev][index];
1793 new_size = 0;
1794
1795 op.resize(gid, tid, lev, static_cast<int>(np));
1796 auto& boxes = op.m_boxes[lev][index];
1797 auto& levels = op.m_levels[lev][index];
1798 auto& tiles = op.m_tiles[lev][index];
1799 auto& src_indices = op.m_src_indices[lev][index];
1800 auto& periodic_shift = op.m_periodic_shift[lev][index];
1801 new_size = hostPartitionTile(src_tile,
1802 lev, gid, tid,
1803 lev_min, lev_max, nGrow, use_mask_lookup,
1804 remove_negative, myproc,
1805 boxes, levels, tiles,
1806 src_indices, periodic_shift);
1807 }
1808 }
1809#endif
1810 BL_PROFILE_VAR_STOP(blp_partition);
1811
1812 ParticleCopyPlan plan;
1813
1814 plan.build(*this, op, h_redistribute_int_comp,
1815 h_redistribute_real_comp, local, max_cells_moved);
1816
1817 // by default, this uses The_Arena();
1820
1821 if (use_comms_arena) {
1822 snd_buffer.setArena(The_Comms_Arena());
1823 rcv_buffer.setArena(The_Comms_Arena());
1824 }
1825
1826 packBuffer(*this, op, plan, snd_buffer);
1827
1828 // clear particles from container
1829 for (int lev = lev_min; lev <= lev_max; ++lev)
1830 {
1831 auto& plev = m_particles[lev];
1832 for (auto& kv : plev)
1833 {
1834 int gid = kv.first.first;
1835 int tid = kv.first.second;
1836 auto index = std::make_pair(gid, tid);
1837 auto& tile = plev[index];
1838 tile.resize(new_sizes[lev][index]);
1839 }
1840 }
1841
1842 for (int lev = lev_min; lev <= lev_max; lev++)
1843 {
1844 particle_detail::clearEmptyEntries(m_particles[lev]);
1845 }
1846
1847 if (std::ssize(m_particles) > theEffectiveFinestLevel+1) {
1848 if (m_verbose > 0) {
1849 amrex::Print() << "ParticleContainer::Redistribute() resizing m_particles from "
1850 << m_particles.size() << " to " << theEffectiveFinestLevel+1 << '\n';
1851 }
1852 AMREX_ASSERT(std::ssize(m_particles) >= 2);
1853
1854 m_particles.resize(theEffectiveFinestLevel + 1);
1855 m_dummy_mf.resize(theEffectiveFinestLevel + 1);
1856 }
1857
1859 {
1860 plan.buildMPIFinish(BufferMap());
1861 communicateParticlesStart(*this, plan, snd_buffer, rcv_buffer);
1862 this->ReserveForRedistribute(plan);
1863 unpackBuffer(*this, plan, snd_buffer, RedistributeUnpackPolicy());
1865 unpackRemotes(*this, plan, rcv_buffer, RedistributeUnpackPolicy());
1866 }
1867 else
1868 {
1870 Gpu::PinnedVector<char> pinned_snd_buffer;
1871 Gpu::PinnedVector<char> pinned_rcv_buffer;
1872
1873 if (snd_buffer.arena()->isPinned()) {
1874 plan.buildMPIFinish(BufferMap());
1876 communicateParticlesStart(*this, plan, snd_buffer, pinned_rcv_buffer);
1877 } else {
1878 pinned_snd_buffer.resize(snd_buffer.size());
1879 Gpu::dtoh_memcpy_async(pinned_snd_buffer.dataPtr(), snd_buffer.dataPtr(), snd_buffer.size());
1880 plan.buildMPIFinish(BufferMap());
1882 communicateParticlesStart(*this, plan, pinned_snd_buffer, pinned_rcv_buffer);
1883 }
1884
1885 this->ReserveForRedistribute(plan);
1886
1887 rcv_buffer.resize(pinned_rcv_buffer.size());
1888 unpackBuffer(*this, plan, snd_buffer, RedistributeUnpackPolicy());
1890 Gpu::htod_memcpy_async(rcv_buffer.dataPtr(), pinned_rcv_buffer.dataPtr(), pinned_rcv_buffer.size());
1891 unpackRemotes(*this, plan, rcv_buffer, RedistributeUnpackPolicy());
1892 }
1893
1895 AMREX_ASSERT(numParticlesOutOfRange(*this, lev_min, lev_max, nGrow) == 0);
1896}
1897
1898template <typename ParticleType, int NArrayReal, int NArrayInt,
1899 template<class> class Allocator, class CellAssignor>
1900void
1903{
1904 BL_PROFILE("ParticleContainer::ReserveForRedistribute()");
1905
1906 std::map<ParticleTileType*, int> addsizes;
1907
1908 for (int lev = 0; lev < this->BufferMap().numLevels(); ++lev) {
1909 for (MFIter mfi = this->MakeMFIter(lev); mfi.isValid(); ++mfi) {
1910 int gid = mfi.index();
1911 int tid = mfi.LocalTileIndex();
1912 auto& tile = this->DefineAndReturnParticleTile(lev, gid, tid);
1913 int num_copies = plan.m_box_counts_h[this->BufferMap().gridAndTileAndLevToBucket(gid, tid, lev)];
1914 if (num_copies > 0) {
1915 addsizes[&tile] += num_copies;
1916 }
1917 }
1918 }
1919
1920 if (plan.m_nrcvs > 0) {
1921 for (int i = 0; i < std::ssize(plan.m_rcv_box_counts); ++i) {
1922 int copy_size = plan.m_rcv_box_counts[i];
1923 int lev = plan.m_rcv_box_levs[i];
1924 int gid = plan.m_rcv_box_ids[i];
1925 int tid = plan.m_rcv_box_tids[i];
1926 auto& tile = this->DefineAndReturnParticleTile(lev, gid, tid);
1927 addsizes[&tile] += copy_size;
1928 }
1929 }
1930
1931 ParticleTileType::reserve(addsizes);
1932}
1933
1934template <typename ParticleType, int NArrayReal, int NArrayInt,
1935 template<class> class Allocator, class CellAssignor>
1936bool
1938{
1939 BL_PROFILE("ParticleContainer::OK()");
1940
1941 if (lev_max == -1) {
1942 lev_max = finestLevel();
1943 }
1944
1945 return (m_dummy_mf.size() >= lev_max+1 && numParticlesOutOfRange(*this, lev_min, lev_max, nGrow) == 0);
1946}
1947
1948template <typename ParticleType, int NArrayReal, int NArrayInt,
1949 template<class> class Allocator, class CellAssignor>
1950void
1952::AddParticlesAtLevel (AoS& particles, int level, int nGrow)
1953{
1954 ParticleTileType ptile;
1955 ptile.GetArrayOfStructs().swap(particles);
1956 AddParticlesAtLevel(ptile, level, nGrow);
1957}
1958
1959template <typename ParticleType, int NArrayReal, int NArrayInt,
1960 template<class> class Allocator, class CellAssignor>
1961void
1963::AddParticlesAtLevel (ParticleTileType& particles, int level, int nGrow)
1964{
1965 BL_PROFILE("ParticleContainer::AddParticlesAtLevel()");
1966
1967 if (std::ssize(m_particles) < level+1)
1968 {
1969 if (Verbose())
1970 {
1971 amrex::Print() << "ParticleContainer::AddParticlesAtLevel resizing m_particles from "
1972 << m_particles.size()
1973 << " to "
1974 << level+1 << '\n';
1975 }
1976 m_particles.resize(level+1);
1977 m_dummy_mf.resize(level+1);
1978 for (int lev = 0; lev < level+1; ++lev) {
1979 RedefineDummyMF(lev);
1980 }
1981 }
1982
1983 auto& ptile = DefineAndReturnParticleTile(level, 0, 0);
1984 int old_np = ptile.size();
1985 int num_to_add = particles.size();
1986 int new_np = old_np + num_to_add;
1987 ptile.resize(new_np);
1988 amrex::copyParticles(ptile, particles, 0, old_np, num_to_add);
1989 Redistribute(level, level, nGrow);
1990 particles.resize(0);
1991}
1992
1993// This is the single-level version for cell-centered density
1994template <typename ParticleType, int NArrayReal, int NArrayInt,
1995 template<class> class Allocator, class CellAssignor>
1996void
1998AssignCellDensitySingleLevel (int rho_index,
1999 MultiFab& mf_to_be_filled,
2000 int lev,
2001 int ncomp,
2002 int particle_lvl_offset) const
2003{
2004 BL_PROFILE("ParticleContainer::AssignCellDensitySingleLevel()");
2005
2006 if (rho_index != 0) { amrex::Abort("AssignCellDensitySingleLevel only works if rho_index = 0"); }
2007
2008 MultiFab* mf_pointer;
2009
2010 if (OnSameGrids(lev, mf_to_be_filled)) {
2011 // If we are already working with the internal mf defined on the
2012 // particle_box_array, then we just work with this.
2013 mf_pointer = &mf_to_be_filled;
2014 }
2015 else {
2016 // If mf_to_be_filled is not defined on the particle_box_array, then we need
2017 // to make a temporary here and copy into mf_to_be_filled at the end.
2018 mf_pointer = new MultiFab(ParticleBoxArray(lev),
2020 ncomp, mf_to_be_filled.nGrow(),
2022 }
2023
2024 // We must have ghost cells for each FAB so that a particle in one grid can spread
2025 // its effect to an adjacent grid by first putting the value into ghost cells of its
2026 // own grid. The mf->SumBoundary call then adds the value from one grid's ghost cell
2027 // to another grid's valid region.
2028 if (mf_pointer->nGrow() < 1) {
2029 amrex::Error("Must have at least one ghost cell when in AssignCellDensitySingleLevel");
2030 }
2031
2032 const auto strttime = amrex::second();
2033
2034 const auto dxi = Geom(lev).InvCellSizeArray();
2035 const auto plo = Geom(lev).ProbLoArray();
2036 const auto pdxi = Geom(lev + particle_lvl_offset).InvCellSizeArray();
2037
2038 if (Geom(lev).isAnyPeriodic() && ! Geom(lev).isAllPeriodic())
2039 {
2040 amrex::Error("AssignCellDensitySingleLevel: problem must be periodic in no or all directions");
2041 }
2042
2043 mf_pointer->setVal(0);
2044
2046#ifdef AMREX_USE_OMP
2047#pragma omp parallel if (Gpu::notInLaunchRegion())
2048#endif
2049 {
2050 FArrayBox local_rho;
2051 for (ParConstIter pti(*this, lev); pti.isValid(); ++pti) {
2052 const Long np = pti.numParticles();
2053 auto ptd = pti.GetParticleTile().getConstParticleTileData();
2054 FArrayBox& fab = (*mf_pointer)[pti];
2055 auto rhoarr = fab.array();
2056#ifdef AMREX_USE_OMP
2057 Box tile_box;
2059 {
2060 tile_box = pti.tilebox();
2061 tile_box.grow(mf_pointer->nGrow());
2062 local_rho.resize(tile_box,ncomp);
2063 local_rho.setVal<RunOn::Host>(0.0);
2064 rhoarr = local_rho.array();
2065 }
2066#endif
2067
2068 if (particle_lvl_offset == 0)
2069 {
2071 {
2072 auto p = ptd[i];
2073 amrex_deposit_cic(p, ncomp, rhoarr, plo, dxi);
2074 });
2075 }
2076 else
2077 {
2079 {
2080 auto p = ptd[i];
2081 amrex_deposit_particle_dx_cic(p, ncomp, rhoarr, plo, dxi, pdxi);
2082 });
2083 }
2084
2085#ifdef AMREX_USE_OMP
2087 {
2088 fab.atomicAdd<RunOn::Host>(local_rho, tile_box, tile_box, 0, 0, ncomp);
2089 }
2090#endif
2091 }
2092 }
2093
2094 mf_pointer->SumBoundary(Geom(lev).periodicity());
2095
2096 // If ncomp > 1, first divide the momenta (component n)
2097 // by the mass (component 0) in order to get velocities.
2098 // Be careful not to divide by zero.
2099 for (int n = 1; n < ncomp; n++)
2100 {
2101 for (MFIter mfi(*mf_pointer); mfi.isValid(); ++mfi)
2102 {
2103 (*mf_pointer)[mfi].protected_divide<RunOn::Device>((*mf_pointer)[mfi],0,n,1);
2104 }
2105 }
2106
2107 // Only multiply the first component by (1/vol) because this converts mass
2108 // to density. If there are additional components (like velocity), we don't
2109 // want to divide those by volume.
2110 const Real* dx = Geom(lev).CellSize();
2111 const Real vol = AMREX_D_TERM(dx[0], *dx[1], *dx[2]);
2112
2113 mf_pointer->mult(Real(1.0)/vol, 0, 1, mf_pointer->nGrow());
2114
2115 // If mf_to_be_filled is not defined on the particle_box_array, then we need
2116 // to copy here from mf_pointer into mf_to_be_filled.
2117 if (mf_pointer != &mf_to_be_filled)
2118 {
2119 mf_to_be_filled.ParallelCopy(*mf_pointer,0,0,ncomp,0,0);
2120 delete mf_pointer;
2121 }
2122
2123 if (m_verbose > 1)
2124 {
2125 auto stoptime = amrex::second() - strttime;
2126
2129
2130 amrex::Print() << "ParticleContainer::AssignCellDensitySingleLevel) time: "
2131 << stoptime << '\n';
2132 }
2133}
2134
2135template <typename ParticleType, int NArrayReal, int NArrayInt,
2136 template<class> class Allocator, class CellAssignor>
2137void
2139ResizeRuntimeRealComp (int new_size, bool communicate)
2140{
2141 int old_size = m_num_runtime_real;
2142
2143 m_runtime_comps_defined = (new_size > 0);
2144 m_num_runtime_real = new_size;
2145 int cur_size = h_redistribute_real_comp.size();
2146 h_redistribute_real_comp.resize(cur_size-old_size+new_size, communicate);
2148
2149 for (int lev = 0; lev < numLevels(); ++lev) {
2150 for (ParIterType pti(*this,lev); pti.isValid(); ++pti) {
2151 auto& tile = DefineAndReturnParticleTile(lev, pti);
2152 auto np = tile.numParticles();
2153 if (np > 0 && new_size > old_size) {
2154 auto& soa = tile.GetStructOfArrays();
2155 soa.resize(np);
2156 }
2157 }
2158 }
2159}
2160
2161template <typename ParticleType, int NArrayReal, int NArrayInt,
2162 template<class> class Allocator, class CellAssignor>
2163void
2165ResizeRuntimeIntComp (int new_size, bool communicate)
2166{
2167 int old_size = m_num_runtime_int;
2168
2169 m_runtime_comps_defined = (new_size > 0);
2170 m_num_runtime_int = new_size;
2171 int cur_size = h_redistribute_int_comp.size();
2172 h_redistribute_int_comp.resize(cur_size-old_size+new_size, communicate);
2174
2175 for (int lev = 0; lev < numLevels(); ++lev) {
2176 for (ParIterType pti(*this,lev); pti.isValid(); ++pti) {
2177 auto& tile = DefineAndReturnParticleTile(lev, pti);
2178 auto np = tile.numParticles();
2179 if (np > 0 && new_size > old_size) {
2180 auto& soa = tile.GetStructOfArrays();
2181 soa.resize(np);
2182 }
2183 }
2184 }
2185}
2186
2187}
#define BL_PROFILE_VAR_START(vname)
Definition AMReX_BLProfiler.H:604
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define BL_PROFILE_VAR_STOP(vname)
Definition AMReX_BLProfiler.H:605
#define BL_PROFILE_SYNC_STOP()
Definition AMReX_BLProfiler.H:709
#define BL_PROFILE_SYNC_START_TIMED(fname)
Definition AMReX_BLProfiler.H:708
#define BL_PROFILE_VAR_NS(fname, vname)
Definition AMReX_BLProfiler.H:603
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:123
#define AMREX_HOST_DEVICE_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:105
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Print on all processors of the default communicator.
Definition AMReX_Print.H:113
BaseFab< T > & atomicAdd(const BaseFab< T > &x) noexcept
Atomic FAB addition (a[i] <- a[i] + b[i]).
Definition AMReX_BaseFab.H:2450
Array4< T const > array() const noexcept
Create an Array4 view over all components.
Definition AMReX_BaseFab.H:496
void setVal(T const &x, const Box &bx, int dcomp, int ncomp) noexcept
The setVal functions set sub-regions in the BaseFab to a constant value. This most general form speci...
Definition AMReX_BaseFab.H:1537
Reference-counted collection of Boxes.
Definition AMReX_BoxArray.H:681
IndexType ixType() const noexcept
Return index type of this BoxArray.
Definition AMReX_BoxArray.H:1269
BoxArray & grow(int n)
Grow each Box by n cells in every direction.
Definition AMReX_BoxArray.cpp:722
std::vector< std::pair< int, Box > > intersections(const Box &bx) const
Return all intersections of bx with this BoxArray.
Definition AMReX_BoxArray.cpp:1205
Box getCellCenteredBox(int index) const noexcept
Return cell-centered box at element index of this BoxArray.
Definition AMReX_BoxArray.H:1100
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition AMReX_BoxArray.H:1252
Long size() const noexcept
Return the number of boxes in the BoxArray.
Definition AMReX_BoxArray.H:758
A list of Boxes sharing a common IndexType.
Definition AMReX_BoxList.H:109
BoxList & complementIn(const Box &b, const BoxList &bl)
Set this BoxList to the complement of BoxList bl in Box b.
Definition AMReX_BoxList.cpp:315
__host__ __device__ BoxND & grow(int i) noexcept
Grow in all directions by i cells (negative shrinks).
Definition AMReX_Box.H:668
__host__ __device__ bool contains(const IntVectND< dim > &p) const noexcept
Return true if argument is contained within BoxND.
Definition AMReX_Box.H:233
GpuArray< Real, 3 > InvCellSizeArray() const noexcept
Returns the inverse cell sizes as a GpuArray for use on host or device.
Definition AMReX_CoordSys.H:97
A Fortran Array of REALs.
Definition AMReX_FArrayBox.H:237
void resize(const Box &b, int N=1, Arena *ar=nullptr)
For debugging purposes we hide BaseFab version and do some extra work.
Definition AMReX_FArrayBox.cpp:178
int size() const noexcept
Return the number of FABs in the FabArray.
Definition AMReX_FabArrayBase.H:115
int nGrow(int direction=0) const noexcept
Return the grow factor that defines the region of definition.
Definition AMReX_FabArrayBase.H:83
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:135
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition AMReX_FabArrayBase.H:100
void ParallelCopy(const FabArray< FAB > &src, const Periodicity &period=Periodicity::NonPeriodic(), CpOp op=FabArrayBase::COPY)
Definition AMReX_FabArray.H:999
void setVal(value_type val)
Set all components in the entire region of each FAB to val.
Definition AMReX_FabArray.H:3068
Array4< typename FabArray< FAB >::value_type const > array(const MFIter &mfi) const noexcept
Read-only Array4 view for the FAB referenced by iterator mfi.
Definition AMReX_FabArray.H:649
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:87
const Box & Domain() const noexcept
Returns our rectangular domain.
Definition AMReX_Geometry.H:246
GpuArray< Real, 3 > ProbLoArray() const noexcept
Return the lo end of the problem domain in a GpuArray for device code.
Definition AMReX_Geometry.H:219
static void streamSynchronize() noexcept
Definition AMReX_GpuDevice.cpp:992
__host__ __device__ bool cellCentered() const noexcept
True if the IndexTypeND is CELL based in all directions.
Definition AMReX_IndexType.H:102
An Integer Vector in dim-Dimensional Space.
Definition AMReX_IntVect.H:156
__host__ __device__ constexpr bool allGE(const IntVectND< dim > &rhs) const noexcept
Returns true if this is greater than or equal to argument for all components. NOTE: This is NOT a str...
Definition AMReX_IntVect.H:549
__host__ static __device__ constexpr IntVectND< dim > TheZeroVector() noexcept
Definition AMReX_IntVect.H:778
a one-thingy-per-box distributed object
Definition AMReX_LayoutData.H:13
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:88
int LocalTileIndex() const noexcept
The current local tile index in the current grid;.
Definition AMReX_MFIter.H:185
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:176
Box validbox() const noexcept
Return the valid Box in which the current tile resides.
Definition AMReX_MFIter.H:167
int index() const noexcept
The index into the underlying BoxArray of the current FAB.
Definition AMReX_MFIter.H:179
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:40
void mult(Real val, int comp, int num_comp, int nghost=0)
Scales the value of each cell in the specified subregion of the MultiFab by the scalar val (a[i] <- a...
Definition AMReX_MultiFab.cpp:1632
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
size_type size() const noexcept
Definition AMReX_PODVector.H:654
void resize(size_type a_new_size, GrowthStrategy strategy=GrowthStrategy::Poisson)
Definition AMReX_PODVector.H:734
T * dataPtr() noexcept
Definition AMReX_PODVector.H:676
Definition AMReX_ParIter.H:147
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:355
const DistributionMapping & ParticleDistributionMap(int lev) const
Get the DistributionMapping for a given level.
Definition AMReX_ParticleContainerBase.H:207
ParGDBBase * m_gdb
Definition AMReX_ParticleContainerBase.H:320
const BoxArray & ParticleBoxArray(int lev) const
Get the BoxArray for a given level.
Definition AMReX_ParticleContainerBase.H:200
virtual void reserveData()
Definition AMReX_ParticleContainerBase.cpp:98
void SetArena(Arena *a)
Definition AMReX_ParticleContainerBase.H:272
static const std::string & AggregationType()
Definition AMReX_ParticleContainerBase.cpp:349
const Geometry & Geom(int lev) const
Get the Geometry for a given level.
Definition AMReX_ParticleContainerBase.H:214
static bool memEfficientSort
Definition AMReX_ParticleContainerBase.H:298
bool RedistributeMaskLooksCheap(int lev, IntVect nghost) const
Definition AMReX_ParticleContainerBase.cpp:389
int numLevels() const
the number of defined levels in the ParticleContainer
Definition AMReX_ParticleContainerBase.H:229
Arena * arena() const
Definition AMReX_ParticleContainerBase.H:268
int m_verbose
Definition AMReX_ParticleContainerBase.H:317
const ParGDBBase * GetParGDB() const
Get the ParGDB object used to define this container (const version).
Definition AMReX_ParticleContainerBase.H:232
bool OnSameGrids(int level, const MF &mf) const
Definition AMReX_ParticleContainerBase.H:266
static bool do_tiling
Definition AMReX_ParticleContainerBase.H:296
void BuildRedistributeMask(int lev, IntVect nghost) const
Definition AMReX_ParticleContainerBase.cpp:455
AmrParticleLocator< DenseBins< Box > > m_particle_locator
Definition AMReX_ParticleContainerBase.H:303
int finestLevel() const
the finest level actually defined for the ParticleContainer
Definition AMReX_ParticleContainerBase.H:223
static bool redistribute_early_out
Definition AMReX_ParticleContainerBase.H:302
void defineBufferMap() const
Definition AMReX_ParticleContainerBase.cpp:130
static int AggregationBuffer()
Definition AMReX_ParticleContainerBase.cpp:369
void RedefineDummyMF(int lev)
Definition AMReX_ParticleContainerBase.cpp:112
int maxLevel() const
the finest allowed level in the ParticleContainer, whether it is defined or not.
Definition AMReX_ParticleContainerBase.H:226
static bool use_comms_arena
Definition AMReX_ParticleContainerBase.H:299
Vector< std::unique_ptr< MultiFab > > m_dummy_mf
Definition AMReX_ParticleContainerBase.H:321
MFIter MakeMFIter(int lev, const MFItInfo &info) const
Definition AMReX_ParticleContainerBase.H:112
const ParticleBufferMap & BufferMap() const
Definition AMReX_ParticleContainerBase.H:245
static IntVect tile_size
Definition AMReX_ParticleContainerBase.H:297
virtual void resizeData()
Definition AMReX_ParticleContainerBase.cpp:103
A distributed container for Particles sorted onto the levels, grids, and tiles of a block-structured ...
Definition AMReX_ParticleContainer.H:154
IntVect Index(const P &p, int lev) const
Definition AMReX_ParticleContainerI.H:190
void ResizeRuntimeIntComp(int new_size, bool communicate)
Definition AMReX_ParticleContainerI.H:2165
bool OK(int lev_min=0, int lev_max=-1, int nGrow=0) const
OK checks that all particles are in the right places (for some value of right).
Definition AMReX_ParticleContainerI.H:1937
void CapacityOfParticlesInGrid(LayoutData< Int > &mem, int lev) const
Return capacity of memory for particles at specific grid.
Definition AMReX_ParticleContainerI.H:616
std::conditional_t< is_rtsoa_pc, ParticleTileRT< typename ParticleType::RealType, typename ParticleType::IntType >, ParticleTile< ParticleType, NArrayReal, NArrayInt, Allocator > > ParticleTileType
Definition AMReX_ParticleContainer.H:192
bool PeriodicShift(P &p) const
Returns true if the particle was shifted.
Definition AMReX_ParticleContainerI.H:381
std::map< std::pair< int, int >, ParticleTileType > ParticleLevel
Definition AMReX_ParticleContainer.H:201
static constexpr int NArrayInt
Number of extra integer components stored in struct-of-array form.
Definition AMReX_ParticleContainer.H:167
static constexpr int NArrayReal
Number of extra Real components stored in struct-of-array form.
Definition AMReX_ParticleContainer.H:165
void SetSoACompileTimeNames(std::vector< std::string > const &rdata_name, std::vector< std::string > const &idata_name)
Definition AMReX_ParticleContainerI.H:98
void Redistribute(int lev_min=0, int lev_max=-1, int nGrow=0, int local=0, bool remove_negative=true)
Redistribute puts all the particles back in the right places (for some value of right).
Definition AMReX_ParticleContainer.H:519
typename ParticleTileType::AoS AoS
Definition AMReX_ParticleContainer.H:204
void SetParticleSize()
Definition AMReX_ParticleContainerI.H:17
void RemoveParticlesAtLevel(int level)
The Following methods are for managing Virtual and Ghost Particles.
Definition AMReX_ParticleContainerI.H:780
const ParticleTileType & ParticlesAt(int lev, int grid, int tile) const
Return the ParticleTile for level "lev", grid "grid" and tile "tile." Const version.
Definition AMReX_ParticleContainer.H:1101
static constexpr int NStructInt
Number of extra integer components in the particle struct.
Definition AMReX_ParticleContainer.H:163
int NumRealComps() const
Definition AMReX_ParticleContainer.H:1399
void clearParticles()
Clear all the particles in this container. This does not free memory.
Definition AMReX_ParticleContainerI.H:1151
void Increment(MultiFab &mf, int level)
Definition AMReX_ParticleContainerI.H:743
Vector< int > h_redistribute_int_comp
Definition AMReX_ParticleContainer.H:1463
void AssignCellDensitySingleLevel(int rho_index, MultiFab &mf, int level, int ncomp=1, int particle_lvl_offset=0) const
Definition AMReX_ParticleContainerI.H:1998
void CreateGhostParticles(int level, int ngrow, AoS &ghosts) const
Create ghost particles for a given level that are copies of particles near coarse->fine boundaries in...
Definition AMReX_ParticleContainerI.H:1106
void SortParticlesByBin(IntVect bin_size)
Sort the particles on each tile by groups of cells, given an IntVect bin_size.
Definition AMReX_ParticleContainerI.H:1315
bool HasIntComp(std::string const &name)
Definition AMReX_ParticleContainerI.H:137
DefaultAssignor CellAssignor
Definition AMReX_ParticleContainer.H:158
const Vector< ParticleLevel > & GetParticles() const
Definition AMReX_ParticleContainer.H:1041
DenseBins< typename ParticleTileType::ParticleTileDataType > m_bins
Definition AMReX_ParticleContainer.H:1589
void ShrinkToFit()
Definition AMReX_ParticleContainerI.H:723
ParConstIter_impl< ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor > ParConstIterType
Definition AMReX_ParticleContainer.H:212
bool HasRealComp(std::string const &name)
Definition AMReX_ParticleContainerI.H:129
void resizeData() override
This resizes the vector of dummy MultiFabs used by the ParticleContainer for the current number of le...
Definition AMReX_ParticleContainerI.H:440
void ResizeRuntimeRealComp(int new_size, bool communicate)
Definition AMReX_ParticleContainerI.H:2139
bool OutsidePeriodic(const P &p) const
Definition AMReX_ParticleContainerI.H:368
Long IncrementWithTotal(MultiFab &mf, int level, bool local=false)
Definition AMReX_ParticleContainerI.H:770
bool RedistributeInPlace(int lev_min, int lev_max, IntVect nGrow, bool remove_negative)
Definition AMReX_ParticleContainerI.H:1466
static constexpr int NStructReal
Number of extra Real components in the particle struct.
Definition AMReX_ParticleContainer.H:161
Long NumberOfParticlesAtLevel(int level, bool only_valid=true, bool only_local=false) const
Returns # of particles at specified the level.
Definition AMReX_ParticleContainerI.H:573
void SortParticlesByCell()
Sort the particles on each tile by cell, using Fortran ordering.
Definition AMReX_ParticleContainerI.H:1306
Vector< int > h_redistribute_real_comp
Definition AMReX_ParticleContainer.H:1462
void addParticles(const PCType &other, bool local=false)
Add particles from other to this ParticleContainer. local controls whether or not to call Redistribut...
Definition AMReX_ParticleContainerI.H:1180
static constexpr bool is_rtsoa_pc
Definition AMReX_ParticleContainer.H:189
int GetRealCompIndex(std::string const &name)
Definition AMReX_ParticleContainerI.H:150
int NumIntComps() const
Definition AMReX_ParticleContainer.H:1400
void ReserveForRedistribute(ParticleCopyPlan const &plan)
Definition AMReX_ParticleContainerI.H:1902
bool Where(const P &prt, ParticleLocData &pld, int lev_min=0, int lev_max=-1, IntVect nGrow=IntVect(0), int local_grid=-1) const
Definition AMReX_ParticleContainerI.H:205
void Redistribute_impl(int lev_min=0, int lev_max=-1, int nGrow=0, int local=0, bool remove_negative=true)
Definition AMReX_ParticleContainer.H:1315
int GetIntCompIndex(std::string const &name)
Definition AMReX_ParticleContainerI.H:172
ParticleTileType & DefineAndReturnParticleTile(int lev, int grid, int tile)
Define and return the ParticleTile for level "lev", grid "grid" and tile "tile.".
Definition AMReX_ParticleContainer.H:1201
void copyParticles(const PCType &other, bool local=false)
Copy particles from other to this ParticleContainer. Will clear all the particles from this container...
Definition AMReX_ParticleContainerI.H:1168
void RemoveParticlesNotAtFinestLevel()
Definition AMReX_ParticleContainerI.H:794
Vector< Long > NumberOfParticlesInGrid(int level, bool only_valid=true, bool only_local=false) const
Definition AMReX_ParticleContainerI.H:517
ParticleLocData Reset(ParticleType &prt, bool update, bool verbose=true, ParticleLocData pld=ParticleLocData()) const
Updates a particle's location (Where), tries to periodic shift any particles that have left the domai...
Definition AMReX_ParticleContainerI.H:397
void CreateVirtualParticles(int level, AoS &virts) const
Creates virtual particles for a given level that represent in some capacity all particles at finer le...
Definition AMReX_ParticleContainerI.H:861
ParIter_impl< ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor > ParIterType
Definition AMReX_ParticleContainer.H:211
void AddParticlesAtLevel(AoS &particles, int level, int nGrow=0)
Add particles from a pbox to the grid at this level.
Definition AMReX_ParticleContainerI.H:1952
Long TotalNumberOfParticles(bool only_valid=true, bool only_local=false) const
Returns # of particles at all levels.
Definition AMReX_ParticleContainerI.H:502
void reserveData() override
This reserves data in the vector of dummy MultiFabs used by the ParticleContainer for the maximum num...
Definition AMReX_ParticleContainerI.H:431
T_ParticleType ParticleType
Definition AMReX_ParticleContainer.H:156
Definition AMReX_ParticleLocator.H:137
void build(const BoxArray &ba, const Geometry &geom, bool a_do_tiling=false, const IntVect &a_tile_size=IntVect(1024000, 1024000, 1024000))
Definition AMReX_ParticleLocator.H:144
AssignGrid< BinIteratorFactory > getGridAssignor() const noexcept
Definition AMReX_ParticleLocator.H:220
This class provides the user with a few print options.
Definition AMReX_Print.H:35
Definition AMReX_Reduce.H:556
Type value()
Definition AMReX_Reduce.H:591
Definition AMReX_Reduce.H:715
void eval(MF const &mf, IntVect const &nghost, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:855
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
A Collection of IArrayBoxes.
Definition AMReX_iMultiFab.H:34
Long sum(int comp, int nghost=0, bool local=false) const
Returns the sum in component comp.
Definition AMReX_iMultiFab.cpp:460
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_particle_real ParticleReal
Floating Point Type for Particles.
Definition AMReX_REAL.H:91
amrex_long Long
Definition AMReX_INT.H:30
T ExclusiveSum(N n, T const *in, T *out, RetSum a_ret_sum=retSum)
Exclusive sum.
Definition AMReX_Scan.H:1045
ArrayND< T, 4, true > Array4
Convenient alias for a 4D ArrayND with three spatial dimensions plus a component dimension.
Definition AMReX_Array4.H:1352
void SumBoundary(const Periodicity &period=Periodicity::NonPeriodic(), bool deterministic=false)
Sum values in overlapped cells.
Definition AMReX_FabArray.H:4018
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Return a copy of b grown uniformly by i cells in every direction.
Definition AMReX_Box.H:1328
PODVector< T, PinnedArenaAllocator< T > > PinnedVector
A PODVector that uses the pinned memory arena.
Definition AMReX_GpuContainers.H:48
PODVector< T, ArenaAllocator< T > > DeviceVector
A PODVector that uses the standard memory Arena. Note that the memory might or might not be managed d...
Definition AMReX_GpuContainers.H:24
Arena * The_Comms_Arena()
Definition AMReX_Arena.cpp:899
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:849
void Min(KeyValuePair< K, V > &vi, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:287
void Max(KeyValuePair< K, V > &vi, int root, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:254
void Bcast(Gpu::DeviceVector< T > &v, int root, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:105
void Sum(Gpu::DeviceVector< T > &v, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:37
void Sum(Gpu::DeviceVector< T > &v, int root, MPI_Comm comm)
Definition AMReX_GpuParallelReduce.H:62
__host__ __device__ AMREX_FORCE_INLINE void AddNoRet(T *sum, T value) noexcept
Definition AMReX_GpuAtomic.H:322
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:329
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:454
bool notInLaunchRegion() noexcept
Definition AMReX_GpuControl.H:89
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:440
void QueueReduction(Func f)
Definition AMReX_Lazy.cpp:7
constexpr Long GhostParticleID
Definition AMReX_Particle.H:19
constexpr Long VirtualParticleID
Definition AMReX_Particle.H:20
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
int NProcsSub() noexcept
number of ranks in current frame
Definition AMReX_ParallelContext.H:74
int IOProcessorNumberSub() noexcept
IO sub-rank in current frame.
Definition AMReX_ParallelContext.H:78
bool UseGpuAwareMpi()
Definition AMReX_ParallelDescriptor.H:113
void GatherLayoutDataToVector(const LayoutData< T > &sendbuf, Vector< T > &recvbuf, int root)
Gather LayoutData values to a vector on root.
Definition AMReX_ParallelDescriptor.H:1286
static constexpr RetSum retSum
Definition AMReX_Scan.H:34
Definition AMReX_Amr.cpp:50
void gatherParticles(PTile &dst, const PTile &src, N np, const Index *inds)
Gather particles copies particles into contiguous order from an arbitrary order. Specifically,...
Definition AMReX_ParticleTransformation.H:739
ParConstIter_impl< Particle< T_NStructReal, T_NStructInt >, T_NArrayReal, T_NArrayInt, Allocator, CellAssignor > ParConstIter
Definition AMReX_ParIter.H:228
__host__ __device__ void swapParticle(const ParticleTileData< T_ParticleType, NAR, NAI > &dst, const ParticleTileData< T_ParticleType, NAR, NAI > &src, int src_i, int dst_i) noexcept
A general single particle swapping routine that can run on the GPU.
Definition AMReX_ParticleTransformation.H:120
__host__ __device__ int getTileIndex(const IntVect &iv, const Box &box, const bool a_do_tiling, const IntVect &a_tile_size, Box &tbx)
Definition AMReX_ParticleUtil.H:192
@ F
Definition AMReX_Order.H:7
__host__ __device__ void copyParticle(const ParticleTileData< T_ParticleType, NAR, NAI > &dst, const ConstParticleTileData< T_ParticleType, NAR, NAI > &src, int src_i, int dst_i) noexcept
A general single particle copying routine that can run on the GPU.
Definition AMReX_ParticleTransformation.H:32
void copyParticles(DstTile &dst, const SrcTile &src) noexcept
Copy particles from src to dst. This version copies all the particles, writing them to the beginning ...
Definition AMReX_ParticleTransformation.H:222
std::string getDefaultCompNameInt(const int i)
Definition AMReX_ParticleUtil.H:1168
@ Host
Definition AMReX_GpuControl.H:65
@ Device
Definition AMReX_GpuControl.H:65
void communicateParticlesStart(const PC &pc, ParticleCopyPlan &plan, const SndBuffer &snd_buffer, RcvBuffer &rcv_buffer)
Definition AMReX_ParticleCommunication.H:915
int partitionParticlesByDest(PTile &ptile, const PLocator &ploc, CellAssignor const &assignor, const ParticleBufferMap &pmap, const GpuArray< Real, 3 > &plo, const GpuArray< Real, 3 > &phi, const GpuArray< ParticleReal, 3 > &rlo, const GpuArray< ParticleReal, 3 > &rhi, const GpuArray< int, 3 > &is_per, int lev, int gid, int tid, int lev_min, int lev_max, IntVect nGrow, bool remove_negative)
Definition AMReX_ParticleUtil.H:703
void ReorderParticles(PTile &ptile, const index_type *permutations)
Reorder particles on the tile ptile using a the permutations array.
Definition AMReX_ParticleUtil.H:1186
__host__ __device__ bool enforcePeriodic(P &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &phi, amrex::GpuArray< amrex::ParticleReal, 3 > const &rlo, amrex::GpuArray< amrex::ParticleReal, 3 > const &rhi, amrex::GpuArray< int, 3 > const &is_per) noexcept
Definition AMReX_ParticleUtil.H:472
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
__host__ __device__ int numTilesInBox(const Box &box, const bool a_do_tiling, const IntVect &a_tile_size)
Definition AMReX_ParticleUtil.H:288
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
std::string getDefaultCompNameReal(const int i)
Definition AMReX_ParticleUtil.H:1153
void ParticleToMesh(PC const &pc, const Vector< MultiFab * > &mf, int lev_min, int lev_max, F &&f, bool zero_out_input=true, bool vol_weight=true)
Deposit particles onto a hierarchy of MultiFabs.
Definition AMReX_AmrParticles.H:191
void unpackBuffer(PC &pc, const ParticleCopyPlan &plan, const Buffer &snd_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:788
double second() noexcept
Definition AMReX_Utility.cpp:874
void packBuffer(const PC &pc, const ParticleCopyOp &op, const ParticleCopyPlan &plan, Buffer &snd_buffer)
Definition AMReX_ParticleCommunication.H:583
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:3028
void communicateParticlesFinish(const ParticleCopyPlan &plan)
Definition AMReX_ParticleCommunication.cpp:445
Index filterAndTransformParticles(DstTile &dst, const SrcTile &src, Index *mask, F const &f, Index src_start, Index dst_start) noexcept
Conditionally copy particles from src to dst based on the value of mask. A transformation will also b...
Definition AMReX_ParticleTransformation.H:516
void unpackRemotes(PC &pc, const ParticleCopyPlan &plan, Buffer &rcv_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:1018
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:233
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
int numParticlesOutOfRange(Iterator const &pti, int nGrow)
Returns the number of particles that are more than nGrow cells from the box correspond to the input i...
Definition AMReX_ParticleUtil.H:37
void PermutationForDeposition(Gpu::DeviceVector< index_type > &perm, index_type nitems, index_type nbins, F const &f)
Definition AMReX_ParticleUtil.H:998
int Verbose() noexcept
Return the verbosity level configured via ParmParse or SetVerbose().
Definition AMReX.cpp:179
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
const int[]
Definition AMReX_BLProfiler.cpp:1665
Index filterParticles(DstTile &dst, const SrcTile &src, const Index *mask) noexcept
Conditionally copy particles from src to dst based on the value of mask.
Definition AMReX_ParticleTransformation.H:389
void transformParticles(DstTile &dst, const SrcTile &src, F &&f) noexcept
Apply the function f to all the particles in src, writing the result to dst. This version does all th...
Definition AMReX_ParticleTransformation.H:273
__host__ __device__ IntVect getParticleCell(P const &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi) noexcept
Returns the cell index for a given particle using the provided lower bounds and cell sizes.
Definition AMReX_ParticleUtil.H:392
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get the Idx-th element of IntVectND<dim>.
Definition AMReX_IntVect.H:1367
Definition AMReX_ParticleLocator.H:262
Definition AMReX_ParticleContainerI.H:1061
amrex::AmrAssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > m_assign_grid
Definition AMReX_ParticleContainerI.H:1064
int m_lev_max
Definition AMReX_ParticleContainerI.H:1063
AssignGridFilter(amrex::AmrAssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > assign_grid, int gid, int level, int nGrow)
This filters based on matching grids.
Definition AMReX_ParticleContainerI.H:1070
int m_nGrow
Definition AMReX_ParticleContainerI.H:1063
int m_lev_min
Definition AMReX_ParticleContainerI.H:1063
AMREX_GPU_HOST_DEVICE int operator()(const SrcData &src, int src_i) const noexcept
Definition AMReX_ParticleContainerI.H:1076
int m_gid
Definition AMReX_ParticleContainerI.H:1063
Definition AMReX_ParticleLocator.H:17
Definition AMReX_ParticleUtil.H:445
Definition AMReX_DenseBins.H:110
Definition AMReX_ParticleContainerI.H:822
Box m_domain
Definition AMReX_ParticleContainerI.H:826
GpuArray< Real, 3 > m_dxi
Definition AMReX_ParticleContainerI.H:825
FilterVirt(const amrex::AssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > &assign_buffer_grid, const GpuArray< Real, 3 > &plo, const GpuArray< Real, 3 > &dxi, const Box &domain)
Definition AMReX_ParticleContainerI.H:828
AMREX_GPU_HOST_DEVICE int operator()(const SrcData &src, int src_i) const noexcept
Definition AMReX_ParticleContainerI.H:835
amrex::AssignGrid< amrex::DenseBinIteratorFactory< amrex::Box > > m_assign_buffer_grid
Definition AMReX_ParticleContainerI.H:824
GpuArray< Real, 3 > m_plo
Definition AMReX_ParticleContainerI.H:825
Definition AMReX_ParticleUtil.H:359
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:52
Definition AMReX_GpuControl.H:180
Definition AMReX_TypeTraits.H:90
FabArray memory allocation information.
Definition AMReX_FabArray.H:73
Definition AMReX_ParticleCommunication.H:66
void resize(int gid, int tid, int lev, int size)
Definition AMReX_ParticleCommunication.cpp:27
void setNumLevels(int num_levels)
Definition AMReX_ParticleCommunication.cpp:18
Vector< std::map< TileKey, Gpu::DeviceVector< IntVect > > > m_periodic_shift
Definition AMReX_ParticleCommunication.H:73
Vector< std::map< TileKey, Gpu::DeviceVector< int > > > m_levels
Definition AMReX_ParticleCommunication.H:70
Vector< std::map< TileKey, Gpu::DeviceVector< int > > > m_boxes
Definition AMReX_ParticleCommunication.H:69
Vector< std::map< TileKey, Gpu::DeviceVector< int > > > m_tiles
Definition AMReX_ParticleCommunication.H:71
Vector< std::map< TileKey, Gpu::DeviceVector< int > > > m_src_indices
Definition AMReX_ParticleCommunication.H:72
Definition AMReX_ParticleCommunication.H:92
Vector< int > m_rcv_box_ids
Definition AMReX_ParticleCommunication.H:366
Vector< int > m_rcv_box_counts
Definition AMReX_ParticleCommunication.H:364
void buildMPIFinish(const ParticleBufferMap &map)
Definition AMReX_ParticleCommunication.cpp:223
void build(const PC &pc, const ParticleCopyOp &op, const Vector< int > &int_comp_mask, const Vector< int > &real_comp_mask, bool local, IntVect max_cells_moved)
Definition AMReX_ParticleCommunication.H:409
Vector< int > m_rcv_box_levs
Definition AMReX_ParticleCommunication.H:369
Vector< int > m_rcv_box_tids
Definition AMReX_ParticleCommunication.H:367
int m_nrcvs
Definition AMReX_ParticleCommunication.H:372
Gpu::HostVector< unsigned int > m_box_counts_h
Definition AMReX_ParticleCommunication.H:361
A struct used for storing a particle's position in the AMR hierarchy.
Definition AMReX_ParticleContainer.H:93
Box m_grown_gridbox
Definition AMReX_ParticleContainer.H:100
IntVect m_cell
Definition AMReX_ParticleContainer.H:97
int m_grid
Definition AMReX_ParticleContainer.H:95
int m_tile
Definition AMReX_ParticleContainer.H:96
int m_lev
Definition AMReX_ParticleContainer.H:94
Box m_tilebox
Definition AMReX_ParticleContainer.H:99
Box m_gridbox
Definition AMReX_ParticleContainer.H:98
The struct used to store particles.
Definition AMReX_Particle.H:405
__host__ __device__ RealVect pos() const &
Definition AMReX_Particle.H:456
Definition AMReX_ParticleCommunication.H:42
Definition AMReX_ParticleContainerI.H:1088
AMREX_GPU_HOST_DEVICE void operator()(DstData &dst, const SrcData &src, int src_i, int dst_i) const noexcept
Definition AMReX_ParticleContainerI.H:1092
Definition AMReX_ParticleContainerI.H:843
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(DstData &dst, const SrcData &src, int src_i, int dst_i) const noexcept
Definition AMReX_ParticleContainerI.H:846