-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpno-mp2.cpp
4087 lines (3140 loc) · 112 KB
/
lpno-mp2.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
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
//
// aoints.cc
//
// Copyright (C) 2012 Edward Valeev
//
// Author: Edward Valeev <[email protected]>
// Maintainer: EV
//
// This file is part of the SC Toolkit.
//
// The SC Toolkit is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// The SC Toolkit 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// The U.S. Government is granted a limited license as per AL 91-7.
//
#include <iostream>
#include <iomanip>
#include <chemistry/molecule/molecule.h>
#include <chemistry/qc/basis/integral.h>
#include <chemistry/qc/basis/split.h>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
//#include <util/keyval/keyvalass.h>
using namespace sc;
class TensorRank4 {
public:
TensorRank4(int dim0, int dim1, int dim2, int dim3) {
dims_[0] = dim0;
dims_[1] = dim1;
dims_[2] = dim2;
dims_[3] = dim3;
data_.resize(dims_[0] * dims_[1] * dims_[2] * dims_[3]);
}
double& operator ()(int i, int j, int k, int l) {
return data_(index(i, j, k, l));
}
const double& operator ()(int i, int j, int k, int l) const {
return data_(index(i, j, k, l));
}
private:
int index(int i, int j, int k, int l) const {
return i * dims_[2] * dims_[1] * dims_[0] + j * dims_[1] * dims_[0]
+ k * dims_[0] + l;
}
size_t dims_[4];
Eigen::VectorXd data_;
};
class TensorRank6 {
public:
TensorRank6(int dim0, int dim1, int dim2, int dim3, int dim4, int dim5) {
dims_[0] = dim0;
dims_[1] = dim1;
dims_[2] = dim2;
dims_[3] = dim3;
dims_[4] = dim4;
dims_[5] = dim5;
data_.resize(
dims_[0] * dims_[1] * dims_[2] * dims_[3] * dims_[4]
* dims_[5]);
}
double& operator ()(int i, int j, int k, int l, int m, int n) {
return data_(index(i, j, k, l, m, n));
}
const double& operator ()(int i, int j, int k, int l, int m, int n) const {
return data_(index(i, j, k, l, m, n));
}
private:
int index(int i, int j, int k, int l, int m, int n) const {
return i * dims_[4] * dims_[3] * dims_[2] * dims_[1] * dims_[0]
+ j * dims_[3] * dims_[2] * dims_[1] * dims_[0]
+ k * dims_[2] * dims_[1] * dims_[0] + l * dims_[1] * dims_[0]
+ m * dims_[0] + n;
}
size_t dims_[6];
Eigen::VectorXd data_;
};
void get_overlap_ints(Ref<OneBodyInt> s_inteval, Eigen::MatrixXd &S_mat) {
const double* buffer = s_inteval->buffer();
const int nshell = s_inteval->basis1()->nshell();
std::cout << "overlap integrals:" << std::endl;
for (int s1 = 0; s1 < nshell; s1++) {
const int bf1_offset = s_inteval->basis1()->shell_to_function(s1);
const int nbf1 = s_inteval->basis1()->shell(s1).nfunction();
for (int s2 = 0; s2 < nshell; s2++) {
const int bf2_offset = s_inteval->basis1()->shell_to_function(s2);
const int nbf2 = s_inteval->basis1()->shell(s2).nfunction();
s_inteval->compute_shell(s1, s2);
int bf12 = 0;
for (int bf1 = 0; bf1 < nbf1; ++bf1) {
for (int bf2 = 0; bf2 < nbf2; ++bf2, ++bf12) {
S_mat(bf1 + bf1_offset, bf2 + bf2_offset) = buffer[bf12];
}
}
}
}
}
void get_core_hamiltonian_ints(Ref<OneBodyInt> h_inteval,
Eigen::MatrixXd &Hcore_mat) {
const double* buffer = h_inteval->buffer();
const int nshell = h_inteval->basis1()->nshell();
std::cout << "hcore integrals:" << std::endl;
for (int s1 = 0; s1 < nshell; s1++) {
const int bf1_offset = h_inteval->basis1()->shell_to_function(s1);
const int nbf1 = h_inteval->basis1()->shell(s1).nfunction();
for (int s2 = 0; s2 < nshell; s2++) {
const int bf2_offset = h_inteval->basis1()->shell_to_function(s2);
const int nbf2 = h_inteval->basis1()->shell(s2).nfunction();
h_inteval->compute_shell(s1, s2);
int bf12 = 0;
for (int bf1 = 0; bf1 < nbf1; ++bf1) {
for (int bf2 = 0; bf2 < nbf2; ++bf2, ++bf12) {
// std::cout << bf1+bf1_offset << " " << bf2+bf2_offset << " "
// << std::setprecision(15) << buffer[bf12] << std::endl;
Hcore_mat(bf1 + bf1_offset, bf2 + bf2_offset) =
buffer[bf12];
}
}
}
}
}
void get_two_electron_ints(Ref<TwoBodyInt> twoecoulomb_inteval,
Eigen::MatrixXd &Heffective_mat) {
const double* buffer = twoecoulomb_inteval->buffer();
const int nshell = twoecoulomb_inteval->basis1()->nshell();
const int nbasis = twoecoulomb_inteval->basis1()->nbasis();
std::cout << "two-e Coulomb integrals:" << std::endl;
for (int s1 = 0; s1 < nshell; s1++) {
const int bf1_offset = twoecoulomb_inteval->basis1()->shell_to_function(
s1);
const int nbf1 = twoecoulomb_inteval->basis1()->shell(s1).nfunction();
for (int s2 = 0; s2 < nshell; s2++) {
const int bf2_offset =
twoecoulomb_inteval->basis1()->shell_to_function(s2);
const int nbf2 =
twoecoulomb_inteval->basis1()->shell(s2).nfunction();
for (int s3 = 0; s3 < nshell; s3++) {
const int bf3_offset =
twoecoulomb_inteval->basis1()->shell_to_function(s3);
const int nbf3 =
twoecoulomb_inteval->basis1()->shell(s3).nfunction();
for (int s4 = 0; s4 < nshell; s4++) {
const int bf4_offset =
twoecoulomb_inteval->basis1()->shell_to_function(
s4);
const int nbf4 =
twoecoulomb_inteval->basis1()->shell(s4).nfunction();
twoecoulomb_inteval->compute_shell(s1, s2, s3, s4);
int bf1234 = 0;
for (int bf1 = 0; bf1 < nbf1; ++bf1) {
for (int bf2 = 0; bf2 < nbf2; ++bf2) {
for (int bf3 = 0; bf3 < nbf3; ++bf3) {
for (int bf4 = 0; bf4 < nbf4; ++bf4, ++bf1234) {
Heffective_mat(
(bf1 + bf1_offset) * nbasis + bf2
+ bf2_offset,
(bf3 + bf3_offset) * nbasis + bf4
+ bf4_offset) =
buffer[bf1234];
}
}
}
}
}
}
}
}
}
Eigen::MatrixXd symmetric_orthogonalization(const Eigen::MatrixXd &S) {
//Symmetric orthogonalization
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverS(S);
Eigen::MatrixXd X = eigensolverS.operatorInverseSqrt();
return X;
}
Eigen::MatrixXd cannonical_orthogonalization(const Eigen::MatrixXd &S,
const int nbasis) {
//Canonical orthogonalization
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverS(S);
Eigen::MatrixXd U = eigensolverS.eigenvectors();
Eigen::MatrixXd Ut = U.transpose();
Eigen::VectorXd s = eigensolverS.eigenvalues();
Eigen::ArrayXd sa = s.array();
Eigen::ArrayXd ss = sa.sqrt();
//sqrts is s^(1/2)
Eigen::MatrixXd sqrts(nbasis, nbasis);
for (size_t i = 0; i < nbasis; i++) {
for (size_t j = 0; j < nbasis; j++) {
if (i == j) {
sqrts(i, j) = ss(i);
} else {
sqrts(i, j) = 0;
}
}
}
Eigen::MatrixXd X = U * sqrts.inverse();
return X;
}
Eigen::MatrixXd core_hamiltonian_transformation(const Eigen::MatrixXd &H,
const Eigen::MatrixXd &X, const Eigen::MatrixXd &Xt) {
Eigen::MatrixXd H_t = Xt * H * X;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverH(H);
Eigen::MatrixXd C_t = eigensolverH.eigenvectors();
Eigen::MatrixXd C = X * C_t;
return C;
}
Eigen::MatrixXd hamiltonian_transformation(const Eigen::MatrixXd &H,
const Eigen::MatrixXd &X, const Eigen::MatrixXd &Xt) {
Eigen::MatrixXd H_t = Xt * H * X;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverH(H_t);
Eigen::MatrixXd C_t = eigensolverH.eigenvectors();
Eigen::MatrixXd C = X * C_t;
return C;
}
Eigen::MatrixXd c_occupied(const Eigen::MatrixXd &C, const int nocc) {
Eigen::MatrixXd c(C.cols(), nocc);
for (size_t i = 0; i < C.cols(); i++) {
for (size_t j = 0; j < nocc; j++) {
c(i, j) = C(i, j);
}
}
return c;
}
Eigen::MatrixXd fock_build(const Eigen::MatrixXd &H_core,
const Eigen::MatrixXd &H_effective, const Eigen::MatrixXd &P,
const int nbasis) {
Eigen::MatrixXd F(nbasis, nbasis);
Eigen::MatrixXd J_ao(nbasis, nbasis), K_ao(nbasis, nbasis);
Eigen::MatrixXd J(nbasis, nbasis), K(nbasis, nbasis);
for (size_t i = 0; i < nbasis; i++) {
for (size_t j = 0; j < nbasis; j++) {
for (size_t k = 0; k < nbasis; k++) {
for (size_t l = 0; l < nbasis; l++) {
J_ao(k, l) = H_effective(i * nbasis + j, l * nbasis + k);
K_ao(k, l) = H_effective(i * nbasis + k, l * nbasis + j);
}
}
J(i, j) = (P * J_ao).trace();
K(i, j) = (P * K_ao).trace();
F(i, j) = H_core(i, j) + J(i, j) - 0.5 * K(i, j);
}
}
return F;
}
double electronic_energy(const Eigen::MatrixXd &H_core,
const Eigen::MatrixXd &F, const Eigen::MatrixXd &P_new,
const int nbasis) {
double E = 0.0;
for (size_t i = 0; i < nbasis; i++) {
for (size_t j = 0; j < nbasis; j++) {
E += P_new(i, j) * (H_core(i, j) + F(i, j));
}
}
return E;
}
double rms_density_norm(const Eigen::MatrixXd &P, const Eigen::MatrixXd &P_new,
const int nbasis) {
double diff = 0;
for (size_t i = 0; i < nbasis; i++) {
for (size_t j = 0; j < nbasis; j++) {
diff += (pow(P_new(i, j) - P(i, j), 2.0));
}
}
return sqrt(diff);
}
Eigen::VectorXd orbital_energies(const Eigen::MatrixXd &F,
const Eigen::MatrixXd &X, const Eigen::MatrixXd &Xt) {
Eigen::MatrixXd F_t = Xt * F * X;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverF(F_t);
Eigen::MatrixXd e_orbital = eigensolverF.eigenvalues();
return e_orbital;
}
Eigen::MatrixXd HartreeFock(const Eigen::MatrixXd &S,
const Eigen::MatrixXd &H_core, const Eigen::MatrixXd &H_effective,
const int nbasis, const int nocc, const double nuc_repulsion,
Eigen::MatrixXd &F, Eigen::MatrixXd &X) {
X = symmetric_orthogonalization(S);
//Eigen::MatrixXd X = cannonical_orthogonalization(S, nbasis);
Eigen::MatrixXd Xt = X.transpose();
//Obtaining C coefficients from H_core_t
Eigen::MatrixXd C = core_hamiltonian_transformation(H_core, X, Xt);
//Obtaining c coefficients of occupied orbitals
Eigen::MatrixXd c = c_occupied(C, nocc);
//Generating density matrix
Eigen::MatrixXd P = 2 * c * c.transpose();
//std::cout << P << std::endl;
//SCF procedure
int count = 0;
double difference = 1;
const double tollerance = 1e-6;
Eigen::MatrixXd C_new(nbasis, nbasis);
while (difference > tollerance) {
count++;
//Creating fock matrix
F = fock_build(H_core, H_effective, P, nbasis);
//std::cout << F << std::endl;
C_new = hamiltonian_transformation(F, X, Xt);
Eigen::MatrixXd c_new = c_occupied(C_new, nocc);
Eigen::MatrixXd P_new = 2.0 * c_new * c_new.transpose();
double E_electronic = electronic_energy(H_core, F, P_new, nbasis);
difference = rms_density_norm(P, P_new, nbasis);
double E_tot = 0.5 * E_electronic + nuc_repulsion;
std::cout << "iter " << count << "\t" << std::setprecision(10)
<< " Energy = " << E_tot << "\t" << "error = " << difference
<< std::endl;
P = P_new;
//P = 0.8 * P + 0.2 * P_new;
}
//std::cout << e_orbital << std::endl;
return C_new;
}
TensorRank4 IntegralTransformation(const Eigen::MatrixXd &H_effective,
const Eigen::MatrixXd &C, const int nbasis) {
TensorRank4 orb(nbasis, nbasis, nbasis, nbasis);
Eigen::MatrixXd g = H_effective;
g.resize(nbasis, nbasis * nbasis * nbasis); //g(p,qrs)
Eigen::MatrixXd t1 = g.transpose() * C; //t1(qrs,i)
t1.resize(nbasis, nbasis * nbasis * nbasis); //t1(q,rsi)
Eigen::MatrixXd t2 = t1.transpose() * C; //t2(rsi,j)
t2.resize(nbasis, nbasis * nbasis * nbasis); //t2(r,sij)
Eigen::MatrixXd t3 = t2.transpose() * C; //t3(sij,k)
t3.resize(nbasis, nbasis * nbasis * nbasis); //t2(s,ijk)
Eigen::MatrixXd t4 = t3.transpose() * C; //t3(ijk,l)
t4.resize(nbasis, nbasis * nbasis * nbasis); //t2(i,jkl)
t4.resize(nbasis * nbasis, nbasis * nbasis);
for (size_t i = 0; i < nbasis; i++) {
for (size_t j = 0; j < nbasis; j++) {
for (size_t k = 0; k < nbasis; k++) {
for (size_t l = 0; l < nbasis; l++) {
orb(i, j, k, l) = t4(i * nbasis + j, k * nbasis + l);
}
}
}
}
return orb;
}
TensorRank4 MP2_second_quantization(const TensorRank4 &orb, const int nocc,
const int nbasis, const Eigen::MatrixXd &F, const Eigen::MatrixXd &X,
TensorRank4 &dij, TensorRank4 &aij) {
TensorRank4 Tij(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tji(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tij_tilda(nbasis, nbasis, nbasis, nbasis);
Eigen::MatrixXd Xt = X.transpose();
Eigen::VectorXd e_orbital = orbital_energies(F, X, Xt);
Eigen::MatrixXd Ft = Xt * F * X;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverFt(Ft);
Eigen::MatrixXd U = eigensolverFt.eigenvectors();
Eigen::MatrixXd FT = U.transpose() * Ft * U;
//std::cout << FT << std::endl;
const int nvirt = nbasis - nocc;
//transformation of Fock matrix (virtual space)
Eigen::MatrixXd F_virt(nvirt, nvirt);
F_virt = Eigen::MatrixXd::Zero(nvirt, nvirt);
Eigen::VectorXd e_virtual(nvirt);
Eigen::MatrixXd E_MP2_pair(nocc, nocc);
E_MP2_pair = Eigen::MatrixXd::Zero(nocc, nocc);
double E_MP2 = 0;
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
double summ = 0;
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Tij(i, a, j, b) = -orb(i, a, j, b)
/ (e_orbital(a) + e_orbital(b) - e_orbital(i)
- e_orbital(j));
Tji(i, a, j, b) = -orb(i, b, j, a)
/ (e_orbital(a) + e_orbital(b) - e_orbital(i)
- e_orbital(j));
Tij_tilda(i, a, j, b) = 2.0 * Tij(i, a, j, b)
- 1.0 * Tji(i, a, j, b);
summ += orb(i, a, j, b) * Tij_tilda(i, a, j, b);
}
}
E_MP2_pair(i, j) = summ;
//std::cout << "Energy of pair " << std::endl;
//std::cout << E_MP2_pair(i, j) << std::endl;
E_MP2 += E_MP2_pair(i, j);
}
}
//LPNO
//based on paper F. Neese et al., J. Chem. Phys. 130, 114108 (2009)
TensorRank4 Dij(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tij_tilda_PNO(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tij_PNO(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tji_PNO(nbasis, nbasis, nbasis, nbasis);
TensorRank4 K_PNO(nbasis, nbasis, nbasis, nbasis);
TensorRank4 K_tilda(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tij_tilda_PNO_T(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tij_PNO_T(nbasis, nbasis, nbasis, nbasis);
TensorRank4 Tji_PNO_T(nbasis, nbasis, nbasis, nbasis);
TensorRank4 K_PNO_T(nbasis, nbasis, nbasis, nbasis);
TensorRank4 K_tilda_T(nbasis, nbasis, nbasis, nbasis);
Eigen::MatrixXd E_MP2_pair_PNO(nocc, nocc);
E_MP2_pair_PNO = Eigen::MatrixXd::Zero(nocc, nocc);
Eigen::MatrixXd E_MP2_pair_PNO_T(nocc, nocc);
E_MP2_pair_PNO_T = Eigen::MatrixXd::Zero(nocc, nocc);
//defined like in orca mdci_pno.cpp under TPNOPairData_RHF::MakePairSpecificPNOs_RHF function
Eigen::MatrixXd E_MP2_pair_Full(nocc, nocc);
E_MP2_pair_Full = Eigen::MatrixXd::Zero(nocc, nocc);
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
Eigen::MatrixXd K_mat(nvirt, nvirt), Kij_PNO(nvirt, nvirt), Jii(
nvirt, nvirt), Jjj(nvirt, nvirt), Kii(nvirt, nvirt), Kjj(
nvirt, nvirt);
K_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
Kij_PNO = Eigen::MatrixXd::Zero(nvirt, nvirt);
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Jii(a - nocc, b - nocc) = orb(i, i, a, b);
Jjj(a - nocc, b - nocc) = orb(j, j, a, b);
Kii(a - nocc, b - nocc) = orb(i, a, i, b);
Kjj(a - nocc, b - nocc) = orb(j, a, j, b);
K_mat(a - nocc, b - nocc) = orb(i, a, j, b);
}
}
//Meyer transformation
Eigen::MatrixXd Gij(nvirt, nvirt);
Gij = Eigen::MatrixXd::Zero(nvirt, nvirt);
if (i == j) {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Gij(a - nocc, b - nocc) = FT(a, b)
+ Kii(a - nocc, b - nocc)
- Jii(a - nocc, b - nocc);
}
}
} else {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Gij(a - nocc, b - nocc) = FT(a, b)
+ Kii(a - nocc, b - nocc)
+ Kjj(a - nocc, b - nocc)
- 0.5 * Jii(a - nocc, b - nocc)
- 0.5 * Jjj(a - nocc, b - nocc);
}
}
}
//std::cout << "G matrix \n"<<Gij << std::endl;
//Diagonalization of G matrix
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverGij(Gij);
Eigen::MatrixXd GV = eigensolverGij.eigenvectors();
Eigen::VectorXd GE = eigensolverGij.eigenvalues();
//transforming K_mat into Kij_new using GV
Eigen::MatrixXd Kij_new = GV.transpose() * K_mat * GV;
//std::cout << "Kij_new matrix \n"<< K_mat << std::endl;
Eigen::MatrixXd Tij_new(nvirt, nvirt);
Tij_new = Eigen::MatrixXd::Zero(nvirt, nvirt);
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Tij_new(a - nocc, b - nocc) = -Kij_new(a - nocc, b - nocc)
/ (GE(a - nocc) + GE(b - nocc) - e_orbital(i)
- e_orbital(j));
}
}
//std::cout << "Tij_new matrix \n"<< Tij_new << std::endl;
//transformation of Tij_new with GV
Eigen::MatrixXd Tij_t = GV * Tij_new * GV.transpose();
//std::cout << "Tij_t matrix \n"<< Tij_t << std::endl;
//transormation of exchange integrals in PNO basis: Kij^ab (ij are in subscript & ab in upperscript)
Eigen::MatrixXd Tij_mat(nvirt, nvirt), Tij_tilda_mat(nvirt, nvirt),
Dij_mat(nvirt, nvirt);
Tij_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
Tij_tilda_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
Dij_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Tij_mat(a - nocc, b - nocc) = Tij_t(a - nocc, b - nocc);
if (i == j) {
Tij_tilda_mat(a - nocc, b - nocc) = 2.0
* Tij_t(a - nocc, b - nocc)
- 1.0 * Tij_t(b - nocc, a - nocc);
} else {
Tij_tilda_mat(a - nocc, b - nocc) = 4.0
* Tij_t(a - nocc, b - nocc)
- 2.0 * Tij_t(b - nocc, a - nocc);
}
}
}
//E_MP2_pair_Full from orca mdci_pno
double summa = 0.0;
if (i == j) {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
summa += Tij_t(a - nocc, b - nocc)
* K_mat(a - nocc, b - nocc);
}
}
} else {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
summa += 2.0 * Tij_t(a - nocc, b - nocc)
* (2.0 * K_mat(a - nocc, b - nocc)
- K_mat(b - nocc, a - nocc));
}
}
}
E_MP2_pair_Full(i, j) = summa;
//std::cout << "Tij_tilda_mat amplitudes \n"<< Tij_tilda_mat << "\n"<< std::cout;
//computation of <Tij_tilda*Tij> = t
//from paper
double t = 0.0;
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
t += Tij_tilda_mat(b - nocc, a - nocc)
* Tij_mat(a - nocc, b - nocc);
}
}
double Nij = 1.0 + t;
//std::cout << Nij << std::endl;
if (i == j) {
Dij_mat = (1.0 + 1.0) / Nij
* (Tij_tilda_mat.transpose() * Tij_mat
+ Tij_tilda_mat * Tij_mat.transpose());
} else {
Dij_mat = 1.0 / Nij
* (Tij_tilda_mat.transpose() * Tij_mat
+ Tij_tilda_mat * Tij_mat.transpose());
};
Eigen::MatrixXd dij_mat(nvirt, nvirt);
dij_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
Eigen::VectorXd aij_vect(nvirt);
aij_vect = Eigen::VectorXd::Zero(nvirt);
Eigen::MatrixXd aij_mat(nvirt, nvirt);
aij_mat = Eigen::MatrixXd::Zero(nvirt, nvirt);
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverDij_mat(
Dij_mat);
dij_mat = eigensolverDij_mat.eigenvectors();
aij_vect = eigensolverDij_mat.eigenvalues();
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
if (a == b) {
aij_mat(a - nocc, b - nocc) = aij_vect(a - nocc);
} else {
aij_mat(a - nocc, b - nocc) = 0;
}
}
}
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Dij(i, a, j, b) = Dij_mat(a - nocc, b - nocc);
dij(i, a, j, b) = dij_mat(a - nocc, b - nocc);
aij(i, a, j, b) = aij_mat(a - nocc, b - nocc);
}
}
std::cout << "PNO occupation number for pair i,j = " << i << ","
<< j << std::endl;
std::cout << aij_vect << "\n" << std::endl;
//
//transformation to PNO basis
//
//transformation of Kij_new exchange integrals into PNO basis
/*
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
double num = 0;
for (size_t c = 0; c < nvirt; c ++) {
for (size_t d = 0; d < nvirt; d ++) {
num += dij_mat(c, a - nocc)*K_mat(c, d)*dij_mat(d, b - nocc);
}
}
Kij_PNO(a - nocc, b - nocc) = num;
}
}
*/
//transformation of K_matrix into PNO basis
Kij_PNO = dij_mat.transpose() * K_mat * dij_mat;
//Construction of fock operator of virtual canonical orbitals
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
F_virt(a - nocc, b - nocc) = FT(a, b);
}
}
//transformation of F_virt into PNO space
Eigen::MatrixXd F_virt_PNO = dij_mat.transpose() * F_virt * dij_mat;
//std::cout << F_virt_PNO << std::endl;
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverF_virt_PNO(
F_virt_PNO);
Eigen::MatrixXd L = eigensolverF_virt_PNO.eigenvectors();
e_virtual = eigensolverF_virt_PNO.eigenvalues();
Eigen::MatrixXd K_mat_tilda(nvirt, nvirt);
K_mat_tilda = Eigen::MatrixXd::Zero(nvirt, nvirt);
K_mat_tilda = L.transpose() * Kij_PNO * L;
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
K_tilda(i, a, j, b) = K_mat_tilda(a - nocc, b - nocc);
}
}
//E pair ij
double summ = 0;
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Tij_PNO(i, a, j, b) = -K_tilda(i, a, j, b)
/ (e_virtual(a - nocc) + e_virtual(b - nocc)
- e_orbital(i) - e_orbital(j));
Tji_PNO(i, a, j, b) = -K_tilda(i, b, j, a)
/ (e_virtual(a - nocc) + e_virtual(b - nocc)
- e_orbital(i) - e_orbital(j));
Tij_tilda_PNO(i, a, j, b) = 2.0 * Tij_PNO(i, a, j, b)
- 1.0 * Tji_PNO(i, a, j, b);
summ += K_tilda(i, a, j, b) * Tij_tilda_PNO(i, a, j, b);
}
}
E_MP2_pair_PNO(i, j) = summ;
std::cout << "Energy of pair Full" << std::endl;
std::cout << E_MP2_pair_Full(i, j) << "\n" << std::endl;
//MP2-PNO energy after PNO truncation for ij pair
//Truncation criteria is TCutPNO < 1e-7 & TCutPairs < 1e-4
double TCutPNO = 1e-15;
double TCutPairs = 0.0; //Hartree
int dimPNO = 0;
double Pair_En;
if (E_MP2_pair_Full(i, j) < 0) {
Pair_En = -1.0 * E_MP2_pair_Full(i, j);
} else {
Pair_En = 1.0 * E_MP2_pair_Full(i, j);
}
if (Pair_En > TCutPairs) {
//counting a number of PNO's for a given pair
if (TCutPNO == 0) {
dimPNO = nvirt;
} else {
for (size_t p = 0; p < nvirt; p++) {
if (aij_vect(p) > TCutPNO) {
dimPNO += 1;
}
}
}
//std::cout << "number of PNO's per pair ij" << std::endl;
//std::cout << dimPNO << std::endl;
//storing PNO coefficients dij after truncation
//aij_vec_T & dij_mat_T are PNO occupation number and coefficients after truncation for given ij pair
Eigen::MatrixXd dij_mat_T1(nvirt, dimPNO);
dij_mat_T1 = Eigen::MatrixXd::Zero(nvirt, dimPNO);
Eigen::MatrixXd dij_mat_T(nvirt, dimPNO + 1);
dij_mat_T = Eigen::MatrixXd::Zero(nvirt, dimPNO + 1);
Eigen::VectorXd aij_vect_T(dimPNO);
aij_vect_T = Eigen::VectorXd::Zero(dimPNO);
for (size_t p = 0; p < dimPNO; p++) {
aij_vect_T(p) = aij_vect(nvirt - dimPNO + p);
for (size_t a = 0; a < nvirt; a++) {
dij_mat_T1(a, p) = dij_mat(a, nvirt - dimPNO + p);
}
}
//std::cout << "size of dij truncated" << std::endl;
//std::cout << dij_mat_T.cols() << std::endl;
if (dij_mat_T1.cols() != 0) {
dij_mat_T = dij_mat_T1;
//std::cout << "dij truncated" << std::endl;
//std::cout << dij_mat_T << std::endl;
//transformation of Kij & Tij with truncated dij and energy calculation of MP2-PNO-truncated
Eigen::MatrixXd Kij_PNO_T(dimPNO, dimPNO);
Kij_PNO_T = Eigen::MatrixXd::Zero(dimPNO, dimPNO);
//transformation of K_matrix into truncated PNO basis
Kij_PNO_T = dij_mat_T.transpose() * K_mat * dij_mat_T;
//transformation of F_virt into truncated PNO space
Eigen::MatrixXd F_virt_PNO_T = dij_mat_T.transpose()
* F_virt * dij_mat_T;
Eigen::VectorXd e_virtual_T(dimPNO);
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigensolverF_virt_PNO_T(
F_virt_PNO_T);
Eigen::MatrixXd L_T =
eigensolverF_virt_PNO_T.eigenvectors();
e_virtual_T = eigensolverF_virt_PNO_T.eigenvalues();
Eigen::MatrixXd K_mat_tilda_T(dimPNO, dimPNO);
K_mat_tilda_T = Eigen::MatrixXd::Zero(dimPNO, dimPNO);
K_mat_tilda_T = L_T.transpose() * Kij_PNO_T * L_T;
for (size_t a = nocc; a < nocc + dimPNO; a++) {
for (size_t b = nocc; b < nocc + dimPNO; b++) {
K_tilda_T(i, a, j, b) = K_mat_tilda_T(a - nocc,
b - nocc);
}
}
//E pair ij
double summ_T = 0;
for (size_t a = nocc; a < nocc + dimPNO; a++) {
for (size_t b = nocc; b < nocc + dimPNO; b++) {
Tij_PNO_T(i, a, j, b) = -K_tilda_T(i, a, j, b)
/ (e_virtual_T(a - nocc)
+ e_virtual_T(b - nocc)
- e_orbital(i) - e_orbital(j));
Tji_PNO_T(i, a, j, b) = -K_tilda_T(i, b, j, a)
/ (e_virtual_T(a - nocc)
+ e_virtual_T(b - nocc)
- e_orbital(i) - e_orbital(j));
Tij_tilda_PNO_T(i, a, j, b) = 2.0
* Tij_PNO_T(i, a, j, b)
- 1.0 * Tji_PNO_T(i, a, j, b);
summ_T += K_tilda_T(i, a, j, b)
* Tij_tilda_PNO_T(i, a, j, b);
}
}
E_MP2_pair_PNO_T(i, j) = summ_T;
}
}
}
}
double E_MP2_tilda = 0.0;
double E_MP2_tilda_T = 0.0;
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
E_MP2_tilda += E_MP2_pair_PNO(i, j);
E_MP2_tilda_T += E_MP2_pair_PNO_T(i, j);
}
}
double E_MP2_new = 0.0;
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j <= i; j++) {
E_MP2_new += E_MP2_pair_Full(i, j);
}
}
std::cout << "MP2 energy" << std::endl;
std::cout << E_MP2 << std::endl;
std::cout << "PNO MP2 energy without truncation" << std::endl;
std::cout << E_MP2_tilda << std::endl;
std::cout << "PNO MP2 energy with truncation" << std::endl;
std::cout << E_MP2_tilda_T << std::endl;
return Tij;
}
TensorRank4 get_residual(const TensorRank4 &orb, const TensorRank4 Tij_n,
const Eigen::MatrixXd &Ft, const int nocc, const int nbasis) {
TensorRank4 Res(nbasis, nbasis, nbasis, nbasis);
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
double summ_c = 0;
for (size_t c = nocc; c < nbasis; c++) {
summ_c += Ft(a, c) * Tij_n(i, c, j, b)
+ Tij_n(i, a, j, c) * Ft(c, b);
}
double summ_k = 0;
for (size_t k = 0; k < nocc; k++) {
summ_k += Ft(i, k) * Tij_n(k, a, j, b)
+ Tij_n(i, a, k, b) * Ft(k, j);
}
Res(i, a, j, b) = orb(i, a, j, b) + summ_c - summ_k;
//Res(i, a, j, b) = orb(i, a, j, b) - summ_k;
}
}
}
}
return Res;
}
TensorRank4 get_increment_of_amplitude(const TensorRank4 &Res,
const Eigen::MatrixXd &Ft, const int nocc, const int nbasis) {
TensorRank4 dT(nbasis, nbasis, nbasis, nbasis);
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
dT(i, a, j, b) = -Res(i, a, j, b)
/ (Ft(a, a) + Ft(b, b) - Ft(i, i) - Ft(j, j));
}
}
}
}
return dT;
}
TensorRank4 get_new_aplitudes(TensorRank4 &Tij_n, const TensorRank4 &dT,
const int nocc, const int nbasis) {
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
for (size_t a = nocc; a < nbasis; a++) {
for (size_t b = nocc; b < nbasis; b++) {
Tij_n(i, a, j, b) = Tij_n(i, a, j, b) + dT(i, a, j, b);
}
}
}
}
return Tij_n;
}
double max_abs_Res(TensorRank4 &Res, const int nocc, const int nbasis) {
double max = 0.0;
for (size_t i = 0; i < nocc; i++) {
for (size_t j = 0; j < nocc; j++) {
for (size_t a = nocc; a < nbasis; a++) {