mixed_element_test.cc 3.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// -*- 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>
#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
16
#if HAVE_DUNE_UGGRID
17
#include <dune/grid/uggrid.hh>
18
19
#endif

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

23
24
#include <dune/vtk/vtkreader.hh>
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

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);

  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 = {
64
65
  {"ascii32", Vtk::FormatTypes::ASCII, Vtk::DataTypes::FLOAT32},
  {"bin32", Vtk::FormatTypes::BINARY, Vtk::DataTypes::FLOAT32},
Praetorius, Simon's avatar
Praetorius, Simon committed
66
  {"zlib32", Vtk::FormatTypes::COMPRESSED, Vtk::DataTypes::FLOAT32},
67
68
  {"ascii64", Vtk::FormatTypes::ASCII, Vtk::DataTypes::FLOAT64},
  {"bin64", Vtk::FormatTypes::BINARY, Vtk::DataTypes::FLOAT64},
Praetorius, Simon's avatar
Praetorius, Simon committed
69
  {"zlib64", Vtk::FormatTypes::COMPRESSED, Vtk::DataTypes::FLOAT64},
70
71
72
73
74
75
};

template <class GridView>
void writer_test (GridView const& gridView)
{
  for (auto const& test_case : test_cases) {
76
    VtkUnstructuredGridWriter<GridView> vtkWriter(gridView, std::get<1>(test_case), std::get<2>(test_case));
77
    vtkWriter.write("mixed_element_test_" + std::get<0>(test_case) + ".vtu");
78
79
80
81
82
83
84
  }
}

template <class Grid, class Test>
void reader_test (Test& test)
{
  for (auto const& test_case : test_cases) {
85
    auto grid = VtkReader<Grid>::createGridFromFile("mixed_element_test_" + std::get<0>(test_case) + ".vtu");
86
    VtkUnstructuredGridWriter<typename Grid::LeafGridView> vtkWriter(grid->leafGridView(),
87
      std::get<1>(test_case), std::get<2>(test_case));
88
89
90
    vtkWriter.write("mixed_element_test_" + std::get<0>(test_case) + "_2.vtu");
    test.check(compare_files("mixed_element_test_" + std::get<0>(test_case) + ".vtu",
                             "mixed_element_test_" + std::get<0>(test_case) + "_2.vtu"), std::get<0>(test_case));
91
92
93
94
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
  }
}


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

int main (int argc, char** argv)
{
  Dune::MPIHelper::instance(argc, argv);

  TestSuite test{};

#ifdef HAVE_UG
  // Test VtkWriter for UGGrid
  using GridType = UGGrid<2>;
  GridFactory<GridType> factory;

  using X = FieldVector<double,2>;
  using E = std::vector<unsigned int>;
  factory.insertVertex(X{0.0, 0.0}); // 0
  factory.insertVertex(X{1.0, 0.0}); // 1
  factory.insertVertex(X{1.0, 1.0}); // 2
  factory.insertVertex(X{0.0, 1.0}); // 3
  factory.insertVertex(X{1.5, 0.5}); // 4

  factory.insertElement(GeometryTypes::quadrilateral, E{0,1,3,2}); // quadrilateral
  factory.insertElement(GeometryTypes::triangle, E{1,4,2}); // triangle

  {
    std::unique_ptr<GridType> gridPtr{ factory.createGrid() };
    writer_test(gridPtr->leafGridView());
  }
  reader_test<GridType>(test);
#endif

  return test.exit();
}