-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv_product.f90
484 lines (385 loc) · 17 KB
/
v_product.f90
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
!this subroutine calculates
!the Coulombian operator on the optimal product basis
subroutine v_product
USE kinds, ONLY : DP
USE wvfct, ONLY : npw,npwx,et
USE mp, ONLY : mp_sum,mp_barrier
USE klist, ONLY : nks,ngk,xk
USE noncollin_module, ONLY: npol, noncolin
USE mp_world, ONLY : world_comm,mpime
USE spin_orb, ONLY: lspinorb
USE io_global, ONLY : stdout, ionode
USE input_simple
USE gvect, ONLY : ngm, gstart,gg, g
USE constants, ONLY : e2, fpi
USE cell_base, ONLY: tpiba,tpiba2,omega,bg,at
USE io_files, ONLY : prefix, tmp_dir
USE klist, ONLY : nks,xk
USE fft_base, ONLY : dfftp, dffts
USE fft_interfaces, ONLY : fwfft, invfft
USE io_files, ONLY : prefix, tmp_dir, diropn
USE wavefunctions, ONLY : psic
USE polarization
USE cell_base, ONLY : alat
implicit none
INTEGER, EXTERNAL :: find_free_unit
INTEGER :: ig,ii,iun,jj,kk
REAL(kind=DP), ALLOCATABLE :: fac(:)
REAL(kind=DP) :: qq
COMPLEX(kind=DP), ALLOCATABLE :: prod_g(:,:)
COMPLEX(kind=DP), ALLOCATABLE :: vmat(:,:)
INTEGER :: ik,jk,ijk(3),ll
REAL(kind=DP) :: qk(3),gk(3),sca
INTEGER :: iunp
LOGICAL :: exst
REAL(kind=DP), ALLOCATABLE :: p_basis_r(:)
COMPLEX(kind=DP), ALLOCATABLE :: p_basis(:,:)
TYPE(polaw) :: pw
INTEGER :: ix,iy,iz,ipol,iw
INTEGER, PARAMETER :: n_int=20
REAL(kind=DP) :: qx(3),qy(3),qz(3),qt(3),qq_fact
COMPLEX(kind=DP), ALLOCATABLE :: amat(:,:),tmp_mat(:,:),p_mat(:,:)
REAL(kind=DP), ALLOCATABLE :: facg(:)
INTEGER, parameter :: n_int_loc = 20*50
REAL(kind=DP) :: model
INTEGER :: n_trovato
write(stdout,*) 'Routine v_product'
allocate(fac(npw_max*npol))
if(l_truncated_coulomb) then
do ig=1,npw_max
qq = g(1,ig)**2.d0 + g(2,ig)**2.d0 + g(3,ig)**2.d0
if (qq > 1.d-8) then
fac(ig)=(e2*fpi/(tpiba2*qq))*(1.d0-dcos(dsqrt(qq)*truncation_radius*tpiba))
else
fac(ig)=e2*fpi*(truncation_radius**2.d0/2.d0)
endif
enddo
else
do ig=1,npw_max
qq = g(1,ig)**2.d0 + g(2,ig)**2.d0 + g(3,ig)**2.d0
if (qq > 1.d-8) then
fac(ig)=e2*fpi/(tpiba2*qq)
else
fac(ig)=0.d0
endif
enddo
endif
fac=fac/omega/nks
if(npol>1) fac(npw_max+1:npw_max*npol)=fac(1:npw_max)
allocate(prod_g(npw_max*npol,nprod_e))
prod_g(1:npw_max*npol,1:nprod_e)=prod_e(1:npw_max*npol,1:nprod_e)
do ii=1,nprod_e
prod_g(1:npw_max*npol,ii)=fac(1:npw_max*npol)*prod_g(1:npw_max*npol,ii)
enddo
allocate(vmat(nprod_e,nprod_e))
call ZGEMM('C','N',nprod_e,nprod_e,npw_max*npol,(1.d0,0.d0),prod_g,npw_max*npol,prod_e,npw_max*npol,(0.d0,0.d0),vmat,nprod_e)
call mp_sum(vmat,world_comm)
!write v_mat on disk
if(ionode) then
iun=find_free_unit()
open( unit= iun, file=trim(tmp_dir)//trim(prefix)//'.v_mat0', &
&status='unknown',form='unformatted')
!product number
write(iun) nprod_e
do ii=1,nprod_e
write(iun) vmat(1:nprod_e,ii)
enddo
endif
if(nks /= nkpoints(1)*nkpoints(2)*nkpoints(3)) then
write(stdout,*) 'K points ill-defined'
stop
endif
if(ionode) write(iun) nkpoints(1:3)
do ik=1,nks!k'
do jk=1,nks!k
qk(1:3)=xk(1:3,ik)-xk(1:3,jk)
do ii=1,3
sca=qk(1)*at(1,ii)+qk(2)*at(2,ii)+qk(3)*at(3,ii)
sca=sca*nkpoints(ii)
ijk(ii)=nint(sca)
if(ijk(ii)<0) ijk(ii)=ijk(ii)+nkpoints(ii)
if(ijk(ii)>= nkpoints(ii)) ijk(ii)=ijk(ii)-nkpoints(ii)
enddo
if(ionode) write(iun) ijk(1:3)
enddo
enddo
!obtain k'-k-->ijk table
!write k'-k--->ijk table on disk
!loop on i,j,k
n_trovato=0
do ii=0,nkpoints(1)-1
do jj=0,nkpoints(2)-1
do kk=0,nkpoints(3)-1
qk(1:3)=bg(1:3,1)*real(ii)/real(nkpoints(1))+bg(1:3,2)*real(jj)/real(nkpoints(2))+&
& bg(1:3,3)*real(kk)/real(nkpoints(3))
qk=-qk!(-) dovrebbe esser giusto da analisi G,k
do ig=1,npw_max
gk(1:3) = qk(1:3) + g(1:3,ig)
qq = gk(1)**2.d0 + gk(2)**2.d0 + gk(3)**2.d0
if (qq > 1.d-8) then
!fac(ig)=e2*fpi/(tpiba2*qq)
fac(ig)=0.d0
do ix=-n_int+1,n_int
do iy=-n_int+1,n_int
do iz=-n_int+1,n_int
qx(:)=0.5d0*(1.d0/dble(n_int*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int*nkpoints(3)))*bg(:,3)
qt(1:3)=qx(1:3)+qy(1:3)+qz(1:3)+gk(1:3)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int))**3.d0)/tpiba2
else
fac(ig)=0.d0
!write(stdout,*) ' TROVATO',qk(1:3),g(1:3,ig)
n_trovato=mpime+1
do ix=-n_int_loc+1,n_int_loc
do iy=-n_int_loc+1,n_int_loc
do iz=-n_int_loc+1,n_int_loc
qx(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int_loc*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int_loc*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int_loc*nkpoints(3)))*bg(:,3)
qt(1:3)=qx(1:3)+qy(1:3)+qz(1:3)+qk(1:3)+g(1:3,ig)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int_loc))**3.d0)/tpiba2
endif
enddo
fac=fac/omega/nks
if(npol>1) fac(npw_max+1:npw_max*npol)=fac(1:npw_max)
prod_g(1:npw_max*npol,1:nprod_e)=prod_e(1:npw_max*npol,1:nprod_e)
do ll=1,nprod_e
prod_g(1:npw_max*npol,ll)=fac(1:npw_max*npol)*prod_g(1:npw_max*npol,ll)
enddo
call ZGEMM('C','N',nprod_e,nprod_e,npw_max*npol,(1.d0,0.d0),prod_g,npw_max*npol,&
&prod_e,npw_max*npol,(0.d0,0.d0),vmat,nprod_e)
call mp_sum(vmat,world_comm)
if(ionode) then
do ll=1,nprod_e
write(iun) vmat(1:nprod_e,ll)
enddo
endif
enddo
enddo
enddo
call mp_sum(n_trovato, world_comm)
!write(stdout,*) 'MPI TROVATO', n_trovato
if(w_type==1) then
do ii=0,nkpoints(1)-1
do jj=0,nkpoints(2)-1
do kk=0,nkpoints(3)-1
qk(1:3)=bg(1:3,1)*real(ii)/real(nkpoints(1))+bg(1:3,2)*real(jj)/real(nkpoints(2))+&
& bg(1:3,3)*real(kk)/real(nkpoints(3))
qk=-qk!era -
do ig=1,npw_max
gk(1:3) = qk(1:3) + g(1:3,ig)
qq = gk(1)**2.d0 + gk(2)**2.d0 + gk(3)**2.d0
!HERE
qq=qq*tpiba2/0.529**2.d0
model=(1.d0/epsm-1.d0)*exp(-3.1415926d0*qq/2.d0/lambdam**2.d0)
if (qq > 1.d-8) then
fac(ig)=0.d0
do ix=-n_int+1,n_int
do iy=-n_int+1,n_int
do iz=-n_int+1,n_int
qx(:)=0.5d0*(1.d0/dble(n_int*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int*nkpoints(3)))*bg(:,3)
qt(1:3)=qx(1:3)+qy(1:3)+qz(1:3)+gk(1:3)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int))**3.d0)/tpiba
fac(ig)=fac(ig)*model
else
fac(ig)=0.d0
do ix=-n_int_loc+1,n_int_loc
do iy=-n_int_loc+1,n_int_loc
do iz=-n_int_loc+1,n_int_loc
qx(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int_loc*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int_loc*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int_loc*nkpoints(3)))*bg(:,3)
qt(1:3)=qx(1:3)+qy(1:3)+qz(1:3)+qk(1:3)+g(1:3,ig)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int_loc))**3.d0)/tpiba2
fac(ig)=fac(ig)*model
endif
enddo
fac=fac/omega/nks
if(npol>1) fac(npw_max+1:npw_max*npol)=fac(1:npw_max)
prod_g(1:npw_max*npol,1:nprod_e)=prod_e(1:npw_max*npol,1:nprod_e)
do ll=1,nprod_e
prod_g(1:npw_max*npol,ll)=fac(1:npw_max*npol)*prod_g(1:npw_max*npol,ll)
enddo
call ZGEMM('C','N',nprod_e,nprod_e,npw_max*npol,(1.d0,0.d0),prod_g,npw_max*npol,&
&prod_e,npw_max*npol,(0.d0,0.d0),vmat,nprod_e)
call mp_sum(vmat,world_comm)
if(ionode) then
do ll=1,nprod_e
write(iun) vmat(1:nprod_e,ll)
enddo
endif
enddo
enddo
enddo
else
!calculate v(i,j,k) matrix
!write it on disk
!part relative to W_c
!read in polarisability basis and put it on G space
iunp=find_free_unit()
allocate(p_basis_r(dfftp%nnr))
allocate(p_basis(npw_max,numpw))
CALL diropn( iunp, 'basis2simple',dfftp%nnr, exst )
do ii=1,numpw
call davcio(p_basis_r,dfftp%nnr,iunp,ii,-1)
!do fft
psic(1:dfftp%nnr)=p_basis_r(1:dfftp%nnr)
CALL fwfft ('Rho', psic, dfftp)
do ig=1,npw_max
p_basis(ig,ii)=psic(dfftp%nl(ig))
enddo
enddo
close(iunp)
deallocate(p_basis_r)
!read in irreducible polarisability operator
!read P
call initialize_polaw(pw)
call read_polaw_global(0, pw)
allocate(amat(nprod_e,numpw),p_mat(numpw,numpw))
allocate(tmp_mat(nprod_e,numpw))
p_mat(1:numpw,1:numpw)=pw%pw(1:numpw,1:numpw)
!calculate |G|
allocate(facg(npw_max*npol))
do ig=1,npw_max
gk(1:3) = g(1:3,ig)
qq = gk(1)**2.d0 + gk(2)**2.d0 + gk(3)**2.d0
if (qq > 1.d-8) then
!facg(ig)=e2*fpi/tpiba2*qq
facg(ig)=0
do ix=-n_int+1,n_int
do iy=-n_int+1,n_int
do iz=-n_int+1,n_int
qx(:)=0.5d0*(1.d0/dble(n_int)*(dble(ix-1))+0.5d0/dble(n_int))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int)*(dble(iy-1))+0.5d0/dble(n_int))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int)*(dble(iz-1))+0.5d0/dble(n_int))*bg(:,3)
qt(:)=qx(1:3)+qy(1:3)+qz(1:3)+g(1:3,ig)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
facg(ig)=facg(ig)+1.d0/qq_fact
enddo
enddo
enddo
facg(ig)=facg(ig)*e2*fpi/(8.d0*(dble(n_int))**3.d0)/tpiba
else
!do integrate
facg(ig)=0.d0
do ix=-n_int_loc+1,n_int_loc
do iy=-n_int_loc+1,n_int_loc
do iz=-n_int_loc+1,n_int_loc
qx(:)=0.5d0*(1.d0/dble(n_int_loc)*(dble(ix-1))+0.5d0/dble(n_int_loc))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int_loc)*(dble(iy-1))+0.5d0/dble(n_int_loc))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int_loc)*(dble(iz-1))+0.5d0/dble(n_int_loc))*bg(:,3)
qt(:)=qx(1:3)+qy(1:3)+qz(1:3)+g(1:3,ig)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
facg(ig)=facg(ig)+1.d0/qq_fact
enddo
enddo
enddo
facg(ig)=facg(ig)*e2*fpi/(8.d0*(dble(n_int_loc))**3.d0)/tpiba2
endif
enddo
if(npol>1) facg(1+(npol-1)*npw_max:npol*npw_max)=facg(1:npw_max)
facg=facg/omega
!loop on q
!calculate fac terms and in case do integrate
do ii=0,nkpoints(1)-1
do jj=0,nkpoints(2)-1
do kk=0,nkpoints(3)-1
qk(1:3)=bg(1:3,1)*real(ii)/real(nkpoints(1))+bg(1:3,2)*real(jj)/real(nkpoints(2))+&
& bg(1:3,3)*real(kk)/real(nkpoints(3))
qk=-qk!!- dovrebbe esser giusto da analisi G,k
do ig=1,npw_max
gk(1:3) = qk(1:3) + g(1:3,ig)
qq = gk(1)**2.d0 + gk(2)**2.d0 + gk(3)**2.d0
if (qq > 1.d-8) then
!fac(ig)=e2*fpi/(tpiba2*qq)
fac(ig)=0.d0
do ix=-n_int+1,n_int
do iy=-n_int+1,n_int
do iz=-n_int+1,n_int
qx(:)=0.5d0*(1.d0/dble(n_int*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int*nkpoints(3)))*bg(:,3)
qt(:)=qx(1:3)+qy(1:3)+qz(1:3)+gk(1:3)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int))**3.d0)/tpiba2
else
!do integrate
fac(ig)=0.d0
!write(stdout,*) ' TROVATO',qk(1:3),g(1:3,ig)
do ix=-n_int_loc+1,n_int_loc
do iy=-n_int_loc+1,n_int_loc
do iz=-n_int_loc+1,n_int_loc
qx(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(1))*(dble(ix-1))+0.5d0/dble(n_int_loc*nkpoints(1)))*bg(:,1)
qy(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(2))*(dble(iy-1))+0.5d0/dble(n_int_loc*nkpoints(2)))*bg(:,2)
qz(:)=0.5d0*(1.d0/dble(n_int_loc*nkpoints(3))*(dble(iz-1))+0.5d0/dble(n_int_loc*nkpoints(3)))*bg(:,3)
qt(:)=qx(1:3)+qy(1:3)+qz(1:3)+qk(1:3)+g(1:3,ig)
qq_fact=qt(1)**2+qt(2)**2+qt(3)**2
fac(ig)=fac(ig)+1.d0/qq_fact
enddo
enddo
enddo
fac(ig)=fac(ig)*e2*fpi/(8.d0*(dble(n_int_loc))**3.d0)/tpiba2
endif
enddo
fac=fac/omega/nks
fac=fac*sqrt(facg)/sqrt(fac)
!calculate <\mathcal{E_\alpha}|(V(q)\Phi_\mu>
!calcuate W_c(q)_\alpha\beta
vmat=0.d0
do ipol=0,npol-1
do iw=1,nprod_e
prod_g(1:npw_max,iw)=fac(1:npw_max)*prod_e(1+ipol*npw_max:npw_max+npw_max*ipol,iw)
enddo
call ZGEMM('C','N',nprod_e,numpw,npw_max,(1.d0,0.d0),prod_g,npw_max*npol,p_basis,npw_max,(0.d0,0.d0),amat,nprod_e)
call mp_sum(amat, world_comm)
call ZGEMM('N','N',nprod_e,numpw,numpw,(1.d0,0.d0),amat,nprod_e,p_mat,numpw,(0.d0,0.d0),tmp_mat,nprod_e)
call ZGEMM('N','C',nprod_e,nprod_e,numpw,(1.d0,0.d0),tmp_mat,nprod_e,amat,nprod_e,(1.d0,0.d0),vmat,nprod_e)
enddo
!writes on disk
if(ionode) then
do ll=1,nprod_e
write(iun) vmat(1:nprod_e,ll)
enddo
endif
enddo
enddo
enddo
deallocate(p_basis)
deallocate(amat)
deallocate(tmp_mat)
deallocate(facg)
call free_memory_polaw(pw)
endif
if(ionode) close(iun)
deallocate(vmat)
deallocate(prod_g)
deallocate(fac)
write(stdout,*) 'Out of v_product'
end subroutine v_product