-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBEATLES.py
More file actions
1408 lines (1288 loc) · 47.5 KB
/
Copy pathBEATLES.py
File metadata and controls
1408 lines (1288 loc) · 47.5 KB
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
#!/usr/bin/python
from __future__ import division
import sys
import math
import cmath
import numpy as np
from numpy import genfromtxt
import csv
from decimal import Decimal
import os
import random
# BEATLES: Bundle of Essential and Assistive Tools Library for Electronic Structure
# A tribute to the Beatles
#
# Updated May 30, 2020 by Hassan Harb
#
# / | \
# / | \
# /O O | O O\
# //|\ /|\ /|\ /|\\
# /=/ \=/ \= / \=/ \=\
# / == == == == == \
# / == == == == == \
# (The original Beatles)
# (ASCII retrieved from https://www.asciiart.eu/music/musicians/beatles )
#
#########################################################################
#
# NBasGrab: reads in a name of .fchk file
# output: -Number of basis functions
# -Charge
# -Multiplicity
# -Number of Atoms
# -Cartesian Coordinates
# -Atomic Symbols
# -SCF Energy
# -Total Energy (needs to be added)
# Section 1: Reading from gaussian formatted checkpoint file
def NBasGrab(filename):
NBasis = 0
NElem = 0
SCFEnergy = 0.0
Charge = 0
Multiplicity = 0
NAtoms = 0
temp = 1
with open(filename, 'r') as origin:
for line in origin:
if "Number of basis functions" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
NBasis = NBasis*10 + int(letter)
if "Charge " in line:
words = line.split()
for i in words:
for letter in i:
if(letter=="-"):
temp = -1
if(letter.isdigit()):
Charge = Charge*10 + int(letter)
Charge = Charge*temp
if "Multiplicity" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
Multiplicity = Multiplicity*10 + int(letter)
if "Number of atoms" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
NAtoms = NAtoms*10 + int(letter)
if "SCF Energy" in line:
words = line.split()
# print "SCF Energy = ", words[3], " Hartree"
SCFEnergy = float(words[3])
# print "SCF Energy (float) = ", SCFEnergy
# if "Total Energy" in line:
# words = line.split()
# TotalEnergy = float(words[3])
# print "Total Energy = ", TotalEnergy, " Hartree"
NElem = NBasis*NBasis
# print "Number of Basis Functions (subroutine) = ", NBasis, "\n"
# print "Charge (subroutine) = ", Charge, "\n"
return NBasis, NElem, Charge, Multiplicity, NAtoms, SCFEnergy
# GeomGet: reads in the file name, number of atoms
# Output: -One dimensional vector (NAtoms * 3) that includes the cartesian coordinates of each atom
#
def GeomGet(filename,NAtoms):
p = 0
r = 0
n = 1
NElements = NAtoms * 3
RawCart = np.zeros(NElements)
if (NElements%5 == 0):
n = 0
RawCartLines = int(NElements/5) + n
# print "Raw Cart lines = ", RawCartLines
# print "Number of Atoms =", NAtoms
# print "Number of coordinates =", NElements
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "Current cartesian coordinates" in line:
i = i + 1
pointer = i
# print "Cartesian Coordinates starts at line :", pointer
endpointer = pointer + RawCartLines - 1
# print "Cartesian Coordinates ends at line :", endpointer
for m in range(0,endpointer - pointer +1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
RawCart[r] = nextline[p]
r = r + 1
p = 0
# print "Raw Cart (subroutine) = ", RawCart
RawCart = RawCart/1.88973
# print "Raw Cart (converted to Angstroms) = ", RawCart
return RawCart
# GetAtoms: Reads in file name, number of atoms
# output: -One dimensional vector (NAtoms) that contains the atomic numbers of the atoms
#
def GetAtoms(filename1,NAtoms):
p = 0
r = 0
n = 1
AtomicNum = np.zeros(NAtoms)
if (NAtoms%6 ==0):
n = 0
AtomLines = int(NAtoms/6) + n
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Atomic numbers" in line:
i = i + 1
pointer = i
endpointer = pointer + AtomLines -1
for m in range(0, endpointer - pointer + 1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
AtomicNum[r] = nextline[p]
r = r + 1
p = 0
return AtomicNum
# MatGrab: Reads in filename, NBasis, user-defined switch
# Output: -Alpha MO Coefficients (Done)
# -Beta MO Coefficients (Done)
# -Alpha Density Matrix (Done)
# -Beta Density Matrix (Done)
# -Alpha MO Energies (Done)
# -Beta MO Energies (Done)
#
# Switch: 1 = Alpha MO Coefficients
# -1 = Beta MO Coefficients
# 2 = Alpha and Beta Density Matrices
# 3 = Alpha MO Energies
# -3 = Beta MO Energies
#
def MatGrab(filename,NBasis,switch):
if (switch == 1):
filename1 = filename
MOElements = NBasis * NBasis
MOlines = int(MOElements/5) + 1
if (NBasis%5 == 0):
MOlines = MOlines - 1
p = 0
r = 0
AOE = 0
MOrawa = np.zeros(NBasis*NBasis)
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Alpha Orbital Energies" in line:
AOE = i
if "Alpha MO coefficients" in line:
i=i+1
AMO=i
# print "Alpha MO coefficients starts at line :", i
j=i+MOlines-1
# print "Alpha MO coefficients ends at line :", j
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
MOrawa[r] = nextline[p]
r = r+1
p = 0
# print "MO Raw = ", MOrawa
return MOrawa
if (switch == -1):
filename1 = filename
MOElements = NBasis * NBasis
MOlines = int(MOElements/5) + 1
if (NBasis%5 == 0):
MOlines = MOlines - 1
p = 0
r = 0
BOE = 0
BMO = 0
MOrawb = np.zeros(NBasis*NBasis)
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Beta Orbital Energies" in line:
BOE = i
if "Beta MO coefficients" in line:
i=i+1
BMO=i
j=i+MOlines-1
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
MOrawb[r] = nextline[p]
r = r+1
p = 0
# print "MO Raw = ", MOrawb
return MOrawb
if (switch == 2):
filename1 = filename
PElements = int(NBasis*(NBasis+1)/2)
Plines = int(PElements/5) + 1
TotalPraw = np.zeros(PElements)
SpinPraw = np.zeros(PElements)
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Total SCF Density" in line:
i=i+1
r = 0
p = 0
# print "Total SCF Density starts at line :", i
j=i+Plines-1
# print "Total SCF Density ends at line :", j
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(0,len(nextline)):
if (r != PElements):
TotalPraw[r] = nextline[p]
r = r+1
p = 0
# HH + : Bug ... :(
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Spin SCF Density" in line:
# print "Found Spin density!"
i=i+1
r = 0
p = 0
# print "Spin SCF Density starts at line: ", i
j=i+Plines-1
# print "Spin SCF Density ends at line: ", j
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
if (r != PElements):
SpinPraw[r] = nextline[p]
r = r+1
p = 0
# HH - : End of bug (hopefully!)
PalphaRaw = (np.add(TotalPraw,SpinPraw)) * 0.5
PbetaRaw = (np.subtract(TotalPraw,SpinPraw)) * 0.5
Palpha = symmetrize(PalphaRaw)
Pbeta = symmetrize(PbetaRaw)
return Palpha, Pbeta
if (switch == 3):
filename1 = filename
AlphaMO = np.zeros(NBasis)
AlphaMOlines = int(NBasis/5) + 1
if (NBasis % 5 == 0):
AlphaMOlines = AlphaMOlines - 1
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Alpha Orbital Energies" in line:
i = i + 1
r = 0
p = 0
# print "Alpha MO Energies starts at line: ", i
j = i + AlphaMOlines - 1
# print "Alpha MO Energies ends at line: ", j
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
AlphaMO[r] = nextline[p]
r = r + 1
p = 0
# print "Alpha MO energies = ", AlphaMO
return AlphaMO
if (switch == -3):
filename1 = filename
BetaMO = np.zeros(NBasis)
BetaMOlines = int(NBasis/5) + 1
if (NBasis % 5 == 0):
BetaMOlines = BetaMOlines - 1
with open(filename1,'r') as origin:
for i, line in enumerate(origin):
if "Beta Orbital Energies" in line:
i = i + 1
r = 0
p = 0
# print "Beta MO Energies starts at line: ", i
j = i + BetaMOlines - 1
# print "Beta MO Energies ends at line: ", j
for m in range(0,j-i+1):
nextline = origin.next()
nextline = nextline.split()
for p in range(p,len(nextline)):
BetaMO[r] = nextline[p]
r = r + 1
p = 0
# print "Beta MO energies = ", BetaMO
return BetaMO
# sci_notation: reads in a number
# output: prints the number in the desired scientific notation. note that this function has a different output than the one found in nio.py
#
def sci_notation(n):
a = '%.8f' % n
return '%.8f' % Decimal(n.real)
# fchk_notation: reads in a number
# output: prints the number in the desired notation for fchk files
#
def fchk_notation(n):
a = '%.8E' % n
return '%.8E' % Decimal(n.real)
# AtomicSymbol: Reads in atomic number of the element
# Output: -Atomic Symbol
#
def AtomicSymbol(AtomicNumber):
p = AtomicNumber - 1
PTlist = ['H','He','Li','Be','B','C','N','O','F','Ne','Na','Mg','Al','Si','P','S','Cl','Ar','K','Ca','Sc','Ti','V','Cr','Mn','Fe','Co','Ni','Cu','Zn','Ga','Ge','As','Se','Br','Kr','Rb','Sr','Y','Zr','Nb','Mo','T','Ru','Rh','Pd','Ah','Cd','In','Sn','Sb','Te','I','Xe','Cs','Ba','La','Ce','Pr','Nd','Pm','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu','Hf','Ta','W','Re','Os','Ir','Pt','Au','Hb','Tl','Pb','Bi','Po','At','Rn','Fr','Ra','Ac','Th','Pa','U','Np','Pu','Am','Cm','Bk','Cf','Es','Fm','Md','No','Lr','Rf','Db','Sg','Bh','Hs','Mt','Ds','Rg','Cn','Uut','Fl','Uup','Lv','Uus','Uuo']
# print "There are currently ", len(PTlist), " atoms defined"
return PTlist[p]
# Symmetrize: Reads in a packed symmetric column matrix into NBasis x NBasis square matrix
# Output: -Matrix(NBasis,NBasis)
#
def symmetrize(a):
Nbas = int((np.sqrt(8*len(a)+1)-1)/2)
b = np.zeros((Nbas,Nbas))
n = 0
for i in range(0,Nbas):
for j in range(0,i+1):
b[i,j]=a[n]
b[j,i]=a[n]
n=n+1
return b
# Column2Square: Reads in a packed column matrix, number of basis functions.
# Output: -Matrix(NBasis,NBasis)
def column2square(A,NBasis):
C = np.zeros((NBasis,NBasis))
t=0
for i in range(0,NBasis):
for j in range(0,NBasis):
C[j,i]=float(A[t])
t=t+1
return C
# GetOverlap: Reads in packed column matrix, number of basis functions.
# Output: -Overlap Matrix (NBasis,NBasis)
def GetOverlap(A,NBasis):
C = column2square(A,NBasis)
CInv = np.linalg.inv(C)
S = np.dot(np.transpose(CInv),CInv)
return S
# PrintSI: Reads in filename, user-defined switch
# Output: -SCF Energy, Charge, Multiplicity, Geometry
#
# Switch: 1 = print to new file (filename1-SI.txt)
# -1 = print to screen
#
def PrintSI(filename1,switch):
NBasis, NElementsGrab, Charge, Multiplicity, NAtoms, SCFEnergy = NBasGrab(filename1)
AtomicNum = GetAtoms(filename1,NAtoms)
RawCart = GeomGet(filename1,NAtoms)
Cart = np.resize(RawCart,(NAtoms,3))
filename2 = os.path.splitext(filename1)[0] + "-SI.txt"
filename1 = os.path.splitext(filename1)[0]
if (switch == 1):
with open(filename2,'w') as f2:
f2.write("SI info for ")
f2.write(filename1)
f2.write("\n\n")
f2.write("SCF Energy = ")
f2.write(str(SCFEnergy))
f2.write(" Hartree")
f2.write("\n\n")
f2.write(str(Charge))
f2.write(" ")
f2.write(str(Multiplicity))
f2.write("\n")
for i in range(0,NAtoms):
h = i + 1
z = AtomicNum[i]
Atom = AtomicSymbol(int(z))
f2.write(Atom)
f2.write(" ")
for j in range(0,3):
if (Cart[i,j] >= 0):
f2.write(" ")
f2.write(str(sci_notation(Cart[i,j])))
f2.write(" ")
f2.write("\n")
f2.write(" ")
f2.write("\n\n")
return filename2
if (switch == -1):
print "SCF Energy = ", SCFEnergy, " Hartree\n"
print "Charge = ", Charge, "\n"
print "Multiplicity = ", Multiplicity, "\n"
print "Cartesian Geometry:\n"
for i in range(0,NAtoms):
h = i + 1
z = AtomicNum[i]
Atom = AtomicSymbol(int(z))
print Atom, sci_notation(Cart[i,0]), sci_notation(Cart[i,1]), sci_notation(Cart[i,2])
print "\n"
# CalcNO: Reads in filename, NBasis
# Output: Natural Orbitals eigenvalues and eigenvectors (both alpha and beta)
#
def CalcNO(filename,NBasis):
Palpha, Pbeta = MatGrab(filename,NBasis,2)
C = MatGrab(filename,NBasis,1)
S = GetOverlap(C,NBasis)
Svals, Svecs = np.linalg.eig(S)
Sval_minhalf = (np.diag(Svals**(0.5)))
Shalf = np.dot(Svecs,np.dot(Sval_minhalf,np.transpose(Svecs)))
NOvalsA, NOvecsA = np.linalg.eig(np.dot(Shalf,np.dot(Shalf,Palpha)))
NOvalsB, NOvecsB = np.linalg.eig(np.dot(Shalf,np.dot(Shalf,Pbeta)))
NOvalsA = NOvalsA.real
NOvalsB = NOvalsB.real
NOvecsA = NOvecsA.real
NOvecsB = NOvecsB.real
NOvecsA = np.dot(np.linalg.inv(Shalf),NOvecsA)
NOvecsB = np.dot(np.linalg.inv(Shalf),NOvecsB)
return NOvecsA, NOvecsB, NOvalsA, NOvalsB
# NElec: Reads in filename
# Output: Total number of electrons, Alpha Electrons, Beta Electrons
#
def NElec(filename):
NElec = 0
NAlpha = 0
NBeta = 0
with open(filename, 'r') as origin:
for line in origin:
if "Number of electrons" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
NElec = NElec*10 + int(letter)
if "Number of alpha electrons" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
NAlpha = NAlpha*10 + int(letter)
if "Number of beta electrons" in line:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
NBeta = NBeta*10 + int(letter)
return NElec, NAlpha, NBeta
# OrbTransform: Reads in Alpha Density Matrix, Beta Density Matrix, Overlap Matrix, n
# Output: New Density Matrices: P' = S**(1-n).P.S**(n)
#
def OrbTransform(Pa,Pb,S,n):
Svals, Svecs = np.linalg.eig(S)
Sval1 = np.diag(Svals**(n))
Sval2 = np.diag(Svals**(1-n))
Sdag1 = np.dot(Svecs,np.dot(Sval1,np.transpose(Svecs)))
Sdag2 = np.dot(Svecs,np.dot(Sval2,np.transpose(Svecs)))
PdagAlpha = np.dot(Sdag1,np.dot(Pa,Sdag2))
PdagBeta = np.dot(Sdag1,np.dot(Pb,Sdag2))
# print "OrbTransform Subroutine test:\n"
# print "PdagAlpha = ", PdagAlpha, "\n"
# print "PdagBeta = ", PdagBeta, "\n"
OvalsA, OvecsA = np.linalg.eig(PdagAlpha)
OvalsB, OvecsB = np.linalg.eig(PdagBeta)
# print "OVals A = ", OvalsA, "\n"
# print "OVecs A = ", OvecsA, "\n"
# print "OVals B = ", OvalsB, "\n"
# print "OVecs B = ", OvecsB, "\n"
return PdagAlpha, PdagBeta, OvecsA, OvecsB, OvalsA, OvalsB
# CartoZmat: Transforms Cartesian coordinates to z-matrix form
# Input: NAtoms, RawCart, AtomicNum
# Output: z-matrix printed on the screen
#
# Note that there are three other functions here, Dist, Angle, and Torsion.
# They are used to calculate the appropriate parameters for the z-matrix
# switch = 1 : print z-matrix to screen
# switch = -1 : print z-matrix to new textfile
def DistAB(e1,e2):
R = 0.0
for i in range(len(e1)):
R = R + (e1[i]-e2[i])**(2)
R = R**(0.5)
return R
def AngleABC(e1,e2,e3):
eab_x = (e2[0] - e1[0]) / DistAB(e1,e2)
eab_y = (e2[1] - e1[1]) / DistAB(e1,e2)
eab_z = (e2[2] - e1[2]) / DistAB(e1,e2)
ebc_x = - (e3[0] - e2[0]) / DistAB(e2,e3)
ebc_y = - (e3[1] - e2[1]) / DistAB(e2,e3)
ebc_z = - (e3[2] - e2[2]) / DistAB(e2,e3)
eab = [eab_x, eab_y, eab_z]
ebc = [ebc_x, ebc_y, ebc_z]
cos_angle = np.dot(eab,ebc)
angle = np.arccos(cos_angle) / 3.1415926535 * 180
return eab, ebc, angle
def TorsionABCD(e1,e2,e3,e4):
eab_x = (e2[0] - e1[0]) / DistAB(e1,e2)
eab_y = (e2[1] - e1[1]) / DistAB(e1,e2)
eab_z = (e2[2] - e1[2]) / DistAB(e1,e2)
ebc_x = (e3[0] - e2[0]) / DistAB(e2,e3)
ebc_y = (e3[1] - e2[1]) / DistAB(e2,e3)
ebc_z = (e3[2] - e2[2]) / DistAB(e2,e3)
ecd_x = (e4[0] - e3[0]) / DistAB(e3,e4)
ecd_y = (e4[1] - e3[1]) / DistAB(e3,e4)
ecd_z = (e4[2] - e3[2]) / DistAB(e3,e4)
eab = [eab_x, eab_y, eab_z]
ebc = [ebc_x, ebc_y, ebc_z]
ecd = [ecd_x, ecd_y, ecd_z]
n1 = np.cross(eab,ebc) / (np.linalg.norm(np.cross(eab,ebc)))
n2 = np.cross(ebc,ecd) / (np.linalg.norm(np.cross(ebc,ecd)))
u1 = n2
u3 = ebc/np.linalg.norm(ebc)
u2 = np.cross(u3,u1)
cos_angle = np.dot(n1,n2)
sin_angle = np.dot(n1,u2)
angle = -math.atan2(sin_angle,cos_angle) / 3.1415926535 * 180
return angle
def CartoZmat(RawCart,NAtoms,AtomicNum,filename2,switch):
if (switch == 1):
Cart = np.resize(RawCart,(NAtoms,3))
# print "Cartesian = ", Cart
# print "Atoms list = ", AtomicNum
for i in range(len(AtomicNum)):
Symbol = AtomicSymbol(int(AtomicNum[i]))
if (i > 2):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e3 = [Cart[2,0],Cart[2,1],Cart[2,2]]
e2 = [Cart[1,0],Cart[1,1],Cart[1,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
eab, ebc, A = AngleABC(e2,e1,e4)
D = TorsionABCD(e4,e1,e2,e3)
print Symbol, 1 , R , 2, A , 3, D
elif (i > 1):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e2 = [Cart[1,0],Cart[1,1],Cart[1,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
eab, ebc, A = AngleABC(e2,e1,e4)
print Symbol, 1 , R , 2, A
elif (i > 0):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
print Symbol, 1, R
elif (i == 0):
print Symbol
elif (switch == -1):
Cart = np.resize(RawCart,(NAtoms,3))
#open new file
filename = os.path.splitext(filename2)[0] + "-zmat.txt"
with open(filename,'w') as f2:
NBasis, NElem, Charge, Multiplicity, NAtoms, SCFEnergy = NBasGrab(filename2)
f2.write("Z-Matrix file for ")
f2.write(filename2)
f2.write("\n\n")
f2.write(str(Charge))
f2.write(" ")
f2.write(str(Multiplicity))
f2.write("\n")
for i in range(len(AtomicNum)):
Symbol = AtomicSymbol(int(AtomicNum[i]))
if (i > 2):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e3 = [Cart[2,0],Cart[2,1],Cart[2,2]]
e2 = [Cart[1,0],Cart[1,1],Cart[1,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
eab, ebc, A = AngleABC(e2,e1,e4)
D = TorsionABCD(e4,e1,e2,e3)
f2.write(Symbol)
f2.write(" 1 ")
f2.write(str(R))
f2.write(" 2 ")
f2.write( str(A))
f2.write(" 3 ")
f2.write(str(D))
f2.write("\n")
elif (i > 1):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e2 = [Cart[1,0],Cart[1,1],Cart[1,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
eab, ebc, A = AngleABC(e2,e1,e4)
f2.write(str(Symbol))
f2.write(" 1 ")
f2.write (str(R))
f2.write(" 2 ")
f2.write(str(A))
f2.write("\n")
elif (i > 0):
e4 = [Cart[i,0],Cart[i,1],Cart[i,2]]
e1 = [Cart[0,0],Cart[0,1],Cart[0,2]]
R = DistAB(e4,e1)
f2.write(Symbol)
f2.write(" 1 ")
f2.write(str(R))
f2.write("\n")
elif (i == 0):
f2.write(Symbol)
f2.write("\n")
# print "test test"
# Section 2: Reading from gaussian matrix files
# MatGrab2: Reads in matrices from gaussian matrix file
#
# Switch: 1 : Alpha Core Hamiltonian
# -1 : Beta Core Hamiltonian
# 2 : Alpha Fock Matrix
# -2 : Beta Fock Matrix
# 3 : Dipole matrix elements (x,y,z) [IN PROGRESS]
def MatGrab2(filename,NBasis,switch):
print "Reading from Matrix file\n"
if (switch == 1):
print "Reading Alpha Core Hamiltonian Matrix:\n"
NElements = int(NBasis*(NBasis + 1)/2)
print "Looking for ", NElements, " elements of the core hamilonian\n"
CoreHRawa = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "CORE HAMILTONIAN ALPHA" in line :
while (p < (NElements)):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done Reading Core Hamolitonian"
j = i+3
i = i + 4
end = j + NLines - 1
nextline = origin.next()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
CoreHRawa[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
return CoreHRawa
if (switch == -1):
print "Reading Beta Core Hamiltonian Matrix:\n"
NElements = int(NBasis*(NBasis + 1)/2)
print "Looking for ", NElements, " elements of the core hamilonian\n"
CoreHRawb = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "CORE HAMILTONIAN BETA" in line :
while (p < (NElements)):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done Reading Core Hamolitonian"
j = i+3
i = i + 4
end = j + NLines - 1
nextline = origin.next()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
CoreHRawb[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
return CoreHRawb
if (switch == 2):
print "Reading Alpha Fock Matrix:\n"
NElements = int(NBasis*(NBasis + 1)/2)
print "Looking for ", NElements, " elements of the fock matrix\n"
FockRawA = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "ALPHA FOCK MATRIX" in line :
while (p < (NElements)):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done Reading fock matrix"
j = i+3
i = i + 4
end = j + NLines - 1
nextline = origin.next()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
FockRawA[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
return FockRawA
if (switch == -2):
print "Reading Beta Fock Matrix:\n"
NElements = int(NBasis*(NBasis + 1)/2)
print "Looking for ", NElements, " elements of the fock matrix\n"
FockRawB = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "BETA FOCK MATRIX" in line :
while (p < (NElements)):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done Reading fock matrix"
j = i+3
i = i + 4
end = j + NLines - 1
nextline = origin.next()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
FockRawB[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
return FockRawB
if (switch == 3):
print "Reading Dipole integrals, matrix x\n"
NElements = int(NBasis*(NBasis +1)/2)
print "Looking for ", NElements, " elements of the Dipole integrals matrix x\n"
DipX_Raw = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if " DIPOLE INTEGRALS, matrix 1" in line:
while (p < NElements):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done reading Dipole X matrix\n"
j = i+3
i = i + 4
end = j + NLines -1
nextline = origin.next()
words = nextline.split()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
DipX_Raw[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
print "Dip X raw = ", DipX_Raw
print "Reading Dipole integrals, matrix y\n"
NElements = int(NBasis*(NBasis +1)/2)
print "Looking for ", NElements, " elements of the Dipole integrals matrix y\n"
DipY_Raw = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if " DIPOLE INTEGRALS, matrix 2" in line:
while (p < NElements):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done reading Dipole Y matrix\n"
j = i+3
i = i + 4
end = j + NLines -1
nextline = origin.next()
words = nextline.split()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
DipY_Raw[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
print "Dip Y raw = ", DipY_Raw
print "Looking for ", NElements, " elements of the Dipole integrals matrix z\n"
DipZ_Raw = np.zeros(NElements)
p = 0
n = 0
r = 0
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if " DIPOLE INTEGRALS, matrix 3" in line:
while (p < NElements):
NLines = NBasis - 5*r
if (NLines < 0):
print "Done reading Dipole Z matrix\n"
j = i+3
i = i + 4
end = j + NLines -1
nextline = origin.next()
words = nextline.split()
for m in range(i,i+NLines):
nextline = origin.next()
words = nextline.split()
for j in range(1,len(words)):
DipZ_Raw[p] = float(words[j].replace('D','E'))
p = p + 1
r = r + 1
i = m - 2
print "Dip Z raw = ", DipZ_Raw
return symmetrizeMat(DipX_Raw), symmetrizeMat(DipY_Raw), symmetrizeMat(DipZ_Raw)
# SymmetrizeMat: Reads in packed matrix (recovered from Matrix file) and prints out NBasis x NBasis matrix
# Input: Packed lower triangular A
# Output: N x N Matrix
def symmetrizeMat(a):
NBasis = int((np.sqrt(8*len(a)+1)-1)/2)
NewMat = np.zeros((NBasis,NBasis))
NElements = len(a)
t = 0
l = 0
start = 0
loop = NBasis
nBlock = int(NBasis/5)
nRem = NBasis%5
# print "nBlock = ", nBlock
# print "nRem = ", nRem
i = start
j = start
if (nBlock == 0):
nBlock =1
while (l < nBlock):
# print "retrieving block ", l
for i in range (start,loop):
for j in range(start,start+5):
if (j<=i):
# print "i,j = ",i,j
NewMat[i,j] = a[t]
NewMat[j,i] = a[t]
# print "A[t]= ", a[t]
t = t + 1
start = start + 5
l = l + 1
# print "t = ", t
# print "values of i and j after nBlock loop is over: ", i, j
j = j + 1
start = j
# print "NBasis - nRem = ", NBasis -nRem
i = NBasis - nRem
while (i < NBasis):
j = start
while (j <= i):
# print "i,j = ",i,j
NewMat[i,j] = a[t]
NewMat[j,i] = a[t]
# print "A[t]= ", a[t]
t = t + 1
j = j + 1
i = i + 1
# print "final value of t = ", t
return NewMat
# ERIRead: reads in regular 2e integrals from formatted matrix file
# Note that to get these integrals, use SCF=Conventional and int=NoRaff (saves integrals to disk and prints out regular 2e integrals)
# Input: matrix filename
# Output: 2D Matrix, two columns: Column 1 = compound index, Column 2 = integral value
#
# Two small functions are defined here: swap(a,b) and Fourindex(a,b,c,d)
def swap(a,b):
return b,a
def Fourindex(a,b,c,d):
a = int(a)
b = int(b)
c = int(c)
d = int(d)
if (a < b):
a, b = swap(a,b)
if (c < d):
c, d = swap(c,d)
e = int(a*(a+1)/2 + b)
f = int(c*(c+1)/2 + d)
if (e<f):
e,f = swap(e,f)
g = e*(e +1)/2 + f
return int(g)
def ERIRead(filename,NBasis):
NElements = 0
p = 0
print "Reading ERIs from Gaussian Matrix File"
print "Subroutine can only read regular 2e integrals (NO RAFINETTI)"
with open(filename,'r') as origin:
for i, line in enumerate(origin):
if "Label REGULAR 2E INTEGRALS" in line:
print "Found 2e integrals!"
words = line.split()
print "Total number of elements = ", words[9]
NElements = int(words[9])
print "NElements = ", NElements
eri_raw = np.zeros((NElements,5))
while (p < NElements):
nextline = origin.next()
words = nextline.split()
eri_raw[p,0] = words[1]
eri_raw[p,1] = words[3]
eri_raw[p,2] = words[5]
eri_raw[p,3] = words[7]
eri_raw[p,4] = float(words[9].replace('D','E'))
# print "(",int(eri_raw[p,0]),int(eri_raw[p,1]),"|",int(eri_raw[p,2]),int(eri_raw[p,3]),") = ", eri_raw[p,4]
p = p + 1
# print "ERI RAW = ", eri_raw
NTotal = Fourindex(NBasis,NBasis,NBasis,NBasis) + 1
eri_array = np.zeros(NTotal)
eri_compact = np.zeros((NElements,2))
print "Total length of sparse 1D vector =", NTotal