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 10subroutine dft_exchange(nbnd_v,nbnd_s,n_set, e_x,ks_wfcs) 11!this subroutine calculates the exchange 12!energy term for every state and writes on disk 13 14 15 16 USE io_global, ONLY : stdout, ionode, ionode_id 17 USE io_files, ONLY : prefix, tmp_dir, iunwfc, nwordwfc 18 USE kinds, ONLY : DP 19 USE basis 20 USE klist 21 USE constants, ONLY : e2, pi, tpi, fpi, RYTOEV 22 USE wvfct, ONLY : npwx, npw, nbnd, wg 23 USE gvecw, ONLY : gcutw 24 USE cell_base, ONLY: at, alat, tpiba, omega, tpiba2,bg 25 USE wannier_gw 26 USE gvect 27 USE gvecs, ONLY : doublegrid 28 USE uspp 29 USE uspp_param, ONLY : lmaxq,upf,nh, nhm 30 USE wavefunctions, ONLY : psic 31 ! USE realus, ONLY : adduspos_gamma_r 32 USE cell_base, ONLY : at, bg, omega 33 USE mp, ONLY : mp_sum, mp_bcast 34 USE mp_world, ONLY : world_comm 35 USE control_flags, ONLY : gamma_only 36 !USE exx, ONLY : exx_divergence_new, exx_grid_init, yukawa,exx_divergence_old 37 USE fft_base, ONLY : dfftp, dffts 38 USE fft_interfaces, ONLY : fwfft, invfft, fft_interpolate 39 USE fft_base, ONLY : dfftp 40 USE io_global, ONLY : ionode 41 USE lsda_mod, ONLY : nspin 42 43 implicit none 44 45 INTEGER, EXTERNAL :: find_free_unit 46 47 INTEGER, INTENT(in) :: nbnd_v(nspin) !number of valence states for both spin channels 48 INTEGER, INTENT(in) :: nbnd_s !number of states considered 49 INTEGER, INTENT(in) :: n_set !defines the number of states to be read from disk at the same time 50 REAL(kind=DP), INTENT(out) :: e_x(nbnd,nspin)!in output exchange energies 51 COMPLEX(kind=DP), INTENT(in) :: ks_wfcs(npwx,nbnd,nspin)!all kohn sham wavefunctions 52 53 REAL(kind=DP), ALLOCATABLE :: fac(:) 54 REAL(kind=DP) :: qq_fact,exxdiv 55 INTEGER :: ig,iiv,iv,jjs,js,hw,ks 56 REAL(kind=DP), ALLOCATABLE :: becpr(:,:) 57 REAL(kind=DP), ALLOCATABLE :: tmpreal1(:), tmpreal_v(:,:),tmpreal_s(:,:) 58 INTEGER :: igk0(npwx) 59 REAL(kind=dp) :: g2kin_bp(npwx) 60 INTEGER :: npw0 61 INTEGER :: jmin,jmax 62 COMPLEX(kind=DP), ALLOCATABLE :: prod_g(:),prod_c(:),prod_g2(:,:) 63 REAL(kind=DP), ALLOCATABLE :: prod_r(:) 64 REAL(kind=DP) :: exc 65 INTEGER :: iun 66 INTEGER, PARAMETER :: n_int=20 67 REAL(kind=DP) :: qx,qy,qz 68 INTEGER :: ix,iy,iz,n_int_loc,iunu 69 REAL(kind=DP), ALLOCATABLE :: e_x_off(:,:,:) 70 COMPLEX(kind=DP) :: c_exc 71 INTEGER :: isv 72 73 74 allocate(fac(ngm)) 75 if(l_whole_s) then 76 allocate(e_x_off(nbnd_s,nbnd_s,nspin)) 77 e_x_off(:,:,:)=0.d0 78 endif 79!sets factors terms 80!sets factors terms 81!this has already been called call exx_grid_init() 82 if(l_truncated_coulomb) then 83 84 do ig=1,ngm 85 86 qq_fact = g(1,ig)**2.d0 + g(2,ig)**2.d0 + g(3,ig)**2.d0 87 88 if (qq_fact > 1.d-8) then 89 fac(ig)=(e2*fpi/(tpiba2*qq_fact))*(1.d0-dcos(dsqrt(qq_fact)*truncation_radius*tpiba)) 90 else 91 fac(ig)=e2*fpi*(truncation_radius**2.d0/2.d0) 92 93 endif 94 95 end do 96 fac(:)=fac(:)/omega 97 else 98 fac(:)=0.d0 99 fac(1:npw)=vg_q(1:npw) 100 endif 101 102 e_x(:,:)=0.d0 103 CALL gk_sort(xk(1,1),ngm,g,gcutw,npw0,igk0,g2kin_bp) 104 105 106 107 108 109 allocate(tmpreal1(dfftp%nnr)) 110 allocate(tmpreal_v(dfftp%nnr,n_set)) 111 allocate(tmpreal_s(dfftp%nnr,n_set)) 112 allocate(prod_g(ngm),prod_g2(ngm,nbnd_s)) 113 allocate(prod_c(dfftp%nnr)) 114 allocate(prod_r(dfftp%nnr)) 115 116!external loop on valence state 117 do isv=1,nspin 118 do iiv=1,ceiling(real(nbnd_v(isv))/real(n_set)) 119 !read states and do fourier transform 120 do hw=(iiv-1)*n_set+1,min(iiv*n_set,nbnd_v(isv)),2 121 psic(:)=(0.d0,0.d0) 122 psic(:)=(0.d0,0.d0) 123 IF ( hw < min(iiv*n_set,nbnd_v(isv))) then 124 psic(dffts%nl(1:npw0)) = ks_wfcs(1:npw0,hw,isv) + & 125 ( 0.D0, 1.D0 ) * ks_wfcs(1:npw0,hw+1,isv) 126 psic(dffts%nlm(1:npw0)) = CONJG( ks_wfcs(1:npw,hw,isv) - & 127 ( 0.D0, 1.D0 ) * ks_wfcs(1:npw0,hw+1,isv) ) 128 ELSE 129 psic(dffts%nl(1:npw0)) = ks_wfcs(1:npw0,hw,isv) 130 psic(dffts%nlm(1:npw0)) = CONJG( ks_wfcs(1:npw0,hw,isv) ) 131 END IF 132 133 CALL invfft ('Wave', psic, dffts) 134 tmpreal1(1:dfftp%nnr)=dble(psic(1:dfftp%nnr)) 135 if(doublegrid) then 136 call fft_interpolate(dffts,tmpreal1, dfftp, tmpreal_v(:,hw-(iiv-1)*n_set)) ! interpolate smooth -> dense 137 else 138 tmpreal_v(:,hw-(iiv-1)*n_set)=tmpreal1(:) 139 endif 140 if ( hw < min(iiv*n_set,nbnd_v(isv))) then 141 tmpreal1(1:dfftp%nnr)=aimag(psic(1:dfftp%nnr)) 142 if(doublegrid) then 143 call fft_interpolate(dffts,tmpreal1, dfftp, tmpreal_v(:,hw-(iiv-1)*n_set+1)) ! interpolate smooth -> dense 144 else 145 tmpreal_v(:,hw-(iiv-1)*n_set+1)=tmpreal1(:) 146 endif 147 endif 148 149 enddo 150 151 152 do jjs=1,ceiling(real(nbnd_s)/real(n_set)) 153 !external loop on states 154 !read states and do fourier transform 155 do hw=(jjs-1)*n_set+1,min(jjs*n_set,nbnd_s),2 156 psic(:)=(0.d0,0.d0) 157 psic(:)=(0.d0,0.d0) 158 IF ( hw < min(jjs*n_set,nbnd_s)) then 159 psic(dffts%nl(1:npw0)) = ks_wfcs(1:npw0,hw,isv) + & 160 ( 0.D0, 1.D0 ) * ks_wfcs(1:npw0,hw+1,isv) 161 psic(dffts%nlm(1:npw0)) = CONJG( ks_wfcs(1:npw,hw,isv) - & 162 ( 0.D0, 1.D0 ) * ks_wfcs(1:npw0,hw+1,isv) ) 163 ELSE 164 psic(dffts%nl(1:npw0)) = ks_wfcs(1:npw0,hw,isv) 165 psic(dffts%nlm(1:npw0)) = CONJG( ks_wfcs(1:npw0,hw,isv) ) 166 END IF 167 168 CALL invfft ('Wave', psic, dffts) 169 tmpreal1(1:dfftp%nnr)=dble(psic(1:dfftp%nnr)) 170 if(doublegrid) then 171 call fft_interpolate(dffts,tmpreal1, dfftp, tmpreal_s(:,hw-(jjs-1)*n_set)) ! interpolate smooth -> dense 172 else 173 tmpreal_s(:,hw-(jjs-1)*n_set)=tmpreal1(:) 174 endif 175 if ( hw < min(jjs*n_set,nbnd_s)) then 176 tmpreal1(1:dfftp%nnr)=aimag(psic(1:dfftp%nnr)) 177 if(doublegrid) then 178 call fft_interpolate(dffts,tmpreal1, dfftp, tmpreal_s(:,hw-(jjs-1)*n_set+1)) ! interp. smooth -> dense 179 else 180 tmpreal_s(:,hw-(jjs-1)*n_set+1)=tmpreal1(:) 181 endif 182 endif 183 184 enddo 185 186 !internal loop on valence states 187 do iv=(iiv-1)*n_set+1,min(iiv*n_set,nbnd_v(isv)) 188 189 jmin=(jjs-1)*n_set+1 190 jmax=min(jjs*n_set,nbnd_s) 191 192!for whole X operator for given iv calculate products in real space with all the 193!KS states and store in G space 194 if(l_whole_s) then 195!NOT_TO_BE_INCLUDED_START 196 do ks=1,nbnd_s,1 197 psic(:)=(0.d0,0.d0) 198 psic(dffts%nl(1:npw0)) = ks_wfcs(1:npw0,ks,isv) 199 psic(dffts%nlm(1:npw0)) = CONJG( ks_wfcs(1:npw0,ks,isv) ) 200 CALL invfft ('Wave', psic, dffts) 201 prod_c(1:dfftp%nnr)=dcmplx(dble(psic(1:dfftp%nnr))*tmpreal_v(1:dfftp%nnr,iv-(iiv-1)*n_set)& 202 & ,0.d0) 203 CALL fwfft ('Rho', prod_c, dfftp) 204 prod_g2(1:ngm,ks)=prod_c(dfftp%nl(1:ngm)) 205 enddo 206!NOT_TO_BE_INCLUDED_END 207 endif 208 209 do js=jmin,jmax 210 !do product in real speace 211 prod_r(:)=tmpreal_v(:,iv-(iiv-1)*n_set)*tmpreal_s(:,js-(jjs-1)*n_set) 212 ! if(okvan) call adduspos_gamma_r & ATTENZIONE 213 ! (iv,js, prod_r(:),1,becpr(:,iv),becpr(:,js)) 214 215 prod_c(:)=dcmplx(prod_r(:),0.d0) 216 CALL fwfft ('Rho', prod_c, dfftp) 217 !go to g_space 218 prod_g(1:ngm)=prod_c(dfftp%nl(1:ngm)) 219 !calculated exchange 220 exc=0.d0 221 do ig=1,ngm 222 exc=exc+2.d0*dble(conjg(prod_g(ig))*prod_g(ig))*fac(ig)*wg(iv,isv)*dble(nspin)/2.d0 223 enddo 224 if(gstart==2) exc=exc-dble(prod_g(1))*dble(prod_g(1))*fac(1)*wg(iv,isv)*dble(nspin)/2.d0 225 call mp_sum(exc,world_comm) 226 exc=-exc 227 e_x(js,isv)=e_x(js,isv)+exc 228 229!poor programmer solution for off diagonal terms.... 230!ONLY FOR NORMCONSERVING PSEUDOS 231 if(l_whole_s) then 232!NOT_TO_BE_INCLUDED_START 233 write(stdout,*) 'Call complete X operator part',iv 234 FLUSH(stdout) 235 do ks=1,nbnd_s,1 236 c_exc=(0.d0,0.d0) 237 do ig=1,ngm 238 c_exc=c_exc+conjg(prod_g2(ig,ks))*prod_g(ig)*fac(ig)+& 239 &prod_g2(ig,ks)*conjg(prod_g(ig))*fac(ig) 240 enddo 241 if(gstart==2) c_exc=c_exc-conjg(prod_g2(1,ks))*prod_g(1)*fac(1) 242 call mp_sum(c_exc,world_comm) 243 c_exc=-c_exc 244 e_x_off(ks,js,isv)=e_x_off(ks,js,isv)+dble(c_exc) 245 enddo 246!NOT_TO_BE_INCLUDED_END 247 endif 248 enddo 249 enddo 250 enddo 251 252 enddo 253 enddo!ivv 254 do isv=1,nspin 255 do iv=1,nbnd_s 256 write(stdout,*) 'Exchange energy', iv,isv, e_x(iv,isv) 257 enddo 258 enddo 259!write on file 260 261 if(ionode) then 262 iun = find_free_unit() 263 open(unit=iun,file=trim(tmp_dir)//trim(prefix)//'.exchange',status='unknown',form='unformatted') 264 write(iun) nbnd_s 265 do isv=1,nspin 266!NOT_TO_BE_INCLUDED_START 267 if(l_selfconsistent) e_x(1:nbnd_s,isv)=0.d0 268!NOT_TO_BE_INCLUDED_END 269 write(iun) e_x(1:nbnd_s,isv) 270 enddo 271 close(iun) 272 endif 273 274!if required write on disk off-diagonal terms 275 276 277 if(l_whole_s) then 278!NOT_TO_BE_INCLUDED_START 279 if(ionode) then 280 do iv=1,nbnd_s 281 write(stdout,*) 'Exchange energy off', iv, e_x_off(iv,iv,1) 282 enddo 283 !write on file 284 285 iun = find_free_unit() 286 open(unit=iun,file=trim(tmp_dir)//trim(prefix)//'.exchange_off',status='unknown',form='unformatted') 287 write(iun) nbnd_s 288 do isv=1,nspin 289 do js=1,nbnd_s 290 write(iun) e_x_off(1:nbnd_s,js,isv) 291 enddo 292 enddo 293 close(iun) 294 endif 295!NOT_TO_BE_INCLUDED_END 296 endif 297 298 299 deallocate(tmpreal1,tmpreal_s,tmpreal_v) 300 301 deallocate(fac) 302 deallocate(prod_c,prod_g,prod_g2) 303 deallocate(prod_r) 304 ! if(okvan) deallocate(becpr) 305 if(l_whole_s) then 306!NOT_TO_BE_INCLUDED_START 307 deallocate(e_x_off) 308!NOT_TO_BE_INCLUDED_END 309 endif 310 311 end subroutine dft_exchange 312 313 314 315!---------------------------------------------------------------------- 316subroutine addus_charge(r_ij,becp_iw,becp_jw) 317 !---------------------------------------------------------------------- 318 ! 319 ! This routine adds to the charge density the part which is due to 320 ! the US augmentation. 321 ! 322 USE kinds, ONLY : DP 323 USE ions_base, ONLY : nat, ntyp => nsp, ityp 324 USE cell_base, ONLY : tpiba 325 USE gvect, ONLY : ngm, gg, g, eigts1, eigts2, & 326 eigts3, mill 327 USE lsda_mod, ONLY : nspin 328 USE scf, ONLY : rho 329 USE uspp, ONLY : okvan, nkb 330 USE uspp_param, ONLY : lmaxq, upf, nh 331 USE wavefunctions, ONLY : psic 332 USE control_flags , ONLY : gamma_only 333 USE fft_base, ONLY : dfftp, dffts 334 USE fft_interfaces, ONLY : fwfft, invfft 335 336 ! 337 implicit none 338 339 COMPLEX(kind=DP), INTENT(inout) :: r_ij(dfftp%nnr)!where to add the us term 340 COMPLEX(kind=DP), INTENT(in) :: becp_iw( nkb)!overlap of wfcs with us projectors 341 COMPLEX(kind=DP), INTENT(in) :: becp_jw( nkb)!overlap of wfcs with us projectors 342 343 344 345 ! 346 ! here the local variables 347 ! 348 349 integer :: ig, na, nt, ih, jh, is 350 ! counters 351 352 real(DP), allocatable :: qmod (:), ylmk0 (:,:) 353 ! the modulus of G 354 ! the spherical harmonics 355 356 complex(DP) :: skk 357 complex(DP), allocatable :: aux (:,:), qgm(:) 358 ! work space for rho(G,nspin) 359 ! Fourier transform of q 360 INTEGER, ALLOCATABLE :: ind_cor(:,:,:) 361 INTEGER :: ijkb0, ikb,np 362 363 if (.not.okvan) return 364 365 366 allocate (aux ( ngm, nspin)) 367 allocate (qmod( ngm)) 368 allocate (qgm( ngm)) 369 allocate (ylmk0( ngm, lmaxq * lmaxq)) 370 371 aux (:,:) = (0.d0, 0.d0) 372 call ylmr2 (lmaxq * lmaxq, ngm, g, gg, ylmk0) 373 do ig = 1, ngm 374 qmod (ig) = sqrt (gg (ig) ) * tpiba 375 enddo 376 377!found index correspondence 378 379 380 allocate(ind_cor(ntyp,nat,maxval(nh(1:ntyp)))) 381 382 ijkb0 = 0 383 do np = 1, ntyp 384 if ( upf(np)%tvanp ) then 385 do na = 1, nat 386 if ( ityp(na) == np ) then 387 do ih = 1, nh(np) 388 ikb = ijkb0 + ih 389 ind_cor(np,na,ih)=ikb 390 enddo 391 ijkb0=ijkb0+nh(np) 392 endif 393 enddo 394 else 395 do na=1,nat 396 if(ityp(na) == np) ijkb0=ijkb0+nh(np) 397 enddo 398 endif 399 enddo 400 401 402 403 404 405 406 407 408 do nt = 1, ntyp 409 if (upf(nt)%tvanp ) then 410 do ih = 1, nh (nt) 411 do jh = 1, nh (nt) 412 call qvan2 (ngm, ih, jh, nt, qmod, qgm, ylmk0) 413 do na = 1, nat 414 if (ityp (na) .eq.nt) then 415 ! 416 ! Multiply becsum and qg with the correct structure factor 417 ! 418 do is = 1, nspin 419 do ig = 1, ngm 420 skk = eigts1 (mill(1,ig), na) * & 421 eigts2 (mill(2,ig), na) * & 422 eigts3 (mill(3,ig), na) 423 aux(ig,is)=aux(ig,is) + qgm(ig)*skk*& 424 &conjg(becp_iw(ind_cor(nt,na,ih)))*becp_jw(ind_cor(nt,na,jh)) 425 enddo 426 enddo 427 endif 428 enddo 429 enddo 430 enddo 431 endif 432 enddo 433 434 deallocate(ind_cor) 435 ! 436 deallocate (ylmk0) 437 deallocate (qgm) 438 deallocate (qmod) 439 ! 440 ! convert aux to real space and add to the charge density 441 ! 442 do is = 1, nspin!SPIN TO BE IMPLEMENTED YET 443 psic(:) = (0.d0, 0.d0) 444 psic( dfftp%nl(:) ) = aux(:,is) 445 if (gamma_only) psic( dfftp%nlm(:) ) = CONJG(aux(:,is)) 446 CALL invfft ('Rho', psic, dfftp) 447 r_ij(:)=r_ij(:)+psic(:) 448 enddo 449 deallocate (aux) 450 451 return 452end subroutine addus_charge 453 454