Discontinuous Galerkin Library
#include "dg/algorithm.h"
eve.h
Go to the documentation of this file.
1#ifndef _DG_EVE_
2#define _DG_EVE_
3
4#include <cmath>
5#include "blas.h"
6#include "functors.h"
7
14namespace dg
15{
16
35template< class ContainerType>
36class EVE
37{
38 public:
39 using container_type = ContainerType;
42 EVE() = default;
44 EVE( const ContainerType& copyable, unsigned max_iter = 100):r( copyable), p( r), ap( r), m_max_iter( max_iter) {}
51 void construct( const ContainerType& copyable, unsigned max_iter = 100) {
52 ap = p = r = copyable;
53 m_max_iter = max_iter;
54 }
56 void set_max( unsigned new_max) {
57 m_max_iter = new_max;
58 }
60 unsigned get_max() const { return m_max_iter; }
62 void set_throw_on_fail( bool throw_on_fail){
63 m_throw_on_fail = throw_on_fail;
64 }
86 template< class MatrixType0, class ContainerType0, class ContainerType1, class MatrixType1, class ContainerType2>
87 unsigned solve( MatrixType0&& A, ContainerType0& x, const ContainerType1& b, MatrixType1&& P, const ContainerType2& W, value_type& ev_max, value_type eps_ev = 1e-12);
88 private:
89 ContainerType r, p, ap;
90 unsigned m_max_iter;
91 bool m_throw_on_fail = true;
92};
93
95
96template< class ContainerType>
97template< class Matrix, class ContainerType0, class ContainerType1, class Preconditioner, class ContainerType2>
98unsigned EVE< ContainerType>::solve( Matrix&& A, ContainerType0& x, const ContainerType1& b, Preconditioner&& P, const ContainerType2& W, value_type& ev_max, value_type eps_ev )
99{
100 blas2::symv( std::forward<Matrix>(A),x,r);
101 blas1::axpby( 1., b, -1., r);
102 blas2::symv( std::forward<Preconditioner>(P), r, p );
103 value_type nrmzr_old = blas2::dot( p, W,r);
104 value_type nrmzr_new, nrmAp;
105 value_type alpha = 1., alpha_inv = 1., delta = 0.;
106 value_type evdash, gamma = 0., lambda, omega, beta = 0.;
107 value_type ev_est = 0.;
108 ev_max = 0.;
109 for( unsigned i=1; i<m_max_iter; i++)
110 {
111 lambda = delta*alpha_inv; // EVE!
112 blas2::symv( std::forward<Matrix>(A), p, ap);
113 nrmAp = blas2::dot( p, W, ap);
114 alpha = nrmzr_old/nrmAp;
115 alpha_inv = nrmAp/nrmzr_old; //EVE!
116 lambda+= alpha_inv; //EVE!
117 blas1::axpby( alpha, p, 1.,x);
118 blas1::axpby( -alpha, ap, 1., r);
119 blas2::symv(std::forward<Preconditioner>(P),r,ap);
120 nrmzr_new = blas2::dot( ap, W, r);
121 delta = nrmzr_new /nrmzr_old; // EVE!
122 evdash = ev_est -lambda; // EVE!
123 omega = sqrt( evdash*evdash +4.*beta*gamma); // EVE!
124 gamma = 0.5 *(1. -evdash /omega); // EVE!
125 ev_max += omega*gamma; // EVE!
126 beta = delta*alpha_inv*alpha_inv; // EVE!
127 if( fabs( ev_est - ev_max) < eps_ev*ev_max)
128 return i;
129 blas1::axpby(1.,ap, delta, p );
130 nrmzr_old=nrmzr_new;
131 ev_est = ev_max;
132 }
133 if( m_throw_on_fail)
134 {
135 throw dg::Fail( eps_ev, Message(_ping_)
136 <<"After "<<m_max_iter<<" EVE iterations");
137 }
138 return m_max_iter;
139}
141
142} //namespace dg
143#endif //_DG_EVE_
The Eigen-Value-Estimator (EVE) finds largest Eigenvalue of .
Definition: eve.h:37
EVE(const ContainerType &copyable, unsigned max_iter=100)
Allocate memory for the pcg method.
Definition: eve.h:44
void set_max(unsigned new_max)
Set maximum number of iterations.
Definition: eve.h:56
void construct(const ContainerType &copyable, unsigned max_iter=100)
Allocate memory for the pcg method.
Definition: eve.h:51
unsigned solve(MatrixType0 &&A, ContainerType0 &x, const ContainerType1 &b, MatrixType1 &&P, const ContainerType2 &W, value_type &ev_max, value_type eps_ev=1e-12)
Preconditioned CG to estimate maximum Eigenvalue of the generalized problem .
get_value_type< ContainerType > value_type
Definition: eve.h:40
EVE()=default
Allocate nothing, Call construct method before usage.
unsigned get_max() const
Get maximum number of iterations.
Definition: eve.h:60
ContainerType container_type
Definition: eve.h:39
void set_throw_on_fail(bool throw_on_fail)
Set or unset a throw on failure-to-converge.
Definition: eve.h:62
#define _ping_
Definition: exceptions.h:12
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
Definition: blas1.h:231
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
Definition: blas2.h:287
get_value_type< MatrixType > dot(const ContainerType1 &x, const MatrixType &m, const ContainerType2 &y)
; Binary reproducible general dot product
Definition: blas2.h:85
Operator< real_type > delta(unsigned n)
Create the unit matrix.
Definition: operator.h:514
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition: tensor_traits.h:38
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Indicate failure to converge.
Definition: exceptions.h:126