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