DUNE-FUNCTIONS (2.8)

gridfunctiontest.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_TEST_GRIDFUNCTIONTEST_HH
4#define DUNE_FUNCTIONS_GRIDFUNCTIONS_TEST_GRIDFUNCTIONTEST_HH
5
6#include <type_traits>
7
8#include <dune/geometry/quadraturerules.hh>
9
10#include <dune/functions/common/functionconcepts.hh>
11#include <dune/functions/gridfunctions/gridfunction.hh>
12#include <dune/functions/gridfunctions/gridviewfunction.hh>
13#include <dune/functions/gridfunctions/gridviewentityset.hh>
14
15
16namespace Dune {
17namespace Functions {
18namespace Test {
19
20
21
22bool checkTrue(bool value, std::string error)
23{
24 if (not(value))
25 std::cout << "TEST FAILURE:" << error << std::endl;
26 return value;
27}
28
29
30
31template<class GridView, class F>
32double integrateGridViewFunction(const GridView& gridView, const F& f, unsigned int quadOrder)
33{
34 static const int dim = GridView::dimension;
35
36 double integral = 0;
37
38 auto fLocal = localFunction(f);
39
40 // Loop over elements and integrate over the function
41 for (const auto& e : elements(gridView))
42 {
43 auto geometry = e.geometry();
44
45 fLocal.bind(e);
46
47 // A quadrature rule
48 const auto& quad = QuadratureRules<double, dim>::rule(e.type(), quadOrder);
49
50 // Loop over all quadrature points
51 for ( size_t pt=0; pt < quad.size(); pt++ ) {
52
53 // Position of the current quadrature point in the reference element
54 auto quadPos = quad[pt].position();
55
56 // The multiplicative factor in the integral transformation formula
57 auto integrationElement = geometry.integrationElement(quadPos);
58
59 integral += fLocal(quadPos) * quad[pt].weight() * integrationElement;
60 }
61 fLocal.unbind();
62 }
63 return integral;
64}
65
66
67template<class GridView, class F>
68bool checkGridViewFunction(const GridView& gridView, const F& f, double exactIntegral, unsigned int quadOrder=1)
69{
70 bool passed = true;
71 double integral;
72
74
75 using EntitySet = typename F::EntitySet;
76 using Domain = typename EntitySet::GlobalCoordinate;
77 using Range = typename std::invoke_result<F, Domain>::type;
78
79
80
81 std::cout << "Checking raw function f on grid view" << std::endl;
82 passed = checkTrue(isGridFunction<decltype(f), Range(Domain), EntitySet>(), "Function does not model GridFunction concept");
83 integral = integrateGridViewFunction(gridView, f, quadOrder);
84 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
85
86 std::cout << "Checking GridFunction<Range(Domain), EntitySet>(f) on grid view" << std::endl;
87 GridFunction<Range(Domain), EntitySet> f2 = f;
88 passed = checkTrue(isGridFunction<decltype(f2), Range(Domain), EntitySet>(), "Function does not model GridFunction concept");
89 integral = integrateGridViewFunction(gridView, f2, quadOrder);
90 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
91
92 std::cout << "Checking GridViewFunction<Range(Domain), GridView>(f) on grid view" << std::endl;
93 GridViewFunction<Range(Domain), GridView> f3 = f;
94 passed = checkTrue(isGridFunction<decltype(f3), Range(Domain), EntitySet>(), "Function does not model GridFunction concept");
95 integral = integrateGridViewFunction(gridView, f3, quadOrder);
96 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
97
98 std::cout << "Checking makeGridFunction(f) on grid view" << std::endl;
99 auto f4 = makeGridViewFunction(f, gridView);
100 passed = checkTrue(isGridFunction<decltype(f4), Range(Domain), EntitySet>(), "Function does not model GridFunction concept");
101 integral = integrateGridViewFunction(gridView, f4, quadOrder);
102 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
103
104 std::cout << "Checking GridViewFunction<Range(Domain), GridView>(makeGridFunction(f)) on grid view" << std::endl;
105 GridViewFunction<Range(Domain), GridView> f5 = f4;
106 integral = integrateGridViewFunction(gridView, f5, quadOrder);
107 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
108
109 std::cout << "Checking default constructed and assigned GridViewFunction<Range(Domain), GridView> on grid view" << std::endl;
110 GridViewFunction<Range(Domain), GridView> f6;
111 f6 = f5;
112 integral = integrateGridViewFunction(gridView, f6, quadOrder);
113 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
114
115 std::cout << "Checking reassigned GridViewFunction<Range(Domain), GridView> on grid view" << std::endl;
116 f6 = f3;
117 integral = integrateGridViewFunction(gridView, f6, quadOrder);
118 passed = checkTrue(std::abs(integral-exactIntegral) < 1e-10, "Integral is "+std::to_string(integral)+" but should be "+std::to_string(exactIntegral));
119
120 return passed;
121}
122
123
124
125
126} // namespace Test
127} // namespace Functions
128} // namespace Dune
129
130
131
132
133#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_TEST_GRIDFUNCTIONTEST_HH
static constexpr bool isGridFunction()
Check if F models the GridFunction concept with given signature and entity set.
Definition: functionconcepts.hh:268
void localFunction(DiscreteGlobalBasisFunction< TT... > &&t)=delete
Construction of local functions from a temporary DiscreteGlobalBasisFunction (forbidden)
Definition: polynomial.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)