Grid 0.7.0
PowerMethod.h
Go to the documentation of this file.
1#pragma once
2namespace Grid {
3template<class Field> class PowerMethod
4{
5 public:
6
7 template<typename T> static RealD normalise(T& v)
8 {
9 RealD nn = norm2(v);
10 nn = sqrt(nn);
11 v = v * (1.0/nn);
12 return nn;
13 }
14
15 RealD operator()(LinearOperatorBase<Field> &HermOp, const Field &src)
16 {
17 GridBase *grid = src.Grid();
18
19 // quickly get an idea of the largest eigenvalue to more properly normalize the residuum
20 RealD evalMaxApprox = 0.0;
21 auto src_n = src;
22 auto tmp = src;
23 const int _MAX_ITER_EST_ = 200;
24
25 for (int i=0;i<_MAX_ITER_EST_;i++) {
26
27 normalise(src_n);
28 HermOp.HermOp(src_n,tmp);
29 RealD vnum = real(innerProduct(src_n,tmp)); // HermOp.
30 RealD vden = norm2(src_n);
31 RealD na = vnum/vden;
32
33 std::cout << GridLogMessage << "PowerMethod: Current approximation of largest eigenvalue " << na << std::endl;
34
35 // if ( (fabs(evalMaxApprox/na - 1.0) < 0.0001) || (i==_MAX_ITER_EST_-1) ) {
36 // evalMaxApprox = na;
37 // return evalMaxApprox;
38 // }
39 evalMaxApprox = na;
40 src_n = tmp;
41 }
42 std::cout << GridLogMessage << " Approximation of largest eigenvalue: " << evalMaxApprox << std::endl;
43 return evalMaxApprox;
44 }
45};
46}
accelerator_inline Grid_simd< S, V > sqrt(const Grid_simd< S, V > &r)
Lattice< vobj > real(const Lattice< vobj > &lhs)
ComplexD innerProduct(const Lattice< vobj > &left, const Lattice< vobj > &right)
RealD norm2(const Lattice< vobj > &arg)
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
double RealD
Definition Simd.h:61
static RealD normalise(T &v)
Definition PowerMethod.h:7
RealD operator()(LinearOperatorBase< Field > &HermOp, const Field &src)
Definition PowerMethod.h:15
virtual void HermOp(const Field &in, Field &out)=0