ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ParticleData.H
Go to the documentation of this file.
1 #ifndef ERF_PARTICLE_DATA_H_
2 #define ERF_PARTICLE_DATA_H_
3 
4 #ifdef ERF_USE_PARTICLES
5 
6 #include <map>
7 #include <vector>
8 #include <list>
9 #include <string>
10 #include <iostream>
11 
12 #include <AMReX_ParmParse.H>
13 #include <AMReX_Print.H>
14 #include <AMReX_Vector.H>
15 #include <AMReX_Gpu.H>
16 
17 #include <ERFPC.H>
18 
19 /**
20  * Container holding many of the particle-related data and options
21  */
22 
23 typedef std::map<std::string, ERFPC*> ParticleSpeciesMap;
24 typedef std::vector<std::string> ParticlesNamesVector;
25 typedef std::list<std::string> ParticlesNamesList;
26 
27 class ParticleData
28 {
29  public:
30 
31  /*! Constructor */
32  ParticleData ()
33  {
34  BL_PROFILE("ParticleData::ParticleData()");
35 
36  amrex::ParmParse pp("particles");
37  m_disable_particle_op = false;
38  pp.query("disable_plt", m_disable_particle_op);
39 
40  m_particle_species.clear();
41  m_namelist.clear();
42  m_namelist_unalloc.clear();
43  }
44 
45  /*! Destructor */
46  ~ParticleData ()
47  {
48  BL_PROFILE("ParticleData::~ParticleData()");
49  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
50  auto particles( m_particle_species[m_namelist[i]] );
51  delete particles;
52  }
53  m_particle_species.clear();
54  m_namelist.clear();
55  m_namelist_unalloc.clear();
56  }
57 
58  /*! Write particle info to plot files */
59  void writePlotFile ( const std::string& a_fname ) const
60  {
61  BL_PROFILE("ParticleData::writePlotFile");
62  if (!m_disable_particle_op) {
63  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
64  auto name( m_namelist[i] );
65  auto particles( m_particle_species.at(name) );
66  particles->WritePlotFile(a_fname, name, particles->varNames());
67  }
68  }
69  }
70 
71  /*! Write checkpoint files */
72  void Checkpoint ( const std::string& a_fname ) const
73  {
74  BL_PROFILE("ParticleData::Checkpoint()");
75  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
76  auto name( m_namelist[i] );
77  auto particles( m_particle_species.at(name) );
78  particles->Checkpoint( a_fname, name, true, particles->varNames() );
79  }
80  }
81 
82  /*! Get mesh plot quantities from each particle container */
83  void GetMeshPlotVarNames ( amrex::Vector<std::string>& a_names ) const
84  {
85  BL_PROFILE("ParticleData::GetMeshPlotVarNames()");
86  a_names.clear();
87  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
88  auto name( m_namelist[i] );
89  auto particles( m_particle_species.at(name) );
90 
91  auto var_names = particles->meshPlotVarNames();
92  for (int n = 0; n < var_names.size(); n++) {
93  a_names.push_back( std::string(name+"_"+var_names[n]) );
94  }
95  }
96  }
97 
98  void GetMeshPlotVar ( const std::string& a_var_name,
99  amrex::MultiFab& a_mf,
100  const int a_lev )
101  {
102  BL_PROFILE("ParticleData::GetMeshPlotVar()");
103  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
104  auto particle_name( m_namelist[i] );
105  auto particles( m_particle_species.at(particle_name) );
106 
107  auto particle_var_names = particles->meshPlotVarNames();
108 
109  for (int n = 0; n < particle_var_names.size(); n++) {
110 
111  std::string var_name = particle_name+"_"+particle_var_names[n];
112  if ( var_name == a_var_name ) {
113  particles->computeMeshVar(particle_var_names[n], a_mf, a_lev);
114  return;
115  }
116  }
117  }
118  amrex::Abort("Requested var_name not found in ParticleData::GetMeshPlotVar");
119  }
120 
121  /*! Redistribute/rebalance particles data */
122  inline void Redistribute ()
123  {
124  BL_PROFILE("ParticleData::Redistribute()");
125  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
126  m_particle_species[m_namelist[i]]->Redistribute();
127  }
128  }
129 
130  /*! Redistribute with terrain-aware k-index fix and level-boundary split/merge */
131  inline void Redistribute (
132  const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& a_z_phys_nd)
133  {
134  BL_PROFILE("ParticleData::Redistribute(z_phys_nd)");
135  for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
136  auto* pc = m_particle_species[m_namelist[i]];
137  pc->FixKIndexAMR(a_z_phys_nd);
138  }
139  }
140 
141  /*! Get species of a given name */
142  inline bool HasSpecies ( const std::string& a_name )
143  {
144  BL_PROFILE("ParticleData::HasSpecies()");
145  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
146  if (it == m_particle_species.end()) {
147  return false;
148  } else {
149  return true;
150  }
151  }
152 
153  /*! Get species of a given name */
154  inline ERFPC* GetSpecies ( const std::string& a_name )
155  {
156  BL_PROFILE("ParticleData::GetSpecies()");
157  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
158  if (it == m_particle_species.end()) {
159  amrex::Print() << "ERROR: unable to find particle species with name \""
160  << a_name << "\"!";
161  return nullptr;
162  } else {
163  return it->second;
164  }
165  }
166 
167  /*! accessor */
168  inline ERFPC* operator[] ( const std::string& a_name )
169  {
170  BL_PROFILE("ParticleData::operator[]");
171  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
172  if (it == m_particle_species.end()) {
173  amrex::Print() << "ERROR: unable to find particle species with name \""
174  << a_name << "\"!";
175  return nullptr;
176  } else {
177  return it->second;
178  }
179  }
180 
181  /*! Get species of a given name (const version) */
182  inline const ERFPC* GetSpecies ( const std::string& a_name ) const
183  {
184  BL_PROFILE("ParticleData::GetSpecies()");
185  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
186  if (it == m_particle_species.end()) {
187  amrex::Print() << "ERROR: unable to find particle species with name \""
188  << a_name << "\"!";
189  return nullptr;
190  } else {
191  return it->second;
192  }
193  }
194 
195  /*! accessor */
196  inline const ERFPC* operator[] ( const std::string& a_name ) const
197  {
198  BL_PROFILE("ParticleData::operator[]");
199  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
200  if (it == m_particle_species.end()) {
201  amrex::Print() << "ERROR: unable to find particle species with name \""
202  << a_name << "\"!";
203  return nullptr;
204  } else {
205  return it->second;
206  }
207  }
208 
209  /*! Add a particle species to this container */
210  inline void pushBack (const std::string& a_name,
211  ERFPC* const a_pc )
212  {
213  BL_PROFILE("ParticleData::pushBack()");
214  AMREX_ASSERT(!contains(a_name));
215  m_particle_species[a_name] = a_pc;
216  m_namelist.push_back(a_name);
217  }
218 
219  /*! Add a name; particle container will be initialized later */
220  inline void addName (const std::string& a_name )
221  {
222  BL_PROFILE("ParticleData::addName()");
223  m_namelist_unalloc.push_back(a_name);
224  }
225 
226  /*! Returns list of names of particle species */
227  inline const ParticlesNamesVector& getNames () const
228  {
229  BL_PROFILE("ParticleData::getNames()");
230  return m_namelist;
231  }
232 
233  /*! Returns list of names of particle species that are unallocated */
234  inline ParticlesNamesList& getNamesUnalloc ()
235  {
236  BL_PROFILE("ParticleData::getNamesUnalloc()");
237  return m_namelist_unalloc;
238  }
239 
240  /*! queries if container has species of a certain name */
241  inline bool contains ( const std::string& a_name ) const
242  {
243  BL_PROFILE("ParticleData::contains()");
244  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
245  return (it != m_particle_species.end());
246  }
247 
248  /*! query if container is empty */
249  inline bool isEmpty () const
250  {
251  BL_PROFILE("ParticleData::isEmpty()");
252  return (m_particle_species.size() == 0);
253  }
254 
255 
256  private:
257 
258  /*! Vector of all particle species */
259  ParticleSpeciesMap m_particle_species;
260  /*! Vector of particle species names */
261  ParticlesNamesVector m_namelist;
262  /*! List with names of unallocated species */
263  ParticlesNamesList m_namelist_unalloc;
264 
265  /*! Disable particle output in plotfile? (because expensive) */
266  bool m_disable_particle_op;
267 };
268 
269 #endif
270 #endif
ParmParse pp("prob")