-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbacktransform_tpdm.cc
311 lines (267 loc) · 12.2 KB
/
backtransform_tpdm.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
*@BEGIN LICENSE
*
* v2RDM-CASSCF, a plugin to:
*
* Psi4: an open-source 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 "backtransform_tpdm.h"
#include <psi4/libtrans/integraltransform.h>
#include <psi4/libpsio/psio.hpp>
#include <psi4/libciomr/libciomr.h>
#include <psi4/libqt/qt.h>
#include <psi4/libiwl/iwl.hpp>
#include <psi4/libtrans/integraltransform_functors.h>
#include <psi4/psifiles.h>
#include <psi4/libtrans/mospace.h>
#define EXTERN
//#include <libdpd/dpd.gbl>
using namespace psi;
TPDMBackTransform::TPDMBackTransform(SharedWavefunction wfn,
SpaceVec spaces,
TransformationType transformationType,
OutputType outputType,
MOOrdering moOrdering,
FrozenOrbitals frozenOrbitals,
bool init):
IntegralTransform(wfn,spaces,transformationType,outputType,moOrdering,frozenOrbitals,init)
{
}
TPDMBackTransform::~TPDMBackTransform()
{
}
// repack the unrestricted MO TPDMs into DPD buffers
// to prepare them for the back transformation in deriv()
// this function is a modifield version of
// IntegralTransform::presort_mo_tpdm_unrestricted()
// from libtrans/integraltransform_sort_mo_tpdm.cc
void TPDMBackTransform::presort_mo_tpdm_unrestricted(){
check_initialized();
int currentActiveDPD = psi::dpd_default;
dpd_set_default(myDPDNum_);
outfile->Printf( "\n");
outfile->Printf( " ==> Back-transforming MO-basis TPDMs <==\n");
outfile->Printf( "\n");
dpdfile4 I;
psio_->open(PSIF_TPDM_PRESORT, PSIO_OPEN_NEW);
global_dpd_->file4_init(&I, PSIF_TPDM_PRESORT, 0, DPD_ID("[A>=A]+"), DPD_ID("[A>=A]+"), "MO TPDM (AA|AA)");
size_t memoryd = memory_ / sizeof(double);
int nump = 0, numq = 0;
for(int h=0; h < nirreps_; ++h){
nump += I.params->ppi[h];
numq += I.params->qpi[h];
}
int **bucketMap = init_int_matrix(nump, numq);
/* Room for one bucket to begin with */
int **bucketOffset = (int **) malloc(sizeof(int *));
bucketOffset[0] = init_int_array(nirreps_);
int **bucketRowDim = (int **) malloc(sizeof(int *));
bucketRowDim[0] = init_int_array(nirreps_);
int **bucketSize = (int **) malloc(sizeof(int *));
bucketSize[0] = init_int_array(nirreps_);
/* Figure out how many passes we need and where each p,q goes */
int nBuckets = 1;
size_t coreLeft = memoryd;
psio_address next;
for(int h = 0; h < nirreps_; ++h){
size_t rowLength = (size_t) I.params->coltot[h^(I.my_irrep)];
for(int row=0; row < I.params->rowtot[h]; ++row) {
if(coreLeft >= rowLength){
coreLeft -= rowLength;
bucketRowDim[nBuckets-1][h]++;
bucketSize[nBuckets-1][h] += rowLength;
} else {
nBuckets++;
coreLeft = memoryd - rowLength;
/* Make room for another bucket */
bucketOffset = (int **) realloc((void *) bucketOffset,
nBuckets * sizeof(int *));
bucketOffset[nBuckets-1] = init_int_array(nirreps_);
bucketOffset[nBuckets-1][h] = row;
bucketRowDim = (int **) realloc((void *) bucketRowDim,
nBuckets * sizeof(int *));
bucketRowDim[nBuckets-1] = init_int_array(nirreps_);
bucketRowDim[nBuckets-1][h] = 1;
bucketSize = (int **) realloc((void *) bucketSize,
nBuckets * sizeof(int *));
bucketSize[nBuckets-1] = init_int_array(nirreps_);
bucketSize[nBuckets-1][h] = rowLength;
}
int p = I.params->roworb[h][row][0];
int q = I.params->roworb[h][row][1];
bucketMap[p][q] = nBuckets - 1;
}
}
outfile->Printf( " Sorting File: %s nbuckets = %d\n", I.label, nBuckets);
// The alpha - alpha spin case
next = PSIO_ZERO;
for(int n=0; n < nBuckets; ++n) { /* nbuckets = number of passes */
/* Prepare target matrix */
for(int h=0; h < nirreps_; h++) {
I.matrix[h] = block_matrix(bucketRowDim[n][h], I.params->coltot[h]);
}
IWL *iwl = new IWL(psio_.get(), PSIF_MO_AA_TPDM, tolerance_, 1, 0);
DPDFillerFunctor aaDpdFiller(&I,n,bucketMap,bucketOffset, true, true);
Label *lblptr = iwl->labels();
Value *valptr = iwl->values();
int lastbuf;
/* Now run through the IWL buffers */
do{
iwl->fetch();
lastbuf = iwl->last_buffer();
for(int index = 0; index < iwl->buffer_count(); ++index){
int labelIndex = 4*index;
int p = (int) lblptr[labelIndex++];//aCorrToPitzer_[abs((int) lblptr[labelIndex++])];
int q = (int) lblptr[labelIndex++];//aCorrToPitzer_[(int) lblptr[labelIndex++]];
int r = (int) lblptr[labelIndex++];//aCorrToPitzer_[(int) lblptr[labelIndex++]];
int s = (int) lblptr[labelIndex++];//aCorrToPitzer_[(int) lblptr[labelIndex++]];
double value = (double) valptr[index];
aaDpdFiller(p,q,r,s,value);
} /* end loop through current buffer */
} while(!lastbuf); /* end loop over reading buffers */
iwl->set_keep_flag(1);
delete iwl;
for(int h=0; h < nirreps_; ++h) {
if(bucketSize[n][h])
psio_->write(I.filenum, I.label, (char *) I.matrix[h][0],
bucketSize[n][h]*((long int) sizeof(double)), next, &next);
free_block(I.matrix[h]);
}
} /* end loop over buckets/passes */
/* Get rid of the input integral file */
psio_->open(PSIF_MO_AA_TPDM, PSIO_OPEN_OLD);
psio_->close(PSIF_MO_AA_TPDM, keepIwlMoTpdm_);
// The alpha - beta spin case
global_dpd_->file4_init(&I, PSIF_TPDM_PRESORT, 0, DPD_ID("[A>=A]+"), DPD_ID("[a>=a]+"), "MO TPDM (AA|aa)");
outfile->Printf( " Sorting File: %s nbuckets = %d\n", I.label, nBuckets);
next = PSIO_ZERO;
for(int n=0; n < nBuckets; ++n) { /* nbuckets = number of passes */
/* Prepare target matrix */
for(int h=0; h < nirreps_; h++) {
I.matrix[h] = block_matrix(bucketRowDim[n][h], I.params->coltot[h]);
}
IWL *iwl = new IWL(psio_.get(), PSIF_MO_AB_TPDM, tolerance_, 1, 0);
DPDFillerFunctor abDpdFiller(&I,n,bucketMap,bucketOffset, true, false);
Label *lblptr = iwl->labels();
Value *valptr = iwl->values();
int lastbuf;
/* Now run through the IWL buffers */
do{
iwl->fetch();
lastbuf = iwl->last_buffer();
for(int index = 0; index < iwl->buffer_count(); ++index){
int labelIndex = 4*index;
int p = (int) lblptr[labelIndex++];//aCorrToPitzer_[abs((int) lblptr[labelIndex++])];
int q = (int) lblptr[labelIndex++];//aCorrToPitzer_[(int) lblptr[labelIndex++]];
int r = (int) lblptr[labelIndex++];//bCorrToPitzer_[(int) lblptr[labelIndex++]];
int s = (int) lblptr[labelIndex++];//bCorrToPitzer_[(int) lblptr[labelIndex++]];
double value = (double) valptr[index];
// Check:
// outfile->Printf("\t%4d %4d %4d %4d = %20.10f\n", p, q, r, s, value);
abDpdFiller(p,q,r,s,value);
} /* end loop through current buffer */
} while(!lastbuf); /* end loop over reading buffers */
iwl->set_keep_flag(1);
delete iwl;
for(int h=0; h < nirreps_; ++h) {
if(bucketSize[n][h])
psio_->write(I.filenum, I.label, (char *) I.matrix[h][0],
bucketSize[n][h]*((long int) sizeof(double)), next, &next);
free_block(I.matrix[h]);
}
} /* end loop over buckets/passes */
/* Get rid of the input integral file */
psio_->open(PSIF_MO_AB_TPDM, PSIO_OPEN_OLD);
psio_->close(PSIF_MO_AB_TPDM, keepIwlMoTpdm_);
// The beta - beta spin case
global_dpd_->file4_init(&I, PSIF_TPDM_PRESORT, 0, DPD_ID("[a>=a]+"), DPD_ID("[a>=a]+"), "MO TPDM (aa|aa)");
outfile->Printf( " Sorting File: %s nbuckets = %d\n", I.label, nBuckets);
outfile->Printf( "\n");
next = PSIO_ZERO;
for(int n=0; n < nBuckets; ++n) { /* nbuckets = number of passes */
/* Prepare target matrix */
for(int h=0; h < nirreps_; h++) {
I.matrix[h] = block_matrix(bucketRowDim[n][h], I.params->coltot[h]);
}
IWL *iwl = new IWL(psio_.get(), PSIF_MO_BB_TPDM, tolerance_, 1, 0);
DPDFillerFunctor bbDpdFiller(&I,n,bucketMap,bucketOffset, true, true);
Label *lblptr = iwl->labels();
Value *valptr = iwl->values();
int lastbuf;
/* Now run through the IWL buffers */
do{
iwl->fetch();
lastbuf = iwl->last_buffer();
for(int index = 0; index < iwl->buffer_count(); ++index){
int labelIndex = 4*index;
int p = (int) lblptr[labelIndex++];//bCorrToPitzer_[abs((int) lblptr[labelIndex++])];
int q = (int) lblptr[labelIndex++];//bCorrToPitzer_[(int) lblptr[labelIndex++]];
int r = (int) lblptr[labelIndex++];//bCorrToPitzer_[(int) lblptr[labelIndex++]];
int s = (int) lblptr[labelIndex++];//bCorrToPitzer_[(int) lblptr[labelIndex++]];
double value = (double) valptr[index];
bbDpdFiller(p,q,r,s,value);
} /* end loop through current buffer */
} while(!lastbuf); /* end loop over reading buffers */
iwl->set_keep_flag(1);
delete iwl;
for(int h=0; h < nirreps_; ++h) {
if(bucketSize[n][h])
psio_->write(I.filenum, I.label, (char *) I.matrix[h][0],
bucketSize[n][h]*((long int) sizeof(double)), next, &next);
free_block(I.matrix[h]);
}
} /* end loop over buckets/passes */
/* Get rid of the input integral file */
psio_->open(PSIF_MO_BB_TPDM, PSIO_OPEN_OLD);
psio_->close(PSIF_MO_BB_TPDM, keepIwlMoTpdm_);
free_int_matrix(bucketMap);
for(int n=0; n < nBuckets; ++n) {
free(bucketOffset[n]);
free(bucketRowDim[n]);
free(bucketSize[n]);
}
free(bucketOffset);
free(bucketRowDim);
free(bucketSize);
dpd_set_default(currentActiveDPD);
tpdmAlreadyPresorted_ = true;
global_dpd_->file4_close(&I);
psio_->close(PSIF_TPDM_PRESORT, 1);
}
void
TPDMBackTransform::backtransform_density()
{
check_initialized();
// This limitation can be remedied by accounting for the fact that Pitzer orbital numbering is not
// dense, so certain quantities must be alloc'd for the full MO space. It's no limitation, though
if(frozenOrbitals_ != FrozenOrbitals::None)
throw SanityCheckError("No orbitals can be frozen in density matrix transformations\n",
__FILE__, __LINE__);
// The full MO space must be in the list of spaces used, let's check
bool allFound = false;
for(int i = 0; i < spacesUsed_.size(); ++i)
if(spacesUsed_[i] == MOSPACE_ALL) allFound = true;
if(!allFound)
throw PSIEXCEPTION("MOSpace::all must be amongst the spaces passed "
"to the integral object's constructor");
backtransform_tpdm_unrestricted();
}