VTK  9.3.20240417
vtkAMReXGridReaderInternal.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
11 #ifndef vtkAMReXGridReaderInternal_h
12 #define vtkAMReXGridReaderInternal_h
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "vtkDataSet.h"
20 
21 VTK_ABI_NAMESPACE_BEGIN
22 class vtkIndent;
23 
24 //==============================================================================
25 // I N T E R N A L A M R e X R E A D E R
26 //==============================================================================
27 
28 // ----------------------------------------------------------------------------
29 // Class RealDecriptor (begin)
30 // ----------------------------------------------------------------------------
31 
32 /*
33  floating point format specification (fmt):
34  - fmt[0] = # of bits per number
35  - fmt[1] = # of bits in exponent
36  - fmt[2] = # of bits in mantissa
37  - fmt[3] = start bit of sign
38  - fmt[4] = start bit of exponent
39  - fmt[5] = start bit of mantissa
40  - fmt[6] = high order mantissa bit (CRAY needs this)
41  - fmt[7] = bias of exponent
42 
43  64 11 52 0 1 12 0 1023 - IEEE Double
44 
45  byte order (ord) handles endianness (and defines size such as float or double)
46  - ord[0] = byte in 1st byte
47  - ord[1] = byte in 2nd byte
48  - ord[2] = byte in 3rd byte
49  - ord[3] = byte in 4th byte
50  - ...
51 */
52 
54 {
55 public:
57  RealDescriptor(const long* format, const int* order, int order_length);
58  const long* format() const&;
59  const std::vector<long>& formatarray() const&;
60  const int* order() const&;
61  const std::vector<int>& orderarray() const&;
62  long numBytes() const;
63  bool operator==(const RealDescriptor& rd) const;
64 
65 private:
66  std::vector<long> fr;
67  std::vector<int> ord;
68 };
69 
70 // ----------------------------------------------------------------------------
71 // Class RealDescriptor ( end )
72 // ----------------------------------------------------------------------------
73 
74 // ----------------------------------------------------------------------------
75 // Class vtkAMReXGridHeader (begin)
76 // ----------------------------------------------------------------------------
77 
79 {
80 public:
83  std::vector<std::string> variableNames;
84 
85  // prefix string indicating a variable is a vector component
86  // Note: this prefix will be removed from any variable name
87  // whether or not the variable name is a properly formed
88  // vector variable name (contains a proper postfix)
89  std::string vectorNamePrefix = "amrexvec";
90 
91  // delimiter must be the same after prefix and before postfix
92  char nameDelim = '_';
93 
94  // variableNames map to (potentially a collection of) variableNames indices
95  std::map<std::string, std::vector<int>> parsedVariableNames;
96  std::map<std::string, std::vector<int>> extraMultiFabParsedVarNames;
97  std::map<std::string, int> extraMultiFabParsedVarMap;
98 
99  int dim;
100  double time;
102  std::vector<double> problemDomainLoEnd;
103  std::vector<double> problemDomainHiEnd;
104  std::vector<int> refinementRatio;
105  std::vector<std::vector<std::vector<int>>> levelDomains;
106  std::vector<int> levelSteps;
107  std::vector<std::vector<double>> cellSize;
110  std::vector<int> levelSize;
111  std::vector<std::vector<std::vector<std::vector<double>>>> levelCells;
112  std::vector<std::string> levelPrefix;
113  std::vector<std::string> multiFabPrefix;
114 
115  // use this to store the prefixes for extra multifabs appended to the end of the main header
117  // only allow one topology per multifab. 0 == vertex data, 3 == cell data
118  // edge and face data not supported
119  std::vector<int> extraMultiFabVarTopology;
120  // prefix for each multifab on each level [fab][level]
121  std::vector<std::vector<std::string>> extraMultiFabPrefixes;
122  // vector of variables stored on each fab [fab][variable]
123  std::vector<std::vector<std::string>> extraMultiFabVariables;
124 
126 
128 
129  void PrintSelf(std::ostream& os, vtkIndent indent);
130  void PrintSelfGenericHeader(std::ostream& os, vtkIndent indent);
131  bool Parse(const std::string& headerData);
132  bool ParseGenericHeader(const std::string& headerData);
133 
134  void SetVectorNamePrefix(const std::string& prefix);
135  void SetNameDelimiter(char delim);
136 
137 private:
138  // if the vectorNamePrefix is detected at the beginning of the name,
139  // remove it along with the expected x/y/z postfix. Otherwise, return
140  // the original string
141  std::string GetBaseVariableName(const std::string& name);
142 
143  // returns 0 if postfix is x, 1 for y and 2 for z. returns -1 otherwise
144  int CheckComponent(const std::string& name);
145 
146  // check if name has the vectorNamePrefix
147  bool HasVectorPrefix(const std::string& name);
148 };
149 
150 // ----------------------------------------------------------------------------
151 // Class vtkAMReXGridHeader ( end )
152 // ----------------------------------------------------------------------------
153 
154 // ----------------------------------------------------------------------------
155 // Class vtkAMReXGridLevelHeader (begin)
156 // ----------------------------------------------------------------------------
157 
159 {
160  enum Version
161  {
162  Undefined_v1 = 0, // ---- undefined
163  Version_v1 = 1, // ---- auto converting version with headers
164  // ---- for each fab in the data files and
165  // ---- min and max values for each fab in the header
166  NoFabHeader_v1 = 2, // ---- no fab headers, no fab mins or maxes
167  NoFabHeaderMinMax_v1 = 3, // ---- no fab headers,
168  // ---- min and max values for each fab in the header
169  NoFabHeaderFAMinMax_v1 = 4 // ---- no fab headers, no fab mins or maxes,
170  // ---- min and max values for each FabArray in the header
171  };
172  enum Ordering
173  {
174  NormalOrder = 1,
175  ReverseOrder = 2
176  };
177 
178 public:
179  int level;
180  int dim;
182  int levelHow;
187  std::vector<std::vector<std::vector<int>>> levelBoxArrays;
190  std::vector<std::string> levelFABFile;
191  std::vector<long> levelFileOffset;
192  std::vector<std::vector<double>> levelMinimumsFAB;
193  std::vector<std::vector<double>> levelMaximumsFAB;
194  std::vector<double> levelFABArrayMinimum;
195  std::vector<double> levelFABArrayMaximum;
199 
201  void PrintSelf(std::ostream& os, vtkIndent indent);
202  void PrintSelfLevelHeader(std::ostream& os, vtkIndent indent);
203  bool Parse(int _level, int _dim, const std::string& headerData);
204  bool ParseLevelHeader(int _level, int _dim, const std::string& headerData);
205 };
206 
207 // ----------------------------------------------------------------------------
208 // Class vtkAMReXGridLevelHeader ( end )
209 // ----------------------------------------------------------------------------
210 
211 // ----------------------------------------------------------------------------
212 // Class vtkAMReXGridReaderInternal (begin)
213 // ----------------------------------------------------------------------------
214 
216 {
217 public:
222  void PrintSelf(std::ostream& os, vtkIndent indent);
223  void SetFileName(char* fName);
224 
225  void ReadMetaData();
226  bool ReadHeader();
230  int GetBlockLevel(int blockIdx);
232  int GetBlockIndexWithinLevel(int blockIdx, int level);
233  void GetBlockAttribute(const char* attribute, int blockIdx, vtkDataSet* pDataSet);
234  void GetExtraMultiFabBlockAttribute(const char* attribute, int blockIdx, vtkDataSet* pDataSet);
235  int GetOffsetOfAttribute(const char* attribute);
236  int GetAttributeOffsetExtraMultiFab(const char* attribute, int fabIndex);
237  int GetExtraMultiFabIndex(const char* attribute);
238  void ReadFAB(std::istream& is);
239  int ReadVersion(std::istream& is);
240  void ReadOrder(std::istream& is, std::vector<int>& ar);
241  void PrintOrder(std::vector<int>& ar);
242  void ReadFormat(std::istream& is, std::vector<long>& ar);
243  void PrintFormat(std::vector<long>& ar);
244  RealDescriptor* ReadRealDescriptor(std::istream& is);
245  long ReadBoxArray(std::istream& is, int* boxArray, int* boxArrayDim);
246  void PrintBoxArray(int* boxArray);
247  int ReadNumberOfAttributes(std::istream& is);
248  void ReadBlockAttribute(std::istream& is, long numberOfPoints, long size, char* buffer);
249  void Convert(
250  void* out, const void* in, long nitems, const RealDescriptor& ord, const RealDescriptor& ird);
252  void* out, const void* in, long nitems, const int* outord, const int* inord, int REALSIZE);
253 
254  template <typename T>
255  void CreateVTKAttributeArray(vtkAOSDataArrayTemplate<T>* dataArray, const RealDescriptor* ord,
256  const RealDescriptor* ird, const std::vector<std::vector<char>>& buffers, int numberOfPoints,
257  const std::string& attribute);
258 
264  friend class vtkAMReXGridHeader;
265  std::vector<vtkAMReXGridLevelHeader*> LevelHeader;
266  std::vector<std::vector<vtkAMReXGridLevelHeader*>> ExtraMultiFabHeader;
267  friend class vtkAMReXGridLeveHeader;
268 };
269 
270 template <typename T>
272  const RealDescriptor* ord, const RealDescriptor* ird,
273  const std::vector<std::vector<char>>& buffers, int numberOfPoints, const std::string& attribute)
274 {
275  int nComps = static_cast<int>(this->Header->parsedVariableNames[attribute].size());
276  if (nComps == 0) // check if the variable is in an extra fab
277  {
278  nComps = static_cast<int>(this->Header->extraMultiFabParsedVarNames[attribute].size());
279  }
280  if (nComps == 0)
281  {
282  return;
283  }
284  dataArray->SetName(attribute.c_str());
285  dataArray->SetNumberOfComponents(nComps);
286  dataArray->SetNumberOfTuples(numberOfPoints);
287  T* arrayPtr = new T[numberOfPoints];
288  for (int j = 0; j < nComps; ++j)
289  {
290  this->Convert(arrayPtr, buffers[j].data(), numberOfPoints, *ord, *ird);
291 
292  // Copy to data array component
293  for (int i = 0; i < numberOfPoints; ++i)
294  {
295  dataArray->SetTypedComponent(i, j, arrayPtr[i]);
296  }
297  }
298  delete[] arrayPtr;
299 }
300 
301 // ----------------------------------------------------------------------------
302 // Class vtkAMReXGridReaderInternal ( end )
303 // ----------------------------------------------------------------------------
304 VTK_ABI_NAMESPACE_END
305 #endif /* vtkAMReXGridReaderInternal_h */
306 // 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:166
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:108
VTKACCELERATORSVTKMCORE_EXPORT vtkDataArray * Convert(const vtkm::cont::Field &input)
@ vector
Definition: vtkX3D.h:237
@ level
Definition: vtkX3D.h:395
@ name
Definition: vtkX3D.h:219
@ size
Definition: vtkX3D.h:253
@ data
Definition: vtkX3D.h:315
@ string
Definition: vtkX3D.h:490