TreeDataTest.cpp 3.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <memory>
#include <functional>

#include <dune/common/filledarray.hh>
#include <dune/grid/yaspgrid.hh>

#include <dune/functions/functionspacebases/compositebasis.hh>
#include <dune/functions/functionspacebases/lagrangebasis.hh>
#include <dune/functions/functionspacebases/powerbasis.hh>

12
#include <amdis/utility/TreeData.hpp>
13
14
15
16
17
18
19
20
21
22
23
24
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

#include "Tests.hpp"

using namespace AMDiS;


template <class Node>
using NodeData = double;

template <class Tree>
bool operator==(TreeData<Tree,NodeData,false> const& t1, TreeData<Tree,NodeData,false> const& t2)
{
  AMDIS_TEST(t1.tree() == t2.tree() && t1.tree() != nullptr);

  bool same = true;
  forEachNode(*t1.tree(), [&](auto const& node, auto) {
    same = same && (t1[node] == t2[node]);
  });

  return same;
}

template <class Tree>
bool operator==(TreeData<Tree,NodeData,true> const& t1, TreeData<Tree,NodeData,true> const& t2)
{
  AMDIS_TEST(t1.tree() == t2.tree() && t1.tree() != nullptr);

  bool same = true;
  forEachLeafNode(*t1.tree(), [&](auto const& node, auto) {
    same = same && (t1[node] == t2[node]);
  });

  return same;
}

int main ()
{
  using namespace Dune;
  using namespace Dune::Functions;
  using namespace Dune::Functions::BasisBuilder;

  FieldVector<double, 2> L; L = 1.0;
  auto s = Dune::filledArray<2>(1);
  YaspGrid<2> grid(L, s);
  auto gridView = grid.leafGridView();

  auto taylorHoodBasis = makeBasis(
    gridView,
    composite(
      power<2>(lagrange<2>()),
      lagrange<1>()
    ));

  auto localView = taylorHoodBasis.localView();
  auto const& tree = localView.tree();
  using Tree = std::remove_reference_t<decltype(tree)>;

  // test treeData for all nodes
  {
    // call default-constructor
    TreeData<Tree, NodeData, false> treeData;
    treeData.init(tree);

    forEachNode(tree, [&](auto const& node, auto) {
      treeData[node] = double(node.treeIndex());
    });

    // call constructor with tree
    TreeData<Tree, NodeData, false> treeData1(tree);

    // call init on non-empty treeData
    treeData1.init(tree);

    // call copy-constructor
    TreeData<Tree, NodeData, false> treeData2(treeData);
    AMDIS_TEST(treeData == treeData2);

    // call copy-assignment operator on empty treeData
    TreeData<Tree, NodeData, false> treeData3;
    treeData3 = treeData;
    AMDIS_TEST(treeData == treeData3);

    // call copy-assignment operator on non-empty treeData
    treeData2 = treeData3;
    AMDIS_TEST(treeData3 == treeData2);

    // call move-assignment operator on non-empty treeData
    treeData = std::move(treeData2);

    // call move-constructor
    TreeData<Tree, NodeData, false> treeData4(std::move(treeData3));
  }

  // test treeData for leaf only
  {
    // call default-constructor
    TreeData<Tree, NodeData, true> treeData;
    treeData.init(tree);

    forEachLeafNode(tree, [&](auto const& node, auto) {
      treeData[node] = double(node.treeIndex());
    });

    // call constructor with tree
    TreeData<Tree, NodeData, true> treeData1(tree);

    // call init on non-empty treeData
    treeData1.init(tree);

    // call copy-constructor
    TreeData<Tree, NodeData, true> treeData2(treeData);
    AMDIS_TEST(treeData == treeData2);

    // call copy-assignment operator on empty treeData
    TreeData<Tree, NodeData, true> treeData3;
    treeData3 = treeData;
    AMDIS_TEST(treeData == treeData3);

    // call copy-assignment operator on non-empty treeData
    treeData2 = treeData3;
    AMDIS_TEST(treeData3 == treeData2);

    // call move-assignment operator on non-empty treeData
    treeData = std::move(treeData2);

    // call move-constructor
    TreeData<Tree, NodeData, true> treeData4(std::move(treeData3));
  }

  // test for operations with uninitialized tree
  {
    // call default-constructor without initialization
    TreeData<Tree, NodeData, true> treeData;

    // call copy-constructor
    TreeData<Tree, NodeData, true> treeData2(treeData);

    // call move-constructor
    TreeData<Tree, NodeData, true> treeData3(std::move(treeData));
  }

  return report_errors();
}