DUNE-ACFEM (2.5.1)

trueerrorestimator.hh
1 #ifndef __TRUEERROR_ESTIMATOR_HH__
2 #define __TRUEERROR_ESTIMATOR_HH__
3 
4 // Local includes
5 #include "estimatorinterface.hh"
6 
7 namespace Dune {
8 
9  namespace ACFem {
10 
23  template<class TrueSolutionFunction, class Norm>
25  : public DefaultEstimator<TrueErrorEstimator<TrueSolutionFunction, Norm> >
26  {
27  typedef TrueSolutionFunction TrueSolutionFunctionType;
28  typedef Norm NormType;
30  typedef DefaultEstimator<ThisType> BaseType;
31  public:
32  // Interface types
33  typedef typename TrueSolutionFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
34  typedef typename DiscreteFunctionSpaceType::RangeFieldType RangeFieldType;
35  typedef typename DiscreteFunctionSpaceType::GridPartType GridPartType;
36  typedef typename GridPartType::GridType GridType;
37  typedef typename GridType::template Codim<0>::Entity ElementType;
38  protected:
39  // Could live without that, but we cache the results of the local
40  // error computations in a vector.
41  typedef std::vector<RangeFieldType> ErrorIndicatorType;
42  typedef typename DiscreteFunctionSpaceType::IteratorType GridIteratorType;
43  typedef typename GridPartType::IndexSetType IndexSetType;
44  public:
45  typedef typename ErrorIndicatorType::const_iterator IteratorType;
46 
47  // Constructor. The estimator-interface expects the estimate-method
48  // to accept only one argument -- the discrete solution to be
49  // estimated -- so the "true" function has to be passed on through
50  // the estimator (or: at least this is the easiest way)
51  explicit TrueErrorEstimator(const TrueSolutionFunctionType& trueSolution)
52  : trueSolution_(trueSolution),
53  dfSpace_(trueSolution_.space()),
54  gridPart_(const_cast<GridPartType&>(dfSpace_.gridPart())),
55  indexSet_(gridPart_.indexSet()),
56  grid_(gridPart_.grid()),
57  norm_(gridPart_)
58  {
59  localIndicators_.resize(indexSet_.size(0));
60  }
61 
63  template<class DiscreteFunctionType>
64  RangeFieldType estimate(const DiscreteFunctionType& uh)
65  {
66  // clear all local estimators
67  clear();
68 
69  // calculate local estimator
70  const GridIteratorType endGrid = dfSpace_.end();
71  for (GridIteratorType it = dfSpace_.begin(); it != endGrid; ++it) {
72  estimateLocal(*it, uh);
73  }
74 
75  double error = 0.0;
76 
77  // sum up local estimators
78  const IteratorType endEstimator = end();
79  for (IteratorType it = begin(); it != endEstimator; ++it) {
80  error += *it;
81  }
82 
83  // obtain global sum
84  error = grid_.comm().sum(error);
85 
86  // print error estimator
87  std::cout << "Computed Error: " << std::sqrt(error) << std::endl;
88  return std::sqrt(error);
89  }
90 
91  const RangeFieldType& operator[](const size_t& idx) const {
92  return localIndicators_[idx];
93  }
94 
95  const RangeFieldType& operator[](const ElementType& entity) const {
96  return (*this)[indexSet_.index(entity)];
97  }
98 
99  size_t size() const {
100  return indexSet_.size(0);
101  }
102 
103  IteratorType begin() const {
104  return localIndicators_.begin();
105  }
106 
107  IteratorType end() const {
108  return localIndicators_.end();
109  }
110 
111  GridType& grid() const {
112  return grid_;
113  }
114 
115  const DiscreteFunctionSpaceType& space() const
116  {
117  return dfSpace_;
118  }
119 
120  private:
121  void clear ()
122  {
123  // resize and clear
124  localIndicators_.resize(indexSet_.size(0));
125  const typename ErrorIndicatorType::iterator end = localIndicators_.end();
126  for (typename ErrorIndicatorType::iterator it = localIndicators_.begin();
127  it != end; ++it ) {
128  *it = 0.0;
129  }
130  }
131 
133  template<class DiscreteFunctionType>
134  void estimateLocal(const ElementType &entity, const DiscreteFunctionType& uh)
135  {
136  const int index = indexSet_.index(entity);
137 
138  // Crazy L2-norm takes a 1d vector as argument. Why?
139  FieldVector<RangeFieldType, 1> dist(0);
140  norm_.distanceLocal(entity, 2*uh.space().order()+2, trueSolution_.localFunction(entity), uh.localFunction(entity), dist);
141  localIndicators_[index] = dist;
142  }
143  private:
144  const TrueSolutionFunctionType& trueSolution_;
145  const DiscreteFunctionSpaceType& dfSpace_;
146  GridPartType& gridPart_;
147  const IndexSetType& indexSet_;
148  GridType& grid_;
149  ErrorIndicatorType localIndicators_;
150  NormType norm_;
151  };
152 
153  template<class TrueSolutionFunction, class Norm>
154  struct EstimatorTraits<TrueErrorEstimator<TrueSolutionFunction, Norm> >
155  {
156  typedef typename TrueSolutionFunction::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
157  typedef typename
158  std::vector<typename DiscreteFunctionSpaceType::RangeFieldType>::const_iterator
159  IteratorType;
160  };
161 
163 
165 
166  } // ACFem
167 
168 } // Dune
169 
170 #endif // __ELLIPTIC_ESTIMATOR_HH__
"estimator" which return the "true" error with respect to some given function in some standard norm.
Definition: trueerrorestimator.hh:26
RangeFieldType estimate(const DiscreteFunctionType &uh)
calculate estimator
Definition: trueerrorestimator.hh:64
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)