Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
discretizationerror.hh
Go to the documentation of this file.
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set ts=8 sw=4 et sts=4:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_DISCRETIZATIONERROR_HH
8#define DUNE_FUFEM_DISCRETIZATIONERROR_HH
9
11
13
15
16namespace Dune
17{
25 template<int ROWS, int COLS>
27 {
28 double result = 0;
29 for (std::size_t i=0; i<a.rows; ++i)
30 for (std::size_t j=0; j<a.cols; ++j)
31 result += a[i][j] * b[i][j];
32
33 return result;
34 }
35}
36
37namespace Dune::Fufem
38{
39
41{
42public:
43
49 template <class Function1, class Function2>
50 static double computeL2DifferenceSquared(const Function1& f1, const Function2 f2, QuadratureRuleKey quadKey)
51 {
52 // The quantity to be computed
53 double differenceSquared = 0;
54
55 const auto gridView = f1.entitySet().gridView();
56 const int dim = decltype(gridView)::dimension;
57
58 // First function always has to be defined on a grid
59 auto localFunction1 = localFunction(f1);
60
61 using Element = typename decltype(gridView)::template Codim<0>::Entity;
62 if constexpr (requires (const Element& e) {localFunction(f2).bind(e);} )
63 { // If second function can be evaluated in local coordinates
64 auto localFunction2 = localFunction(f2);
65
66 for (auto&& element : elements(gridView))
67 {
68 localFunction1.bind(element);
69 localFunction2.bind(element);
70
71 // Get quadrature formula
72 quadKey.setGeometryType(element.type());
73 const auto& quad = QuadratureRuleCache<double, dim>::rule(quadKey);
74
75 for (auto&& qp : quad)
76 {
77 const auto integrationElement = element.geometry().integrationElement(qp.position());
78
79 // Evaluate functions
80 const auto value1 = localFunction1(qp.position());
81 const auto value2 = localFunction2(qp.position());
82
83 // integrate squared difference
84 differenceSquared += Dune::dot(value1 - value2, value1 - value2) * qp.weight() * integrationElement;
85 }
86 }
87 }
88 else
89 { // Second function has to be evaluated in global coordinates
90 for (auto&& element : elements(gridView))
91 {
92 localFunction1.bind(element);
93
94 // Get quadrature formula
95 quadKey.setGeometryType(element.type());
96 const auto& quad = QuadratureRuleCache<double, dim>::rule(quadKey);
97
98 for (auto&& qp : quad)
99 {
100 const auto integrationElement = element.geometry().integrationElement(qp.position());
101
102 // Evaluate functions
103 const auto value1 = localFunction1(qp.position());
104 const auto value2 = f2(element.geometry().global(qp.position()));
105
106 // integrate squared difference
107 differenceSquared += Dune::dot(value1 - value2, value1 - value2) * qp.weight() * integrationElement;
108 }
109 }
110 }
111
112 return differenceSquared;
113 }
114};
115
116} // namespace Dune::Fufem
117
118#endif
119
auto dot(const A &a, const B &b) -> typename std::enable_if< IsNumber< A >::value &&!IsVector< A >::value &&!std::is_same< typename FieldTraits< A >::field_type, typename FieldTraits< A >::real_type > ::value, decltype(conj(a) *b)>::type
size_type dim() const
Definition dunefunctionsboundaryfunctionalassembler.hh:29
static constexpr std::integral_constant< int, ROWS > rows
static constexpr std::integral_constant< int, COLS > cols
Definition discretizationerror.hh:41
static double computeL2DifferenceSquared(const Function1 &f1, const Function2 f2, QuadratureRuleKey quadKey)
Compute squared L2 difference between two functions.
Definition discretizationerror.hh:50
A token that specifies a quadrature rule.
Definition quadraturerulecache.hh:39
void setGeometryType(const Dune::GeometryType &gt)
Definition quadraturerulecache.hh:117
static const Dune::QuadratureRule< coord_type, dim > & rule(const Dune::GeometryType &gt, const int order, int refinement)
Definition quadraturerulecache.hh:196