Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_AmrParticles.H
Go to the documentation of this file.
1#ifndef AMREX_AmrParticles_H_
2#define AMREX_AmrParticles_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Particles.H>
7#include <AMReX_AmrParGDB.H>
10
15
16namespace amrex {
17
18// This is already documented in AMReX_ParticleContainer.H. So we need Doxygen to
19// ignore it to avoid warnings.
21
32template <typename ParticleType, int NArrayReal, int NArrayInt,
33 template<class> class Allocator, class CellAssignor>
34void
36::AssignDensity (int rho_index,
37 Vector<std::unique_ptr<MultiFab> >& mf_to_be_filled,
38 int lev_min, int ncomp, int finest_level, int ngrow) const
39{
40
41 BL_PROFILE("ParticleContainer::AssignDensity()");
42
43 if (rho_index != 0) { amrex::Abort("AssignDensity only works if rho_index = 0"); }
44
46 BL_ASSERT(NStructReal >= ncomp);
47 BL_ASSERT(ncomp == 1 || ncomp == AMREX_SPACEDIM+1);
48
49 if (finest_level == -1) {
50 finest_level = this->finestLevel();
51 }
52 while (!this->m_gdb->LevelDefined(finest_level)) {
53 finest_level--;
54 }
55
56 ngrow = std::max(ngrow, 2);
57
58 // Create the space for mf_to_be_filled, regardless of whether we'll need a temporary mf
59 mf_to_be_filled.resize(finest_level+1);
60 for (int lev = lev_min; lev <= finest_level; lev++)
61 {
62 auto ng = lev == lev_min ? IntVect(AMREX_D_DECL(ngrow,ngrow,ngrow)) : this->m_gdb->refRatio(lev-1);
63 mf_to_be_filled[lev] = std::make_unique<MultiFab>(this->m_gdb->boxArray(lev),
64 this->m_gdb->DistributionMap(lev),
65 ncomp, ng);
66 mf_to_be_filled[lev]->setVal(0.0);
67 }
68
69 // Test whether the grid structure of the boxArray is the same as the ParticleBoxArray at all levels
70 bool all_grids_the_same = true;
71 for (int lev = lev_min; lev <= finest_level; lev++)
72 {
73 if (!this->OnSameGrids(lev, *mf_to_be_filled[lev]))
74 {
75 all_grids_the_same = false;
76 break;
77 }
78 }
79
81 if (!all_grids_the_same)
82 {
83 // Create the space for the temporary, mf_part
84 mf_part.resize(finest_level+1);
85 for (int lev = lev_min; lev <= finest_level; lev++)
86 {
87 auto ng = lev == lev_min ? IntVect(AMREX_D_DECL(ngrow,ngrow,ngrow)) : this->m_gdb->refRatio(lev-1);
88 mf_part[lev] = std::make_unique<MultiFab>(this->ParticleBoxArray(lev),
89 this->ParticleDistributionMap(lev),
90 ncomp, ng,
92 mf_part[lev]->setVal(0.0);
93 }
94 }
95
96 auto & mf = (all_grids_the_same) ? mf_to_be_filled : mf_part;
97
98 if (finest_level == 0)
99 {
100 //
101 // Just use the far simpler single-level version.
102 //
103 this->AssignCellDensitySingleLevel(rho_index, *mf[0], 0, ncomp);
104 if ( ! all_grids_the_same) {
105 mf_to_be_filled[0]->ParallelCopy(*mf[0],0,0,ncomp,0,0);
106 }
107 return;
108 }
109
110 // configure this to do a no-op at the physical boundaries.
113 Vector<BCRec> bcs(ncomp, BCRec(lo_bc, hi_bc));
114 PCInterp mapper;
115
116 Vector<std::unique_ptr<MultiFab> > tmp(finest_level+1);
117 for (int lev = lev_min; lev <= finest_level; ++lev) {
118 const BoxArray& ba = mf[lev]->boxArray();
119 const DistributionMapping& dm = mf[lev]->DistributionMap();
120 tmp[lev] = std::make_unique<MultiFab>(ba, dm, ncomp, 0,
122 tmp[lev]->setVal(0.0);
123 }
124
125 for (int lev = lev_min; lev <= finest_level; ++lev) {
126
127 this->AssignCellDensitySingleLevel(rho_index, *mf[lev], lev, ncomp, 0);
128
129 if (lev < finest_level) {
130 PhysBCFunctNoOp cphysbc, fphysbc;
131 amrex::InterpFromCoarseLevel(*tmp[lev+1], 0.0, *mf[lev],
132 rho_index, rho_index, ncomp,
133 this->m_gdb->Geom(lev),
134 this->m_gdb->Geom(lev+1),
135 cphysbc, 0, fphysbc, 0,
136 this->m_gdb->refRatio(lev), &mapper,
137 bcs, 0);
138 }
139
140 if (lev > lev_min) {
141 // Note - this will double count the mass on the coarse level in
142 // regions covered by the fine level, but this will be corrected
143 // below in the call to average_down.
144 amrex::sum_fine_to_coarse(*mf[lev], *mf[lev-1], rho_index, ncomp,
145 this->m_gdb->refRatio(lev-1),
146 this->m_gdb->Geom(lev-1),
147 this->m_gdb->Geom(lev));
148 }
149
150 mf[lev]->plus(*tmp[lev], rho_index, ncomp, 0);
151 }
152
153 for (int lev = finest_level - 1; lev >= lev_min; --lev) {
154 amrex::average_down(*mf[lev+1], *mf[lev], rho_index, ncomp, this->m_gdb->refRatio(lev));
155 }
156
157 if (!all_grids_the_same) {
158 for (int lev = lev_min; lev <= finest_level; lev++) {
159 //
160 // We haven't interpolated the ghost cells so we can't copy them
161 //
162 mf_to_be_filled[lev]->ParallelCopy(*mf_part[lev],0,0,ncomp,0,0);
163 }
164 }
165 if (lev_min > 0) {
166 int nlevels = finest_level - lev_min + 1;
167 for (int i = 0; i < nlevels; i++)
168 {
169 mf_to_be_filled[i] = std::move(mf_to_be_filled[i+lev_min]);
170 }
171 mf_to_be_filled.resize(nlevels);
172 }
173}
174
188template <class PC, class F>
190void
191ParticleToMesh (PC const& pc, const Vector<MultiFab*>& mf,
192 int lev_min, int lev_max, F&& f,
193 bool zero_out_input=true, bool vol_weight=true)
194{
195 BL_PROFILE("amrex::ParticleToMesh");
196
197 if (lev_max == -1) { lev_max = pc.finestLevel(); }
198 while (!pc.GetParGDB()->LevelDefined(lev_max)) { lev_max--; }
199
200 if (lev_max == 0)
201 {
202 ParticleToMesh(pc, *mf[0], 0, std::forward<F>(f), zero_out_input);
203 if (vol_weight) {
204 const Real* dx = pc.Geom(0).CellSize();
205 const Real vol = AMREX_D_TERM(dx[0], *dx[1], *dx[2]);
206 mf[0]->mult(Real(1.0)/vol, 0, mf[0]->nComp(), mf[0]->nGrow());
207 }
208 return;
209 }
210
211 int ngrow = amrex::max(AMREX_D_DECL(mf[0]->nGrow(0),
212 mf[0]->nGrow(1),
213 mf[0]->nGrow(2)), 2);
214
215 if (zero_out_input)
216 {
217 for (int lev = lev_min; lev <= lev_max; ++lev)
218 {
219 mf[lev]->setVal(Real(0.0));
220 }
221 }
222
223 Vector<MultiFab> mf_part(lev_max+1);
224 Vector<MultiFab> mf_tmp( lev_max+1);
225 for (int lev = lev_min; lev <= lev_max; ++lev)
226 {
227 mf_part[lev].define(pc.ParticleBoxArray(lev),
228 pc.ParticleDistributionMap(lev),
229 mf[lev]->nComp(), ngrow,
230 MFInfo().SetArena(The_Async_Arena()));
231 mf_tmp[lev].define( pc.ParticleBoxArray(lev),
232 pc.ParticleDistributionMap(lev),
233 mf[lev]->nComp(), 0,
234 MFInfo().SetArena(The_Async_Arena()));
235 mf_part[lev].setVal(0.0);
236 mf_tmp[lev].setVal( 0.0);
237 }
238
239 // configure this to do a no-op at the physical boundaries.
242 Vector<BCRec> bcs(mf_part[lev_min].nComp(), BCRec(lo_bc, hi_bc));
243 PCInterp mapper;
244
245 for (int lev = lev_min; lev <= lev_max; ++lev)
246 {
247 ParticleToMesh(pc, mf_part[lev], lev, f, true); // always zero out input on temp level
248 if (vol_weight) {
249 const Real* dx = pc.Geom(lev).CellSize();
250 const Real vol = AMREX_D_TERM(dx[0], *dx[1], *dx[2]);
251 mf_part[lev].mult(Real(1.0)/vol, 0, mf_part[lev].nComp(), mf_part[lev].nGrow());
252 }
253
254 if (lev < lev_max) {
255 PhysBCFunctNoOp cphysbc, fphysbc;
256 amrex::InterpFromCoarseLevel(mf_tmp[lev+1], 0.0, mf_part[lev],
257 0, 0, mf_part[lev].nComp(),
258 pc.GetParGDB()->Geom(lev),
259 pc.GetParGDB()->Geom(lev+1),
260 cphysbc, 0, fphysbc, 0,
261 pc.GetParGDB()->refRatio(lev), &mapper,
262 bcs, 0);
263 }
264
265 if (lev > lev_min) {
266 // Note - this will double count the mass on the coarse level in
267 // regions covered by the fine level, but this will be corrected
268 // below in the call to average_down.
269 amrex::sum_fine_to_coarse(mf_part[lev], mf_part[lev-1], 0, mf_part[lev].nComp(),
270 pc.GetParGDB()->refRatio(lev-1),
271 pc.GetParGDB()->Geom(lev-1),
272 pc.GetParGDB()->Geom(lev));
273 }
274
275 mf_part[lev].plus(mf_tmp[lev], 0, mf_part[lev].nComp(), 0);
276 }
277
278 for (int lev = lev_max-1; lev >= lev_min; --lev) {
279 amrex::average_down(mf_part[lev+1], mf_part[lev], 0, mf_part[lev].nComp(),
280 pc.GetParGDB()->refRatio(lev));
281 }
282
283 for (int lev = lev_min; lev <= lev_max; lev++)
284 {
285 if (zero_out_input) {
286 mf[lev]->ParallelCopy(mf_part[lev],0,0,mf_part[lev].nComp(),0,0);
287 } else { // do not zero out input
288 mf[lev]->ParallelAdd(mf_part[lev],0,0,mf_part[lev].nComp(),0,0);
289 }
290 }
291}
292
293template <typename T_ParticleType, int NArrayReal=0, int NArrayInt=0,
294 template<class> class Allocator=DefaultAllocator, class CellAssignor=DefaultAssignor>
298class AmrParticleContainer_impl // NOLINT(cppcoreguidelines-virtual-class-destructor)
299 : public ParticleContainer_impl<T_ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
300{
301
302public:
303
304 using ParticleType = T_ParticleType;
305
311
317
327 const Vector<DistributionMapping> & dmap,
328 const Vector<BoxArray> & ba,
329 const Vector<int> & rr)
330 : ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>(geom, dmap, ba, rr)
331 {
332 }
333
334 ~AmrParticleContainer_impl () override = default;
335
338
340 AmrParticleContainer_impl& operator= ( AmrParticleContainer_impl && ) noexcept = default;
341};
342
344template <int T_NStructReal, int T_NStructInt=0, int T_NArrayReal=0, int T_NArrayInt=0,
345 template<class> class Allocator=DefaultAllocator, class CellAssignor=DefaultAssignor>
346using AmrParticleContainer = AmrParticleContainer_impl<Particle<T_NStructReal, T_NStructInt>, T_NArrayReal, T_NArrayInt, Allocator, CellAssignor>;
347
370
371}
372
373#endif
Particle Geometry, DistributionMapping and BoxArray helper shared by AmrCore-based apps.
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
High-level FillPatch helpers for AMR coarse-to-fine synchronization.
Collection of spatial interpolaters used by FillPatch and flux-register logic.
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Provide basic functionalities to set up an AMR hierarchy.
Definition AMReX_AmrCore.H:31
Particle container that automatically pulls grid metadata from AmrCore when available.
Definition AMReX_AmrParticles.H:300
AmrParticleContainer_impl(const AmrParticleContainer_impl &)=delete
AmrParticleContainer_impl & operator=(const AmrParticleContainer_impl &)=delete
AmrParticleContainer_impl(const Vector< Geometry > &geom, const Vector< DistributionMapping > &dmap, const Vector< BoxArray > &ba, const Vector< int > &rr)
Construct from explicit Geometry, DistributionMapping and BoxArray.
Definition AMReX_AmrParticles.H:326
~AmrParticleContainer_impl() override=default
AmrParticleContainer_impl(AmrCore *amr_core)
Construct from amr_core.
Definition AMReX_AmrParticles.H:313
AmrParticleContainer_impl(AmrParticleContainer_impl &&) noexcept=default
T_ParticleType ParticleType
Definition AMReX_AmrParticles.H:304
AmrParticleContainer_impl()
Construct an empty container (call Define later).
Definition AMReX_AmrParticles.H:307
AmrTracerParticleContainer(AmrTracerParticleContainer &&) noexcept=default
~AmrTracerParticleContainer() override=default
AmrTracerParticleContainer(const AmrTracerParticleContainer &)=delete
AmrTracerParticleContainer(AmrCore *amr_core)
Construct from amr_core.
Definition AMReX_AmrParticles.H:357
Boundary condition record storing low/high face types per coordinate.
Definition AMReX_BCRec.H:25
Reference-counted collection of Boxes.
Definition AMReX_BoxArray.H:681
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:55
Piecewise Constant interpolation on cell centered data.
Definition AMReX_Interpolater.H:423
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
void SetArena(Arena *a)
Definition AMReX_ParticleContainerBase.H:272
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
int finestLevel() const
the finest level actually defined for the ParticleContainer
Definition AMReX_ParticleContainerBase.H:223
A distributed container for Particles sorted onto the levels, grids, and tiles of a block-structured ...
Definition AMReX_ParticleContainer.H:154
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 AssignCellDensitySingleLevel(int rho_index, MultiFab &mf, int level, int ncomp=1, int particle_lvl_offset=0) const
Definition AMReX_ParticleContainerI.H:1998
T_CellAssignor CellAssignor
Definition AMReX_ParticleContainer.H:158
static constexpr int NStructReal
Number of extra Real components in the particle struct.
Definition AMReX_ParticleContainer.H:161
Definition AMReX_PhysBCFunct.H:128
TracerParticleContainer(ParGDBBase *gdb)
Definition AMReX_TracerParticles.H:14
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:849
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:54
@ int_dir
Definition AMReX_BC_TYPES.H:89
Definition AMReX_Amr.cpp:50
@ F
Definition AMReX_Order.H:7
void average_down(const MultiFab &S_fine, MultiFab &S_crse, const Geometry &fgeom, const Geometry &cgeom, int scomp, int ncomp, int rr)
Definition AMReX_MultiFabUtil.cpp:360
amrex::ArenaAllocator< T > DefaultAllocator
Definition AMReX_GpuAllocators.H:205
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
AmrParticleContainer_impl< Particle< T_NStructReal, T_NStructInt >, T_NArrayReal, T_NArrayInt, Allocator, CellAssignor > AmrParticleContainer
Convenience alias that binds Particle<T_NStructReal,T_NStructInt> to AmrParticleContainer_impl.
Definition AMReX_AmrParticles.H:346
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:3028
void sum_fine_to_coarse(const MultiFab &S_fine, MultiFab &S_crse, int scomp, int ncomp, const IntVect &ratio, const Geometry &cgeom, const Geometry &)
Definition AMReX_MultiFabUtil.cpp:440
void InterpFromCoarseLevel(MF &mf, Real time, const MF &cmf, int scomp, int dcomp, int ncomp, const Geometry &cgeom, const Geometry &fgeom, BC &cbc, int cbccomp, BC &fbc, int fbccomp, const IntVect &ratio, Interp *mapper, const Vector< BCRec > &bcs, int bcscomp, const PreInterpHook &pre_interp={}, const PostInterpHook &post_interp={})
Fill with interpolation of coarse level data.
Definition AMReX_FillPatchUtil_I.H:1029
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
Definition AMReX_ParticleUtil.H:445
Definition AMReX_TypeTraits.H:90
FabArray memory allocation information.
Definition AMReX_FabArray.H:73
The struct used to store particles.
Definition AMReX_Particle.H:405