-
Notifications
You must be signed in to change notification settings - Fork 0
/
hfHandle.cpp
164 lines (108 loc) · 3.05 KB
/
hfHandle.cpp
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
#include <fstream>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include <sstream>
#include "fileHandle/filehandle.h"
#include "hfHandle.h"
hfHandle::hfHandle(){ }
hfHandle::hfHandle(std::string in, std::string out){ init(in, out); }
void hfHandle::init(std::string in, std::string out)
{
std::ifstream intgrl;
intgrl.open(in.c_str());
o.open(out);
double val;
int i, j, k, l;
intgrl >> E_nuc >> no >> no_e >> acc;
if(no_e/2 > no)
{
o % "No. of atoms and electons mismatch";
}
intgrl >> i >> j >> val;
std::string str;
overlap = MatrixXd::Constant(no, no, 0);
Hcore = MatrixXd::Constant(no, no, 0);
while(getline(intgrl, str))
{
if (str == "*") break;
std::istringstream ss(str);
ss >> i >> j >> val;
overlap(i-1, j-1) = val;
if(i != j) overlap(j-1, i-1) = val;
}
while(getline(intgrl, str))
{
if (str == "*") break;
std::istringstream ss(str);
ss >> i >> j >> val;
Hcore(i-1, j-1) = val;
if(i != j) Hcore(j-1, i-1) = val;
}
s_no = SUM(no);
two_e = MatrixXd::Constant(SUM(s_no), 1, 0);
while(getline(intgrl, str))
{
if (str == "*") break;
std::istringstream ss(str);
ss >> i >> j >> k >> l >> val;
two_e(AtomToIndex(i,j,k,l)) = val;
}
SelfAdjointEigenSolver<MatrixXd> es_ov(overlap);
S_inv_sqrt = es_ov.operatorInverseSqrt();
fock = S_inv_sqrt.transpose()*Hcore*S_inv_sqrt;
SelfAdjointEigenSolver<MatrixXd> es_fock(fock, ComputeEigenvectors);
C = S_inv_sqrt*es_fock.eigenvectors();
D = MatrixXd::Constant(no, no, 0);
for(int i=0; i<no; i++)
for(int j=0; j<no; j++)
{
val = 0;
for(int m=0; m<no_e/2; m++) val += C(i, m)*C(j, m);
D(i, j) = val;
}
E_elec = 0;
for(int i=0;i<no; i++)
for(int j=0; j<no; j++)
E_elec += D(i,j)*(Hcore(i,j) + fock(i,j));
Eprev = E_elec;
o << "Iter" << "E(elec)" << "E(tot)" << "deltaE" << fileHandle::endl;
o << '0' << E_elec << getTotalE() << " --- " << fileHandle::endl;
}
void hfHandle::coreSCF()
{
for(int i=0; i < no; i++)
for(int j=0; j < no; j++)
{
fock(i, j) = Hcore(i,j);
for(int k=0; k < no; k++)
for(int l=0; l < no; l++)
fock(i,j) += D(k,l) * ( 2.0 * two_e( AtomToIndex(i+1,j+1,k+1,l+1) ) - two_e( AtomToIndex(i+1,k+1,j+1,l+1) ) );
}
double val;
SelfAdjointEigenSolver<MatrixXd> es_fock(fock, ComputeEigenvectors);
C = S_inv_sqrt*es_fock.eigenvectors();
D = MatrixXd::Constant(no, no, 0);
for(int i=0; i<no; i++)
for(int j=0; j<no; j++)
{
val = 0;
for(int m=0; m<no_e/2; m++) val += C(i, m)*C(j, m);
D(i, j) = val;
}
E_elec = 0;
for(int i=0;i<no; i++)
for(int j=0; j<no; j++)
E_elec += D(i,j)*(Hcore(i,j) + fock(i,j));
}
void hfHandle::work()
{
int i=0;
do
{
Eprev = E_elec;
coreSCF();
i++;
o << i << E_elec << getTotalE() << E_elec-Eprev << fileHandle::endl;
}while(!converged());
}