-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcpdft_solver.cc
3683 lines (2802 loc) · 137 KB
/
mcpdft_solver.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @BEGIN LICENSE
*
* mcpdft by Psi4 Developer, a plugin to:
*
* Psi4: an open-source quantum chemistry software package
*
* Copyright (c) 2007-2016 The Psi4 Developers.
*
* The copyrights for code used from other parties are included in
* the corresponding files.
*
* 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.
*
* @END LICENSE
*/
#ifdef _OPENMP
#include<omp.h>
#else
#define omp_get_wtime() ( (double)clock() / CLOCKS_PER_SEC )
#define omp_get_max_threads() 1
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <tuple>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include "psi4/libpsi4util/libpsi4util.h"
#include "psi4/libqt/qt.h"
// jk object
#include "psi4/libfock/jk.h"
// for dft
#include "psi4/libfock/v.h"
#include "psi4/libfunctional/superfunctional.h"
// for grid
#include "psi4/libfock/points.h"
#include "psi4/libfock/cubature.h"
#include "psi4/psi4-dec.h"
#include "psi4/liboptions/liboptions.h"
#include "psi4/libpsio/psio.hpp"
#include "psi4/libmints/wavefunction.h"
#include "psi4/libmints/mintshelper.h"
#include "psi4/libmints/matrix.h"
#include "psi4/libmints/vector.h"
#include "psi4/libmints/basisset.h"
#include "psi4/libmints/gshell.h"
#include "psi4/libmints/molecule.h"
#include "psi4/lib3index/dftensor.h"
#include "psi4/libqt/qt.h"
// for potential object
#include "psi4/libfock/v.h"
#include "psi4/libfunctional/superfunctional.h"
#include "psi4/libscf_solver/hf.h"
// mcpdft
#include "mcpdft_solver.h"
// blas
#include "blas.h"
#include "blas_mangle.h"
// for reading 2RDM
#include "psi4/psi4-dec.h"
#include <psi4/psifiles.h>
#include <psi4/libiwl/iwl.h>
#include <psi4/libpsio/psio.hpp>
#include <psi4/libtrans/integraltransform.h>
#include <psi4/libpsi4util/PsiOutStream.h>
using namespace psi;
using namespace fnocc;
namespace psi{ namespace mcpdft {
// the MCPDFTSolver class derives from the Wavefunction class and inherits its members
MCPDFTSolver::MCPDFTSolver(std::shared_ptr<Wavefunction> reference_wavefunction,Options & options_):
Wavefunction(options_){
reference_wavefunction_ = reference_wavefunction;
common_init();
}
MCPDFTSolver::~MCPDFTSolver() {
}
// initialize members of the MCPDFTSolver class
void MCPDFTSolver::common_init() {
is_low_memory_ = false;
reference_energy_ = Process::environment.globals["V2RDM TOTAL ENERGY"];
if ( options_.get_str("MCPDFT_METHOD") == "1DH_MCPDFT"
|| options_.get_str("MCPDFT_METHOD") == "RS1DH_MCPDFT"
|| options_.get_str("MCPDFT_METHOD") == "LS1DH_MCPDFT" ) {
// calculating mp2 energy for double-hybrids
mp2_corr_energy_ = Process::environment.globals["MP2 CORRELATION ENERGY"];
// WArning message about the reference WFN for Double-Hybrid methods
outfile->Printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
outfile->Printf(" ! Caution: For double-hybrid PDFT methods, the reference !\n");
outfile->Printf(" ! wavefunction should be a single Slater determinant. !\n");
outfile->Printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
}
shallow_copy(reference_wavefunction_);
// number of alpha electrons
nalpha_ = reference_wavefunction_->nalpha();
// number of beta electrons
nbeta_ = reference_wavefunction_->nbeta();
// number of alpha electrons per irrep
nalphapi_ = reference_wavefunction_->nalphapi();
// number of beta electrons per irrep
nbetapi_ = reference_wavefunction_->nbetapi();
// number of frozen core orbitals per irrep
frzcpi_ = reference_wavefunction_->frzcpi();
// number of frozen virtual orbitals per irrep
frzvpi_ = reference_wavefunction_->frzvpi();
// number of molecular orbials per irrep
nmopi_ = reference_wavefunction_->nmopi();
// number of symmetry orbials per irrep
nsopi_ = reference_wavefunction_->nsopi();
// number of irreducible representations
nirrep_ = reference_wavefunction_->nirrep();
// total number of symmetry orbitals
nso_ = reference_wavefunction_->nso();
// total number of molecular orbitals
nmo_ = reference_wavefunction_->nmo();
// if ( options_.get_str("MCPDFT_METHOD") == "1DH_MCPDFT"
// || options_.get_str("MCPDFT_METHOD") == "RS1DH_MCPDFT" ) {
// int nrstdocc = 0;
// int nelec = 0;
// for (int h = 0; h < nirrep_; h++){
// nrstdocc += doccpi_[h];
// nelec += nalphapi_[h];
// nelec += nbetapi_[h];
// printf("docc nelec = %i %i\n",nrstdocc,nelec);
// }
// if (nelec != nrstdocc) throw PsiException("All electrons should be frozen for double-hybrids!\n \
// The reference wave function should be single-determinant.\n",__FILE__,__LINE__);
// }
// grab the molecule from the reference wave function
molecule_ = reference_wavefunction_->molecule();
// SO/MO transformation matrices
Ca_ = std::shared_ptr<Matrix>(reference_wavefunction_->Ca());
Cb_ = std::shared_ptr<Matrix>(reference_wavefunction_->Cb());
// overlap integrals
S_ = std::shared_ptr<Matrix>(reference_wavefunction_->S());
std::shared_ptr<Matrix> Sevec;
std::shared_ptr<Vector> Seval;
if ( (options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT") ) {
// allocate memory for eigenvectors and eigenvalues of the overlap matrix
Sevec = (std::shared_ptr<Matrix>) ( new Matrix(nso_,nso_) );
Seval = (std::shared_ptr<Vector>) ( new Vector(nso_) );
Sm1_ = (std::shared_ptr<Matrix>) ( new Matrix(nso_,nso_) );
// build S^(-1) matrix
S_->diagonalize(Sevec,Seval);
for (int mu = 0; mu < nso_; mu++) {
Sm1_->pointer()[mu][mu] = 1.0 / Seval->pointer()[mu] ;
}
// transform Sm1_ back to nonorthogonal basis
Sm1_->back_transform(Sevec);
}
// SO-basis Fock matrices
Fa_ = std::shared_ptr<Matrix>(reference_wavefunction_->Fa());
Fb_ = std::shared_ptr<Matrix>(reference_wavefunction_->Fb());
// SO-basis density matrices
Da_ = std::shared_ptr<Matrix>(reference_wavefunction_->Da());
Db_ = std::shared_ptr<Matrix>(reference_wavefunction_->Db());
// orbital energies
epsilon_a_ = std::make_shared<Vector>(nmopi_);
epsilon_a_->copy(*reference_wavefunction_->epsilon_a());
epsilon_b_ = std::make_shared<Vector>(nmopi_);
epsilon_b_->copy(*reference_wavefunction_->epsilon_b());
// orbital energies
occupation_a_= std::shared_ptr<Vector>(new Vector(nmopi_));
occupation_b_= std::shared_ptr<Vector>(new Vector(nmopi_));
for (int h = 0; h < nirrep_; h++) {
for (int i = 0; i < nalphapi_[h]; i++) {
occupation_a_->set(h, i, 1.0);
}
for (int i = 0; i < nbetapi_[h]; i++) {
occupation_b_->set(h, i, 1.0);
}
}
// occupation_a_->print();
// set the wavefunction name
name_ = "DFT";
// restricted orbitals, unrestricted rdms
same_a_b_orbs_ = true;
same_a_b_dens_ = false;
// symmetry of orbitals:
symmetry_ = (int*)malloc(nmo_*sizeof(int));
memset((void*)symmetry_,'\0',nmo_*sizeof(int));
int count = 0;
for (int h = 0; h < nirrep_; h++) {
for (int i = 0; i < nsopi_[h]; i++) {
symmetry_[count++] = h;
}
}
// pitzer offset
pitzer_offset_ = (int*)malloc(nirrep_*sizeof(int));
count = 0;
for (int h = 0; h < nirrep_; h++) {
pitzer_offset_[h] = count;
count += nsopi_[h];
}
// evaluate basis function values on a grid:
scf::HF* scfwfn = (scf::HF*)reference_wavefunction_.get();
std::shared_ptr<SuperFunctional> functional = scfwfn->functional();
potential_ = scfwfn->V_potential();
// phi matrix (real-space <- AO basis mapping)
// since grid is stored in blocks, we need to build a full phi matrix
// from the individual blocks:
std::shared_ptr<BasisSet> primary = reference_wavefunction_->get_basisset("ORBITAL");
int nao = primary->nbf();
std::shared_ptr<Matrix> Da_ao (new Matrix(nao,nao));
std::shared_ptr<Matrix> Db_ao (new Matrix(nao,nao));
points_func_ = potential_->properties()[0];
points_func_->set_pointers(Da_ao,Db_ao);
// determine number of grid points
int nblocks = potential_->nblocks();
phi_points_ = 0;
max_functions_ = 0;
max_points_ = 0;
for (int myblock = 0; myblock < nblocks; myblock++) {
std::shared_ptr<BlockOPoints> block = potential_->get_block(myblock);
points_func_->compute_points(block);
int npoints = block->npoints();
phi_points_ += npoints;
const std::vector<int>& function_map = block->functions_local_to_global();
int nlocal = function_map.size();
if ( nlocal > max_functions_ ) max_functions_ = nlocal;
if ( npoints > max_points_ ) max_points_ = npoints;
}
// what is the derivative level?
deriv_ = functional->deriv();
is_gga_ = functional->is_gga();
is_meta_ = functional->is_meta();
is_unpolarized_ = functional->is_unpolarized();
std::vector < std::shared_ptr<Matrix> > JK = BuildJK();
double caa = Da_->vector_dot(JK[0]);
double cab = Da_->vector_dot(JK[1]);
double cba = Db_->vector_dot(JK[0]);
double cbb = Db_->vector_dot(JK[1]);
coulomb_energy_ = 0.5 * ( caa + cab + cba + cbb );
// hf_ex_energy_ = 0.0;
// lr_ex_energy_ = 0.0;
if ( (options_.get_str("MCPDFT_METHOD") != "MCPDFT") ) {
// HF exchange energy should be computed using K object
double kaa = Da_->vector_dot(JK[2]);
double kbb = Db_->vector_dot(JK[3]);
hf_ex_energy_ = -0.5 * (kaa + kbb);
if ( (options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT") ) {
std::shared_ptr<Matrix> Q_ao_1 (new Matrix(nso_,nso_));
std::shared_ptr<Matrix> Q_ao_2 (new Matrix(nso_,nso_));
std::shared_ptr<Matrix> Q_temp (new Matrix(nso_,nso_));
std::shared_ptr<Matrix> D_tot (new Matrix(Da_));
D_tot->add(Db_);
std::shared_ptr<Matrix> K_tot (new Matrix(JK[2]));
K_tot->add(JK[3]);
Q_temp->gemm(false,false,1.0,Sm1_,K_tot,0.0);
Q_ao_1->gemm(false,false,0.5,Q_temp,D_tot,0.0);
Q_temp->gemm(false,false,1.0,D_tot,K_tot,0.0);
Q_ao_2->gemm(false,false,0.5,Q_temp,Sm1_,0.0);
Q_ao_ = std::shared_ptr<Matrix>(new Matrix(Q_ao_1));
Q_ao_->add(Q_ao_2);
}
if ( (options_.get_str("MCPDFT_METHOD") != "1H_MCPDFT")
&& (options_.get_str("MCPDFT_METHOD") != "LH_MCPDFT")
&& (options_.get_str("MCPDFT_METHOD") != "1DH_MCPDFT") ) {
// long range (LR) exchange energy calculated using JK object
double wkaa = Da_->vector_dot(JK[4]);
double wkbb = Db_->vector_dot(JK[5]);
lr_ex_energy_ = -0.5 * (wkaa + wkbb);
}
}
/* ==============================================
estimating the memory requirement for MCPDFT
============================================== */
outfile->Printf("\n");
outfile->Printf(" ==> Memory requirements <==\n");
outfile->Printf("\n");
// memory is from process::environment
memory_ = Process::environment.get_memory();
// assume 500 MB is already allocated by psi4
long int n_extra = 500 * 1024 * 1024 / 8;
long int n_mem = n_extra;
long int available_mem = 0;
long int n_init = 0;
long int n_phiAO = 0;
long int n_grids = 0;
long int n_transf = 0;
long int n_rho = 0;
long int n_pi = 0;
long int n_R = 0;
long int n_transl = 0;
long int n_df = 0;
// default basic vectors and matrices initialized using common_init()
n_init = nso_ * nso_; // S_ matrix
n_init += 2 * nso_ * nso_; // Ca_ and Cb_ matrices
n_init += 2 * nso_ * nso_; // Fa_ and Fb_ matrices
n_init += 2 * nmo_; // epsilon_a_ and epsilon_b_ vectors
n_mem += n_init;
// phiAO, phiAO_x, phiAO_y, phiAO_z
n_phiAO = phi_points_ * nso_;
if ( is_gga_ || is_meta_ ) {
n_phiAO += 3 * phi_points_ * nso_;
}
n_mem += n_phiAO;
// required memory for grids x, y, and z and weights w vectors
n_grids = 4 * phi_points_;
n_mem += n_grids;
// memory needed for AO->MO transformation
n_transf = nirrep_ * phi_points_; // phi_points_list vector
n_transf += phi_points_ * nso_; // super_phi_ matrix
n_transf += nso_ * nso_; // AO2SO matrix in TransformPhiMatrixAOMO()
n_transf += phi_points_ * nso_; // temporary matrix called three times in TransformPhiMatrixAOMO()
if ( is_gga_ || is_meta_ ) {
n_transf += 3 * phi_points_ * nso_; // for super_phi_x_, _y_ and _z_ gradient matrices
}
n_mem += n_transf;
// memory needed for storing rho and rho' matrices
n_rho = 3 * phi_points_; // rho_a_, rho_b_ and rho_ vectors
if ( is_gga_ || is_meta_ ) {
n_rho += 3 * phi_points_; // rho_a_x_, rho_a_y_ and rho_a_z_ gradient vectors
n_rho += 3 * phi_points_; // rho_b_x_, rho_b_y_ and rho_b_z_ gradient vectors
n_rho += 3 * phi_points_; // sigma_aa_, sigma_ab_ and sigma_bb_ vectors
}
n_mem += n_rho;
// memory needed for storing pi vector
n_pi = phi_points_; // pi_ vector
if ( is_gga_ || is_meta_ ) {
n_pi += 3 * phi_points_; // pi_x_, pi_y_ and pi_z_ gradient vectors
}
n_mem += n_pi;
// memory needed for storing on-top ratio vector
n_R = phi_points_;
n_mem += n_R;
// memory needed for (full-) translation step
n_transl = 2 * phi_points_; // (f)tr_rho_a_, (f)tr_rho_b_ vectors
if ( is_gga_ || is_meta_ ) {
n_transl += 3 * phi_points_; // (f)tr_rho_a_x_, (f)tr_rho_a_y_ and (f)tr_rho_a_z_ gradient vectors
n_transl += 3 * phi_points_; // (f)tr_rho_b_x_, (f)tr_rho_b_y_ and (f)tr_rho_b_z_ gradient vectors
n_transl += 3 * phi_points_; // (f)tr_sigma_aa_, (f)tr_sigma_ab_ and (f)tr_sigma_bb_ vectors
}
n_mem += n_transl;
if ( n_mem * 8.0 > (double)memory_ ) {
outfile->Printf("\n");
outfile->Printf(" <<< Warning >>> \n");
outfile->Printf("\n");
outfile->Printf(" Not enough memory. Switching to low-memory algorithm.\n");
outfile->Printf(" For optimal performance, increase the available memory\n");
outfile->Printf(" by at least %7.2lf mb.\n",(8.0 * n_mem - memory_)/1024.0/1024.0);
outfile->Printf("\n");
is_low_memory_ = true;
n_mem -= n_transf;
n_mem -= n_phiAO;
if ( n_mem * 8.0 > (double)memory_ ) {
outfile->Printf("\n");
outfile->Printf(" Wow. On second thought, there isn't even enough memory for the low-memory algorithm!\n");
outfile->Printf("\n");
throw PsiException("Not enough memory.",__FILE__,__LINE__); fflush(stdout);
}
}
outfile->Printf(" =========================================================\n");
outfile->Printf(" memory specified by the user: %7.2lf MB\n",(double)memory_ / 1024.0 / 1024.0);
outfile->Printf(" ---------------------------------------------------------\n");
outfile->Printf(" initialization: %7.2lf MB\n",n_init * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" grid-points and weights: %7.2lf MB\n",n_grids * sizeof(double) / 1024.0 / 1024.0);
if ( !is_low_memory_ ) {
outfile->Printf(" phi & phi' (AO): %7.2lf MB\n",n_phiAO * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" AO->SO transformation: %7.2lf MB\n",n_transf* sizeof(double) / 1024.0 / 1024.0);
}
outfile->Printf(" rho and rho': %7.2lf MB\n",n_rho * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" on-top pair density: %7.2lf MB\n",n_pi * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" on-top ratio: %7.2lf MB\n",n_R * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" translation step: %7.2lf MB\n",n_transl * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" extra: %7.2lf MB\n",n_extra * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" ---------------------------------------------------------\n");
outfile->Printf(" total memory for MCPDFT: %7.2lf MB\n",n_mem * sizeof(double) / 1024.0 / 1024.0);
outfile->Printf(" =========================================================\n");
// memory available after allocating all we need for MCPDFT (deleting the temporary matrices and vectors)
n_mem -= nirrep_ * phi_points_; // phi_points_list vector
n_mem -= nso_ * nso_; // AO2SO matrix in TransformPhiMatrixAOMO()
if ( !is_low_memory_ ) {
n_mem -= n_phiAO; // ao-basis phi matrices
n_mem -= phi_points_ * nso_; // temporary matrix called three times in TransformPhiMatrixAOMO()
}
available_memory_ = memory_ - n_mem * 8L;
outfile->Printf("\n");
outfile->Printf(" available memeory for building JK object: %7.2lf MB\n",(double)available_memory_ / 1024.0 / 1024.0);
outfile->Printf("\n");
grid_x_ = std::shared_ptr<Vector>(new Vector("GRID X",phi_points_));
grid_y_ = std::shared_ptr<Vector>(new Vector("GRID Y",phi_points_));
grid_z_ = std::shared_ptr<Vector>(new Vector("GRID Z",phi_points_));
grid_w_ = std::shared_ptr<Vector>(new Vector("GRID W",phi_points_));
if ( is_low_memory_ ) {
GetGridInfo();
}else {
outfile->Printf("\n");
outfile->Printf(" ==> Build Phi and Phi' matrices ..."); fflush(stdout);
std::shared_ptr<Matrix> super_phi_ao = std::shared_ptr<Matrix>(new Matrix("SUPER PHI (AO)",phi_points_,nso_));
std::shared_ptr<Matrix> super_phi_x_ao;
std::shared_ptr<Matrix> super_phi_y_ao;
std::shared_ptr<Matrix> super_phi_z_ao;
if ( is_gga_ || is_meta_ ) {
super_phi_x_ao = std::shared_ptr<Matrix>(new Matrix("SUPER PHI X (AO)",phi_points_,nso_));
super_phi_y_ao = std::shared_ptr<Matrix>(new Matrix("SUPER PHI Y (AO)",phi_points_,nso_));
super_phi_z_ao = std::shared_ptr<Matrix>(new Matrix("SUPER PHI Z (AO)",phi_points_,nso_));
}
// build phi matrix and derivative phi matrices (ao basis)
BuildPhiMatrixAO("PHI",super_phi_ao);
if ( is_gga_ || is_meta_ ) {
BuildPhiMatrixAO("PHI_X",super_phi_x_ao);
BuildPhiMatrixAO("PHI_Y",super_phi_y_ao);
BuildPhiMatrixAO("PHI_Z",super_phi_z_ao);
}
// transform the orbital label in phi matrix and derivative phi matrix to the MO basis
Dimension phi_points_list = Dimension(nirrep_,"phi_points");
for (int h = 0; h < nirrep_; h++) {
phi_points_list[h] = phi_points_;
}
super_phi_ = std::shared_ptr<Matrix>(new Matrix("SUPER PHI",phi_points_list,nsopi_));
TransformPhiMatrixAOMO(super_phi_ao,super_phi_);
if ( is_gga_ || is_meta_ ) {
super_phi_x_ = std::shared_ptr<Matrix>(new Matrix("SUPER PHI X",phi_points_list,nsopi_));
super_phi_y_ = std::shared_ptr<Matrix>(new Matrix("SUPER PHI Y",phi_points_list,nsopi_));
super_phi_z_ = std::shared_ptr<Matrix>(new Matrix("SUPER PHI Z",phi_points_list,nsopi_));
TransformPhiMatrixAOMO(super_phi_x_ao,super_phi_x_);
TransformPhiMatrixAOMO(super_phi_y_ao,super_phi_y_);
TransformPhiMatrixAOMO(super_phi_z_ao,super_phi_z_);
}
outfile->Printf(" Done. <==\n");
}
// calculating the exact energy desity function on real space
if ( (options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT") ) {
ex_exact_ = std::shared_ptr<Vector>(new Vector(phi_points_));
double * ex_exact_p = ex_exact_->pointer();
double ** Q_ao_p = Q_ao_->pointer();
double ** phi_ao_p = super_phi_ao_->pointer();
for (int p = 0; p < phi_points_; p++) {
double dum = 0.0;
for (int mu = 0; mu < nso_; mu++) {
for (int nu = 0; nu < nso_; nu++) {
dum += Q_ao_p[mu][nu] * phi_ao_p[p][mu] * phi_ao_p[p][nu];
}
}
ex_exact_p[p] = dum;
}
}
}// end of common_init()
void MCPDFTSolver::GetGridInfo() {
int nblocks = potential_->nblocks();
phi_points_ = 0;
for (int myblock = 0; myblock < nblocks; myblock++) {
std::shared_ptr<BlockOPoints> block = potential_->get_block(myblock);
points_func_->compute_points(block);
int npoints = block->npoints();
const std::vector<int>& function_map = block->functions_local_to_global();
int nlocal = function_map.size();
double ** phi = points_func_->basis_value("PHI")->pointer();
double * x = block->x();
double * y = block->y();
double * z = block->z();
double * w = block->w();
for (int p = 0; p < npoints; p++) {
grid_x_->pointer()[phi_points_ + p] = x[p];
grid_y_->pointer()[phi_points_ + p] = y[p];
grid_z_->pointer()[phi_points_ + p] = z[p];
grid_w_->pointer()[phi_points_ + p] = w[p];
}
phi_points_ += npoints;
}
}//end of GetGridInfo()
void MCPDFTSolver::BuildPhiMatrixAO(std::string phi_type, std::shared_ptr<Matrix> myphi) {
int nblocks = potential_->nblocks();
phi_points_ = 0;
for (int myblock = 0; myblock < nblocks; myblock++) {
std::shared_ptr<BlockOPoints> block = potential_->get_block(myblock);
points_func_->compute_points(block);
int npoints = block->npoints();
const std::vector<int>& function_map = block->functions_local_to_global();
int nlocal = function_map.size();
double ** phi = points_func_->basis_value(phi_type)->pointer();
double * x = block->x();
double * y = block->y();
double * z = block->z();
double * w = block->w();
// Doing some test to see everything including Pi etc is correct on
// Molcas' grid points through comparison.
// std::ifstream dataIn;
// dataIn.open("H2.grids_test");
//
// if (!dataIn)
// std::cout << "Error opening file.\n";
// else {
// int p = 0;
// while (!dataIn.eof()){
//
// dataIn >> x[p];
// dataIn >> y[p];
// dataIn >> z[p];
// p++;
// }
// }
// dataIn.close();
// for (int p = 0; p < npoints; p++) {
// outfile->Printf("\n y[");
// outfile->Printf("%d",p);
// outfile->Printf("] = %20.7lf\n",y[p]);
// }
for (int p = 0; p < npoints; p++) {
grid_x_->pointer()[phi_points_ + p] = x[p];
grid_y_->pointer()[phi_points_ + p] = y[p];
grid_z_->pointer()[phi_points_ + p] = z[p];
grid_w_->pointer()[phi_points_ + p] = w[p];
for (int nu = 0; nu < nlocal; nu++) {
int nug = function_map[nu];
myphi->pointer()[phi_points_ + p][nug] = phi[p][nu];
}
}
phi_points_ += npoints;
}
}
void MCPDFTSolver::TransformPhiMatrixAOMO(std::shared_ptr<Matrix> phi_in, std::shared_ptr<Matrix> phi_out) {
std::shared_ptr<Matrix> phi_temp (new Matrix(phi_out));
// grab AO->SO transformation matrix
std::shared_ptr<Matrix> ao2so = reference_wavefunction_->aotoso();
for (int h = 0; h < nirrep_; h++) {
if (nsopi_[h] != 0 )
F_DGEMM('n','n',nsopi_[h],phi_points_,nso_,1.0,ao2so->pointer(h)[0],nsopi_[h],phi_in->pointer()[0],nso_,0.0,phi_temp->pointer(h)[0],nsopi_[h]);
}
for (int h = 0; h < nirrep_; h++) {
if (nsopi_[h] != 0 )
F_DGEMM('n','n',nsopi_[h],phi_points_,nsopi_[h],1.0,Ca_->pointer(h)[0],nsopi_[h],phi_temp->pointer(h)[0],nsopi_[h],0.0,phi_out->pointer(h)[0],nsopi_[h]);
}
}
double MCPDFTSolver::compute_energy() {
/* ========================================================================================
* (1) Calculation of the range-separated global (double-)hybrid formalis is based
* on Eq. (12) of the reference: C. Kalai and J, Toulouse J. Chem. Phys. 148, 164105 (2018)
* (2) Calculation of linearly scaled 1-parameter double-hybrid (LS1DH) is based upon Eq.
* (10) of the manuscript: Toulouse J. et al., J. Chem. Phys. 135, 101102 (2011)
======================================================================================== */
// read 1- and 2-RDM from disk and build rho(r), rho'(r), pi(r), and pi'(r)
if ( options_.get_str("MCPDFT_REFERENCE") == "V2RDM" ) {
ReadOPDM();
ReadTPDM();
}else if ( options_.get_str("MCPDFT_REFERENCE") == "CI" ) {
if ( nirrep_ > 1 ) {
throw PsiException("error, MCPDFT_REFERENCE = CI only works with symmetry c1",__FILE__,__LINE__);
}
// read 1-RDM
ReadCIOPDM(Da_,"opdm_a.txt");
ReadCIOPDM(Db_,"opdm_b.txt");
// allocate memory 2-RDM
double * D2ab = (double*)malloc(nmo_*nmo_*nmo_*nmo_*sizeof(double));
memset((void*)D2ab,'\0',nmo_*nmo_*nmo_*nmo_*sizeof(double));
// read 2-RDM
ReadCITPDM(D2ab,"tpdm_ab.txt");
// build alpha- and beta-spin densities and gradients (already built for MCPDFT_REFERENCE = V2RDM)
outfile->Printf("\n");
outfile->Printf(" ==> Build Rho <== \n ");
BuildRho();
// build on-top pair density (already built for MCPDFT_REFERENCE = V2RDM)
outfile->Printf("\n");
outfile->Printf(" ==> Build Pi <==\n\n");
BuildPi(D2ab);
free(D2ab);
}else {
throw PsiException("invalid MCPDFT_REFERENCE type",__FILE__,__LINE__);
}
// build R(r) = 4 * Pi(r) / rho(r)
outfile->Printf(" ==> Build the on-top ratio R ...");
Build_R();
outfile->Printf(" Done. <==\n\n");
// translate the alpha and beta densities and their corresponding gradients
if ( options_.get_str("MCPDFT_TRANSLATION_TYPE") == "REGULAR") {
outfile->Printf(" ==> Regular translation of densities and/or density gradients ...\n");
Translate();
outfile->Printf(" ... Done. <==\n\n");
}else {
outfile->Printf(" ==> Full translation of densities and/or density gradients ...\n");
Fully_Translate();
outfile->Printf(" ... Done. <==\n\n");
}
// Build the local mixing function (LMF) f(r)
/*
if (options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT") {
outfile->Printf(" ==> Build the local mixing function f(r) ...");
BuildLMF();
outfile->Printf(" Done. <==\n\n");
}
*/
// calculate the on-top energy
outfile->Printf(" ==> Evaluate the on-top energy contribution <==\n");
outfile->Printf("\n");
// writing the QTAIM wfn file
//if ( options_.get_bool("WRITE_QTAIM_WFN") )
// WriteQTAIM(Ca_,Cb_,epsilon_a_,epsilon_b_,occupation_a_,occupation_b_,"aimpac.txt");
/* ===================================================================================================
calculate the complement short-range MCPDFT XC functional energy:
E = min(Psi->N) { <Psi| T + Wee_LR(w) + lambda * Wee_SR(w) + Vne |Psi> + E_HXC_(w,lambda)[rho,pi] }
=================================================================================================== */
// initialization of the hybrid (lambda) and range-separation (omega) parameters
double mcpdft_lambda = 0.0;
double mcpdft_omega = 0.0;
if ( (options_.get_str("MCPDFT_METHOD") != "MCPDFT")
&& (options_.get_str("MCPDFT_METHOD") != "LH_MCPDFT") ) {
if ( (options_.get_str("MCPDFT_METHOD") != "1H_MCPDFT")
&& (options_.get_str("MCPDFT_METHOD") != "1DH_MCPDFT") ) {
// extracting the omega value from input file (default w=0.0)
mcpdft_omega = options_.get_double("MCPDFT_OMEGA");
outfile->Printf(" ==> Range-separation parameter (omega) = %5.2lf ", mcpdft_omega);
outfile->Printf("<==\n");
// calculating the LR and SR two-electron energies
lr_Vee_energy_ = RangeSeparatedTEE("LR");
sr_Vee_energy_ = RangeSeparatedTEE("SR");
lr_hartree_energy_ = RangeSeparated_HF_TEE("LR");
sr_hartree_energy_ = RangeSeparated_HF_TEE("SR");
}
// extracting the lambda value from input file (default lambda=0.0)
mcpdft_lambda = options_.get_double("MCPDFT_LAMBDA");
outfile->Printf(" ==> Coupling parameter for hybrid MCPDFT (lambda) = %5.2lf ", mcpdft_lambda);
outfile->Printf("<==\n");
}
// computing the exchange and correlation density functional energies
double mcpdft_ex = 0.0;
double mcpdft_ec = 0.0;
if ( (options_.get_str("MCPDFT_METHOD") == "RS1H_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "RS1DH_MCPDFT") ) {
if ( options_.get_str("MCPDFT_FUNCTIONAL") == "WPBE" ) {
mcpdft_ex = EX_wPBE_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
mcpdft_ec = EC_PBE_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_ab_, tr_sigma_bb_);
}else if ( options_.get_str("MCPDFT_FUNCTIONAL") == "WBLYP" ) {
mcpdft_ex = EX_wB88_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
mcpdft_ec = EC_LYP_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_ab_, tr_sigma_bb_);
}else if ( options_.get_str("MCPDFT_FUNCTIONAL") == "WB97X" ) {
throw PsiException("Sorry! The requested range-separated functional has not yet been implemented!",__FILE__,__LINE__);
}else {
throw PsiException("Sorry! The requested range-separated functional has not yet been implemented!",__FILE__,__LINE__);
}
}else if ( (options_.get_str("MCPDFT_METHOD") == "MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "1H_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "1DH_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "LS1DH_MCPDFT") ) {
if ( options_.get_str("MCPDFT_FUNCTIONAL") == "SVWN" ) {
mcpdft_ex = EX_LSDA(tr_rho_a_, tr_rho_b_);
mcpdft_ec = EC_VWN3_RPA_III(tr_rho_a_, tr_rho_b_);
}else if ( options_.get_str("MCPDFT_FUNCTIONAL") == "BLYP" ) {
if ( options_.get_str("MCPDFT_METHOD") == "LH_MCPDFT" ) {
mcpdft_ex = Lh_EX_B88_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
}else{
mcpdft_ex = EX_B88_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
}
mcpdft_ec = EC_LYP_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_ab_, tr_sigma_bb_);
}else if ( options_.get_str("MCPDFT_FUNCTIONAL") == "PBE"
|| options_.get_str("MCPDFT_FUNCTIONAL") == "REVPBE" ) {
mcpdft_ex = EX_PBE_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
mcpdft_ec = EC_PBE_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_ab_, tr_sigma_bb_);
}else if ( options_.get_str("MCPDFT_FUNCTIONAL") == "BOP" ) {
mcpdft_ex = EX_B88_I(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
mcpdft_ec = EC_B88_OP(tr_rho_a_, tr_rho_b_, tr_sigma_aa_, tr_sigma_bb_);
}else{
throw PsiException("Please choose a proper functional for MCDPFT",__FILE__,__LINE__);
}
}else {
throw PsiException("Please choose a valid method for MCDPFT",__FILE__,__LINE__);
}
// evaluate the kinetic, potential, and coulomb energies
outfile->Printf(" ==> Evaluate kinetic, potential, and coulomb energies <==\n");
outfile->Printf("\n");
// one-electron terms:
std::shared_ptr<MintsHelper> mints(new MintsHelper(reference_wavefunction_));
//Da_->print();
// SharedMatrix ha (new Matrix(mints->so_potential()));
// ha->add(mints->so_kinetic());
// ha->transform(Ca_);
// SharedMatrix hb (new Matrix(mints->so_potential()));
// hb->add(mints->so_kinetic());
// hb->transform(Cb_);
// double one_electron_energy = Da_->vector_dot(ha)
// + Db_->vector_dot(hb);
double one_electron_energy = 0.0;
// kinetic-energy
SharedMatrix Ta (new Matrix(mints->so_kinetic()));
Ta->transform(Ca_);
SharedMatrix Tb (new Matrix(mints->so_kinetic()));
Tb->transform(Cb_);
double kinetic_energy = Da_->vector_dot(Ta)
+ Db_->vector_dot(Tb);
// nuclear-attraction potential energy
SharedMatrix Va (new Matrix(mints->so_potential()));
Va->transform(Ca_);
SharedMatrix Vb (new Matrix(mints->so_potential()));
Vb->transform(Cb_);
double nuclear_attraction_energy = Da_->vector_dot(Va)
+ Db_->vector_dot(Vb);
one_electron_energy = nuclear_attraction_energy + kinetic_energy;
//printf("yay!\n\n"); fflush(stdout);
// // Coulomb energy computed using J object
// // std::vector < std::shared_ptr<Matrix> > JK = BuildJK();
//
//printf("yay!\n\n"); fflush(stdout);
// double caa = Da_->vector_dot(JK[0]);
//printf("yay!\n\n"); fflush(stdout);
// double cab = Da_->vector_dot(JK[1]);
//printf("yay!\n\n"); fflush(stdout);
// double cba = Db_->vector_dot(JK[0]);
//printf("yay!\n\n"); fflush(stdout);
// double cbb = Db_->vector_dot(JK[1]);
//printf("yay!\n\n"); fflush(stdout);
//
// double coulomb_energy = 0.5 * ( caa + cab + cba + cbb );
// classical nuclear repulsion energy
double nuclear_repulsion_energy = molecule_->nuclear_repulsion_energy({0.0,0.0,0.0});
// two-electron energy from reference wavefunction: < Psi| r12^-1 | Psi >
two_electron_energy_ = reference_energy_ - nuclear_repulsion_energy - one_electron_energy;
// double hf_ex_energy = 0.0;
// double lr_ex_energy = 0.0;
// if ( (options_.get_str("MCPDFT_METHOD") != "MCPDFT") ) {
//
// // HF exchange energy should be computed using K object
// double kaa = Da_->vector_dot(JK[2]);
// double kbb = Db_->vector_dot(JK[3]);
//
// hf_ex_energy = -0.5 * (kaa + kbb);
//
// if ( (options_.get_str("MCPDFT_METHOD") != "1H_MCPDFT")
// && (options_.get_str("MCPDFT_METHOD") != "1DH_MCPDFT") ) {
//
// // long range (LR) exchange energy calculated using JK object
// double wkaa = Da_->vector_dot(JK[4]);
// double wkbb = Db_->vector_dot(JK[5]);
//
// lr_ex_energy = -0.5 * (wkaa + wkbb);
// }
// }
// printf("Coulomb_energy %20.12lf \n", coulomb_energy);
// printf("hartree_ex_energy %20.12lf \n", hf_ex_energy);
// printf("lr_ex_energy %20.12lf \n", lr_ex_energy);
// printf("J-K %20.12lf \n",coulomb_energy+ 0.25* hf_ex_energy);
// printf("J-wK %20.12lf \n",coulomb_energy+ 0.75* lr_ex_energy);
// printf("J-K-wk %20.12lf \n",coulomb_energy+ 0.25* hf_ex_energy + 0.75* lr_ex_energy);
// printf("two electron energies (ref, total, sr, lr) = %20.12lf %20.12lf %20.12lf %20.12lf \n\n",two_electron_energy_,sr_Vee_energy_+lr_Vee_energy_,sr_Vee_energy_,lr_Vee_energy_);
// print total energy and its components
outfile->Printf(" ==> Energetics <==\n");
outfile->Printf("\n");
// outfile->Printf(" nuclear repulsion energy = %20.12lf\n",molecule_->nuclear_repulsion_energy());
outfile->Printf(" nuclear repulsion energy = %20.12lf\n",nuclear_repulsion_energy);
outfile->Printf(" nuclear attraction energy = %20.12lf\n",nuclear_attraction_energy);
outfile->Printf(" kinetic energy = %20.12lf\n",kinetic_energy);
outfile->Printf(" one-electron energy = %20.12lf\n",one_electron_energy);
if ( (options_.get_str("MCPDFT_METHOD") == "1H_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "1DH_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "LS1DH_MCPDFT") ) {
outfile->Printf(" two-electron energy = %20.12lf\n",two_electron_energy_);
outfile->Printf(" HF-exchange energy = %20.12lf\n",hf_ex_energy_);
if ( (options_.get_str("MCPDFT_METHOD") == "1DH_MCPDFT")
|| (options_.get_str("MCPDFT_METHOD") == "LS1DH_MCPDFT") ) {
outfile->Printf(" MP2-correlation energy = %20.12lf\n",mp2_corr_energy_);
}
}else if( (options_.get_str("MCPDFT_METHOD") == "RS1H_MCPDFT") || (options_.get_str("MCPDFT_METHOD") == "RS1DH_MCPDFT") ) {
outfile->Printf(" two-electron energy = %20.12lf\n",two_electron_energy_);
outfile->Printf(" SR-exchange energy = %20.12lf\n",sr_hartree_energy_);
outfile->Printf(" LR-exchange enrgy = %20.12lf\n",lr_hartree_energy_);
outfile->Printf(" SR+LR-exchange enrgy = %20.12lf\n",lr_hartree_energy_+sr_hartree_energy_);
if ( options_.get_str("MCPDFT_METHOD") == "RS1DH_MCPDFT") {
outfile->Printf(" MP2-correlation energy = %20.12lf\n",mp2_corr_energy_);
}
}
outfile->Printf(" classical coulomb energy = %20.12lf\n",coulomb_energy_);
if ( options_.get_str("MCPDFT_REFERENCE") == "V2RDM") {
outfile->Printf(" v2RDM-CASSCF energy contribution = %20.12lf\n",
nuclear_repulsion_energy + one_electron_energy + coulomb_energy_);
}else{
outfile->Printf(" CASSCF energy contribution = %20.12lf\n",