TestXdmfHDF5Writer.cpp 3.85 KB
Newer Older
1 2
#include "XdmfArray.hpp"
#include "XdmfHDF5Controller.hpp"
3
#include "XdmfHDF5Writer.hpp"
4
#include <iostream>
5
#include <assert.h>
6

7
int main(int, char **)
8
{
9
  shared_ptr<XdmfArray> array = XdmfArray::New();
10 11 12
  array->pushBack(0);
  array->pushBack(1);
  array->pushBack(2);
13

14 15 16
  //
  // Default operation - Always write to separate datasets.
  //
17

18 19
  std::cout << array->getHeavyDataController() << " ?= " << "NULL"
            << std::endl; 
20

21
  assert(array->getHeavyDataController() == NULL);
22
  shared_ptr<XdmfHDF5Writer> writer = XdmfHDF5Writer::New("hdf5WriterTest.h5");
23
  array->accept(writer);
24 25
  shared_ptr<XdmfHDF5Controller> firstController =
    shared_dynamic_cast<XdmfHDF5Controller>(array->getHeavyDataController());
26 27
  std::string firstPath = firstController->getDataSetPath();
  array->accept(writer);
28 29
  shared_ptr<XdmfHDF5Controller> secondController =
    shared_dynamic_cast<XdmfHDF5Controller>(array->getHeavyDataController());
30
  std::string secondPath = secondController->getDataSetPath();
31 32 33

  std::cout << firstPath << " ?!= " << secondPath << std::endl;

34
  assert(firstPath.compare(secondPath) != 0);
35

36 37 38 39 40
  //
  // Overwrite operation - Always write to the same datasets.
  //
  writer->setMode(XdmfHDF5Writer::Overwrite);
  array->accept(writer);
41 42
  shared_ptr<XdmfHDF5Controller> thirdController =
    shared_dynamic_cast<XdmfHDF5Controller>(array->getHeavyDataController());
43
  std::string thirdPath = thirdController->getDataSetPath();
44 45 46

  std::cout << secondPath << " ?= " << thirdPath << std::endl;

47
  assert(secondPath.compare(thirdPath) == 0);
48

49 50
  array->pushBack(3);
  array->accept(writer);
51 52
  shared_ptr<XdmfHDF5Controller> fourthController =
    shared_dynamic_cast<XdmfHDF5Controller>(array->getHeavyDataController());
53
  std::string fourthPath = fourthController->getDataSetPath();
54 55 56

  std::cout << thirdPath << " ?= " << fourthPath << std::endl;

57
  assert(thirdPath.compare(fourthPath) == 0);
58

59 60 61
  array->erase(0);
  array->erase(0);
  array->accept(writer);
62 63
  shared_ptr<XdmfHDF5Controller> fifthController = 
    shared_dynamic_cast<XdmfHDF5Controller>(array->getHeavyDataController());
64
  std::string fifthPath = fifthController->getDataSetPath();
65 66 67

  std::cout << fourthPath << " ?= " << fifthPath << std::endl;

68
  assert(fourthPath.compare(fifthPath) == 0);
69

70 71 72 73 74 75 76 77 78
  //
  // Append operation - Append data to same dataset.
  //
  writer->setMode(XdmfHDF5Writer::Append);
  // Append 4 times
  array->accept(writer);
  array->accept(writer);
  array->accept(writer);
  array->accept(writer);
79 80 81

  std::cout << array->getSize() << " ?= " << 2 << std::endl;

82 83
  assert(array->getSize() == 2);
  array->read();
84 85 86

  std::cout << array->getSize() << " ?= " << 10 << std::endl;

87
  assert(array->getSize() == 10);
88
  for(int i=0; i<5; ++i) {
89 90 91
    std::cout << array->getValue<int>(i*2) << " ?= " << 2 << std::endl;
    std::cout << array->getValue<int>(i*2 + 1) << " ?= " << 3 << std::endl;

92 93 94
    assert(array->getValue<int>(i*2) == 2);
    assert(array->getValue<int>(i*2 + 1) == 3);
  }
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  //
  // Using Deflate Compression
  //
  shared_ptr<XdmfHDF5Writer> compressedWriter = XdmfHDF5Writer::New("hdf5CompressionTestDeflate.h5");
  compressedWriter->setMode(XdmfHDF5Writer::Default);
  compressedWriter->setUseDeflate(true);
  compressedWriter->setDeflateFactor(6);
  shared_ptr<XdmfArray> compressedArray = XdmfArray::New();
  for (unsigned int i = 0; i < 20000; ++i)
  {
    compressedArray->pushBack(i);
  }

  compressedArray->accept(compressedWriter);

  compressedArray->release();

  compressedArray->read();

  for (unsigned int i = 0; i < 20000; ++i)
  {
    assert(compressedArray->getValue<unsigned int>(i) == i);
  }

  // For comparison
  shared_ptr<XdmfHDF5Writer> compareWriter = XdmfHDF5Writer::New("hdf5CompressionTestComparison.h5");
  shared_ptr<XdmfArray> compareArray = XdmfArray::New();
  for (unsigned int i = 0; i < 20000; ++i)
  {
    compareArray->pushBack(i);
  }

  compareArray->accept(compareWriter);

130
  return 0;
131
}