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


    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_types
    IMPLICIT NONE
17
    TYPE(t_xcpot_inbuild),INTENT(IN)     :: xcpot
Daniel Wortmann's avatar
Daniel Wortmann committed
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 67 68 69 70 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
    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
    TYPE(t_usdus),INTENT(INOUT)  :: usdus
    TYPE(t_potden),INTENT(IN)    :: vx
    !     .. Scalar Arguments ..

    INTEGER, INTENT (IN) :: jsp 



    !     .. Array Arguments ..
    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)
    TYPE(t_hamovlp),INTENT(INOUT)::hamovlp

    !     .. Local Scalars ..
    INTEGER               ::  ic,indx,m,ig1,ig2
    INTEGER               ::  nlharm,nnbas,typsym,lm
    INTEGER               ::  noded,nodeu
    INTEGER               ::  nbasf0
    INTEGER               ::  i,j,l,ll,l1,l2 ,m1,m2  ,j1,j2
    INTEGER               ::  ok,p1,p2,lh,mh,pp1,pp2
    INTEGER               ::  igrid,itype,ilharm,istar
    INTEGER               ::  ineq,iatom,ilo,ilop,ieq,icentry
    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                  ::  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               ::  vpw(stars%ng3)
    COMPLEX               ::  vxc(hamovlp%matsize)
    COMPLEX               ::  vrmat(hybrid%maxlmindx,hybrid%maxlmindx)
    COMPLEX               ::  carr(hybrid%maxlmindx,DIMENSION%nvd),carr1(DIMENSION%nvd,DIMENSION%nvd)
    COMPLEX ,ALLOCATABLE  ::  ahlp(:,:,:),bhlp(:,:,:)
    COMPLEX, ALLOCATABLE  ::  bascof(:,:,:)
    COMPLEX               ::  bascof_lo(3,-atoms%llod:atoms%llod,4*atoms%llod+2,atoms%nlod, atoms%nat)



    CALL timestart("subvxc")
    vxc=0


    !  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
109
       END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
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
       !  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
144 145


Daniel Wortmann's avatar
Daniel Wortmann committed
146
    ! compute APW coefficients
147

Daniel Wortmann's avatar
Daniel Wortmann committed
148 149 150
    !  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'
151

Daniel Wortmann's avatar
Daniel Wortmann committed
152
    CALL abcof3( input,atoms,sym,jsp,cell, bk,lapw,&
153
         usdus,oneD,ahlp,bhlp,bascof_lo)
154

Daniel Wortmann's avatar
Daniel Wortmann committed
155 156 157 158
    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
159

Daniel Wortmann's avatar
Daniel Wortmann committed
160 161
    DO itype=1,atoms%ntype
       DO ieq=1,atoms%neq(itype)
162 163 164
          ic   = ic + 1
          indx = 0
          DO l=0,atoms%lmax(itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
165 166 167 168 169 170 171 172 173 174 175 176
             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
177
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
178 179 180 181
       END DO
    END DO

    DEALLOCATE( ahlp,bhlp )
182

Daniel Wortmann's avatar
Daniel Wortmann committed
183 184 185
    ! Loop over atom types
    iatom = 0
    DO itype = 1,atoms%ntype
186

Daniel Wortmann's avatar
Daniel Wortmann committed
187 188
       typsym = atoms%ntypsy( SUM(atoms%neq(:itype-1))+1 )
       nlharm = sphhar%nlh(typsym)
189

Daniel Wortmann's avatar
Daniel Wortmann committed
190 191
       ! Calculate vxc = vtot - vcoul
       DO l=0,nlharm
192
          DO i=1,atoms%jri(itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
193 194 195 196 197 198 199
             IF(l.EQ.0) THEN
                !               vr(i,0)= vrtot(i,0,itype)*sfp/rmsh(i,itype) -  vrcou(i,0,itype,jsp)   
                vr(i,0)=  vx%mt(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)=  vx%mt(i,l,itype,jsp)      
             END IF
200
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
201 202
       END DO

203

Daniel Wortmann's avatar
Daniel Wortmann committed
204 205 206
       ! Calculate MT contribution to vxc matrix elements
       ! Precompute auxiliary radial integrals
       DO ilharm = 0,nlharm
207 208
          i = 0
          DO l1 = 0,atoms%lmax(itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
             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
225
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
226 227

             END DO
228
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
229 230 231 232 233 234 235
       END DO

       ! Calculate muffin tin contribution to vxc matrix
       vrmat=0

       j1=0
       DO l1 = 0,atoms%lmax(itype) ! loop: left basis function
236
          DO m1 = -l1,l1
Daniel Wortmann's avatar
Daniel Wortmann committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
             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
256

Daniel Wortmann's avatar
Daniel Wortmann committed
257 258
                      END DO
                   END DO
259 260
                END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
261
             END DO
262
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
263 264
       END DO
       nnbas = j1
265

Daniel Wortmann's avatar
Daniel Wortmann committed
266 267
       !        ! Project on bascof
       DO ineq = 1,atoms%neq(itype)
268 269
          iatom = iatom+1

Daniel Wortmann's avatar
Daniel Wortmann committed
270
          carr (:nnbas,:lapw%nv(jsp)) = CONJG(MATMUL(vrmat(:nnbas,:nnbas), TRANSPOSE(bascof(:lapw%nv(jsp),:nnbas,iatom)) ))
271

Daniel Wortmann's avatar
Daniel Wortmann committed
272
          carr1(:lapw%nv(jsp),:lapw%nv(jsp)) = MATMUL(bascof(:lapw%nv(jsp),:nnbas,iatom),carr(:nnbas,:lapw%nv(jsp)) )
273 274
          ic    = 0
          DO j = 1,lapw%nv(jsp)
Daniel Wortmann's avatar
Daniel Wortmann committed
275 276 277 278 279 280 281 282
             !            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
283
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
       END DO

    END DO ! End loop over atom types

    ! ---------------------------------------------------------------
    ! Calculate plane wave contribution
    DO i=1,stars%ng3
       vpw(i)= vx%pw(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
300 301 302 303 304
          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))
Daniel Wortmann's avatar
Daniel Wortmann committed
305 306
          IF(istar.NE.0) THEN
             vxc(ic)= vxc(ic) + stars%rgphs(gg(1),gg(2),gg(3))*vpw(istar)
307
          ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
308 309 310
             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)
311
          ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
312 313 314 315 316 317
       ENDDO
    ENDDO

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

Daniel Wortmann's avatar
Daniel Wortmann committed
319
    IF( ANY( atoms%nlo .NE. 0) ) THEN 
320

Daniel Wortmann's avatar
Daniel Wortmann committed
321 322 323 324 325
       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
326

Daniel Wortmann's avatar
Daniel Wortmann committed
327
       DO itype=1,atoms%ntype
328

Daniel Wortmann's avatar
Daniel Wortmann committed
329
          typsym = atoms%ntypsy(SUM(atoms%neq(:itype-1))+1)
330 331 332 333
          nlharm = sphhar%nlh(typsym)

          ! Calculate vxc = vtot - vcoul
          DO l=0,nlharm
Daniel Wortmann's avatar
Daniel Wortmann committed
334 335 336 337 338 339 340 341 342
             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)=  vx%mt(i,0,itype,jsp)*sfp_const/atoms%rmsh(i,itype)  !
                ELSE                                              ! vxc = vtot - vcoul
                   vr(i,l)=  vx%mt(i,l,itype,jsp)                    !
                   !                 vr(i,l)=  vrtot(i,l,itype)-vrcou(i,l,itype,jsp)
                END IF
             END DO
343 344 345 346
          END DO

          ! Precompute auxiliary radial integrals
          DO ilharm=0,nlharm
Daniel Wortmann's avatar
Daniel Wortmann committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
             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
364
                      END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
365
                   END DO
366 367

                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
368
             END DO
369 370 371 372
          END DO


          DO ieq = 1,atoms%neq(itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
373 374
             iatom = iatom + 1
             IF( (atoms%invsat(iatom).EQ.0) .OR. (atoms%invsat(iatom) .EQ. 1) ) THEN
375

Daniel Wortmann's avatar
Daniel Wortmann committed
376 377
                IF( atoms%invsat(iatom) .EQ. 0 ) invsfct = 1
                IF( atoms%invsat(iatom) .EQ. 1 ) invsfct = 2
378

Daniel Wortmann's avatar
Daniel Wortmann committed
379 380 381 382 383 384 385 386 387

                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)
388
                            ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
389
                               pp1 = p1
390 391
                            END IF

Daniel Wortmann's avatar
Daniel Wortmann committed
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
                            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
                                        IF (hamovlp%l_real) THEN
                                           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

                                        IF (hamovlp%l_real) THEN

                                           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

                                     IF (hamovlp%l_real) THEN
                                        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)
522 523

          END DO ! ieq
Daniel Wortmann's avatar
Daniel Wortmann committed
524 525 526 527 528
       END DO !itype

    END IF ! if any atoms%llo

    !initialize weighting factor
529 530
    a_ex=xcpot%get_exchange_weight()
    
Daniel Wortmann's avatar
Daniel Wortmann committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    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

    CALL timestop("subvxc")

    DEALLOCATE( bascof )

  END SUBROUTINE subvxc

END MODULE m_subvxc