Chombo + EB + MF  3.2
DataFileIF.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _DATAFILEIF_H_
12 #define _DATAFILEIF_H_
13 
14 #include <iostream>
15 #include <fstream>
16 using std::istream;
17 using std::ifstream;
18 
19 #include "MayDay.H"
20 #include "RealVect.H"
21 #include "IntVectSet.H"
22 #include "FArrayBox.H"
23 #include "RefCountedPtr.H"
24 
25 #include "BaseIF.H"
26 
27 #include "NamespaceHeader.H"
28 
29 ///
30 /**
31  This implicit function reads data from a file and produces function values
32  from the data read. The data lies on a rectilinear grid where the number
33  of grid points in each direction, the spacing, and the origin are specified
34  (by the user or in the file). Function values within the data region are
35  obtained using trilinear interpolation. Function values outside the data
36  region are the maximum value of the data read.
37 
38  The data can be read from standard input or a named file. The data can
39  have no header, an ASCII header containing the number of grid points in each
40  direction:
41  <pre>
42  numX numY [numZ]
43  </pre>
44  or an ASCII header containing the above information followed by the data
45  spacing in each direction and the origin of the data - both in physical
46  coordinates:
47  <pre>
48  deltaX deltaY [deltaZ]
49  originX originY [originZ]
50  </pre>
51  Following any header is the data either in ASCII or binary. The data is
52  assumed to vary most rapidly in the x coordinate, then the y coordinate,
53  and, finally, in the z coordinate (for 3D data).
54  */
55 class DataFileIF: public BaseIF
56 {
57 public:
58  ///
59  /**
60  Type of data being read
61  */
62  enum DataType
63  {
65  ASCII = 0,
68  };
69 
70  ///
71  /**
72  Size of data being read
73  */
74  enum DataSize
75  {
77  CharSize = 0,
80  };
81 
82  ///
83  /**
84  Constructor specifying the form of the data (a_dataType - ASCII or
85  Binary), a level set value (a_value), and whether inside the domain
86  is where data is less than the level set value (a_inside). Data is
87  read from standard input and a complete ASCII header (see above) is
88  excepted.
89  */
90  DataFileIF(const DataFileIF::DataType& a_dataType,
91  const Real& a_value,
92  const bool& a_inside,
93  const bool& a_useCubicInterp = false,
94  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
95 
96  ///
97  /**
98  Constructor specifying filename (a_filename), the form of the data
99  (a_dataType - ASCII or Binary), level set value (a_value), and whether
100  inside the domain is where data is less than the level set value
101  (a_inside). Data is read from the file named and a complete ASCII
102  header (see above) is expected.
103  */
104  DataFileIF(const char* const a_filename,
105  const DataFileIF::DataType& a_dataType,
106  const Real& a_value,
107  const bool& a_inside,
108  const bool& a_useCubicInterp = false,
109  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
110 
111  ///
112  /**
113  Constructor specifying the form of the data (a_dataType - ASCII or
114  Binary), the spacing (a_spacing), the physical origin (a_origin), a
115  level set value (a_value), and whether inside the domain is where
116  data is less than the level set value (a_inside). Data is read from
117  standard input and an ASCII header (see above) containing the number
118  of grid points in each direction is excepted.
119  */
120  DataFileIF(const DataFileIF::DataType& a_dataType,
121  const RealVect& a_spacing,
122  const RealVect& a_origin,
123  const Real& a_value,
124  const bool& a_inside,
125  const bool& a_useCubicInterp = false,
126  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
127 
128  ///
129  /**
130  Constructor specifying filename (a_filename), the form of the data
131  (a_dataType - ASCII or Binary), the spacing (a_spacing), the physical
132  origin (a_origin), a level set value (a_value), and whether inside
133  the domain is where data is less than the level set value (a_inside).
134  Data is read from the file named and an ASCII header (see above)
135  containing the number of grid points in each direction is excepted.
136  */
137  DataFileIF(const char* const a_filename,
138  const DataFileIF::DataType& a_dataType,
139  const RealVect& a_spacing,
140  const RealVect& a_origin,
141  const Real& a_value,
142  const bool& a_inside,
143  const bool& a_useCubicInterp = false,
144  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
145 
146  ///
147  /**
148  Constructor specifying the form of the data (a_dataType - ASCII or
149  Binary), the spacing (a_spacing), the physical origin (a_origin), the
150  number of grid points in each direction (a_num), a level set value
151  (a_value), and whether inside the domain is where data is less than
152  the level set value (a_inside). Data is read from standard input
153  and no ASCII header (see above) is excepted.
154  */
155  DataFileIF(const DataFileIF::DataType& a_dataType,
156  const IntVect& a_num,
157  const RealVect& a_spacing,
158  const RealVect& a_origin,
159  const Real& a_value,
160  const bool& a_inside,
161  const bool& a_useCubicInterp = false,
162  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
163 
164  ///
165  /**
166  Constructor specifying filename (a_filename), the form of the data
167  (a_dataType - ASCII or Binary), the spacing (a_spacing), the physical
168  origin (a_origin), the number of grid points in each direction (a_num),
169  a level set value (a_value), and whether inside the domain is where
170  data is less than the level set value (a_inside). Data is read from
171  the file named and no ASCII header (see above) is excepted.
172  */
173  DataFileIF(const char* const a_filename,
174  const DataFileIF::DataType& a_dataType,
175  const IntVect& a_num,
176  const RealVect& a_spacing,
177  const RealVect& a_origin,
178  const Real& a_value,
179  const bool& a_inside,
180  const bool& a_useCubicInterp = false,
181  const DataFileIF::DataSize& a_dataSize = DataFileIF::InvalidSize);
182 
183  /// Copy constructor
184  DataFileIF(const DataFileIF& a_inputIF);
185 
186  ///
187  /**
188  This is used by the factory (see below) to create a new object. All
189  objects created in this way share a refcounted pointer to their data.
190 
191  Constructor specifying a refcounted pointer to the data (a_real_data
192  or a_char_data), the no data value (a_noDataValue), the spacing
193  (a_spacing), the physical origin (a_origin), the number of grid points
194  in each direction (a_num), a level set value (a_value), and whether
195  inside the domain is where data is less than the level set value
196  (a_inside).
197  */
198  DataFileIF(const RefCountedPtr<FArrayBox> a_real_data,
199  const RefCountedPtr<BaseFab<unsigned char> > a_char_data,
200  const Real& a_noDataValue,
201  const IntVect& a_num,
202  const RealVect& a_spacing,
203  const RealVect& a_origin,
204  const Real& a_value,
205  const bool& a_inside,
206  const bool& a_useCubicInterp = false);
207 
208  /// Destructor
209  virtual ~DataFileIF();
210 
211  ///
212  /**
213  Return the header information
214  */
215  virtual void GetHeader(IntVect& a_num,
216  RealVect& a_spacing,
217  RealVect& a_origin) const;
218 
219  ///
220  /**
221  Return the parameter information
222  */
223  virtual void GetParams(Real& a_value,
224  bool& a_inside,
225  bool& a_useCubicInterp) const;
226 
227  ///
228  /**
229  Set the parameter information
230  */
231  virtual void SetParams(const Real& a_value,
232  const bool& a_inside,
233  const bool& a_useCubicInterp = false);
234 
235  ///
236  /**
237  value to use when we are outside
238  */
239  virtual void SetNoDataValue(const Real& a_value);
240 
241  ///
242  /**
243  Return the value of the function at a_point using trilinear interpolation
244  of the data. If a_point is outside the data then return the maximum data
245  value.
246  */
247  virtual Real value(const RealVect& a_point) const;
248 
249  virtual Real value(const IndexTM<Real,GLOBALDIM>& a_point) const;
250 
251  virtual Real value(const IndexTM<int,GLOBALDIM> & a_partialDerivative,
252  const IndexTM<Real,GLOBALDIM>& a_point) const;
253 
254  virtual BaseIF* newImplicitFunction() const;
255 
256  void GetFullHeader(IntVect& a_num,
257  RealVect& a_spacing,
258  RealVect& a_origin)
259  {
260  a_num = m_num;
261  a_spacing = m_spacing;
262  a_origin = m_origin;
263  }
264 
266  {
267  return m_real_data;
268  }
269 
271  {
272  return m_char_data;
273  }
274 
275 protected:
276  void OpenFile(ifstream& a_file,
277  const char* const a_filename);
278 
279  void CloseFile(ifstream& a_file);
280 
281  void ReadMinHeader(IntVect& a_num,
282  istream& a_file);
283 
284  void ReadFullHeader(IntVect& a_num,
285  RealVect& a_spacing,
286  RealVect& a_origin,
287  istream& a_file);
288 
289  void ReadData(Real& a_maxValue,
290  istream& a_file,
291  const DataFileIF::DataType& a_dataType,
292  DataFileIF::DataSize a_dataSize,
293  const IntVect& a_num);
294 
295  void MakeCorners(void);
296 
297  IntVect m_num; // number of grid points in each direction
298  RealVect m_spacing; // grid spacing (in physical coordinates)
299  RealVect m_origin; // grid origin (in physical coordinates)
300 
301  Real m_value; // level set value
302 
303  bool m_inside; // inside less than flag
304 
305  // the data - copies all share this
306  RefCountedPtr<FArrayBox> m_real_data; // Real data stored as Real
307  RefCountedPtr<BaseFab<unsigned char> > m_char_data; // Char data stored as unsigned char's
308 
309  Real m_noDataValue; // no data value
310 
311  IntVectSet m_cornersLinear; // corners of a 1x1 box for linear interpolation
312  IntVectSet m_cornersCubic; // corners of a 4x4 box for cubic interpolation
313 
315 
316 private:
318  {
319  MayDay::Abort("DataFileIF uses strong construction");
320  }
321 
322  void operator=(const DataFileIF& a_inputIF)
323  {
324  MayDay::Abort("DataFileIF doesn't allow assignment");
325  }
326 };
327 
328 #include "NamespaceFooter.H"
329 #endif
bool m_inside
Definition: DataFileIF.H:303
An irregular domain on an integer lattice.
Definition: IntVectSet.H:44
virtual void SetParams(const Real &a_value, const bool &a_inside, const bool &a_useCubicInterp=false)
virtual void SetNoDataValue(const Real &a_value)
void CloseFile(ifstream &a_file)
virtual Real value(const RealVect &a_point) const
Definition: DataFileIF.H:65
Definition: DataFileIF.H:64
RefCountedPtr< FArrayBox > GetRealData(void)
Definition: DataFileIF.H:265
Real m_noDataValue
Definition: DataFileIF.H:309
IntVectSet m_cornersLinear
Definition: DataFileIF.H:311
virtual ~DataFileIF()
Destructor.
void ReadMinHeader(IntVect &a_num, istream &a_file)
virtual void GetParams(Real &a_value, bool &a_inside, bool &a_useCubicInterp) const
Definition: DataFileIF.H:78
void MakeCorners(void)
RealVect m_origin
Definition: DataFileIF.H:299
RealVect m_spacing
Definition: DataFileIF.H:298
bool m_useCubicInterp
Definition: DataFileIF.H:314
virtual BaseIF * newImplicitFunction() const
DataFileIF()
Definition: DataFileIF.H:317
RefCountedPtr< BaseFab< unsigned char > > m_char_data
Definition: DataFileIF.H:307
RefCountedPtr< BaseFab< unsigned char > > GetCharData(void)
Definition: DataFileIF.H:270
Definition: DataFileIF.H:79
Definition: BaseIF.H:32
IntVectSet m_cornersCubic
Definition: DataFileIF.H:312
Definition: DataFileIF.H:66
Definition: DataFileIF.H:55
double Real
Definition: REAL.H:33
DataType
Definition: DataFileIF.H:62
Definition: DataFileIF.H:77
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
Real m_value
Definition: DataFileIF.H:301
void GetFullHeader(IntVect &a_num, RealVect &a_spacing, RealVect &a_origin)
Definition: DataFileIF.H:256
RefCountedPtr< FArrayBox > m_real_data
Definition: DataFileIF.H:306
virtual void GetHeader(IntVect &a_num, RealVect &a_spacing, RealVect &a_origin) const
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
Definition: DataFileIF.H:76
Definition: DataFileIF.H:67
IntVect m_num
Definition: DataFileIF.H:297
void ReadFullHeader(IntVect &a_num, RealVect &a_spacing, RealVect &a_origin, istream &a_file)
void OpenFile(ifstream &a_file, const char *const a_filename)
DataSize
Definition: DataFileIF.H:74
void ReadData(Real &a_maxValue, istream &a_file, const DataFileIF::DataType &a_dataType, DataFileIF::DataSize a_dataSize, const IntVect &a_num)
void operator=(const DataFileIF &a_inputIF)
Definition: DataFileIF.H:322
static void Abort(const char *const a_msg=m_nullString)
Print out message to cerr and exit via abort() (if serial) or MPI_Abort() (if parallel).