BackupRestoreTest.cpp 4.63 KB
Newer Older
1
#include <amdis/AMDiS.hpp>
2
#include <amdis/Integrate.hpp>
3
4
#include <amdis/ProblemStat.hpp>

5
6
7
#include <dune/grid/onedgrid.hh>
#include <dune/grid/yaspgrid.hh>

8
9
10
11
12
13
14
15
16
17
#if HAVE_DUNE_SPGRID
#include <dune/grid/spgrid.hh>
#endif
#if HAVE_DUNE_ALUGRID
#include <dune/alugrid/grid.hh>
#endif
#if HAVE_DUNE_FOAMGRID
#include <dune/foamgrid/foamgrid.hh>
#endif

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Tests.hpp"

using namespace AMDiS;

template <class Grid, class Factory>
void test(Factory factory)
{
  using Param  = TaylorHoodBasis<Grid>;
  using Problem = ProblemStat<Param>;

  std::size_t num_elements = 0;
  std::size_t num_vertices = 0;

  // backup
  {
    std::unique_ptr<Grid> grid(factory());
    Problem prob("test", *grid);
    prob.initialize(INIT_ALL);
    prob.globalRefine(2);

38
39
40
    prob.solution(Dune::Indices::_0) << [](auto const& x) { return x; };
    prob.solution(Dune::Indices::_1) << [](auto const& x) { return two_norm(x); };

41
42
43
44
45
46
47
48
49
50
51
52
53
54
    num_elements = prob.grid()->size(0);
    num_vertices = prob.grid()->size(Grid::dimension);

    AdaptInfo adaptInfo("adapt");
    prob.backup(adaptInfo);
  }

  // restore
  {
    Problem prob("test");
    prob.restore(INIT_ALL);

    AMDIS_TEST_EQ(num_elements, prob.grid()->size(0));
    AMDIS_TEST_EQ(num_vertices, prob.grid()->size(Grid::dimension));
55

56
57
58
    DOFVector<typename Param::GlobalBasis> vec(*prob.globalBasis());
    makeDOFVectorView(vec, Dune::Indices::_0) << [](auto const& x) { return x; };
    makeDOFVectorView(vec, Dune::Indices::_1) << [](auto const& x) { return two_norm(x); };
59
60
61
62
63
64
65
66

    double error0 = std::sqrt(integrate(
      unary_dot(prob.solution(Dune::Indices::_0) - makeDiscreteFunction(vec, Dune::Indices::_0)), prob.gridView()));
    AMDIS_TEST(error0 < 1.e-10);

    double error1 = std::sqrt(integrate(
      pow<2>(prob.solution(Dune::Indices::_1) - makeDiscreteFunction(vec, Dune::Indices::_1)), prob.gridView()));
    AMDIS_TEST(error1 < 1.e-10);
67
68
69
70
71
72
73
  }
}

template <class Grid>
void test_cube()
{
  using Factory = Dune::StructuredGridFactory<Grid>;
74
75
76
77
  Dune::FieldVector<double,Grid::dimensionworld> left(0.0);
  Dune::FieldVector<double,Grid::dimensionworld> right(1.0);
  auto sizes = Dune::filledArray<Grid::dimension, unsigned int>(2);
  test<Grid>([&]() { return Factory::createCubeGrid(left, right, sizes); });
78
79
80
81
82
83
}

template <class Grid>
void test_simplex()
{
  using Factory = Dune::StructuredGridFactory<Grid>;
84
85
86
87
  Dune::FieldVector<double,Grid::dimensionworld> left(0.0);
  Dune::FieldVector<double,Grid::dimensionworld> right(1.0);
  auto sizes = Dune::filledArray<Grid::dimension, unsigned int>(2);
  test<Grid>([&]() { return Factory::createSimplexGrid(left, right, sizes); });
88
89
90
91
92
93
}

int main(int argc, char** argv)
{
  AMDiS::init(argc, argv);

94
95
96
97
98
  std::string filename = "test";
  Parameters::set("test->backup->grid", filename + ".grid");
  Parameters::set("test->restore->grid", filename + ".grid");
  Parameters::set("test->backup->solution", filename + ".solution");
  Parameters::set("test->restore->solution", filename + ".solution");
99

100
#if GRID_ID == 0
101
  test_cube<Dune::YaspGrid<2>>();
102
  test_cube<Dune::YaspGrid<3>>();
103
104
#elif GRID_ID == 1 && HAVE_DUNE_UGGRID
  test_cube<Dune::UGGrid<2>>();
105
  test_cube<Dune::UGGrid<3>>();
106
107
#elif GRID_ID == 2 && HAVE_DUNE_ALUGRID
  test_cube<Dune::ALUGrid<2,2,Dune::cube,Dune::nonconforming>>();
108
109
  test_cube<Dune::ALUGrid<3,3,Dune::cube,Dune::nonconforming>>();
  //test_cube<Dune::ALUGrid<2,3,Dune::cube,Dune::nonconforming>>();
110
111
112
113
#elif GRID_ID == 3 && HAVE_DUNE_SPGRID
  test<Dune::SPGrid<double,2>>([]() {
    return std::unique_ptr<Dune::SPGrid<double,2>>(
      new Dune::SPGrid<double,2>(Dune::SPDomain<double, 2>({0.0,0.0}, {1.0,1.0}), Dune::SPMultiIndex<2>({2,2}))); });
114
115
116
  test<Dune::SPGrid<double,3>>([]() {
    return std::unique_ptr<Dune::SPGrid<double,3>>(
      new Dune::SPGrid<double,3>(Dune::SPDomain<double, 3>({0.0,0.0,0.0}, {1.0,1.0,1.0}), Dune::SPMultiIndex<3>({2,2,2}))); });
117
118
#elif GRID_ID == 4 && HAVE_DUNE_UGGRID
  test_simplex<Dune::UGGrid<2>>();
119
  test_simplex<Dune::UGGrid<3>>();
120
121
#elif GRID_ID == 5 && HAVE_DUNE_ALUGRID
  test_simplex<Dune::ALUGrid<2,2,Dune::simplex,Dune::nonconforming>>();
122
123
  test_simplex<Dune::ALUGrid<3,3,Dune::simplex,Dune::nonconforming>>();
  //test_simplex<Dune::ALUGrid<2,3,Dune::simplex,Dune::nonconforming>>();
124
125
#elif GRID_ID == 6 && HAVE_DUNE_ALUGRID
  test_simplex<Dune::ALUGrid<2,2,Dune::simplex,Dune::conforming>>();
126
127
128
129
  test_simplex<Dune::ALUGrid<3,3,Dune::simplex,Dune::conforming>>();
  //test_simplex<Dune::ALUGrid<2,3,Dune::simplex,Dune::conforming>>();
#elif GRID_ID == 7
  test_cube<Dune::OneDGrid>();
130
131
132
#endif
  // test_simplex<Dune::AlbertaGrid<2,2>>(); // Segmentation fault
  // test_simplex<Dune::FoamGrid<2,2>>(); // Segmentation fault
133
134

  AMDiS::finalize();
135
  return report_errors();
136
}