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
41template <typename F, typename T, template<class, class> class PTDType, class RType, class IType>
43auto call_f (F const& f,
44 const PTDType<RType, IType>& p,
45 const int i, Array4<T> const& fabarr,
46 GpuArray<Real,AMREX_SPACEDIM> const& plo,
47 GpuArray<Real,AMREX_SPACEDIM> const& dxi) noexcept
48{
49 if constexpr (IsCallable<F, decltype(p), int, decltype(fabarr), decltype(plo), decltype(dxi)>::value) {
50 return f(p, i, fabarr, plo, dxi);
51 } else {
52 return f(p, i, fabarr);
53 }
54}
55}
57
58template <class PC, class MF, class F>
60void
61ParticleToMesh (PC const& pc, MF& mf, int lev, F const& f, bool zero_out_input=true)
62{
63 BL_PROFILE("amrex::ParticleToMesh");
64
65 if (zero_out_input) { mf.setVal(0.0); }
66
67 MF* mf_pointer;
68
69 if (pc.OnSameGrids(lev, mf) && zero_out_input)
70 {
71 mf_pointer = &mf;
72 } else {
73 mf_pointer = new MF(pc.ParticleBoxArray(lev),
74 pc.ParticleDistributionMap(lev),
75 mf.nComp(), mf.nGrowVect(),
76 MFInfo().SetArena(The_Async_Arena()));
77 mf_pointer->setVal(0.0);
78 }
79
80 const auto plo = pc.Geom(lev).ProbLoArray();
81 const auto dxi = pc.Geom(lev).InvCellSizeArray();
82
83 using ParIter = typename PC::ParConstIterType;
84#ifdef AMREX_USE_GPU
86 {
87 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
88 {
89 const auto& tile = pti.GetParticleTile();
90 const auto np = tile.numParticles();
91 const auto& ptd = tile.getConstParticleTileData();
92
93 auto& fab = (*mf_pointer)[pti];
94 auto fabarr = fab.array();
95
96 AMREX_FOR_1D( np, i,
97 {
98 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
99 });
100 }
101 }
102 else
103#endif
104 {
105#ifdef AMREX_USE_OMP
106#pragma omp parallel if (Gpu::notInLaunchRegion())
107#endif
108 {
109 typename MF::FABType::value_type local_fab;
110 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
111 {
112 const auto& tile = pti.GetParticleTile();
113 const auto np = tile.numParticles();
114 const auto& ptd = tile.getConstParticleTileData();
115
116 auto& fab = (*mf_pointer)[pti];
117
118 Box tile_box = pti.tilebox();
119 tile_box.grow(mf_pointer->nGrowVect());
120 local_fab.resize(tile_box,mf_pointer->nComp());
121 local_fab.template setVal<RunOn::Host>(0.0);
122 auto fabarr = local_fab.array();
123
124 AMREX_FOR_1D( np, i,
125 {
126 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
127 });
128
129 fab.template atomicAdd<RunOn::Host>(local_fab, tile_box, tile_box,
130 0, 0, mf_pointer->nComp());
131 }
132 }
133 }
134
135 if (mf_pointer != &mf)
136 {
137 mf.ParallelAdd(*mf_pointer, 0, 0, mf_pointer->nComp(),
138 mf_pointer->nGrowVect(), IntVect(0), pc.Geom(lev).periodicity());
139 delete mf_pointer;
140 } else {
141 mf_pointer->SumBoundary(pc.Geom(lev).periodicity());
142 }
143}
144
145template <class PC, class MF, class F>
146requires (IsParticleContainer<PC>::value)
147void
148MeshToParticle (PC& pc, MF const& mf, int lev, F const& f)
149{
150 BL_PROFILE("amrex::MeshToParticle");
151
152 MF* mf_pointer = pc.OnSameGrids(lev, mf) ?
153 const_cast<MF*>(&mf) : new MF(pc.ParticleBoxArray(lev),
154 pc.ParticleDistributionMap(lev),
155 mf.nComp(), mf.nGrowVect(),
156 MFInfo().SetArena(The_Async_Arena()));
157
158 if (mf_pointer != &mf) {mf_pointer->ParallelCopy(mf,0,0,mf.nComp(),mf.nGrowVect(),mf.nGrowVect()); }
159
160 const auto plo = pc.Geom(lev).ProbLoArray();
161 const auto dxi = pc.Geom(lev).InvCellSizeArray();
162
163 using ParIter = typename PC::ParIterType;
164#ifdef AMREX_USE_OMP
165#pragma omp parallel if (Gpu::notInLaunchRegion())
166#endif
167 for(ParIter pti(pc, lev); pti.isValid(); ++pti)
168 {
169 auto& tile = pti.GetParticleTile();
170 const auto np = tile.numParticles();
171 const auto& ptd = tile.getParticleTileData();
172
173 const auto& fab = (*mf_pointer)[pti];
174 auto fabarr = fab.array();
175
176 AMREX_FOR_1D( np, i,
177 {
178 particle_detail::call_f(f, ptd, i, fabarr, plo, dxi);
179 });
180 }
181
182 if (mf_pointer != &mf) { delete mf_pointer; }
183}
184
185}
186#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:123
#define AMREX_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:97
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
__host__ __device__ BoxND & grow(int i) noexcept
Grow in all directions by i cells (negative shrinks).
Definition AMReX_Box.H:668
__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:927
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:849
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
Definition AMReX_Amr.cpp:50
@ F
Definition AMReX_Order.H:7
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
void MeshToParticle(PC &pc, MF const &mf, int lev, F const &f)
Definition AMReX_ParticleMesh.H:148
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
ParIter_impl< Particle< T_NStructReal, T_NStructInt >, T_NArrayReal, T_NArrayInt, Allocator, CellAssignor > ParIter
Definition AMReX_ParIter.H:239
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
const int[]
Definition AMReX_BLProfiler.cpp:1665
void setVal(MF &dst, typename MF::value_type val)
dst = val
Definition AMReX_FabArrayUtility.H:2174
Definition AMReX_TypeTraits.H:90
FabArray memory allocation information.
Definition AMReX_FabArray.H:73