-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpySCF.py
executable file
·1303 lines (1008 loc) · 54 KB
/
pySCF.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
#!/usr/bin/env python3
# pySCF.
# Performs closed-shell Hartree-Fock SCF calculations using s-type orbitals only.
#
# Author: Raúl Coterillo ([email protected])
# Version: November 2021
from math import erf, floor
import numpy as np
import argparse
import sys, os
import time
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def main() -> None:
""" Main program loop. """
# parse command line arguments
p = argparse.ArgumentParser(
prog="pySCF.py",
description="Performs a closed-shell Hartree-Fock SCF calculation using s-type orbitals only.",
epilog="That's all, folks!"
)
p.add_argument("input_file", type=str, nargs=1, action="store",
help="input file for the program")
p.add_argument("-r", "-R", "--read-integrals", dest="read_ints", action="store_true",
help="read integrals from the input file (default: calculate integrals instead)")
p.add_argument("-w", "-W", "--write-integrals", dest="write_ints", action="store_true",
help="write integrals to a file called 'integrals.txt' (default: do not write)")
p.add_argument("-s", "-S", "-SCF", "--write-SCF", dest="write_SCF", action="store_true",
help="write SCF matrices to a file called 'SCF.txt' (default: do not write)")
p.add_argument("-b", "-B", "--external-basis", dest="basis", type=str, nargs=1, action="store", default=None,
help="reads basis from Gaussian basis file (.gbs file from basissetexchange.org)")
p.add_argument("-i", "-I", "--max-iterations", dest="maxits", type=int, nargs=1, action="store", default=[100],
help="sets the maximum number of SCF cycles (default: 100)")
p.add_argument("-e", "-E", "--energy-threshold", dest="Ethresh", type=float, nargs=1, action="store", default=[1e-8],
help="sets the energy convergence threshold (default: 1E-8 Ha)")
p.add_argument("-p", "-P", "--density-threshold", dest="Pthresh", type=float, nargs=1, action="store", default=[1e-8],
help="sets the density matrix convergence threshold (default: 1E-8)")
args = p.parse_args()
# print the program header
HF.print_header()
# format the command line arguments
INPUT_FILE = args.input_file[0]
if args.basis is not None:
BASIS_FILE = args.basis[0]
else:
BASIS_FILE = None
MAXITS = args.maxits[0] # max number of SCF iterations
ETHRESH = args.Ethresh[0] # energy convergence threshold
PTHRESH = args.Pthresh[0] # density convergence treshold
READ_INTEGRALS = args.read_ints # read integrals from input?
WRITE_INTEGRALS = args.write_ints # write integrals to file?
WRITE_SCF = args.write_SCF # write SCF matrices to file?
# check if the input files exist
if not os.path.isfile(INPUT_FILE):
print(f"ERROR: Input file '{INPUT_FILE}' does not exist.")
sys.exit()
if BASIS_FILE is not None and not os.path.isfile(BASIS_FILE):
print(f"ERROR: Basis file '{BASIS_FILE}' does not exist.")
sys.exit()
# initialize the program (read input information)
sim = HF(INPUT_FILE, basis_file=BASIS_FILE)
# print the information from the input file
sim.print_sim_info()
# run scf calculation
sim.run_single_point(
max_iter=MAXITS,
e_thresh=ETHRESH,
p_thresh=PTHRESH,
read_integrals=READ_INTEGRALS,
write_integrals=WRITE_INTEGRALS,
write_SCF=WRITE_SCF
)
# print timer summary
sim.tm.print_summary()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
class HF():
"""
A class to perform Hartree-Fock calculations.
Attributes
----------
input_file : str
input file of the simulation
basis_file : str
Gaussian external basis set file
tm : Timer
internal class that stores all execution time information
nats : int
number of atoms
atnums : np.array[nats]
atomic numbers
labels : list[nats]
atomic labels
positions : np.array[nats,3]
atomic positions (in Angstrom)
charge : int
overall system charge
nels : int
number of electrons
norbs : int
number of basis functions
max_nprim : int
max number of primitives per basis function
basis_centers : nd.array[norbs]
atomic centers of the basis functions
basis_nprims : nd.array[norbs]
number of primitives per basis function
basis_values : nd.array[norbs, max_nprim, 2]
exponents and contraction coefficients of the primitives
S : np.array[norbs, norbs]
overlap matrix
T : np.array[norbs, norbs]
kinetic energy integral matrix
V : np.array[norbs, norbs]
potential energy integral matrix
O : np.array[norbs, norbs, norbs, norbs]
two-electron integral matrix
Methods
-------
__init__(input_file:str, basis_file:str=None) -> None:
Initializes the class and parses all the input file information.
print_header(textwidth=TEXTW, textshift=TEXTS) -> None:
prints the program name and authors
print_sim_info(self, textwidth:int=TEXTW, textshift=TEXTS) -> None:
Prints a summary of the information in the input file.
bohr_geometry(self) -> np.array:
returns the system's geometry (self.position) in Bohr
distance_bohr(self, i:int, j:int) -> float:
return the distance between two atoms, in Bohr
distance_angstrom(self, i:int, j:int) -> float:
return the distance between two atoms, in Angstrom
read_integrals(self) -> list:
reads the integrals from the input file
calc_integrals(self) -> list:
calculate the integrals using the basis set information
write_integrals(self, fname:str, prec=16) -> None:
writes the integrals to an external file
nuclear_repulsion(self) -> float:
calculates the inter-nuclear repulsion energy
SCF_setup(self) -> None:
creates the transformation matrix and a guess for the density matrix
SCF_step(self) -> None:
performs an self-consistent field step
print_orbitals(self, textwidth:int=TEXTW, textshift:int= TEXTS,) -> None:
print orbital energies and coefficients
print_population(self, textwidth:int=TEXTW, textshift:int= TEXTS,) -> None:
prints a Mulliken/Lowdin population analysis of the orbitals
write_SCF(self, fname:str, prec=16) -> None:
writes the SCF matrices (X, P, Hc, F, etc) to an external file
run_single_point(self, max_iter:int = 100,
e_thresh:float = 1e-8,
p_thresh:float = 1e-8,
textwidth:int = TEXTW,
textshift:int = TEXTS,
read_integrals:bool = False,
write_integrals:bool= False,
write_SCF:bool = False) -> None:
performs a complete single point calculation.
Classes
-------
Timer()
registers the execution time of the different functions
"""
# default printing parameters
TEXTW = 72 # text width
TEXTS = 4 # text shift
def __init__(self, input_file:str, basis_file:str=None) -> None:
"""
Initializes the class and parses all the input file information.
Parameters
----------
input_file : str
input file containing all the SCF information
input_file : str
input file containing all the SCF information
"""
self.tm = self.Timer(self.TEXTW, self.TEXTS)
self.input_file = input_file
self.basis_file = basis_file
# parse input file
with open(input_file, "r") as f:
self.tm.start("read_input")
# read number of atoms
f.readline() # number of atoms
self.nats = int(f.readline().split()[0])
# read geometry
f.readline() # Atom labels, atom number, coords (Angstrom)
self.labels = list(np.zeros(self.nats))
self.atnums = np.zeros((self.nats), dtype=int)
self.positions = np.zeros((self.nats, 3), dtype=float)
for at in range(self.nats):
line = f.readline().split()
self.labels[at] = line[0]
self.atnums[at] = int(line[1])
self.positions[at,:] = [float(x) for x in line[2:]]
# read charge
f.readline() # Overall charge
self.charge = int(f.readline().split()[0])
self.nels = np.sum(self.atnums) - self.charge
if self.nels%2 != 0:
print("WARNING")
if basis_file is None:
# read number of basis functions
f.readline() # Number of basis funcs
self.norbs = int(f.readline().split()[0])
# read max number of primitives
f.readline() # Maximum number of primitives
self.max_nprim = int(f.readline().split()[0])
# read basis
f.readline() # Basis set: Func no, At label, Z, Atom no // nprim // (zeta cjk)
self.basis_centers = np.zeros(self.norbs, dtype=int)
self.basis_nprims = np.zeros(self.norbs, dtype=int)
self.basis_values = np.zeros((self.norbs, self.max_nprim, 2), dtype=float)
for orb in range(self.norbs): # iterate over basis functions
line = f.readline().split() # read line
self.basis_centers[orb] = int(line[3]) - 1 # atomic center
self.basis_nprims[orb] = int(f.readline().split()[0]) # number of primitives
for p in range(self.basis_nprims[orb]):
self.basis_values[orb, p, :] = [float(x) for x in f.readline().split()[:2]]
else:
# read basis info
with open(basis_file, "r") as b:
for _ in range(12): # skip the file header
b.readline()
basis = {}
while True:
line = b.readline()
# check if EOF has been reached
if line == "":
break
norb = 0
label, _ = line.split()
basis[label] = {}
end_of_atom = False
# read atom info
while not end_of_atom:
line = b.readline()
# check if end of atom is reached
if "*" in line:
end_of_atom = True
# otherwise, read a new orbital
else:
# read orbital information
basis[label][norb] = []
orb_type, nprim, _ = line.split()
# if the orbital is s-type, keep it
if orb_type == "S":
# read the primitives for the orbital
for p in range(int(nprim)):
data = [float(x.replace('D', 'E')) for x in b.readline().split()]
basis[label][norb].append(data)
# if it is not s-type, discard it
else:
for p in range(int(nprim)):
b.readline()
basis[label].pop(norb)
norb += 1
# adapt to the original data structures
self.norbs = 0
self.max_nprim = 0
for at in self.labels:
self.norbs += len(basis[at])
for orb in basis[at].keys():
if len(basis[at][orb]) > self.max_nprim:
self.max_nprim = len(basis[at][orb])
self.basis_centers = np.zeros(self.norbs, dtype=int)
self.basis_nprims = np.zeros(self.norbs, dtype=int)
self.basis_values = np.zeros((self.norbs, self.max_nprim, 2), dtype=float)
orb_index = 0
for at in range(self.nats):
lab = self.labels[at]
for orb in basis[lab].keys():
self.basis_centers[orb_index] = at
self.basis_nprims[orb_index] = len(basis[lab][orb])
for p in range(self.basis_nprims[orb_index]):
self.basis_values[orb_index, p, 0] = basis[lab][orb][p][0]
# apply normalization
N = np.power(2*self.basis_values[orb_index, p, 0]/np.pi, 3./4.)
self.basis_values[orb_index, p, 1] = N*basis[lab][orb][p][1]
orb_index += 1
self.n_1e_ints = int(self.norbs*(self.norbs+1)/2)
self.n_2e_ints = int(self.norbs*(self.norbs+1)*(self.norbs**2+self.norbs+2)/8)
self.tm.stop("read_input")
pass
def print_header(textwidth=TEXTW, textshift=TEXTS) -> None:
sh = textshift
print()
print(" "*sh + "*"*textwidth)
print("")
print(" "*sh + " "*int((textwidth-34)/2) + r' _____ _____ ______ ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r' / ____|/ ____| ____|' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r' _ __ _ _| (___ | | | |__ ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r'| _ \| | | |\___ \| | | __| ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r'| |_) | |_| |____) | |____| | ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r'| .__/ \__, |_____/ \_____|_| ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r'| | __/ | ' + " "*int((textwidth-34)/2))
print(" "*sh + " "*int((textwidth-34)/2) + r'|_| |___/ ' + " "*int((textwidth-34)/2))
print("")
print(" "*sh + f"{'a Python-based Hartree-Fock SCF implementation':^{textwidth}}")
print("")
print(" "*sh + f"{'Raúl Coterillo Ruisánchez':^{textwidth}}")
print(" "*sh + f"{'[email protected]':^{textwidth}}")
print(" "*sh + f"{'':^{textwidth}}")
print(" "*sh + f"{'November 2021':^{textwidth}}")
print()
print(" "*sh + "*"*textwidth)
print()
def print_sim_info(self, textwidth:int=TEXTW, textshift=TEXTS) -> None:
"""
Prints a summary of the information in the input file.
Parameters
----------
textwidth : int (Default 80)
number of characters per row
"""
sh = textshift
half = int(textwidth/2)
quart = int(textwidth/4)
eigth = int(textwidth/8)
print(" "*sh + f"{'~#~#~#~ INPUT FILE INFORMATION ~#~#~#~':^{textwidth}}")
print("")
print(" "*sh + f"{'Input file:':{quart}}{self.input_file:>{quart+half}}")
if not (self.basis_file is None):
print(" "*sh + f"{'Basis file:':{quart}}{self.basis_file:>{quart+half}}")
print("")
print(" "*sh + f"{'Number of Atoms:':{half}}{self.nats:>{half}}")
print(" "*sh + f"{'Input Geometry:':<{textwidth}}")
print("")
print(" "*sh + "-"*textwidth)
print(" "*sh + f"{'Index':^{eigth}}{'Label':^{eigth}}{'x (Ang)':^{quart}}{'y (Ang)':^{quart}}{'z (Ang)':^{quart}}")
print(" "*sh + "-"*textwidth)
for at in range(self.nats):
print(" "*sh + f"{at:^{eigth}}{self.labels[at]:^{eigth}}{self.positions[at][0]:^{quart}.6f}{self.positions[at][1]:^{quart}.6f}{self.positions[at][2]:^{quart}.6f}")
print(" "*sh + "-"*textwidth)
print("")
print(" "*sh + f"{'Number of Electrons:':{half}}{self.nels:{half}}")
print(" "*sh + f"{'Overall Charge:':{half}}{self.charge:{half}}")
print("")
print(" "*sh + f"{'Number of Basis Functions:':{half}}{self.norbs:{half}}")
print(" "*sh + f"{'Number of Primitive Gaussians:':{half}}{np.sum(self.basis_nprims):{half}}")
print(" "*sh + f"{'Basis Set Specification:':<{textwidth}}")
print("")
print(" "*sh + "-"*textwidth)
print(" "*sh + f"{'Orbital':^{eigth}}{'At.Lab':^{eigth}}{'At.Ind':^{eigth}}{'N.Prim':^{eigth}}{'Zeta':^{quart}}{'N':^{quart}}")
print(" "*sh + "-"*textwidth)
for orb in range(self.norbs):
print(" "*sh + f"{orb:^{eigth}}{self.labels[self.basis_centers[orb]]:^{eigth}}{self.basis_centers[orb]:^{eigth}}{self.basis_nprims[orb]:^{eigth}}{'':^{half}}")
for p in range(self.basis_nprims[orb]):
print(" "*sh + f"{'':^{half}}{self.basis_values[orb, p, 0]:^{quart}.8f}{self.basis_values[orb, p, 1]:^{quart}.8f}")
print(" "*sh + "-"*textwidth)
print("")
# ========================================================================================= #
# #
# Functions related to with system's geometry. #
# #
# ========================================================================================= #
def bohr_geometry(self) -> np.array:
""" Returns the system's geometry array, in bohr. """
return self.positions/0.529177210903
def distance_bohr(self, i:int, j:int) -> float:
""" Returns the distance between two atoms, in bohr. """
return np.linalg.norm(self.positions[i,:] - self.positions[j,:])/0.529177210903
def distance_angstrom(self, i:int, j:int) -> float:
""" Returns the distance between two atoms, in angstrom. """
return np.linalg.norm(self.positions[i,:] - self.positions[j,:])
# ========================================================================================= #
# #
# Functions related with matrix element integral calculations. #
# #
# ========================================================================================= #
def read_integrals(self) -> list:
"""
Reads the integrals from the input file.
Returns
-------
S : np.array[norbs, norbs]
overlap matrix
T : np.array[norbs, norbs]
kinetic energy integral matrix
V : np.array[norbs, norbs]
potential energy integral matrix
O : np.array[norbs, norbs, norbs, norbs]
two-electron integral matrix
"""
self.tm.start("read_integrals")
K = self.norbs
self.S = np.zeros((K, K), dtype=float) # overlap matrix
self.T = np.zeros((K, K), dtype=float) # kinetic integrals
self.V = np.zeros((K, K), dtype=float) # nuclear integrals
self.O = np.zeros((K, K, K, K), dtype=float) # two-electron integrals
# parse input file
with open(self.input_file, "r") as f:
# skip all lines until "Overlap integrals" is read
while True:
if "Overlap" in f.readline():
break
self.tm.start("1e_ints", parent="read_integrals")
# read overlap integrals
for j in range(self.norbs):
for i in range(j+1):
self.S[i,j] = float(f.readline().split()[2])
# symmetry ops
self.S[j,i] = self.S[i,j]
# read kinetic integrals
f.readline() # Kinetic integrals
for j in range(self.norbs):
for i in range(j+1):
self.T[i,j] = float(f.readline().split()[2])
# symmetry ops
self.T[j,i] = self.T[i,j]
# read nuclear integrals
f.readline() # Nuclear Attraction integrals
for j in range(self.norbs):
for i in range(j+1):
self.V[i,j] = float(f.readline().split()[2])
# symmetry ops
self.V[j,i] = self.V[i,j]
self.tm.stop("1e_ints", parent="read_integrals")
self.tm.start("2e_ints", parent="read_integrals")
# read two electron integrals
f.readline() # Two-Electron integrals
for i in range(K):
for j in range(K):
if i >= j:
for k in range(K):
for l in range(K):
if k >= l:
if i*(i+1)/2.+j >= k*(k+1)/2+l:
self.O[i,j,k,l] = float(f.readline().split()[4])
# symmetry ops
self.O[j,i,k,l] = self.O[i,j,k,l] # i <-> j
self.O[i,j,l,k] = self.O[i,j,k,l] # l <-> k
self.O[j,i,l,k] = self.O[i,j,k,l] # i <-> j, l<->k
self.O[k,l,i,j] = self.O[i,j,k,l] # ij <-> kl
self.O[k,l,j,i] = self.O[i,j,k,l] # ij <-> kl, i <-> j
self.O[l,k,i,j] = self.O[i,j,k,l] # ij <-> kl, l <-> k
self.O[l,k,j,i] = self.O[i,j,k,l] # ij <-> kl, i <-> j, l<->k
self.tm.stop("2e_ints", parent="read_integrals")
self.tm.stop("read_integrals")
return self.S, self.T, self.V, self.O
def calc_integrals(self) -> list:
"""
Calculates the required integrals using the structure and basis set information.
Returns
-------
S : np.array[norbs, norbs]
overlap matrix
T : np.array[norbs, norbs]
kinetic energy integral matrix
V : np.array[norbs, norbs]
potential energy integral matrix
O : np.array[norbs, norbs, norbs, norbs]
two-electron integral matrix
"""
self.tm.start("calc_integrals")
K = self.norbs
geom = self.bohr_geometry() # atomic positions in bohr
charge = self.atnums # atomic charges
nprims = self.basis_nprims # nº of primitives of each contracted basis function
values = self.basis_values # primitive coefficients of each contracted basis function
centers = self.basis_centers # center of each contracted basis function
self.S = np.zeros((K, K), dtype=float) # overlap matrix
self.T = np.zeros((K, K), dtype=float) # kinetic integrals
self.V = np.zeros((K, K), dtype=float) # nuclear integrals
self.O = np.zeros((K, K, K, K), dtype=float) # two-electron integrals
self.tm.start("1e_ints", parent="calc_integrals")
# precalculate some terms to speed up later iterations
dist = np.zeros((K,K), dtype=float) # interatomic distances
zeta = np.zeros((K, self.max_nprim, K, self.max_nprim), dtype=float) # zeta terms
xi = np.zeros((K, self.max_nprim, K, self.max_nprim), dtype=float) # xi terms
normc = np.zeros((K, self.max_nprim, K, self.max_nprim), dtype=float) # normalizations
gausp = np.zeros((K, self.max_nprim, K, self.max_nprim, 3), dtype=float) # gaussian products
# iterate over required primitive pairs
for i in range(K):
for j in range(i+1):
# intercenter distance squared
dist[i,j] = np.power(self.distance_bohr(centers[i],centers[j]),2)
dist[j,i] = dist[i,j]
for a in range(nprims[i]):
for b in range(nprims[j]):
# zeta
zeta[i,a,j,b] = values[i,a,0] + values[j,b,0]
zeta[j,b,i,a] = zeta[i,a,j,b]
# xi
xi[i,a,j,b] = values[i,a,0]*values[j,b,0]/zeta[i,a,j,b]
xi[j,b,i,a] = xi[i,a,j,b]
# d (normalization coefficients)
normc[i,a,j,b] = values[i,a,1]*values[j,b,1]
normc[j,b,i,a] = normc[i,a,j,b]
# gaussian product centers
gausp[i,a,j,b] = (values[i,a,0]*geom[centers[i]] + values[j,b,0]*geom[centers[j]])/zeta[i,a,j,b]
# iterate over the required one-center integrals
for i in range(K):
for j in range(i+1):
# iterate over primitives
for a in range(nprims[i]):
for b in range(nprims[j]):
S_iajb = np.exp(-xi[i,a,j,b]*dist[i,j])*np.sqrt(np.power((np.pi/zeta[i,a,j,b]),3))
T_iajb = xi[i,a,j,b]*(3 - 2*xi[i,a,j,b]*dist[i,j])*S_iajb
self.S[i,j] += normc[i,a,j,b]*S_iajb
self.T[i,j] += normc[i,a,j,b]*T_iajb
for n in range(self.nats): # iterate over nuclei
V_iajbN = -2*charge[n]*np.sqrt(zeta[i,a,j,b]/np.pi)*S_iajb
# check if basis funcs are on the same atom for the Bois function
if centers[i] == centers[j] == n:
bois0 = 1
else:
x = zeta[i,a,j,b]*np.power(np.linalg.norm(geom[n] - gausp[i,a,j,b]),2)
bois0 = 0.5*np.sqrt(np.pi/x)*erf(np.sqrt(x))
self.V[i,j] += normc[i,a,j,b]*V_iajbN*bois0
# symmetry ops
self.S[j,i] = self.S[i,j]
self.T[j,i] = self.T[i,j]
self.V[j,i] = self.V[i,j]
self.tm.stop("1e_ints", parent="calc_integrals")
self.tm.start("2e_ints", parent="calc_integrals")
# iterate over the required two-center integrals
for i in range(K):
for j in range(K):
if i >= j:
for k in range(K):
for l in range(K):
if k >= l:
if i*(i+1)/2.+j >= k*(k+1)/2+l:
# iterate over primitives
for a in range(nprims[i]):
for b in range(nprims[j]):
for c in range(nprims[k]):
for d in range(nprims[l]):
K_iajb = np.sqrt(2)*np.pi**(5./4.)/zeta[i,a,j,b]*np.exp(-xi[i,a,j,b]*dist[i,j])
K_kcld = np.sqrt(2)*np.pi**(5./4.)/zeta[k,c,l,d]*np.exp(-xi[k,c,l,d]*dist[k,l])
gausp_dist = np.linalg.norm(gausp[i,a,j,b] - gausp[k,c,l,d])
# Boys function check
if gausp_dist < 1e-5:
bois0 = 1
else:
rho = zeta[i,a,j,b]*zeta[k,c,l,d]/(zeta[i,a,j,b]+zeta[k,c,l,d])
x = rho*np.power(gausp_dist,2)
bois0 = 0.5*np.sqrt(np.pi/x)*erf(np.sqrt(x))
self.O[i,j,k,l] += normc[i,a,j,b]*normc[k,c,l,d]*K_iajb*K_kcld*bois0/np.sqrt(zeta[i,a,j,b]+zeta[k,c,l,d])
# symmetry ops
self.O[j,i,k,l] = self.O[i,j,k,l] # i <-> j
self.O[i,j,l,k] = self.O[i,j,k,l] # l <-> k
self.O[j,i,l,k] = self.O[i,j,k,l] # i <-> j, l<->k
self.O[k,l,i,j] = self.O[i,j,k,l] # ij <-> kl
self.O[k,l,j,i] = self.O[i,j,k,l] # ij <-> kl, i <-> j
self.O[l,k,i,j] = self.O[i,j,k,l] # ij <-> kl, l <-> k
self.O[l,k,j,i] = self.O[i,j,k,l] # ij <-> kl, i <-> j, l<->k
self.tm.stop("2e_ints", parent="calc_integrals")
self.tm.stop("calc_integrals")
return self.S, self.T, self.V, self.O
def write_integrals(self, fname:str, prec=16) -> None:
"""
Writes the stored integrals in a file.
Parameters
----------
fname : str
name of the file where the integrals will be written
prec : int (default: 16)
decimal precision with which to write the matrix values
"""
self.tm.start("write_integrals")
K = self.norbs
# create output file
with open(fname, "w") as f:
self.tm.start("1e_ints", parent="write_integrals")
# write overlap integrals
f.write("Overlap integrals:\n")
for j in range(self.norbs):
for i in range(j+1):
f.write(f"{i:6d}{j:6d}{'':6}{self.S[i,j]:>18.{prec}f}\n")
# write kinetic integrals
f.write("\nKinetic Integrals:\n")
for j in range(self.norbs):
for i in range(j+1):
f.write(f"{i:6d}{j:6d}{'':6}{self.T[i,j]:>18.{prec}f}\n")
# write nuclear integrals
f.write("\nNuclear Integrals:\n")
for j in range(self.norbs):
for i in range(j+1):
f.write(f"{i:6d}{j:6d}{'':6}{self.T[i,j]:>18.{prec}f}\n")
self.tm.stop("1e_ints", parent="write_integrals")
self.tm.start("2e_ints", parent="write_integrals")
# write two-electron integrals
f.write("\nTwo-Electron Integrals:\n")
for i in range(K):
for j in range(K):
if i >= j:
for k in range(K):
for l in range(K):
if k >= l:
if i*(i+1)/2.+j >= k*(k+1)/2+l:
f.write(f"{i:6d}{j:6d}{k:6d}{l:6d}{'':6}{self.O[i,j,k,l]:>18.{prec}f}\n")
self.tm.stop("2e_ints", parent="write_integrals")
self.tm.stop("write_integrals")
# ========================================================================================= #
# #
# Functions related with the SCF energy calculation #
# #
# ========================================================================================= #
def nuclear_repulsion(self) -> float:
"""
Calculates the nuclear repulsion energy.
Returns
-------
the nuclear repulsion energy, in Ha
"""
self.V_nuc = 0
for i in range(self.nats-1):
for j in range(i+1, self.nats):
self.V_nuc += self.atnums[i]*self.atnums[j]/self.distance_bohr(i,j)
return self.V_nuc
def SCF_setup(self) -> None:
"""
Sets up all the SCF reusable variables, such as
the integrals and the transformation matrix, as
class attributes.
"""
K = self.norbs
# diagonalize overlap matrix, obtaining
# eigenvalue (to be) matrix A and eigenvectors U
eigs, U = np.linalg.eigh(self.S)
# calculate the square root of the A matrix
alpha = np.diag(1/np.sqrt(eigs))
# build the transformation matrix X, transforming back A to S
self.X = np.matmul(np.matmul(U,alpha), np.transpose(U))
# build the core hamiltonian
self.Hc = self.T + self.V
# take it as the Fock operator (no two electron term yet)
self.F = self.Hc
# transform the Fock operator to the orthogonalized basis
self.Hc0 = np.matmul(np.transpose(self.X), np.matmul(self.F, self.X))
# calculate the transformed coefficient matrix,
# obtaining the orbital energies in the process
self.orbital_energies, self.C0 = np.linalg.eigh(self.Hc0)
# get back the coefficient matrix
self.C = np.matmul(self.X,self.C0)
# initial density matrix
self.P = np.zeros((K,K))
for i in range(K):
for j in range(K):
for a in range(int(self.nels/2)):
self.P[i, j] += self.C[i, a]*self.C[j,a]
# initial energy
self.E = 0
for i in range(K):
for j in range(K):
self.E += self.P[i,j]*(self.Hc[i,j]+self.F[i,j])
def SCF_step(self) -> None:
"""
Performs a self-consistent field step, meaning:
- it recalculates the density matrix P
- builds the G matrix
- builds and transforms the Fock matrix F
- diagonalizes it and obtains the orbitals
- calculates the electronic energy
"""
K = self.norbs
# new density matrix
self.P = np.zeros((K,K))
for i in range(K):
for j in range(K):
for a in range(int(self.nels/2)):
self.P[i, j] += self.C[i, a]*self.C[j,a]
# build the G matrix, using the density matrix
# P and the two-electron integrals
G = np.zeros((K,K))
for i in range(K):
for j in range(K):
for k in range(K): # lda
for l in range(K): #sigma
G[i,j] += self.P[k,l]*(2*self.O[i,j,k,l] - self.O[i,k,j,l])
# build Fock operator
self.F = self.Hc + G
# transform the Fock operator to the orthogonal basis
self.F0 = np.matmul(np.transpose(self.X), np.matmul(self.F, self.X))
# calculate the transformed coefficient matrix,
# obtaining the orbital energies in the process
self.orbital_energies, self.C0 = np.linalg.eigh(self.F0)
# get back the coefficient matrix
self.C = np.matmul(self.X,self.C0)
# calculate the total energy
self.E = 0
for i in range(K):
for j in range(K):
self.E += self.P[i,j]*(self.Hc[i,j]+self.F[i,j])
# ========================================================================================= #
# #
# Printing functions related with the SCF energy calculation #
# #
# ========================================================================================= #
def print_orbitals(self, textwidth:int=TEXTW, textshift:int= TEXTS) -> None:
""" Prints orbital information, i.e the energies and coefficients. """
sh = textshift
third = int(textwidth/3)
quart = int(textwidth/4)
K = self.norbs
eners = self.orbital_energies
eners_ev = eners*27.211386245988
print(" "*sh + f"{'~#~#~#~ ORBITAL INFORMATION ~#~#~#~':^{textwidth}}", flush=True)
print("")
print(" "*sh + f"{'Energies':^{textwidth}}", flush=True)
print(" "*sh + "-"*textwidth)
print(" "*sh + f"{'Orbital':^{third}}{'Energy (Ha)':^{third}}{'Energy(eV)':^{third}}")
print(" "*sh + "-"*textwidth)
for orb in range(K):
print(" "*sh + f"{orb:^{third}}{eners[orb]:^{third}.8E}{eners_ev[orb]:^{third}.8E}")
print(" "*sh + "-"*textwidth)
nrows = floor(K/3)
rest = K%3
print("")
print(" "*sh + f"{'Coefficients':^{textwidth}}\n", flush=True)
print(" "*sh + "-"*textwidth)
for line in range(nrows):
print(" "*sh + f"{'Orbital':^{quart}}{line*3:^{quart}}{line*3+1:^{quart}}{line*3+2:^{quart}}")
print(" "*sh + "-"*textwidth)
for orb in range(K):
print(" "*sh + f"{orb:^{quart}}{self.C[orb, line*3]:^{quart}.8f}{self.C[orb, line*3+1]:^{quart}.8f}{self.C[orb, line*3+2]:^{quart}.8f}")
print(" "*sh + "-"*textwidth)
if rest != 0:
header = " "*sh + f"{'Orbital':^{quart}}"
for r in range(rest):
header += f"{nrows*3+r:^{quart}}"
print(header)
print(" "*sh + "-"*int((rest+1)*quart))
for orb in range(K):
l = " "*sh + f"{orb:^{quart}}"
for r in range(rest):
l += f"{self.C[orb, nrows*3+r]:^{quart}.8f}"
print(l)
print(" "*sh + "-"*int((rest+1)*quart))
print("")
pass
def print_population(self, textwidth:int=TEXTW, textshift:int= TEXTS,) -> None:
""" Performs a population (charge) analysis on the system, and prints the results. """
sh = textshift
third = int(textwidth/3)
sixth = int(textwidth/6)
K = self.norbs
labs = self.labels
# calculate mulliken population by orbital
mull_pop = np.diag(2*np.matmul(self.P, self.S))
lowd_pop = np.diag(np.matmul(np.linalg.inv(self.X), np.matmul(2*np.matmul(self.P, self.X), self.S)))
# add contributions by atom
mull_ele = np.zeros(self.nats)
lowd_ele = np.zeros(self.nats)
for o in range(K):
at = self.basis_centers[o]
mull_ele[at] += mull_pop[o]
lowd_ele[at] += lowd_pop[o]
# calculate charges
mull_ch = self.atnums - mull_ele
lowd_ch = self.atnums - lowd_ele
print(" "*sh + f"{'~#~#~#~ POPULATION ANALYSIS ~#~#~#~':^{textwidth}}", flush=True)
print("")
print(" "*sh + "-"*textwidth)
print(" "*sh + f"{'Atom':^{third}}{'Mulliken':^{third}}{'Löwdin':^{third}}")
print(" "*sh + f"{'-'*(third-4):^{third}}{'-'*(third-4):^{third}}{'-'*(third-4):^{third}}")
print(" "*sh + f"{'Index':^{sixth}}{'Label':^{sixth}}{'Pop':^{sixth}}{'Charge':^{sixth}}{'Pop':^{sixth}}{'Charge':^{sixth}}")
print(" "*sh + "-"*textwidth)
for at in range(self.nats):
print(" "*sh + f"{at:^{sixth}}{labs[at]:^{sixth}}{mull_ele[at]:^{sixth}.4f}{mull_ch[at]:^{sixth}.4f}{lowd_ele[at]:^{sixth}.4f}{lowd_ch[at]:^{sixth}.4f}")