Chombo + EB  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  {
64  Invalid = -1,
65  ASCII = 0,
68  };
69 
70  ///
71  /**
72  Constructor specifying the form of the data (a_dataType - ASCII or
73  Binary), a level set value (a_value), and whether inside the domain
74  is where data is less than the level set value (a_inside). Data is
75  read from standard input and a complete ASCII header (see above) is
76  excepted.
77  */
78  DataFileIF(const DataFileIF::DataType& a_dataType,
79  const Real& a_value,
80  const bool& a_inside,
81  const bool& a_useCubicInterp = false);
82 
83  ///
84  /**
85  Constructor specifying filename (a_filename), the form of the data
86  (a_dataType - ASCII or Binary), level set value (a_value), and whether
87  inside the domain is where data is less than the level set value
88  (a_inside). Data is read from the file named and a complete ASCII
89  header (see above) is expected.
90  */
91  DataFileIF(const char* const a_filename,
92  const DataFileIF::DataType& a_dataType,
93  const Real& a_value,
94  const bool& a_inside,
95  const bool& a_useCubicInterp = false);
96 
97  ///
98  /**
99  Constructor specifying the form of the data (a_dataType - ASCII or
100  Binary), the spacing (a_spacing), the physical origin (a_origin), a
101  level set value (a_value), and whether inside the domain is where
102  data is less than the level set value (a_inside). Data is read from
103  standard input and an ASCII header (see above) containing the number
104  of grid points in each direction is excepted.
105  */
106  DataFileIF(const DataFileIF::DataType& a_dataType,
107  const RealVect& a_spacing,
108  const RealVect& a_origin,
109  const Real& a_value,
110  const bool& a_inside,
111  const bool& a_useCubicInterp = false);
112 
113  ///
114  /**
115  Constructor specifying filename (a_filename), the form of the data
116  (a_dataType - ASCII or Binary), the spacing (a_spacing), the physical
117  origin (a_origin), a level set value (a_value), and whether inside
118  the domain is where data is less than the level set value (a_inside).
119  Data is read from the file named and an ASCII header (see above)
120  containing the number of grid points in each direction is excepted.
121  */
122  DataFileIF(const char* const a_filename,
123  const DataFileIF::DataType& a_dataType,
124  const RealVect& a_spacing,
125  const RealVect& a_origin,
126  const Real& a_value,
127  const bool& a_inside,
128  const bool& a_useCubicInterp = false);
129 
130  ///
131  /**
132  Constructor specifying the form of the data (a_dataType - ASCII or
133  Binary), the spacing (a_spacing), the physical origin (a_origin), the
134  number of grid points in each direction (a_num), a level set value
135  (a_value), and whether inside the domain is where data is less than
136  the level set value (a_inside). Data is read from standard input
137  and no ASCII header (see above) is excepted.
138  */
139  DataFileIF(const DataFileIF::DataType& a_dataType,
140  const IntVect& a_num,
141  const RealVect& a_spacing,
142  const RealVect& a_origin,
143  const Real& a_value,
144  const bool& a_inside,
145  const bool& a_useCubicInterp = false);
146 
147  ///
148  /**
149  Constructor specifying filename (a_filename), the form of the data
150  (a_dataType - ASCII or Binary), the spacing (a_spacing), the physical
151  origin (a_origin), the number of grid points in each direction (a_num),
152  a level set value (a_value), and whether inside the domain is where
153  data is less than the level set value (a_inside). Data is read from
154  the file named and no ASCII header (see above) is excepted.
155  */
156  DataFileIF(const char* const a_filename,
157  const DataFileIF::DataType& a_dataType,
158  const IntVect& a_num,
159  const RealVect& a_spacing,
160  const RealVect& a_origin,
161  const Real& a_value,
162  const bool& a_inside,
163  const bool& a_useCubicInterp = false);
164 
165  /// Copy constructor
166  DataFileIF(const DataFileIF& a_inputIF);
167 
168  ///
169  /**
170  This is used by the factory (see below) to create a new object. All
171  objects created in this way share a refcounted pointer to their data.
172 
173  Constructor specifying a refcounted pointer to the data (a_ascii_data
174  or a_binary_data), the no data value (a_noDataValue), the spacing
175  (a_spacing), the physical origin (a_origin), the number of grid points
176  in each direction (a_num), a level set value (a_value), and whether
177  inside the domain is where data is less than the level set value
178  (a_inside).
179  */
180  DataFileIF(const RefCountedPtr<FArrayBox> a_ascii_data,
181  const RefCountedPtr<BaseFab<unsigned char> > a_binary_data,
182  const Real& a_noDataValue,
183  const IntVect& a_num,
184  const RealVect& a_spacing,
185  const RealVect& a_origin,
186  const Real& a_value,
187  const bool& a_inside,
188  const bool& a_useCubicInterp = false);
189 
190  /// Destructor
191  virtual ~DataFileIF();
192 
193  ///
194  /**
195  Return the header information
196  */
197  virtual void GetHeader(IntVect& a_num,
198  RealVect& a_spacing,
199  RealVect& a_origin) const;
200 
201  ///
202  /**
203  Return the parameter information
204  */
205  virtual void GetParams(Real& a_value,
206  bool& a_inside,
207  bool& a_useCubicInterp) const;
208 
209  ///
210  /**
211  Set the parameter information
212  */
213  virtual void SetParams(const Real& a_value,
214  const bool& a_inside,
215  const bool& a_useCubicInterp = false);
216 
217  ///
218  /**
219  value to use when we are outside
220  */
221  virtual void SetNoDataValue(const Real& a_value);
222 
223  ///
224  /**
225  Return the value of the function at a_point using trilinear interpolation
226  of the data. If a_point is outside the data then return the maximum data
227  value.
228  */
229  virtual Real value(const RealVect& a_point) const;
230 
231  virtual Real value(const IndexTM<Real,GLOBALDIM>& a_point) const;
232 
233  virtual Real value(const IndexTM<int,GLOBALDIM> & a_partialDerivative,
234  const IndexTM<Real,GLOBALDIM>& a_point) const;
235 
236  virtual BaseIF* newImplicitFunction() const;
237 
238  void GetFullHeader(IntVect& a_num,
239  RealVect& a_spacing,
240  RealVect& a_origin)
241  {
242  a_num = m_num;
243  a_spacing = m_spacing;
244  a_origin = m_origin;
245  }
246 
248  {
249  return m_ascii_data;
250  }
251 
253  {
254  return m_binary_data;
255  }
256 
257 protected:
258  void OpenFile(ifstream& a_file,
259  const char* const a_filename);
260 
261  void CloseFile(ifstream& a_file);
262 
263  void ReadMinHeader(IntVect& a_num,
264  istream& a_file);
265 
266  void ReadFullHeader(IntVect& a_num,
267  RealVect& a_spacing,
268  RealVect& a_origin,
269  istream& a_file);
270 
271  void ReadData(Real& a_maxValue,
272  istream& a_file,
273  const DataFileIF::DataType& a_dataType,
274  const IntVect& a_num);
275 
276  void MakeCorners(void);
277 
278  IntVect m_num; // number of grid points in each direction
279  RealVect m_spacing; // grid spacing (in physical coordinates)
280  RealVect m_origin; // grid origin (in physical coordinates)
281 
282  Real m_value; // level set value
283 
284  bool m_inside; // inside less than flag
285 
286  // the data - copies all share this
287  RefCountedPtr<FArrayBox> m_ascii_data; // ASCII data stored as Real
288  RefCountedPtr<BaseFab<unsigned char> > m_binary_data; // binary data stored as unsigned char's
289 
290  Real m_noDataValue; // no data value
291 
292  IntVectSet m_cornersLinear; // corners of a 1x1 box for linear interpolation
293  IntVectSet m_cornersCubic; // corners of a 4x4 box for cubic interpolation
294 
296 
297 private:
299  {
300  MayDay::Abort("DataFileIF uses strong construction");
301  }
302 
303  void operator=(const DataFileIF& a_inputIF)
304  {
305  MayDay::Abort("DataFileIF doesn't allow assignment");
306  }
307 };
308 
309 #include "NamespaceFooter.H"
310 #endif
bool m_inside
Definition: DataFileIF.H:284
RefCountedPtr< FArrayBox > GetAsciiData(void)
Definition: DataFileIF.H:247
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
void ReadData(Real &a_maxValue, istream &a_file, const DataFileIF::DataType &a_dataType, const IntVect &a_num)
Definition: DataFileIF.H:65
Real m_noDataValue
Definition: DataFileIF.H:290
IntVectSet m_cornersLinear
Definition: DataFileIF.H:292
virtual ~DataFileIF()
Destructor.
void ReadMinHeader(IntVect &a_num, istream &a_file)
virtual void GetParams(Real &a_value, bool &a_inside, bool &a_useCubicInterp) const
void MakeCorners(void)
RealVect m_origin
Definition: DataFileIF.H:280
RealVect m_spacing
Definition: DataFileIF.H:279
bool m_useCubicInterp
Definition: DataFileIF.H:295
virtual BaseIF * newImplicitFunction() const
DataFileIF()
Definition: DataFileIF.H:298
Definition: BaseIF.H:32
IntVectSet m_cornersCubic
Definition: DataFileIF.H:293
Definition: DataFileIF.H:66
Definition: DataFileIF.H:55
RefCountedPtr< FArrayBox > m_ascii_data
Definition: DataFileIF.H:287
double Real
Definition: REAL.H:33
RefCountedPtr< BaseFab< unsigned char > > m_binary_data
Definition: DataFileIF.H:288
DataType
Definition: DataFileIF.H:62
RefCountedPtr< BaseFab< unsigned char > > GetBinaryData(void)
Definition: DataFileIF.H:252
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
Real m_value
Definition: DataFileIF.H:282
void GetFullHeader(IntVect &a_num, RealVect &a_spacing, RealVect &a_origin)
Definition: DataFileIF.H:238
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:64
Definition: DataFileIF.H:67
IntVect m_num
Definition: DataFileIF.H:278
void ReadFullHeader(IntVect &a_num, RealVect &a_spacing, RealVect &a_origin, istream &a_file)
void OpenFile(ifstream &a_file, const char *const a_filename)
void operator=(const DataFileIF &a_inputIF)
Definition: DataFileIF.H:303
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).