forked from ferchault/APHF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRHF.py
189 lines (140 loc) · 4.43 KB
/
RHF.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
"""
Copyright (C) 2015 Rocco Meli, 2021 Guido Falk von Rudorff
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from matrices import *
import numpy.linalg as la
import mpmath
def RHF_step(basis, molecule, N, H, X, P_old, ee, G_ee_cache, verbose=False):
"""
Restricted Hartree-Fock self-consistent field setp.
INPUT:
BASIS: basis set
MOLECULE: molecule, collection of atom object
N: Number of electrons
H: core Hamiltonian
X: tranformation matrix
P_OLD: Old density matrix
EE: List of electron-electron Integrals
VERBOSE: verbose flag (set True to print everything on screen)
"""
if verbose:
print("\nDensity matrix P:")
print(P_old)
G = G_ee(basis, G_ee_cache, P_old) # Compute electron-electron interaction matrix
if verbose:
print("\nG matrix:")
print(G)
F = H + G # Compute Fock matrix
if verbose:
print("\nFock matrix:")
print(F)
Fx = np.dot(
X.conj().T, np.dot(F, X)
) # Compute Fock matrix in the orthonormal basis set (S=I in this set)
if verbose:
print("\nFock matrix in orthogonal orbital basis:")
print(Fx)
e, Cx = mpmath.eigh(
NP2MP(Fx)
) # Compute eigenvalues and eigenvectors of the Fock matrix
# Sort eigenvalues from smallest to highest (needed to compute P correctly)
e = np.array(e)
Cx = MP2NP(Cx)
idx = e.argsort()
e = e[idx]
Cx = Cx[:, idx]
if verbose:
print("\nCoefficients in orthogonal orbital basis:")
print(Cx)
if verbose:
print("\nEnergies in orthogonal orbital basis:")
print(e)
C = np.dot(
X, Cx
) # Transform coefficient matrix in the orthonormal basis to the original basis
if verbose:
print("\nCoefficients:")
print(C)
Pnew = P_density(C, N) # Compute the new density matrix
return Pnew, F, e
def delta_P(P_old, P_new):
"""
Compute the difference between two density matrices.
INTPUT:
P_OLD: Olde density matrix
P_NEW: New density matrix
OUTPUT:
DELTA: difference between the two density matrices
Source:
Modern Quantum Chemistry
Szabo and Ostlund
Dover
1989
"""
delta = 0
n = P_old.shape[0]
for i in range(n):
for j in range(n):
delta += (P_old[i, j] - P_new[i, j]) ** 2
return (delta / mpmath.mp.mpf("4.0")) ** mpmath.mp.mpf("0.5")
def energy_el(P, F, H):
"""
Compute electronic energy.
INPUT:
P: Density matrix
F: Fock matrix
H: Core Hamiltonian
Source:
Modern Quantum Chemistry
Szabo and Ostlund
Dover
1989
"""
# Size of the basis set
K = P.shape[0]
E = 0
for i in range(K):
for j in range(K):
E += mpmath.mp.mpf("0.5") * P[i, j] * (H[i, j] + F[i, j])
return E
def energy_n(molecule):
"""
Compute nuclear energy (classical nucleus-nucleus repulsion)
INPUT:
MOLECULE: molecule, as a collection of atoms
OUTPUT:
ENERGY_N: Nuclear energy
"""
en = 0
for i in range(len(molecule)):
for j in range(i + 1, len(molecule)):
# Select atoms from molecule
atomi = molecule[i]
atomj = molecule[j]
# Extract distance from atom
Ri = np.asarray(atomi.R)
Rj = np.asarray(atomj.R)
en += atomi.Zeff * atomj.Zeff / la.norm(Ri - Rj)
return en
def energy_tot(P, F, H, molecule):
"""
Compute total energy (electronic plus nuclear).
INPUT:
P: Density matrix
F: Fock matrix
H: Core Hamiltonian
MOLECULE: molecule, as a collection of atoms
OUTPUT:
ENERGY_TOT: total energy
"""
return energy_el(P, F, H) + energy_n(molecule)