Field3D

#include <MACFieldIO.h>

Inheritance diagram for MACFieldIO:
FieldIO RefBase

Public Types

typedef MACFieldIO class_type
 
typedef boost::intrusive_ptr< MACFieldIOPtr
 
- Public Types inherited from FieldIO
typedef FieldIO class_type
 
typedef boost::intrusive_ptr< FieldIOPtr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr< RefBasePtr
 

Public Member Functions

virtual std::string className () const
 Returns the class name. More...
 
 MACFieldIO ()
 Ctor. More...
 
virtual FieldBase::Ptr read (hid_t layerGroup, const std::string &filename, const std::string &layerPath, DataTypeEnum typeEnum)
 Reads the field at the given location and tries to create a MACField object from it. More...
 
virtual bool write (hid_t layerGroup, FieldBase::Ptr field)
 Writes the given field to disk. More...
 
virtual ~MACFieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from FieldIO
 FieldIO ()
 Ctor. More...
 
virtual ~FieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from RefBase
void ref () const
 Used by boost::intrusive_pointer. More...
 
size_t refcnt ()
 Used by boost::intrusive_pointer. More...
 
void unref () const
 Used by boost::intrusive_pointer. More...
 
 RefBase ()
 
 RefBase (const RefBase &)
 Copy constructor. More...
 
RefBaseoperator= (const RefBase &)
 Assignment operator. More...
 
virtual ~RefBase ()
 Destructor. More...
 
virtual bool checkRTTI (const char *typenameStr)=0
 This function is only implemented by concrete classes and triggers the actual RTTI check through matchRTTI();. More...
 
bool matchRTTI (const char *typenameStr)
 Performs a check to see if the given typename string matches this class' This needs to be implemented in -all- subclasses, even abstract ones. More...
 

Static Public Member Functions

static const char * classType ()
 
static FieldIO::Ptr create ()
 
- Static Public Member Functions inherited from FieldIO
static const char * classType ()
 
- Static Public Member Functions inherited from RefBase
static const char * classType ()
 

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_CLASS
 
- Public Attributes inherited from FieldIO
 DEFINE_FIELD_RTTI_ABSTRACT_CLASS
 

Private Types

typedef FieldIO base
 Convenience typedef for referring to base class. More...
 

Private Member Functions

template<class Data_T >
bool readData (hid_t location, typename MACField< Data_T >::Ptr result)
 Reads the data that is dependent on the data type on disk. More...
 
template<class Data_T >
bool writeData (hid_t layerGroup, typename MACField< Data_T >::Ptr field, MACComponent comp)
 This call writes out the u,v,w data. More...
 
template<class Data_T >
bool writeInternal (hid_t layerGroup, typename MACField< Data_T >::Ptr field)
 This call writes all the attributes and sets up the data space. More...
 

Static Private Attributes

static const std::string k_bitsPerComponentStr
 
static const std::string k_componentsStr
 
static const std::string k_dataWindowStr
 
static const std::string k_extentsStr
 
static const std::string k_uDataStr
 
static const std::string k_vDataStr
 
static const std::string k_versionAttrName
 
static const int k_versionNumber
 
static const std::string k_wDataStr
 

Detailed Description

Defines the IO for a MACField object

Definition at line 78 of file MACFieldIO.h.

Member Typedef Documentation

◆ Ptr

typedef boost::intrusive_ptr<MACFieldIO> MACFieldIO::Ptr

Definition at line 85 of file MACFieldIO.h.

◆ class_type

Definition at line 89 of file MACFieldIO.h.

◆ base

typedef FieldIO MACFieldIO::base
private

Convenience typedef for referring to base class.

Definition at line 160 of file MACFieldIO.h.

Constructor & Destructor Documentation

◆ MACFieldIO()

MACFieldIO::MACFieldIO ( )
inline

Ctor.

Definition at line 100 of file MACFieldIO.h.

101  : FieldIO()
102  { }
FieldIO()
Ctor.
Definition: FieldIO.h:105

◆ ~MACFieldIO()

virtual MACFieldIO::~MACFieldIO ( )
inlinevirtual

Dtor.

Definition at line 105 of file MACFieldIO.h.

106  { /* Empty */ }

Member Function Documentation

◆ classType()

static const char* MACFieldIO::classType ( )
inlinestatic

Definition at line 92 of file MACFieldIO.h.

93  {
94  return "MACFieldIO";
95  }

◆ create()

static FieldIO::Ptr MACFieldIO::create ( )
inlinestatic

Definition at line 108 of file MACFieldIO.h.

References read(), and write().

Referenced by initIO().

109  { return Ptr(new MACFieldIO); }
boost::intrusive_ptr< MACFieldIO > Ptr
Definition: MACFieldIO.h:85

◆ read()

FieldBase::Ptr MACFieldIO::read ( hid_t  layerGroup,
const std::string &  filename,
const std::string &  layerPath,
DataTypeEnum  typeEnum 
)
virtual

Reads the field at the given location and tries to create a MACField object from it.

Returns
Null if no object was read

Implements FieldIO.

Definition at line 79 of file MACFieldIO.cpp.

References DataTypeVecDouble, DataTypeVecFloat, DataTypeVecHalf, Hdf5Util::readAttribute(), and ResizableField< Data_T >::setSize().

Referenced by create().

82 {
83  Box3i extents, dataW;
84  int components;
85 
86  //hsize_t dims[1];
87 
88  if (layerGroup == -1)
89  throw BadHdf5IdException("Bad layer group in MACFieldIO::read");
90 
91  int version;
92  if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
93  throw MissingAttributeException("Couldn't find attribute " +
95 
96  if (version != k_versionNumber)
97  throw UnsupportedVersionException("MACField version not supported: " +
98  lexical_cast<std::string>(version));
99 
100  if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x))
101  throw MissingAttributeException("Couldn't find attribute " +
102  k_extentsStr);
103 
104  if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x))
105  throw MissingAttributeException("Couldn't find attribute " +
107 
108  if (!readAttribute(layerGroup, k_componentsStr, 1, components))
109  throw MissingAttributeException("Couldn't find attribute " +
111  // Check the data type ---
112  int bits;
113  if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits))
114  throw MissingAttributeException("Couldn't find attribute: " +
116 
117  // Build a MACField to store everything in
118  FieldBase::Ptr result;
119  switch (bits) {
120  case 16:
121  {
122  if (typeEnum != DataTypeVecHalf) break;
124  field->setSize(extents, dataW);
125  readData<V3h>(layerGroup, field);
126 
127  result = field;
128  }
129  break;
130  case 64:
131  {
132  if (typeEnum != DataTypeVecDouble) break;
134  field->setSize(extents, dataW);
135  readData<V3d>(layerGroup, field);
136 
137  result = field;
138  }
139  break;
140  case 32:
141  default:
142  {
143  if (typeEnum != DataTypeVecFloat) break;
145  field->setSize(extents, dataW);
146  readData<V3f>(layerGroup, field);
147 
148  result = field;
149  }
150  }
151 
152  return result;
153 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Definition: MACField.h:95
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
static const std::string k_componentsStr
Definition: MACFieldIO.h:151
static const int k_versionNumber
Definition: MACFieldIO.h:147
boost::intrusive_ptr< MACField > Ptr
Definition: MACField.h:101
bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
static const std::string k_extentsStr
Definition: MACFieldIO.h:149
static const std::string k_versionAttrName
Definition: MACFieldIO.h:148
static const std::string k_dataWindowStr
Definition: MACFieldIO.h:150
static const std::string k_bitsPerComponentStr
Definition: MACFieldIO.h:152

◆ write()

bool MACFieldIO::write ( hid_t  layerGroup,
FieldBase::Ptr  field 
)
virtual

Writes the given field to disk.

Returns
true if successful, otherwise false

Implements FieldIO.

Definition at line 158 of file MACFieldIO.cpp.

References FIELD3D_NAMESPACE_SOURCE_CLOSE, field_dynamic_cast(), and Hdf5Util::writeAttribute().

Referenced by create().

159 {
160  if (layerGroup == -1) {
161  throw BadHdf5IdException("Bad layer group in MACFieldIO::write");
162  }
163 
164  // Add version attribute
165  if (!writeAttribute(layerGroup, k_versionAttrName,
166  1, k_versionNumber)) {
167  throw WriteAttributeException("Couldn't write attribute " +
169  }
170 
171  MACField<V3h>::Ptr vecHalfField =
173  MACField<V3f>::Ptr vecFloatField =
175  MACField<V3d>::Ptr vecDoubleField =
177 
178  bool success = true;
179  if (vecFloatField) {
180  success = writeInternal<V3f>(layerGroup, vecFloatField);
181  } else if (vecHalfField) {
182  success = writeInternal<V3h>(layerGroup, vecHalfField);
183  } else if (vecDoubleField) {
184  success = writeInternal<V3d>(layerGroup, vecDoubleField);
185  } else {
186  throw WriteLayerException("MACFieldIO does not support the given "
187  "MACField template parameter");
188  }
189 
190  return success;
191 }
Field_T::Ptr field_dynamic_cast(RefBase::Ptr field)
Dynamic cast that uses string-comparison in order to be safe even after an object crosses a shared li...
Definition: RefCount.h:224
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Definition: MACField.h:95
static const int k_versionNumber
Definition: MACFieldIO.h:147
boost::intrusive_ptr< MACField > Ptr
Definition: MACField.h:101
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static const std::string k_versionAttrName
Definition: MACFieldIO.h:148

◆ className()

virtual std::string MACFieldIO::className ( ) const
inlinevirtual

Returns the class name.

Implements FieldIO.

Definition at line 125 of file MACFieldIO.h.

References readData(), writeData(), and writeInternal().

126  { return "MACField"; }

◆ writeInternal()

template<class Data_T >
bool MACFieldIO::writeInternal ( hid_t  layerGroup,
typename MACField< Data_T >::Ptr  field 
)
private

This call writes all the attributes and sets up the data space.

Todo:
Tune the chunk size of the gzip call

Definition at line 169 of file MACFieldIO.h.

References FieldTraits< Data_T >::dataDims(), FieldRes::dataWindow(), FieldRes::extents(), MACField< Data_T >::getComponentSize(), DataTypeTraits< T >::h5bits(), k_bitsPerComponentStr, k_componentsStr, k_dataWindowStr, k_extentsStr, MACCompU, MACCompV, MACCompW, and Hdf5Util::writeAttribute().

Referenced by className().

171 {
172  using namespace Exc;
173  using namespace Hdf5Util;
174 
175  int components = FieldTraits<Data_T>::dataDims();
176  V3i compSize = field->getComponentSize();
177  int size[3];
178  size[0] = compSize.x;
179  size[1] = compSize.y;
180  size[2] = compSize.z;
181 
182  Box3i ext(field->extents()), dw(field->dataWindow());
183 
184  // Add extents attribute ---
185 
186  int extents[6] =
187  { ext.min.x, ext.min.y, ext.min.z, ext.max.x, ext.max.y, ext.max.z };
188 
189  if (!writeAttribute(layerGroup, k_extentsStr, 6, extents[0]))
190  throw WriteAttributeException("Couldn't write attribute " + k_extentsStr);
191 
192  // Add data window attribute ---
193 
194  int dataWindow[6] =
195  { dw.min.x, dw.min.y, dw.min.z, dw.max.x, dw.max.y, dw.max.z };
196 
197  if (!writeAttribute(layerGroup, k_dataWindowStr, 6, dataWindow[0]))
198  throw WriteAttributeException("Couldn't write attribute " + k_dataWindowStr);
199 
200  // Add components attribute ---
201 
202  if (!writeAttribute(layerGroup, k_componentsStr, 1, components))
203  throw WriteAttributeException("Couldn't write attribute " + k_componentsStr);
204 
205  // Add the bits per component attribute ---
206 
207  int bits = DataTypeTraits<Data_T>::h5bits();
208  if (!writeAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) {
209  throw WriteAttributeException("Couldn't write attribute " + k_bitsPerComponentStr);
210  return false;
211  }
212 
213  // Add data to file ---
214  if (!writeData<Data_T>(layerGroup, field, MACCompU)) {
215  throw WriteMACFieldDataException("Error writing u_data");
216  return false;
217  }
218  if (!writeData<Data_T>(layerGroup, field, MACCompV)) {
219  throw WriteMACFieldDataException("Error writing v_data");
220  return false;
221  }
222  if (!writeData<Data_T>(layerGroup, field, MACCompW)) {
223  throw WriteMACFieldDataException("Error writing w_data");
224  return false;
225  }
226 
227  return true;
228 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
static int dataDims()
Dimensions of the given data type. i.e. 3 for V3f, 1 for float.
Namespace for Exception objects.
Definition: Exception.h:57
static const std::string k_componentsStr
Definition: MACFieldIO.h:151
Imath::V3i V3i
Definition: SpiMathLib.h:71
static const std::string k_extentsStr
Definition: MACFieldIO.h:149
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static int h5bits()
static const std::string k_dataWindowStr
Definition: MACFieldIO.h:150
static const std::string k_bitsPerComponentStr
Definition: MACFieldIO.h:152

◆ writeData()

template<class Data_T >
bool MACFieldIO::writeData ( hid_t  layerGroup,
typename MACField< Data_T >::Ptr  field,
MACComponent  comp 
)
private

This call writes out the u,v,w data.

Definition at line 233 of file MACFieldIO.h.

References MACField< Data_T >::cbegin_comp(), Hdf5Util::checkHdf5Gzip(), MACField< Data_T >::getComponentSize(), Hdf5Util::H5Base::id(), k_uDataStr, k_vDataStr, k_wDataStr, MACCompU, MACCompV, and MACCompW.

Referenced by className().

236 {
237  using namespace Exc;
238  using namespace Hdf5Util;
239 
240  const V3i &compSize = field->getComponentSize();
241 
242  hsize_t totalSize[1];
243  std::string compStr;
244 
245  switch (comp) {
246  case MACCompU:
247  totalSize[0] = compSize.x;
248  compStr = k_uDataStr;
249  break;
250  case MACCompV:
251  totalSize[0] = compSize.y;
252  compStr = k_vDataStr;
253  break;
254  case MACCompW:
255  totalSize[0] = compSize.z;
256  compStr = k_wDataStr;
257  break;
258  default:
259  break;
260  }
261 
262  // Make sure chunk size isn't too big.
263  hsize_t preferredChunkSize = 4096 * 16;
264  const hsize_t chunkSize = std::min(preferredChunkSize, totalSize[0] / 2);
265 
266  H5ScopedScreate dataSpace(H5S_SIMPLE);
267 
268  if (dataSpace.id() < 0)
269  throw CreateDataSpaceException("Couldn't create data space in "
270  "MACFieldIO::writeData");
271 
272  // Create a "simple" data structure ---
273 
274  H5Sset_extent_simple(dataSpace.id(), 1, totalSize, NULL);
275 
276  // Set up gzip property list
277  bool gzipAvailable = checkHdf5Gzip();
278  hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
279  if (gzipAvailable) {
280  herr_t status = H5Pset_deflate(dcpl, 9);
281  if (status < 0) {
282  return false;
283  }
284  status = H5Pset_chunk(dcpl, 1, &chunkSize);
285  if (status < 0) {
286  return false;
287  }
288  }
289 
290  H5ScopedDcreate dataSet(layerGroup, compStr,
292  dataSpace.id(),
293  H5P_DEFAULT, dcpl, H5P_DEFAULT);
294 
295  if (dataSet.id() < 0)
296  throw CreateDataSetException("Couldn't create data set in "
297  "MACFieldIO::writeData");
298 
299  hid_t err = H5Dwrite(dataSet,
301  H5S_ALL, H5S_ALL,
302  H5P_DEFAULT, &(*field->cbegin_comp(comp)));
303  if (err < 0)
304  throw Exc::WriteLayerException("Error writing layer in "
305  "MACFieldIO::writeData");
306 
307 
308  return true;
309 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
static const std::string k_vDataStr
Definition: MACFieldIO.h:154
Namespace for Exception objects.
Definition: Exception.h:57
static const std::string k_uDataStr
Definition: MACFieldIO.h:153
Imath::V3i V3i
Definition: SpiMathLib.h:71
static const std::string k_wDataStr
Definition: MACFieldIO.h:155
bool checkHdf5Gzip()
Checks whether gzip is available in the current hdf5 library.
Definition: Hdf5Util.cpp:680
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:213
Scoped object - creates a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:240

◆ readData()

template<class Data_T >
bool MACFieldIO::readData ( hid_t  location,
typename MACField< Data_T >::Ptr  result 
)
private

Reads the data that is dependent on the data type on disk.

Definition at line 314 of file MACFieldIO.h.

References MACField< Data_T >::begin_comp(), FIELD3D_NAMESPACE_HEADER_CLOSE, Hdf5Util::H5Base::id(), k_uDataStr, k_vDataStr, k_wDataStr, MACCompU, MACCompV, MACCompW, and DataTypeTraits< T >::name().

Referenced by className().

316 {
317  using namespace std;
318  using namespace Exc;
319  using namespace Hdf5Util;
320 
321  hsize_t dims[1];
322 
323  // read u_data
324  {
325 
326  H5ScopedDopen dataSet(layerGroup, k_uDataStr, H5P_DEFAULT);
327  if (dataSet.id() < 0)
328  throw OpenDataSetException("Couldn't open data set: " + k_uDataStr);
329 
330  H5ScopedDget_space dataSpace(dataSet.id());
331  H5ScopedDget_type dataType(dataSet.id());
332  H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
333 
334  if (dataSpace.id() < 0)
335  throw GetDataSpaceException("Couldn't get data space");
336 
337  if (dataType.id() < 0)
338  throw GetDataTypeException("Couldn't get data type");
339 
340  if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
341  H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompU))) < 0)
342  {
343  std::string typeName = "MACField<" +
345  throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
346  }
347 
348  }
349 
350  // read v_data
351  {
352 
353  H5ScopedDopen dataSet(layerGroup, k_vDataStr, H5P_DEFAULT);
354  if (dataSet.id() < 0)
355  throw OpenDataSetException("Couldn't open data set: " + k_vDataStr);
356 
357  H5ScopedDget_space dataSpace(dataSet.id());
358  H5ScopedDget_type dataType(dataSet.id());
359  H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
360 
361  if (dataSpace.id() < 0)
362  throw GetDataSpaceException("Couldn't get data space");
363 
364  if (dataType.id() < 0)
365  throw GetDataTypeException("Couldn't get data type");
366 
367 
368  if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
369  H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompV))) < 0)
370  {
371  std::string typeName = "MACField<" +
373  throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
374  }
375 
376  }
377 
378  // read w_data
379  {
380 
381  H5ScopedDopen dataSet(layerGroup, k_wDataStr, H5P_DEFAULT);
382  if (dataSet.id() < 0)
383  throw OpenDataSetException("Couldn't open data set: " + k_wDataStr);
384 
385  H5ScopedDget_space dataSpace(dataSet.id());
386  H5ScopedDget_type dataType(dataSet.id());
387  H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
388 
389  if (dataSpace.id() < 0)
390  throw GetDataSpaceException("Couldn't get data space");
391 
392  if (dataType.id() < 0)
393  throw GetDataTypeException("Couldn't get data type");
394 
395 
396  if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
397  H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin_comp(MACCompW))) < 0)
398  {
399  std::string typeName = "MACField<" +
401  throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
402  }
403 
404  }
405 
406  return true;
407 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
static const std::string k_vDataStr
Definition: MACFieldIO.h:154
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:381
Namespace for Exception objects.
Definition: Exception.h:57
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:325
static std::string name()
Definition: Traits.h:101
static const std::string k_uDataStr
Definition: MACFieldIO.h:153
static const std::string k_wDataStr
Definition: MACFieldIO.h:155
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:353

Member Data Documentation

◆ DEFINE_FIELD_RTTI_CONCRETE_CLASS

MACFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 90 of file MACFieldIO.h.

◆ k_versionNumber

const int MACFieldIO::k_versionNumber
staticprivate

Definition at line 147 of file MACFieldIO.h.

◆ k_versionAttrName

const std::string MACFieldIO::k_versionAttrName
staticprivate

Definition at line 148 of file MACFieldIO.h.

◆ k_extentsStr

const std::string MACFieldIO::k_extentsStr
staticprivate

Definition at line 149 of file MACFieldIO.h.

Referenced by writeInternal().

◆ k_dataWindowStr

const std::string MACFieldIO::k_dataWindowStr
staticprivate

Definition at line 150 of file MACFieldIO.h.

Referenced by writeInternal().

◆ k_componentsStr

const std::string MACFieldIO::k_componentsStr
staticprivate

Definition at line 151 of file MACFieldIO.h.

Referenced by writeInternal().

◆ k_bitsPerComponentStr

const std::string MACFieldIO::k_bitsPerComponentStr
staticprivate

Definition at line 152 of file MACFieldIO.h.

Referenced by writeInternal().

◆ k_uDataStr

const std::string MACFieldIO::k_uDataStr
staticprivate

Definition at line 153 of file MACFieldIO.h.

Referenced by readData(), and writeData().

◆ k_vDataStr

const std::string MACFieldIO::k_vDataStr
staticprivate

Definition at line 154 of file MACFieldIO.h.

Referenced by readData(), and writeData().

◆ k_wDataStr

const std::string MACFieldIO::k_wDataStr
staticprivate

Definition at line 155 of file MACFieldIO.h.

Referenced by readData(), and writeData().


The documentation for this class was generated from the following files: