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  /*! Get species of a given name */
131  inline bool HasSpecies ( const std::string& a_name )
132  {
133  BL_PROFILE("ParticleData::HasSpecies()");
134  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
135  if (it == m_particle_species.end()) {
136  return false;
137  } else {
138  return true;
139  }
140  }
141 
142  /*! Get species of a given name */
143  inline ERFPC* GetSpecies ( const std::string& a_name )
144  {
145  BL_PROFILE("ParticleData::GetSpecies()");
146  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
147  if (it == m_particle_species.end()) {
148  amrex::Print() << "ERROR: unable to find particle species with name \""
149  << a_name << "\"!";
150  return nullptr;
151  } else {
152  return it->second;
153  }
154  }
155 
156  /*! accessor */
157  inline ERFPC* operator[] ( const std::string& a_name )
158  {
159  BL_PROFILE("ParticleData::operator[]");
160  ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
161  if (it == m_particle_species.end()) {
162  amrex::Print() << "ERROR: unable to find particle species with name \""
163  << a_name << "\"!";
164  return nullptr;
165  } else {
166  return it->second;
167  }
168  }
169 
170  /*! Get species of a given name (const version) */
171  inline const ERFPC* GetSpecies ( const std::string& a_name ) const
172  {
173  BL_PROFILE("ParticleData::GetSpecies()");
174  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
175  if (it == m_particle_species.end()) {
176  amrex::Print() << "ERROR: unable to find particle species with name \""
177  << a_name << "\"!";
178  return nullptr;
179  } else {
180  return it->second;
181  }
182  }
183 
184  /*! accessor */
185  inline const ERFPC* operator[] ( const std::string& a_name ) const
186  {
187  BL_PROFILE("ParticleData::operator[]");
188  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
189  if (it == m_particle_species.end()) {
190  amrex::Print() << "ERROR: unable to find particle species with name \""
191  << a_name << "\"!";
192  return nullptr;
193  } else {
194  return it->second;
195  }
196  }
197 
198  /*! Add a particle species to this container */
199  inline void pushBack (const std::string& a_name,
200  ERFPC* const a_pc )
201  {
202  BL_PROFILE("ParticleData::pushBack()");
203  AMREX_ASSERT(!contains(a_name));
204  m_particle_species[a_name] = a_pc;
205  m_namelist.push_back(a_name);
206  }
207 
208  /*! Add a name; particle container will be initialized later */
209  inline void addName (const std::string& a_name )
210  {
211  BL_PROFILE("ParticleData::addName()");
212  m_namelist_unalloc.push_back(a_name);
213  }
214 
215  /*! Returns list of names of particle species */
216  inline const ParticlesNamesVector& getNames () const
217  {
218  BL_PROFILE("ParticleData::getNames()");
219  return m_namelist;
220  }
221 
222  /*! Returns list of names of particle species that are unallocated */
223  inline ParticlesNamesList& getNamesUnalloc ()
224  {
225  BL_PROFILE("ParticleData::getNamesUnalloc()");
226  return m_namelist_unalloc;
227  }
228 
229  /*! queries if container has species of a certain name */
230  inline bool contains ( const std::string& a_name ) const
231  {
232  BL_PROFILE("ParticleData::contains()");
233  ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
234  return (it != m_particle_species.end());
235  }
236 
237  /*! query if container is empty */
238  inline bool isEmpty () const
239  {
240  BL_PROFILE("ParticleData::isEmpty()");
241  return (m_particle_species.size() == 0);
242  }
243 
244 
245  private:
246 
247  /*! Vector of all particle species */
248  ParticleSpeciesMap m_particle_species;
249  /*! Vector of particle species names */
250  ParticlesNamesVector m_namelist;
251  /*! List with names of unallocated species */
252  ParticlesNamesList m_namelist_unalloc;
253 
254  /*! Disable particle output in plotfile? (because expensive) */
255  bool m_disable_particle_op;
256 };
257 
258 #endif
259 #endif
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pp(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:230