-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiatomic.py
189 lines (152 loc) · 4.83 KB
/
diatomic.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
import argparse
import time
import jax.numpy as jnp
import matplotlib.pyplot as plt
import numpy
import scipy
from jax import grad, jit, random
from jax.config import config
from pyscf import gto, scf
from scipy.optimize import minimize
import adscf
config.update("jax_enable_x64", True)
parser = argparse.ArgumentParser(
description='Draw potential energy curves for a diatomic molecule.')
parser.add_argument('molecule', choices=['H2', 'HF'])
parser.add_argument('basis', choices=['STO-3G', '3-21G'])
args = parser.parse_args()
key = random.PRNGKey(0)
x = []
y = []
x_aug = []
y_aug = []
x_scf = []
y_scf = []
min_range = 2 if args.molecule == 'H2' else 5
max_range = 26 if args.molecule == 'H2' else 31
for i in range(min_range, max_range):
R = 0.1 * i
print(f"interatomic distance: {R:.2f}")
mol = gto.Mole()
mol.charge = 0
mol.spin = 0
if args.molecule == 'H2':
mol.build(atom=f'H 0.0 0.0 0.0; H 0.0 0.0 {R:.2f}',
basis=args.basis, unit='Angstrom')
elif args.molecule == 'HF':
mol.build(atom=f'H 0.0 0.0 0.0; F 0.0 0.0 {R:.2f}',
basis=args.basis, unit='Angstrom')
calcEnergy, gradEnergy = adscf.calcEnergy_create(mol)
start = time.time()
# RHF energy calculation by PySCF
mf = scf.RHF(mol)
mf.scf()
elapsed_time = time.time() - start
print("SCF: {:.3f} ms".format(elapsed_time * 1000))
e_scf = scf.hf.energy_tot(mf)
x_scf.append(R)
y_scf.append(e_scf)
# Curvilinear search using Cayley transformation
start = time.time()
# parameters
tau = 1.0
tau_m = 1e-10
tau_M = 1e10
rho = 1e-4
delta = 0.1
eta = 0.5
epsilon = 1e-6
max_iter = 5000
# 1. initialize X0
S = mol.intor_symmetric('int1e_ovlp') # overlap matrix
S64 = numpy.asarray(S, dtype=numpy.float64)
X_np = scipy.linalg.inv(scipy.linalg.sqrtm(S64))
X = jnp.asarray(X_np)
# 2. set C=f(X0) and Q0=1
C = calcEnergy(X)
Q = 1.0
# 3. calculate G0 and A0
G = gradEnergy(X)
A = G @ X.T @ S - S @ X @ G.T
# function to calculate Y(tau)
I = jnp.identity(len(S))
def Y_tau(tau, X, A):
return jnp.linalg.inv(I + 0.5 * tau * A @ S) @ (I - 0.5 * tau * A @ S) @ X
# main loop
for k in range(max_iter):
Y = Y_tau(tau, X, A)
A_norm = jnp.linalg.norm(A, "fro")
X_old, Q_old, G_old = X, Q, G
# 5
while calcEnergy(Y) > C - rho * tau * A_norm**2.0:
tau *= delta # 6
Y = Y_tau(tau, X, A)
# 8
X_new = Y
Q_new = eta * Q + 1.0
C = (eta * Q * C + calcEnergy(X_new)) / Q_new
# 9
G_new = gradEnergy(X_new)
A_new = G_new @ X_new.T @ S - S @ X_new @ G_new.T
# 10
Sk = X_new - X
Yk = G_new - G
if k % 2 == 0:
tau_k = jnp.trace(Sk.T @ Sk) / abs(jnp.trace(Sk.T @ Yk))
else:
tau_k = abs(jnp.trace(Sk.T @ Yk)) / jnp.trace(Yk.T @ Yk)
tau = max(min(tau_k, tau_M), tau_m)
# Update variables for next iteration
X, Q, G, A = X_new, Q_new, G_new, A_new
# Check loop condition (4)
cond = jnp.linalg.norm(A @ X)
if cond < epsilon:
break
elapsed_time = time.time() - start
print("Curvilinear search: {:.3f} ms".format(elapsed_time*1000))
e = calcEnergy(X) + mol.energy_nuc()
print(f"total energy = {e}")
x.append(R)
y.append(e)
# augmented Lagrangian
@jit
def orthogonality(x):
C = jnp.reshape(x, [len(S), len(S)])
return jnp.linalg.norm(C.transpose()@S@C - jnp.identity(len(S)))
start = time.time()
x0 = random.uniform(key, (S.size,))
# 1
mu = 1.0
lam = 0.0
constraint = orthogonality(x0)
# 2
while constraint > 1e-6:
def target(x):
h = orthogonality(x)
return calcEnergy(x) + mu * h ** 2.0 + lam * h
# 3
res = minimize(jit(target), x0, jac=jit(grad(jit(target))),
method="BFGS", options={'maxiter': 100})
x0 = res.x
constraint = orthogonality(x0)
# 4
lam += 2.0 * mu * constraint
# 5
mu *= 2.0
elapsed_time = time.time() - start
print("Augmented: {:.3f} s".format(elapsed_time*1000))
energy = res.fun+mol.energy_nuc()
print(f"calculated energy = {energy}\n")
x_aug.append(R)
y_aug.append(energy)
p2 = plt.plot(x_scf, y_scf, marker="o")
p1 = plt.plot(x_aug, y_aug, marker="*")
p0 = plt.plot(x, y, marker="x")
plt.xlabel("interatomic distance (Å)", fontsize=16)
plt.ylabel("total energy (Eh)", fontsize=16)
location = 'upper right' if args.molecule == 'H2' else 'lower right'
plt.legend((p0[0], p1[0], p2[0]),
("Curvilinear search", "Augmented Lagrangian", "PySCF"),
loc=location)
plt.savefig(f"result-{args.molecule}-{args.basis}.png", dpi=300)
plt.show()