reader_writer_test.cc 5.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <cstring>
#include <iostream>
10
#include <set>
11
12
13
14
15
16
#include <vector>

#include <dune/common/parallel/mpihelper.hh> // An initializer of MPI
#include <dune/common/filledarray.hh>
#include <dune/common/test/testsuite.hh>

Praetorius, Simon's avatar
Praetorius, Simon committed
17
#if HAVE_DUNE_UGGRID
18
#include <dune/grid/uggrid.hh>
19
20
#endif

21
22
23
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/utility/structuredgridfactory.hh>

24
25
26
27
#if HAVE_DUNE_ALUGRID
#include <dune/alugrid/grid.hh>
#endif

28
29
#include <dune/vtk/vtkreader.hh>
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
30
#include <dune/vtk/gridcreators/continuousgridcreator.hh>
31
32
33
34
35
36
37
38

using namespace Dune;

// see https://stackoverflow.com/questions/6163611/compare-two-files
bool compare_files (std::string const& fn1, std::string const& fn2)
{
  std::ifstream in1(fn1, std::ios::binary);
  std::ifstream in2(fn2, std::ios::binary);
39
40
41
42
  if (!in1 || !in2) {
    std::cout << "can not find file " << fn1 << " or file " << fn2 << "\n";
    return false;
  }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

  std::ifstream::pos_type size1 = in1.seekg(0, std::ifstream::end).tellg();
  in1.seekg(0, std::ifstream::beg);

  std::ifstream::pos_type size2 = in2.seekg(0, std::ifstream::end).tellg();
  in2.seekg(0, std::ifstream::beg);

  if (size1 != size2)
    return false;

  static const std::size_t BLOCKSIZE = 4096;
  std::size_t remaining = size1;

  while (remaining) {
    char buffer1[BLOCKSIZE], buffer2[BLOCKSIZE];
    std::size_t size = std::min(BLOCKSIZE, remaining);

    in1.read(buffer1, size);
    in2.read(buffer2, size);

    if (0 != std::memcmp(buffer1, buffer2, size))
      return false;

    remaining -= size;
  }

  return true;
}

using TestCases = std::set<std::tuple<std::string,Vtk::FormatTypes,Vtk::DataTypes>>;
static TestCases test_cases = {
  {"ascii32", Vtk::ASCII, Vtk::FLOAT32},
  {"bin32", Vtk::BINARY, Vtk::FLOAT32},
  {"zlib32", Vtk::COMPRESSED, Vtk::FLOAT32},
  {"ascii64", Vtk::ASCII, Vtk::FLOAT64},
  {"bin64", Vtk::BINARY, Vtk::FLOAT64},
  {"zlib64", Vtk::COMPRESSED, Vtk::FLOAT64},
};

82
template <class Test>
83
void compare (Test& test, Vtk::Path const& dir, Vtk::Path const& name)
84
85
86
87
88
{
  test.check(compare_files(dir.string() + '/' + name.string() + ".vtu",
                           dir.string() + '/' + name.string() + "_2.vtu"));
}

89
90
91
92
template <class GridView>
void writer_test (GridView const& gridView)
{
  for (auto const& test_case : test_cases) {
93
    VtkUnstructuredGridWriter<GridView> vtkWriter(gridView, std::get<1>(test_case), std::get<2>(test_case));
94
    vtkWriter.write("reader_writer_test_" + std::get<0>(test_case) + ".vtu");
95
96
97
98
  }
}

template <class Grid, class Test>
Praetorius, Simon's avatar
Praetorius, Simon committed
99
void reader_test (MPIHelper& mpi, Test& test)
100
{
101
  std::string ext = ".vtu";
102
  for (auto const& test_case : test_cases) {
103
104
105
106
107
    std::vector<std::string> pieces1, pieces2;

    {
      GridFactory<Grid> factory;
      VtkReader<Grid> reader{factory};
108
      reader.read("reader_writer_test_" + std::get<0>(test_case) + ext);
109

110
111
      std::unique_ptr<Grid> grid{factory.createGrid()};
      pieces1 = reader.pieces();
112
113
114
115
116
117
118
119
120

      VtkUnstructuredGridWriter<typename Grid::LeafGridView> vtkWriter(grid->leafGridView(),
        std::get<1>(test_case), std::get<2>(test_case));
      vtkWriter.write("reader_writer_test_" + std::get<0>(test_case) + "_2.vtu");
    }

    {
      GridFactory<Grid> factory2;
      VtkReader<Grid> reader2{factory2};
121
      reader2.read("reader_writer_test_" + std::get<0>(test_case) + "_2" + ext, false);
122
      pieces2 = reader2.pieces();
123
    }
124
125
126
127

    test.check(pieces1.size() == pieces2.size(), "pieces1.size == pieces2.size");
    for (std::size_t i = 0; i < pieces1.size(); ++i)
      test.check(compare_files(pieces1[i], pieces2[i]));
128
129
130
131
132
133
134
135
136
  }
}


template <int I>
using int_ = std::integral_constant<int,I>;

int main (int argc, char** argv)
{
137
138
139
140
141
  auto& mpi = Dune::MPIHelper::instance(argc, argv);
  if (mpi.size() > 1) {
    std::cout << "Parallel VtkReader not yet supported\n";
    return 0;
  }
142
143
144

  TestSuite test{};

Praetorius, Simon's avatar
Praetorius, Simon committed
145
#if HAVE_DUNE_UGGRID
146
  // Test VtkWriter for UGGrid
Praetorius, Simon's avatar
Praetorius, Simon committed
147
  Hybrid::forEach(std::make_tuple(int_<2>{}, int_<3>{}), [&test,&mpi](auto dim)
148
149
150
151
152
153
154
  {
    using GridType = UGGrid<dim.value>;
    {
      FieldVector<double,dim.value> lowerLeft; lowerLeft = 0.0;
      FieldVector<double,dim.value> upperRight; upperRight = 1.0;
      auto numElements = filledArray<dim.value,unsigned int>(4);
      auto gridPtr = StructuredGridFactory<GridType>::createSimplexGrid(lowerLeft, upperRight, numElements);
155
156
157
158
159
      gridPtr->loadBalance();

      writer_test(gridPtr->leafGridView());
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
160
    reader_test<GridType>(mpi,test);
161
162
163
  });
#endif

164
#if DUNE_VERSION_LT(DUNE_GRID,2,7) && HAVE_DUNE_ALUGRID
165
  // Test VtkWriter for ALUGrid. Currently the 2.7 branch is not working.
Praetorius, Simon's avatar
Praetorius, Simon committed
166
  Hybrid::forEach(std::make_tuple(int_<2>{}, int_<3>{}), [&test,&mpi](auto dim)
167
168
169
170
171
172
173
174
  {
    using GridType = Dune::ALUGrid<dim.value, dim.value, Dune::simplex, Dune::conforming>;
    {
      FieldVector<double,dim.value> lowerLeft; lowerLeft = 0.0;
      FieldVector<double,dim.value> upperRight; upperRight = 1.0;
      auto numElements = filledArray<dim.value,unsigned int>(4);
      auto gridPtr = StructuredGridFactory<GridType>::createSimplexGrid(lowerLeft, upperRight, numElements);
      gridPtr->loadBalance();
175
176
177
178

      writer_test(gridPtr->leafGridView());
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
179
    reader_test<GridType>(mpi,test);
180
181
182
183
184
185
186
187
  });
#endif

  // Test VtkWriter for YaspGrid
  Hybrid::forEach(std::make_tuple(int_<1>{}, int_<2>{}, int_<3>{}), [](auto dim)
  {
    using GridType = YaspGrid<dim.value>;
    FieldVector<double,dim.value> upperRight; upperRight = 1.0;
188
    auto numElements = filledArray<dim.value,int>(8);
189
190
191
192
193
194
    GridType grid(upperRight, numElements);
    writer_test(grid.leafGridView());
  });

  return test.exit();
}