SENSEI
A frame work for generic in situ analytics
DataRequirements.h
1 #ifndef DataRequirements_h
2 #define DataRequirements_h
3 
4 #include "senseiConfig.h"
5 
6 #include <string>
7 #include <vector>
8 #include <map>
9 #include <set>
10 #include <pugixml.hpp>
11 
12 namespace sensei
13 {
14 
15 class DataAdaptor;
16 class MeshRequirementsIterator;
17 class ArrayRequirementsIterator;
18 
19 /** This is a helper class that handles the common task of specifying the set
20  * of meshes and arrays rqeuired to perform a specific analysis. An analysis
21  * would typically intialize the data requirement from XML and then probe the
22  * requirements during execution to get the minimal set of data required to
23  * complete the analysis.
24  */
25 class SENSEI_EXPORT DataRequirements
26 {
27 public:
30 
31  /// Returns true if the object is empty
32  bool Empty() const { return this->MeshArrayMap.empty(); }
33 
34  /** initialize from XML. the XML should contain one or more
35  * mesh elements each with zero or more array groups
36  *
37  * ```xml
38  * <parent>
39  * <mesh name="mesh_1" structure_only="1">
40  * <cell_arrays> array_1, ... array_n </cell_arrays>
41  * <point_arrays> array_1, ... array_n </point_arrays>
42  * </mesh>
43  * .
44  * .
45  * .
46  * <mesh name="mesh_n" structure_only="0">
47  * <cell_arrays> array_1, ... array_n </cell_arrays>
48  * <point_arrays> array_1, ... array_n </point_arrays>
49  * </mesh>
50  * </parent>
51  * ```
52  *
53  * @param[in] parent XML node which contains mesh elements
54  * @returns the number of mesh elements processed.
55  */
56  int Initialize(pugi::xml_node parent);
57 
58  /** This is a convenience method that fills in all of data that the data
59  * adaptor makes avaialable.
60  *
61  * @param[in] adaptor a DataAdaptor instance
62  * @param[in] structureOnly true if mesh geometry
63  * and topology are not needed
64  * @returns zero if successful, non zero if an error occurred
65  */
66  int Initialize(DataAdaptor *adaptor, bool structureOnly);
67 
68  /** Adds a mesh. The requirement consists of a mesh name and weather it is
69  * structure only
70  *
71  * @param[in] meshName name of the mesh
72  * @param[in] structureOnly flag indicating if mesh geometry is needed
73  * @returns zero if successful
74  */
75  int AddRequirement(const std::string &meshName, bool structureOnly);
76 
77  /** Adds a set of arrays on a specific mesh. The requirement consists of a
78  * mesh name and a list of arrays
79  *
80  * @param[in] meshName name of the mesh
81  * @param[in] association type of arrays
82  * @param[in] arrays a list of the required arrays
83  * @returns zero if successful
84  */
85  int AddRequirement(const std::string &meshName, int association,
86  const std::vector<std::string> &arrays);
87 
88  /** Adds a set of arrays on a specific mesh. The requirement consists of a
89  * mesh name and a list of arrays
90  *
91  * @param[in] meshName name of the mesh
92  * @param[in] association type of the required array
93  * @param[in] array name of the required array
94  * @returns zero if successful
95  */
96  int AddRequirement(const std::string &meshName, int association,
97  const std::string &array);
98 
99  /** Get the list of meshes.
100  *
101  * @param[out] meshes a vector where mesh names will be stored
102  * @returns zero if successful
103  */
104  int GetRequiredMeshes(std::vector<std::string> &meshes) const;
105 
106  /// Get the number of required meshes
107  unsigned int GetNumberOfRequiredMeshes() const;
108 
109  /// Get the name of the ith required mesh
110  int GetRequiredMesh(unsigned int id, std::string &mesh) const;
111 
112  /** For the named mesh, gets the list of required arrays.
113  *
114  * @param[in] meshName the name of the mesh
115  * @param[in] association svtkDataObject::POINT, svtkDataObject::CELL, etc
116  * @param[out] arrays a vector where the arrays will be stored
117  * @returns zero if successful
118  */
119  int GetRequiredArrays(const std::string &meshName, int association,
120  std::vector<std::string> &arrays) const;
121 
122  /** For the named mesh, and association, gets the number of required arrays
123  *
124  * @param[in] meshName the name of the mesh
125  * @param[in] association svtkDataObject::POINT, svtkDataObject::CELL, etc
126  * @param[out] number of arrays
127  * @returns zero if successful
128  */
129  int GetNumberOfRequiredArrays(const std::string &meshName,
130  int association, unsigned int &nArrays) const;
131 
132  /// Clear the contents of the container
133  void Clear();
134 
135  /// Get an iterator for the named mesh
136  MeshRequirementsIterator GetMeshRequirementsIterator() const;
137 
138  /// Get an iterator for the associated arrays
139  ArrayRequirementsIterator GetArrayRequirementsIterator(
140  const std::string &meshName) const;
141 
142 public:
143  using AssocArrayMapType = std::map<int, std::vector<std::string>>;
144  using MeshArrayMapType = std::map<std::string, AssocArrayMapType>;
145  using MeshNamesType = std::map<std::string, bool>;
146 
147 private:
148  friend class ArrayRequirementsIterator;
149  friend class MeshRequirementsIterator;
150 
151  MeshNamesType MeshNames;
152  MeshArrayMapType MeshArrayMap;
153 };
154 
155 /// iterate over the meshes
157 {
158 public:
159  MeshRequirementsIterator() : Valid(false) {}
160 
161  MeshRequirementsIterator(const DataRequirements::MeshNamesType &meshNames)
162  : Valid(true), It(meshNames.cbegin()), End(meshNames.cend()) {}
163 
164  /// advance to the next requirement
165  MeshRequirementsIterator &operator++(){ ++this->It; return *this; }
166 
167  /// Test if the iterator is finished
168  operator bool() const { return Valid && (this->It != this->End); }
169 
170  /// returns the mesh name
171  const std::string &MeshName(){ return this->It->first; }
172 
173  bool StructureOnly(){ return this->It->second; }
174 
175 private:
176  bool Valid;
177  DataRequirements::MeshNamesType::const_iterator It;
178  DataRequirements::MeshNamesType::const_iterator End;
179 };
180 
181 
182 /** Iterate over the mesh's arrays. One can iterate either over associations
183  * and access all of an associations arrays or one by one over the arrays.
184  */
186 {
187 public:
188  enum {MODE_ARRAY, MODE_ASSOCIATION};
189 
190  ArrayRequirementsIterator() : Valid(false), Mode(MODE_ARRAY) {}
191 
192  ArrayRequirementsIterator(const DataRequirements::AssocArrayMapType &aa)
193  : Valid(true), Mode(MODE_ARRAY), It(aa.cbegin()), End(aa.cend())
194  { this->UpdateArrayIterator(); }
195 
196  /// Set the mode of operator++
197  void SetMode(int mode){ this->Mode = mode; }
198 
199  /// Test if the iterator is finished
200  operator bool() const
201  {
202  if (Valid)
203  {
204  if (this->Mode == MODE_ASSOCIATION)
205  {
206  if (this->It == this->End)
207  return false;
208  else
209  return true;
210  }
211  else if (this->Mode == MODE_ARRAY)
212  {
213  if ((this->It == this->End) && (this->AIt == this->AEnd))
214  return false;
215  else
216  return true;
217  }
218  }
219  return false;
220  }
221 
222  /// returns the type of requirement (point,cell, etc)
223  int Association(){ return this->It->first; }
224 
225  /// Get the next group of requied arrays
226  const std::vector<std::string> &Arrays(){ return this->It->second; }
227 
228  /// Get the next requied array
229  const std::string &Array(){ return *this->AIt; }
230 
231  /// Advance the iterator
233  {
234  if (this->Mode == MODE_ARRAY)
235  return this->NextArray();
236  return this->NextAssociation();
237  }
238 
239  /// move to the next group od arrays
241  {
242  if (this->It != this->End)
243  {
244  ++this->It;
245  this->UpdateArrayIterator();
246  // the current association is empty
247  if (this->AIt == this->AEnd)
248  return this->NextAssociation();
249  }
250  return *this;
251  }
252 
253  /// advance to the next array
255  {
256  ++this->AIt;
257  // at the end of this association's arrays
258  if (this->AIt == this->AEnd)
259  this->NextAssociation();
260  return *this;
261  }
262 
263 private:
264 
265  /// move array iterator to the next group of arrays
266  void UpdateArrayIterator()
267  {
268  if (this->It != this->End)
269  {
270  this->AIt = this->It->second.cbegin();
271  this->AEnd = this->It->second.cend();
272  // the current association is empty
273  if (this->AIt == this->AEnd)
274  this->NextAssociation();
275  }
276  }
277 
278  bool Valid;
279  int Mode;
280  DataRequirements::AssocArrayMapType::const_iterator It;
281  DataRequirements::AssocArrayMapType::const_iterator End;
282  std::vector<std::string>::const_iterator AIt;
283  std::vector<std::string>::const_iterator AEnd;
284 };
285 
286 }
287 
288 #endif
MeshRequirementsIterator & operator++()
advance to the next requirement
Definition: DataRequirements.h:165
Iterate over the mesh&#39;s arrays.
Definition: DataRequirements.h:185
int Initialize(MPI_Comm comm, const std::string &fileName, InTransitDataAdaptor *&dataAdaptor)
Creates a sensei::ConfigurableAnalysis adaptor and sensei::InTransitDataAdaptor based on a SENSEI XML...
int Association()
returns the type of requirement (point,cell, etc)
Definition: DataRequirements.h:223
ArrayRequirementsIterator & operator++()
Advance the iterator.
Definition: DataRequirements.h:232
const std::string & Array()
Get the next requied array.
Definition: DataRequirements.h:229
ArrayRequirementsIterator & NextAssociation()
move to the next group od arrays
Definition: DataRequirements.h:240
bool Empty() const
Returns true if the object is empty.
Definition: DataRequirements.h:32
void SetMode(int mode)
Set the mode of operator++.
Definition: DataRequirements.h:197
iterate over the meshes
Definition: DataRequirements.h:156
This is a helper class that handles the common task of specifying the set of meshes and arrays rqeuir...
Definition: DataRequirements.h:25
const std::vector< std::string > & Arrays()
Get the next group of requied arrays.
Definition: DataRequirements.h:226
SENSEI.
Definition: ADIOS2AnalysisAdaptor.h:27
Base class that defines the interface for fetching data from a simulation.
Definition: DataAdaptor.h:25
ArrayRequirementsIterator & NextArray()
advance to the next array
Definition: DataRequirements.h:254
const std::string & MeshName()
returns the mesh name
Definition: DataRequirements.h:171