ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERFPC.H
Go to the documentation of this file.
1 #ifndef ERF_PC_H_
2 #define ERF_PC_H_
3 
4 #ifdef ERF_USE_PARTICLES
5 
6 #include <string>
7 #include <AMReX_Particles.H>
8 #include <AMReX_ParticleUtil.H>
10 
11 /** Particle cell-binning functor for DenseBins. Maps a particle to its
12  * cell-tile index via amrex::getParticleCell + amrex::getTileIndex. */
13 struct GetParticleBinERF
14 {
15  amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> plo;
16  amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> dxi;
17  amrex::Box domain;
18  amrex::IntVect bin_size;
19  amrex::Box box;
20  template <typename P>
21  AMREX_GPU_HOST_DEVICE
22  unsigned int operator() (const P& p) const noexcept
23  {
24  amrex::Box tbx;
25  auto iv = amrex::getParticleCell(p, plo, dxi, domain);
26  auto tid = amrex::getTileIndex(iv, box, true, bin_size, tbx);
27  return static_cast<unsigned int>(tid);
28  }
29 };
30 
31 /**
32  * @brief Index mapping for integer particle attributes.
33  */
34 struct ERFParticlesIntIdx
35 {
36  enum {
37  ncomps = 0
38  };
39 };
40 
41 /**
42  * @brief Index mapping for real-valued particle attributes.
43  */
44 struct ERFParticlesRealIdx
45 {
46  enum {
47  vx = 0,
48  vy,
49  vz,
50  mass,
51  temperature,
52  ncomps
53  };
54 };
55 
56 namespace ERFParticleGeometry
57 {
58  /*! \brief Physical thickness of cell (i,j,k): the mean of its four vertical edges */
59  AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
60  amrex::Real cell_thickness (amrex::Array4<amrex::Real const> const& z_nd, int i, int j, int k) noexcept
61  {
62  return amrex::Real(0.25) * ( z_nd(i , j , k+1) - z_nd(i , j , k)
63  + z_nd(i+1, j , k+1) - z_nd(i+1, j , k)
64  + z_nd(i , j+1, k+1) - z_nd(i , j+1, k)
65  + z_nd(i+1, j+1, k+1) - z_nd(i+1, j+1, k) );
66  }
67 }
68 
69 namespace ERFParticleInitializations
70 {
71  /* list of particle initializations */
72  const std::string init_box_uniform = "box";
73 }
74 
75 namespace ERFParticleNames
76 {
77  const std::string tracers = "tracer_particles";
78 }
79 
80 /**
81  * @brief Particle container for ERF particles.
82  */
83 /*! \brief Behavior a derived container selects before ERFPC reads the input file.
84  *
85  * ERFPC::readInputs() runs from the ERFPC constructor, so a derived class cannot
86  * influence it through a virtual -- the base version is the one that runs. These
87  * options are passed down the constructor instead.
88  */
89 struct ERFPCOptions
90 {
91  /*! Read the generic particle-initialization inputs: initial_distribution_type,
92  * particle_box_lo / particle_box_hi, place_randomly_in_cells and
93  * initial_particles_per_cell. A container with its own initialization scheme
94  * (SuperDropletPC) sets this false, so those names stay free for it to define
95  * with its own meaning under the same input prefix. */
96  bool read_particle_init = true;
97 
98  /*! Value of advect_with_gravity before the input file is consulted. Tracers
99  * default to off, super-droplets to on. */
100  bool advect_with_gravity = false;
101 };
102 
103 class ERFPC : public amrex::ParticleContainer< 0, // AoS real attributes
104  0, // AoS integer attributes
105  ERFParticlesRealIdx::ncomps, // SoA real attributes
106  ERFParticlesIntIdx::ncomps, // SoA integer attributes
107  amrex::DefaultAllocator >
108 {
109  public:
110 
111  /*! Constructor */
112  ERFPC ( amrex::ParGDBBase* a_gdb,
113  const std::string& a_name = "particles",
114  const ERFPCOptions& a_opts = ERFPCOptions() )
115  : amrex::ParticleContainer< 0, // AoS real attributes
116  0, // AoS integer attributes
117  ERFParticlesRealIdx::ncomps, // SoA real attributes
118  ERFParticlesIntIdx::ncomps, // SoA integer attributes
119  amrex::DefaultAllocator> (a_gdb)
120  {
121  BL_PROFILE("ERFPCPC::ERFPC()");
122  m_name = a_name;
123  m_opts = a_opts;
124  readInputs();
125  }
126 
127  /*! Constructor */
128  ERFPC ( const amrex::Geometry& a_geom,
129  const amrex::DistributionMapping& a_dmap,
130  const amrex::BoxArray& a_ba,
131  const std::string& a_name = "particles",
132  const ERFPCOptions& a_opts = ERFPCOptions() )
133  : amrex::ParticleContainer< 0, // AoS real attributes
134  0, // AoS integer attributes
135  ERFParticlesRealIdx::ncomps, // SoA real attributes
136  ERFParticlesIntIdx::ncomps, // SoA integer attributes
137  amrex::DefaultAllocator> ( a_geom, a_dmap, a_ba )
138  {
139  BL_PROFILE("ERFPCPC::ERFPC()");
140  m_name = a_name;
141  m_opts = a_opts;
142  readInputs();
143  }
144 
145  /*! Initialize particles in domain */
146  virtual void InitializeParticles (const double time, const std::unique_ptr<amrex::MultiFab>& a_ptr = nullptr);
147 
148  /*! Evolve particles for one time step */
149  virtual void EvolveParticles (int a_lev,
150  double a_dt_lev,
151  amrex::Vector<amrex::Vector<amrex::MultiFab>>& a_flow_vars,
152  const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& a_z_phys_nd );
153 
154  /*! Get real-type particle attribute names */
155  virtual amrex::Vector<std::string> varNames () const
156  {
157  BL_PROFILE("ERFPCPC::varNames()");
158  return {AMREX_D_DECL("xvel","yvel","zvel"),"mass","temperature"};
159  }
160 
161  /*! Get real-type particle attribute names */
162  virtual amrex::Vector<std::string> meshPlotVarNames () const
163  {
164  BL_PROFILE("ERFPCPC::varNames()");
165  return {"mass_density"};
166  }
167 
168  /*! Uses midpoint method to advance particles using flow velocity. */
169  virtual void AdvectWithFlow (amrex::MultiFab* a_umac,
170  int a_lev,
171  double a_dt,
172  const std::unique_ptr<amrex::MultiFab>& a_z_height);
173 
174  /*! Uses midpoint method to advance particles falling under gravity. */
175  virtual void AdvectWithGravity (int a_lev,
176  double a_dt,
177  const std::unique_ptr<amrex::MultiFab>& a_z_height);
178 
179  /*! Interpolates flow temperature to particles */
180  virtual void ComputeTemperature (const amrex::MultiFab& a_ucons,
181  int a_lev,
182  double /*a_dt*/,
183  const std::unique_ptr<amrex::MultiFab>& a_z_height);
184 
185  /*! Compute mass density */
186  virtual void massDensity ( amrex::MultiFab&,
187  const amrex::MultiFab& a_z_phys_nd,
188  const int&, const int& a_comp = 0) const;
189 
190  /*! Compute mesh variable from particles */
191  virtual void computeMeshVar ( const std::string& a_var_name,
192  amrex::MultiFab& a_mf,
193  const amrex::MultiFab& a_z_phys_nd,
194  const int a_lev) const
195  {
196  if (a_var_name == "mass_density") {
197  massDensity( a_mf, a_z_phys_nd, a_lev );
198  } else {
199  a_mf.setVal(0.0);
200  }
201  }
202 
203  /*! Specify if particles should advect with flow */
204  inline void setAdvectWithFlow (bool a_flag)
205  {
206  BL_PROFILE("ERFPCPC::setAdvectWithFlow()");
207  m_advect_w_flow = a_flag;
208  }
209  /*! Specify if particles fall under gravity */
210  inline void setAdvectWithGravity (bool a_flag)
211  {
212  BL_PROFILE("ERFPCPC::setAdvectWithGravity()");
213  m_advect_w_gravity = a_flag;
214  }
215 
216  // the following functions should ideally be private or protected, but need to be
217  // public due to CUDA extended lambda capture rules
218 
219  /*! \brief Helper for ParticleToMesh operations on terrain-following grids.
220  *
221  * Generic CIC deposition from particles to a MultiFab. The caller provides
222  * a device lambda `value_func(ptd, i)` that returns the per-particle scalar
223  * to deposit (e.g. mass, number, flux). Implementation in ERFPCParticleToMesh.H.
224  */
225  template<typename ValueFunc>
226  void ERFPCParticleToMesh (amrex::MultiFab& a_mf,
227  const amrex::MultiFab& a_z_phys_nd,
228  int a_lev, int a_comp,
229  ValueFunc&& value_func,
230  bool composite = false) const;
231 
232  /*! Default particle initialization */
233  void initializeParticlesUniformDistributionInBox (const std::unique_ptr<amrex::MultiFab>& a_ptr,
234  const amrex::RealBox& particle_box);
235 
236  /*! Replace pos(2) (zeta) with the corresponding physical z, in place,
237  * on every level. Used around plotfile writes so the on-disk
238  * particles are in physical coordinates. */
239  void ConvertZetaToZ (const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& a_z_phys_nd);
240 
241  /*! Inverse of ConvertZetaToZ: replace pos(2) (physical z) with zeta. */
242  void ConvertZToZeta (const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& a_z_phys_nd);
243 
244  /*! Route fine-level particles whose position is outside the level's
245  * BA down to coarser levels, so per-level Redistribute on the fine
246  * level does not assert. */
247  void ExtractAndRouteOORParticles ( int a_lev );
248 
249  /*! Diagnostic: count particles per level and halo (for AMR) */
250  void CountParticlesPerLevelAndHalo ( int a_finest_level );
251 
252  /*! Split coarse-level particles in newly-refined cells after AMR regrid
253  * to restore the per-cell super-droplet count. Handles cascading
254  * refinement across multiple new levels in a single call (see the
255  * SuperDropletPC override for details). Default no-op. */
256  virtual void SplitParticlesForRefinement ( int /*a_finest_level*/ ) {}
257 
258  /*! Split new entrants and merge departees at AMR level boundaries.
259  * Called every Redistribute(z_phys_nd) to maintain uniform per-cell
260  * super-droplet density across levels. Default no-op. */
261  virtual void SplitMergeAtLevelBoundary () {}
262 
263  /*! Regrid-time de-refinement merge. For cells that were in the old
264  * fine BoxArray at level `a_lev` but are no longer in the current
265  * one, reduces the coarse-level super-droplet count from `np_bin`
266  * to `np_bin / merge_factor`. Default no-op. */
267  virtual void MergeParticlesAtDerefinement ( int /*a_lev*/,
268  const amrex::BoxArray& /*a_old_fine_ba*/,
269  const amrex::IntVect& /*a_ref_ratio*/ ) {}
270 
271  protected:
272 
273  bool m_advect_w_flow; /*!< advect with flow velocity */
274  bool m_advect_w_gravity; /*!< advect under gravitational force */
275 
276  amrex::RealBox m_particle_box; /*!< box within which to place particles */
277 
278  std::string m_name;
279 
280  ERFPCOptions m_opts; /*!< selected by the derived container */ /*!< name of this particle species */
281 
282  std::string m_initialization_type; /*!< initial particle distribution type */
283  int m_ppc_init; /*!< initial number of particles per cell */
284 
285  double m_inject_start_time; /*!< particles will only be initialized/injected if time >= m_inject_start_time */
286 
287  bool m_initialized = false; /*!< have the particles been initialized */
288 
289  bool m_stable_redistribute; /*!< use stable redistribute for deterministic simulations */
290 
291  /*! read inputs from file */
292  virtual void readInputs ();
293 
294  private:
295 
296  bool place_randomly_in_cells; /*!< place particles at random positions? */
297 };
298 
299 #endif
300 #endif
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
Dimensionless numeric literals and pure mathematical constants.
@ P
Definition: ERF_IndexDefines.H:221
@ p
Definition: ERF_WSM6.H:303
Definition: ERF_ConsoleIO.cpp:17