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
 
typedef boost::weak_ptr< RefBaseWeakPtr
 

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...
 
WeakPtr weakPtr () const
 
 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 FieldIO::Ptr create ()
 
static const char * staticClassType ()
 
- Static Public Member Functions inherited from FieldIO
static const char * staticClassType ()
 
- Static Public Member Functions inherited from RefBase
static const char * staticClassType ()
 

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

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

Definition at line 85 of file MACFieldIO.h.

Definition at line 89 of file MACFieldIO.h.

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 ( )
inline

Ctor.

Definition at line 100 of file MACFieldIO.h.

101  : FieldIO()
102  { }
FieldIO()
Ctor.
Definition: FieldIO.h:105
virtual MACFieldIO::~MACFieldIO ( )
inlinevirtual

Dtor.

Definition at line 105 of file MACFieldIO.h.

106  { /* Empty */ }

Member Function Documentation

static const char* MACFieldIO::staticClassType ( )
inlinestatic

Definition at line 92 of file MACFieldIO.h.

93  {
94  return "MACFieldIO";
95  }
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
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
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
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
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
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:256
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
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
static const std::string k_versionAttrName
Definition: MACFieldIO.h:148
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"; }
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(), 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 
177  Box3i ext(field->extents()), dw(field->dataWindow());
178 
179  // Add extents attribute ---
180 
181  int extents[6] =
182  { ext.min.x, ext.min.y, ext.min.z, ext.max.x, ext.max.y, ext.max.z };
183 
184  if (!writeAttribute(layerGroup, k_extentsStr, 6, extents[0]))
185  throw WriteAttributeException("Couldn't write attribute " + k_extentsStr);
186 
187  // Add data window attribute ---
188 
189  int dataWindow[6] =
190  { dw.min.x, dw.min.y, dw.min.z, dw.max.x, dw.max.y, dw.max.z };
191 
192  if (!writeAttribute(layerGroup, k_dataWindowStr, 6, dataWindow[0]))
193  throw WriteAttributeException("Couldn't write attribute " + k_dataWindowStr);
194 
195  // Add components attribute ---
196 
197  if (!writeAttribute(layerGroup, k_componentsStr, 1, components))
198  throw WriteAttributeException("Couldn't write attribute " + k_componentsStr);
199 
200  // Add the bits per component attribute ---
201 
202  int bits = DataTypeTraits<Data_T>::h5bits();
203  if (!writeAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) {
204  throw WriteAttributeException("Couldn't write attribute " + k_bitsPerComponentStr);
205  return false;
206  }
207 
208  // Add data to file ---
209  if (!writeData<Data_T>(layerGroup, field, MACCompU)) {
210  throw WriteMACFieldDataException("Error writing u_data");
211  return false;
212  }
213  if (!writeData<Data_T>(layerGroup, field, MACCompV)) {
214  throw WriteMACFieldDataException("Error writing v_data");
215  return false;
216  }
217  if (!writeData<Data_T>(layerGroup, field, MACCompW)) {
218  throw WriteMACFieldDataException("Error writing w_data");
219  return false;
220  }
221 
222  return true;
223 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
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
static const std::string k_extentsStr
Definition: MACFieldIO.h:149
static int h5bits()
static const std::string k_dataWindowStr
Definition: MACFieldIO.h:150
static const std::string k_bitsPerComponentStr
Definition: MACFieldIO.h:152
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 228 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().

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

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

Member Data Documentation

MACFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 90 of file MACFieldIO.h.

const int MACFieldIO::k_versionNumber
staticprivate

Definition at line 147 of file MACFieldIO.h.

const std::string MACFieldIO::k_versionAttrName
staticprivate

Definition at line 148 of file MACFieldIO.h.

const std::string MACFieldIO::k_extentsStr
staticprivate

Definition at line 149 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_dataWindowStr
staticprivate

Definition at line 150 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_componentsStr
staticprivate

Definition at line 151 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_bitsPerComponentStr
staticprivate

Definition at line 152 of file MACFieldIO.h.

Referenced by writeInternal().

const std::string MACFieldIO::k_uDataStr
staticprivate

Definition at line 153 of file MACFieldIO.h.

Referenced by readData(), and writeData().

const std::string MACFieldIO::k_vDataStr
staticprivate

Definition at line 154 of file MACFieldIO.h.

Referenced by readData(), and writeData().

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: