subvxc.F90 21.3 KB
Newer Older
1 2
      MODULE m_subvxc
      CONTAINS
Daniel Wortmann's avatar
Daniel Wortmann committed
3 4
      SUBROUTINE subvxc(lapw,bk, dimension,input,jsp,vr0, atoms,usdus, hybrid, el,ello,sym,&
           nlot_d,kveclo, cell, sphhar, stars,xcpot,mpi,oneD,hamovlp)
5 6 7 8 9 10 11 12 13 14 15 16 17 18


      USE m_intgr,     ONLY : intgr3
      USE m_constants
      USE m_gaunt,     ONLY : gaunt1
      USE m_wrapper
      USE m_loddop
      USE m_radflo
      USE m_radfun
      USE m_abcof3
      USE m_icorrkeys
      USE m_hybridmix
      USE m_types
      IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
19 20 21 22 23 24 25 26 27 28 29 30
      TYPE(t_xcpot),INTENT(IN)     :: xcpot
      TYPE(t_mpi),INTENT(IN)       :: mpi
      TYPE(t_dimension),INTENT(IN) :: dimension
      TYPE(t_oneD),INTENT(IN)      :: oneD
      TYPE(t_hybrid),INTENT(INOUT) :: hybrid
      TYPE(t_input),INTENT(IN)     :: input
      TYPE(t_sym),INTENT(IN)       :: sym
      TYPE(t_stars),INTENT(IN)     :: stars
      TYPE(t_cell),INTENT(IN)      :: cell
      TYPE(t_sphhar),INTENT(IN)    :: sphhar
      TYPE(t_atoms),INTENT(IN)     :: atoms
      TYPE(t_lapw),INTENT(IN)      :: lapw
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      TYPE(t_usdus),INTENT(INOUT)  :: usdus
!     .. Scalar Arguments ..
    
      INTEGER, INTENT (IN) :: jsp 
      INTEGER, INTENT (IN) :: nlot_d
     



!     .. Array Arguments ..
      INTEGER, INTENT (IN) :: kveclo(nlot_d)

      REAL,    INTENT (IN) :: vr0(atoms%jmtd,atoms%ntype,dimension%jspd)               ! just for radial functions
      REAL,    INTENT (IN) :: el(0:atoms%lmaxd,atoms%ntype,dimension%jspd)
      REAL,    INTENT (IN) :: ello(atoms%nlod,atoms%ntype,dimension%jspd)
      REAL,    INTENT (IN) :: bk(3)
Daniel Wortmann's avatar
Daniel Wortmann committed
47
      TYPE(t_hamovlp),INTENT(INOUT)::hamovlp
48 49 50

!     .. Local Scalars ..
      INTEGER               ::  ic,indx,m,ig1,ig2
Daniel Wortmann's avatar
Daniel Wortmann committed
51
      INTEGER               ::  nlharm,nnbas,typsym,lm
52
      INTEGER               ::  noded,nodeu
Daniel Wortmann's avatar
Daniel Wortmann committed
53
      INTEGER               ::  nbasf0
54
      INTEGER               ::  i,j,l,ll,l1,l2 ,m1,m2  ,j1,j2
Daniel Wortmann's avatar
Daniel Wortmann committed
55 56 57
      INTEGER               ::  ok,p1,p2,lh,mh,pp1,pp2
      INTEGER               ::  igrid,itype,ilharm,istar
      INTEGER               ::  ineq,iatom,ilo,ilop,ieq,icentry
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
      INTEGER               ::  ikvecat,ikvecprevat,invsfct,ikvec,ikvecp
      INTEGER               ::  lp,mp,pp
      REAL                  ::  a_ex
      REAL                  ::  wronk
      COMPLEX               ::  rc,rr

!     .. Local Arrays ..
      INTEGER               ::  gg(3)
      INTEGER               ::  pointer_lo(atoms%nlod,atoms%ntype)
      
      REAL                  ::  integ(0:sphhar%nlhd,hybrid%maxindx,0:atoms%lmaxd,hybrid%maxindx,0:atoms%lmaxd)
      REAL                  ::  grid(atoms%jmtd)
      REAL                  ::  vr(atoms%jmtd,0:sphhar%nlhd)
      REAL                  ::  vxr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,dimension%jspd)
      REAL                  ::  f(atoms%jmtd,2,0:atoms%lmaxd),g(atoms%jmtd,2,0:atoms%lmaxd)
      REAL                  ::  flo(atoms%jmtd,2,atoms%nlod)
      REAL                  ::  uuilon(atoms%nlod,atoms%ntype),duilon(atoms%nlod,atoms%ntype)
      REAL                  ::  ulouilopn(atoms%nlod,atoms%nlod,atoms%ntype)
      
      
      REAL                  ::  bas1(atoms%jmtd,hybrid%maxindx,0:atoms%lmaxd,atoms%ntype),&
                                bas2(atoms%jmtd,hybrid%maxindx,0:atoms%lmaxd,atoms%ntype)

      COMPLEX               ::  vxpw(stars%ng3,dimension%jspd)
      COMPLEX               ::  vpw(stars%ng3)
Daniel Wortmann's avatar
Daniel Wortmann committed
83
      COMPLEX               ::  vxc(hamovlp%matsize)
84 85 86 87
      COMPLEX               ::  vrmat(hybrid%maxlmindx,hybrid%maxlmindx)
      COMPLEX               ::  carr(hybrid%maxlmindx,dimension%nvd),carr1(dimension%nvd,dimension%nvd)
      COMPLEX ,ALLOCATABLE  ::  ahlp(:,:,:),bhlp(:,:,:)
      COMPLEX, ALLOCATABLE  ::  bascof(:,:,:)
Daniel Wortmann's avatar
Daniel Wortmann committed
88
      COMPLEX               ::  bascof_lo(3,-atoms%llod:atoms%llod,4*atoms%llod+2,atoms%nlod, atoms%nat)
89

Daniel Wortmann's avatar
Daniel Wortmann committed
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 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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

      CALL timestart("subvxc")
      vxc=0

      
      OPEN (11,file='potx',form='unformatted',status='old')
      STOP "IO in subvxc TODO"
      !CALL loddop(&
      !            dimension,stars,oneD%n2d,vacuum,atoms,sphhar,&
      !            input%nq2,sym,sym,&
      !            11,&
      !            iop,dop,iter,vxr,vxpw,vxz,vxzxy,name)
      CLOSE(11)

      !  calculate radial functions
      hybrid%nindx      = 2
      DO itype = 1,atoms%ntype

          
!
!--->    generate the radial basis-functions for each l
!

         WRITE(6,'(a,i3,a)') new_line('n')//new_line('n')//' wavefunction parameters for atom type',itype,':'
         WRITE(6,'(31x,a,32x,a)') 'radial function','energy derivative'
         WRITE(6,'(a)') '  l    energy            value        '//&
               'derivative    nodes          value        derivative    nodes       norm        wronskian'
         DO l = 0,atoms%lmax(itype)
            CALL radfun(l,itype,jsp,el(l,itype,jsp),vr0(1,itype,jsp),atoms,&
                       f(1,1,l),g(1,1,l),usdus,&
                       nodeu,noded,wronk)
            WRITE (6,FMT=8010) l,el(l,itype,jsp),usdus%us(l,itype,jsp),&
                   usdus%dus(l,itype,jsp),nodeu,usdus%uds(l,itype,jsp),usdus%duds(l,itype,jsp),noded,&
                   usdus%ddn(l,itype,jsp),wronk
         END DO
!  8000    FORMAT (1x,/,/,' wavefunction parameters for atom type',i3,':',
!      +          /,t32,'radial function',t79,'energy derivative',/,t3,
!      +          'l',t8,'energy',t26,'value',t39,'derivative',t53,
!      +          'nodes',t68,'value',t81,'derivative',t95,'nodes',t107,
!      +          'norm',t119,'wronskian')
 8010    FORMAT (i3,f10.5,2 (5x,1p,2e16.7,i5),1p,2e16.7)

         bas1(:,1,:,itype)=f(:,1,:)
         bas1(:,2,:,itype)=g(:,1,:)
         bas2(:,1,:,itype)=f(:,2,:)
         bas2(:,2,:,itype)=g(:,2,:)

!
!--->   generate the extra radial basis-functions for the local orbitals,
!--->   if there are any.
!        
         IF (atoms%nlo(itype).GE.1) THEN

            CALL radflo(&
                    atoms,itype,jsp,ello(1,1,jsp),vr0(1,itype,jsp),&
                    f,g,mpi,&
                    usdus,&
                    uuilon,duilon,ulouilopn,flo,.TRUE.)

            DO i=1,atoms%nlo(itype)
              hybrid%nindx(atoms%llo(i,itype),itype) = hybrid%nindx(atoms%llo(i,itype),itype) + 1
              pointer_lo(i,itype)       = hybrid%nindx(atoms%llo(i,itype),itype)
              bas1(:,hybrid%nindx(atoms%llo(i,itype),itype),atoms%llo(i,itype),itype)=&
                                                              flo(:,1,i)
              bas2(:,hybrid%nindx(atoms%llo(i,itype),itype),atoms%llo(i,itype),itype)=&
                                                              flo(:,2,i)
           END DO
         END IF
       END DO


      ! compute APW coefficients

      !  calculate bascof
      ALLOCATE( ahlp(dimension%nvd,0:dimension%lmd,atoms%nat),bhlp(dimension%nvd,0:dimension%lmd,atoms%nat),stat=ok)
      IF( ok .ne. 0 ) STOP 'subvxc: error in allocation of ahlp/bhlp'

      CALL abcof3( input,atoms,sym,jsp,cell, bk,lapw,&
                   usdus, kveclo,oneD,ahlp,bhlp,bascof_lo)

      ALLOCATE( bascof(dimension%nvd,2*(dimension%lmd+1),atoms%nat), stat=ok )
      IF( ok .ne. 0 ) STOP 'subvxc: error in allocation of bascof'
      bascof = 0
      ic     = 0

      DO itype=1,atoms%ntype
        DO ieq=1,atoms%neq(itype)
          ic   = ic + 1
          indx = 0
          DO l=0,atoms%lmax(itype)
            ll = l*(l+1)
            DO M=-l,l
              lm=ll+M 
              DO i=1,2
                indx = indx + 1
                IF( i .eq. 1) THEN
                  bascof(:,indx,ic) = ahlp(:,lm,ic)
                ELSE IF( i .eq. 2 ) THEN
                  bascof(:,indx,ic) = bhlp(:,lm,ic)
                END IF
              END DO
            END DO
          END DO
        END DO
      END DO 

      DEALLOCATE( ahlp,bhlp )

      ! Loop over atom types
      iatom = 0
      DO itype = 1,atoms%ntype
      
        typsym = atoms%ntypsy( sum(atoms%neq(:itype-1))+1 )
        nlharm = sphhar%nlh(typsym)

        ! Calculate vxc = vtot - vcoul
        DO l=0,nlharm
          DO i=1,atoms%jri(itype)
            IF(l.eq.0) THEN
!               vr(i,0)= vrtot(i,0,itype)*sfp/rmsh(i,itype) -  vrcou(i,0,itype,jsp)   
              vr(i,0)=  vxr(i,0,itype,jsp)*sfp_const/atoms%rmsh(i,itype)  !
            ELSE                                              ! vxc = vtot - vcoul
!               vr(i,l)= vrtot(i,l,itype)-vrcou(i,l,itype,jsp)
              vr(i,l)=  vxr(i,l,itype,jsp)      
            END IF
          END DO
        END DO

          
        ! Calculate MT contribution to vxc matrix elements
        ! Precompute auxiliary radial integrals
        DO ilharm = 0,nlharm
          i = 0
          DO l1 = 0,atoms%lmax(itype)
            DO p1 = 1,2
              i = i + 1
              j = 0
              DO l2 = 0,atoms%lmax(itype)     
                DO p2 = 1,2
                  j = j + 1
                  IF( j .le. i) THEN
                    DO igrid = 1,atoms%jri(itype)
                      grid(igrid)=vr(igrid,ilharm)*(bas1(igrid,p1,l1,itype)*bas1(igrid,p2,l2,itype)+ bas2(igrid,p1,l1,itype)*bas2(igrid,p2,l2,itype) )
                    END DO

                    CALL intgr3(grid,atoms%rmsh(:,itype),atoms%dx(itype),atoms%jri(itype),integ(ilharm,p1,l1,p2,l2) ) ! numerical integration
            
                    integ(ilharm,p2,l2,p1,l1)=integ(ilharm,p1,l1,p2,l2)
                  END IF
                END DO
              END DO
            
            END DO
          END DO
        END DO
        
        ! Calculate muffin tin contribution to vxc matrix
        vrmat=0
        
        j1=0
        DO l1 = 0,atoms%lmax(itype) ! loop: left basis function
          DO m1 = -l1,l1
            DO p1 = 1,2
              j1 = j1+1
              j2 = 0
              DO l2 = 0,atoms%lmax(itype) ! loop: right basis function
                DO m2 = -l2,l2
                  DO p2 = 1,2
                    j2 = j2+1
                    rr = 0
                    DO ilharm = 0,nlharm ! loop: lattice harmonics of vxc
                      l = sphhar%llh(ilharm,typsym)
                      DO i = 1,sphhar%nmem(ilharm,typsym)
                        M  = sphhar%mlh(i,ilharm,typsym)
                        rc = sphhar%clnu(i,ilharm,typsym)* gaunt1(l1,l,l2,m1,M,m2,atoms%lmaxd)
                        rr = rr+integ(ilharm,p1,l1,p2,l2)*rc
                      END DO
                    END DO

                    rc           = cmplx(0,1)**(l2-l1) ! adjusts to a/b/ccof-scaling
                    vrmat(j1,j2) = rr*rc
              
                  END DO
                END DO
              END DO

            END DO
          END DO
        END DO
        nnbas = j1

!        ! Project on bascof
        DO ineq = 1,atoms%neq(itype)
          iatom = iatom+1

          carr (:nnbas,:lapw%nv(jsp)) = conjg(matmul(vrmat(:nnbas,:nnbas), transpose(bascof(:lapw%nv(jsp),:nnbas,iatom)) ))

          carr1(:lapw%nv(jsp),:lapw%nv(jsp)) = matmul(bascof(:lapw%nv(jsp),:nnbas,iatom),carr(:nnbas,:lapw%nv(jsp)) )
          ic    = 0
          DO j = 1,lapw%nv(jsp)
!            carr(:nnbas) =  matmul(vrmat(:nnbas,:nnbas),
!     +                             bascof(j,:nnbas,iatom) )
            DO i = 1,j
             ic = ic + 1
              vxc(ic) = vxc(ic) + carr1(i,j)
!             vxc(ic) = vxc(ic) + conjg(dotprod ( bascof(i,:nnbas,iatom),
!     +                                           carr(:nnbas) ))
            END DO
          END DO
        END DO
        
      END DO ! End loop over atom types

      ! ---------------------------------------------------------------
      ! Calculate plane wave contribution
      DO i=1,stars%ng3
        vpw(i)= vxpw(i,jsp) 
!         vpw(i)=vpwtot(i)-vpwcou(i,jsp)      
      END DO
      
      ! Calculate vxc-matrix,  left basis function (ig1)
      !                        right basis function (ig2)
      ic = 0
      DO ig1=1,lapw%nv(jsp)
        DO ig2=1,ig1
          ic = ic + 1
          gg(1)=lapw%k1(ig1,jsp)-lapw%k1(ig2,jsp)
          gg(2)=lapw%k2(ig1,jsp)-lapw%k2(ig2,jsp)
          gg(3)=lapw%k3(ig1,jsp)-lapw%k3(ig2,jsp)
          istar=stars%ig(gg(1),gg(2),gg(3))
          IF(istar.ne.0) THEN
            vxc(ic)= vxc(ic) + stars%rgphs(gg(1),gg(2),gg(3))*vpw(istar)
          ELSE
            IF ( mpi%irank == 0 ) WRITE(6,'(A,/6I5)') 'Warning: Gi-Gj not in any star:',&
              lapw%k1(ig1,jsp),lapw%k2(ig1,jsp),lapw%k3(ig1,jsp),&
              lapw%k1(ig2,jsp),lapw%k2(ig2,jsp),lapw%k3(ig2,jsp)
          ENDIF
        ENDDO
      ENDDO

      !    
      ! -------------------------------------------------------------------
      ! Calculate local orbital contribution

      IF( any( atoms%nlo .ne. 0) ) THEN 

        nbasf0      = lapw%nv(jsp)*(lapw%nv(jsp)+1)/2    ! number of pure APW contributions
        icentry     = nbasf0                   ! icentry counts the entry in the matrix vxc
        iatom       = 0
        ikvecat     = 0
        ikvecprevat = 0

        DO itype=1,atoms%ntype
         
          typsym = atoms%ntypsy(sum(atoms%neq(:itype-1))+1)
          nlharm = sphhar%nlh(typsym)

          ! Calculate vxc = vtot - vcoul
          DO l=0,nlharm
            DO i=1,atoms%jri(itype)
              IF(l.eq.0) THEN
!                 vr(i,0)= vrtot(i,0,itype)*sfp/rmsh(i,itype) -  vrcou(i,0,itype,jsp)
                vr(i,0)=  vxr(i,0,itype,jsp)*sfp_const/atoms%rmsh(i,itype)  !
              ELSE                                              ! vxc = vtot - vcoul
                vr(i,l)=  vxr(i,l,itype,jsp)                    !
!                 vr(i,l)=  vrtot(i,l,itype)-vrcou(i,l,itype,jsp)
              END IF
            END DO
          END DO

          ! Precompute auxiliary radial integrals
          DO ilharm=0,nlharm
            i = 0
            DO l1=0,atoms%lmax(itype)
              DO p1=1,hybrid%nindx(l1,itype)
                i = i + 1
                j = 0
                DO l2=0,atoms%lmax(itype)     
                  DO p2=1,hybrid%nindx(l2,itype)
                    j = j + 1
                    IF( j .le. i) THEN
                      DO igrid=1,atoms%jri(itype)
                        grid(igrid)=vr(igrid,ilharm)*(bas1(igrid,p1,l1,itype)*bas1(igrid,p2,l2,itype)+ bas2(igrid,p1,l1,itype)*bas2(igrid,p2,l2,itype))
                      END DO

                      CALL intgr3(grid,atoms%rmsh(:,itype),atoms%dx(itype),atoms%jri(itype),integ(ilharm,p1,l1,p2,l2) ) ! numerical integration
            
                      integ(ilharm,p2,l2,p1,l1) = integ(ilharm,p1,l1,p2,l2)
                    END IF
                  END DO
                END DO
            
              END DO
            END DO
          END DO


          DO ieq = 1,atoms%neq(itype)
            iatom = iatom + 1
            IF( (atoms%invsat(iatom).eq.0) .or. (atoms%invsat(iatom) .eq. 1) ) THEN
              
              IF( atoms%invsat(iatom) .eq. 0 ) invsfct = 1
              IF( atoms%invsat(iatom) .eq. 1 ) invsfct = 2
              

              DO ilo = 1,atoms%nlo(itype)
                l1 = atoms%llo(ilo,itype)
                DO ikvec = 1,invsfct*(2*l1+1)
                  
                  DO m1 = -l1,l1
                    DO p1 = 1,3
                      IF( p1 .eq. 3) THEN
                        pp1 = pointer_lo(ilo,itype)
                      ELSE
                        pp1 = p1
                      END IF

                      IF( hybrid%nindx(l1,itype) .le. 2) STOP 'subvxc: error hybrid%nindx'
                
                      lm = 0
                      
                      !loop over APW
                      DO l2 = 0,atoms%lmax(itype)
                        DO m2 = -l2,l2
                          DO p2 = 1,2
                            lm = lm + 1

                            rr = 0
                            DO ilharm = 0,nlharm
                              lh = sphhar%llh(ilharm,typsym)
                              DO i = 1,sphhar%nmem(ilharm,typsym)
                                mh = sphhar%mlh(i,ilharm,typsym)
                                rc = sphhar%clnu(i,ilharm,typsym)* gaunt1(l1,lh,l2,m1,mh,m2,atoms%lmaxd)
                                rr = rr+integ(ilharm,p2,l2,pp1,l1)*rc
                              END DO
                            END DO

                            rc = cmplx(0d0,1d0)**(l2-l1) ! adjusts to a/b/ccof-scaling

                            ! ic counts the entry in vxc
                            ic = icentry
                            DO i=1,lapw%nv(jsp)
                              ic = ic + 1
#ifdef CPP_INVERSION
                              vxc(ic) = vxc(ic) + invsfct * real(rr*rc*bascof(i,lm,iatom) * conjg(bascof_lo(p1,m1,ikvec,ilo, iatom)))
#else
                              vxc(ic) = vxc(ic) + rr*rc*bascof(i,lm,iatom) *conjg(bascof_lo(p1,m1,ikvec,ilo, iatom))
#endif
                            END DO
                        
                          END DO  !p2
                        END DO  ! m2
                      END DO ! l2 ->  loop over APW


                      ! calcualte matrix-elements with local orbitals at the same atom
                      IF( ic .ne. icentry + lapw%nv(jsp) ) STOP 'subvxc: error counting ic'
                      
                      ic = ic + ikvecprevat

                      DO ilop = 1,(ilo-1)
                        lp = atoms%llo(ilop,itype)
                        
                        DO ikvecp = 1,invsfct*(2*lp+1)
                        
                          ic = ic + 1

                          DO mp = -lp,lp
                            DO pp = 1,3
                              IF ( pp .eq. 3) THEN
                                pp2 = pointer_lo(ilop,itype)
                              ELSE
                                pp2 = pp
                              END IF

                              rr = 0
                              DO ilharm = 0,nlharm
                                lh = sphhar%llh(ilharm,typsym)
                                DO i = 1,sphhar%nmem(ilharm,typsym)
                                  mh = sphhar%mlh(i,ilharm,typsym)
                                  rc = sphhar%clnu(i,ilharm,typsym)* gaunt1(l1,lh,lp,m1,mh,mp,atoms%lmaxd)
                                  rr = rr+integ(ilharm,pp2,lp,pp1,l1)*rc
                                END DO
                              END DO

                              rc = cmplx(0d0,1d0)**(lp-l1) ! adjusts to a/b/ccof-scaling

#ifdef CPP_INVERSION
                              vxc(ic) = vxc(ic) + invsfct * real( rr*rc*bascof_lo(pp,mp, ikvecp,ilop,iatom) * conjg(bascof_lo(p1,m1,ikvec,ilo, iatom)) )
#else
                              vxc(ic) = vxc(ic) + rr*rc*bascof_lo(pp,mp,ikvecp, ilop,iatom) *conjg(bascof_lo(p1,m1,ikvec,ilo, iatom))
#endif                            
                            END DO ! pp
                          END DO ! mp
                          
                        END DO !ikvecp
                      END DO ! ilop
                                                            
                      ! calculate matrix-elements of one local orbital with itself

                      DO ikvecp = 1,ikvec
                        ic = ic + 1

                        lp   = l1
                        ilop = ilo
                        DO mp = -lp,lp
                          DO pp = 1,3
                            IF ( pp .eq. 3) THEN
                              pp2 = pointer_lo(ilop,itype)
                            ELSE
                              pp2 = pp
                            END IF

                            rr = 0
                            DO ilharm = 0,nlharm
                              lh = sphhar%llh(ilharm,typsym)
                              DO i = 1,sphhar%nmem(ilharm,typsym)
                                mh = sphhar%mlh(i,ilharm,typsym)
                                rc = sphhar%clnu(i,ilharm,typsym)* gaunt1(l1,lh,lp,m1,mh,mp,atoms%lmaxd)
                                rr = rr+integ(ilharm,pp2,lp,pp1,l1)*rc
                              END DO
                            END DO

                            rc = cmplx(0d0,1d0)**(lp-l1) ! adjusts to a/b/ccof-scaling

#ifdef CPP_INVERSION
                            vxc(ic) = vxc(ic) + invsfct*real( rr*rc* bascof_lo(pp,mp,ikvecp,ilop,iatom) * conjg(bascof_lo(p1,m1,ikvec,ilo, iatom)) )
#else
                            vxc(ic) = vxc(ic) + rr*rc*bascof_lo(pp,mp,ikvecp,ilop, iatom) * conjg(bascof_lo(p1,m1,ikvec,ilo, iatom))
#endif                            
                          END DO ! pp
                        END DO ! mp

                      END DO ! ikvecp
                      
                      
                    END DO  ! p1
                  END DO  ! m1
                
                  icentry = ic       
                END DO !ikvec
                ikvecat = ikvecat + invsfct*(2*l1+1)
              END DO  ! ilo
              ikvecprevat = ikvecprevat + ikvecat
              ikvecat     = 0
            END IF  ! atoms%invsat(iatom)
            
          END DO ! ieq
        END DO !itype
      
      END IF ! if any atoms%llo

      !initialize weighting factor
      IF( xcpot%icorr .eq. icorr_hf) THEN
        a_ex = amix_hf
      ELSE  IF( xcpot%icorr .eq. icorr_pbe0 ) THEN
        a_ex = amix_pbe0
      ELSE IF ( xcpot%icorr .eq. icorr_hse ) THEN
        a_ex = aMix_HSE
      ELSE IF ( xcpot%icorr .eq. icorr_vhse ) THEN
        a_ex = aMix_VHSE()
      ELSE
        STOP 'subvxc: error icorr'
      END IF

Daniel Wortmann's avatar
Daniel Wortmann committed
556 557 558 559 560 561 562 563 564
      IF (hamovlp%l_real) THEN
         DO i=1,hamovlp%matsize
            hamovlp%a_r(i) = hamovlp%a_r(i) - a_ex*real(vxc(i))
         ENDDO
      ELSE
         DO i=1,hamovlp%matsize
            hamovlp%a_c(i) = hamovlp%a_c(i) - a_ex*vxc(i)
         ENDDO
      ENDIF
565 566 567 568 569 570 571 572

      CALL timestop("subvxc")

      DEALLOCATE( bascof )

      END SUBROUTINE subvxc

      END MODULE m_subvxc