-
Notifications
You must be signed in to change notification settings - Fork 1
/
gf.py
194 lines (170 loc) · 6.84 KB
/
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
import collections
import numpy as np
#import gminres
import scipy.sparse.linalg as spla
from pyscf.cc import eom_rccsd
from pyscf.cc.eom_rccsd import EOMIP, EOMEA
###################
# EA Greens #
###################
def greens_b_vector_ea_rhf(cc,p):
nocc, nvir = cc.t1.shape
ds_type = cc.t1.dtype
vector1 = np.zeros((nvir),dtype=ds_type)
vector2 = np.zeros((nocc,nvir,nvir),dtype=ds_type)
if p < nocc:
# Changed both to minus
vector1 += -cc.t1[p,:]
vector2 += -cc.t2[p,:,:,:]
else:
vector1[ p-nocc ] = 1.0
return eom_rccsd.amplitudes_to_vector_ea(vector1,vector2)
def greens_e_vector_ea_rhf(cc,p):
nocc, nvir = cc.t1.shape
ds_type = cc.t1.dtype
vector1 = np.zeros((nvir),dtype=ds_type)
vector2 = np.zeros((nocc,nvir,nvir),dtype=ds_type)
if hasattr(cc, 'l1') and cc.l1 is not None:
l1 = cc.l1
l2 = cc.l2
else:
l1 = cc.t1
l2 = cc.t2
if p < nocc:
# Changed both to plus
vector1 += l1[p,:]
vector2 += (2*l2[p,:,:,:] - l2[:,p,:,:])
else:
vector1[ p-nocc ] = -1.0
vector1 += np.einsum('ia,i->a', l1, cc.t1[:,p-nocc])
vector1 += 2*np.einsum('klca,klc->a', l2, cc.t2[:,:,:,p-nocc])
vector1 -= np.einsum('klca,lkc->a', l2, cc.t2[:,:,:,p-nocc])
vector2[:,p-nocc,:] += -2.*l1
vector2[:,:,p-nocc] += l1
vector2 += 2*np.einsum('k,jkba->jab', cc.t1[:,p-nocc], l2)
vector2 -= np.einsum('k,jkab->jab', cc.t1[:,p-nocc], l2)
return eom_rccsd.amplitudes_to_vector_ea(vector1,vector2)
###################
# IP Greens #
###################
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 eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def greens_e_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 hasattr(cc, 'l1') and cc.l1 is not None:
l1 = cc.l1
l2 = cc.l2
else:
l1 = cc.t1
l2 = cc.t2
if p < nocc:
vector1[ p ] = -1.0
vector1 += np.einsum('ia,a->i', l1, cc.t1[p,:])
vector1 += 2*np.einsum('ilcd,lcd->i', l2, cc.t2[p,:,:,:])
vector1 -= np.einsum('ilcd,ldc->i', l2, cc.t2[p,:,:,:])
vector2[p,:,:] += -2.*l1
vector2[:,p,:] += l1
vector2 += 2*np.einsum('c,ijcb->ijb', cc.t1[p,:], l2)
vector2 -= np.einsum('c,jicb->ijb', cc.t1[p,:], l2)
else:
vector1 += -l1[:,p-nocc]
vector2 += -2*l2[:,:,p-nocc,:] + l2[:,:,:,p-nocc]
return eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def greens_func_multiply(ham,vector,linear_part,*args):
return np.array(ham(vector,*args) + (linear_part)*vector)
def initial_ip_guess(cc):
nocc, nvir = cc.t1.shape
vector1 = np.zeros((nocc),dtype=complex)
vector2 = np.zeros((nocc,nocc,nvir),dtype=complex)
return eom_rccsd.amplitudes_to_vector_ip(vector1,vector2)
def initial_ea_guess(cc):
nocc, nvir = cc.t1.shape
vector1 = np.zeros((nvir),dtype=complex)
vector2 = np.zeros((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, 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")
Sw = initial_ip_guess(cc)
Sw += np.random.rand(Sw.shape[0])
diag = self.eomip.get_diag()
imds = self.eomip.make_imds()
e_vector = list()
for q in qs:
e_vector.append(greens_e_vector_ip_rhf(cc,q))
gfvals = np.zeros((len(ps),len(qs),len(omegas)),dtype=complex)
for ip, p in enumerate(ps):
print 'gf idx', ip
b_vector = greens_b_vector_ip_rhf(cc,p)
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(self.eomip.matvec, vector, omega + 1j*self.eta, imds)
size = len(b_vector)
Ax = spla.LinearOperator((size,size), matr_multiply)
mx = spla.LinearOperator((size,size), invprecond_multiply)
Sw, info = spla.gmres(Ax, b_vector, x0=Sw, tol=1e-14, M=mx)
if info != 0:
raise RuntimeError
for iq,q in enumerate(qs):
gfvals[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, 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")
Sw = initial_ea_guess(cc)
diag = self.eomea.get_diag()
e_vector = list()
for p in ps:
e_vector.append(greens_e_vector_ea_rhf(cc,p))
gfvals = np.zeros((len(ps),len(qs),len(omegas)),dtype=complex)
for iq, q in enumerate(qs):
b_vector = greens_b_vector_ea_rhf(cc,q)
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(self.eomea.matvec, vector, -omega + 1j*self.eta)
size = len(b_vector)
Ax = spla.LinearOperator((size,size), matr_multiply)
mx = spla.LinearOperator((size,size), invprecond_multiply)
Sw, info = spla.gmres(Ax, b_vector, x0=Sw, tol=1e-15, M=mx)
print '##################info######################'
print info
#print '#################Sw##########################'
#print Sw
#print '###############e_vector#######################'
#print e_vector
for ip,p in enumerate(ps):
#print '##############e_vector[ip]###############3'
#print e_vector[ip]
gfvals[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, p, q, omegas):
#return self.solve_ip(p, q, omegas) #, self.solve_ea(p, q, omegas)
return self.solve_ea(p,q,omegas)