-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkpts_gf.py
254 lines (218 loc) · 9.57 KB
/
kpts_gf.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
import collections
import numpy as np
import scipy.sparse.linalg as spla
from pyscf.cc import eom_rccsd
from pyscf.cc.eom_rccsd import EOMIP, EOMEA
from pyscf.pbc.lib import kpts_helper
import time
import sys
###################
# EA Greens #
###################
def greens_b_vector_ea_rhf(cc, p, kp=None):
nkpts, nocc, nvir = cc.t1.shape
ds_type = cc.t1.dtype
vector1 = np.zeros((nvir),dtype=ds_type)
vector2 = np.zeros((nkpts,nkpts,nocc,nvir,nvir),dtype=ds_type)
if p < nocc:
vector1 += -cc.t1[kp,p,:]
for ki in range(nkpts):
for kj in range(nkpts):
vector2[ki,kj] += -cc.t2[kp,ki,kj,p,:,:,:]
else:
vector1[ p-nocc ] = 1.0
return eom_rccsd.amplitudes_to_vector_ea(vector1,vector2)
def greens_e_vector_ea_rhf(cc, p, kp=None):
nkpts, nocc, nvir = cc.t1.shape
ds_type = cc.t1.dtype
vector1 = np.zeros((nvir),dtype=ds_type)
vector2 = np.zeros((nkpts,nkpts,nocc,nvir,nvir),dtype=ds_type)
if hasattr(cc, 'l1') and cc.l1 is not None:
l1 = cc.l1
l2 = cc.l2
else:
l1 = np.conj(cc.t1)
l2 = np.conj(cc.t2)
if p < nocc:
vector1 += l1[kp,p,:]
for ki in range(nkpts):
for kj in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
kb = kconserv[ki,kj,kp]
ka = kconserv[ki,kb,kj]
vector2[ki,kj] += 2*l2[kp,ki,kj,p,:,:,:]
vector2[ki,kj] -= l2[ki,kp,kj,:,p,:,:]
else:
vector1[ p-nocc ] = -1.0
vector1 += np.einsum('ia,i->a', l1[kp], cc.t1[kp,:,p-nocc])
for kk in range(nkpts):
for kl in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
kc = kconserv[kl,kp,kk]
vector1 += 2 * np.einsum('klca,klc->a', l2[kk,kl,kc], \
cc.t2[kk,kl,kc,:,:,:,p-nocc])
vector1 -= np.einsum('klca,lkc->a', l2[kk,kl,kc], \
cc.t2[kl,kk,kc,:,:,:,p-nocc])
for kb in range(nkpts):
vector2[kb,kp,:,p-nocc,:] += -2.*l1[kb]
for ka in range(nkpts):
# kj == ka
# kb == kc == kp
vector2[ka,ka,:,:,p-nocc] += l1[ka]
for kj in range(nkpts):
for ka in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
kb = kconserv[kp,kj,ka]
vector2[kj,ka] += 2*np.einsum('k,jkba->jab', \
cc.t1[kp,:,p-nocc], l2[kj,kp,kb,:,:,:,:])
vector2[kj,ka] -= np.einsum('k,jkab->jab', \
cc.t1[kp,:,p-nocc], l2[kj,kp,ka,:,:,:,:])
return eom_rccsd.amplitudes_to_vector_ea(vector1,vector2)
###################
# IP Greens #
###################
def greens_b_vector_ip_rhf(cc,p,kp=None):
nkpts, nocc, nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nkpts,nkpts,nocc,nocc,nvir),dtype=complex)
if p < nocc:
vector1[p] = 1.0
else:
vector1 += cc.t1[kp,:,p-nocc]
for ki in range(nkpts):
for kj in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
ka = kconserv[ki,kj,kp]
vector2[ki,kj] += cc.t2[ki,kj,ka,:,:,:,p-nocc]
return eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def greens_e_vector_ip_rhf(cc,p,kp=None):
nkpts, nocc, nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nkpts,nkpts,nocc,nocc,nvir),dtype=complex)
if hasattr(cc, 'l1') and cc.l1 is not None:
l1 = cc.l1
l2 = cc.l2
else:
l1 = np.conj(cc.t1)
l2 = np.conj(cc.t2)
if p < nocc:
vector1[p] = -1.0
vector1 += np.einsum('ia,a->i', l1[kp], cc.t1[kp,p,:])
for kl in range(nkpts):
for kc in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
kd = kconserv[kp,kl,kc]
vector1 += 2 * np.einsum('ilcd,lcd->i', \
l2[kp,kl,kc], cc.t2[kp,kl,kc,p,:,:,:])
vector1 -= np.einsum('ilcd,ldc->i', \
l2[kp,kl,kc], cc.t2[kp,kl,kd,p,:,:,:])
for kj in range(nkpts):
vector2[kp,kj,p,:,:] += -2*l1[kj]
for ki in range(nkpts):
# kj == kk == kp, ki == kb
vector2[ki,kp,:,p,:] += l1[ki]
for kj in range(nkpts):
# kc == kk == kp
vector2[ki,kj] += 2*np.einsum('c,ijcb->ijb', \
cc.t1[kp,p,:], l2[ki,kj,kp,:,:,:,:])
vector2[ki,kj] -= np.einsum('c,jicb->ijb', \
cc.t1[kp,p,:], l2[kj,ki,kp,:,:,:,:])
else:
vector1 += -l1[kp,:,p-nocc]
for ki in range(nkpts):
for kj in range(nkpts):
kconserv = kpts_helper.get_kconserv(cc._scf.cell, cc.kpts)
kb = kconserv[ki,kj,kp]
vector2[ki, kj] += -2*l2[ki,kj,kp,:,:,p-nocc,:] + \
l2[ki,kj,kb,:,:,:,p-nocc]
return eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def greens_func_multiply(ham,vector,linear_part,kp):
return np.array(ham(vector,kp) + (linear_part)*vector)
def initial_ip_guess(cc):
nkpts, nocc, nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nkpts,nkpts,nocc,nocc,nvir),dtype=complex)
return eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def initial_ea_guess(cc):
nkpts, nocc, nvir = cc.t1.shape
vector1 = np.zeros((nvir),dtype=complex)
vector2 = np.zeros((nkpts,nkpts,nocc,nvir,nvir),dtype=complex)
return eom_rccsd.amplitudes_to_vector_ea(vector1,vector2)
class OneParticleGF(object):
def __init__(self, cc, eta=0.01):
self.cc = cc
self.eomip = EOMIP(cc)
self.eomea = EOMEA(cc)
self.eta = eta
def solve_ip(self, kptlist, ps, qs, omegas):
if not isinstance(ps, collections.Iterable): ps = [ps]
if not isinstance(qs, collections.Iterable): qs = [qs]
cc = self.cc
print("solving ip portion")
S0 = initial_ip_guess(cc)
gfvals = np.zeros((len(kptlist), len(ps),len(qs),len(omegas)),dtype=complex)
for kp, ikpt in enumerate(kptlist):
e_vector=list()
for q in qs:
e_vector.append(greens_e_vector_ip_rhf(cc,q,kp))
for ip, p in enumerate(ps):
b_vector = greens_b_vector_ip_rhf(cc,p,kp)
cc.kshift = kp
diag = cc.ipccsd_diag(kp)
for iw, omega in enumerate(omegas):
invprecond_multiply = lambda x: x/(omega + diag + 1j*self.eta)
def matr_multiply(vector,args=None):
return greens_func_multiply(cc.ipccsd_matvec, vector, omega + 1j*self.eta, kp)
size = len(b_vector)
Ax = spla.LinearOperator((size,size), matr_multiply)
mx = spla.LinearOperator((size,size), invprecond_multiply)
start = time.time()
Sw, info = spla.gcrotmk(Ax, b_vector, x0=S0, atol=0, tol=1e-2)
end = time.time()
print 'past gcrotmk with info and time',info,(end-start)
sys.stdout.flush()
if info != 0:
raise RuntimeError
for iq,q in enumerate(qs):
gfvals[kp,ip,iq,iw] = -np.dot(e_vector[iq],Sw)
if len(ps) == 1 and len(qs) == 1:
return gfvals[:,0,0,:]
else:
return gfvals
def solve_ea(self, kptlist, ps, qs, omegas):
if not isinstance(ps, collections.Iterable): ps = [ps]
if not isinstance(qs, collections.Iterable): qs = [qs]
cc = self.cc
print("solving ea portion")
S0 = initial_ea_guess(cc)
gfvals = np.zeros((len(kptlist),len(ps),len(qs),len(omegas)),dtype=complex)
for kp, ikpt in enumerate(kptlist):
e_vector=list()
for p in ps:
e_vector.append(greens_e_vector_ea_rhf(cc,p,kp))
for iq, q in enumerate(qs):
b_vector = greens_b_vector_ea_rhf(cc,q,kp)
cc.kshift = kp
diag = cc.eaccsd_diag(kp)
for iw, omega in enumerate(omegas):
invprecond_multiply = lambda x: x/(-omega + diag + 1j*self.eta)
def matr_multiply(vector,args=None):
return greens_func_multiply(cc.eaccsd_matvec, vector, -omega + 1j*self.eta, kp)
size = len(b_vector)
Ax = spla.LinearOperator((size,size), matr_multiply)
mx = spla.LinearOperator((size,size), invprecond_multiply)
start = time.time()
Sw, info = spla.gcrotmk(Ax, b_vector, x0=S0, atol=0, tol=1e-2)
end = time.time()
print 'past gcrotmk with info and time',info,(end-start)
sys.stdout.flush()
if info != 0:
raise RuntimeError
for ip,p in enumerate(ps):
gfvals[kp,ip,iq,iw] = np.dot(e_vector[ip],Sw)
if len(ps) == 1 and len(qs) == 1:
return gfvals[:,0,0,:]
else:
return gfvals
def kernel(self, k, p, q, omegas):
return self.solve_ip(k, p, q, omegas), self.solve_ea(k, p, q, omegas)