KASKADE 7 development version
cgTerminationCriteria.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2017 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12#ifndef CGTERMINATIONCRITERIA_HH
13#define CGTERMINATIONCRITERIA_HH
14
15namespace Kaskade
16{
22 template <class R>
23 class PCGTerminationCriterion {
24 public:
28 typedef R Real;
29
33 virtual void clear() = 0;
34
40 virtual void setTolerance(Real tol) = 0;
41
47 virtual void setLookAhead(int lah) = 0;
55 virtual void addStepQuantities(Real stepLength, Real qAq, Real qPq, Real rPINVr) = 0;
56
57
61 virtual int getMaxIterationSteps() = 0;
67 virtual operator bool() = 0;
68
69 virtual bool minimalDecreaseAchieved() { assert("not implemented"); return false; }
70 };
71
72
79 template <class R>
81 public:
82 typedef R Real;
83
95 StrakosTichyEnergyErrorTerminationCriterion(Real tol_, int maxit_, double eps_ = 1e-12)
96 : tol(tol_), maxit(maxit_), eps(eps_)
97 {}
98
111 StrakosTichyEnergyErrorTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_ = 1e-12)
112 : tol(tol_), maxit(maxit_), eps(eps_), minTol(minTol_)
113 {}
114
118 virtual void clear()
119 {
120 scaledGamma2.clear();
121 energyNorm = 0;
122 }
123
129 virtual void setTolerance(Real tol_) { tol = tol_; }//minTol = sqrt(tol); }
130
138 virtual void setLookAhead(int d_) { d = d_; }
139
147 virtual void addStepQuantities(Real stepLength, Real, Real, Real rPINVr)
148 {
149 scaledGamma2.push_back( stepLength * rPINVr );
150 energyNorm += stepLength * rPINVr;
151 }
152
157 {
158 return maxit;
159 }
164 virtual operator bool()
165 {
166 return scaledGamma2.size() > d && relativeError() < std::max(eps,tol*tol);
167 }
168
173 {
174 if( scaledGamma2.size() < d ) return std::numeric_limits<Real>::max();
175 return std::accumulate(scaledGamma2.end() - d, scaledGamma2.end(), 0.) / energyNorm;
176 }
177
178 bool minimalDecreaseAchieved() { return relativeError() < minTol; }
179
180 private:
181 Real tol;
182 int maxit;
183 // squared gammas
184 std::vector<Real> scaledGamma2;
185 Real energyNorm = 0;
186 int d = 50;
187 double eps = 1e-12, minTol = 1e-2;
188 };
189
190
198 template <class R>
200 public:
201 typedef R Real;
202
214 StrakosTichyPTerminationCriterion(Real tol_, int maxit_, double eps_ = 1e-12)
215 : tol(tol_), maxit(maxit_), eps(eps_)
216 {}
217
230 StrakosTichyPTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_ = 1e-12)
231 : tol(tol_), maxit(maxit_), eps(eps_), minTol(minTol_)
232 {}
233
237 virtual void clear()
238 {
239 scaledGamma2.clear();
240 energyNorm = 0;
241 }
242
248 virtual void setTolerance(Real tol_) { tol = tol_; }
249
257 virtual void setLookAhead(int d_) { d = d_; }
258
266 virtual void addStepQuantities(Real stepLength, Real qAq, Real qPq, Real rPINVr)
267 {
268 scaledGamma2.push_back( stepLength * rPINVr );
269 energyNorm += stepLength * rPINVr;
270 steps2.push_back(qPq/qAq);
271 }
272
277 {
278 return maxit;
279 }
284 virtual operator bool()
285 {
286 return scaledGamma2.size() > 2*d && relativeError() < std::max(eps,tol*tol);
287 }
288
293 {
294 if( scaledGamma2.size() < 2*d ) return std::numeric_limits<Real>::max();
295
296 size_t j = scaledGamma2.size() - 2*d;
297 double tau = 0;
298 for(size_t i = j; i < j + d; ++i)
299 tau += steps2[i] * ( scaledGamma2[i] + 2 * std::accumulate(scaledGamma2.begin()+i+1, scaledGamma2.end(),0) );
300 return tau;
301 }
302
303 bool minimalDecreaseAchieved() { return relativeError() < minTol; }
304
305 private:
306 Real tol;
307 int maxit;
308 // squared gammas
309 std::vector<Real> scaledGamma2, steps2;
310 Real energyNorm = 0;
311 int d = 50;
312 double eps = 1e-12, minTol = 1e-2;
313 };
314
315}
316
317#endif // CGTERMINATIONCRITERIA_HH
Interface for IterateType::PCG termination criterion policy classes.
Definition: linalg/apcg.hh:64
virtual void setTolerance(Real tol)=0
set requested tolerance
virtual void clear()=0
re-initializes the termination criterion for a new IterateType::CG run
virtual int getMaxIterationSteps()=0
get the maximum number of allowed iteration steps
virtual void setLookAhead(int lah)=0
set requested look-ahead count
virtual void addStepQuantities(Real stepLength, Real qAq, Real qPq, Real rPINVr)=0
addStepQuantities supplies algorithmic quantities to the termination criterion
Relative energy error termination criterion according to Strakos, Tichy: Error estimation in precondi...
StrakosTichyEnergyErrorTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_=1e-12)
constructor
StrakosTichyEnergyErrorTerminationCriterion(Real tol_, int maxit_, double eps_=1e-12)
constructor
virtual void setLookAhead(int d_)
set requested lookahead value
virtual void clear()
re-initializes the termination criterion for a new IterateType::CG run
virtual void addStepQuantities(Real stepLength, Real, Real, Real rPINVr)
addStepQuantities supplies algorithmic quantities to the termination criterion
virtual void setTolerance(Real tol_)
set requested relative tolerance
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
Real relativeError()
returns the estimated absolute energy error
Relative error termination criterion based on the norm induced by the preconditioner,...
virtual void setTolerance(Real tol_)
set requested relative tolerance
Real relativeError()
returns the estimated absolute energy error
StrakosTichyPTerminationCriterion(Real tol_, int maxit_, double eps_=1e-12)
constructor
virtual void clear()
re-initializes the termination criterion for a new IterateType::CG run
StrakosTichyPTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_=1e-12)
constructor
virtual void addStepQuantities(Real stepLength, Real qAq, Real qPq, Real rPINVr)
addStepQuantities supplies algorithmic quantities to the termination criterion
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
virtual void setLookAhead(int d_)
set requested lookahead value
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition: fixdune.hh:110