-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChevplusRSCG.py
459 lines (353 loc) · 11.3 KB
/
ChevplusRSCG.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from scipy.sparse import lil_matrix
from numpy.linalg import norm
import numpy as np
from multiprocessing import Pool
from multiprocessing import Process
from scipy import linalg
import time
def xy2i(ix,iy,Nx):
ii = (iy)*Nx+ix
return ii
def init_delta(Nx,Ny,delta):
vec_delta = lil_matrix((Nx*Ny,Nx*Ny))
for ii in range(Nx*Ny):
vec_delta[ii,ii] = delta
return vec_delta
def calc_A(Nx,Ny,mu,vec_delta,aa):
Ln = Nx*Ny*2
A = lil_matrix((Ln,Ln))
# A.setdiag(-mu)
for ix in range(Nx):
for iy in range(Ny):
ii = xy2i(ix,iy,Nx)
jx = ix
jy = iy
jj = xy2i(jx,jy,Nx)
A[ii,jj] = -mu
# print(ii)
# +1 in x direction
jx = ix +1
if jx == Nx:
jx = 0
jy = iy
jj = xy2i(jx,jy,Nx)
A[ii,jj] = -1.0
# -1 in x direction
jx = ix -1
if jx == -1:
jx = Nx-1
jy = iy
jj = xy2i(jx,jy,Nx)
A[ii,jj] = -1.0
# + 1 in y direction
jx = ix
jy = iy +1
if jy == Ny:
jy = 0
jj = xy2i(jx,jy,Nx)
A[ii,jj] = -1.0
# -1 in y direction
jx = ix
jy = iy -1
if jy == -1:
jy = Ny-1
jj = xy2i(jx,jy,Nx)
A[ii,jj] = -1.0
for ii in range(Nx*Ny):
# print(ii)
for jj in range(Nx*Ny):
# print("jj",jj)
A[ii+Nx*Ny,jj+Nx*Ny] = -A[ii,jj]
A[ii,jj+Nx*Ny] = vec_delta[ii,jj]
A[ii+Nx*Ny,jj] = vec_delta[jj,ii]
A = A/aa
# print(A)
A = A.tocsr()
# print(A)
return A
def calc_A2(A2,Nx,Ny,mu,vec_delta,aa):
Ln = Nx*Ny*2
A = A2.tolil()
A = A*aa
for ii in range(Nx*Ny):
# print(ii)
for jj in range(Nx*Ny):
# print("jj",jj)
A[ii,jj+Nx*Ny] = vec_delta[ii,jj]
A[ii+Nx*Ny,jj] = vec_delta[jj,ii]
A = A/aa
# print(A)
A = A.tocsr()
# print(A)
return A
def iteration_RSCG(nc,Nx,Ny,U,mu,full,T,omegamax):
Ln = Nx*Ny*2
vec_delta = init_delta(Nx,Ny,0.1)
vec_delta_old = vec_delta
aa = 1.0
A = calc_A(Nx,Ny,mu,vec_delta,aa)
itemax = 100
for ite in range(itemax):
if full == False:
eps = 1e-8
vec_delta = calc_meanfields_RSCG(eps,A,Nx,Ny,Ln,T,omegamax)
else:
vec_delta = calc_meanfields_full_finite(nc,A,Nx,Ny,Ln,T,omegamax)
vec_delta = vec_delta*U
# A = calc_A(Nx,Ny,1e-12,vec_delta,10.0)
A = calc_A2(A,Nx,Ny,1e-12,vec_delta,aa)
eps = 0.0
nor = 0.0
for i in range(Nx*Ny):
eps += abs(vec_delta[i,i] -vec_delta_old[i,i])**2
nor += abs(vec_delta_old[i,i])**2
eps = eps/nor
print "ite = ",ite,eps
if eps <= 1e-6:
print "End",vec_delta[Nx/2,Ny/2]
break
vec_delta_old = vec_delta
return vec_delta
# print(vec_delta)
def iteration(nc,Nx,Ny,aa,bb,omegac,U,full,mu):
Ln = Nx*Ny*2
vec_delta = init_delta(Nx,Ny,0.1)
vec_delta_old = vec_delta
A = calc_A(Nx,Ny,mu,vec_delta,10.0)
itemax = 100
for ite in range(itemax):
if full == False:
vec_delta = calc_meanfields(nc,A,Nx,Ny,Ln,aa,bb,omegac)
else:
vec_delta = calc_meanfields_full(nc,A,Nx,Ny,Ln,aa,bb,omegac)
vec_delta = vec_delta*U
# A = calc_A(Nx,Ny,1e-12,vec_delta,10.0)
A = calc_A2(A,Nx,Ny,1e-12,vec_delta,10.0)
eps = 0.0
nor = 0.0
for i in range(Nx*Ny):
eps += abs(vec_delta[i,i] -vec_delta_old[i,i])**2
nor += abs(vec_delta_old[i,i])**2
eps = eps/nor
print "ite = ",ite,eps
if eps <= 1e-6:
print "End",vec_delta[Nx/2,Ny/2]
break
vec_delta_old = vec_delta
return vec_delta
# print(vec_delta)
def RSCG(eps,n_omega,left_i,right_j,vec_sigma,A,Ln):
#--Line 2 in Table III.
vec_x = np.zeros(Ln)
vec_b = np.zeros(Ln)
vec_b[right_j] = 1.0
vec_r = np.zeros(Ln)
vec_p = np.zeros(Ln)
vec_r[right_j] = 1.0
vec_p[right_j] = 1.0
alpham = 1.0
betam = 0.0
#--
Sigma = vec_b[left_i] #Line 3. Sigma=V.b. V=v1^T, v1^T=e(j)^T = (0,0,0,0,...,1,...,0,0,0)
#---Initialization of arrays
vec_Ap = np.zeros(Ln)
vec_g = np.zeros(n_omega, dtype=np.complex)
vec_rhok = np.ones(n_omega, dtype=np.complex)
vec_rhokp = np.ones(n_omega, dtype=np.complex)
vec_rhokm = np.ones(n_omega, dtype=np.complex)
vec_alpha = np.zeros(n_omega, dtype=np.complex)
vec_beta = np.zeros(n_omega, dtype=np.complex)
vec_Theta = np.zeros(n_omega, dtype=np.complex)
vec_Pi = np.ones(n_omega, dtype=np.complex)*Sigma
#---
flag = True
hi = 1.0
# a = -A.todense()
# x = np.linalg.solve(a, vec_b)
ep = 1e-15
while hi > eps:
vec_Ap = -A.dot(vec_p) # A pk = (-H).pk
rsum = np.dot(vec_r,vec_r) #(rk,rk)
alpha = rsum/np.dot(vec_p,vec_Ap) #Line 6 (rk,rk)/(pk,A pk)
# print alpha,rsum
vec_x += alpha*vec_p #Line 7
vec_r += - alpha*vec_Ap #Line 8
beta = np.dot(vec_r,vec_r)/rsum #Line9 (r_{k+1},r_{k+1})/(rk,rk)
vec_p = vec_r + beta*vec_p #Line 10
Sigma = vec_r[left_i] #Line 11 Sigma=V.r_{k+1}
# index = vec_rhok > ep
#---- Lines 12-17
vec_rhokp = np.where(vec_rhok > ep,vec_rhok*vec_rhokm*alpham/(vec_rhokm*alpham*(1.0+alpha*vec_sigma)+alpha*betam*(vec_rhokm-vec_rhok)),vec_rhok) #Line 13
vec_alpha = np.where(vec_rhok> ep, alpha*vec_rhokp/vec_rhok,0.0) #Line 14
vec_Theta = vec_Theta+vec_alpha*vec_Pi #Line 15
vec_beta = np.where(vec_rhok > ep,((vec_rhokp/vec_rhok)**2)*beta,1.0) #Line 16
vec_Pi = np.where(vec_rhok > ep,vec_rhokp*Sigma+ vec_beta*vec_Pi,vec_Pi) #Line 17
vec_g = vec_Theta
vec_rhokm = vec_rhok
vec_rhok = vec_rhokp
#----
alpham = alpha
betam = beta
hi = rsum
continue
return vec_g
def calc_meanfields_RSCG(eps,A,Nx,Ny,Ln,T,omegamax):
A = A*1.0
ci = 1j
pi = np.arctan(1.0)*4
vec_delta = lil_matrix((Nx*Ny,Nx*Ny))
# omegamax = omegac #pi*T(2*n+1), omegac/(T*pi)
n_omega = (int(omegamax/(T*pi)))/2-1
vec_sigma = np.zeros(2*n_omega, dtype=np.complex)
for n in range(2*n_omega):
vec_sigma[n] = pi*T*(2.0*(n-n_omega)+1)*ci
for ix in range(Nx):
for iy in range(Ny):
ii = xy2i(ix,iy,Nx)
jj = ii + Nx*Ny
right_j = jj
left_i = ii
vec_g = RSCG(eps,2*n_omega,left_i,right_j,vec_sigma,A,Ln)
vec_delta[ii,ii] = np.real(T*np.sum(vec_g))
return vec_delta
def calc_meanfields(nc,A,Nx,Ny,Ln,aa,bb,omegac):
vec_delta = lil_matrix((Nx*Ny,Nx*Ny))
for ix in range(Nx):
for iy in range(Ny):
ii = xy2i(ix,iy,Nx)
jj = ii + Nx*Ny
right_j = jj
left_i = ii
vec_ai = calc_polynomials(nc,left_i,right_j,A,Ln)
density = calc_meanfield(vec_ai,aa,bb,omegac,nc)
vec_delta[ii,ii] = density
return vec_delta
def calc_meanfields_full(nc,A,Nx,Ny,Ln,aa,bb,omegac):
a = A.todense()*aa
w, v = linalg.eigh(a, lower=True, eigvals_only=False, overwrite_a=False, eigvals=None)
vec_delta = lil_matrix((Nx*Ny,Nx*Ny))
for ix in range(Nx):
for iy in range(Ny):
ii = xy2i(ix,iy,Nx)
jj = ii + Nx*Ny
delta = 0.0
for i in range(Nx*Ny*2):
if w[i] <= 0.0:
if abs(w[i]) <= omegac:
delta += v[ii,i]*v[jj,i]
vec_delta[ii,ii] = delta
# print delta
return vec_delta
def calc_meanfields_full_finite(nc,A,Nx,Ny,Ln,T,omegamax):
a = A.todense()
w, v = linalg.eigh(a, lower=True, eigvals_only=False, overwrite_a=False, eigvals=None)
vec_delta = lil_matrix((Nx*Ny,Nx*Ny))
for ix in range(Nx):
for iy in range(Ny):
ii = xy2i(ix,iy,Nx)
jj = ii + Nx*Ny
delta = 0.0
delta = calc_green(ii,jj,Nx,Ny,w,v,omegamax,T)
vec_delta[ii,ii] = delta
# print delta
return vec_delta
def calc_green(ii,jj,Nx,Ny,w,v,omegamax,T):
pi = np.arctan(1.0)*4.0
ci = 1j
n_omega = (int(omegamax/(T*pi)))/2-1
vec_sigma = np.zeros(2*n_omega, dtype=np.complex)
vec_g = np.zeros(2*n_omega, dtype=np.complex)
for n in range(2*n_omega):
vec_sigma[n] = pi*T*(2.0*(n-n_omega)+1)*ci
for i in range(Nx*Ny*2):
vec_g += v[ii,i]*v[jj,i]/(vec_sigma - w[i])
delta = T*np.sum(np.real(vec_g))
return delta
def calc_meanfield(vec_ai,aa,bb,omegac,nc):
ba = np.arccos(-bb/aa)
omeb = np.arccos(-(omegac+bb)/aa)
pi = np.arctan(1.0)*4
density = 0.0
for j in range(nc-1):
i = j + 1
density += vec_ai[i]*(np.sin(i*omeb)-np.sin(i*ba))/i
density += vec_ai[0]*(omeb-ba)/2.0
density = density*2/pi
return density
def calc_polynomials(nc,left_i,right_j,A,Ln):
vec_jnmm = np.zeros(Ln)
vec_jnm = np.zeros(Ln)
vec_jn = np.zeros(Ln)
vec_jn[right_j] = 1.0
vec_ai = np.zeros(nc)
for nn in range(nc):
if nn == 0:
vec_jnmm = 0.0
vec_jnm = 0.0
vec_jn[right_j] = 1.0
elif nn == 1:
vec_jn = A.dot(vec_jn)
else:
vec_jn = 2.0*A.dot(vec_jnm) - vec_jnmm
vec_ai[nn] = vec_jn[left_i]
vec_jnmm = vec_jnm
vec_jnm = vec_jn
return vec_ai
def main():
# for i in range(1,6):
# print("test")
nc = 1000
nx = 10
ny = 10
vec_delta = init_delta(nx,ny,0.1)
mu = -1.0
A = calc_A(nx,ny,mu,vec_delta,10.0)
# print A
# quit()
U = -4.0
mu = -1.5
a = A.todense()
w, v = linalg.eigh(a, lower=True, eigvals_only=False, overwrite_a=False, eigvals=None)
# print(w
vec_ai = calc_polynomials(nc,1,1+nx*ny,A,nx*ny*2)
# print(vec_ai)
density = calc_meanfield(vec_ai,10.0,0.0,10.0,nc)
# print(density)
aa = 10.0
bb = 0.0
omegac = 10.0
Ln = nx*ny*2
# vec_delta = calc_meanfields(nc,A,nx,ny,Ln,aa,bb,omegac)
# print(vec_delta)
print "Exact diagonalization"
start = time.time()
iteration(nc,nx,ny,aa,bb,omegac,U,True,mu)
elapsed_time = time.time()-start
print "elapsed_time",elapsed_time
print "Chebyshev polynomial method"
start = time.time()
iteration(nc,nx,ny,aa,bb,omegac,U,False,mu)
elapsed_time = time.time()-start
print "elapsed_time",elapsed_time
print "RSCG method"
T = 0.05
pi = np.arctan(1.0)*4.0
omegamax = pi*240
start = time.time()
iteration_RSCG(nc,nx,ny,U,mu,False,T,omegamax)
elapsed_time = time.time()-start
print "elapsed_time",elapsed_time
print "Green-function-based Exact diagonalization"
start = time.time()
iteration_RSCG(nc,nx,ny,U,mu,True,T,omegamax)
elapsed_time = time.time()-start
print "elapsed_time",elapsed_time
# x = np.zeros((nx*ny*2))
# for i in range(nx*ny*2):
# x[i] = i + 1
# print(x)
# x = matrix([[1],[1],[1],[2],[2],[3]])
# y = A.dot(x)
# print(y)
if __name__ == "__main__":
main()