-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
direct_ms0.py
349 lines (298 loc) · 12.2 KB
/
direct_ms0.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
#!/usr/bin/env python
#
# Author: Qiming Sun <osirpt.sun@gmail.com>
#
# FCI solver for equivalent number of alpha and beta electrons
# (requires MS=0, can be singlet, triplet, quintet, dep on init guess)
#
# Other files in the directory
# direct_ms0 MS=0, same number of alpha and beta nelectrons
# direct_spin0 singlet
# direct_spin1 arbitary number of alpha and beta electrons, based on RHF/ROHF
# MO integrals
# direct_uhf arbitary number of alpha and beta electrons, based on UHF
# MO integrals
#
import os
import ctypes
import numpy
import scipy.linalg
import pyscf.lib
import pyscf.ao2mo
import davidson
import cistring
import rdm
import direct_spin1
_loaderpath = os.path.dirname(pyscf.lib.__file__)
libfci = numpy.ctypeslib.load_library('libmcscf', _loaderpath)
def contract_1e(f1e, fcivec, norb, nelec, link_index=None):
if link_index is None:
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
link_index = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
na,nlink,_ = link_index.shape
ci1 = numpy.empty((na,na))
f1e_tril = pyscf.lib.pack_tril(f1e)
libfci.FCIcontract_1e_ms0(f1e_tril.ctypes.data_as(ctypes.c_void_p),
fcivec.ctypes.data_as(ctypes.c_void_p),
ci1.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(norb), ctypes.c_int(na),
ctypes.c_int(nlink),
link_index.ctypes.data_as(ctypes.c_void_p))
return ci1
def contract_2e(eri, fcivec, norb, nelec, link_index=None, bufsize=1024):
eri = pyscf.ao2mo.restore(4, eri, norb)
if not eri.flags.c_contiguous:
eri = eri.copy()
if link_index is None:
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
link_index = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
na,nlink,_ = link_index.shape
ci1 = numpy.empty((na,na))
libfci.FCIcontract_2e_ms0(eri.ctypes.data_as(ctypes.c_void_p),
fcivec.ctypes.data_as(ctypes.c_void_p),
ci1.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(norb), ctypes.c_int(na),
ctypes.c_int(nlink),
link_index.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(bufsize))
return ci1
def absorb_h1e(*args, **kwargs):
return direct_spin1.absorb_h1e(*args, **kwargs)
def make_hdiag(h1e, eri, norb, nelec):
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
eri = pyscf.ao2mo.restore(1, eri, norb)
link_index = cistring.gen_linkstr_index(range(norb), neleca)
na = link_index.shape[0]
occslist = link_index[:,:neleca,0].copy('C')
hdiag = numpy.empty((na,na))
jdiag = numpy.einsum('iijj->ij',eri).copy('C')
kdiag = numpy.einsum('ijji->ij',eri).copy('C')
libfci.FCImake_hdiag(hdiag.ctypes.data_as(ctypes.c_void_p),
h1e.ctypes.data_as(ctypes.c_void_p),
jdiag.ctypes.data_as(ctypes.c_void_p),
kdiag.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(norb), ctypes.c_int(na),
ctypes.c_int(neleca),
occslist.ctypes.data_as(ctypes.c_void_p))
# symmetrize hdiag to reduce numerical error
hdiag = pyscf.lib.transpose_sum(hdiag, inplace=True) * .5
return hdiag.ravel()
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
eri = pyscf.ao2mo.restore(1, eri, norb)
na = cistring.num_strings(norb, neleca)
addr = numpy.argsort(hdiag)[:np]
# symmetrize addra/addrb
addra = addr / na
addrb = addr % na
stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
dtype=numpy.long)
strb = numpy.array([cistring.addr2str(norb,neleca,ib) for ib in addrb],
dtype=numpy.long)
np = len(addr)
h0 = numpy.zeros((np,np))
libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
h1e.ctypes.data_as(ctypes.c_void_p),
eri.ctypes.data_as(ctypes.c_void_p),
stra.ctypes.data_as(ctypes.c_void_p),
strb.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(norb), ctypes.c_int(np))
for i in range(np):
h0[i,i] = hdiag[addr[i]]
h0 = pyscf.lib.hermi_triu(h0)
return addr, h0
# be careful with single determinant initial guess. It may lead to the
# eigvalue of first davidson iter being equal to hdiag
def kernel(h1e, eri, norb, nelec, ci0=None, eshift=.1, **kwargs):
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
link_index = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
na = link_index.shape[0]
h2e = direct_spin1.absorb_h1e(h1e, eri, norb, nelec, .5)
hdiag = make_hdiag(h1e, eri, norb, nelec)
addr, h0 = pspace(h1e, eri, norb, nelec, hdiag)
pw, pv = scipy.linalg.eigh(h0)
if len(addr) == na*na:
ci0 = numpy.empty((na*na))
ci0[addr] = pv[:,0]
return pw[0], ci0.reshape(na,na)
def precond(r, e0, x0, *args):
#h0e0 = h0 - numpy.eye(len(addr))*(e0-eshift)
h0e0inv = numpy.dot(pv/(pw-(e0-eshift)), pv.T)
hdiaginv = 1/(hdiag - (e0-eshift))
h0x0 = x0 * hdiaginv
#h0x0[addr] = numpy.linalg.solve(h0e0, x0[addr])
h0x0[addr] = numpy.dot(h0e0inv, x0[addr])
h0r = r * hdiaginv
#h0r[addr] = numpy.linalg.solve(h0e0, r[addr])
h0r[addr] = numpy.dot(h0e0inv, r[addr])
e1 = numpy.dot(x0, h0r) / numpy.dot(x0, h0x0)
x1 = r - e1*x0
#pspace_x1 = x1[addr].copy()
x1 *= hdiaginv
# pspace (h0-e0)^{-1} cause diverging?
#x1[addr] = numpy.linalg.solve(h0e0, pspace_x1)
return x1
#precond = lambda x, e, *args: x/(hdiag-(e-eshift))
def hop(c):
hc = contract_2e(h2e, c, norb, nelec, link_index)
return hc.ravel()
#TODO: check spin of initial guess
if ci0 is None:
# we need better initial guess
ci0 = numpy.zeros(na*na)
#ci0[addr] = pv[:,0]
ci0[0] = 1
else:
ci0 = ci0.ravel()
e, c = davidson.dsyev(hop, ci0, precond, tol=1e-8, lindep=1e-8)
return e, c.reshape(na,na)
# alpha and beta 1pdm
def make_rdm1s(fcivec, norb, nelec, link_index=None):
rdm1a = rdm.make_rdm1('FCImake_rdm1a', fcivec, fcivec,
norb, nelec, link_index)
rdm1b = rdm.make_rdm1('FCImake_rdm1b', fcivec, fcivec,
norb, nelec, link_index)
return rdm1a, rdm1b
# dm_pq = <|p^+ q|>
def make_rdm1(fcivec, norb, nelec, link_index=None):
rdm1a, rdm1b = make_rdm1s(fcivec, norb, nelec, link_index)
return rdm1a + rdm1b
# dm_pq,rs = <|p^+ q r^+ s|>
# need call reorder_rdm for this rdm2
def make_rdm12(fcivec, norb, nelec, link_index=None):
return rdm.make_rdm12('FCImake_rdm12_ms0', fcivec, fcivec,
norb, nelec, link_index)
# dm_pq = <I|p^+ q|J>
def trans_rdm1s(cibra, ciket, norb, nelec, link_index=None):
if link_index is None:
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
link_index = cistring.gen_linkstr_index(range(norb), neleca)
rdm1a = rdm.make_rdm1('FCItrans_rdm1a', cibra, ciket,
norb, nelec, link_index)
rdm1b = rdm.make_rdm1('FCItrans_rdm1b', cibra, ciket,
norb, nelec, link_index)
return rdm1a, rdm1b
def trans_rdm1(cibra, ciket, norb, nelec, link_index=None):
rdm1a, rdm1b = trans_rdm1s(cibra, ciket, norb, nelec, link_index)
return rdm1a + rdm1b
# dm_pq,rs = <I|p^+ q r^+ s|J>
def trans_rdm12(cibra, ciket, norb, nelec, link_index=None):
return rdm.make_rdm12('FCItrans_rdm12_ms0', cibra, ciket,
norb, nelec, link_index)
def energy(h1e, eri, fcivec, norb, nelec, link_index=None):
h2e = direct_spin1.absorb_h1e(h1e, eri, norb, nelec, .5)
ci1 = contract_2e(h2e, fcivec, norb, nelec, link_index)
return numpy.dot(fcivec.ravel(), ci1.ravel())
###############################################################
# direct-CI driver
###############################################################
def kernel_ms0(fci, h1e, eri, norb, nelec, ci0=None):
if isinstance(nelec, int):
neleca = nelec/2
else:
neleca, nelecb = nelec
assert(neleca == nelecb)
link_index = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
na = link_index.shape[0]
h2e = fci.absorb_h1e(h1e, eri, norb, nelec, .5)
hdiag = fci.make_hdiag(h1e, eri, norb, nelec)
addr, h0 = fci.pspace(h1e, eri, norb, nelec, hdiag)
pw, pv = scipy.linalg.eigh(h0)
if len(addr) == na*na:
ci0 = numpy.empty((na*na))
ci0[addr] = pv[:,0]
return pw[0], ci0.reshape(na,na)
precond = fci.make_precond(hdiag, pw, pv, addr)
def hop(c):
hc = fci.contract_2e(h2e, c, norb, nelec, link_index)
return hc.ravel()
#TODO: check spin of initial guess
if ci0 is None:
# we need better initial guess
ci0 = numpy.zeros(na*na)
#ci0[addr] = pv[:,0]
ci0[0] = 1
else:
ci0 = ci0.ravel()
#e, c = davidson.dsyev(hop, ci0, precond, tol=fci.tol, lindep=fci.lindep)
e, c = fci.eig(hop, ci0, precond)
return e, c.reshape(na,na)
class FCISolver(direct_spin1.FCISolver):
def make_hdiag(self, h1e, eri, norb, nelec):
return make_hdiag(h1e, eri, norb, nelec)
def pspace(self, h1e, eri, norb, nelec, hdiag, np=400):
return pspace(h1e, eri, norb, nelec, hdiag, np)
def contract_1e(self, f1e, fcivec, norb, nelec, link_index=None, **kwargs):
return contract_1e(f1e, fcivec, norb, nelec, link_index, **kwargs)
def contract_2e(self, eri, fcivec, norb, nelec, link_index=None, **kwargs):
return contract_2e(eri, fcivec, norb, nelec, link_index, **kwargs)
def kernel(self, h1e, eri, norb, nelec, ci0=None, **kwargs):
self.mol.check_sanity(self)
return kernel_ms0(self, h1e, eri, norb, nelec, ci0)
def make_rdm1s(self, fcivec, norb, nelec, link_index=None, **kwargs):
return make_rdm1s(fcivec, norb, nelec, link_index)
def make_rdm1(self, fcivec, norb, nelec, link_index=None, **kwargs):
return make_rdm1(fcivec, norb, nelec, link_index)
def make_rdm12(self, fcivec, norb, nelec, link_index=None, **kwargs):
return make_rdm12(fcivec, norb, nelec, link_index)
def trans_rdm1s(self, cibra, ciket, norb, nelec, link_index=None, **kwargs):
return trans_rdm1s(cibra, ciket, norb, nelec, link_index)
def trans_rdm1(self, cibra, ciket, norb, nelec, link_index=None, **kwargs):
return trans_rdm1(cibra, ciket, norb, nelec, link_index)
def trans_rdm12(self, cibra, ciket, norb, nelec, link_index=None, **kwargs):
return trans_rdm12(cibra, ciket, norb, nelec, link_index)
if __name__ == '__main__':
import time
from pyscf import gto
from pyscf import scf
from pyscf import ao2mo
mol = gto.Mole()
mol.verbose = 0
mol.output = None#"out_h2o"
mol.atom = [
['H', ( 1.,-1. , 0. )],
['H', ( 0.,-1. ,-1. )],
# ['H', ( 1.,-0.5 ,-1. )],
# ['H', ( 0.,-0.5 ,-1. )],
# ['H', ( 0.,-0.5 ,-0. )],
# ['H', ( 0.,-0. ,-1. )],
['H', ( 1.,-0.5 , 0. )],
['H', ( 0., 1. , 1. )],
]
mol.basis = {'H': '6-31g'}
mol.build()
m = scf.RHF(mol)
ehf = m.scf()
cis = FCISolver(mol)
norb = m.mo_coeff.shape[1]
nelec = mol.nelectron
h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff))
eri = ao2mo.incore.general(m._eri, (m.mo_coeff,)*4, compact=False)
print(time.clock())
e, c = cis.kernel(h1e, eri, norb, nelec)
print(e - -4.48686469648)
print(time.clock())