forked from Jasonmyu/kccgf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs_greens_function.py
351 lines (316 loc) · 10.1 KB
/
cs_greens_function.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
import sys
from numpy import linalg as LA
import numpy as np
import scipy.sparse.linalg as spla
from math import *
from pyscf import gto
from pyscf import scf
from pyscf import cc
import gminres
import arnoldi_solver
import antisymeri
from ccsd import ccsd
#import eom_driver
def print2Vec(title,vec):
print "*******************************"
print title
print "*******************************"
shape0 = vec.shape[0]
shape1 = vec.shape[1]
for i in xrange(shape0):
for j in xrange(shape1):
dVal = vec[i,j]
if abs(dVal) > 1e-13:
print "%3d %3d %20.16f" %(i,j,dVal)
def print4Vec(title,vec):
print "*******************************"
print title
print "*******************************"
shape0 = vec.shape[0]//2
shape1 = vec.shape[1]//2
shape2 = vec.shape[2]//2
shape3 = vec.shape[3]//2
print " :: AABB spin combination ::"
for i in range(shape0):
spin_i = 1 #i%2
for j in range(shape1):
spin_j = 0 #j%2
for a in range(shape2):
spin_a = 1 #1a%2
for b in range(shape3):
spin_b = 0 #b%2
si = 2*i + spin_i
sj = 2*j + spin_j
sa = 2*a + spin_a
sb = 2*b + spin_b
dval = vec[si,sj,sa,sb]
if abs(dval) > 1e-13:
print "%3d %3d %3d %3d %20.16f" %(si,sj,sa,sb,vec[si,sj,sa,sb])
print ""
print " :: AAAA spin combination ::"
for i in range(shape0):
spin_i = 0 #i%2
for j in range(shape1):
spin_j = 0 #j%2
for a in range(shape2):
spin_a = 0 #1a%2
for b in range(shape3):
spin_b = 0 #b%2
si = 2*i + spin_i
sj = 2*j + spin_j
sa = 2*a + spin_a
sb = 2*b + spin_b
dval = vec[si,sj,sa,sb]
if abs(dval) > 1e-13:
print "%3d %3d %3d %3d %20.16f" %(si,sj,sa,sb,vec[si,sj,sa,sb])
print "*******************************"
def ea_b_vec(cc,p):
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nvir),dtype=complex)
vector2 = np.zeros((nocc,nvir,nvir),dtype=complex)
if p >= nocc:
vector1[ p-nocc ] += 1.0
else:
vector1 = cc.t1[p,:]
vector2 = 1.0 * cc.t2[p,:,:,:]
return cc.amplitudes_to_vector_ea(vector1,vector2)
def ea_e_vec(cc,q):
nocc,nvir = cc.t1.shape
lamb1 = cc.L1
lamb2 = cc.L2
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nvir),dtype=complex)
vector2 = np.zeros((nocc,nvir,nvir),dtype=complex)
if q >= nocc:
vector1[q-nocc] += 1.0
vector1 += np.einsum('ia,i->a',lamb1, cc.t1[:,q-nocc])
vector1 += (1./4.) * np.einsum( 'ilcd,ild->c', lamb2, cc.t2[:,:,q-nocc,:] )
vector1 -= (1./4.) * np.einsum( 'ildc,ild->c', lamb2, cc.t2[:,:,q-nocc,:] )
#
#
# Notice the change in signs between this and the green's function
# equations in the Nooijen paper. The sign comes from working with
# s(i,j,a) versus s(j,i,a)
#
#
vector2[:,q-nocc,:] += 1.0*lamb1
vector2[:,:,q-nocc] -= 1.0*lamb1
vector2 += (1./2.) * np.einsum( 'i,ijcb->jcb', cc.t1[:,q-nocc], lamb2 )
vector2 -= (1./2.) * np.einsum( 'i,jicb->jcb', cc.t1[:,q-nocc], lamb2 )
else:
vector1 += lamb1[q,:]
vector2 += - 0.5 * ( lamb2[:,q,:,:] - lamb2[q,:,:,:] )
return cc.amplitudes_to_vector_ea(vector1,vector2)
def eom_ccsd_ip_greenfunction(cc):
eta = 0.1
omega = [0.0];
state = 6
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
#
#
# Making initial guess
#
#
#vector1 += np.ones(nocc)
#vector1 += 1j*np.ones(nocc)
#vector2 += np.ones((nocc,nocc,nvir))
#vector2 += 1j*np.ones((nocc,nocc,nvir))
v0 = cc.amplitudes_to_vector_ip(vector1,vector2)
v0 = v0.reshape(v0.shape[0],1)
#
#
# Making pre conditioner
#
#
vector1 -= vector1
vector2 -= vector2
vector1 += np.ones(nocc)
vector2 += np.ones((nocc,nocc,nvir))
P = cc.amplitudes_to_vector_ip(vector1,vector2)
P = P.reshape( P.shape[0], 1 )
#print "precon : ", P
#
#
# Making 'b' vector
#
#
b = ip_b_vec(cc,state)
b = b.reshape( b.shape[0], 1 )
print "bvector..."
for i in xrange(b.shape[0]):
dval = b[i]
if abs(dval) > 1e-15:
print "b : ", i, dval
#
#
# Making 'e' vector
#
#
e = ip_e_vec(cc,state)
e = e.reshape( e.shape[0], 1 )
print "evector..."
for i in xrange(e.shape[0]):
dval = e[i]
if abs(dval) > 1e-15:
print "e : ", i, dval
gg = arnoldi_solver.arnoldi(cc,v0,P)
solution = gg.getSolution()
#print "solution = "
#print solution
print "green's function = "
print np.vdot(e,solution)
def eom_ccsd_ea_greenfunction(cc):
eta = 0.04777412092810763
omega = [0.0];
state = 0
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nvir),dtype=complex)
vector2 = np.zeros((nocc,nvir,nvir),dtype=complex)
#
#
# Making initial guess
#
#
vector1 += np.ones(nvir)
#vector1 += 1j*np.ones(nvir)
vector2 += np.ones((nocc,nvir,nvir))
#vector2 += 1j*np.ones((nocc,nvir,nvir))
v0 = cc.amplitudes_to_vector_ea(vector1,vector2)
v0 = v0.reshape(v0.shape[0],1)
#
#
# Making pre conditioner
#
#
vector1 -= vector1
vector2 -= vector2
vector1 += np.ones(nvir)
vector2 += np.ones((nocc,nvir,nvir))
P = cc.amplitudes_to_vector_ea(vector1,vector2)
P = P.reshape( P.shape[0], 1 )
#print "precon : ", P
#
#
# Making 'b' vector
#
#
b = ea_b_vec(cc,state)
b = b.reshape( b.shape[0], 1 )
#for i in xrange(b.shape[0]):
# dval = b[i]
# if abs(dval) > 1e-15:
# print "b : ", i, dval
#
#
# Making 'e' vector
#
#
e = ea_e_vec(cc,state)
e = e.reshape( e.shape[0], 1 )
#print "evector..."
#for i in xrange(e.shape[0]):
# dval = e[i]
# if abs(dval) > 1e-15:
# print "e : ", i, dval
#for i in xrange(e.shape[0]):
# print e[i]
gg = gminres.gMinRes(cc.ea_eom_ccsd_matvec,b,v0,P)
#gg = arnoldi_solver.arnoldi(cc,v0,P)
solution = gg.getSolution()
print "green's function = "
print np.vdot(e,solution)
def gf_solver_ea( cc ):
eom_ccsd_ea_greenfunction( cc )
def gf_solver_ip( cc ):
eom_ccsd_ip_greenfunction( cc )
def greens_b_vector_ip_rhf(cc,p):
nocc, nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
if p < nocc:
vector1[ p ] += 1.0
else:
vector1 = cc.t1[:,p-nocc]
vector2 = cc.t2[:,:,:,p-nocc]
return cc.amplitudes_to_vector_ip(vector1,vector2)
def greens_e_vector_ip_rhf(cc,p):
nocc, nvir = cc.t1.shape
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
if p < nocc:
vector1[ p ] = -1.0
vector1 += np.einsum('ia,a->i', cc.l1, cc.t1[p,:])
vector1 += 2*np.einsum('ilcd,lcd->i', cc.l2, cc.t2[p,:,:,:])
vector1 -= np.einsum('ilcd,ldc->i', cc.l2, cc.t2[p,:,:,:])
vector2[p,:,:] += -2.*cc.l1
vector2[:,p,:] += cc.l1
vector2 += 2*np.einsum('c,ijcb->ijb', cc.t1[p,:], cc.l2)
vector2 -= np.einsum('c,jicb->ijb', cc.t1[p,:], cc.l2)
else:
vector1 += -lamb1[:,p-nocc]
vector2 += -2*lamb2[:,:,p-nocc,:] + lamb2[:,:,:,p-nocc]
def greens_func_multiply(ham,vector,imag_part,real_part,args=None):
return ham(vector) + (1j*imag_part + real_part)*vector
def ip_b_vec(cc,p):
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
if p < nocc:
vector1[ p ] += 1.0
else:
vector1 = cc.t1[:,p-nocc]
vector2 = 1.0 * cc.t2[:,:,:,p-nocc]
return cc.amplitudes_to_vector_ip(vector1,vector2)
def ip_e_vec(cc,p):
nocc,nvir = cc.t1.shape
lamb1 = cc.L1
lamb2 = cc.L2
nocc,nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
print "LAM1"
print lamb1
#print "LAM2"
#print lamb2
print4Vec("LAMBDA-2",lamb2)
if p < nocc:
vector1[p] -= 1.0
vector1 += np.einsum('ia,a->i',lamb1, cc.t1[p,:])
vector1 += (1./4.) * np.einsum( 'ilcd,lcd->i', lamb2, cc.t2[p,:,:,:] )
vector1 -= (1./4.) * np.einsum( 'ilcd,ldc->i', lamb2, cc.t2[p,:,:,:] )
#
#
# Notice the change in signs between this and the green's function
# equations in the Nooijen paper. The sign comes from working with
# s(i,j,a) versus s(j,i,a)
#
#
vector2[p,:,:] += 1.0*lamb1
vector2[:,p,:] -= 1.0*lamb1
vector2 -= (1./2.) * np.einsum( 'c,ijcb->ijb', cc.t1[p,:], lamb2 )
vector2 += (1./2.) * np.einsum( 'c,jicb->ijb', cc.t1[p,:], lamb2 )
else:
vector1 += lamb1[:,p-nocc]
vector2 += - 0.5 * ( lamb2[:,:,p-nocc,:] - lamb2[:,:,:,p-nocc] )
return cc.amplitudes_to_vector_ip(vector1,vector2)
#def eom_ccsd_ip_greenfunction(cc):
# eta = 0.1
# omega = [0.0];
# state = 0
#
# nocc,nvir = cc.t1.shape
# vector1 = np.zeros((nocc),dtype=complex)
# vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
# if p < nocc:
# vector1[ p ] -= 1.0
# vector1 += np.einsum('ic,c->i',cc.lamb1,cc.t1[p,:])
# vector1 += 0.25 * np.einsum('ilcd,lcd->i', lamb2, cc.t2[p,:,:,:] )
# vector1 -= 0.25 * np.einsum('ilcd,ldc->i', lamb2, cc.t2[p,:,:,:] )
# #vector2
# else:
# vector1 += cc.lamb1[:,p-nocc]
# vector2 += 0.5 * (cc.t2[:,:,p-nocc,:] - cc.t2[:,:,:,p-nocc])
# return cc.amplitudes_to_vector_ip(vector1,vector2)