VTK  9.2.20230331
vtkAMReXGridReaderInternal.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkAMReXGridReaderInternal.hpp
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
23 #ifndef vtkAMReXGridReaderInternal_h
24 #define vtkAMReXGridReaderInternal_h
25 
26 #include <map>
27 #include <string>
28 #include <vector>
29 
30 #include "vtkDataSet.h"
32 
33 VTK_ABI_NAMESPACE_BEGIN
34 class vtkIndent;
35 
36 //==============================================================================
37 // I N T E R N A L A M R e X R E A D E R
38 //==============================================================================
39 
40 // ----------------------------------------------------------------------------
41 // Class RealDecriptor (begin)
42 // ----------------------------------------------------------------------------
43 
44 /*
45  floating point format specification (fmt):
46  - fmt[0] = # of bits per number
47  - fmt[1] = # of bits in exponent
48  - fmt[2] = # of bits in mantissa
49  - fmt[3] = start bit of sign
50  - fmt[4] = start bit of exponent
51  - fmt[5] = start bit of mantissa
52  - fmt[6] = high order mantissa bit (CRAY needs this)
53  - fmt[7] = bias of exponent
54 
55  64 11 52 0 1 12 0 1023 - IEEE Double
56 
57  byte order (ord) handles endianness (and defines size such as float or double)
58  - ord[0] = byte in 1st byte
59  - ord[1] = byte in 2nd byte
60  - ord[2] = byte in 3rd byte
61  - ord[3] = byte in 4th byte
62  - ...
63 */
64 
66 {
67 public:
69  RealDescriptor(const long* format, const int* order, int order_length);
70  const long* format() const&;
71  const std::vector<long>& formatarray() const&;
72  const int* order() const&;
73  const std::vector<int>& orderarray() const&;
74  long numBytes() const;
75  bool operator==(const RealDescriptor& rd) const;
76 
77 private:
78  std::vector<long> fr;
79  std::vector<int> ord;
80 };
81 
82 // ----------------------------------------------------------------------------
83 // Class RealDescriptor ( end )
84 // ----------------------------------------------------------------------------
85 
86 // ----------------------------------------------------------------------------
87 // Class vtkAMReXGridHeader (begin)
88 // ----------------------------------------------------------------------------
89 
91 {
92 public:
95  std::vector<std::string> variableNames;
96 
97  // prefix string indicating a variable is a vector component
98  // Note: this prefix will be removed from any variable name
99  // whether or not the variable name is a properly formed
100  // vector variable name (contains a proper postfix)
101  std::string vectorNamePrefix = "amrexvec";
102 
103  // delimiter must be the same after prefix and before postfix
104  char nameDelim = '_';
105 
106  // variableNames map to (potentially a collection of) variableNames indices
107  std::map<std::string, std::vector<int>> parsedVariableNames;
108  std::map<std::string, std::vector<int>> extraMultiFabParsedVarNames;
109  std::map<std::string, int> extraMultiFabParsedVarMap;
110 
111  int dim;
112  double time;
114  std::vector<double> problemDomainLoEnd;
115  std::vector<double> problemDomainHiEnd;
116  std::vector<int> refinementRatio;
117  std::vector<std::vector<std::vector<int>>> levelDomains;
118  std::vector<int> levelSteps;
119  std::vector<std::vector<double>> cellSize;
122  std::vector<int> levelSize;
123  std::vector<std::vector<std::vector<std::vector<double>>>> levelCells;
124  std::vector<std::string> levelPrefix;
125  std::vector<std::string> multiFabPrefix;
126 
127  // use this to store the prefixes for extra multifabs appended to the end of the main header
129  // only allow one topology per multifab. 0 == vertex data, 3 == cell data
130  // edge and face data not supported
131  std::vector<int> extraMultiFabVarTopology;
132  // prefix for each multifab on each level [fab][level]
133  std::vector<std::vector<std::string>> extraMultiFabPrefixes;
134  // vector of variables stored on each fab [fab][variable]
135  std::vector<std::vector<std::string>> extraMultiFabVariables;
136 
138 
140 
141  void PrintSelf(std::ostream& os, vtkIndent indent);
142  void PrintSelfGenericHeader(std::ostream& os, vtkIndent indent);
143  bool Parse(const std::string& headerData);
144  bool ParseGenericHeader(const std::string& headerData);
145 
146  void SetVectorNamePrefix(const std::string& prefix);
147  void SetNameDelimiter(char delim);
148 
149 private:
150  // if the vectorNamePrefix is detected at the beginning of the name,
151  // remove it along with the expected x/y/z postfix. Otherwise, return
152  // the original string
153  std::string GetBaseVariableName(const std::string& name);
154 
155  // returns 0 if postfix is x, 1 for y and 2 for z. returns -1 otherwise
156  int CheckComponent(const std::string& name);
157 
158  // check if name has the vectorNamePrefix
159  bool HasVectorPrefix(const std::string& name);
160 };
161 
162 // ----------------------------------------------------------------------------
163 // Class vtkAMReXGridHeader ( end )
164 // ----------------------------------------------------------------------------
165 
166 // ----------------------------------------------------------------------------
167 // Class vtkAMReXGridLevelHeader (begin)
168 // ----------------------------------------------------------------------------
169 
171 {
172  enum Version
173  {
174  Undefined_v1 = 0, // ---- undefined
175  Version_v1 = 1, // ---- auto converting version with headers
176  // ---- for each fab in the data files and
177  // ---- min and max values for each fab in the header
178  NoFabHeader_v1 = 2, // ---- no fab headers, no fab mins or maxes
179  NoFabHeaderMinMax_v1 = 3, // ---- no fab headers,
180  // ---- min and max values for each fab in the header
181  NoFabHeaderFAMinMax_v1 = 4 // ---- no fab headers, no fab mins or maxes,
182  // ---- min and max values for each FabArray in the header
183  };
184  enum Ordering
185  {
186  NormalOrder = 1,
187  ReverseOrder = 2
188  };
189 
190 public:
191  int level;
192  int dim;
194  int levelHow;
199  std::vector<std::vector<std::vector<int>>> levelBoxArrays;
202  std::vector<std::string> levelFABFile;
203  std::vector<long> levelFileOffset;
204  std::vector<std::vector<double>> levelMinimumsFAB;
205  std::vector<std::vector<double>> levelMaximumsFAB;
206  std::vector<double> levelFABArrayMinimum;
207  std::vector<double> levelFABArrayMaximum;
211 
213  void PrintSelf(std::ostream& os, vtkIndent indent);
214  void PrintSelfLevelHeader(std::ostream& os, vtkIndent indent);
215  bool Parse(int _level, int _dim, const std::string& headerData);
216  bool ParseLevelHeader(int _level, int _dim, const std::string& headerData);
217 };
218 
219 // ----------------------------------------------------------------------------
220 // Class vtkAMReXGridLevelHeader ( end )
221 // ----------------------------------------------------------------------------
222 
223 // ----------------------------------------------------------------------------
224 // Class vtkAMReXGridReaderInternal (begin)
225 // ----------------------------------------------------------------------------
226 
228 {
229 public:
234  void PrintSelf(std::ostream& os, vtkIndent indent);
235  void SetFileName(char* fName);
236 
237  void ReadMetaData();
238  bool ReadHeader();
242  int GetBlockLevel(int blockIdx);
244  int GetBlockIndexWithinLevel(int blockIdx, int level);
245  void GetBlockAttribute(const char* attribute, int blockIdx, vtkDataSet* pDataSet);
246  void GetExtraMultiFabBlockAttribute(const char* attribute, int blockIdx, vtkDataSet* pDataSet);
247  int GetOffsetOfAttribute(const char* attribute);
248  int GetAttributeOffsetExtraMultiFab(const char* attribute, int fabIndex);
249  int GetExtraMultiFabIndex(const char* attribute);
250  void ReadFAB(std::istream& is);
251  int ReadVersion(std::istream& is);
252  void ReadOrder(std::istream& is, std::vector<int>& ar);
253  void PrintOrder(std::vector<int>& ar);
254  void ReadFormat(std::istream& is, std::vector<long>& ar);
255  void PrintFormat(std::vector<long>& ar);
256  RealDescriptor* ReadRealDescriptor(std::istream& is);
257  long ReadBoxArray(std::istream& is, int* boxArray, int* boxArrayDim);
258  void PrintBoxArray(int* boxArray);
259  int ReadNumberOfAttributes(std::istream& is);
260  void ReadBlockAttribute(std::istream& is, long numberOfPoints, long size, char* buffer);
261  void Convert(
262  void* out, const void* in, long nitems, const RealDescriptor& ord, const RealDescriptor& ird);
264  void* out, const void* in, long nitems, const int* outord, const int* inord, int REALSIZE);
265 
266  template <typename T>
267  void CreateVTKAttributeArray(vtkAOSDataArrayTemplate<T>* dataArray, const RealDescriptor* ord,
268  const RealDescriptor* ird, const std::vector<std::vector<char>>& buffers, int numberOfPoints,
269  const std::string& attribute);
270 
276  friend class vtkAMReXGridHeader;
277  std::vector<vtkAMReXGridLevelHeader*> LevelHeader;
278  std::vector<std::vector<vtkAMReXGridLevelHeader*>> ExtraMultiFabHeader;
279  friend class vtkAMReXGridLeveHeader;
280 };
281 
282 template <typename T>
284  const RealDescriptor* ord, const RealDescriptor* ird,
285  const std::vector<std::vector<char>>& buffers, int numberOfPoints, const std::string& attribute)
286 {
287  int nComps = static_cast<int>(this->Header->parsedVariableNames[attribute].size());
288  if (nComps == 0) // check if the variable is in an extra fab
289  {
290  nComps = static_cast<int>(this->Header->extraMultiFabParsedVarNames[attribute].size());
291  }
292  if (nComps == 0)
293  {
294  return;
295  }
296  dataArray->SetName(attribute.c_str());
297  dataArray->SetNumberOfComponents(nComps);
298  dataArray->SetNumberOfTuples(numberOfPoints);
299  T* arrayPtr = new T[numberOfPoints];
300  for (int j = 0; j < nComps; ++j)
301  {
302  this->Convert(arrayPtr, buffers[j].data(), numberOfPoints, *ord, *ird);
303 
304  // Copy to data array component
305  for (int i = 0; i < numberOfPoints; ++i)
306  {
307  dataArray->SetTypedComponent(i, j, arrayPtr[i]);
308  }
309  }
310  delete[] arrayPtr;
311 }
312 
313 // ----------------------------------------------------------------------------
314 // Class vtkAMReXGridReaderInternal ( end )
315 // ----------------------------------------------------------------------------
316 VTK_ABI_NAMESPACE_END
317 #endif /* vtkAMReXGridReaderInternal_h */
318 // VTK-HeaderTest-Exclude: vtkAMReXGridReaderInternal.h
const int * order() const &
const long * format() const &
long numBytes() const
const std::vector< long > & formatarray() const &
const std::vector< int > & orderarray() const &
RealDescriptor(const long *format, const int *order, int order_length)
void SetNameDelimiter(char delim)
std::vector< std::vector< double > > cellSize
std::vector< int > extraMultiFabVarTopology
std::vector< std::vector< std::string > > extraMultiFabVariables
std::vector< std::vector< std::vector< int > > > levelDomains
std::vector< double > problemDomainLoEnd
bool ParseGenericHeader(const std::string &headerData)
void PrintSelf(std::ostream &os, vtkIndent indent)
std::vector< std::vector< std::string > > extraMultiFabPrefixes
std::vector< double > problemDomainHiEnd
bool Parse(const std::string &headerData)
std::vector< std::string > multiFabPrefix
std::vector< std::string > levelPrefix
std::map< std::string, int > extraMultiFabParsedVarMap
std::vector< int > refinementRatio
std::vector< std::string > variableNames
std::map< std::string, std::vector< int > > parsedVariableNames
std::vector< std::vector< std::vector< std::vector< double > > > > levelCells
void PrintSelfGenericHeader(std::ostream &os, vtkIndent indent)
std::map< std::string, std::vector< int > > extraMultiFabParsedVarNames
void SetVectorNamePrefix(const std::string &prefix)
std::vector< double > levelFABArrayMaximum
std::vector< std::vector< std::vector< int > > > levelBoxArrays
std::vector< std::vector< double > > levelMaximumsFAB
std::vector< std::vector< double > > levelMinimumsFAB
void PrintSelf(std::ostream &os, vtkIndent indent)
void PrintSelfLevelHeader(std::ostream &os, vtkIndent indent)
std::vector< std::string > levelFABFile
bool ParseLevelHeader(int _level, int _dim, const std::string &headerData)
std::vector< double > levelFABArrayMinimum
bool Parse(int _level, int _dim, const std::string &headerData)
Consists of the low-level AMReX Reader used by the vtkAMReXGridReader.
void ReadFAB(std::istream &is)
void ReadFormat(std::istream &is, std::vector< long > &ar)
void Convert(void *out, const void *in, long nitems, const RealDescriptor &ord, const RealDescriptor &ird)
int GetOffsetOfAttribute(const char *attribute)
long ReadBoxArray(std::istream &is, int *boxArray, int *boxArrayDim)
std::vector< vtkAMReXGridLevelHeader * > LevelHeader
void GetBlockAttribute(const char *attribute, int blockIdx, vtkDataSet *pDataSet)
void PrintSelf(std::ostream &os, vtkIndent indent)
int GetAttributeOffsetExtraMultiFab(const char *attribute, int fabIndex)
int GetBlockLevel(int blockIdx)
void PermuteOrder(void *out, const void *in, long nitems, const int *outord, const int *inord, int REALSIZE)
void PrintBoxArray(int *boxArray)
int GetBlockIndexWithinLevel(int blockIdx, int level)
int ReadNumberOfAttributes(std::istream &is)
RealDescriptor * ReadRealDescriptor(std::istream &is)
void PrintOrder(std::vector< int > &ar)
void CreateVTKAttributeArray(vtkAOSDataArrayTemplate< T > *dataArray, const RealDescriptor *ord, const RealDescriptor *ird, const std::vector< std::vector< char >> &buffers, int numberOfPoints, const std::string &attribute)
int GetExtraMultiFabIndex(const char *attribute)
void PrintFormat(std::vector< long > &ar)
void ReadBlockAttribute(std::istream &is, long numberOfPoints, long size, char *buffer)
int ReadVersion(std::istream &is)
std::vector< std::vector< vtkAMReXGridLevelHeader * > > ExtraMultiFabHeader
void SetFileName(char *fName)
void GetExtraMultiFabBlockAttribute(const char *attribute, int blockIdx, vtkDataSet *pDataSet)
void ReadOrder(std::istream &is, std::vector< int > &ar)
Array-Of-Structs implementation of vtkGenericDataArray.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
virtual void SetName(const char *)
Set/get array's name.
abstract class to specify dataset behavior
Definition: vtkDataSet.h:174
void SetNumberOfComponents(int num) override
Set/Get the dimension (n) of the components.
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
a simple class to control print indentation
Definition: vtkIndent.h:120
VTKACCELERATORSVTKMCORE_EXPORT vtkDataArray * Convert(const vtkm::cont::Field &input)
@ vector
Definition: vtkX3D.h:249
@ level
Definition: vtkX3D.h:407
@ name
Definition: vtkX3D.h:231
@ size
Definition: vtkX3D.h:265
@ data
Definition: vtkX3D.h:327
@ string
Definition: vtkX3D.h:502