1subroutine mkppse(iband,jband,ikk,omegap2,xkf,lossfn,fsum, &
2& vol,pi,nwpt,wmax,nbcore,nbocc,ncband,ngkpt,natom,xred,projwf,nlmn, &
3& pwmatel,tpwmatel, &
4& kg,kgq,enrgy,enrgyq,cg,cgq,npwt,npwtq,bantot,bantotq,ncg,ncgq, &
5& indxkpw,indxkpwq,indxkbnd,indxkbndq,indxkcg,indxkcgq,npwarr,npwarrq, &
6& kpt,kptq,nkpt,nkptq,nqpt,nsymk,nsymkq,symk,symkq,nsym,nsymq,symrel,syminv, &
7& ihlf,ihlfq,lvtrans,lvtransq,bmet,blat,ipaw, &
8& ipwlf,npwlf,ipwndx,npwndx,ntpwndx, &
9& npwup,invpw2ndx,pwsymndx,iqsymndx,invpwndx,pwupmin,pwupmax, &
10& igmx,igmn,igndx,igndxq,ikndx,ikndxq,iqndx,isymndx,isymndxq,npw,npwq, &
11& nband,nbandq,nsppol,shiftk,shiftkq,zz,zzpp,zzcorr,cse,csepp,csecorr,xse)
12implicit none
13integer :: iband,jband,ikk(3),nwpt,nbcore,nbocc,ncband,ngkpt(3),natom,nlmn
14integer :: igmn(3),igmx(3)
15integer :: npwt,npwtq,bantot,bantotq,ncg,ncgq,nkpt,nkptq,nqpt,nsym,nsymq,nsppol
16integer :: npw,npwq,ipw1,npwlf,npwndx,ntpwndx,ipwa,ipaw
17double precision :: vol,pi,wmax,xred(3,natom)
18double precision :: omegap2,xkf,ppfct
19double complex :: lossfn(nwpt,nqpt,ntpwndx)
20double complex :: projwf(natom,nlmn,nkpt,ncband)
21integer :: kg(3,npwt),kgq(3,npwtq)
22double precision :: enrgy(bantot),enrgyq(bantotq)
23double complex :: cg(ncg),cgq(ncgq)
24integer :: indxkpw(nkpt),indxkpwq(nkptq),indxkbnd(nkpt),indxkbndq(nkptq)
25integer :: indxkcg(nkpt),indxkcgq(nkptq),npwarr(nkpt),npwarrq(nkptq)
26double precision :: kpt(3,nkpt),kptq(3,nkptq),shiftk(3),shiftkq(3)
27integer :: nsymk(nkpt),nsymkq(nkptq),symk(nkpt,nsym*2),symkq(nkptq,nsymq*2)
28integer :: symrel(3,3,nsym),syminv(3,3,nsymq)
29integer :: lvtrans(3,ngkpt(1),ngkpt(2),ngkpt(3))
30integer :: lvtransq(3,ngkpt(1),ngkpt(2),ngkpt(3))
31integer :: ihlf(nkpt),ihlfq(nkptq)
32double precision :: bmet(3,3),blat(3,3)
33integer :: ipwlf(3,npwlf),ipwndx(2,ntpwndx)
34integer :: igndx(nkpt,igmn(1):igmx(1),igmn(2):igmx(2),igmn(3):igmx(3))
35integer :: igndxq(nkptq,igmn(1):igmx(1),igmn(2):igmx(2),igmn(3):igmx(3))
36integer :: ikndx(ngkpt(1),ngkpt(2),ngkpt(3)),ikndxq(ngkpt(1),ngkpt(2),ngkpt(3))
37integer :: iqndx(ngkpt(1),ngkpt(2),ngkpt(3))
38integer :: isymndx(ngkpt(1),ngkpt(2),ngkpt(3)),isymndxq(ngkpt(1),ngkpt(2),ngkpt(3))
39integer :: nband(nkpt*nsppol),nbandq(nkptq*nsppol)
40double complex :: sei,seipw,seiold,seibc,csepw,cseold,csebc
41double complex :: xse1,xse2
42double complex :: cse(nbcore+1:ncband),cse1(nbcore+1:ncband),cse2(nbcore+1:ncband)
43double complex :: csepp(nbcore+1:ncband),cse1pp(nbcore+1:ncband)
44double complex :: csecorr(nbcore+1:ncband),cse1corr(nbcore+1:ncband)
45double complex :: zz(nbcore+1:ncband),zz1(nbcore+1:ncband),zz2(nbcore+1:ncband)
46double complex :: zzpp(nbcore+1:ncband),zz1pp(nbcore+1:ncband)
47double complex :: zzcorr(nbcore+1:ncband),zz1corr(nbcore+1:ncband)
48double precision :: xse
49integer :: ii,jj,kk,ix,iy,iz,iqq(3),iqqp(3),jka(3),jkb(3),jkk(3),iskip,isign,iloss,insd
50integer :: ikpt,ikptq,iks(3),ikslf1(3),ikslf2(3)
51integer :: pwupmin(3),pwupmax(3)
52integer :: igg(3),igpw(3),igh(3),igg0(3),icenter,isym,isymq,ibp,iqpt,iqsym,iw,ie,je,ies
53double precision :: xck(3),xckq(3),qadj(6)
54double complex :: cmatel,cmatel2,amatel,amatel2,vqmat2(ngkpt(1),ngkpt(2),ngkpt(3))
55double complex :: vfactor(ngkpt(1),ngkpt(2),ngkpt(3)),vfv(8),lossv(8)
56double precision :: vq2(8)
57double precision :: omega(ngkpt(1),ngkpt(2),ngkpt(3))
58double precision :: whi,wlo,wwhi,wwlo,ww,enval(8),dw,eshift,wwq
59double precision :: abr,rlr
60double complex :: tseincr,tseincrx,dse,dcse
61integer :: iwh,iwl,ibmin,ibmax
62double precision :: vq(ngkpt(1),ngkpt(2),ngkpt(3)),qq(3),qp(3),qq2,qp2,qs(3),xk(3),xkmq(3),ek,ekmq,ekpw,xpw(3)
63double precision :: stvec(ngkpt(3)),stvec2(ngkpt(3))
64integer :: npwup,iipw,jjpw
65integer :: iqsymndx(ngkpt(1),ngkpt(2),ngkpt(3)),invpw2ndx(npwup,npwup),invpwndx(pwupmin(1):pwupmax(1),pwupmin(2):pwupmax(2),pwupmin(3):pwupmax(3))
66integer :: pwsymndx(npwup,2*nsym)
67integer :: ipw2,jw
68double precision :: fsum(npwup,nqpt)
69double precision :: eps1(nbcore+1:ncband),eps2,eps
70double precision :: eval,brd,esprd
71double precision :: gfo,gamma(3),pln(3),dist(3)
72double complex :: wint(nbcore+1:ncband),wint0(nbcore+1:ncband)
73double complex :: w2int(nbcore+1:ncband),w2int0(nbcore+1:ncband)
74double complex :: wppint(nbcore+1:ncband),wppint0(nbcore+1:ncband)
75double complex :: wpp2int(nbcore+1:ncband),wpp2int0(nbcore+1:ncband)
76double complex :: cmgamma2,gterm
77double complex :: pwmatel(nlmn,nlmn,npwup,ngkpt(1),ngkpt(2),ngkpt(3)), &
78&                tpwmatel(nlmn,nlmn,npwup,ngkpt(1),ngkpt(2),ngkpt(3))
79integer :: iqsing
80integer :: idum
81logical :: lx,lc,lcorr,lqsing
82double precision :: xdum
83double precision :: linterp
84external linterp
85
86abr=1.d-6
87rlr=1.d-6
88sei=0.d0
89xse=0.d0
90cse=(0.d0,0.d0)
91csecorr=(0.d0,0.d0)
92zz=(0.d0,0.d0)
93igg0=(/0,0,0/)
94dw=wmax/dble(nwpt)
95ikpt=ikndx(ikk(1),ikk(2),ikk(3))
96isym=isymndx(ikk(1),ikk(2),ikk(3))
97ek=enrgy(indxkbnd(ikpt)+iband)
98
99gamma=(blat(1,:)+blat(2,:)+blat(3,:))/2.d0
100do ii=1,3
101  jj=mod(ii,3)+1
102  kk=mod(ii+1,3)+1
103  pln(1)=blat(jj,2)*blat(kk,3)-blat(jj,3)*blat(kk,2)
104  pln(2)=-blat(jj,1)*blat(kk,3)+blat(jj,3)*blat(kk,1)
105  pln(3)=blat(jj,1)*blat(kk,2)-blat(jj,2)*blat(kk,1)
106  dist(ii)=dot_product(gamma,pln)/sqrt(dot_product(pln,pln))
107enddo
108gfo=minval(dist)
109
110!write(6,'(a)') '      finding contibutions from band:      plane waves:'
111do ibp=nbcore+1,ncband
112!do ibp=1,1
113  xse2=xse
114  cse2=cse
115  if (ibp.gt.nbocc) then
116    isign=-1
117  else
118    isign=1
119  endif
120  if (ibp.ge.ibmin.and.ibp.le.ibmax) then
121    iloss=1
122  else
123    iloss=0
124  endif
125  do ipw1=1,npwarr(ikpt)
126!  do ipw1=1,1
127    ipw2=ipw1
128    igpw=kg(:,indxkpw(ikpt)+ipw1)
129    if (igpw(1).ge.pwupmin(1).and.igpw(1).le.pwupmax(1).and.  &
130&       igpw(2).ge.pwupmin(2).and.igpw(2).le.pwupmax(2).and.  &
131&       igpw(3).ge.pwupmin(3).and.igpw(3).le.pwupmax(3)) then
132      ipwa=invpwndx(igpw(1),igpw(2),igpw(3))
133    else
134      ipwa=0
135    endif
136    xpw=dble(igpw)+dble(ikk)/dble(ngkpt)
137    ekpw=0.d0
138    do ii=1,3
139    do jj=1,3
140      ekpw=ekpw+xpw(ii)*bmet(ii,jj)*xpw(jj)
141    enddo
142    enddo
143    if (ekpw.gt.8.d0) cycle
144    do ix=1,ngkpt(1)
145    do iy=1,ngkpt(2)
146    do iz=1,ngkpt(3)
147!    do ix=2,2
148!    do iy=2,2
149!    do iz=4,4
150      iqq=(/ix,iy,iz/)
151!write(6,*) '>>>>> iqq = ',iqq
152!write(6,'(a,3f10.5)') 'kpt = ',kpt(:,ikpt)
153      iks=iqq-ngkpt/2
154      jka=ikk-iks
155      jkk=mod(jka,ngkpt(ii))
156      do ii=1,3
157        if (jkk(ii).le.0) jkk(ii)=jkk(ii)+ngkpt(ii)
158      enddo
159      if (iks(1).eq.0.and.iks(2).eq.0.and.iks(3).eq.0) then
160!      if (.true.) then
161        ikptq=ikndxq(jkk(1),jkk(2),jkk(3))
162        jkb=nint(kptq(:,ikptq)*dble(ngkpt))+ngkpt/2
163        qq=kpt(:,ikpt)-kptq(:,ikptq)
164!write(6,'(a,3f10.5)') 'kptq = ',kptq(:,ikptq)
165      else
166        ikptq=ikndx(jkk(1),jkk(2),jkk(3))
167        jkb=nint(kpt(:,ikptq)*dble(ngkpt))+ngkpt/2
168        qq=kpt(:,ikpt)-kpt(:,ikptq)
169!write(6,'(a,3f10.5)') 'kptq = ',kptq(:,ikptq)
170      endif
171      igg=nint(dble(jkb-jka)/dble(ngkpt))
172      qq=qq+igg+igpw
173      qq2=0.d0
174      do ii=1,3
175      do jj=1,3
176        qq2=qq2+qq(ii)*bmet(ii,jj)*qq(jj)
177      enddo
178      enddo
179      vq(ix,iy,iz)=4.d0*pi/qq2
180      wwq=sqrt(omegap2+(xkf**2/3.d0)*qq2+qq2**2/4.d0)
181!write(6,'(a,3f10.5)') 'qq = ',qq
182!write(6,'(a,3f10.5)') 'qq2 = ',qq2
183!write(6,*) 'vq = ',vq(ix,iy,iz)
184!write(6,*) 'jka = ',jka
185!write(6,*) 'jkk = ',jkk
186!write(6,*) 'jkb = ',jkb
187!write(6,*) 'igg = ',igg
188!write(6,*) 'ikpt = ',ikpt
189!write(6,*) 'ikptq = ',ikptq
190      if (iks(1).eq.0.and.iks(2).eq.0.and.iks(3).eq.0) then
191!      if (.true.) then
192        call mkmatelX(ibp,iband,ikpt,ikptq,igpw,igg, &
193&         ncg,ncgq,nkpt,nkptq,npwt,npwtq,igmx,igmn,igndx,igndxq, &
194&         isym,isymq,symrel,syminv,nsym,nsymq,ihlf,ihlfq,kpt,kptq, &
195&         lvtrans(1:3,ikk(1),ikk(2),ikk(3)),lvtransq(1:3,ikk(1),ikk(2),ikk(3)), &
196&         cg,cgq,indxkcg,indxkcgq,indxkpw,indxkpwq,npwarr,npwarrq,kg,kgq, &
197&         cmatel)
198        if (ipaw.ne.0) then
199!*** need to calculate pwmatel for all plane waves
200!*** ipw1 indexes full plane wave set, not the local field plane wave set
201!*** pwmatel is calculated for
202!          call mkmatelP(pi,xred,natom,ibp,iband,ikpt,ikptq,qq,igg0,ngkpt, &
203!&               pwmatel(:,:,ipw1,ix,iy,iz),tpwmatel(:,:,ipw1,ix,iy,iz), &
204!&               projwf,nlmn,nkpt,nkptq,ncband,amatel)
205        else
206          amatel=0.d0
207        endif
208        if (ibp.gt.nbocc) then
209          omega(ix,iy,iz)=-enrgyq(indxkbndq(ikptq)+ibp)
210        else
211          omega(ix,iy,iz)=enrgyq(indxkbndq(ikptq)+ibp)
212        endif
213      else
214        call mkmatelX(ibp,iband,ikpt,ikptq,igpw,igg, &
215&         ncg,ncg,nkpt,nkpt,npwt,npwt,igmx,igmn,igndx,igndx, &
216&         isym,isymq,symrel,syminv,nsym,nsym,ihlf,ihlf,kpt,kpt, &
217&         lvtrans(1:3,ikk(1),ikk(2),ikk(3)),lvtrans(1:3,ikk(1),ikk(2),ikk(3)), &
218&         cg,cg,indxkcg,indxkcg,indxkpw,indxkpw,npwarr,npwarr,kg,kg, &
219&         cmatel)
220        if (ipaw.ne.0) then
221!          call mkmatelP(pi,xred,natom,ibp,iband,ikpt,ikptq,qq,igg0,ngkpt, &
222!&               pwmatel(:,:,ipw1,ix,iy,iz),tpwmatel(:,:,ipw1,ix,iy,iz), &
223!&               projwf,nlmn,nkpt,nkpt,ncband,amatel)
224        else
225          amatel=0.d0
226        endif
227        if (ibp.gt.nbocc) then
228          omega(ix,iy,iz)=-enrgy(indxkbnd(ikptq)+ibp)
229        else
230          omega(ix,iy,iz)=enrgy(indxkbnd(ikptq)+ibp)
231        endif
232      endif
233      if (iband.eq.jband) then
234        cmatel2=cmatel
235        amatel2=amatel
236      elseif (iks(1).eq.0.and.iks(2).eq.0.and.iks(3).eq.0) then
237!      if (.true.) then
238        call mkmatelX(ibp,jband,ikpt,ikptq,igpw,igg, &
239&         ncg,ncgq,nkpt,nkptq,npwt,npwtq,igmx,igmn,igndx,igndxq, &
240&         isym,isymq,symrel,syminv,nsym,nsymq,ihlf,ihlfq,kpt,kptq, &
241&         lvtrans(1:3,ikk(1),ikk(2),ikk(3)),lvtransq(1:3,ikk(1),ikk(2),ikk(3)), &
242&         cg,cgq,indxkcg,indxkcgq,indxkpw,indxkpwq,npwarr,npwarrq,kg,kgq, &
243&         cmatel2)
244        if (ipaw.ne.0) then
245!*** need to calculate pwmatel for all plane waves
246!*** ipw1 indexes full plane wave set, not the local field plane wave set
247!*** pwmatel is calculated for
248!          call mkmatelP(pi,xred,natom,ibp,jband,ikpt,ikptq,qq,igg0,ngkpt, &
249!&               pwmatel(:,:,ipw1,ix,iy,iz),tpwmatel(:,:,ipw1,ix,iy,iz), &
250!&               projwf,nlmn,nkpt,nkptq,ncband,amatel2)
251        else
252          amatel2=0.d0
253        endif
254      else
255        call mkmatelX(ibp,jband,ikpt,ikptq,igpw,igg, &
256&         ncg,ncg,nkpt,nkpt,npwt,npwt,igmx,igmn,igndx,igndx, &
257&         isym,isymq,symrel,syminv,nsym,nsym,ihlf,ihlf,kpt,kpt, &
258&         lvtrans(1:3,ikk(1),ikk(2),ikk(3)),lvtrans(1:3,ikk(1),ikk(2),ikk(3)), &
259&         cg,cg,indxkcg,indxkcg,indxkpw,indxkpw,npwarr,npwarr,kg,kg, &
260&         cmatel2)
261        if (ipaw.ne.0) then
262!          call mkmatelP(pi,xred,natom,ibp,jband,ikpt,ikptq,qq,igg0,ngkpt, &
263!&               pwmatel(:,:,ipw1,ix,iy,iz),tpwmatel(:,:,ipw1,ix,iy,iz), &
264!&               projwf,nlmn,nkpt,nkpt,ncband,amatel2)
265        else
266          amatel2=0.d0
267        endif
268      endif
269      vqmat2(ix,iy,iz)=(cmatel+amatel)*conjg(cmatel2+amatel2)*vq(ix,iy,iz)
270      if (ix.eq.ngkpt(1)/2.and.iy.eq.ngkpt(2)/2.and.iz.eq.ngkpt(3)/2) then
271        cmgamma2=(cmatel+amatel)*conjg(cmatel+amatel)
272      endif
273!write(6,*) 'cmatel  = ',cmatel
274!write(6,*) 'vqmat2 = ',vqmat2(iqq(1),iqq(2),iqq(3))
275!write(56,*) '>>>>>',iqq,vqmat2(iqq(1),iqq(2),iqq(3))
276!      stvec(iqq(3))=vq(ix,iy,iz)
277!      stvec(iqq(3))=sqrt(qq2)
278!      stvec(iqq(3))=dble(vqmat2(iqq(1),iqq(2),iqq(3)))
279!      stvec2(iqq(3))=dimag(vqmat2(iqq(1),iqq(2),iqq(3)))
280!      stvec(iqq(3))=dble(cmatel)
281!      stvec2(iqq(3))=dimag(cmatel)
282!      stvec(iqq(3))=dble(cmatel*conjg(cmatel))
283    enddo
284!    write(76,'(10f8.2)') stvec(:)
285!    write(76,'(10es8.1)') stvec(:)
286!    write(76,'(10es8.1)') stvec2(:)
287!    write(76,*)
288    enddo
289!    write(76,*) iqq(1)+1,'--------------------------------------------------',ibp
290    enddo
291!stop
292!return
293! Cut material for testing energy dependence of self energy at bottom of file
294
295    lx=ibp.le.nbocc
296    lcorr=ipwa.ne.0
297    if (lx) xse1=(0.d0,0.d0)
298    do ie=nbcore+1,ncband
299      cse1(ie)=(0.d0,0.d0)
300      if (lcorr) cse1corr(ie)=(0.d0,0.d0)
301      zz1(ie)=(0.d0,0.d0)
302      eps=enrgy(indxkbnd(ikpt)+ie)
303      if (ibp.gt.nbocc) then
304        eps1(ie)=eps
305      else
306        eps1(ie)=-eps
307      endif
308    enddo
309    brd=dw
310
311
312    if (ipw1.eq.1.or.ipw2.eq.1) then
313      ix=ngkpt(1)/2
314      iy=ngkpt(2)/2
315      iz=ngkpt(3)/2
316      qadj(1)=abs(vqmat2(ix,iy,iz)/vqmat2(ix+1,iy,iz))
317      qadj(2)=abs(vqmat2(ix,iy,iz)/vqmat2(ix-1,iy,iz))
318      qadj(3)=abs(vqmat2(ix,iy,iz)/vqmat2(ix,iy+1,iz))
319      qadj(4)=abs(vqmat2(ix,iy,iz)/vqmat2(ix,iy-1,iz))
320      qadj(5)=abs(vqmat2(ix,iy,iz)/vqmat2(ix,iy,iz+1))
321      qadj(6)=abs(vqmat2(ix,iy,iz)/vqmat2(ix,iy,iz-1))
322      if (qadj(1).gt.1.d3.and.qadj(2).gt.1.d3.and.qadj(3).gt.1.d3.and.  &
323&         qadj(4).gt.1.d3.and.qadj(5).gt.1.d3.and.qadj(6).gt.1.d3) then
324        lqsing=.true.
325        iqq=(/ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2/)
326        iqpt=iqndx(iqq(1),iqq(2),iqq(3))
327!        call fespread(iqq,ngkpt,omega,esprd)
328!        brd=max(esprd,dw)
329        qq2=4.d0*pi/vq(ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2)
330        wwq=sqrt(omegap2+(xkf**2/3.d0)*qq2+qq2**2/4.d0)
331        ppfct=pi*omegap2/(2.d0*wwq)
332        if (lcorr) then
333          call locateelement(iqq,ipwa,ipwa,ngkpt,iqsymndx,npwup,nsym,pwsymndx,invpw2ndx,jjpw)
334        else
335          jjpw=0
336        endif
337        do ie=nbcore+1,ncband
338          eval=omega(iqq(1),iqq(2),iqq(3))+eps1(ie)-wwq
339          wppint0(ie)=dble(ppfct/(eval,brd))
340          wpp2int0(ie)=-dble(ppfct/(eval,brd)**2)
341          wint0(ie)=(0.d0,0.d0)
342          w2int0(ie)=(0.d0,0.d0)
343          if (jjpw.ne.0) then
344            do iw=1,nwpt
345              ww=iw*dw
346              eval=omega(iqq(1),iqq(2),iqq(3))+eps1(ie)-ww
347              wint0(ie)=wint0(ie)+dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)
348              w2int0(ie)=w2int0(ie)-dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)**2
349            enddo
350          endif
351        enddo
352      else
353        lqsing=.false.
354      endif
355    else
356      lqsing=.false.
357    endif
358
359    do ix=1,ngkpt(1)
360    do iy=1,ngkpt(2)
361    do iz=1,ngkpt(3)
362!    do ix=1,1
363!    do iy=1,1
364!    do iz=1,1
365      iqq=(/ix,iy,iz/)
366      iqpt=iqndx(ix,iy,iz)
367!      call fespread(iqq,ngkpt,omega,esprd)
368!      brd=3*max(esprd,dw)
369!write(6,'(3i3,es20.10)') ix,iy,iz,esprd*27.2114
370      qq2=4.d0*pi/vq(ix,iy,iz)
371      wwq=sqrt(omegap2+(xkf**2/3.d0)*qq2+qq2**2/4.d0)
372      ppfct=pi*omegap2/(2.d0*wwq)
373      if (lcorr) then
374        call locateelement(iqq,ipwa,ipwa,ngkpt,iqsymndx,npwup,nsym,pwsymndx,invpw2ndx,jjpw)
375      else
376        jjpw=0
377      endif
378      do ie=nbcore+1,ncband
379        eval=omega(ix,iy,iz)+eps1(ie)-wwq
380        wppint(ie)=ppfct/(eval,brd)
381        wpp2int(ie)=-ppfct/(eval,brd)**2
382        wint(ie)=(0.d0,0.d0)
383        w2int(ie)=(0.d0,0.d0)
384        if (jjpw.ne.0) then
385          do iw=1,nwpt
386            ww=iw*dw
387            eval=omega(ix,iy,iz)+eps1(ie)-ww
388            wint(ie)=wint(ie)+dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)
389            w2int(ie)=w2int(ie)-dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)**2
390          enddo
391        endif
392      enddo
393      if (.not.iqsing) then
394        if (lx) xse1=xse1+vqmat2(ix,iy,iz)
395        cse1pp=cse1pp+wppint*vqmat2(ix,iy,iz)
396        zz1pp=zz1pp+wpp2int*vqmat2(ix,iy,iz)
397        if (lcorr) then
398          cse1=cse1+wint*vqmat2(ix,iy,iz)
399          zz1=zz1+w2int*vqmat2(ix,iy,iz)
400          cse1corr=cse1corr &
401&                +wppint*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt))
402          zz1corr=zz1corr &
403&                +wpp2int*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt))
404        endif
405      else
406        if (sqrt(qq2).lt.gfo) then
407          if (ix.eq.ngkpt(1)/2.and.iy.eq.ngkpt(2)/2.and.iz.eq.ngkpt(3)/2) cycle
408          gterm=cmgamma2*(1+cos(pi*sqrt(qq2)/gfo))/(2.d0)
409          if (lx) xse1=xse1+vqmat2(ix,iy,iz)-gterm*vq(ix,iy,iz)
410          cse1pp=cse1pp+wppint*vqmat2(ix,iy,iz)-wppint0*gterm*vq(ix,iy,iz)
411          zz1pp=zz1pp+wpp2int*vqmat2(ix,iy,iz)-wpp2int0*gterm*vq(ix,iy,iz)
412          if (lcorr) then
413            cse1=cse1+wint*vqmat2(ix,iy,iz)-wint0*gterm*vq(ix,iy,iz)
414            zz1=zz1+w2int*vqmat2(ix,iy,iz)-w2int0*gterm*vq(ix,iy,iz)
415            cse1corr=cse1corr &
416&                 +wppint*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt)) &
417&                 -wppint0*gterm*vq(ix,iy,iz)*(1.d0-fsum(ipwa,1))
418            zz1corr=zz1corr &
419&                 +wpp2int*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt)) &
420&                 -wpp2int0*gterm*vq(ix,iy,iz)*(1.d0-fsum(ipwa,1))
421          endif
422        else
423          if (lx) xse1=xse1+vqmat2(ix,iy,iz)
424          cse1pp=cse1pp+wppint*vqmat2(ix,iy,iz)
425          zz1pp=zz1pp+wpp2int*vqmat2(ix,iy,iz)
426          if (lcorr) then
427            cse1=cse1+wint*vqmat2(ix,iy,iz)
428            zz1=zz1+w2int*vqmat2(ix,iy,iz)
429            cse1corr=cse1corr &
430&                 +wppint*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt))
431            zz1corr=zz1corr &
432&                 +wpp2int*vqmat2(ix,iy,iz)*(1.d0-fsum(ipwa,iqpt))
433          endif
434        endif
435      endif
436    enddo
437    enddo
438    enddo
439    if (lx) xse1=xse1/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol)
440    cse1pp=cse1pp/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
441    zz1pp=zz1pp/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
442    if (lcorr) then
443      cse1=cse1/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
444      zz1=zz1/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
445      cse1corr=cse1corr/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
446      zz1pp=zz1pp/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
447    endif
448    if (iqsing) then
449! 1/pi Integral (d^3q/(2 pi)^3) wint0 gterm vq
450      if (lx) xse1=xse1+cmgamma2*8.d0*pi*pi*gfo/((2.d0*pi)**3)
451      cse1pp=cse1pp+wppint0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)
452      zz1pp=zz1pp+wpp2int0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)
453      if (lcorr) then
454        cse1=cse1+wint0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)
455        zz1=zz1+w2int0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)
456        cse1corr=cse1corr+ &
457&         wppint0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)*(1.d0-fsum(ipwa,1))
458        zz1corr=zz1corr+ &
459&         wpp2int0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)*(1.d0-fsum(ipwa,1))
460      endif
461    endif
462    if (lx) xse1=-xse1
463    cse1pp=-cse1pp*isign
464    if (lcorr) then
465      cse1=-cse1*isign
466      zz1=-zz1*isign
467      cse1corr=-cse1corr*isign
468      zz1corr=-zz1corr*isign
469    endif
470    if (lx) xse=xse+dble(xse1)
471    csepp=csepp+cse1pp
472    if (lcorr) then
473      cse=cse+cse1
474      zz=zz+zz1
475      csecorr=csecorr+cse1corr
476      zzcorr=zzcorr+zz1corr
477    endif
478
479    omega=-isign*omega-ek+dw/2
480    lx=.false.
481    ictr=(/ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2/)
482    do ix=1,ngkpt(1)
483    do iy=1,ngkpt(2)
484    do iz=1,ngkpt(3)
485      iqq=(/ix,iy,iz/)
486      call fval(omega,iqq,iqqp,ngkpt,enval)
487      if (lqsing) call fval(vq,iqq,iqqp,ngkpt,vq2)
488      do iw=1,nwpt
489        call fpol(vfactor(:,:,:,iw),ngkpt,iqq,iqqp,vfv(:,iw))
490      enddo
491      if (lx) call fpol(vqmat2(:,:,:),ngkpt,iqq,iqqp,vfx)
492      do itet=1,6
493        lqcentr=.false.
494        do iv=1,4
495          evtx0(iv)=enval(ivndx(iv,itet))
496          rrpyr(1:3,iv)=rr(1:3,ivndx(iv,itet))
497          do iw=1,nwpt
498            vpyr0(iv,iw)=vfv(ivndx(iv,itet),iw)
499          enddo
500          if (lx) xpyr0(iv)=vfx(ivndx(iv,itet))
501          if (lqsing.and..not.lqcentr) then
502            iqv=iqq+nint(rrpyr(:,iv))
503            if (iqv(1).eq.ictr(1).and.iqv(2).eq.ictr(2).and.iqv(3).eq.ictr(3)) then
504              lqcentr=.true.
505            endif
506          endif
507        enddo
508        call indxhpsort(4,4,evtx0,indxe)
509        evtx=evtx0(indxe)    ! order vertices by energy
510        do ii=1,4
511          jj=indxe(ii)
512          do kk=1,3
513            kvtx(kk,ii)=dot_product(iqq+rrpyr(:,jj)-ictr,qkcvt(:,kk))
514          enddo
515        enddo
516        xk(1:3,1)=(/0.d0,0.d0,0.d0/)
517        do ii=2,4
518          xk(1:3,ii)=kvtx(1:3,ii)-kvtx(1:3,1)
519        enddo
520        call cross(xk(:,3),xk(:,4),vdum)
521        tvol=dot_product(vdum,xk(:,2))
522        do jj=1,3  ! make contragradient
523          call cross(xk(1:3,mod(jj,3)+2),xk(1:3,mod(jj+1,3)+2),rg(1:3,jj))
524        enddo
525        rg=rg/tvol
526        tvol=abs(tvol)
527        iwwlo=nint(evtx(1)/dw)
528        iwwhi=nint(evtx(4)/dw)
529        de21=evtx(2)-evtx(1)
530        de31=evtx(3)-evtx(1)
531        de32=evtx(3)-evtx(2)
532        de41=evtx(4)-evtx(1)
533        de42=evtx(4)-evtx(2)
534        de43=evtx(4)-evtx(3)
535        thresh=1.d-5*de41
536        if (lqcentr) then            ! integral around coulomb singularity
537          do iv=1,4
538            vqvtx0(iv)=vq2(ivndx(iv,itet))
539          enddo
540          bgrad=(/0.d0,0.d0,0.d0/)   ! energy gradient
541          do jj=1,3
542            bgrad=bgrad+(evtx(jj+1)-evtx(1))*rg(:,jj)
543          enddo
544          xmult=4.d0*pi/sqrt(dot_product(bgrad,bgrad))
545          do iw=1,nwpt
546            vpyr(:)=vpyr0(:,iw)/vqvtx0
547            aa0(iw)=vpyr(indxe(1))
548            av(1:3,iw)=(/0.d0,0.d0,0.d0/)
549            do jj=1,3
550              av(1:3,iw)=av(1:3,iw) &
551&                       +(vpyr(indxe(jj+1))-vpyr(indxe(1)))*rg(1:3,jj)
552            enddo
553          enddo
554          if (lx) then
555            xpyr=xpyr0/vqvtx0
556            xme=xpyr(indxe(1))
557            xv=(/0.d0,0.d0,0.d0/)
558            do jj=1,3
559              xv=xv+(xpyr(indxe(jj+1))-xpyr(indxe(1)))*rg(:,jj)
560            enddo
561          endif
562          do iww=iwwlo,iwwhi
563            www=dw*iww
564            if (www.lt.evtx(1)) then
565              cycle
566            elseif (www.lt.evtx(2)) then
567              call singint(1,www,xk,kvtx(:,1),evtx,sint1,svec)
568            elseif (www.lt.evtx(3)) then
569              if (de21.lt.thresh.and.de43.lt.thresh) then
570                call singint2(www,xk,kvtx(:,1),evtx,sint1a,sveca)
571              elseif (de21.ge.de43) then
572                call singint(2,www,xk,kvtx(:,1),evtx,sint1a,sveca)
573                call singint(1,www,xk,kvtx(:,1),evtx,sint1b,svecb)
574              else
575                call singint(3,www,xk,kvtx(:,1),evtx,sint1a,sveca)
576                call singint(4,www,xk,kvtx(:,1),evtx,sint1b,svecb)
577              endif
578              sint1=sint1b-sint1a
579              svec=svecb-sveca
580            elseif (www.lt.evtx(4)) then
581              call singint(4,www,xk,kvtx(:,1),evtx,sint1,svec)
582            else
583              cycle
584            endif
585            sint1=sint1*xmult
586            svec=svec*xmult
587            do iw=1,nwpt
588              ie=iww-isign*iw
589              ssi(ie)=ssi(ie)+(aa0(iw)*sint1+dot_product(av(:,iw),svec))*dw
590            enddo
591            if (lx) ssx=ssx+(xme*sint1+dot_product(xv,svec))*dw
592          enddo
593        else                         ! non-singular, tetrahedron method
594          fbx(1)=tvol/(2.d0*de21*de31*de41)
595          if (de21.ge.de43) then
596            fbx(2)=tvol/(2.d0*de21*de32*de42)
597          else
598            fbx(3)=tvol/(2.d0*de31*de32*de43)
599          endif
600          fbx(4)=tvol/(2.d0*de41*de42*de43)
601          do ii=1,4
602            cmx(1:3,ii)=(/0.d0,0.d0,0.d0/)
603            do jj=1,4
604              if (jj.ne.ii) cmx(1:3,ii)=cmx(1:3,ii)+(xk(1:3,jj)-xk(1:3,ii)) &
605&                                                  /(3*(evtx(jj)-evtx(ii)))
606            enddo
607          enddo
608          do iw=1,nwpt
609            aa0(iw)=vpyr0(indxe(1),iw)   ! base value of function
610            av(1:3,iw)=(/0.d0,0.d0,0.d0/) ! gradient of function
611            do jj=1,3
612              av(1:3,iw)=av(1:3,iw) &
613&                       +(vpyr0(indxe(jj+1),iw)-vpyr0(indxe(1),iw))*rg(1:3,jj)
614            enddo
615          enddo
616          if (lx) then
617            xme=xpyr0(indxe(1))
618            xv=(/0.d0,0.d0,0.d0/)
619            do jj=1,3
620              xv=xv+(xpyr0(indxe(jj+1))-xpyr0(indxe(1)))*rg(:,jj)
621            enddo
622          endif
623          do iww=iwwlo,iwwhi
624!          do iww=2,2
625!write(6,*) iww
626            www=dw*iww
627            if (www.lt.evtx(1)) then
628              cycle
629            elseif (www.lt.evtx(2)) then
630              sint1=fbx(1)*(www-evtx(1))**2
631              svec=(xk(1:3,1)+(www-evtx(1))*cmx(1:3,1))*sint1
632            elseif (www.lt.evtx(3)) then
633              if (de21.lt.thresh.and.de43.lt.thresh) then
634                xkt(:,1)=xk(:,3)*(www-evtx(1))/de31
635                xkt(:,2)=xk(:,4)*(www-evtx(1))/de41
636                xkt(:,3)=(xk(:,4)-xk(:,2))*(www-evtx(2))/de42+xk(:,2)
637                sint1=tvol*(2*(www-evtx(1))/(de31*de41) &
638&                     -(www-evtx(1))**2*(de31+de41)/(de31*de41)**2)/2
639                svec=((xkt(:,1)+xkt(:,3))/2.d0)*sint1
640              elseif (de21.ge.de43) then
641                fb(1)=fbx(1)*(www-evtx(1))**2
642                fb(2)=fbx(2)*(www-evtx(2))**2
643                sint1=fb(1)-fb(2)
644                cm(1:3,1)=xk(1:3,1)+(www-evtx(1))*cmx(1:3,1)
645                cm(1:3,2)=xk(1:3,2)+(www-evtx(2))*cmx(1:3,2)
646                svec=cm(:,1)*fb(1)-cm(:,2)*fb(2)
647              else
648                fb(3)=fbx(3)*(www-evtx(3))**2
649                fb(4)=fbx(4)*(www-evtx(4))**2
650                sint1=fb(4)-fb(3)
651                cm(1:3,3)=xk(1:3,3)+(www-evtx(3))*cmx(1:3,3)
652                cm(1:3,4)=xk(1:3,4)+(www-evtx(4))*cmx(1:3,4)
653                svec=cm(:,4)*fb(4)-cm(:,3)*fb(3)
654              endif
655            elseif (www.lt.evtx(4)) then
656              sint1=fbx(4)*(www-evtx(4))**2
657              svec=(xk(1:3,4)+(www-evtx(4))*cmx(1:3,4))*sint1
658            else
659              cycle
660            endif
661            do iw=1,nwpt
662              ie=iww-isign*iw
663              ssi(ie)=ssi(ie)+(aa0(iw)*sint1+dot_product(av(:,iw),svec))*dw
664            enddo
665            if (lx) ssx=ssx+(xme*sint1+dot_product(xv,svec))*dw
666          enddo
667        endif
668      enddo
669    enddo
670    enddo
671    enddo
672    ssi=-isign*ssi/((2*pi)**3*pi)
673    if (lx) ssx=-ssx/((2*pi)**3)
674
675
676
677
678!if (lx) then
679!  write(33,'(i4,4x,i5,3x,3i3,5x,"(",f10.6,",",f10.6,")",5x,f10.6)') &
680!&        ibp,ipw1,kg(ii,indxkpw(ikpt)+ipw1),cse1*27.2114,dble(xse1)*27.2114
681!else
682!  write(33,'(i4,4x,i5,3x,3i3,5x,"(",f10.6,",",f10.6,")")') &
683!&        ibp,ipw1,kg(ii,indxkpw(ikpt)+ipw1),cse1*27.2114
684!endif
685  enddo
686!  if (lx) then
687!    write(6,'(i4,5x,"(",f10.6,",",f10.6,")",5x,f10.6)') ibp,(cse(4)-cse2(4))*27.2114,dble(xse-xse2)*27.2114
688!  else
689!    write(6,'(i4,5x,"(",f10.6,",",f10.6,")",f10.6)') ibp,(cse(4)-cse2(4))*27.2114
690!  endif
691enddo
692
693!zz=1.d0/(1.d0-zz)
694!zzpp=1.d0/(1.d0-zzpp)
695!zzcorr=1.d0/(1.d0-zz-zzcorr)
696!write(6,*) cse*27.2114
697!write(6,*) csecorr*27.2114
698!write(6,*) xse*27.2114
699!write(6,*) zz
700
701
702return
703end subroutine mkppse
704
705!**************************************************************************
706
707subroutine tebound(iqq,ngkpt,omega,insd)
708implicit none
709integer :: iqq(3),ngkpt(3),insd
710double precision :: omega(ngkpt(1),ngkpt(2),ngkpt(3))
711integer :: ix,iy,iz,iqp(3)
712double precision :: e1,e0
713
714insd=0
715e0=omega(iqq(1),iqq(2),iqq(3))
716do ix=0,1
717do iy=0,1
718do iz=0,1
719  iqp=mod(iqq+(/ix,iy,iz/)-1+ngkpt,ngkpt)+1
720  e1=omega(iqp(1),iqp(2),iqp(3))
721  if (e0*e1.lt.0.d0) insd=1
722enddo
723enddo
724enddo
725
726return
727end subroutine tebound
728
729!**************************************************************************
730! cut material from mkcse for testing energy dependence of self energy
731
732!    do ix=1,ngkpt(1)
733!    do iy=1,ngkpt(2)
734!    do iz=1,ngkpt(3)
735!      iqq=(/ix,iy,iz/)
736!      call fhilo(omega,iqq,ngkpt,whi,wlo)
737!      if (ix.eq.1.and.iy.eq.1.and.iz.eq.1) then
738!        wwhi=whi
739!        wwlo=wlo
740!      else
741!        wwhi=max(wwhi,whi)
742!        wwlo=min(wwlo,wlo)
743!      endif
744!    enddo
745!    enddo
746!    enddo
747!    if (ibp.gt.nbocc) then
748!      ioff=nint(-wwlo*dble(nwpt)/wmax)
749!    else
750!      ioff=nint(wwhi*dble(nwpt)/wmax)-nwpt
751!    endif
752!    ioff2=nint(ek*dble(nwpt)/wmax)
753!    do ie=1,nwpt
754!      eps=(ie+ioff-ioff2)*dw+ek+dw/2.d0
755!      ssi(ie)=(0.d0,0.d0)
756!!IF (.TRUE.) THEN
757!IF (.FALSE.) THEN
758!      if (ibp.gt.nbocc) then
759!        iwh=nint((eps+wwhi)*dble(nwpt)/wmax)
760!        iwl=nint((eps+wwlo)*dble(nwpt)/wmax)
761!      else
762!        iwh=nint((wwhi-eps)*dble(nwpt)/wmax)
763!        iwl=nint((wwlo-eps)*dble(nwpt)/wmax)
764!      endif
765!      do iw=max(iwl,1),min(iwh,nwpt)
766!        if (ibp.gt.nbocc) then
767!          ww=iw*dw-eps
768!        else
769!          ww=iw*dw+eps
770!        endif
771!!        write(6,*) ie,iw,ww*27.2114
772!        do ix=1,ngkpt(1)
773!        do iy=1,ngkpt(2)
774!        do iz=1,ngkpt(3)
775!          iqq=(/ix,iy,iz/)
776!          iqpt=iqndx(iqq(1),iqq(2),iqq(3))
777!          call locateelement(iqq,ipw1,ipw2,ngkpt,iqsymndx,npwup,nsym,pwsymndx,invpw2ndx,jjpw)
778!          if (jjpw.ne.0) then
779!            vfactor(iqq(1),iqq(2),iqq(3))=vqmat2(iqq(1),iqq(2),iqq(3))*dimag(lossfn(iw,iqpt,jjpw))
780!          else
781!            vfactor(iqq(1),iqq(2),iqq(3))=(0.d0,0.d0)
782!          endif
783!!          stvec(iqq(3))=dble(vfactor(iqq(1),iqq(2),iqq(3)))
784!!          stvec2(iqq(3))=dimag(vfactor(iqq(1),iqq(2),iqq(3)))
785!!          stvec(iqq(3))=dimag(lossfn(iw,iqpt,jjpw))
786!!          stvec(iqq(3))=dble(iqpt)
787!!          stvec2(iqq(3))=dble(jjpw)
788!        enddo
789!!        write(77,'(10es8.1)') stvec(:)
790!!        write(77,'(10es8.1)') stvec2(:)
791!!        write(77,'(10i8)') nint(stvec(:))
792!!        write(77,'(10i8)') nint(stvec2(:))
793!!        write(77,*)
794!        enddo
795!!        write(77,*) iqq(1)+1,'--------------------------------------------------',ibp
796!        enddo
797!        seiold=sei
798!        do ix=1,ngkpt(1)
799!        do iy=1,ngkpt(2)
800!        do iz=1,ngkpt(3)
801!          iqq=(/ix,iy,iz/)
802!          call fval(omega,iqq,iqqp,ngkpt,enval)
803!          call fpol(vfactor,ngkpt,iqq,iqqp,vfv)
804!!          write(42,*) '>>>>',iqq,ww
805!!          write(42,*)
806!!          write(42,'(2es12.3,2(4x,2es12.3))') enval(1:2),dble(vfv(1:2)),dimag(vfv(1:2))
807!!          write(42,'(2es12.3,2(4x,2es12.3))') enval(3:4),dble(vfv(3:4)),dimag(vfv(3:4))
808!!          write(42,*)
809!!          write(42,'(2es12.3,2(4x,2es12.3))') enval(5:6),dble(vfv(5:6)),dimag(vfv(5:6))
810!!          write(42,'(2es12.3,2(4x,2es12.3))') enval(7:8),dble(vfv(7:8)),dimag(vfv(7:8))
811!!          write(42,*)
812!          call cubeint(enval,ww,vfv,tseincr)
813!!          write(42,*) tseincr
814!          call fcenter(iqq,ngkpt,icenter)
815!          if (abs(tseincr).ne.0.d0.and.icenter.ne.0 &
816!&   .and.(ipw1.eq.1.or.ipw2.eq.1)) then
817!            rlr=1.d-2
818!            abr=1.d-2
819!!            write(42,*) '>>>>',iqq,ww
820!!            write(6,*) tseincr
821!            call fvq2(iqq,shiftk,shiftkq,bmet,ngkpt,ipw1,ipw2,ipwlf,npwlf,vq2)
822!!            write(42,*)
823!!            write(42,'(2es12.3,5x,2es12.3)') vq2(1:2),dble(vfv(1:2))
824!!            write(42,'(2es12.3,5x,2es12.3)') vq2(3:4),dble(vfv(3:4))
825!!            write(42,*)
826!!            write(42,'(2es12.3,5x,2es12.3)') vq2(5:6),dble(vfv(5:6))
827!!            write(42,'(2es12.3,5x,2es12.3)') vq2(7:8),dble(vfv(7:8))
828!!            write(42,*)
829!            if (ipw1.ge.ipw2) then
830!              lossv=vfv*vq2/(4.d0*pi)
831!              call subint2(iqq,shiftk,shiftkq,lossv,enval,ww,iw,bmet,iqndx,ngkpt,nwpt,nqpt,ikndxq,vol,ipw1,ipw2,ipwlf,npwlf,pi,rlr,abr,tseincr)
832!            else
833!              lossv=-conjg(vfv)*vq2/(4.d0*pi)
834!              tseincrx=-conjg(tseincr)
835!              call subint2(iqq,shiftk,shiftkq,lossv,enval,ww,iw,bmet,iqndx,ngkpt,nwpt,nqpt,ikndxq,vol,ipw1,ipw2,ipwlf,npwlf,pi,rlr,abr,tseincrx)
836!              tseincr=-conjg(tseincrx)
837!            endif
838!!            write(6,*) tseincr
839!          endif
840!!          write(42,*) tseincr
841!          dse=tseincr/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol)
842!          ssi(ie)=ssi(ie)+dse*dw*isign/pi
843!!          write(66,'(3i4,5x,es10.3)') iqq,dse
844!!          write(66,'(3i4,5x,f20.6)') iqq,dble(dse)*27.2114
845!!          write(66,'(3i4,a)') iqq,'-----------------------------------------------'
846!!          write(66,'(es10.3,5x,2es10.3)') ww,dse
847!!          write(66,*)
848!!          write(66,'(2es10.3,5x,2es10.3)') dble(vfv(1)),dble(vfv(2)),enval(1:2)-ww
849!!          write(66,'(2es10.3,5x,2es10.3)') dble(vfv(3)),dble(vfv(4)),enval(3:4)-ww
850!!          write(66,*)
851!!          write(66,'(2es10.3,5x,2es10.3)') dble(vfv(5)),dble(vfv(6)),enval(5:6)-ww
852!!          write(66,'(2es10.3,5x,2es10.3)') dble(vfv(7)),dble(vfv(8)),enval(7:8)-ww
853!!          write(42,*) ssi(iw)
854!!          stvec(iqq(3))=dble(tseincr)
855!!          stvec2(iqq(3))=dimag(tseincr)
856!!          stvec(iqq(3))=dble(icenter)
857!        enddo
858!!        write(78,'(10es8.1)') stvec(:)
859!!        write(78,'(10es8.1)') stvec2(:)
860!!        write(78,*)
861!!        write(78,'(10i4)') nint(stvec(:))
862!        enddo
863!!        write(78,*) iqq(1)+1,'--------------------------------------------------',ibp
864!        enddo
865!      enddo
866!ELSE
867!      brd=dw
868!      if (ibp.gt.nbocc) then
869!        eps1=eps
870!      else
871!        eps1=-eps
872!      endif
873!      if (abs(vqmat2(ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2)/vqmat2(ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2-1)).gt.1.d3 &
874!&     .and.(ipw1.eq.1.or.ipw2.eq.1)) then
875!        iqsing=1
876!        iqpt=iqndx(ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2)
877!        call locateelement(iqq,ipw1,ipw2,ngkpt,iqsymndx,npwup,nsym,pwsymndx,invpw2ndx,jjpw)
878!        wint0=(0.d0,0.d0)
879!        if (jjpw.ne.0) then
880!          do iw=1,nwpt
881!            ww=iw*dw
882!            eval=omega(ngkpt(1)/2,ngkpt(2)/2,ngkpt(3)/2)+eps1-ww
883!            wint0=wint0+dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)
884!          enddo
885!        endif
886!!        ssi(ie)=ssi(ie)+wint0*cmgamma2*gfo/pi
887!!        ssi(ie)=ssi(ie)+wint0*cmgamma2*4.d0*pi*gfo**3*(1.d0/6.d0-1.d0/(pi**2))
888!      else
889!        iqsing=0
890!      endif
891!      do ix=1,ngkpt(1)
892!      do iy=1,ngkpt(2)
893!      do iz=1,ngkpt(3)
894!        iqq=(/ix,iy,iz/)
895!        iqpt=iqndx(ix,iy,iz)
896!        wint=(0.d0,0.d0)
897!        call locateelement(iqq,ipw1,ipw2,ngkpt,iqsymndx,npwup,nsym,pwsymndx,invpw2ndx,jjpw)
898!        if (jjpw.eq.0) cycle
899!        do iw=1,nwpt
900!          ww=iw*dw
901!          eval=omega(ix,iy,iz)+eps1-ww
902!          wint=wint+dw*dimag(lossfn(iw,iqpt,jjpw))/(eval,brd)
903!        enddo
904!        if (iqsing.eq.0) then
905!          ssi(ie)=ssi(ie)+wint*vqmat2(ix,iy,iz)
906!        else
907!          qq2=4.d0*pi/vq(ix,iy,iz)
908!          if (sqrt(qq2).lt.gfo) then
909!            gterm=cmgamma2*(1+cos(pi*sqrt(qq2)/gfo))/(2.d0)
910!            ssi(ie)=ssi(ie)+(wint*vqmat2(ix,iy,iz)/vq(ix,iy,iz)-wint0*gterm)*vq(ix,iy,iz)
911!          else
912!            gterm=0.d0
913!            ssi(ie)=ssi(ie)+wint*vqmat2(ix,iy,iz)
914!          endif
915!        endif
916!      enddo
917!      enddo
918!      enddo
919!      ssi(ie)=ssi(ie)/(dble(ngkpt(1)*ngkpt(2)*ngkpt(3))*vol*pi)
920!      if (iqsing.eq.1) then
921!! 1/pi Integral (d^3q/(2 pi)^3) wint0 gterm vq
922!        ssi(ie)=ssi(ie)+wint0*cmgamma2*8.d0*pi*pi*gfo/(pi*(2.d0*pi)**3)
923!      endif
924!ENDIF
925!    enddo
926!
927!    do ie=1,nwpt
928!!      eps1=ek+dble(ie-nwpt/2)*dw+dw/2.d0
929!!      ssc(ie)=(0.d0,0.d0)
930!!      do je=1,nwpt
931!!        if (ie.eq.(je+ioff-ioff2+nwpt/2)) then
932!!          ssc(ie)=ssc(ie)-(0.d0,pi)*ssi(je)
933!!        else
934!!          eps2=dble(je+ioff-ioff2)*dw+ek+dw/2.d0
935!!          ssc(ie)=ssc(ie)+ssi(je)*dw/(eps2-eps1)
936!!        endif
937!!write(6,'(2i4,2f10.3,es12.3,2(2x,2es11.3))') ie,je,eps1*27.2114,eps2*27.2114,1/(eps2-eps1),ssc(ie)*27.2114,dble(ssi(je)*27.2114)
938!!      enddo
939!!      write(69,'(i4,f10.3,2(3x,2es12.3))') ie,eps1*27.2114,ssc(ie)*27.2114,ssi(ie)*27.2114
940!      sec(ie)=sec(ie)+ssi(ie)
941!    enddo
942