-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDavidson.h
165 lines (146 loc) · 4.76 KB
/
Davidson.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef __TTNS_DAVIDSON_H
#define __TTNS_DAVIDSON_H 1
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <functional>
#include <btas/QSPARSE/QSDArray.h>
#include "Input.h"
namespace ttns
{
namespace davidson
{
template<size_t N, class Q>
using Functor = std::function<void(const btas::QSDArray<N, Q>&, btas::QSDArray<N, Q>&)>;
//
// Davidson's precondition
//
template<size_t N, class Q>
void precondition (const double& eval, const btas::QSDArray<N, Q>& diag, btas::QSDArray<N, Q>& errv)
{
for(auto ir = errv.begin(); ir != errv.end(); ++ir)
{
auto id = diag.find(ir->first);
if(id != diag.end())
{
auto irx = ir->second->begin();
auto idx = id->second->begin();
for(; irx != ir->second->end(); ++irx, ++idx)
{
double denm = eval - *idx;
if(fabs(denm) < 1.0e-12) denm = 1.0e-12;
*irx /= denm;
}
}
else
{
btas::Dscal(1.0/eval, (*ir->second));
}
}
}
//
// Davidson eigen solver
//
template<size_t N, class Q>
double diagonalize (
const Functor<N, Q>& f_contract,
const btas::QSDArray<N, Q>& diag,
btas::QSDArray<N, Q>& wfnc,
const SweepParameters& param)
{
int max_ritz = param.MaxDimDavidson();
double eval = 0.0;
// reserve working space
std::vector<btas::QSDArray<N, Q>> trial(max_ritz);
std::vector<btas::QSDArray<N, Q>> sigma(max_ritz);
btas::QSDcopy(wfnc, trial[0]);
int niter = 0;
int iconv = 0;
std::cout << "\t\t\t----------------------------------------------------------------" << std::endl;
while(iconv < 1 && niter < param.MaxIterDavidson())
{
std::cout << "\t\t\t\tmacro iteration [ " << std::setw(2) << niter << " ] " << std::endl;
std::cout << "\t\t\t----------------------------------------------------------------" << std::endl;
// to keep numerical stability
btas::Normalize(trial[0]);
sigma[0].clear();
f_contract(trial[0], sigma[0]);
for(int m = 1; m <= max_ritz; ++m)
{
// compute small Hamiltonian matrix
btas::DArray<2> heff(m, m);
btas::DArray<2> ovlp(m, m);
for(int i = 0; i < m; ++i)
{
heff(i, i) = btas::QSDdotc(trial[i], sigma[i]);
ovlp(i, i) = btas::QSDdotc(trial[i], trial[i]);
for(int j = 0; j < i; ++j)
{
double hij = btas::QSDdotc(trial[i], sigma[j]);
heff(i, j) = hij;
heff(j, i) = hij;
double sij = btas::QSDdotc(trial[i], trial[j]);
ovlp(i, j) = sij;
ovlp(j, i) = sij;
}
}
// solve eigenvalue problem to obtain Ritz value & vector
btas::DArray<2> rvec;
btas::DArray<1> rval;
Dsyev('V', 'U', heff, rval, rvec);
eval = rval(0);
std::cout << "\t\t\t\tmicro iteration [ " << std::setw(2) << m << " ] :: " << std::setprecision(16) << std::setw(22) << std::fixed << eval << std::endl;
// rotate trial & sigma vectors by Ritz vector
std::vector<btas::QSDArray<N, Q>> trial_save(m);
std::vector<btas::QSDArray<N, Q>> sigma_save(m);
for(int i = 0; i < m; ++i)
{
btas::QSDcopy(trial[i], trial_save[i]);
btas::QSDcopy(sigma[i], sigma_save[i]);
btas::QSDscal(rvec(i, i), trial[i]);
btas::QSDscal(rvec(i, i), sigma[i]);
}
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < m; ++j)
{
if(i != j)
{
btas::QSDaxpy(rvec(i, j), trial_save[i], trial[j]);
btas::QSDaxpy(rvec(i, j), sigma_save[i], sigma[j]);
}
}
}
// compute error vector
btas::QSDArray<N, Q> evec;
btas::QSDArray<N, Q> errv;
btas::QSDcopy( trial[0], evec);
btas::QSDcopy( sigma[0], errv);
btas::QSDaxpy(-eval, evec, errv);
double rnorm = btas::QSDdotc(errv, errv);
if(rnorm < param.ToleDavidson()) { ++iconv; break; }
// solve correction equation
if(m < max_ritz)
{
precondition(eval, diag, errv);
for(int i = 0; i < m; ++i)
{
btas::Normalize(errv);
btas::Orthogonalize(trial[i], errv);
}
btas::Normalize(errv);
btas::QSDcopy(errv, trial[m]);
sigma[m].clear();
f_contract(trial[m], sigma[m]);
}
}
++niter;
std::cout << "\t\t\t----------------------------------------------------------------" << std::endl;
}
btas::QSDcopy(trial[0], wfnc);
return eval;
}
} // namespace davidson
} // namespace ttns
#endif // __TTNS_DAVIDSON_H