REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_ParticleData.H
Go to the documentation of this file.
1#ifndef _REMORA_PARTICLE_DATA_H_
2#define _REMORA_PARTICLE_DATA_H_
3
4#ifdef REMORA_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 <REMORA_PC.H>
18
19/**
20 * Container holding many of the particle-related data and options
21 */
22
23typedef std::map<std::string, REMORAPC*> ParticleSpeciesMap;
24typedef std::vector<std::string> ParticlesNamesVector;
25typedef std::list<std::string> ParticlesNamesList;
26
27class ParticleData
28{
29 public:
30
31 /*! Constructor */
32 ParticleData ()
33 {
34 BL_PROFILE("ParticleData::ParticleData()");
35 m_particle_species.clear();
36 m_namelist.clear();
37 m_namelist_unalloc.clear();
38 }
39
40 /*! Destructor */
41 ~ParticleData ()
42 {
43 BL_PROFILE("ParticleData::~ParticleData()");
44 for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
45 auto particles( m_particle_species[m_namelist[i]] );
46 delete particles;
47 }
48 m_particle_species.clear();
49 m_namelist.clear();
50 m_namelist_unalloc.clear();
51 }
52
53 /*! Write checkpoint files */
54 void Checkpoint ( const std::string& a_fname ) const
55 {
56 BL_PROFILE("ParticleData::Checkpoint()");
57 for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
58 auto name( m_namelist[i] );
59 auto particles( m_particle_species.at(name) );
60 particles->Checkpoint( a_fname, name, true, particles->varNames() );
61 }
62 }
63
64 /*! Read from restart file */
65 void Restart ( amrex::ParGDBBase* a_gdb, const std::string& a_fname )
66 {
67 BL_PROFILE("ParticleData::Restart()");
68 AMREX_ASSERT(isEmpty());
69 for (auto it = m_namelist_unalloc.begin(); it != m_namelist_unalloc.end(); ++it) {
70 std::string species_name( *it );
71 REMORAPC* pc = new REMORAPC( a_gdb, species_name );
72 pc->Restart(a_fname, species_name );
73 pushBack( species_name, pc );
74 }
75 m_namelist_unalloc.clear();
76 }
77
78 /*! Get mesh plot quantities from each particle container */
79 void GetMeshPlotVarNames ( amrex::Vector<std::string>& a_names ) const
80 {
81 BL_PROFILE("ParticleData::GetMeshPlotVarNames()");
82 a_names.clear();
83 for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
84 auto name( m_namelist[i] );
85 auto particles( m_particle_species.at(name) );
86
87 auto var_names = particles->meshPlotVarNames();
88 for (int n = 0; n < var_names.size(); n++) {
89 a_names.push_back( std::string(name+"_"+var_names[n]) );
90 }
91 }
92 }
93
94 void GetMeshPlotVar ( const std::string& a_var_name,
95 amrex::MultiFab& a_mf,
96 const int a_lev )
97 {
98 BL_PROFILE("ParticleData::GetMeshPlotVar()");
99 for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
100 auto particle_name( m_namelist[i] );
101 auto particles( m_particle_species.at(particle_name) );
102
103 auto particle_var_names = particles->meshPlotVarNames();
104
105 for (int n = 0; n < particle_var_names.size(); n++) {
106
107 std::string var_name = particle_name+"_"+particle_var_names[n];
108 if ( var_name == a_var_name ) {
109 particles->computeMeshVar(particle_var_names[n], a_mf, a_lev);
110 return;
111 }
112 }
113 }
114 amrex::Abort("Requested var_name not found in ParticleData::GetMeshPlotVar");
115 }
116
117 /*! Redistribute/rebalance particles data */
118 inline void Redistribute ()
119 {
120 BL_PROFILE("ParticleData::Redistribute()");
121 for (ParticlesNamesVector::size_type i = 0; i < m_namelist.size(); i++) {
122 m_particle_species[m_namelist[i]]->Redistribute();
123 }
124 }
125
126 /*! Get species of a given name */
127 inline REMORAPC* GetSpecies ( const std::string& a_name )
128 {
129 BL_PROFILE("ParticleData::GetSpecies()");
130 ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
131 if (it == m_particle_species.end()) {
132 amrex::Print() << "ERROR: unable to find particle species with name \""
133 << a_name << "\"!";
134 return nullptr;
135 } else {
136 return it->second;
137 }
138 }
139
140 /*! accessor */
141 inline REMORAPC* operator[] ( const std::string& a_name )
142 {
143 BL_PROFILE("ParticleData::operator[]");
144 ParticleSpeciesMap::iterator it (m_particle_species.find(a_name));
145 if (it == m_particle_species.end()) {
146 amrex::Print() << "ERROR: unable to find particle species with name \""
147 << a_name << "\"!";
148 return nullptr;
149 } else {
150 return it->second;
151 }
152 }
153
154 /*! Get species of a given name (const version) */
155 inline const REMORAPC* GetSpecies ( const std::string& a_name ) const
156 {
157 BL_PROFILE("ParticleData::GetSpecies()");
158 ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
159 if (it == m_particle_species.end()) {
160 amrex::Print() << "ERROR: unable to find particle species with name \""
161 << a_name << "\"!";
162 return nullptr;
163 } else {
164 return it->second;
165 }
166 }
167
168 /*! accessor */
169 inline const REMORAPC* operator[] ( const std::string& a_name ) const
170 {
171 BL_PROFILE("ParticleData::operator[]");
172 ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
173 if (it == m_particle_species.end()) {
174 amrex::Print() << "ERROR: unable to find particle species with name \""
175 << a_name << "\"!";
176 return nullptr;
177 } else {
178 return it->second;
179 }
180 }
181
182 /*! Add a particle species to this container */
183 inline void pushBack (const std::string& a_name,
184 REMORAPC* const a_pc )
185 {
186 BL_PROFILE("ParticleData::pushBack()");
187 AMREX_ASSERT(!contains(a_name));
188 m_particle_species[a_name] = a_pc;
189 m_namelist.push_back(a_name);
190 }
191
192 /*! Add a name; particle container will be initialized later */
193 inline void addName (const std::string& a_name )
194 {
195 BL_PROFILE("ParticleData::addName()");
196 m_namelist_unalloc.push_back(a_name);
197 }
198
199 /*! Returns list of names of particle species */
200 inline const ParticlesNamesVector& getNames () const
201 {
202 BL_PROFILE("ParticleData::getNames()");
203 return m_namelist;
204 }
205
206 /*! Returns list of names of particle species that are unallocated */
207 inline ParticlesNamesList& getNamesUnalloc ()
208 {
209 BL_PROFILE("ParticleData::getNamesUnalloc()");
210 return m_namelist_unalloc;
211 }
212
213 /*! queries if container has species of a certain name */
214 inline bool contains ( const std::string& a_name ) const
215 {
216 BL_PROFILE("ParticleData::contains()");
217 ParticleSpeciesMap::const_iterator it (m_particle_species.find(a_name));
218 return (it != m_particle_species.end());
219 }
220
221 /*! query if container is empty */
222 inline bool isEmpty () const
223 {
224 BL_PROFILE("ParticleData::isEmpty()");
225 return (m_particle_species.size() == 0);
226 }
227
228
229 private:
230
231 /*! Vector of all particle species */
232 ParticleSpeciesMap m_particle_species;
233 /*! Vector of particle species names */
234 ParticlesNamesVector m_namelist;
235 /*! List with names of unallocated species */
236 ParticlesNamesList m_namelist_unalloc;
237};
238
239#endif
240#endif
241