visxcg.f90 16.8 KB
Newer Older
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
MODULE m_visxcg
  USE m_juDFT
  !     ******************************************************
  !     subroutine generates the exchange-correlation potential
  !     in the interstitial region    c.l.fu
  !     including gradient corrections. t.a. 1996.
  !     ******************************************************
CONTAINS
  SUBROUTINE visxcg(&
       &                  ifftd,stars,sym,&
       &                  ifftxc3d,&
       &                  cell,&
       &                  qpw,cdom,&
       &                  xcpot,input,&
       &                  obsolete,noco,&
       &                  rhmn,ichsmrg,&
       &                  vpw,vpw_w,vxpw,vxpw_w,&
       &                  excpw)

    !     ******************************************************
    !     instead of visxcor.f: the different exchange-correlation
    !     potentials defined through the key icorr are called through
    !     the driver subroutine vxcallg.f,for the energy density - excallg
    !     subroutines vectorized
    !     ** r.pentcheva 22.01.96
    !     *********************************************************
    !     in case of total = .true. calculates the ex-corr. energy
    !     density
    !     ** r.pentcheva 08.05.96
    !     ******************************************************************

    USE m_grdrsis
    USE m_prpxcfftmap
    USE m_mkgxyz3
    USE m_xcallg, ONLY : vxcallg,excallg
    USE m_fft3d
    USE m_fft3dxc
    USE m_types
    IMPLICIT NONE

    TYPE(t_xcpot),INTENT(IN)      :: xcpot
    TYPE(t_obsolete),INTENT(IN)   :: obsolete
    TYPE(t_input),INTENT(IN)      :: input
    TYPE(t_noco),INTENT(IN)       :: noco
    TYPE(t_sym),INTENT(IN)        :: sym
    TYPE(t_stars),INTENT(IN)      :: stars
    TYPE(t_cell),INTENT(IN)       :: cell
    !     ..
    !     .. Scalar Arguments ..
    !     ..
    !     .. Array Arguments ..

    REAL rhmn,rhmni ,d_15,sprsv
    INTEGER ichsmrg
    !     lwb: if true, white-bird trick.
    !     lwbc: l-white-bird-current.
    LOGICAL  lwbc
    !
    !----->  fft  information  for xc potential + energy
    !
    INTEGER, ALLOCATABLE :: igxc_fft(:)
    REAL,    ALLOCATABLE :: gxc_fft(:,:)

    !
    !-----> charge density, potential and energy density
    !
67 68 69 70
    COMPLEX, INTENT (IN) :: qpw(stars%ng3,input%jspins)
    COMPLEX, INTENT (OUT) :: excpw(stars%ng3)
    COMPLEX, INTENT (INOUT) ::vpw(stars%ng3,input%jspins),vpw_w(stars%ng3,input%jspins),cdom(stars%ng3)
    COMPLEX, INTENT (INOUT) ::vxpw(stars%ng3,input%jspins),vxpw_w(stars%ng3,input%jspins)
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
    !     ..
    !     .. Local Scalars ..
    INTEGER :: i ,k,js,nt,ifftxc3,idm,jdm,ndm,ig
    COMPLEX :: ci
    REAL    :: rhotot
    INTEGER :: ifftd,ifftxc3d

    !     ..
    !     .. Local Arrays ..
    COMPLEX, ALLOCATABLE :: fg3(:),cqpw(:,:), ph_wrk(:)
    REAL,    ALLOCATABLE :: bf3(:)
    REAL,    ALLOCATABLE :: rho(:,:),rhd1(:,:,:),rhd2(:,:,:)
    REAL,    ALLOCATABLE :: mx(:),my(:)
    REAL,    ALLOCATABLE :: magmom(:),dmagmom(:,:),ddmagmom(:,:,:) 
    ! 
    REAL, ALLOCATABLE :: vx(:,:),vxc(:,:),exc(:),vcon(:) 
    REAL, ALLOCATABLE :: agr(:),agru(:),agrd(:)
    REAL, ALLOCATABLE :: g2r(:),g2ru(:),g2rd(:)
    REAL, ALLOCATABLE :: gggr(:),gggru(:),gggrd(:)
    REAL, ALLOCATABLE :: gzgr(:)

    !     .. unused input (needed for other noco GGA-implementations) ..

    !ta+
    !.....------------------------------------------------------------------
    !------->          abbreviations
    !
    !     ph_wrk: work array containing phase * g_x,gy...... 
    !     qpw   : charge density stored as stars
    !     rho   : charge density stored in real space
    !     vxc   : exchange-correlation potential in real space
    !     exc   : exchange-correlation energy density in real space
    !     kxc1d  : dimension of the charge density fft box in the pos. domain
    !     kxc2d  : defined in dimens.f program (subroutine apws).1,2,3 indic
    !     kxc3d  ; a_1, a_2, a_3 directions.
    !     kq(i) : i=1,2,3 actual length of the fft-box for which fft is done
    !     nstr  : number of members (arms) of reciprocal lattice (g) vector
    !             of each star.
    !     nxc3_fft: number of stars in the  charge density  fft-box
    !     ng3   : number of 3 dim. stars in the charge density sphere define
    !             by gmax
    !     kmxxc_fft: number of g-vectors forming the nxc3_fft stars in the
    !               charge density or xc-density sphere
    !     kimax : number of g-vectors forming the ng3 stars in the gmax-sphe
    !     ifftxc3d: elements (g-vectors) in the charge density  fft-box
    !     igfft : pointer from the g-sphere (stored as stars) to fft-grid
    !             and     from fft-grid to g-sphere (stored as stars)
    !     pgfft : contains the phases of the g-vectors of sph.
    !     isn   : isn = +1, fft transform for g-space to r-space
    !             isn = -1, vice versa
    !
    !-------------------------------------------------------------------
    !
    !---> set up pointer for backtransformation of from g-vector in
    !     positive domain of xc density fftbox into stars.
    !     also the x,y,z components of the g-vectors are set up to calculate
    !     derivatives.
    !     in principle this can also be done in main program once.
    !     it is done here to save memory.
    !
131 132
    ifftd=27*stars%mx1*stars%mx2*stars%mx3
    ifftxc3d = stars%kxc1_fft*stars%kxc2_fft*stars%kxc3_fft
133 134 135 136 137 138 139 140 141
    ALLOCATE ( igxc_fft(0:ifftxc3d-1),gxc_fft(0:ifftxc3d-1,3) )
    CALL prp_xcfft_map(&
         &                   stars,sym,&
         &                   cell,&
         &                   igxc_fft,gxc_fft)
    !
    ifftxc3=stars%kxc1_fft*stars%kxc2_fft*stars%kxc3_fft
    lwbc=obsolete%lwb

142 143
    IF (stars%ng3.GT.stars%ng3) THEN
       WRITE(6,'(/'' stars%ng3.gt.stars%ng3. stars%ng3,stars%ng3='',2i6)') stars%ng3,stars%ng3
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
       CALL juDFT_error("ng3.gt.n3d",calledby="visxcg")
    ENDIF

    d_15=1.e-15
    !
    ci=CMPLX(0.,1.)
    !
    ! Allocate arrays
    ! ff
    ALLOCATE( bf3(0:ifftd-1),ph_wrk(0:ifftxc3d-1),  &
         &          rho(0:ifftxc3d-1,input%jspins),rhd1(0:ifftxc3d-1,input%jspins,3),&
         &          rhd2(0:ifftxc3d-1,input%jspins,6) )
    IF (noco%l_noco)  THEN
       ALLOCATE( mx(0:ifftxc3-1),my(0:ifftxc3-1),&
            &            magmom(0:ifftxc3-1),  &
            &            dmagmom(0:ifftxc3-1,3),ddmagmom(0:ifftxc3-1,3,3) )
    END IF


    !-->     transform charge density to real space

    DO js=1,input%jspins
       CALL fft3dxc(&
            &              rho(0:,js),bf3,&
            &              qpw(:,js),&
            &              stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,&
            &              stars%nxc3_fft,stars%kmxxc_fft,+1,&
            &              stars%igfft(0:,1),igxc_fft,stars%pgfft,stars%nstr)
    END DO

    IF (noco%l_noco) THEN  

       !       for off-diagonal parts the same
       CALL fft3dxc(&
            &               mx,my,&
            &               cdom,&
            &               stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,&
            &               stars%nxc3_fft,stars%kmxxc_fft,+1,&
            &               stars%igfft(0:,1),igxc_fft,stars%pgfft,stars%nstr)

       DO i=0,ifftxc3-1 
          rhotot= 0.5*( rho(i,1) + rho(i,2) )
          magmom(i)= SQRT(  (0.5*(rho(i,1)-rho(i,2)))**2 &
               &                    + mx(i)**2 + my(i)**2 )
          rho(i,1)= rhotot+magmom(i)
          rho(i,2)= rhotot-magmom(i)
       END DO

    ENDIF

    IF (xcpot%igrd.EQ.0) GOTO 100  

    ! In collinear calculations all derivatives are calculated in g-spce,
    ! in non-collinear calculations the derivatives of |m| are calculated in real space. 

    !-->   for d(rho)/d(x,y,z) = rhd1(:,:,idm) (idm=1,2,3).
    !
    !         ph_wrk: exp(i*(g_x,g_y,g_z)*tau) * g_(x,y,z).

203
    ALLOCATE(cqpw(stars%ng3,input%jspins))
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

    DO js= 1,input%jspins
       DO i = 1,stars%ng3
          cqpw(i,js)= ci*qpw(i,js)
       END DO
    END DO

    DO idm=1,3

       DO ig = 0 , stars%kmxxc_fft - 1
          ph_wrk(ig) = stars%pgfft(ig) * gxc_fft(ig,idm)
       END DO

       DO js=1,input%jspins
          CALL fft3dxc(&
               &           rhd1(0:,js,idm),bf3,&
               &           cqpw(:,js),&
               &           stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,stars%nxc3_fft,stars%kmxxc_fft,+1,&
               &           stars%igfft(0:,1),igxc_fft,ph_wrk,stars%nstr)
       END DO

    END DO

    IF (noco%l_noco) THEN

       CALL grdrsis(&
            &           magmom,cell,stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,obsolete,&
            &           dmagmom )

       DO i=0,ifftxc3-1
          DO idm=1,3
             rhotot= rhd1(i,1,idm)/2.+rhd1(i,2,idm)/2.
             rhd1(i,1,idm)= rhotot+dmagmom(i,idm) 
             rhd1(i,2,idm)= rhotot-dmagmom(i,idm) 
          END DO
       END DO

    END IF

    IF (lwbc) GOTO 100 

    !-->   for dd(rho)/d(xx,xy,yy,zx,yz,zz) = rhd2(:,:,idm) (idm=1,2,3,4,5,6)
    !
    !         ph_wrk: exp(i*(g_x,g_y,g_z)*tau) * g_(x,y,z) * g_(x,y,z)

    DO i = 1,stars%ng3
       DO js=1,input%jspins 
          cqpw(i,js)= -qpw(i,js)
       END DO
    END DO

    ndm = 0
    DO idm = 1,3
       DO jdm = 1,idm
          ndm = ndm + 1

          DO ig = 0 , stars%kmxxc_fft-1
             ph_wrk(ig) = stars%pgfft(ig)*gxc_fft(ig,idm)*gxc_fft(ig,jdm)
          ENDDO

          DO js=1,input%jspins
             CALL fft3dxc(&
                  &                rhd2(0:,js,ndm),bf3,&
                  &                cqpw(:,js),&
                  &                stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,stars%nxc3_fft,stars%kmxxc_fft,+1,&
                  &                stars%igfft(0:,1),igxc_fft,ph_wrk,stars%nstr)
          END DO
       END DO ! jdm 
    END DO   ! idm 

    DEALLOCATE(cqpw)

    IF (noco%l_noco) THEN

       DO idm = 1,3
          CALL grdrsis(&
               &           dmagmom(0,idm),cell,stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,obsolete,&
               &           ddmagmom(0,1,idm) )
       END DO

       ndm= 0
       DO idm = 1,3
          DO jdm = 1,idm
             ndm = ndm + 1  

             DO i=0,ifftxc3-1
                rhotot= rhd2(i,1,ndm)/2.+rhd2(i,2,ndm)/2.
                rhd2(i,1,ndm)= rhotot +&
                     &         ( ddmagmom(i,jdm,idm) + ddmagmom(i,idm,jdm) )/2. 
                rhd2(i,2,ndm)= rhotot -&
                     &         ( ddmagmom(i,jdm,idm) + ddmagmom(i,idm,jdm) )/2. 
             END DO

          ENDDO !jdm
       ENDDO   !idm 

    END IF

100 CONTINUE


    DEALLOCATE ( ph_wrk )
    IF (noco%l_noco) THEN 
       DEALLOCATE(mx,my,magmom,dmagmom,ddmagmom) 
    END IF
    !
    DO js=1,input%jspins 
       DO i=0,ifftxc3-1
          rho(i,js)=MAX(rho(i,js),d_15)
       ENDDO
    END DO
    bf3=0.0
    ! allocate the other arrays 
    !
    ALLOCATE (agr(0:ifftxc3d-1),agru(0:ifftxc3d-1),agrd(0:ifftxc3d-1),&
         &          g2r(0:ifftxc3d-1),g2ru(0:ifftxc3d-1),g2rd(0:ifftxc3d-1),&
         &       gggr(0:ifftxc3d-1),gggru(0:ifftxc3d-1),gggrd(0:ifftxc3d-1),&
         &       gzgr(0:ifftxc3d-1))

    !
    !     calculate the quantities such as abs(grad(rho)),.. used in
    !     evaluating the gradient contributions to potential and energy.
    !
    CALL mkgxyz3&
         &            (xcpot%igrd,ifftxc3d,input%jspins,ifftxc3,input%jspins,rho,&
         &             rhd1(0,1,1),rhd1(0,1,2),rhd1(0,1,3),&
         &             rhd2(0,1,1),rhd2(0,1,3),rhd2(0,1,6),&
         &             rhd2(0,1,5),rhd2(0,1,4),rhd2(0,1,2),&
         &             agr,agru,agrd,g2r,g2ru,g2rd,gggr,gggru,gggrd,&
         &             gzgr)

    DEALLOCATE ( rhd1,rhd2 )
    ALLOCATE ( vxc(0:ifftxc3d-1,input%jspins) )
    ALLOCATE ( vx (0:ifftxc3d-1,input%jspins) )
    !
    !     calculate the exchange-correlation potential in  real space
    !
    nt=ifftxc3
    !
    !      rhmni: rho_minimum_interstitial.

    rhmni=10.e+10

    DO js=1,input%jspins
       DO i=0,ifftxc3-1
          rho(i,js)=MAX(rho(i,js),d_15)
          rhmni=MIN(rhmni,rho(i,js))
       ENDDO
    ENDDO

    IF (rhmni.LT.rhmn) THEN
       rhmn=rhmni
       ichsmrg=2
    ENDIF

    IF (rhmn.LT.obsolete%chng) THEN
       WRITE(6,'(/'' rhmn.lt.obsolete%chng in visxc. rhmn,obsolete%chng='',&
            &     2d9.2)') rhmn,obsolete%chng
       !          CALL juDFT_error("visxcg: rhmn.lt.chng",calledby="visxcg")
    ENDIF

    CALL vxcallg(xcpot%icorr,lwbc,input%jspins,SIZE(agr),nt,rho,&
         &              agr,agru,agrd,g2r,g2ru,g2rd,&
         &              gggr,gggru,gggrd,gzgr,&
         &              vx,vxc)
    !
    !----> back fft to g space
    !----> perform back  fft transform: vxc(r) --> vxc(star)
    !
373
    ALLOCATE(fg3(stars%ng3))
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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

    DO js = 1,input%jspins
       bf3=0.0
       CALL fft3dxc(&
            &               vxc(0:,js),bf3,&
            &               fg3,&
            &               stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,&
            &               stars%nxc3_fft,stars%kmxxc_fft,-1,&
            &               stars%igfft(0:,1),igxc_fft,stars%pgfft,stars%nstr)
       !
       DO k = 1,stars%nxc3_fft
          vpw(k,js) = vpw(k,js) + fg3(k)
       ENDDO

       !
       !====>  INCLUDING TOTAL ENERGY
       !
       IF (input%total) THEN
          !
          !----> Perform fft transform: vxc(star) --> vxc(r) 
          !     !Use large fft mesh for convolution
          !
          fg3(stars%nxc3_fft+1:)=0.0
          ALLOCATE ( vcon(0:ifftd-1) )
          CALL fft3d(&
               &                vcon(0),bf3,&
               &                fg3,&
               &                stars,+1)
          !
          !----> Convolute with step function
          !
          DO i=0,ifftd-1
             vcon(i)=stars%ufft(i)*vcon(i)
          ENDDO
          bf3=0.0
          CALL fft3d(&
               &                vcon(0),bf3,&
               &                fg3,&
               &                stars,-1,.FALSE.)
          DEALLOCATE ( vcon )
          !
          !----> add to warped coulomb potential
          !
          DO k = 1,stars%ng3
             vpw_w(k,js) = vpw_w(k,js) + fg3(k)
          ENDDO

       ENDIF

       bf3=0.0
       CALL fft3dxc(&
            &                 vx(0:,js),bf3,&
            &                 fg3,&
            &                 stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,&
            &                 stars%nxc3_fft,stars%kmxxc_fft,-1,&
            &                 stars%igfft(0:,1),igxc_fft,stars%pgfft,stars%nstr)
       !
       DO k = 1,stars%nxc3_fft
          vxpw(k,js) = vxpw(k,js) + fg3(k)
       ENDDO

       !
       !====>   INCLUDING TOTAL ENERGY
       !
       IF (input%total) THEN
          !
          !---->  Perform fft transform: vxc(star) --> vxc(r) 
          !       !Use large fft mesh for convolution
          !

          fg3(stars%nxc3_fft+1:)=0.0
          ALLOCATE ( vcon(0:ifftd-1) )
          CALL fft3d(&
               &                vcon(0),bf3,&
               &                fg3,&
               &                stars,+1)
          !
          !----> Convolute with step function
          !
          DO i=0,ifftd-1
             vcon(i)=stars%ufft(i)*vcon(i)
          ENDDO
          bf3=0.0
          CALL fft3d(&
               &                vcon(0),bf3,&
               &                fg3,&
               &                stars,-1,.FALSE.)
          DEALLOCATE ( vcon )
          !
          !----> add to warped exchange-potential
          !
          DO k = 1,stars%ng3
             vxpw_w(k,js) = vxpw_w(k,js) + fg3(k)
          ENDDO

       ENDIF

    ENDDO
    DEALLOCATE ( vx,vxc )
    !
    !     calculate the ex.-cor energy density in real space
    !
    IF (input%total) THEN
       ALLOCATE ( exc(0:ifftxc3d-1) )
       CALL excallg(xcpot%icorr,lwbc,input%jspins,nt,rho,&
            &               agr,agru,agrd,g2r,g2ru,g2rd,&
            &               gggr,gggru,gggrd,gzgr,&
            &               exc)
       !
       !---->   perform back  fft transform: exc(r) --> exc(star)
       !
       bf3=0.0
       CALL fft3dxc(&
            &                exc,bf3,&
            &                fg3,&
            &                stars%kxc1_fft,stars%kxc2_fft,stars%kxc3_fft,stars%nxc3_fft,stars%kmxxc_fft,-1,&
            &                stars%igfft(0:,1),igxc_fft,stars%pgfft,stars%nstr)
       DEALLOCATE ( exc )
       !
       !---->   Perform fft transform: exc(star) --> exc(r) 
       !        !Use large fft mesh for convolution
       !
       fg3(stars%nxc3_fft+1:)=0.0
       bf3=0.0
       ALLOCATE ( vcon(0:ifftd-1) )
       CALL fft3d(vcon,bf3,fg3,&
            &             stars,+1)

       DO i=0,ifftd-1
          vcon(i)=stars%ufft(i)*vcon(i)
       ENDDO
       !
       !         ---> back fft to g space
       !
       bf3=0.0
       CALL fft3d(vcon,bf3,excpw,&
            &             stars,-1,.FALSE.)
       DEALLOCATE ( vcon )
       !
    ENDIF

    DEALLOCATE(fg3)
    DEALLOCATE ( bf3,rho,igxc_fft,gxc_fft )
    DEALLOCATE ( agr,agru,agrd,g2r,g2ru,g2rd,&
         &             gggr,gggru,gggrd,gzgr )



  END SUBROUTINE visxcg
END MODULE m_visxcg