coulombmatrix.F90 85.4 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
!
!     Calculates the Coulomb matrix
!
!     v      =  < M    | v | M    >
!      k,IJ        k,I        k,J
!
!     with the mixed-basis functions M (indices I and J).
!
!     Note that
!                 *
!     v      =  v     .
!      k,JI      k,IJ
!
!     In the code: coulomb(IJ,k) = v     where only the upper triangle (I<=J) is stored.
!                                   k,IJ 
!
!     The Coulomb matrix v(IJ,k) diverges at the Gamma-point. Here, we apply the decomposition
!
!              (0)        (1)   *        2-l              (0)*   (0)    (1)*        m  (1)
!     v     = v    + SUM v   * Y  (k) / k        with    v    = v   ,  v      = (-1)  v
!      k,IJ    IJ     lm  IJ    lm                        JI     IJ     JI,lm          IJ,l,-m
!
!     where a = atom index, R  = position vector, T  = Wigner-Seitz radius (scalar).
!                            a                     0
!                                    (0)
!     In the code: coulomb(IJ,1)  = v    where only the upper triangle (I<=J) is stored,
!                                    IJ
!                                    (1)
Daniel Wortmann's avatar
Daniel Wortmann committed
29
!                  coulfac(IJ,lm) = v                                    IJ,lm
30 31 32 33
!
!     For the PW contribution we have to construct plane waves within the MT spheres with the help
!     of spherical Bessel functions. The value lexp (LEXP in gwinp) is the corresponding cutoff.
!
Daniel Wortmann's avatar
Daniel Wortmann committed
34
MODULE m_coulombmatrix
35

Daniel Wortmann's avatar
Daniel Wortmann committed
36
CONTAINS
37 38


Daniel Wortmann's avatar
Daniel Wortmann committed
39
  SUBROUTINE coulombmatrix(mpi,atoms,kpts,cell, sym, hybrid, xcpot,l_restart)
40

41 42
    USE m_types
    USE m_juDFT
Daniel Wortmann's avatar
Daniel Wortmann committed
43 44 45 46 47 48 49
    USE m_constants    , ONLY : pi_const
    USE m_olap         , ONLY : olap_pw,gptnorm
    USE m_trafo        , ONLY : symmetrize,bramat_trafo
    USE m_util         , ONLY : sphbessel,intgrf,intgrf_init, harmonicsr,primitivef
    USE m_hsefunctional, ONLY : change_coulombmatrix
    USE m_wrapper
    USE m_io_hybrid
50

51

Daniel Wortmann's avatar
Daniel Wortmann committed
52 53
    IMPLICIT NONE

54
    TYPE(t_xcpot_inbuild),INTENT(IN)     :: xcpot
Daniel Wortmann's avatar
Daniel Wortmann committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    TYPE(t_mpi),INTENT(IN)       :: mpi
    TYPE(t_hybrid),INTENT(INOUT) :: hybrid
    TYPE(t_sym),INTENT(IN)       :: sym
    TYPE(t_cell),INTENT(IN)      :: cell
    TYPE(t_kpts),INTENT(IN)      :: kpts
    TYPE(t_atoms),INTENT(IN)     :: atoms

    ! - scalars -
    LOGICAL    , INTENT(IN)    :: l_restart


    ! - local scalars -
    INTEGER                    :: inviop
    INTEGER                    :: nqnrm,iqnrm,iqnrm1,iqnrm2, iqnrmstart,iqnrmstep
    INTEGER                    :: itype,l ,ix,iy,iy0,i,j,lm,l1,l2,m1, m2,ineq,idum,ikpt,ikpt0,ikpt1
    INTEGER                    :: lm1,lm2,itype1,itype2,ineq1,ineq2,n, n1,n2,ng
    INTEGER                    :: ic,ic1,ic2,ic3,ic4,ic5,ic6,ic7,ic8
    INTEGER                    :: igpt,igpt1,igpt2,igptp,igptp1,igptp2
    INTEGER                    :: isym,isym1,isym2,igpt0
74
    INTEGER                    :: ok
Daniel Wortmann's avatar
Daniel Wortmann committed
75 76
    INTEGER                    :: m
    INTEGER                    :: ikptmin,ikptmax,nkminmax
77
    INTEGER                    :: maxfac
Daniel Wortmann's avatar
Daniel Wortmann committed
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

    LOGICAL                    :: lsym

    REAL                       :: rdum,rdum1,rdum2
    REAL                       :: svol,qnorm,qnorm1,qnorm2,gnorm
    REAL                       :: fcoulfac
    REAL                       :: time1,time2

    COMPLEX                    :: cdum,cdum1,cexp,csum

    ! - local arrays -
    INTEGER                    :: g(3)
    INTEGER                    :: nbasm1(kpts%nkptf)
    INTEGER    , ALLOCATABLE   :: pqnrm(:,:)
    INTEGER                    :: rrot(3,3,sym%nsym),invrrot(3,3,sym%nsym)
    INTEGER    , ALLOCATABLE   :: iarr(:),POINTER(:,:,:,:)!,pointer(:,:,:)
    INTEGER                    :: igptmin(kpts%nkpt),igptmax(kpts%nkpt)
    INTEGER    , ALLOCATABLE   :: nsym_gpt(:,:), sym_gpt(:,:,:)
    INTEGER                    :: nsym1(kpts%nkpt+1), sym1(sym%nsym,kpts%nkpt+1)

    LOGICAL                    :: calc_mt(kpts%nkpt)

    REAL                       :: q(3),q1(3),q2(3)
    REAL                       :: integrand(atoms%jmtd),primf1(atoms%jmtd), primf2(atoms%jmtd)
    REAL                       :: mat(hybrid%maxindxm1*(hybrid%maxindxm1+1)/2)
    REAL                       :: moment(hybrid%maxindxm1,0:hybrid%maxlcutm1,atoms%ntype), moment2(hybrid%maxindxm1,atoms%ntype)
    REAL                       :: sphbes(atoms%jmtd,0:hybrid%maxlcutm1)
    REAL                       :: sphbesmoment1(atoms%jmtd,0:hybrid%maxlcutm1)
    REAL                       :: rarr(0:hybrid%lexp+1),rarr1(0:hybrid%maxlcutm1)
    REAL       , ALLOCATABLE   :: gmat(:,:),qnrm(:)
    REAL       , ALLOCATABLE   :: sphbesmoment(:,:,:)
    REAL       , ALLOCATABLE   :: sphbes0(:,:,:)   
    REAL       , ALLOCATABLE   :: olap(:,:,:,:),integral(:,:,:,:)
    REAL       , ALLOCATABLE   :: gridf(:,:)
112 113 114
    REAL                       :: facA(0:MAX(2*atoms%lmaxd+hybrid%maxlcutm1+1,4*MAX(hybrid%maxlcutm1,hybrid%lexp)+1))
    REAL                       :: facB(0:MAX(2*atoms%lmaxd+hybrid%maxlcutm1+1,4*MAX(hybrid%maxlcutm1,hybrid%lexp)+1))
    REAL                       :: facC(-1:MAX(2*atoms%lmaxd+hybrid%maxlcutm1+1,4*MAX(hybrid%maxlcutm1,hybrid%lexp)+1))
Daniel Wortmann's avatar
Daniel Wortmann committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    COMPLEX                    :: cexp1(atoms%ntype),csumf(9)
    COMPLEX                    :: structconst((2*hybrid%lexp+1)**2 ,atoms%nat,atoms%nat, kpts%nkpt)             ! nw = 1
    COMPLEX                    :: y((hybrid%lexp+1)**2),y1((hybrid%lexp+1)**2), y2((hybrid%lexp+1)**2)
    COMPLEX                    :: dwgn(-hybrid%maxlcutm1:hybrid%maxlcutm1, -hybrid%maxlcutm1:hybrid%maxlcutm1, 0:hybrid%maxlcutm1,sym%nsym)
    COMPLEX    , ALLOCATABLE   :: smat(:,:)
    COMPLEX    , ALLOCATABLE   :: coulmat(:,:)
    COMPLEX    , ALLOCATABLE   :: carr2(:,:),carr2a(:,:), carr2b(:,:)
    COMPLEX    , ALLOCATABLE   :: structconst1(:,:)
    REAL       , ALLOCATABLE   :: coulomb_mt1(:,:,:,:,:)

    !REAL       , ALLOCATABLE   :: coulomb(:,:) !At the moment we always calculate a complex coulomb matrix
    REAL       , ALLOCATABLE   :: coulomb_mt2_r(:,:,:,:,:), coulomb_mt3_r(:,:,:,:)
    REAL       , ALLOCATABLE   :: coulomb_mtir_r(:,:,:), coulombp_mtir_r(:,:)
    COMPLEX   , ALLOCATABLE   :: coulomb(:,:)
    COMPLEX   , ALLOCATABLE   :: coulomb_mt2_c(:,:,:,:,:), coulomb_mt3_c(:,:,:,:)
    COMPLEX   , ALLOCATABLE   :: coulomb_mtir_c(:,:,:), coulombp_mtir_c(:,:)

    INTEGER                    :: ishift,ishift1
    INTEGER                    :: iatom,iatom1
    INTEGER                    :: indx1,indx2,indx3,indx4
    LOGICAL                    :: l_found,l_warn,l_warned, l_plot = .FALSE.!.true.!.false.
    TYPE(t_mat)                :: olapm,coulhlp
138

139
    CALL timestart("Coulomb matrix setup")
140

Daniel Wortmann's avatar
Daniel Wortmann committed
141 142
    svol     = SQRT(cell%vol)
    fcoulfac = 4*pi_const/cell%vol
143 144 145 146 147 148 149 150 151
    maxfac   = MAX(2*atoms%lmaxd+hybrid%maxlcutm1+1,4*MAX(hybrid%maxlcutm1,hybrid%lexp)+1)

    facA(0)       = 1                    !
    facB(0)      = 1                    ! Define:
    facC(-1:0) = 1                    ! facA(i)    = i!
    DO i=1,maxfac                       ! facB(i)   = sqrt(i!)
       facA(i)    = facA(i-1)*i            ! facC(i) = (2i+1)!!
       facB(i)   = facB(i-1)*SQRT(i*1d0) !
       facC(i) = facC(i-1)*(2*i+1)   !
Daniel Wortmann's avatar
Daniel Wortmann committed
152
    END DO
153

154
    CALL intgrf_init(atoms%ntype,atoms%jmtd,atoms%jri,atoms%dx,atoms%rmsh,gridf)
155

156
    nbasm1    = hybrid%nbasp  + hybrid%ngptm(:)
157

Daniel Wortmann's avatar
Daniel Wortmann committed
158 159
    !     Calculate the structure constant
    CALL structureconstant(structconst,cell,hybrid, atoms,kpts, mpi)
160 161 162 163




Daniel Wortmann's avatar
Daniel Wortmann committed
164
    IF ( mpi%irank == 0 ) WRITE(6,'(//A)') '### subroutine: coulombmatrix ###'
165

Daniel Wortmann's avatar
Daniel Wortmann committed
166 167 168
    !
    !     Matrix allocation
    !
169

Daniel Wortmann's avatar
Daniel Wortmann committed
170
    IF(ALLOCATED(coulomb)) DEALLOCATE (coulomb)
171

Daniel Wortmann's avatar
Daniel Wortmann committed
172 173 174
    ALLOCATE ( coulomb(hybrid%maxbasm1*(hybrid%maxbasm1+1)/2,kpts%nkpt) , stat = ok )
    IF( ok .NE. 0 ) STOP 'coulombmatrix: failure allocation coulomb matrix'
    coulomb = 0
175

Daniel Wortmann's avatar
Daniel Wortmann committed
176
    IF ( mpi%irank == 0 ) WRITE(6,'(/A,F6.1," MB")') 'Size of coulomb matrix:',16d0/1048576*SIZE(coulomb)
177

Daniel Wortmann's avatar
Daniel Wortmann committed
178 179
    !     Generate Symmetry:
    !     Reduce list of g-Points so that only one of each symm-equivalent is calculated
180

181
#ifndef CPP_NOCOULSYM
Daniel Wortmann's avatar
Daniel Wortmann committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    IF ( mpi%irank == 0 ) WRITE(6,'(/A)',advance='no') 'Setup for symmetry...'
    CALL cpu_TIME(time1)
    ! calculate rotations in reciprocal space
    DO isym = 1,sym%nsym
       IF( isym .LE. sym%nop ) THEN
          inviop         = sym%invtab(isym)
          rrot(:,:,isym) = TRANSPOSE(sym%mrot(:,:,inviop))
          DO l = 0,hybrid%maxlcutm1
             dwgn(:,:,l,isym) = TRANSPOSE(hybrid%d_wgn2(-hybrid%maxlcutm1:hybrid%maxlcutm1,-hybrid%maxlcutm1:hybrid%maxlcutm1,l,isym) )
          END DO
       ELSE
          inviop           = isym - sym%nop
          rrot(:,:,isym)   = -rrot(:,:,inviop)
          dwgn(:,:,:,isym) = dwgn(:,:,:,inviop)
          DO l = 0,hybrid%maxlcutm1
             DO m1 = -l,l
198
                DO m2 = -l,-1
Daniel Wortmann's avatar
Daniel Wortmann committed
199 200 201
                   cdum                = dwgn(m1, m2,l,isym)
                   dwgn(m1, m2,l,isym) = dwgn(m1,-m2,l,isym) * (-1)**m2
                   dwgn(m1,-m2,l,isym) = cdum                * (-1)**m2
202
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
203
             END DO
204
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
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
       END IF
    END DO
    invrrot(:,:,:sym%nop)   = rrot(:,:,sym%invtab)
    IF (sym%nsym > sym%nop) THEN
       invrrot(:,:,sym%nop+1:) = rrot(:,:,sym%invtab+sym%nop)
    END IF

    ! Get symmetry operations that leave bk(:,ikpt) invariant -> sym1
    nsym1 = 0
    DO ikpt = 1,kpts%nkpt
       isym1 = 0
       DO isym = 1,sym%nsym
          ! temporary fix until bramat_trafo is correct
          ! for systems with symmetries including translations
          IF ( isym > sym%nop ) THEN
             isym2 = isym-sym%nop
          ELSE
             isym2 = isym
          END IF
          IF ( ANY(sym%tau(:,isym2) /= 0) ) CYCLE

          IF(ALL(ABS(MATMUL(rrot(:,:,isym),kpts%bk(:,ikpt)) -kpts%bk(:,ikpt)).LT.1d-12)) THEN
             isym1            = isym1 + 1
             sym1(isym1,ikpt) = isym
          END IF
       END DO
       nsym1(ikpt) = isym1
    END DO
    ! Define reduced lists of G points -> pgptm1(:,ikpt), ikpt=1,..,nkpt
    !ALLOCATE ( hybrid%pgptm1(hybrid%maxgptm,kpts%nkpt)) !in mixedbasis
    ALLOCATE (iarr(hybrid%maxgptm), POINTER(kpts%nkpt,&
         MINVAL(hybrid%gptm(1,:))-1:MAXVAL(hybrid%gptm(1,:))+1,&
         MINVAL(hybrid%gptm(2,:))-1:MAXVAL(hybrid%gptm(2,:))+1,&
         MINVAL(hybrid%gptm(3,:))-1:MAXVAL(hybrid%gptm(3,:))+1))
    hybrid%pgptm1 = 0 ; iarr = 0 ; POINTER = 0
    DO ikpt = 1,kpts%nkpt
       DO igpt = 1,hybrid%ngptm(ikpt)
          g = hybrid%gptm(:,hybrid%pgptm(igpt,ikpt))
          POINTER(ikpt,g(1),g(2),g(3)) = igpt
       END DO
       iarr = 0
       j    = 0
       DO igpt = hybrid%ngptm(ikpt),1,-1
          IF (iarr(igpt).EQ.0) THEN
             j              = j + 1
             hybrid%pgptm1(j,ikpt) = igpt
             DO isym1 = 1,nsym1(ikpt)
                g = MATMUL ( rrot(:,:,sym1(isym1,ikpt)) , hybrid%gptm(:,hybrid%pgptm(igpt,ikpt)) )
                i = POINTER(ikpt,g(1),g(2),g(3))
                IF(i.EQ.0) STOP 'coulombmatrix: zero pointer (bug?)'
255
                iarr(i) = 1
Daniel Wortmann's avatar
Daniel Wortmann committed
256 257 258 259 260 261
             END DO
          END IF
       END DO
       hybrid%ngptm1(ikpt) = j
    END DO
    DEALLOCATE ( iarr )
262

Daniel Wortmann's avatar
Daniel Wortmann committed
263 264 265
    IF ( mpi%irank == 0 ) WRITE(6,'(12X,A)',advance='no') 'done'
    CALL cpu_TIME(time2)
    IF ( mpi%irank == 0 ) WRITE(6,'(2X,A,F8.2,A)') '( Timing:', time2-time1, ' )'
266

Daniel Wortmann's avatar
Daniel Wortmann committed
267
    ! no symmetry used
268
#else 
Daniel Wortmann's avatar
Daniel Wortmann committed
269 270 271 272 273
    ALLOCATE ( hybrid%pgptm1(hybrid%maxgptm,kpts%nkpt) )
    DO ikpt = 1,kpts%nkpt
       hybrid%pgptm1(:,ikpt)    = (/ (igpt0, igpt0 = 1,hybrid%maxgptm) /)
       hybrid%ngptm1(ikpt)      = hybrid%ngptm(ikpt)
    END DO
274
#endif
275

Daniel Wortmann's avatar
Daniel Wortmann committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    ! Distribute the work as equally as possible over the processes
    ikptmin  = 1
    ikptmax  = kpts%nkpt
    igptmin  = 1
    igptmax  = hybrid%ngptm1(:kpts%nkpt)
    calc_mt  = .TRUE.
    nkminmax = kpts%nkpt

    IF ( mpi%irank == 0 ) WRITE(6,'(A)',advance='no') 'Preparations...'
    CALL cpu_TIME(time1)

    ! Define gmat (symmetric)
    i = (hybrid%lexp+1)**2
    ALLOCATE ( gmat(i,i) )
    gmat = 0
    lm1 = 0
    DO l1=0,hybrid%lexp
       DO m1=-l1,l1
294 295
          lm1 = lm1 + 1
          lm2 = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
296 297 298 299
          lp1:DO l2=0,l1
             DO m2=-l2,l2
                lm2 = lm2 + 1
                IF(lm2.GT.lm1) EXIT lp1 ! Don't cross the diagonal!
300 301
                gmat(lm1,lm2) = facB(l1+l2+m2-m1)*facB(l1+l2+m1-m2)/&
                     ( facB(l1+m1)*facB(l1-m1)*facB(l2+m2)*facB(l2-m2) ) /&
Daniel Wortmann's avatar
Daniel Wortmann committed
302 303 304
                     SQRT(1d0*(2*l1+1)*(2*l2+1)*(2*(l1+l2)+1))*(4*pi_const)**1.5d0
                gmat(lm2,lm1) = gmat(lm1,lm2)
             END DO
305
          END DO LP1
Daniel Wortmann's avatar
Daniel Wortmann committed
306 307 308 309 310
       END DO
    END DO
    ! Calculate moments of MT functions
    DO itype=1,atoms%ntype
       DO l=0,hybrid%lcutm1(itype)
311
          DO i=1,hybrid%nindxm1(l,itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
312 313 314
             ! note that hybrid%basm1 already contains the factor rgrid
             moment(i,l,itype) = intgrf(atoms%rmsh(:,itype)**(l+1)*hybrid%basm1(:,i,l,itype),&
                  atoms%jri,atoms%jmtd,atoms%rmsh,atoms%dx,atoms%ntype,itype,gridf)
315
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
316 317
       END DO
       DO i =1,hybrid%nindxm1(0,itype)
318
          moment2(i,itype) = intgrf(atoms%rmsh(:,itype)**3*hybrid%basm1(:,i,0,itype),&
Daniel Wortmann's avatar
Daniel Wortmann committed
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
               atoms%jri,atoms%jmtd,atoms%rmsh,atoms%dx,atoms%ntype,itype,gridf)
       END DO
    END DO
    ! Look for different qnorm = |k+G|, definition of qnrm and pqnrm.
    CALL getnorm(kpts,hybrid%gptm,hybrid%ngptm,hybrid%pgptm, qnrm,nqnrm,pqnrm,cell)
    ALLOCATE ( sphbesmoment(0:hybrid%lexp,atoms%ntype,nqnrm),&
         olap(hybrid%maxindxm1,0:hybrid%maxlcutm1,atoms%ntype,nqnrm),&
         integral(hybrid%maxindxm1,0:hybrid%maxlcutm1,atoms%ntype,nqnrm) )
    sphbes        = 0
    sphbesmoment  = 0
    sphbesmoment1 = 0
    olap          = 0
    integral      = 0

    ! Calculate moments of spherical Bessel functions (for (2) and (3))              (->sphbesmoment)
    ! Calculate overlap of spherical Bessel functions with basis functions (for (2)) (->olap)
    ! Calculate overlap of sphbesmoment1(r,l)         with basis functions (for (2)) (->integral)
    ! We use           sphbes(r,l) = j_l(qr)
    ! and       sphbesmoment1(r,l) = 1/r**(l-1) * INT(0..r) r'**(l+2) * j_l(qr') dr'
    !                                + r**(l+2) * INT(r..S) r'**(1-l) * j_l(qr') dr' .

    iqnrmstart = mpi%irank + 1
    iqnrmstep  = mpi%isize

    DO iqnrm = iqnrmstart,nqnrm,iqnrmstep
       qnorm = qnrm(iqnrm)
       DO itype = 1,atoms%ntype
346 347 348 349
          ng            = atoms%jri(itype)
          rdum          = atoms%rmt(itype)
          sphbes        = 0
          sphbesmoment1 = 0 
Daniel Wortmann's avatar
Daniel Wortmann committed
350 351 352 353 354 355
          IF(qnorm.EQ.0) THEN
             sphbesmoment(0,itype,iqnrm) = rdum**3 / 3
             DO i = 1,ng
                sphbes(i,0)        = 1
                sphbesmoment1(i,0) = atoms%rmsh(i,itype)**2 / 3 + ( rdum**2 - atoms%rmsh(i,itype)**2 ) / 2
             END DO
356
          ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
             CALL sphbessel(rarr,qnorm*rdum,hybrid%lexp+1)
             DO l = 0,hybrid%lexp
                sphbesmoment(l,itype,iqnrm) = rdum**(l+2) * rarr(l+1) / qnorm
             END DO
             DO i = ng,1,-1
                rdum = atoms%rmsh(i,itype)
                CALL sphbessel(rarr,qnorm*rdum,hybrid%lcutm1(itype)+1)
                DO l = 0,hybrid%lcutm1(itype)
                   sphbes(i,l) = rarr(l)
                   IF(l.NE.0) THEN ; rdum1 = -rdum**(1-l) * rarr(l-1)
                   ELSE            ; rdum1 = -COS(qnorm*rdum) / qnorm
                   ENDIF
                   IF(i.EQ.ng)  rarr1(l) = rdum1
                   sphbesmoment1(i,l) = (rdum**(l+2)*rarr(l+1)/rdum**(l+1)&
                        + ( rarr1(l) - rdum1 ) * rdum**l ) / qnorm
                END DO
             END DO
374 375
          END IF
          DO l = 0,hybrid%lcutm1(itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
376 377 378 379 380
             DO n = 1,hybrid%nindxm1(l,itype)
                ! note that hybrid%basm1 already contains one factor rgrid
                olap(n,l,itype,iqnrm)     =  &
                     intgrf(atoms%rmsh(:,itype)*hybrid%basm1(:,n,l,itype)*sphbes(:,l),&
                     atoms%jri,atoms%jmtd,atoms%rmsh,atoms%dx,atoms%ntype,itype,gridf)
381

Daniel Wortmann's avatar
Daniel Wortmann committed
382 383 384
                integral(n,l,itype,iqnrm) = &
                     intgrf(atoms%rmsh(:,itype)*hybrid%basm1(:,n,l,itype)*sphbesmoment1(:,l),&
                     atoms%jri,atoms%jmtd,atoms%rmsh,atoms%dx,atoms%ntype,itype,gridf)
385

Daniel Wortmann's avatar
Daniel Wortmann committed
386
             END DO
387
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
388 389
       END DO
    END DO
390

Daniel Wortmann's avatar
Daniel Wortmann committed
391 392 393 394 395 396
    IF ( mpi%irank == 0 ) THEN
       WRITE(6,'(18X,A)',advance='no') 'done'
       CALL cpu_TIME(time2) 
       WRITE(6,'(2X,A,F8.2,A)',advance='no') '( Timing:',time2-time1,' )'
       WRITE(6,*)
    END IF
397

Daniel Wortmann's avatar
Daniel Wortmann committed
398 399 400
    !
    !     (1) Case < MT | v | MT >
    !
401

Daniel Wortmann's avatar
Daniel Wortmann committed
402
    IF( mpi%irank == 0 ) WRITE(6,'(A)',advance='no') '< MT | v | MT > contribution...'
403

Daniel Wortmann's avatar
Daniel Wortmann committed
404
    CALL cpu_TIME(time1)
405

Daniel Wortmann's avatar
Daniel Wortmann committed
406
    IF ( ANY( calc_mt ) ) THEN
407

Daniel Wortmann's avatar
Daniel Wortmann committed
408
       !       (1a) r,r' in same MT
409

Daniel Wortmann's avatar
Daniel Wortmann committed
410 411 412 413
       ix  = 0
       iy  = 0
       iy0 = 0
       DO itype=1,atoms%ntype
414
          DO ineq=1,atoms%neq(itype) 
Daniel Wortmann's avatar
Daniel Wortmann committed
415 416 417 418 419 420 421
             ! Here the diagonal block matrices do not depend on ineq. In (1b) they do depend on ineq, though,
             DO l=0,hybrid%lcutm1(itype)
                DO n2=1,hybrid%nindxm1(l,itype)
                   ! note that hybrid%basm1 already contains the factor rgrid
                   CALL primitivef(primf1,hybrid%basm1(:,n2,l,itype)&
                        *atoms%rmsh(:,itype)**(l+1),atoms%rmsh,atoms%dx,atoms%jri,atoms%jmtd,itype,atoms%ntype)
                   ! -itype is to enforce inward integration
422 423
                   CALL primitivef(primf2,hybrid%basm1(:atoms%jri(itype),n2,l,itype)&
                        /atoms%rmsh(:atoms%jri(itype),itype)**l,atoms%rmsh,atoms%dx,atoms%jri,atoms%jmtd,-itype,atoms%ntype)
Daniel Wortmann's avatar
Daniel Wortmann committed
424

425
                   primf1(:atoms%jri(itype)) = primf1(:atoms%jri(itype)) / atoms%rmsh(:atoms%jri(itype),itype)**l
Daniel Wortmann's avatar
Daniel Wortmann committed
426 427 428 429 430 431 432 433 434
                   primf2 = primf2 * atoms%rmsh(:,itype)**(l+1)

                   DO n1=1,n2
                      integrand = hybrid%basm1(:,n1,l,itype) * (primf1 + primf2)
                      !                 call intgr0( (4*pimach())/(2*l+1)*integrand,rmsh(1,itype),dx(itype),jri(itype),mat(n2*(n2-1)/2+n1) )
                      mat(n2*(n2-1)/2+n1) = (4*pi_const)/(2*l+1)&
                           * intgrf(integrand,atoms%jri,atoms%jmtd,atoms%rmsh,atoms%dx,&
                           atoms%ntype,itype,gridf)
                   END DO
435 436
                END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
437 438 439 440 441 442 443 444 445 446 447 448 449
                ! distribute mat for m=-l,l on coulomb in block-matrix form
                DO M=-l,l
                   DO n2=1,hybrid%nindxm1(l,itype)
                      ix = ix + 1
                      iy = iy0
                      DO n1=1,n2
                         iy                   = iy + 1
                         i                    = ix*(ix-1)/2+iy
                         j                    = n2*(n2-1)/2+n1
                         coulomb(i,kpts%nkpt) = mat(j)
                      END DO
                   END DO
                   iy0 = ix
450 451
                END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
452
             END DO
453
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
454
       END DO
455

Daniel Wortmann's avatar
Daniel Wortmann committed
456
       !       (1b) r,r' in different MT
457

Daniel Wortmann's avatar
Daniel Wortmann committed
458 459 460
       ALLOCATE( coulmat(hybrid%nbasp,hybrid%nbasp), stat=ok)
       IF( ok .NE. 0 ) STOP 'coulombmatrix: failure allocation coulmat'
       coulmat = 0
461

Daniel Wortmann's avatar
Daniel Wortmann committed
462
    END IF
463

Daniel Wortmann's avatar
Daniel Wortmann committed
464
    DO ikpt=ikptmin,ikptmax
465

Daniel Wortmann's avatar
Daniel Wortmann committed
466 467
       ! only the first rank handles the MT-MT part
       IF ( calc_mt(ikpt) ) THEN
468 469 470 471

          ix  = 0
          ic2 = 0
          DO itype2=1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
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
             DO ineq2=1,atoms%neq(itype2)
                ic2 = ic2 + 1
                lm2 = 0
                DO l2=0,hybrid%lcutm1(itype2)
                   DO m2=-l2,l2
                      lm2 = lm2 + 1
                      DO n2=1,hybrid%nindxm1(l2,itype2)
                         ix  = ix + 1

                         iy  = 0
                         ic1 = 0
                         lp2: DO itype1=1,itype2
                            DO ineq1=1,atoms%neq(itype1)
                               ic1 = ic1 + 1
                               lm1 = 0
                               DO l1=0,hybrid%lcutm1(itype1)
                                  DO m1=-l1,l1
                                     lm1 = lm1 + 1
                                     DO n1=1,hybrid%nindxm1(l1,itype1)
                                        iy = iy + 1
                                        IF(iy.GT.ix) EXIT lp2 ! Don't cross the diagonal!
                                        rdum            = (-1)**(l2+m2)* moment(n1,l1,itype1)*moment(n2,l2,itype2)*gmat(lm1,lm2)
                                        l               = l1 + l2
                                        lm              = l**2 + l + m1 - m2 + 1
                                        idum            = ix*(ix-1)/2+iy
                                        coulmat(iy,ix)  = coulomb(idum,kpts%nkpt)&
498
                                             + EXP(CMPLX(0.0,1.0)* 2*pi_const * dot_PRODUCT(kpts%bk(:,ikpt),&
Daniel Wortmann's avatar
Daniel Wortmann committed
499 500 501 502 503 504
                                             atoms%taual(:,ic2)-atoms%taual(:,ic1))) *rdum * structconst(lm,ic1,ic2,ikpt)

                                        coulmat(ix,iy)  = CONJG(coulmat(iy,ix))
                                     END DO
                                  END DO
                               END DO
505
                            END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
506
                         END DO lp2
507

Daniel Wortmann's avatar
Daniel Wortmann committed
508 509
                      END DO
                   END DO
510
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
511
             END DO
512 513
          END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
514 515 516 517 518 519
          IF ( sym%invs) THEN
             !symmetrize makes the Coulomb matrix real symmetric
             CALL symmetrize(coulmat,hybrid%nbasp,hybrid%nbasp,3,.FALSE.,&
                  atoms,hybrid%lcutm1,hybrid%maxlcutm1,&
                  hybrid%nindxm1,sym)
          ENDIF
520

Daniel Wortmann's avatar
Daniel Wortmann committed
521
          coulomb(:hybrid%nbasp*(hybrid%nbasp+1)/2,ikpt) = packmat(coulmat)
522

Daniel Wortmann's avatar
Daniel Wortmann committed
523
       END IF
524

Daniel Wortmann's avatar
Daniel Wortmann committed
525 526
    END DO
    IF ( ANY( calc_mt ) ) DEALLOCATE( coulmat )
527

Daniel Wortmann's avatar
Daniel Wortmann committed
528 529 530 531 532 533
    IF ( mpi%irank == 0 ) THEN
       WRITE(6,'(2X,A)',advance='no') 'done'
       CALL cpu_TIME(time2) 
       WRITE(6,'(2X,A,F8.2,A)',advance='no') '( Timing:',time2-time1,' )'
       WRITE(6,*)
    END IF
534

Daniel Wortmann's avatar
Daniel Wortmann committed
535
    IF(hybrid%maxgptm.EQ.0) GOTO 1 ! skip calculation of plane-wave contribution if mixed basis does not contain plane waves
536

Daniel Wortmann's avatar
Daniel Wortmann committed
537 538 539
    !
    !     (2) Case < MT | v | PW >
    !
540

Daniel Wortmann's avatar
Daniel Wortmann committed
541
    IF( mpi%irank == 0 ) WRITE(6,'(A)',advance='no') '< MT | v | PW > contribution...'
542

Daniel Wortmann's avatar
Daniel Wortmann committed
543
    CALL cpu_TIME(time1)
544

Daniel Wortmann's avatar
Daniel Wortmann committed
545 546 547
    !     (2a) r in MT, r' everywhere
    !     (2b) r,r' in same MT
    !     (2c) r,r' in different MT
548

Daniel Wortmann's avatar
Daniel Wortmann committed
549 550 551
    ALLOCATE( coulmat(hybrid%nbasp,hybrid%maxgptm), stat=ok )
    IF( ok .NE. 0 ) STOP 'coulombmatrix: failure allocation coulmat'
    coulmat = 0
552

Daniel Wortmann's avatar
Daniel Wortmann committed
553
    DO ikpt = ikptmin,ikptmax !1,kpts%nkpt
554

Daniel Wortmann's avatar
Daniel Wortmann committed
555
       coulmat = 0
556

Daniel Wortmann's avatar
Daniel Wortmann committed
557 558
       ! start to loop over interstitial plane waves
       DO igpt0 = igptmin(ikpt),igptmax(ikpt) !1,hybrid%ngptm1(ikpt)
559 560
          igpt  = hybrid%pgptm1(igpt0,ikpt)
          igptp = hybrid%pgptm(igpt,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
561 562 563
          ix    = hybrid%nbasp + igpt
          q     = MATMUL ( kpts%bk(:,ikpt) + hybrid%gptm(:,igptp), cell%bmat )
          qnorm = SQRT(SUM(q**2))
564
          iqnrm = pqnrm(igpt,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
565
          IF(ABS(qnrm(iqnrm)-qnorm).GT.1d-12) STOP 'coulombmatrix: qnorm does not equal corresponding & element in qnrm (bug?)' ! We shouldn't stop here!
566

Daniel Wortmann's avatar
Daniel Wortmann committed
567 568
          CALL harmonicsr(y1,MATMUL(kpts%bk(:,kpts%nkpt),cell%bmat),2)
          CALL harmonicsr(y2,MATMUL(hybrid%gptm(:,igptp)     ,cell%bmat),2)
569
          CALL harmonicsr(y,q,hybrid%lexp)
Daniel Wortmann's avatar
Daniel Wortmann committed
570
          y1 = CONJG(y1) ; y2 = CONJG(y2) ; y = CONJG(y)
571 572 573 574

          iy = 0
          ic = 0
          DO itype = 1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588
             DO ineq = 1,atoms%neq(itype)
                ic = ic + 1
                lm = 0
                DO l = 0,hybrid%lcutm1(itype)
                   DO M = -l,l
                      lm = lm + 1

                      ! calculate sum over lm and centers for (2c) -> csum, csumf
                      csum  = 0
                      csumf = 0
                      ic1   = 0
                      DO itype1=1,atoms%ntype
                         DO ineq1=1,atoms%neq(itype1)
                            ic1  = ic1 + 1
589
                            cexp = 4*pi_const * EXP( CMPLX(0.0,1.0) * 2*pi_const&
Daniel Wortmann's avatar
Daniel Wortmann committed
590 591 592 593 594 595 596
                                 * ( dot_PRODUCT( kpts%bk(:,ikpt)+hybrid%gptm(:,igptp),atoms%taual(:,ic1) )&
                                 - dot_PRODUCT( kpts%bk(:,ikpt),atoms%taual(:,ic)  ) ) )

                            lm1 = 0
                            DO l1=0,hybrid%lexp
                               l2   = l + l1 ! for structconst
                               idum = 1
597
                               cdum = sphbesmoment(l1,itype1,iqnrm) * CMPLX(0.0,1.0)**(l1) * cexp
Daniel Wortmann's avatar
Daniel Wortmann committed
598 599 600 601 602 603 604 605 606 607 608
                               DO m1=-l1,l1
                                  lm1  = lm1 + 1
                                  m2   = M - m1              ! for structconst
                                  lm2  = l2**2 + l2 + m2 + 1 !
                                  csum = csum - idum * gmat(lm1,lm) * y(lm1) * cdum * structconst(lm2,ic,ic1,ikpt)
                                  idum = -idum ! factor (-1)*(l1+m1)
                               END DO
                            END DO

                            ! add contribution of (2c) to csum and csumf coming from linear and quadratic orders of Y_lm*(G) / G * j_(l+1)(GS)
                            IF(ikpt.EQ.1.AND.l.LE.2) THEN
609
                               cexp      = EXP(CMPLX(0.0,1.0)*2*pi_const * dot_PRODUCT(hybrid%gptm(:,igptp),atoms%taual(:,ic1)))&
Daniel Wortmann's avatar
Daniel Wortmann committed
610 611
                                    * gmat(lm,1) * 4*pi_const/cell%vol
                               csumf(lm) = csumf(lm) - cexp * SQRT(4*pi_const) *&
612
                                    CMPLX(0.0,1.0)**l * sphbesmoment(0,itype1,iqnrm) / facC(l-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
613 614 615
                               IF(l.EQ.0) THEN
                                  IF(igpt.NE.1) THEN
                                     csum = csum - cexp * ( sphbesmoment(0,itype1,iqnrm)*atoms%rmt(itype1)**2 -&
616
                                          sphbesmoment(2,itype1,iqnrm)*2.0/3 ) / 10
Daniel Wortmann's avatar
Daniel Wortmann committed
617 618 619 620
                                  ELSE
                                     csum = csum - cexp * atoms%rmt(itype1)**5/30
                                  END IF
                               ELSE IF(l.EQ.1) THEN
621
                                  csum = csum + cexp * CMPLX(0.0,1.0) * SQRT(4*pi_const)&
Daniel Wortmann's avatar
Daniel Wortmann committed
622 623 624 625 626
                                       * sphbesmoment(1,itype1,iqnrm) * y(lm) / 3
                               END IF
                            END IF

                         END DO
627 628
                      END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
629 630
                      ! add contribution of (2a) to csumf
                      IF(ikpt.EQ.1.AND.igpt.EQ.1.AND.l.LE.2) THEN
631
                         csumf(lm)=csumf(lm) + (4*pi_const)**2 * CMPLX(0.0,1.0)**l / facC(l)
632 633
                      END IF

Daniel Wortmann's avatar
Daniel Wortmann committed
634 635
                      ! finally define coulomb
                      idum = ix*(ix-1)/2
636
                      cdum = (4*pi_const)**2 * CMPLX(0.0,1.0)**(l) * y(lm) * EXP(CMPLX(0.0,1.0) * 2*pi_const&
Daniel Wortmann's avatar
Daniel Wortmann committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
                           * dot_PRODUCT(hybrid%gptm(:,igptp),atoms%taual(:,ic)))
                      DO n=1,hybrid%nindxm1(l,itype)
                         iy = iy + 1

                         IF(ikpt.EQ.1.AND.igpt.EQ.1) THEN
                            IF(l.EQ.0) coulmat(iy,ix-hybrid%nbasp) =&
                                 - cdum * moment2(n,itype) / 6 / svol         ! (2a)
                            coulmat(iy,ix-hybrid%nbasp)   = coulmat(iy,ix-hybrid%nbasp)&
                                 + ( - cdum / (2*l+1) * integral(n,l,itype,iqnrm)& ! (2b)&
                                 + csum * moment(n,l,itype) ) / svol          ! (2c)
                         ELSE
                            coulmat(iy,ix-hybrid%nbasp)   = &
                                 (   cdum * olap(n,l,itype,iqnrm) / qnorm**2  &  ! (2a)&
                                 - cdum / (2*l+1) * integral(n,l,itype,iqnrm)& ! (2b)&
                                 + csum * moment(n,l,itype) ) / svol          ! (2c)

                         END IF
654

Daniel Wortmann's avatar
Daniel Wortmann committed
655
                      END DO
656

Daniel Wortmann's avatar
Daniel Wortmann committed
657
                   END DO
658
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
659
             END DO
660 661

          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
662 663 664 665 666 667 668 669 670 671 672 673
       END DO

       IF ( sym%invs) THEN
          CALL symmetrize(coulmat,hybrid%nbasp,hybrid%ngptm(ikpt),1,.FALSE.,&
               atoms,hybrid%lcutm1,hybrid%maxlcutm1, hybrid%nindxm1,sym)
       ENDIF

       M = hybrid%nbasp*(hybrid%nbasp+1)/2
       DO i=1,hybrid%ngptm(ikpt)
          DO j=1,hybrid%nbasp+i
             M = M + 1
             IF(j.LE. hybrid%nbasp) coulomb(M,ikpt) = coulmat(j,i)
674
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
675 676
       END DO
    END DO
677

Daniel Wortmann's avatar
Daniel Wortmann committed
678
    DEALLOCATE( coulmat,olap,integral )
679

Daniel Wortmann's avatar
Daniel Wortmann committed
680 681 682 683 684
    IF ( mpi%irank == 0 ) THEN
       WRITE(6,'(2X,A)',advance='no') 'done'
       CALL cpu_TIME(time2) 
       WRITE(6,'(2X,A,F8.2,A)') '( Timing:',time2-time1,' )'
    END IF
685

Daniel Wortmann's avatar
Daniel Wortmann committed
686 687 688
    !
    !     (3) Case < PW | v | PW >
    !
689

Daniel Wortmann's avatar
Daniel Wortmann committed
690
    IF( mpi%irank == 0 ) WRITE(6,'(A)',advance='no') '< PW | v | PW > contribution...'
691

Daniel Wortmann's avatar
Daniel Wortmann committed
692
    CALL cpu_TIME(time1)
693

Daniel Wortmann's avatar
Daniel Wortmann committed
694
    !     (3a) r,r' everywhere; r everywhere, r' in MT; r in MT, r' everywhere
695

Daniel Wortmann's avatar
Daniel Wortmann committed
696 697 698 699 700 701
    CALL cpu_TIME(time1)
    ! Calculate the hermitian matrix smat(i,j) = sum(a) integral(MT(a)) exp[i(Gj-Gi)r] dr
    ALLOCATE ( smat(hybrid%gptmd,hybrid%gptmd) )
    smat = 0
    DO igpt2=1,hybrid%gptmd
       DO igpt1=1,igpt2
702 703
          g     = hybrid%gptm(:,igpt2)-hybrid%gptm(:,igpt1)
          gnorm = gptnorm(g,cell%bmat)
Daniel Wortmann's avatar
Daniel Wortmann committed
704 705 706 707
          IF(gnorm.EQ.0) THEN
             DO itype=1,atoms%ntype
                smat(igpt1,igpt2) = smat(igpt1,igpt2) + atoms%neq(itype) * 4*pi_const*atoms%rmt(itype)**3/3
             END DO
708
          ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
709 710 711 712 713 714 715
             ic = 0
             DO itype=1,atoms%ntype
                rdum = atoms%rmt(itype) * gnorm
                rdum = 4*pi_const * ( SIN(rdum) - rdum * COS(rdum) ) / gnorm**3
                DO ineq=1,atoms%neq(itype)
                   ic                = ic + 1
                   smat(igpt1,igpt2) = smat(igpt1,igpt2)&
716
                        + rdum * EXP( CMPLX(0.0,1.0) * 2*pi_const * dot_PRODUCT(atoms%taual(:,ic),g) )
Daniel Wortmann's avatar
Daniel Wortmann committed
717 718
                END DO
             END DO
719
          END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
720 721 722
          smat(igpt2,igpt1) = CONJG(smat(igpt1,igpt2))
       END DO
    END DO
723

Daniel Wortmann's avatar
Daniel Wortmann committed
724 725
    ! Coulomb matrix, contribution (3a)
    DO ikpt=ikptmin,ikptmax
726

Daniel Wortmann's avatar
Daniel Wortmann committed
727
       DO igpt0=igptmin(ikpt),igptmax(ikpt)
728 729
          igpt2  = hybrid%pgptm1(igpt0,ikpt)
          igptp2 = hybrid%pgptm(igpt2,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
730 731 732 733 734
          ix     = hybrid%nbasp + igpt2
          iy     = hybrid%nbasp
          q2     = MATMUL ( kpts%bk(:,ikpt) + hybrid%gptm(:,igptp2) , cell%bmat )
          rdum2  = SUM(q2**2)
          IF( rdum2 .NE. 0 ) rdum2 = 4*pi_const/rdum2
735 736

          DO igpt1=1,igpt2
Daniel Wortmann's avatar
Daniel Wortmann committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
             igptp1 = hybrid%pgptm(igpt1,ikpt)
             iy     = iy + 1
             q1     = MATMUL ( kpts%bk(:,ikpt) + hybrid%gptm(:,igptp1) , cell%bmat )
             idum   = ix*(ix-1)/2+iy
             rdum1  = SUM(q1**2)
             IF( rdum1 .NE. 0 ) rdum1 = 4*pi_const/rdum1

             IF(ikpt.EQ.1) THEN
                IF(igpt1.NE.1) THEN
                   coulomb(idum,1) = - smat(igptp1,igptp2) * rdum1 / cell%vol
                END IF
                IF(igpt2.NE.1) THEN
                   coulomb(idum,1) = coulomb(idum,1) - smat(igptp1,igptp2) * rdum2 / cell%vol
                END IF
             ELSE
                coulomb(idum,ikpt) = - smat(igptp1,igptp2) * ( rdum1 + rdum2 ) / cell%vol
             END IF
754
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
755 756
          IF(ikpt.NE.1.OR.igpt2.NE.1) THEN                  !
             coulomb(idum,ikpt) = coulomb(idum,ikpt) + rdum2 ! diagonal term
757
          END IF                                            !
Daniel Wortmann's avatar
Daniel Wortmann committed
758
       END DO
759

Daniel Wortmann's avatar
Daniel Wortmann committed
760 761
    END DO
    !     (3b) r,r' in different MT
762

Daniel Wortmann's avatar
Daniel Wortmann committed
763
    DO ikpt=ikptmin,ikptmax!1,kpts%nkpt
764

Daniel Wortmann's avatar
Daniel Wortmann committed
765 766 767 768
       ! group together quantities which depend only on l,m and igpt -> carr2a
       ALLOCATE( carr2a((hybrid%lexp+1)**2,hybrid%maxgptm),carr2b(atoms%nat,hybrid%maxgptm) )
       carr2a = 0 ; carr2b = 0
       DO igpt=1,hybrid%ngptm(ikpt)
769 770
          igptp = hybrid%pgptm(igpt,ikpt)
          iqnrm = pqnrm(igpt,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
771
          q     = MATMUL ( kpts%bk(:,ikpt) + hybrid%gptm(:,igptp),cell%bmat)
772
          CALL harmonicsr(y,q,hybrid%lexp)
Daniel Wortmann's avatar
Daniel Wortmann committed
773
          y     = CONJG(y)
774 775
          lm = 0
          DO l=0,hybrid%lexp
Daniel Wortmann's avatar
Daniel Wortmann committed
776 777
             DO M=-l,l
                lm              = lm + 1
778
                carr2a(lm,igpt) = 4*pi_const * CMPLX(0.0,1.0)**(l) * y(lm)
Daniel Wortmann's avatar
Daniel Wortmann committed
779
             END DO
780 781
          END DO
          DO ic = 1,atoms%nat
782
             carr2b(ic,igpt) = EXP ( -CMPLX(0.0,1.0) * 2*pi_const * &
Daniel Wortmann's avatar
Daniel Wortmann committed
783
                  dot_PRODUCT(kpts%bk(:,ikpt)+hybrid%gptm(:,igptp),atoms%taual(:,ic)) )
784
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
785
       END DO
786

Daniel Wortmann's avatar
Daniel Wortmann committed
787 788 789 790 791
       !finally we can loop over the plane waves (G: igpt1,igpt2)
       ALLOCATE ( carr2(atoms%nat,(hybrid%lexp+1)**2),&
            structconst1(atoms%nat,(2*hybrid%lexp+1)**2) )
       carr2 = 0 ; structconst1 = 0
       DO igpt0=igptmin(ikpt),igptmax(ikpt)!1,hybrid%ngptm1(ikpt)
792
          igpt2  = hybrid%pgptm1(igpt0,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
793
          ix     = hybrid%nbasp + igpt2
794 795 796 797 798
          igptp2 = hybrid%pgptm(igpt2,ikpt)
          iqnrm2 = pqnrm(igpt2,ikpt)
          ic2    = 0
          carr2  = 0
          DO itype2 = 1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
799 800 801 802 803 804
             DO ineq2 = 1,atoms%neq(itype2)
                ic2   = ic2 + 1
                cexp  = CONJG ( carr2b(ic2,igpt2) )
                lm2   = 0
                DO ic1 = 1,atoms%nat
                   structconst1(ic1,:) = structconst(:,ic1,ic2,ikpt)
805
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
                DO l2 = 0,hybrid%lexp
                   idum = 1
                   DO m2 = -l2,l2
                      lm2  = lm2 + 1
                      cdum = idum * sphbesmoment(l2,itype2,iqnrm2) * cexp * carr2a(lm2,igpt2)
                      IF( cdum .NE. 0 ) THEN
                         lm1 = 0
                         DO l1 = 0,hybrid%lexp
                            l  =  l1 + l2
                            M  = -l1 - m2 !first loop of m1
                            lm = l**2 + l + M
                            DO m1 = -l1,l1
                               lm1  = lm1 + 1
                               lm   = lm  + 1
                               cdum1= cdum * gmat(lm1,lm2)
                               DO ic1 = 1,atoms%nat
                                  carr2(ic1,lm1) = carr2(ic1,lm1) + cdum1 * structconst1(ic1,lm)
                               END DO
                            END DO
                         END DO
                      END IF
                      idum = -idum !factor (-1)**(l+M)
                   END DO
                END DO
             END DO
831 832
          END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
833
          iy = hybrid%nbasp
834
          DO igpt1=1,igpt2
Daniel Wortmann's avatar
Daniel Wortmann committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
             iy      = iy + 1
             igptp1  = hybrid%pgptm(igpt1,ikpt)
             iqnrm1  = pqnrm(igpt1,ikpt)
             csum    = 0
             ic      = 0
             DO itype=1,atoms%ntype
                DO ineq=1,atoms%neq(itype)
                   ic   = ic  + 1
                   cexp = carr2b(ic,igpt1)
                   lm   = 0
                   DO l=0,hybrid%lexp
                      cdum = cexp * sphbesmoment(l,itype,iqnrm1)
                      DO M=-l,l
                         lm   = lm + 1
                         csum = csum + cdum * carr2(ic,lm) * CONJG ( carr2a(lm,igpt1) ) ! for coulomb
                      END DO
                   END DO
852
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
853 854 855
             END DO
             idum = ix*(ix-1)/2+iy
             coulomb(idum,ikpt) = coulomb(idum,ikpt) + csum / cell%vol
856
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
       END DO
       DEALLOCATE( carr2,carr2a,carr2b,structconst1 )
    END DO !ikpt
    !     Add corrections from higher orders in (3b) to coulomb(:,1)
    ! (1) igpt1 > 1 , igpt2 > 1  (finite G vectors)
    rdum = (4*pi_const)**(1.5d0)/cell%vol**2 * gmat(1,1)
    DO igpt0 = 1,hybrid%ngptm1(1)
       igpt2  = hybrid%pgptm1(igpt0,1) ; IF ( igpt2 == 1 ) CYCLE
       ix     = hybrid%nbasp + igpt2
       iqnrm2 = pqnrm(igpt2,1)
       igptp2 = hybrid%pgptm(igpt2,1)
       q2     = MATMUL(hybrid%gptm(:,igptp2),cell%bmat)
       qnorm2 = SQRT(SUM(q2**2))
       iy     = hybrid%nbasp + 1
       DO igpt1 = 2,igpt2
872 873 874 875
          iy     = iy + 1
          idum   = ix*(ix-1)/2+iy
          iqnrm1 = pqnrm(igpt1,1)
          igptp1 = hybrid%pgptm(igpt1,1)
Daniel Wortmann's avatar
Daniel Wortmann committed
876 877 878
          q1     = MATMUL(hybrid%gptm(:,igptp1),cell%bmat)
          qnorm1 = SQRT(SUM(q1**2))
          rdum1  = dot_PRODUCT(q1,q2) / (qnorm1*qnorm2)
879 880
          ic1    = 0
          DO itype1 = 1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
881 882 883 884 885 886
             DO ineq1 = 1,atoms%neq(itype1)
                ic1 = ic1 + 1
                ic2 = 0
                DO itype2 = 1,atoms%ntype
                   DO ineq2 = 1,atoms%neq(itype2)
                      ic2  = ic2 + 1
887
                      cdum = EXP ( CMPLX(0.0,1.0) * 2*pi_const *&
Daniel Wortmann's avatar
Daniel Wortmann committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901
                           ( - dot_PRODUCT(hybrid%gptm(:,igptp1),atoms%taual(:,ic1))&
                           + dot_PRODUCT(hybrid%gptm(:,igptp2),atoms%taual(:,ic2)) ) )
                      coulomb(idum,1) = coulomb(idum,1) + rdum * cdum * (&
                           - sphbesmoment(1,itype1,iqnrm1) &
                           * sphbesmoment(1,itype2,iqnrm2) * rdum1  / 3&
                           - sphbesmoment(0,itype1,iqnrm1)&
                           * sphbesmoment(2,itype2,iqnrm2)          / 6&
                           - sphbesmoment(2,itype1,iqnrm1)&
                           * sphbesmoment(0,itype2,iqnrm2)          / 6 &
                           + sphbesmoment(0,itype1,iqnrm1)&
                           * sphbesmoment(1,itype2,iqnrm2) / qnorm2 / 2&
                           + sphbesmoment(1,itype1,iqnrm1)&
                           * sphbesmoment(0,itype2,iqnrm2) / qnorm1 / 2 )
                   END DO
902
                END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
903
             END DO
904
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
905 906 907 908 909 910 911 912 913 914 915 916
       END DO
    END DO
    ! (2) igpt1 = 1 , igpt2 > 1  (first G vector vanishes, second finite)
    iy = hybrid%nbasp + 1
    DO igpt0 = 1,hybrid%ngptm1(1)
       igpt2  = hybrid%pgptm1(igpt0,1) ; IF ( igpt2 == 1 ) CYCLE
       ix     = hybrid%nbasp + igpt2
       iqnrm2 = pqnrm(igpt2,1)
       igptp2 = hybrid%pgptm(igpt2,1)
       qnorm2 = qnrm(iqnrm2)
       idum   = ix*(ix-1)/2+iy
       DO itype1 = 1,atoms%ntype
917
          DO ineq1 = 1,atoms%neq(itype1)
Daniel Wortmann's avatar
Daniel Wortmann committed
918 919 920 921
             ic2 = 0
             DO itype2 = 1,atoms%ntype
                DO ineq2 = 1,atoms%neq(itype2)
                   ic2  = ic2 + 1
922
                   cdum = EXP ( CMPLX(0.0,1.0) * 2*pi_const * dot_PRODUCT(hybrid%gptm(:,igptp2),atoms%taual(:,ic2)) )
Daniel Wortmann's avatar
Daniel Wortmann committed
923 924 925 926 927 928 929
                   coulomb(idum,1) = coulomb(idum,1)&
                        + rdum * cdum * atoms%rmt(itype1)**3 * (&
                        + sphbesmoment(0,itype2,iqnrm2) / 30 * atoms%rmt(itype1)**2&
                        - sphbesmoment(2,itype2,iqnrm2) / 18 &
                        + sphbesmoment(1,itype2,iqnrm2) /  6 / qnorm2 )
                END DO
             END DO
930
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
931 932 933 934 935 936 937 938
       END DO
    END DO
    ! (2) igpt1 = 1 , igpt2 = 1  (vanishing G vectors)
    iy   = hybrid%nbasp + 1
    ix   = hybrid%nbasp + 1
    idum = ix*(ix-1)/2+iy
    DO itype1 = 1,atoms%ntype
       DO ineq1 = 1,atoms%neq(itype1)
939
          DO itype2 = 1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
940 941 942 943 944
             DO ineq2 = 1,atoms%neq(itype2)
                coulomb(idum,1) = coulomb(idum,1)&
                     + rdum * atoms%rmt(itype1)**3 * atoms%rmt(itype2)**3 *&
                     ( atoms%rmt(itype1)**2 + atoms%rmt(itype2)**2 ) / 90
             END DO
945
          END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
946 947
       END DO
    END DO
948
  
Daniel Wortmann's avatar
Daniel Wortmann committed
949
    !     (3c) r,r' in same MT
950

Daniel Wortmann's avatar
Daniel Wortmann committed
951 952 953 954 955 956
    ! Calculate sphbesintegral
    ALLOCATE ( sphbes0(-1:hybrid%lexp+2,atoms%ntype,nqnrm),&
         &           carr2((hybrid%lexp+1)**2,hybrid%maxgptm) )
    sphbes0 = 0 ; carr2 = 0
    DO iqnrm = 1,nqnrm
       DO itype = 1,atoms%ntype
957 958
          rdum = qnrm(iqnrm) * atoms%rmt(itype)
          CALL sphbessel(sphbes0(0,itype,iqnrm),rdum,hybrid%lexp+2)
Daniel Wortmann's avatar
Daniel Wortmann committed
959 960 961
          IF( rdum.NE.0 ) sphbes0(-1,itype,iqnrm) = COS(rdum)/rdum
       END DO
    END DO
962

Daniel Wortmann's avatar
Daniel Wortmann committed
963 964
    l_warn = ( mpi%irank == 0 )
    DO ikpt=ikptmin,ikptmax!1,nkpt
965

Daniel Wortmann's avatar
Daniel Wortmann committed
966
       DO igpt = 1,hybrid%ngptm(ikpt)
967
          igptp = hybrid%pgptm(igpt,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
968
          q     = MATMUL ( kpts%bk(:,ikpt) + hybrid%gptm(:,igptp), cell%bmat )
969
          CALL harmonicsr(carr2(:,igpt),q,hybrid%lexp)
Daniel Wortmann's avatar
Daniel Wortmann committed
970
       END DO
971

Daniel Wortmann's avatar
Daniel Wortmann committed
972
       DO igpt0=igptmin(ikpt),igptmax(ikpt)!1,hybrid%ngptm1(ikpt)
973
          igpt2  = hybrid%pgptm1(igpt0,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
974
          ix     = hybrid%nbasp + igpt2
975 976
          igptp2 = hybrid%pgptm(igpt2,ikpt)
          iqnrm2 = pqnrm(igpt2,ikpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
977 978 979
          q2     = MATMUL (kpts%bk(:,ikpt) + hybrid%gptm(:,igptp2),cell%bmat)
          y2     = CONJG ( carr2(:,igpt2) )
          iy     = hybrid%nbasp
980
          DO igpt1=1,igpt2