Block-Structured AMR Software Framework
 
Loading...
Searching...
No Matches
AMReX_ParticleMesh.H
Go to the documentation of this file.
1#ifndef AMREX_PARTICLEMESH_H_
2#define AMREX_PARTICLEMESH_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_TypeTraits.H>
6#include <AMReX_MultiFab.H>
8#include <type_traits>
9
10namespace amrex {
11
13namespace particle_detail {
14
15template <typename F, typename T, typename T_ParticleType, template<class, int, int> class PTDType, int NAR, int NAI>
17auto call_f (F const& f,
18 const PTDType<T_ParticleType, NAR, NAI>& p,
19 const int i, Array4<T> const& fabarr,
20 GpuArray<Real,AMREX_SPACEDIM> const& plo,
21 GpuArray<Real,AMREX_SPACEDIM> const& dxi) noexcept
22{
23 using PTDTypeT = std::remove_const_t<std::remove_reference_t<decltype(p)>>;
24 if constexpr ( ! T_ParticleType::is_soa_particle &&
25 IsCallable<F, typename PTDTypeT::ParticleRefType, decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
26 return f(p.m_aos[i], fabarr, plo, dxi);
27 } else if constexpr ( ! T_ParticleType::is_soa_particle &&
28 IsCallable<F, typename PTDTypeT::ParticleRefType, decltype(fabarr)>::value) {
29 return f(p.m_aos[i], fabarr);
30 } else if constexpr (IsCallable<F, decltype(p.getSuperParticle(i)), decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
31 return f(p.getSuperParticle(i), fabarr, plo, dxi);
32 } else if constexpr (IsCallable<F, decltype(p.getSuperParticle(i)), decltype(fabarr)>::value) {
33 return f(p.getSuperParticle(i), fabarr);
34 } else if constexpr (IsCallable<F, decltype(p), int, decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
35 return f(p, i, fabarr, plo, dxi);
36 } else {
37 return f(p, i, fabarr);
38 }
39}
40}
42
43template <class PC, class MF, class F, std::enable_if_t<IsParticleContainer<PC>::value, int> foo = 0>
44void
45ParticleToMesh (PC const& pc, MF& mf, int lev, F const& f, bool zero_out_input=true)
46{
47 BL_PROFILE("amrex::ParticleToMesh");
48
49 if (zero_out_input) { mf.setVal(0.0); }
50
51 MF* mf_pointer;
52
53 if (pc.OnSameGrids(lev, mf) && zero_out_input)
54 {
55 mf_pointer = &mf;
56 } else {
57 mf_pointer = new MF(pc.ParticleBoxArray(lev),
58 pc.ParticleDistributionMap(lev),
59 mf.nComp(), mf.nGrowVect());
60 mf_pointer->setVal(0.0);
61 }
62
63 const auto plo = pc.Geom(lev).ProbLoArray();
64 const auto dxi = pc.Geom(lev).InvCellSizeArray();
65
66 using ParIter = typename PC::ParConstIterType;
67#ifdef AMREX_USE_GPU
69 {
70 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
71 {
72 const auto& tile = pti.GetParticleTile();
73 const auto np = tile.numParticles();
74 const auto& ptd = tile.getConstParticleTileData();
75
76 auto& fab = (*mf_pointer)[pti];
77 auto fabarr = fab.array();
78
79 AMREX_FOR_1D( np, i,
80 {
81 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
82 });
83 }
84 }
85 else
86#endif
87 {
88#ifdef AMREX_USE_OMP
89#pragma omp parallel if (Gpu::notInLaunchRegion())
90#endif
91 {
92 typename MF::FABType::value_type local_fab;
93 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
94 {
95 const auto& tile = pti.GetParticleTile();
96 const auto np = tile.numParticles();
97 const auto& ptd = tile.getConstParticleTileData();
98
99 auto& fab = (*mf_pointer)[pti];
100
101 Box tile_box = pti.tilebox();
102 tile_box.grow(mf_pointer->nGrowVect());
103 local_fab.resize(tile_box,mf_pointer->nComp());
104 local_fab.template setVal<RunOn::Host>(0.0);
105 auto fabarr = local_fab.array();
106
107 AMREX_FOR_1D( np, i,
108 {
109 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
110 });
111
112 fab.template atomicAdd<RunOn::Host>(local_fab, tile_box, tile_box,
113 0, 0, mf_pointer->nComp());
114 }
115 }
116 }
117
118 if (mf_pointer != &mf)
119 {
120 mf.ParallelAdd(*mf_pointer, 0, 0, mf_pointer->nComp(),
121 mf_pointer->nGrowVect(), IntVect(0), pc.Geom(lev).periodicity());
122 delete mf_pointer;
123 } else {
124 mf_pointer->SumBoundary(pc.Geom(lev).periodicity());
125 }
126}
127
128template <class PC, class MF, class F, std::enable_if_t<IsParticleContainer<PC>::value, int> foo = 0>
129void
130MeshToParticle (PC& pc, MF const& mf, int lev, F const& f)
131{
132 BL_PROFILE("amrex::MeshToParticle");
133
134 MF* mf_pointer = pc.OnSameGrids(lev, mf) ?
135 const_cast<MF*>(&mf) : new MF(pc.ParticleBoxArray(lev),
136 pc.ParticleDistributionMap(lev),
137 mf.nComp(), mf.nGrowVect());
138
139 if (mf_pointer != &mf) {mf_pointer->ParallelCopy(mf,0,0,mf.nComp(),mf.nGrowVect(),mf.nGrowVect()); }
140
141 const auto plo = pc.Geom(lev).ProbLoArray();
142 const auto dxi = pc.Geom(lev).InvCellSizeArray();
143
144 using ParIter = typename PC::ParIterType;
145#ifdef AMREX_USE_OMP
146#pragma omp parallel if (Gpu::notInLaunchRegion())
147#endif
148 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
149 {
150 auto& tile = pti.GetParticleTile();
151 const auto np = tile.numParticles();
152 const auto& ptd = tile.getParticleTileData();
153
154 const auto& fab = (*mf_pointer)[pti];
155 auto fabarr = fab.array();
156
157 AMREX_FOR_1D( np, i,
158 {
159 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
160 });
161 }
162
163 if (mf_pointer != &mf) { delete mf_pointer; }
164}
165
166}
167#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:97
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
__host__ __device__ BoxND & grow(int i) noexcept
Definition AMReX_Box.H:641
__host__ __device__ BoxND< new_dim > resize() const noexcept
Return a new BoxND of size new_dim by either shrinking or expanding this BoxND.
Definition AMReX_Box.H:893
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:147
ParticleTileRef GetParticleTile() const
Definition AMReX_ParIter.H:84
Definition AMReX_ParIter.H:115
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:92
Definition AMReX_Amr.cpp:49
void MeshToParticle(PC &pc, MF const &mf, int lev, F const &f)
Definition AMReX_ParticleMesh.H:130
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)
Definition AMReX_AmrParticles.H:156