DiscreteFunctionTest.cpp 2.42 KB
Newer Older
1
2
3
4
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:

#include <iostream>
5
6
#include <type_traits>

7
8
#include <amdis/AMDiS.hpp>
#include <amdis/ProblemStat.hpp>
9
10
#include <amdis/gridfunctions/DiscreteFunction.hpp>
#include <amdis/gridfunctions/DOFVectorView.hpp>
11
12
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

#include "Tests.hpp"

using namespace AMDiS;

using ElliptParam   = YaspGridBasis<2, 2>;
using ElliptProblem = ProblemStat<ElliptParam>;

template <class GB, class T>
bool comp(DOFVector<GB,T> const& U, DOFVector<GB,T> const& V)
{
  if (U.size() != V.size())
    return false;

  using Int = typename DOFVector<GB,T>::size_type;
  for (Int i = 0; i < U.size(); ++i) {
    if (U[i] != V[i])
      return false;
  }
  return true;
}

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

  using namespace Dune::Indices;

  ElliptProblem prob("ellipt");
  prob.initialize(INIT_ALL);

  // create 3 copies of the solution vector
  auto U0 = prob.getSolutionVector();
  auto U1 = prob.getSolutionVector();
  auto U2 = prob.getSolutionVector();

  auto u0 = makeDOFVectorView(U0);
  auto u1 = makeDOFVectorView(U1);
  auto u2 = makeDOFVectorView(U2);

  auto expr = [](auto const& x) { return x[0] + x[1]; };
  u0.interpolate_noalias(expr);
  u1.interpolate(expr);
  u2 << expr;

  AMDIS_TEST( comp(U0, U1) );
  AMDIS_TEST( comp(U0, U2) );

  auto expr2 = [](auto const& x) { return 2*x[0] - 3*x[1]; };

  u0.interpolate_noalias(u2 + expr2);
  u1.interpolate(u1 + expr2);
  u2 += expr2;

  AMDIS_TEST( comp(U0, U1) );
  AMDIS_TEST( comp(U0, U2) );

  auto expr3 = [](auto const& x) { return -0.5*x[0] - 2*x[1]; };

  u0.interpolate_noalias(u2 - expr3);
  u1.interpolate(u1 - expr3);
  u2 -= expr3;

  AMDIS_TEST( comp(U0, U1) );
  AMDIS_TEST( comp(U0, U2) );

  auto du0 = derivative(u0);
  auto localFct = localFunction(u0);
  auto dlocalFct = derivative(localFct);
  for (auto const& e : elements(prob.gridView()))
  {
    localFct.bind(e);
    auto geo = e.geometry();
    auto local = referenceElement(geo).position(0,0);
    auto y0 = localFct(local);
    auto y1 = u0(geo.global(local));

    AMDIS_TEST(std::abs(y0 - y1) < 1.e-10);
    localFct.unbind();

    dlocalFct.bind(e);
    auto g0 = dlocalFct(local);
    auto g1 = du0(geo.global(local));

    AMDIS_TEST(two_norm(g0 - g1) < 1.e-10);
    dlocalFct.unbind();
  }

99
  auto V0 = makeDOFVector<float>(prob.globalBasis());
100
101
102
  auto v0 = makeDOFVectorView(V0);
  v0 << expr;

103
104
105
  AMDiS::finalize();
  return 0;
}