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