DUNE-ACFEM (unstable)

trueerrorestimator.hh
1#ifndef __TRUEERROR_ESTIMATOR_HH__
2#define __TRUEERROR_ESTIMATOR_HH__
3
4#include <numeric>
5
6#include <dune/fem/function/localfunction/const.hh>
7
8#include "../common/entitystorage.hh"
9
10namespace Dune {
11
12 namespace ACFem {
13
26 template<class TrueSolutionFunction, class Norm>
28 : public EntityStorage<typename TrueSolutionFunction::GridPartType,
29 typename TrueSolutionFunction::GridPartType::ctype>
30 {
31 typedef TrueSolutionFunction TrueSolutionFunctionType;
32 typedef Norm NormType;
34 using BaseType = EntityStorage<typename TrueSolutionFunction::GridPartType,
35 typename TrueSolutionFunction::GridPartType::ctype>;
36 public:
37 // Interface types
38 typedef typename TrueSolutionFunctionType::GridPartType GridPartType;
39 typedef typename TrueSolutionFunction::FunctionSpaceType::RangeFieldType RangeFieldType;
40 typedef typename GridPartType::GridType GridType;
41 typedef typename GridType::template Codim<0>::Entity ElementType;
42 protected:
43 // Could live without that, but we cache the results of the local
44 // error computations in a vector.
45 typedef std::vector<RangeFieldType> ErrorIndicatorType;
46 typedef typename GridPartType::IndexSetType IndexSetType;
47 public:
48 typedef typename ErrorIndicatorType::const_iterator IteratorType;
49
50 // Constructor. The estimator-interface expects the estimate-method
51 // to accept only one argument -- the discrete solution to be
52 // estimated -- so the "true" function has to be passed on through
53 // the estimator (or: at least this is the easiest way)
54 explicit TrueErrorEstimator(const TrueSolutionFunctionType& trueSolution)
55 : BaseType(trueSolution.gridPart()),
56 trueSolution_(trueSolution),
57 gridPart_(trueSolution.gridPart()),
58 indexSet_(gridPart_.indexSet()),
59 grid_(gridPart_.grid()),
60 norm_(gridPart_)
61 {
62 }
63
65 template<class DiscreteFunctionType>
66 RangeFieldType estimate(const DiscreteFunctionType& uh)
67 {
68 // clear all local estimators
69 clear();
70
71 Fem::ConstLocalFunction<DiscreteFunctionType> uhLocal(uh);
72
73 // calculate local estimator
74 const auto endGrid = uh.space().end();
75 for (auto it = uh.space().begin(); it != endGrid; ++it) {
76 estimateLocal(*it, uhLocal);
77 }
78
79 RangeFieldType error = 0.0;
80
81 // sum up local estimators
82 error = std::accumulate(this->storage_.begin(), this->storage_.end(), error);
83
84 // obtain global sum
85 error = grid_.comm().sum(error);
86
87 // print error estimator
88 std::cout << "Computed Error: " << std::sqrt(error) << std::endl;
89 return std::sqrt(error);
90 }
91
92 private:
93 void clear ()
94 {
95 // resize and clear
96 BaseType::clear();
97 }
98
100 template<class DiscreteLocalFunctionType>
101 void estimateLocal(const ElementType &entity, DiscreteLocalFunctionType& uh)
102 {
103 const int index = indexSet_.index(entity);
104
105 uh.bind(entity);
106 trueSolution_.bind(entity);
107
108 // Crazy L2-norm takes a 1d vector as argument. Why?
109 FieldVector<RangeFieldType, 1> dist(0);
110 norm_.distanceLocal(entity, 2*uh.order()+2, trueSolution_, uh, dist);
111 this->storage_[index] = dist;
112
113 trueSolution_.unbind();
114 uh.unbind();
115 }
116 private:
117 Fem::ConstLocalFunction<TrueSolutionFunctionType> trueSolution_;
118 const GridPartType& gridPart_;
119 const IndexSetType& indexSet_;
120 const GridType& grid_;
121 NormType norm_;
122 };
123
125
127
128 } // ACFem
129
130} // Dune
131
132#endif // __ELLIPTIC_ESTIMATOR_HH__
"estimator" which return the "true" error with respect to some given function in some standard norm.
Definition: trueerrorestimator.hh:30
RangeFieldType estimate(const DiscreteFunctionType &uh)
calculate estimator
Definition: trueerrorestimator.hh:66
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)