forked from rachelglenn/python_code_to_check_C
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CCSD_Helper.py
1259 lines (1089 loc) · 49.9 KB
/
CCSD_Helper.py
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
################################################################
#
#
# Created by: Rachel Glenn
# Date: 12/14/2016
# This code calculates the converaged CCSD energy, pseudo energy, the t1, t2, lam1, and lam2
# It also calculates the single particle density matrix using the converged t1, t2, lam1, and lam2
#
#
#####################################################################
import sys
import os
from copy import deepcopy
import numpy as np
import cmath
import pandas as pd
#from pandas import *
import psi4 as psi4
sys.path.append(os.environ['HOME']+'/miniconda2/lib/python2.7/site-packages')
from opt_einsum import contract
import time
import csv
class CCSD_Helper(object):
def __init__(self,psi): #ndocc=None):
self.counter = 0
self.mol = psi4.core.get_active_molecule()
mol = self.mol
self.wfn = psi4.scf_helper('SCF', return_wfn = True)
self.scf_e = psi4.energy('scf')
self.mints = psi4.core.MintsHelper(self.wfn.basisset())
self.nmo = self.wfn.nmo()
self.ccsd_e = psi4.energy('ccsd')
self.S = np.asarray(self.mints.ao_overlap())
#define ndocc
# Orthoganlizer
A = self.mints.ao_overlap()
A.power(-0.5, 1.e-14)
self.A = np.asarray(A)
self.ndocc =int(sum(mol.Z(A) for A in range(mol.natom())) / 2)
self.wfn.Ca_subset("AO", "ACTIVE").print_out()
self.C = self.wfn.Ca()
V = np.asarray(self.mints.ao_potential())
T = np.asarray(self.mints.ao_kinetic())
self.H = T + V
self.occ = slice(2*self.ndocc)
self.vir = slice(2*self.ndocc, 2*self.nmo)
#MO energies
self.eps = np.asarray(self.wfn.epsilon_a()).repeat(2, axis=0)
self.TEI = self.TEI_MO()
###############Setup the Fock matrix and TEIs #####################
def TEI_MO(self, C=None):
if C is None: C = self.C
return np.asarray(self.mints.mo_spin_eri(C, C))
def GenS12(self):
# Update S, transform to MO basis and tile for alpha/beta spin
S = self.S
nmo = self.nmo
S = S.repeat(2, axis=1).repeat(2, axis=0)
S = S*np.tile(np.identity(2),(nmo,nmo))
evals, evecs = np.linalg.eigh(S)
nmo = self.nmo
Ls = np.zeros(shape=(2*nmo,2*nmo))
Lsplus = np.zeros(shape=(2*nmo,2*nmo))
for i in range (2*nmo):
Ls[i][i]= 1/np.sqrt(evals[i])
Lsplus[i][i]= np.sqrt(evals[i])
S12 = contract('il,lk,jk->ij', evecs, Ls, evecs)
S12plus = contract('il,lk,jk->ij', evecs, Lsplus, evecs)
return S12, S12plus
def F_MO(self, H=None, C=None):
if H is None: H = self.H
if C is None: C = self.C
TEI = self.TEI_MO(C)
occ = self.occ
nmo =self.nmo
# Update H, transform to MO basis and tile for alpha/beta spin
H = contract('vi,uv,uj->ij', C, H, C)
H = H.repeat(2, axis=1).repeat(2, axis=0)
H = H*np.tile(np.identity(2),(nmo,nmo))
F= H + contract('pmqm->pq', TEI[:, occ, :, occ])
return F
def MO_E(self, H=None, C=None):
if H is None: H = self.H
if C is None: C = self.C
F = self.F_MO(H,C)
evals, evecs = np.linalg.eigh(F)
return evals
def MP2_E(self, alpha, H=None, C=None):
#alpha is a text variable to select the output
if H is None: H = self.H
if C is None: C = self.C
eps = self.MO_E(H,C)
o = self.occ
v = self.vir
self.TEI = self.TEI_MO(C)
TEI = self.TEI
Dem = eps[o].reshape(-1, 1, 1, 1) + eps[o].reshape(-1, 1, 1) - eps[v].reshape(-1, 1) - eps[v]
Dem = 1/Dem
T2 = contract('ijab,ijab->ijab', TEI[o, o, v, v],Dem)
MP2 = contract('ijab,ijab->', T2, TEI[o, o, v, v])
T2 = TEI[o, o ,v, v]*Dem
MP2 = np.sum(TEI[o, o, v, v]*T2)
#print MP2
MP2_E = self.scf_e + 1/4.0*MP2
if alpha is 'Test':
pass
return self.scf_e, MP2_E, T2
############################################################
#
# T1 and T2-equations (CCSD)
# By R. Glenn, I used T. Daniel Crawfords equations
#
#
#
############################################################
#Build Fvv
def Fae(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = F[v, v].copy()
term2 = - 0.5*contract('me,ma->ae', F[o, v], t1)
term3 = contract('mafe,mf->ae', TEI[o, v, v, v], t1)
tau = t2.copy() + contract('ia,jb->ijab', t1, t1)
term4 =-0.5*contract('mnef,mnaf->ae', TEI[o, o, v, v], tau)
total = term1 + term2 + term3 + term4
return total
#Build Foo
def Fmi(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = F[o, o].copy()
term2 =0.5*contract('me,ie->mi', F[o, v], t1)
term3 = contract('mnie,ne->mi', TEI[o, o, o, v], t1)
tau = t2.copy() + contract('ia,jb->ijab', t1, t1)
term4 = 0.5*contract('mnef,inef->mi', TEI[o, o, v, v], tau)
total = term1 + term2 + term3 + term4
return total
#Build Fov
def Fme(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = F[o, v].copy()
term2 = contract('mnef,nf->me', TEI[o, o, v, v], t1)
total = term1 + term2
return total
#####################################################
#
# T1-rhs-CCSD
#
#####################################################
def T1eq_rhs(self, t1, t2, F):
#All terms in the T1 Equation
v = self.vir
o = self.occ
TEI = self.TEI
fae = self.Fae(t1, t2, F)
fmi = self.Fmi(t1, t2, F)
fme = self.Fme(t1, t2, F)
term1 = F[o, v].copy()
term2 = contract('ae,ie->ia', fae, t1)
term3 = -contract('mi,ma->ia', fmi,t1)
term4 = contract('me,imae->ia', fme, t2)
#extra terms
extra1 = -contract('naif,nf->ia', TEI[o, v, o, v], t1)
extra2 = -0.5*contract('nmei,mnae->ia', TEI[o, o, v, o], t2)
extra3 = -0.5*contract('maef,imef->ia', TEI[o, v, v, v], t2)
total = term1 + term2 + term3 + term4 + extra1 + extra2 + extra3
return total
#######################################################
#Build Woooo for t2 terms
def Wmnij(self, t1 ,t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[o, o, o, o].copy()
term2a = contract('mnie,je->mnij', TEI[o, o, o, v], t1)
term2 = term2a - term2a.swapaxes(2,3) #swap ij
tau = 0.25*t2 + 0.5*contract('ia,jb->ijab', t1, t1) - 0.5*contract('ib,ja->ijab', t1, t1)
term3 = contract('mnef,ijef->mnij', TEI[o, o, v, v], tau)
total = term1 + term2 + term3
return total
#Build Woooo for t1 * t1 like terms
def Wmnij_2(self, t1 ,t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[o, o, o, o].copy()
term2a = contract('mnie,je->mnij', TEI[o, o, o, v], t1)
term2 = term2a - term2a.swapaxes(2,3) #swap ij
tau = contract('ia,jb->ijab', t1, t1)
term4a = 0.25*contract('mnef,ijef->mnij', TEI[o, o, v, v], tau)
term4 = term4a - term4a.swapaxes(2,3)
total = term1 + term2 + term4
return total
#Build Wvvvv for t2 terms
def Wabef(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[v, v, v, v].copy()
term2tmp = -contract('amef,mb->abef', TEI[v, o, v, v], t1)
term2 = term2tmp - term2tmp.swapaxes(0,1) #swap ab
tau = contract('ia,jb->ijab', t1, t1) #- contract('ib,ja->ijab', t1, t1)
term3 = 0.25*contract('mnef,mnab->abef', TEI[o, o, v, v], t2)
term4a = 0.5*contract('mnef,mnab->abef', TEI[o, o, v, v], tau)
term4 = term4a - term4a.swapaxes(0,1)
total = term1 + term2 + term3 + term4
return total
#Build Wvvvv for t1 * t1 like terms
def Wabef_2(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[v, v, v, v].copy()
term2tmp = -contract('amef,mb->abef', TEI[v, o, v, v], t1)
term2 = term2tmp - term2tmp.swapaxes(0,1) #swap ab
tau = contract('ia,jb->ijab', t1, t1) - contract('ib,ja->ijab', t1, t1)
term4 = 0.25*contract('mnef,mnab->abef', TEI[o, o, v, v], tau)
total = term1 + term2 + term4
return total
#Build Wovvo
def Wmbej(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[o, v, v, o].copy()
term2 = -contract('mnej,nb->mbej', TEI[o, o, v, o], t1)
t2t1 = 0.5*t2 + contract('jf,nb->jnfb', t1, t1)
term34 = -contract('mnef,jnfb->mbej', TEI[o, o, v, v], t2t1)
term5 = contract('mbef,jf->mbej', TEI[o, v, v, v], t1)
total = term1 + term2 + term34 + term5
return total
########### Build T2 Equation################################
def T2eq_rhs(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
fae = self.Fae(t1, t2, F)
fmi = self.Fmi(t1, t2, F)
fme = self.Fme(t1, t2, F)
wmnij = self.Wmnij(t1, t2, F)
wabef = self.Wabef(t1, t2, F)
wmbej = self.Wmbej(t1, t2, F)
wabef_2 = self.Wabef_2(t1 ,t2, F)
wmnij_2 = self.Wmnij_2(t1 ,t2, F)
#All terms in the T2 Equation
term1 = TEI[o, o, v, v].copy()
term2tmp = fae - 0.5 *contract('me,mb->be', fme, t1)
term2a = contract('be,ijae->ijab', term2tmp, t2)
term2 = term2a - term2a.swapaxes(2, 3) #swap ab
term3temp = fmi + 0.5 *contract('me,je->mj', fme, t1)
term3a = -contract('mj,imab->ijab', term3temp, t2)
term3 = term3a - term3a.swapaxes(0, 1) #swap ij
tau = contract('ma,nb->mnab', t1, t1) - contract('na,mb->mnab', t1, t1)
term44 = 0.5*contract('mnij,mnab->ijab', wmnij, t2)
term55 = 0.5*contract('abef,ijef->ijab', wabef, t2)
term44 += 0.5*contract('mnij,mnab->ijab', wmnij_2, tau)
term55 += 0.5*contract('abef,ijef->ijab', wabef_2, tau)
term6tmp = contract('mbej,imae->ijab', wmbej, t2)
term6tmp = term6tmp - contract('mbej,ie,ma->ijab', TEI[o, v, v, o], t1, t1)
term6 = term6tmp - term6tmp.swapaxes(2, 3) - term6tmp.swapaxes(0, 1) + term6tmp.swapaxes(0, 1).swapaxes(2, 3)
term7tmp = contract('abej,ie->ijab', TEI[v ,v, v, o], t1)
term7 = term7tmp - term7tmp.swapaxes(0, 1) #swap ij
term8tmp = -contract('mbij,ma->ijab', TEI[o, v, o, o], t1)
term8 = term8tmp - term8tmp.swapaxes(2, 3) #swap ab
total = term1 + term2 + term3 + term44 + term55 + term6 + term7 + term8
return total
#Calculate the CCSD energy
def CCSD_Corr_E(self, t1, t2, F):
o = self.occ
v = self.vir
TEI = self.TEI
term1 = contract('ia,ia->',F[o, v], t1)
term2 = 0.25*contract('ijab,ijab->', TEI[o, o, v, v], t2)
term3 = 0.5*contract('ijab,ia,jb->', TEI[o, o, v, v], t1, t1)
total = term1 + term2
total = total + term3
return total
# update the T2 iteratively
def corrected_T2(self, t2, dt2, F):
o = self.occ
v = self.vir
eps, evecs = np.linalg.eigh(F)
Dem = eps[o].reshape(-1, 1, 1, 1)
Dem = Dem + eps[o].reshape(-1, 1, 1)
Dem = Dem - eps[v].reshape(-1, 1)
Dem = Dem - eps[v]
Dem = 1/Dem
t2 = t2 + contract('ijab,ijab->ijab', dt2, Dem)
return t2
# update the T1 iteratively
def corrected_T1(self, t1, dt1, F):
o = self.occ
v = self.vir
eps, evecs = np.linalg.eigh(F)
Dem = eps[o].reshape(-1, 1) - eps[v]
Dem = 1/Dem
t1 = t1 + contract('ia,ia->ia', dt1, Dem)
return t1
#Routine for DIIS solver, builds all arrays(maxsize) before B is computed
def DIIS_solver(self, t1, t2, F, maxsize, maxiter, E_min):
#Store the maxsize number of t1 and t2
T1rhs = self.T1eq_rhs(t1, t2, np.longdouble(F))
T2rhs = self.T2eq_rhs(t1, t2, np.longdouble(F))
t1 = np.longdouble(self.corrected_T1(t1, T1rhs, F))
t2 = np.longdouble(self.corrected_T2(t2, T2rhs, F))
t1stored = [t1.copy()]
t2stored = [t2.copy()]
errort1 = []
errort2 = []
for n in range(1, maxsize+1):
T1rhs = self.T1eq_rhs(t1, t2, np.longdouble(F))
T2rhs = self.T2eq_rhs(t1, t2, np.longdouble(F))
t1 = np.longdouble(self.corrected_T1(t1, T1rhs, F))
t2 = np.longdouble(self.corrected_T2(t2, T2rhs, F))
t1stored.append(t1.copy())
t2stored.append(t2.copy())
errort1.append(t1stored[n]- t1stored[n-1])
errort2.append(t2stored[n]- t2stored[n-1])
# Build B
B = np.ones((maxsize + 1, maxsize + 1)) * -1
B[-1, -1] = 0
for z in range(1, maxiter):
CCSD_E_old = self.CCSD_Corr_E( t1, t2, F)
for n in range(maxsize):
for m in range(maxsize):
a = contract('ia,ia->',errort1[m], errort1[n])
b = contract('ijab,ijab->', errort2[m], errort2[n])
B[n, m] = a + b
# Build residual vector
A = np.zeros(maxsize + 1)
A[-1] = -1
c = np.linalg.solve(B, A)
# Update t1 and t2
t1 = 0.0*t1
t2 = 0.0*t2
for n in range(maxsize):
t1 += c[n] * t1stored[n+1]
t2 += c[n] * t2stored[n+1]
oldt1 = t1.copy()
oldt2 = t2.copy()
#test if converged
CCSD_E = self.CCSD_Corr_E( t1, t2, F)
diff_E = CCSD_E - CCSD_E_old
if (abs(diff_E) < E_min):
break
#update t1 and t2 list
T1rhs = self.T1eq_rhs(t1, t2, np.longdouble(F))
T2rhs = self.T2eq_rhs(t1, t2, np.longdouble(F))
t1 = np.longdouble(self.corrected_T1(t1, T1rhs, F))
t2 = np.longdouble(self.corrected_T2(t2, T2rhs, F))
t1stored.append(t1.copy())
t2stored.append(t2.copy())
errort1.append(t1 - oldt1)
errort2.append(t2 - oldt2)
print("inter =", z, "\t", "CCSD_E =", CCSD_E,"diff=", diff_E)
del t1stored[0]
del t2stored[0]
del errort1[0]
del errort2[0]
return CCSD_E, t1, t2
#a regular iterative solver, Slow, don't use
def NO_DIIS_solver(self, t1, t2, F, maxsize, maxiter, E_min):
i=0
for x in range (maxiter):
CCSDE_Em = self.CCSD_Corr_E(t1, t2, F)
T1rhs = self.T1eq_rhs(t1, t2, np.longdouble(F))
T2rhs = self.T2eq_rhs(t1, t2, np.longdouble(F))
t1 = np.longdouble(self.corrected_T1(t1, T1rhs, F))
t2 = np.longdouble(self.corrected_T2(t2, T2rhs, F))
CCSD_E = self.CCSD_Corr_E(t1, t2, F)
diff_E = np.abs( CCSD_E -CCSDE_Em )
i+=1
if (abs(diff_E) < E_min):
break
print("inter =", i, "\t", "CCSD_E =", CCSD_E,"diff=", diff_E)
return CCSD_E, t1, t2
##############################################################################
#
#
#
# Lambda Equations (CCSD):
# Derived by R. Glenn
#
#
#
#######################################################################
# Build Fvv for L1 and L2
def LRFea(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = F[v, v].copy()
term2 = - contract('ma,me->ea', F[o, v], t1)
term3 = contract('emaf,mf->ea', TEI[v, o, v, v], t1)
tau = 0.5*t2 + contract('ia,jb->ijab', t1, t1)
term4 =-contract('mnaf,mnef->ea', TEI[o, o, v, v], tau)
#Fea = term1 + term2 + term3 + term4
total = term1 + term2 + term3 + term4
return total
#Build Foo for L1 and L2
def LRFim(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = F[o, o].copy()
term2 = contract('ie,me->im', F[o, v], t1)
term3 = contract('inmf,nf->im', TEI[o, o, o, v], t1)
tau = 0.5*t2 + contract('ia,jb->ijab', t1, t1)
term4 = contract('inef,mnef->im', TEI[o, o, v, v], tau)
total = term1 + term2 + term3 + term4
return total
#Build Wovvo
def LSWieam(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[o, v, v, o].copy()
term2 = contract('eifa,mf->ieam', TEI[v, o, v, v], t1)
term3 = -contract('nima,ne->ieam', TEI[o, o, o, v], t1)
tau = t2 + contract('ia,jb->ijab', t1, t1)
term4 = -contract('ijab,mjbe->ieam', TEI[o, o, v, v], tau)
total = term1 + term2 + term3 + term4
return total
#Build Wvvvo
def LRWefam(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
Fme = self.Fme(t1, t2, F)
Wabef = self.LSWabef(t1, t2, F)
term1 = 0.5*TEI[v, v, v, o].copy()
term2 = 0.5*contract('na,mnef->efam', Fme, t2)
term3 = contract('efab,mb->efam', Wabef, t1)
term4a = -TEI[o, v, v, o].copy() + contract('jnab,nmfb->jfam', TEI[o, o, v, v], t2)
term4 = contract('jfam,je->efam', term4a, t1)
tau =0.25*t2 + 0.5*contract('ia,jb->ijab', t1, t1) #- contract('ib,ja->ijab', t1, t1)
term5 = contract('jnam,jnef->efam', TEI[o, o, v, o], tau)
term6 = -contract('jfab,jmeb->efam', TEI[o, v, v, v], t2)
total = term1 + (term2 + term3 + term4 + term5 + term6) #+ extra
return total
#Build Wovoo
def LRWibjm(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
Fme = self.Fme(t1, t2, F)
Wmnij = self.LSWmnij(t1, t2, F)
term1 = -0.5*TEI[o, v, o, o].copy()
term2 = 0.5*contract('ie,jmbe->ibjm', Fme, t2)
term3 = contract('injm,nb->ibjm', Wmnij, t1)
term4a = -TEI[o, v, v, o].copy() - contract('inef,nmfb->ibem', TEI[o, o, v, v], t2)
term4 = contract('ibem,je->ibjm', term4a, t1)
tau = 0.25*t2 + 0.5*contract('ia,jb->ijab', t1, t1) #-contract('ib,ja->ijab', t1, t1)
term5 = -contract('ibef,jmef->ibjm', TEI[o, v, v, v], tau)
term6 = contract('inem,jneb->ibjm', TEI[o, o, v, o], t2)
total = term1 + (term2 + term3 + term4 + term5 + term6)
return total
def Gfe(self, t2, lam2):
return -0.5*contract('mnfb,mneb->fe', lam2, t2)
def Gmn(self, t2, lam2):
return 0.5*contract('njed,mjed->nm', lam2, t2)
#Build Wvovv
def LWfiea(self, t1):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[v, o, v, v].copy()
term2 = -contract('jiea,jf->fiea', TEI[o, o, v, v], t1)
total = term1 + term2
return total
#Build Wooov
def LWmina(self, t1):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = TEI[o, o, o, v].copy()
term2 = contract('mifa,nf->mina', TEI[o, o, v, v], t1)
total = term1 + term2
return total
###############Lam1 Equation#####################
def lam_1eq_rhs(self, t1, t2, lam1, lam2, F):
v = self.vir
o = self.occ
TEI = self.TEI
Fia = self.Fme(t1, t2, F)
Fea = self.LRFea(t1, t2, F)
Fim = self.LRFim(t1, t2, F)
Wieam = self.LSWieam(t1, t2, F)
Wefam = self.LRWefam(t1, t2, F)
Wibjm= self.LRWibjm(t1, t2, F)
Gef = self.Gfe(t2, lam2)
Gmn = self.Gmn(t2, lam2)
Weifa = self.LWfiea(t1)
Wmina = self.LWmina(t1)
term1 = Fia.copy()
term2 = contract('ea,ie->ia', Fea, lam1)
term3 = -contract('im,ma->ia', Fim, lam1)
term4 = contract('ieam,me->ia', Wieam, lam1)
term5 = contract('efam,imef->ia', Wefam, lam2)
term6 = contract('ibjm,jmab->ia', Wibjm, lam2)
term7 = -contract('fe,fiea->ia', Gef, Weifa)
term8 = -contract('nm,mina->ia', Gmn, Wmina)
total = (term1 + (term2 + term3 + term4) + term5 + term6 + term7 + term8)
return total
################################################################
# Build Woooo
def LSWmnij(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = 0.5*TEI[o, o, o, o].copy()
term2 = contract('ijme,ne->ijmn', TEI[o, o, o, v], t1)
tau = 0.25*t2 + 0.5*contract('ia,jb->ijab', t1, t1)
term3 = contract('ijfe,mnfe->ijmn', TEI[o, o, v, v], tau)
total = (term1 + term2 + term3)
return total
#Build Wvvvv
def LSWabef(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
term1 = 0.5*TEI[v, v, v, v].copy()
term2 = -contract('emab,mf->efab', TEI[v, o, v, v], t1)
tau = 0.25*t2 + 0.5*contract('ia,jb->ijab', t1, t1)
term3 = contract('nmab,nmef->efab', TEI[o, o, v, v], tau)
total = (term1 + term2 + term3 )
return total
########################Lam 2 Equations################
def lam2eq_rhs(self, t1, t2, lam1, lam2, F):
v = self.vir
o = self.occ
TEI = self.TEI
Feb = self.LRFea(t1, t2, F)
Fjm = self.LRFim(t1, t2, F)
Wijmn = self.LSWmnij(t1, t2, F)
Wefab = self.LSWabef(t1, t2, F)
Wjebm = self.LSWieam(t1, t2, F)
Wejab = self.LWfiea(t1)
Wijmb = self.LWmina(t1)
Fjb = self.Fme(t1, t2, F)
Gbe = self.Gfe(t2, lam2)
Gmj = self.Gmn(t2, lam2)
term1 = TEI[o, o, v, v]
term2a = contract('eb,ijae->ijab', Feb, lam2)
term2 = term2a - term2a.swapaxes(2,3)
term3a = -contract('jm,imab->ijab', Fjm, lam2)
term3 = term3a - term3a.swapaxes(0,1)
term4 = contract('ijmn,mnab->ijab', Wijmn, lam2)
term5 = contract('efab,ijef->ijab', Wefab, lam2)
term6a = contract('ejab,ie->ijab', Wejab, lam1)
term6 = term6a - term6a.swapaxes(0,1)
term7a = -contract('ijmb,ma->ijab', Wijmb, lam1)
term7 = term7a - term7a.swapaxes(2,3)
#term8 and 9
term89a = contract('jebm,imae->ijab', Wjebm, lam2) + contract('jb,ia->ijab', Fjb, lam1)
term89 = term89a
term89 = term89 - term89a.swapaxes(2,3)
term89 = term89 - term89a.swapaxes(0,1)
term89 = term89 + term89a.swapaxes(0,1).swapaxes(2,3)
term10a = contract('ijfb,af->ijab', TEI[o, o, v, v], Gbe)
term10 = term10a - term10a.swapaxes(2,3)
term11a = -contract('mjab,im->ijab', TEI[o, o, v, v], Gmj)
term11 = term11a - term11a.swapaxes(0,1)
total = term1 + term2 + term3 + term4 + (term5 + term6) + term7 + term89
total = total + term10 + term11
return total
def CCSD_pseudo_E(self, t1, t2, lam1, lam2, F):
o = self.occ
v = self.vir
TEI = self.TEI
term1 = contract('ia,ia->', F[o, v], lam1)
term2 = 0.25*contract('ijab,ijab->', TEI[o, o, v, v], lam2)
return term1, term2
def corrected_lam2(self, lam2, dlam2, F):
o = self.occ
v = self.vir
eps, evecs = np.linalg.eigh(F)
Dem = eps[o].reshape(-1, 1, 1, 1)
Dem = Dem + eps[o].reshape(-1, 1, 1)
Dem = Dem - eps[v].reshape(-1, 1)
Dem = Dem - eps[v]
Dem = 1/Dem
lam2 = lam2 + contract('ijab,ijab->ijab', dlam2, Dem)
return lam2
def corrected_lam1(self, lam1, dlam1, F):
o = self.occ
v = self.vir
eps, evecs = np.linalg.eigh(F)
Dem = eps[o].reshape(-1, 1) - eps[v]
Dem = 1/Dem
lam1 = lam1 + contract('ia,ia->ia', dlam1, Dem)
return lam1
def NO_DIIS_solve_lamr(self, t1, t2, lam1, lam2, F, maxsize, maxiter, E_min):
i=0
print("this is the convergence", E_min)
for x in range (maxiter):
E1, E2 = self.CCSD_pseudo_E(t1, t2, lam1, lam2, F)
pseudo_Em = E1 +E2
lam1rhs = self.lam_1eq_rhs(t1, t2, lam1, lam2, np.longdouble(F))
lam2rhs = self.lam2eq_rhs(t1, t2, lam1, lam2 ,np.longdouble(F))
lam1 = np.longdouble(self.corrected_lam1(lam1, lam1rhs, F))
lam2 = np.longdouble(self.corrected_lam2(lam2, lam2rhs, F))
E1, E2 = self.CCSD_pseudo_E(t1, t2, lam1, lam2, F)
pseudo_E = E1 +E2
diff_E = np.abs( pseudo_E -pseudo_Em )
i+=1
if (abs(diff_E) < E_min):
break
#pass
print("inter =", i, "\t", "pseudo_E =", pseudo_E,"diff=", diff_E)
print(E1, E2)
return pseudo_E, lam1, lam2
def DIIS_solver_Lam(self, t1, t2, lam1, lam2, F, maxsize, maxiter, E_min):
#Store the maxsize number of t1 and t2
lam1rhs = self.lam_1eq_rhs(t1, t2, lam1, lam2, np.longdouble(F))
lam2rhs = self.lam2eq_rhs(t1, t2, lam1, lam2 ,np.longdouble(F))
lam1 = np.longdouble(self.corrected_lam1(lam1, lam1rhs, F))
lam2 = np.longdouble(self.corrected_lam2(lam2, lam2rhs, F))
lam1stored = [lam1.copy()]
lam2stored = [lam2.copy()]
errort1 = []
errort2 = []
for n in range(1, maxsize+1):
lam1rhs = self.lam_1eq_rhs(t1, t2, lam1, lam2, np.longdouble(F))
lam2rhs = self.lam2eq_rhs(t1, t2, lam1, lam2 ,np.longdouble(F))
lam1 = np.longdouble(self.corrected_lam1(lam1, lam1rhs, F))
lam2 = np.longdouble(self.corrected_lam2(lam2, lam2rhs, F))
lam1stored.append(lam1.copy())
lam2stored.append(lam2.copy())
errort1.append(lam1stored[n]-lam1stored[n-1])
errort2.append(lam2stored[n]- lam2stored[n-1])
# Build B
B = np.ones((maxsize + 1, maxsize + 1)) * -1
B[-1, -1] = 0
for z in range(1, maxiter):
E1, E2 = self.CCSD_pseudo_E(t1, t2, lam1, lam2, F)
CCSD_E_old = E1 + E2
for n in range(maxsize):
for m in range(maxsize):
a = contract('ia,ia->',errort1[m], errort1[n])
b = contract('ijab,ijab->', errort2[m], errort2[n])
B[n, m] = a + b
# Build residual vector
A = np.zeros(maxsize + 1)
A[-1] = -1
c = np.linalg.solve(B, A)
# Update t1 and t2
lam1 = 0.0*lam1
lam2 = 0.0*lam2
for n in range(maxsize):
lam1 += c[n] * lam1stored[n+1]
lam2 += c[n] * lam2stored[n+1]
oldlam1 = lam1.copy()
oldlam2 = lam2.copy()
#test if converged
E1, E2 = self.CCSD_pseudo_E(t1, t2, lam1, lam2, F)
CCSD_E = E1 + E2
diff_E = CCSD_E - CCSD_E_old
if (abs(diff_E) < E_min):
break
#update t1 and t2 list
lam1rhs = self.lam_1eq_rhs(t1, t2, lam1, lam2, np.longdouble(F))
lam2rhs = self.lam2eq_rhs(t1, t2, lam1, lam2 ,np.longdouble(F))
lam1 = np.longdouble(self.corrected_lam1(lam1, lam1rhs, F))
lam2 = np.longdouble(self.corrected_lam2(lam2, lam2rhs, F))
lam1stored.append(lam1.copy())
lam2stored.append(lam2.copy())
errort1.append(lam1 - oldlam1)
errort2.append(lam2 - oldlam2)
print("inter =", z, "\t", "Pseudo_E =", CCSD_E,"diff=", diff_E)
#print("inter =", z, "\t", "CCSD_E =", CCSD_E,"diff=", diff_E, "lam1E=", E1, "lam2E=", E2
del lam1stored[0]
del lam2stored[0]
del errort1[0]
del errort2[0]
print("Lambda1 energy =", E1)
print("Lambda2 energy =", E2)
return CCSD_E, lam1, lam2
def print_2(self, t1):
t1_tmp = t1.ravel()
sort_t1 = sorted(t1_tmp, reverse=True)
for x in range(len(sort_t1)-1):
if (round(sort_t1[x], 10) ==0e13 or round(sort_t1[x+1], 13) == round(sort_t1[x], 13)):
pass
else:
print('\t', ('% 5.13f' % sort_t1[x]))
print('\t', ('% 5.13f' % sort_t1[-1]))
def remove_dup(self, t):
s = []
for i in t:
i = round(i, 10)
if i not in s:
s.append(i)
return s
def print_T_amp(self, t1, t2):
sort_t1 = sorted(t1.ravel())
sort_t1 = self.remove_dup(sort_t1)
sort_t2 = sorted(t2.ravel())
sort_t2 = self.remove_dup(sort_t2)
print("\n The largest T1 values:")
for x in range(len(sort_t1)):
#if (round(sort_t1[x], 5) ==0e5 or x % 2 or 30< x < 60 ):
if (round(sort_t1[x], 5) ==0e5 ):
pass
else:
print('\t', ('% 5.10f' % sort_t1[x]))
print("\n The largest T2 values are:")
for x in range(len(sort_t2)):
if (round(sort_t2[x],2) ==0e5 ): #or x % 2 or x > 20):
pass
else:
print('\t', ('% 5.10f' % sort_t2[x]))
def print_L_amp(self, lam1, lam2):
sort_lam1 = sorted(-abs(lam1.ravel()))
sort_lam1 = self.remove_dup(sort_lam1)
sort_lam2 = sorted(lam2.ravel())
sort_lam2 = self.remove_dup(sort_lam2)
print("\n The largest lam1 values:")
for x in range(len(sort_lam1)):
if (round(sort_lam1[x], 5) ==0e5):# or x % 2 or x >20):
pass
else:
print('\t', ('% 5.10f' % sort_lam1[x]))
print("\n The largest lam2 values are:")
for x in range(len(sort_lam2)):
if (round(sort_lam2[x],2) ==0.00 or x % 2 or x > 20):
pass
else:
print('\t', ('% 5.10f' % sort_lam2[x]))
############################################################
#
# T1 and T2-equations (CC2)
# By R. Glenn, I used T. Daniel Crawfords equations
#
#
#
############################################################
########### Build T2 Equation################################
def T2eq_rhs_CC2(self, t1, t2, F):
v = self.vir
o = self.occ
TEI = self.TEI
fae = F[v, v].copy()
fmi = F[o, o].copy()
wabef_2 = self.Wabef_2(t1 ,t2, F)
wmnij_2 = self.Wmnij_2(t1 ,t2, F)
#All terms in the T2 Equation
term1 = TEI[o, o, v, v].copy()
term2tmp = fae #- 0.5 *contract('me,mb->be', fme, t1)
term2a = contract('be,ijae->ijab', term2tmp, t2)
term2 = term2a - term2a.swapaxes(2, 3) #swap ab
term3temp = fmi #+ 0.5 *contract('me,je->mj', fme, t1)
term3a = -contract('mj,imab->ijab', term3temp, t2)
term3 = term3a - term3a.swapaxes(0, 1) #swap ij
tau = contract('ma,nb->mnab', t1, t1) - contract('na,mb->mnab', t1, t1)
term44 = 0.5*contract('mnij,mnab->ijab', wmnij_2, tau)
term55 = 0.5*contract('abef,ijef->ijab', wabef_2, tau)
term6tmp = - contract('mbej,ie,ma->ijab', TEI[o, v, v, o], t1, t1)
term6 = term6tmp - term6tmp.swapaxes(2, 3) - term6tmp.swapaxes(0, 1) + term6tmp.swapaxes(0, 1).swapaxes(2, 3)
term7tmp = contract('abej,ie->ijab', TEI[v ,v, v, o], t1)
term7 = term7tmp - term7tmp.swapaxes(0, 1) #swap ij
term8tmp = -contract('mbij,ma->ijab', TEI[o, v, o, o], t1)
term8 = term8tmp - term8tmp.swapaxes(2, 3) #swap ab
total = term1 + term2 + term3 + term44 + term55 + term6 + term7 + term8
return total
#Routine for DIIS solver, builds all arrays(maxsize) before B is computed
def DIIS_solver_CC2(self, t1, t2, F, maxsize, maxiter, E_min):
#Store the maxsize number of t1 and t2
T1rhs = self.T1eq_rhs(t1, t2, F)
T2rhs = self.T2eq_rhs_CC2(t1, t2, F)
t1 = self.corrected_T1(t1, T1rhs, F)
t2 = self.corrected_T2(t2, T2rhs, F)
t1stored = [t1.copy()]
t2stored = [t2.copy()]
errort1 = []
errort2 = []
for n in range(1, maxsize+1):
T1rhs = self.T1eq_rhs(t1, t2, F)
T2rhs = self.T2eq_rhs_CC2(t1, t2, F)
t1 = self.corrected_T1(t1, T1rhs, F)
t2 = self.corrected_T2(t2, T2rhs, F)
t1stored.append(t1.copy())
t2stored.append(t2.copy())
errort1.append(t1stored[n]- t1stored[n-1])
errort2.append(t2stored[n]- t2stored[n-1])
# Build B
B = np.ones((maxsize + 1, maxsize + 1)) * -1
B[-1, -1] = 0
for z in range(1, maxiter):
CC2_E_old = self.CCSD_Corr_E( t1, t2, F)
for n in range(maxsize):
for m in range(maxsize):
a = contract('ia,ia->',errort1[m], errort1[n])
b = contract('ijab,ijab->', errort2[m], errort2[n])
B[n, m] = a + b
# Build residual vector
A = np.zeros(maxsize + 1)
A[-1] = -1
c = np.linalg.solve(B, A)
# Update t1 and t2
t1 = 0.0*t1
t2 = 0.0*t2
for n in range(maxsize):
t1 += c[n] * t1stored[n+1]
t2 += c[n] * t2stored[n+1]
oldt1 = t1.copy()
oldt2 = t2.copy()
#test if converged
CC2_E = self.CCSD_Corr_E( t1, t2, F)
diff_E = CC2_E - CC2_E_old
if (abs(diff_E) < E_min):
break
#update t1 and t2 list
T1rhs = self.T1eq_rhs(t1, t2, F)
T2rhs = self.T2eq_rhs_CC2(t1, t2, F)
t1 = self.corrected_T1(t1, T1rhs, F)
t2 = self.corrected_T2(t2, T2rhs, F)
t1stored.append(t1.copy())
t2stored.append(t2.copy())
errort1.append(t1 - oldt1)
errort2.append(t2 - oldt2)
print("inter =", z, "\t", "CC2_E =", CC2_E,"diff=", diff_E)
del t1stored[0]
del t2stored[0]
del errort1[0]
del errort2[0]
return CC2_E, t1, t2
##################################################################
#
#
# Single-electron density matrix equations-derived by R. Glenn
#
#
#####################################################################
#Dipoles in the MO basis
def Defd_dipole(self):
C = np.asarray(self.C)
nmo = self.nmo
tmp_dipoles = self.mints.so_dipole()
dipoles_xyz = []
for n in range(3):
temp = contract('li,lk,kj->ij',C,tmp_dipoles[n],C)
temp = temp.repeat(2, axis=1).repeat(2, axis=0)
temp = temp*np.tile(np.identity(2),(nmo,nmo))
dipoles_xyz.append(temp)
return dipoles_xyz
#Build Dvv
def Dij(self, t1, t2, lam1, lam2):
term1 = contract('je,ie->ij', lam1, t1)
term2 = 0.5*contract('jmea,imea->ij', lam2, t2)
total = -(term1 + term2)
return total
#Build Doo
def Dab(self, t1, t2, lam1, lam2):
term1 = contract('nb,na->ab', lam1, t1)
term2 = 0.5*contract('mneb,mnea->ab', lam2, t2)
total = term1 + term2
return total
#Build Dvo
def Dai(self, t1, t2, lam1, lam2):
term1a = t1
term1 = contract('ia->ai', term1a)