forked from loriab/v2rdm_casscf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiis.cc
247 lines (205 loc) · 7.85 KB
/
diis.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
*@BEGIN LICENSE
*
* v2RDM-CASSCF, a plugin to:
*
* PSI4: an ab initio quantum chemistry software package
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (c) 2014, The Florida State University. All rights reserved.
*
*@END LICENSE
*
*/
#include"v2rdm_solver.h"
#include<../bin/fnocc/blas.h>
#include<libqt/qt.h>
#include<psifiles.h>
using namespace psi;
using namespace fnocc;
/*================================================================
diis functions
================================================================*/
namespace psi{ namespace v2rdm_casscf{
// TODO: replace junk1/junk2
void v2RDMSolver::DIIS(double*c,long int nvec,int replace_diis_iter){
long int nvar = nvec+1;
long int * ipiv = (long int*)malloc(nvar*sizeof(long int));
double * temp = (double*)malloc(sizeof(double)*maxdiis_*maxdiis_);
double * A = (double*)malloc(sizeof(double)*nvar*nvar);
double * B = (double*)malloc(sizeof(double)*nvar);
memset((void*)A,'\0',nvar*nvar*sizeof(double));
memset((void*)B,'\0',nvar*sizeof(double));
B[nvec] = -1.;
char*evector=(char*)malloc(1000*sizeof(char));
boost::shared_ptr<PSIO> psio(new PSIO());
psio->open(PSIF_DCC_EVEC,PSIO_OPEN_OLD);
// add row to matrix, don't build the whole thing.
psio->read_entry(PSIF_DCC_EVEC,"error matrix",(char*)&temp[0],maxdiis_*maxdiis_*sizeof(double));
for (long int i = 0; i < nvec; i++){
for (long int j = 0; j < nvec; j++){
A[i*nvar+j] = temp[i*maxdiis_+j];
}
}
if (nvec <= 3) {
for (long int i = 0; i < nvec; i++) {
sprintf(evector,"evector%li",i+1);
psio->read_entry(PSIF_DCC_EVEC,evector,(char*)&junk1[0],dimdiis_*sizeof(double));
for (long int j = i; j < nvec; j++){
sprintf(evector,"evector%li",j+1);
psio->read_entry(PSIF_DCC_EVEC,evector,(char*)&junk2[0],dimdiis_*sizeof(double));
double sum = C_DDOT(dimdiis_,junk1,1,junk2,1);
A[i*nvar+j] = sum;
A[j*nvar+i] = sum;
}
}
}else {
long int i;
if (nvec<=maxdiis_ && diis_oiter_<=maxdiis_){
i = nvec - 1;
}
else{
i = replace_diis_iter - 1;
}
sprintf(evector,"evector%li",i+1);
psio->read_entry(PSIF_DCC_EVEC,evector,(char*)&junk1[0],dimdiis_*sizeof(double));
for (long int j = 0; j < nvec; j++){
sprintf(evector,"evector%li",j+1);
psio->read_entry(PSIF_DCC_EVEC,evector,(char*)&junk2[0],dimdiis_*sizeof(double));
double sum = C_DDOT(dimdiis_,junk1,1,junk2,1);
A[i*nvar+j] = sum;
A[j*nvar+i] = sum;
}
}
long int j = nvec;
for (long int i = 0; i < nvar; i++){
A[j*nvar+i] = -1.0;
A[i*nvar+j] = -1.0;
}
A[nvar*nvar-1] = 0.;
// save matrix for next iteration
for (long int i = 0; i < nvec; i++){
for (long int j = 0; j < nvec; j++){
temp[i*maxdiis_+j] = A[i*nvar+j];
}
}
psio->write_entry(PSIF_DCC_EVEC,"error matrix",(char*)&temp[0],maxdiis_*maxdiis_*sizeof(double));
free(temp);
psio->close(PSIF_DCC_EVEC,1);
free(evector);
long int nrhs,lda,ldb,info;
nrhs = 1;
lda = ldb = nvar;
info = 0;
DGESV(nvar,nrhs,A,lda,ipiv,B,ldb,info);
C_DCOPY(nvec,B,1,c,1);
free(A);
free(B);
free(ipiv);
psio.reset();
}
void v2RDMSolver::DIIS_WriteOldVector(long int iter,int diis_iter,int replace_diis_iter){
char*oldvector=(char*)malloc(1000*sizeof(char));
if (diis_iter<=maxdiis_ && iter<=maxdiis_){
sprintf(oldvector,"oldvector%i",diis_iter);
}
else{
sprintf(oldvector,"oldvector%i",replace_diis_iter);
}
boost::shared_ptr<PSIO> psio(new PSIO());
if (diis_iter==0) {
psio->open(PSIF_DCC_OVEC,PSIO_OPEN_NEW);
}else {
psio->open(PSIF_DCC_OVEC,PSIO_OPEN_OLD);
}
psio_address addr;
addr = PSIO_ZERO;
psio->write(PSIF_DCC_OVEC,oldvector,(char*)&rx->pointer()[0],dimdiis_*sizeof(double),addr,&addr);
psio->write(PSIF_DCC_OVEC,oldvector,(char*)&rz->pointer()[0],dimdiis_*sizeof(double),addr,&addr);
psio->close(PSIF_DCC_OVEC,1);
psio.reset();
free(oldvector);
}
void v2RDMSolver::DIIS_WriteErrorVector(int diis_iter,int replace_diis_iter,int iter){
char*evector = (char*)malloc(1000*sizeof(char));
if (diis_iter<=maxdiis_ && iter<=maxdiis_){
sprintf(evector,"evector%i",diis_iter);
}
else{
sprintf(evector,"evector%i",replace_diis_iter);
}
boost::shared_ptr<PSIO> psio(new PSIO());
if (diis_iter==0) {
psio->open(PSIF_DCC_EVEC,PSIO_OPEN_NEW);
double * temp = (double*)malloc(maxdiis_*maxdiis_*sizeof(double));
memset((void*)temp,'\0',maxdiis_*maxdiis_*sizeof(double));
psio->write_entry(PSIF_DCC_EVEC,"error matrix",(char*)&temp[0],maxdiis_*maxdiis_*sizeof(double));
free(temp);
}
else {
psio->open(PSIF_DCC_EVEC,PSIO_OPEN_OLD);
}
psio_address addr;
addr = PSIO_ZERO;
psio->write(PSIF_DCC_EVEC,evector,(char*)&rx_error->pointer()[0],dimdiis_*sizeof(double),addr,&addr);
psio->write(PSIF_DCC_EVEC,evector,(char*)&rz_error->pointer()[0],dimdiis_*sizeof(double),addr,&addr);
psio->close(PSIF_DCC_EVEC,1);
psio.reset();
free(evector);
}
void v2RDMSolver::DIIS_Extrapolate(int diis_iter,int&replace_diis_iter){
char*oldvector;
oldvector=(char*)malloc(1000*sizeof(char));
boost::shared_ptr<PSIO> psio(new PSIO());
psio->open(PSIF_DCC_OVEC,PSIO_OPEN_OLD);
psio_address addr;
memset((void*)rx->pointer(),'\0',dimdiis_*sizeof(double));
memset((void*)rz->pointer(),'\0',dimdiis_*sizeof(double));
long int max = diis_iter;
if (max > maxdiis_) max = maxdiis_;
double min = 1.e9;
for (long int j=1; j<=max; j++){
addr = PSIO_ZERO;
sprintf(oldvector,"oldvector%li",j);
psio->read(PSIF_DCC_OVEC,oldvector,(char*)&junk1[0],dimdiis_*sizeof(double),addr,&addr);
C_DAXPY(dimdiis_,diisvec_[j-1],junk1,1,rx->pointer(),1);
psio->read(PSIF_DCC_OVEC,oldvector,(char*)&junk1[0],dimdiis_*sizeof(double),addr,&addr);
C_DAXPY(dimdiis_,diisvec_[j-1],junk1,1,rz->pointer(),1);
//if ( fabs( diisvec[j-1] ) < min ) {
// min = fabs( diisvec[j-1] );
// replace_diis_iter = j;
//}
}
psio->close(PSIF_DCC_OVEC,1);
free(oldvector);
// now, build x = rx^2, z = rz^2
double * x_p = x->pointer();
double * z_p = z->pointer();
double * rx_p = rx->pointer();
double * rz_p = rz->pointer();
// loop over each block of x/z
for (int i = 0; i < dimensions_.size(); i++) {
if ( dimensions_[i] == 0 ) continue;
int myoffset = 0;
for (int j = 0; j < i; j++) {
myoffset += dimensions_[j] * dimensions_[j];
}
F_DGEMM('n','n',dimensions_[i],dimensions_[i],dimensions_[i],1.0,rx_p+myoffset,dimensions_[i],rx_p+myoffset,dimensions_[i],0.0,x_p+myoffset,dimensions_[i]);
F_DGEMM('n','n',dimensions_[i],dimensions_[i],dimensions_[i],1.0,rz_p+myoffset,dimensions_[i],rz_p+myoffset,dimensions_[i],0.0,z_p+myoffset,dimensions_[i]);
}
psio.reset();
}
}}