1! 2! Copyright (C) 2001-2013 Quantum ESPRESSO group 3! This file is distributed under the terms of the 4! GNU General Public License. See the file `License' 5! in the root directory of the present distribution, 6! or http://www.gnu.org/copyleft/gpl.txt . 7! 8! 9! 10!----------------------------------------------------------------------- 11subroutine solve_head 12 !----------------------------------------------------------------------- 13 ! 14 !calculates the head and wings of the dielectric matrix 15 ! 16 USE ions_base, ONLY : nat 17 USE io_global, ONLY : stdout, ionode,ionode_id 18 USE io_files, ONLY : diropn,prefix, tmp_dir 19 use pwcom 20 USE cell_base, ONLY : omega, tpiba2 21 USE wavefunctions, ONLY : evc 22 USE kinds, ONLY : DP 23 USE becmod, ONLY : becp,calbec 24 USE uspp_param, ONLY : nhm 25 use phcom 26 use units_lr, ONLY : iuwfc, lrwfc 27 USE wannier_gw, ONLY : n_gauss, omega_gauss, grid_type,& 28 nsteps_lanczos,second_grid_n,second_grid_i,& 29 l_scissor,scissor,len_head_block_freq, & 30 len_head_block_wfc 31 USE control_ph, ONLY : tr2_ph 32 USE gvect, ONLY : ngm, ngm_g, ig_l2g, gstart, g 33 USE gvecs, ONLY : doublegrid 34 USE mp, ONLY : mp_sum, mp_barrier, mp_bcast 35 USE mp_world, ONLY : world_comm, mpime, nproc 36 USE uspp, ONLY : nkb, vkb 37! USE symme, ONLY: s 38 USE mp_pools, ONLY : inter_pool_comm, intra_pool_comm 39 USE symme, only : crys_to_cart, symmatrix 40 USE mp_wave, ONLY : mergewf,splitwf 41 USE fft_base, ONLY : dfftp, dffts 42 USE fft_interfaces, ONLY : fwfft, invfft, fft_interpolate 43 USE buffers, ONLY : get_buffer 44 USE constants, ONLY : rytoev, fpi 45 46 use qpoint, ONLY : npwq, nksq 47 use control_lr, ONLY : nbnd_occ, lgamma 48 49 implicit none 50 51 INTEGER, EXTERNAL :: find_free_unit 52 53 real(DP) :: thresh, anorm, averlt, dr2 54 ! thresh: convergence threshold 55 ! anorm : the norm of the error 56 ! averlt: average number of iterations 57 ! dr2 : self-consistency error 58 59 60 61 62 complex(DP) , allocatable :: ps (:,:) 63 64 logical :: conv_root, exst 65 ! conv_root: true if linear system is converged 66 67 integer :: kter, iter0, ipol,jpol, ibnd, jbnd, iter, lter, & 68 ik, ig, irr, ir, is, nrec, ios 69 ! counters 70 integer :: ltaver, lintercall 71 72 real(DP) :: tcpu, get_clock 73 ! timing variables 74 75 REAL(kind=DP), ALLOCATABLE :: head(:,:),head_tmp(:) 76 COMPLEX(kind=DP) :: sca, sca2 77 REAL(kind=DP), ALLOCATABLE :: x(:),w(:), freqs(:) 78 ! COMPLEX(kind=DP), ALLOCATABLE :: e_head(:,:)!wing of symmetric dielectric matrix (for G of local processor) 79 COMPLEX(kind=DP), ALLOCATABLE :: e_head_g(:),e_head_g_tmp(:,:,:) 80 COMPLEX(kind=DP), ALLOCATABLE :: e_head_pol(:,:,:) 81 INTEGER :: i, j,k,iun 82 REAL(kind=DP) :: ww, weight 83 COMPLEX(kind=DP), ALLOCATABLE :: tmp_g(:) 84 COMPLEX(kind=DP), ALLOCATABLE :: psi_v(:,:), prod(:) 85 COMPLEX(kind=DP), ALLOCATABLE :: pola_charge(:,:,:,:) 86 COMPLEX(kind=DP), ALLOCATABLE :: dpsi_ipol(:,:,:) 87 REAL(kind=DP), ALLOCATABLE :: epsilon_g(:,:,:) 88 INTEGER :: i_start,idumm,idumm1,idumm2,idumm3,ii 89 REAL(kind=DP) :: rdumm 90 COMPLEX(kind=DP), ALLOCATABLE :: d(:,:),f(:,:),omat(:,:,:) 91 INTEGER :: iv, info 92 COMPLEX(kind=DP), ALLOCATABLE :: z_dl(:),z_d(:),z_du(:),z_b(:) 93 COMPLEX(kind=DP) :: csca, csca1 94 COMPLEX(kind=DP), ALLOCATABLE :: t_out(:,:,:), psi_tmp(:) 95 INTEGER :: n 96 INTEGER :: npwx_g 97 98 INTEGER :: ib,lenb,first_b,last_b,n_block,nbnd_block 99 100 INTEGER :: freq_block, m_block,first_f,last_f,lenf,im 101 102 103 write(stdout,*) 'Routine solve_head' 104 FLUSH(stdout) 105 106 if(grid_type==5) then 107 n=n_gauss 108 n_gauss=n+second_grid_n*(1+second_grid_i*2) 109 endif 110 111 !allocate(e_head(npw,n_gauss+1)) 112 !allocate(e_head_pol(ngm,n_gauss+1,3)) 113 !e_head(:,:) =(0.d0,0.d0) 114 allocate(x(2*n_gauss+1),w(2*n_gauss+1), freqs(n_gauss+1)) 115 allocate(head(n_gauss+1,3),head_tmp(n_gauss+1)) 116 head(:,:)=0.d0 117 allocate(prod(dfftp%nnr)) 118 allocate (tmp_g(ngm)) 119 ! allocate( pola_charge(dfftp%nnr,nspin,3,n_gauss+1)) 120 allocate(epsilon_g(3,3,n_gauss+1)) 121 allocate(psi_tmp(npwx)) 122 123 124 125 126 epsilon_g(:,:,:)=0.d0 127! e_head_pol(:,:,:)=0.d0 128! pola_charge(:,:,:,:)=0.d0 129 130 131!setup Gauss Legendre frequency grid 132!IT'S OF CAPITAL IMPORTANCE TO NULLIFY THE FOLLOWING ARRAYS 133 x(:)=0.d0 134 w(:)=0.d0 135 if(grid_type==0) then 136 call legzo(n_gauss*2+1,x,w) 137 freqs(1:n_gauss+1)=-x(n_gauss+1:2*n_gauss+1)*omega_gauss 138 else if(grid_type==2) then 139 call legzo(n_gauss,x,w) 140 freqs(1) = 0.d0 141 freqs(2:n_gauss+1)=(1.d0-x(1:n_gauss))*omega_gauss/2.d0 142 else if(grid_type==3) then!equally spaced grid 143 freqs(1) = 0.d0 144 do i=1,n_gauss 145 freqs(1+i)=omega_gauss*dble(i)/dble(n_gauss) 146 enddo 147 else if(grid_type==4) then!equally spaced grid shifted of 1/2 148 freqs(1) = 0.d0 149 do i=1,n_gauss 150 freqs(i+1)=(omega_gauss/dble(n_gauss))*dble(i)-(0.5d0*omega_gauss/dble(n_gauss)) 151 enddo 152 else!equally spaced grid more dense at -1 , 0 and 1 153 freqs(1)=0.d0 154 155 ii=2 156 do i=1,second_grid_n 157 freqs(ii)=(omega_gauss/dble(2*second_grid_n*n))*dble(i)-0.5d0*omega_gauss/dble(2*second_grid_n*n) 158 ii=ii+1 159 enddo 160 do j=1,second_grid_i 161 do i=1,second_grid_n 162 freqs(ii)=(omega_gauss/dble(2*second_grid_n*n))*dble(i+second_grid_n+2*second_grid_n*(j-1))& 163 &-0.5d0*omega_gauss/dble(2*second_grid_n*n) 164 ii=ii+1 165 enddo 166 freqs(ii)=omega_gauss/dble(n)*dble(j) 167 ii=ii+1 168 do i=1,second_grid_n 169 freqs(ii)=(omega_gauss/dble(2*second_grid_n*n))*dble(i+2*second_grid_n*j)& 170 &-0.5d0*omega_gauss/dble(2*second_grid_n*n) 171 ii=ii+1 172 enddo 173 enddo 174 do i=second_grid_i+1,n 175 freqs(ii)=omega_gauss/dble(n)*dble(i) 176 ii=ii+1 177 enddo 178 179 180 181 endif 182 do i=1,n_gauss+1 183 write(stdout,*) 'Freq',i,freqs(i) 184 enddo 185 FLUSH( stdout ) 186 187 deallocate(x,w) 188 head(:,:)=0.d0 189 190 191 192 193 194 !if (lsda) call errore ('solve_head', ' LSDA not implemented', 1) 195 196 call start_clock ('solve_head') 197 198 199 200 allocate (ps (nbnd,nbnd)) 201 ps (:,:) = (0.d0, 0.d0) 202 203 204 205 IF (ionode .AND. fildrho /= ' ') THEN 206 INQUIRE (UNIT = iudrho, OPENED = exst) 207 IF (exst) CLOSE (UNIT = iudrho, STATUS='keep') 208 CALL DIROPN (iudrho, TRIM(fildrho)//'.E', lrdrho, exst) 209 end if 210 ! 211 212 ! 213 ! if q=0 for a metal: allocate and compute local DOS at Ef 214 ! 215 if (degauss.ne.0.d0.or..not.lgamma) call errore ('solve_e', & 216 'called in the wrong case', 1) 217 ! 218 219 ! 220 ! only one iteration is required 221 ! 222 223 if(.not.l_scissor) scissor=0.d0 224 225!loop on frequency blocks 226 if(len_head_block_freq==0) then 227 freq_block=n_gauss+1 228 m_block=1 229 else 230 m_block=(n_gauss+1)/len_head_block_freq 231 if(m_block*len_head_block_freq < (n_gauss+1)) m_block=m_block+1 232 if(len_head_block_freq>(n_gauss+1)) then 233 freq_block=n_gauss+1 234 else 235 freq_block=len_head_block_freq 236 endif 237 endif 238 239!loop on frequency blocks 240 do im=1,m_block 241 242 epsilon_g=0.d0 243 244 write(stdout,*) 'FREQUENCY BLOCK : ', im 245 246 first_f=(im-1)*freq_block+1 247 last_f=im*freq_block 248 if(last_f>(n_gauss+1)) last_f=n_gauss+1 249 lenf=last_f-first_f+1 250 251 allocate( pola_charge(dfftp%nnr,nspin,3,lenf)) 252 allocate(e_head_pol(ngm,lenf,3)) 253 254 e_head_pol(:,:,:)=0.d0 255 pola_charge(:,:,:,:)=0.d0 256 257 258 259!loop on k points 260 do ik=1, nksq 261 if(len_head_block_wfc==0) then 262 nbnd_block=nbnd_occ(ik) 263 n_block=1 264 else 265 n_block=nbnd_occ(ik)/len_head_block_wfc 266 if(n_block*len_head_block_wfc < nbnd_occ(ik)) n_block=n_block+1 267 if(len_head_block_wfc>nbnd_occ(ik)) then 268 nbnd_block=nbnd_occ(ik) 269 else 270 nbnd_block=len_head_block_wfc 271 endif 272 endif 273 274 275 276 write(stdout,*) 'ik:', ik 277 FLUSH(stdout) 278 weight = wk (ik) 279 ww = fpi * weight / omega 280 281 if (lsda) current_spin = isk (ik) 282 npw = ngk(ik) 283 ! 284 ! reads unperturbed wavefuctions psi_k in G_space, for all bands 285 ! 286 if (nksq.gt.1) call get_buffer(evc, lrwfc, iuwfc, ik) 287 npwq = npw 288 call init_us_2 (npw, igk_k(1,ik), xk (1, ik), vkb) 289 290 291 292 ! 293 ! compute the kinetic energy 294 ! 295 do ig = 1, npwq 296 g2kin (ig) = ( (xk (1,ik ) + g (1,igk_k (ig,ik)) ) **2 + & 297 (xk (2,ik ) + g (2,igk_k (ig,ik)) ) **2 + & 298 (xk (3,ik ) + g (3,igk_k (ig,ik)) ) **2 ) * tpiba2 299 enddo 300 ! 301 do ib=1,n_block 302 303 write(stdout,*) 'BLOCK : ', ib 304 305 first_b=(ib-1)*nbnd_block+1 306 last_b=ib*nbnd_block 307 if(last_b>nbnd_occ(ik)) last_b=nbnd_occ(ik) 308 lenb=last_b-first_b+1 309 310 allocate (dpsi_ipol(npwx,lenb,3)) 311 allocate(t_out(npwx,nsteps_lanczos,lenb)) 312 allocate(psi_v(dffts%nnr, lenb)) 313 314 dpsi_ipol(:,:,:)=(0.d0,0.d0) 315 316 317 !trasform valence wavefunctions to real space 318 do ibnd=1,lenb 319 psi_v(:,ibnd) = ( 0.D0, 0.D0 ) 320 psi_v(dffts%nl(igk_k(1:npw,ik)),ibnd) = evc(1:npw,first_b+ibnd-1) 321 CALL invfft ('Wave', psi_v(:,ibnd), dffts) 322 enddo 323 324 325 326!loop on carthesian directions 327 do ipol = 1,3 328 write(stdout,*) 'ipol:', ipol 329 FLUSH(stdout) 330 ! 331 ! computes/reads P_c^+ x psi_kpoint into dvpsi array 332 ! 333 334 do jpol=1,3 335 336 call dvpsi_e (ik, jpol) 337 338 ! 339 ! Orthogonalize dvpsi to valence states: ps = <evc|dvpsi> 340 ! 341 CALL ZGEMM( 'C', 'N', nbnd_occ (ik), nbnd_occ (ik), npw, & 342 (1.d0,0.d0), evc(1,1), npwx, dvpsi(1,1), npwx, (0.d0,0.d0), & 343 ps(1,1), nbnd ) 344#if defined(__MPI) 345 !call reduce (2 * nbnd * nbnd_occ (ik), ps) 346 call mp_sum(ps(1:nbnd_occ (ik),1:nbnd_occ (ik)),world_comm) 347#endif 348 ! dpsi is used as work space to store S|evc> 349 ! 350 351 CALL calbec(npw,vkb,evc,becp,nbnd_occ(ik)) 352 CALL s_psi (npwx, npw, nbnd_occ(ik), evc, dpsi) 353 ! 354 355 ! |dvpsi> = - (|dvpsi> - S|evc><evc|dvpsi>) 356 ! note the change of sign! 357 ! 358 CALL ZGEMM( 'N', 'N', npw, nbnd_occ(ik), nbnd_occ(ik), & 359 (1.d0,0.d0), dpsi(1,1), npwx, ps(1,1), nbnd, (-1.d0,0.d0), & 360 dvpsi(1,1), npwx ) 361!create lanczos chain for dvpsi 362 dpsi_ipol(1:npw,1:lenb,jpol)=dvpsi(1:npw,first_b:last_b) 363 enddo 364 dvpsi(1:npw,1:lenb)=dpsi_ipol(1:npw,1:lenb,ipol) 365 366 367 allocate(d(nsteps_lanczos,lenb),f(nsteps_lanczos,lenb)) 368 allocate(omat(nsteps_lanczos,3,lenb)) 369 write(stdout,*) 'before lanczos_state_k' 370 371 372 373 374 call lanczos_state_k(ik,lenb, nsteps_lanczos ,dvpsi,d,f,omat,dpsi_ipol,t_out) 375 write(stdout,*) 'after lanczos_state_k' 376!loop on frequency 377 allocate(z_dl(nsteps_lanczos-1),z_d(nsteps_lanczos),z_du(nsteps_lanczos-1),z_b(nsteps_lanczos)) 378 do i=1,lenf 379!loop on valence states 380 do iv=1,lenb 381!invert Hamiltonian 382 z_dl(1:nsteps_lanczos-1)=conjg(f(1:nsteps_lanczos-1,iv)) 383 z_du(1:nsteps_lanczos-1)=f(1:nsteps_lanczos-1,iv) 384 z_d(1:nsteps_lanczos)=d(1:nsteps_lanczos,iv)+dcmplx(-et(first_b+iv-1,ik)-scissor(1)/rytoev,freqs(first_f+i-1)) 385 z_b(:)=(0.d0,0.d0) 386 z_b(1)=dble(omat(1,ipol,iv)) 387 call zgtsv(nsteps_lanczos,1,z_dl,z_d,z_du,z_b,nsteps_lanczos,info) 388 if(info/=0) then 389 write(stdout,*) 'problems with ZGTSV' 390 FLUSH(stdout) 391 stop 392 endif 393 do jpol=1,3 394!multiply with overlap factors 395 call zgemm('T','N',1,1,nsteps_lanczos,(1.d0,0.d0),omat(:,jpol,iv),nsteps_lanczos& 396 &,z_b,nsteps_lanczos,(0.d0,0.d0),csca,1) 397!update epsilon array NO SYMMETRIES for the moment 398 epsilon_g(jpol,ipol,i)=epsilon_g(jpol,ipol,i)+4.d0*ww*dble(csca) 399 enddo 400!update part for wing calculation 401 call zgemm('N','N',npw,1,nsteps_lanczos,(1.d0,0.d0),t_out(:,:,iv),npwx,z_b,nsteps_lanczos,& 402 &(0.d0,0.d0),psi_tmp,npwx) 403!fourier trasform 404 prod(:) = ( 0.D0, 0.D0 ) 405 prod(dffts%nl(igk_k(1:npw,ik))) = psi_tmp(1:npw) 406 CALL invfft ('Wave', prod, dffts) 407 408 409! product dpsi * psi_v 410 prod(1:dffts%nnr)=conjg(prod(1:dffts%nnr))*psi_v(1:dffts%nnr,iv) 411 if(doublegrid) call fft_interpolate(dffts, prod, dfftp, prod) 412 413!US part STLL TO BE ADDED!! 414 pola_charge(1:dffts%nnr,1,ipol,i)=pola_charge(1:dffts%nnr,1,ipol,i)-prod(1:dffts%nnr)*ww 415 416 417 enddo 418 enddo 419 deallocate(z_dl,z_d,z_du,z_b) 420 deallocate(d,f,omat) 421 enddo 422 deallocate(dpsi_ipol) 423 deallocate(t_out) 424 deallocate(psi_v) 425 end do!on ib 426 enddo! on ik 427 428!print out results 429 430 431 432! 433! symmetrize 434! 435 436 do i=1,lenf 437 WRITE( stdout,'(/,10x,"Unsymmetrized in crystal axis ",/)') 438 WRITE( stdout,'(10x,"(",3f15.5," )")') ((epsilon_g(ipol,jpol,i),& 439 & ipol=1,3),jpol=1,3) 440 441 ! call symtns (epsilon_g(:,:,i), nsym, s) 442 ! 443 ! pass to cartesian axis 444 ! 445 WRITE( stdout,'(/,10x,"Symmetrized in crystal axis ",/)') 446 WRITE( stdout,'(10x,"(",3f15.5," )")') ((epsilon_g(ipol,jpol,i),& 447 & ipol=1,3),jpol=1,3) 448 ! call trntns (epsilon_g(:,:,i), at, bg, 1) 449 450 call crys_to_cart ( epsilon_g(:,:,i) ) 451 call symmatrix ( epsilon_g(:,:,i)) 452 ! 453 ! add the diagonal part 454 ! 455 456 ! 457 ! and print the result 458 ! 459 WRITE( stdout, '(/,10x,"Dielectric constant in cartesian axis ",/)') 460 461 WRITE( stdout, '(10x,"(",3f18.9," )")') ((epsilon_g(ipol,jpol,i), ipol=1,3), jpol=1,3) 462 463 head(first_f+i-1,1)=epsilon_g(1,1,i) 464 head(first_f+i-1,2)=epsilon_g(2,2,i) 465 head(first_f+i-1,3)=epsilon_g(3,3,i) 466 467 468#if defined(__MPI) 469 call mp_sum ( pola_charge(:,:,:,i) , inter_pool_comm ) 470 call psyme (pola_charge(:,:,:,i)) 471#else 472 call syme (pola_charge(:,:,:,i)) 473#endif 474 475 do ipol=1,3 476 CALL fwfft ('Rho', pola_charge(1:dfftp%nnr,1,ipol,i), dfftp) 477 tmp_g(:)=(0.d0,0.d0) 478 tmp_g(gstart:ngm)=pola_charge(dfftp%nl(gstart:ngm),1,ipol,i) 479 480!loop on frequency 481 do ig=gstart,ngm 482 e_head_pol(ig,i,ipol)=-4.d0*tmp_g(ig) 483 enddo 484 enddo 485 486 487 enddo 488 489 490!writes on file wings 491 492!collect data 493 494 495 496 497!calculate total number of G for wave function 498 npwx_g=ngm 499 call mp_sum(npwx_g,world_comm) 500 allocate(e_head_g(ngm_g)) 501 502 if(ionode) then 503 iun = find_free_unit() 504 if(im==1) then 505 open( unit= iun, file=trim(tmp_dir)//trim(prefix)//'.e_head', status='unknown',form='unformatted') 506 write(iun) n_gauss 507 write(iun) omega_gauss 508 write(iun) freqs(1:n_gauss+1) 509 write(iun) npwx_g 510 else 511 open( unit= iun, file=trim(tmp_dir)//trim(prefix)//'.e_head', status='old',form='unformatted',& 512 &position='append') 513 endif 514 endif 515 516 call mp_barrier( world_comm ) 517 518 519 do i=1,lenf 520 do ipol=1,3 521 e_head_g(:)=(0.d0,0.d0) 522 call mergewf(e_head_pol(:,i,ipol),e_head_g ,ngm,ig_l2g,mpime,nproc,ionode_id,intra_pool_comm) 523 if(ionode) then 524 write(iun) e_head_g(1:npwx_g) 525 endif 526 enddo 527 enddo 528 call mp_barrier( world_comm ) 529 write(stdout,*) 'ATT02' 530 if(ionode) close(iun) 531 532 call mp_barrier( world_comm ) 533 write(stdout,*) 'ATT1' 534 deallocate(pola_charge) 535 deallocate(e_head_pol) 536 deallocate(e_head_g) 537 end do!im 538 539!writes on file head 540 541 if(ionode) then 542 iun = find_free_unit() 543 open( unit= iun, file=trim(tmp_dir)//trim(prefix)//'.head', status='unknown',form='unformatted') 544 write(iun) n_gauss 545 write(iun) omega_gauss 546 write(iun) freqs(1:n_gauss+1) 547 write(iun) head(1:n_gauss+1,1) 548 write(iun) head(1:n_gauss+1,2) 549 write(iun) head(1:n_gauss+1,3) 550 close(iun) 551 endif 552 553 554 555 deallocate(psi_tmp) 556 deallocate(prod) 557 558 deallocate (ps) 559 560 561 562 563 564 deallocate(head,head_tmp,freqs) 565 deallocate( tmp_g) 566 deallocate(epsilon_g) 567 568 569 570 call mp_barrier( world_comm ) 571 write(stdout,*) 'ATT2' 572 573 call stop_clock ('solve_head') 574 return 575end subroutine solve_head 576 577