KASKADE 7 development version
Loading...
Searching...
No Matches
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
15#include <cassert>
16#include <cmath>
17#include <limits>
18#include <numeric>
19
20#include <dune/common/fvector.hh>
21#include <dune/common/fmatrix.hh>
22
23namespace Kaskade
24{
36 template <class R>
38 public:
42 typedef R Real;
43
47 virtual void clear() = 0;
48
55
63 virtual void step(Real gamma) = 0;
64
78 virtual void stepDetail(Real alpha, Real qAq, Real qPq, Real rPINVr) {}
79
84 virtual void residual(Real sigma) {}
85
89 virtual int getMaxIterationSteps() = 0;
90
95 virtual operator bool() = 0;
96
100 virtual bool minimalDecreaseAchieved() { assert("not implemented"); return false; }
101 };
102
103 // -----------------------------------------------------------------------------------------------
104 // -----------------------------------------------------------------------------------------------
105
111 template <class R = double>
113 {
114 public:
118 typedef R Real;
119
124 : n(n_), count(0)
125 {}
126
130 virtual void clear()
131 {
132 count = 0;
133 }
134
139 {
140 return *this;
141 }
142
146 virtual void step(Real )
147 {
148 ++count;
149 }
150
155 virtual operator bool()
156 {
157 return count >= n;
158 }
159
164 {
165 return n;
166 }
167
168 private:
169 int n;
170 int count;
171 };
172
173 // -----------------------------------------------------------------------------------------------
174 // -----------------------------------------------------------------------------------------------
175
209 template <class R = double>
211 {
212 public:
213 typedef R Real;
214
225 : tol(atol), maxit(maxit), lookah(6)
226 {}
227
231 virtual void clear()
232 {
233 gammas2.clear();
234 }
235
242 {
243 assert(atol>=0);
244 tol = atol;
245 return *this;
246 }
247
256 {
257 assert(lah>=0);
258 lookah = lah;
259 return *this;
260 }
261
266 virtual void step(Real gamma2)
267 {
268 gammas2.push_back(gamma2);
269 }
270
275 {
276 return maxit;
277 }
278
283 virtual operator bool()
284 {
285 return (gammas2.size()>=5 && error() <= tol) || gammas2.size()>=maxit;
286 }
287
291 Real error() const
292 {
293 int const n = gammas2.size();
294
295 // Check whether there is enough data for robust lookahead error estimation
296 if (n<lookah)
297 return std::numeric_limits<R>::max();
298
299 // Estimate local linear contraction rate of the form gamma2(k+i) = a q^i
300 // in logarithmic scaling log(gamma2(k+1)) = loga + i logq. Do a least squares fit
301 // || [1 0 ] [log(gamma2(k) ] ||
302 // || [1 1 ] [loga] - [log(gamma2(k+1)] ||
303 // || [1 2 ] [logq] [log(gamma2(k+2)] || = min
304 // || [. . ] [ ... ] ||
305 // || [1 k-1] [log(gamma2(k+2)] ||
306 //
307 // The normal equations are just
308 // [m (m-1)m/2 ] [loga] [ sum log(gamma2(i)) ]
309 // [(m-1)m/2 (m-1)m(2m-1)/6] [logq] = [ sum i*log(gamma2(i)) ]
310 int const m = std::min(n,5+lookah);
313 AtA[0][0] = m; AtA[0][1] = (m-1)*m/2;
314 AtA[1][0] = (m-1)*m/2; AtA[1][1] = (m-1)*m*(2*m-1)/6;
315 for (int i=0; i<m; ++i)
316 {
317 f[0] += std::log(gammas2[n-m+i]);
318 f[1] += i*std::log(gammas2[n-m+i]);
319 }
321 AtA.solve(x,f);
322 R q = std::exp(x[1]);
323
324 // Estimate error eps^2 = sum gamma^2(i) + q*gamma(n-1)/(1-q)
325 R eps2 = std::accumulate(begin(gammas2)+n-lookah,end(gammas2),q*gammas2.back()/(1-q));
326 return std::sqrt(eps2);
327 }
328
332 void setMaxit(int m)
333 {
334 maxit = m;
335 }
336
337 std::vector<Real> const& gamma2() const {return gammas2; }
338
339 private:
340 Real tol;
341 int maxit;
342 // squared gammas
343 std::vector<Real> gammas2;
344 int lookah;
345 };
346
347 // -----------------------------------------------------------------------------------------------
348 // -----------------------------------------------------------------------------------------------
349
376 template <class R>
378 public:
379 typedef R Real;
380
394 StrakosTichyEnergyErrorTerminationCriterion(Real tol_, int maxit_, double eps_ = 1e-12)
395 : tol(tol_), maxit(maxit_), eps(eps_)
396 {}
397
413 double eps_ = 1e-12)
414 : tol(tol_), maxit(maxit_), eps(eps_), minTol(minTol_)
415 {}
416
420 virtual void clear()
421 {
422 scaledGamma2.clear();
423 energyNorm = 0;
424 }
425
431 virtual PCGTerminationCriterion<Real>& tolerance(Real tol_) { tol = tol_; return *this; }
432
440 void setLookAhead(int d_) { d = d_; }
441
446 virtual void step(Real gamma)
447 {
448 scaledGamma2.push_back( gamma );
449 energyNorm += gamma;
450 }
451
456 {
457 return maxit;
458 }
459
463 virtual operator bool()
464 {
465 return scaledGamma2.size() > d && relativeError() < std::max(eps,tol*tol);
466 }
467
472 {
473 if( scaledGamma2.size() < d ) return std::numeric_limits<Real>::max();
474 return std::accumulate(scaledGamma2.end() - d, scaledGamma2.end(), 0.) / energyNorm;
475 }
476
477 bool minimalDecreaseAchieved() { return relativeError() < minTol; }
478
479 private:
480 Real tol;
481 int maxit;
482 // squared gammas
483 std::vector<Real> scaledGamma2;
484 Real energyNorm = 0;
485 int d = 50;
486 double eps = 1e-12, minTol = 1e-2;
487 };
488
489 // -----------------------------------------------------------------------------------------------
490 // -----------------------------------------------------------------------------------------------
491
519 template <class R>
521 public:
522 typedef R Real;
523
537 StrakosTichyPTerminationCriterion(Real tol_, int maxit_, double eps_ = 1e-12)
538 : tol(tol_), maxit(maxit_), eps(eps_)
539 {}
540
555 StrakosTichyPTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_ = 1e-12)
556 : tol(tol_), maxit(maxit_), eps(eps_), minTol(minTol_)
557 {}
558
562 virtual void clear()
563 {
564 scaledGamma2.clear();
565 steps2.clear();
566 energyNorm = 0;
567 }
568
574 virtual PCGTerminationCriterion<Real>& tolerance(Real tol_) { tol = tol_; return *this; }
575
583 void setLookAhead(int d_) { d = d_; }
584
589 virtual void step(Real gamma)
590 {
591 scaledGamma2.push_back( gamma );
592 energyNorm += gamma;
593 }
594
602 virtual void stepDetail(Real alpha, Real qAq, Real qPq, Real rPINVr)
603 {
604 steps2.push_back(qPq/qAq);
605 }
606
611 {
612 return maxit;
613 }
614
618 virtual operator bool()
619 {
620 return scaledGamma2.size() > 2*d && relativeError() < std::max(eps,tol*tol);
621 }
622
627 {
628 if( scaledGamma2.size() < 2*d ) return std::numeric_limits<Real>::max();
629
630 size_t j = scaledGamma2.size() - 2*d;
631 double tau = 0;
632 for(size_t i = j; i < j + d; ++i)
633 tau += steps2[i] * ( scaledGamma2[i] +
634 2 * std::accumulate(scaledGamma2.begin()+i+1, scaledGamma2.end(),0) );
635 return tau;
636 }
637
638 bool minimalDecreaseAchieved() { return relativeError() < minTol; }
639
640 private:
641 Real tol;
642 int maxit;
643 // squared gammas
644 std::vector<Real> scaledGamma2, steps2;
645 Real energyNorm = 0;
646 int d = 50;
647 double eps = 1e-12, minTol = 1e-2;
648 };
649
650}
651
652#endif // CGTERMINATIONCRITERIA_HH
virtual void step(Real)
supplies energy of step to the termination criterion
virtual PCGTerminationCriterion< R > & tolerance(Real)
unused
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
virtual void clear()
re-initializes the termination criterion for a new IterateType::CG run
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
virtual void clear()
re-initializes the termination criterion for a new IterateType::CG run
virtual void step(Real gamma2)
supplies energy of step to the termination criterion
PCGEnergyErrorTerminationCriterion(Real atol, int maxit)
constructor
Real error() const
returns the estimated absolute energy error
PCGEnergyErrorTerminationCriterion< Real > & lookahead(int lah)
set requested lookahead value
void setMaxit(int m)
Sets a new value for the maximal number of iterations.
std::vector< Real > const & gamma2() const
virtual PCGEnergyErrorTerminationCriterion< Real > & tolerance(Real atol)
set requested absolute tolerance
Interface for IterateType::PCG termination criterion policy classes.
virtual void step(Real gamma)=0
supplies energy of step to the termination criterion
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 residual(Real sigma)
supplies the preconditioned residual to the termination criterion
virtual bool minimalDecreaseAchieved()
whether a minimal decrease has been achieved (used by hybrid CG)
virtual void stepDetail(Real alpha, Real qAq, Real qPq, Real rPINVr)
supplies additional algorithmic quantities to the termination criterion
virtual PCGTerminationCriterion< Real > & tolerance(Real tol)=0
set requested tolerance
virtual PCGTerminationCriterion< Real > & tolerance(Real tol_)
set requested relative tolerance
StrakosTichyEnergyErrorTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_=1e-12)
constructor
StrakosTichyEnergyErrorTerminationCriterion(Real tol_, int maxit_, double eps_=1e-12)
constructor
bool minimalDecreaseAchieved()
whether a minimal decrease has been achieved (used by hybrid CG)
virtual void clear()
re-initializes the termination criterion for a new IterateType::CG run
virtual void step(Real gamma)
supplies energy of step to the termination criterion
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
void setLookAhead(int d_)
set requested lookahead value
Real relativeError()
returns the estimated relative energy error
Real relativeError()
returns the estimated relative 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
virtual PCGTerminationCriterion< Real > & tolerance(Real tol_)
set requested relative tolerance
bool minimalDecreaseAchieved()
whether a minimal decrease has been achieved (used by hybrid CG)
StrakosTichyPTerminationCriterion(Real tol_, Real minTol_, int maxit_, double eps_=1e-12)
constructor
virtual void step(Real gamma)
supplies energy of step to the termination criterion
virtual int getMaxIterationSteps()
get the maximum number of allowed iteration steps
virtual void stepDetail(Real alpha, Real qAq, Real qPq, Real rPINVr)
supplies additional quantities for the -criterion
void setLookAhead(int d_)
set requested lookahead value
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
double energyNorm(const Dune::BCRSMatrix< Dune::FieldMatrix< double, 1, 1 > > &matrix_, const Dune::BlockVector< Dune::FieldVector< double, 1 > > &vec_)