coulombmatrix.F90 95.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
!
!     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.
Matthias Redies's avatar
Matthias Redies committed
15
!                                   k,IJ
16 17 18 19 20 21 22 23 24 25 26 27 28
!
!     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

Matthias Redies's avatar
Matthias Redies committed
38 39 40 41 42 43 44
   SUBROUTINE coulombmatrix(mpi, atoms, kpts, cell, sym, hybrid, xcpot, l_restart)

      USE m_types
      USE m_juDFT
      USE m_constants, ONLY: pi_const
      USE m_olap, ONLY: olap_pw, gptnorm
      USE m_trafo, ONLY: symmetrize, bramat_trafo
Matthias Redies's avatar
Matthias Redies committed
45
      USE m_util, ONLY: intgrf, intgrf_init, primitivef
Matthias Redies's avatar
Matthias Redies committed
46 47 48
      USE m_hsefunctional, ONLY: change_coulombmatrix
      USE m_wrapper
      USE m_io_hybrid
Matthias Redies's avatar
Matthias Redies committed
49
      use m_ylm
Matthias Redies's avatar
Matthias Redies committed
50
      use m_sphbes, only: sphbes
Matthias Redies's avatar
Matthias Redies committed
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
      IMPLICIT NONE

      TYPE(t_xcpot_inbuild), INTENT(IN)     :: xcpot
      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
      INTEGER                    :: ok
      INTEGER                    :: m
      INTEGER                    :: ikptmin, ikptmax, nkminmax
      INTEGER                    :: maxfac

      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)
Matthias Redies's avatar
Matthias Redies committed
103
      REAL                       :: sphbes_var(atoms%jmtd, 0:hybrid%maxlcutm1)
Matthias Redies's avatar
Matthias Redies committed
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
      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(:, :)
      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))

      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

      CALL timestart("Coulomb matrix setup")

      svol = SQRT(cell%vol)
      fcoulfac = 4*pi_const/cell%vol
      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)!!
149
         facB(i) = facB(i - 1)*SQRT(i*1.0) !
Matthias Redies's avatar
Matthias Redies committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
         facC(i) = facC(i - 1)*(2*i + 1)   !
      END DO

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

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

      !     Calculate the structure constant
      CALL structureconstant(structconst, cell, hybrid, atoms, kpts, mpi)

      IF (mpi%irank == 0) WRITE (6, '(//A)') '### subroutine: coulombmatrix ###'

      !
      !     Matrix allocation
      !

Matthias Redies's avatar
Matthias Redies committed
166
      call timestart("coulomb allocation")
Matthias Redies's avatar
Matthias Redies committed
167 168 169
      IF (ALLOCATED(coulomb)) DEALLOCATE (coulomb)

      ALLOCATE (coulomb(hybrid%maxbasm1*(hybrid%maxbasm1 + 1)/2, kpts%nkpt), stat=ok)
Matthias Redies's avatar
Matthias Redies committed
170
      IF (ok /= 0) STOP 'coulombmatrix: failure allocation coulomb matrix'
Matthias Redies's avatar
Matthias Redies committed
171
      coulomb = 0
Matthias Redies's avatar
Matthias Redies committed
172
      call timestop("coulomb allocation")
Matthias Redies's avatar
Matthias Redies committed
173

174
      IF (mpi%irank == 0) WRITE (6, '(/A,F6.1," MB")') 'Size of coulomb matrix:', 16.0/1048576*SIZE(coulomb)
Matthias Redies's avatar
Matthias Redies committed
175 176 177

      !     Generate Symmetry:
      !     Reduce list of g-Points so that only one of each symm-equivalent is calculated
178

179
#ifndef CPP_NOCOULSYM
Matthias Redies's avatar
Matthias Redies committed
180 181 182 183
      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
Matthias Redies's avatar
Matthias Redies committed
184
         IF (isym <= sym%nop) THEN
Matthias Redies's avatar
Matthias Redies committed
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
            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
                  DO m2 = -l, -1
                     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
                  END DO
               END DO
            END DO
         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

Matthias Redies's avatar
Matthias Redies committed
225
            IF (ALL(ABS(MATMUL(rrot(:, :, isym), kpts%bk(:, ikpt)) - kpts%bk(:, ikpt)) < 1e-12)) THEN
Matthias Redies's avatar
Matthias Redies committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
               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
Matthias Redies's avatar
Matthias Redies committed
247
            IF (iarr(igpt) == 0) THEN
Matthias Redies's avatar
Matthias Redies committed
248 249 250 251 252
               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))
Matthias Redies's avatar
Matthias Redies committed
253
                  IF (i == 0) STOP 'coulombmatrix: zero pointer (bug?)'
Matthias Redies's avatar
Matthias Redies committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                  iarr(i) = 1
               END DO
            END IF
         END DO
         hybrid%ngptm1(ikpt) = j
      END DO
      DEALLOCATE (iarr)

      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, ' )'

      ! no symmetry used
#else
      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
273
#endif
274

Matthias Redies's avatar
Matthias Redies committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      ! 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)

      call timestart("define gmat")
      ! 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
            lm1 = lm1 + 1
            lm2 = 0
            lp1: DO l2 = 0, l1
               DO m2 = -l2, l2
                  lm2 = lm2 + 1
Matthias Redies's avatar
Matthias Redies committed
299
                  IF (lm2 > lm1) EXIT lp1 ! Don't cross the diagonal!
Matthias Redies's avatar
Matthias Redies committed
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))/ &
302
                                   SQRT(1.0*(2*l1 + 1)*(2*l2 + 1)*(2*(l1 + l2) + 1))*(4*pi_const)**1.5
Matthias Redies's avatar
Matthias Redies committed
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
                  gmat(lm2, lm1) = gmat(lm1, lm2)
               END DO
            END DO LP1
         END DO
      END DO
      call timestop("define gmat")

      ! Calculate moments of MT functions
      call timestart("calc moments of MT")
      DO itype = 1, atoms%ntype
         DO l = 0, hybrid%lcutm1(itype)
            DO i = 1, hybrid%nindxm1(l, itype)
               ! 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)
            END DO
         END DO
         DO i = 1, hybrid%nindxm1(0, itype)
            moment2(i, itype) = intgrf(atoms%rmsh(:, itype)**3*hybrid%basm1(:, i, 0, itype), &
                                       atoms%jri, atoms%jmtd, atoms%rmsh, atoms%dx, atoms%ntype, itype, gridf)
         END DO
      END DO
      call timestop("calc moments of MT")

      call timestart("getnorm")
      ! 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))
Matthias Redies's avatar
Matthias Redies committed
333
      sphbes_var = 0
Matthias Redies's avatar
Matthias Redies committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
      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
      call timestop("getnorm")

      call timestart("Bessel calculation")
      DO iqnrm = iqnrmstart, nqnrm, iqnrmstep
         qnorm = qnrm(iqnrm)
         DO itype = 1, atoms%ntype
            ng = atoms%jri(itype)
            rdum = atoms%rmt(itype)
Matthias Redies's avatar
Matthias Redies committed
356
            sphbes_var = 0
Matthias Redies's avatar
Matthias Redies committed
357
            sphbesmoment1 = 0
Matthias Redies's avatar
Matthias Redies committed
358
            IF (qnorm == 0) THEN
Matthias Redies's avatar
Matthias Redies committed
359 360
               sphbesmoment(0, itype, iqnrm) = rdum**3/3
               DO i = 1, ng
Matthias Redies's avatar
Matthias Redies committed
361
                  sphbes_var(i, 0) = 1
Matthias Redies's avatar
Matthias Redies committed
362 363 364
                  sphbesmoment1(i, 0) = atoms%rmsh(i, itype)**2/3 + (rdum**2 - atoms%rmsh(i, itype)**2)/2
               END DO
            ELSE
Matthias Redies's avatar
Matthias Redies committed
365
               call sphbes(hybrid%lexp + 1, qnorm*rdum, rarr)
Matthias Redies's avatar
Matthias Redies committed
366 367 368 369 370
               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)
Matthias Redies's avatar
Matthias Redies committed
371
                  call sphbes(hybrid%lcutm1(itype) + 1, qnorm*rdum, rarr)
Matthias Redies's avatar
Matthias Redies committed
372
                  DO l = 0, hybrid%lcutm1(itype)
Matthias Redies's avatar
Matthias Redies committed
373
                     sphbes_var(i, l) = rarr(l)
Matthias Redies's avatar
Matthias Redies committed
374
                     IF (l /= 0) THEN; rdum1 = -rdum**(1 - l)*rarr(l - 1)
Matthias Redies's avatar
Matthias Redies committed
375 376
                     ELSE; rdum1 = -COS(qnorm*rdum)/qnorm
                     ENDIF
Matthias Redies's avatar
Matthias Redies committed
377
                     IF (i == ng) rarr1(l) = rdum1
Matthias Redies's avatar
Matthias Redies committed
378 379 380 381 382 383 384 385 386
                     sphbesmoment1(i, l) = (rdum**(l + 2)*rarr(l + 1)/rdum**(l + 1) &
                                            + (rarr1(l) - rdum1)*rdum**l)/qnorm
                  END DO
               END DO
            END IF
            DO l = 0, hybrid%lcutm1(itype)
               DO n = 1, hybrid%nindxm1(l, itype)
                  ! note that hybrid%basm1 already contains one factor rgrid
                  olap(n, l, itype, iqnrm) = &
Matthias Redies's avatar
Matthias Redies committed
387
                     intgrf(atoms%rmsh(:, itype)*hybrid%basm1(:, n, l, itype)*sphbes_var(:, l), &
Matthias Redies's avatar
Matthias Redies committed
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
                            atoms%jri, atoms%jmtd, atoms%rmsh, atoms%dx, atoms%ntype, itype, gridf)

                  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)

               END DO
            END DO
         END DO
      END DO
      call timestop("Bessel calculation")

      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

      !
      !     (1) Case < MT | v | MT >
      !

      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') '< MT | v | MT > contribution...'

      CALL cpu_TIME(time1)

      IF (ANY(calc_mt)) THEN

         !       (1a) r,r' in same MT
         call timestart("loop 1")
         ix = 0
         iy = 0
         iy0 = 0
         DO itype = 1, atoms%ntype
            DO ineq = 1, atoms%neq(itype)
               ! 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
                     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)

                     primf1(:atoms%jri(itype)) = primf1(:atoms%jri(itype))/atoms%rmsh(:atoms%jri(itype), itype)**l
                     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
                  END DO

                  ! 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
                  END DO

               END DO
            END DO
         END DO
         call timestop("loop 1")

         !       (1b) r,r' in different MT

         ALLOCATE (coulmat(hybrid%nbasp, hybrid%nbasp), stat=ok)
Matthias Redies's avatar
Matthias Redies committed
471
         IF (ok /= 0) STOP 'coulombmatrix: failure allocation coulmat'
Matthias Redies's avatar
Matthias Redies 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 498 499 500 501 502 503 504
         coulmat = 0

      END IF

      DO ikpt = ikptmin, ikptmax

         ! only the first rank handles the MT-MT part
         call timestart("MT-MT part")
         IF (calc_mt(ikpt)) THEN

            ix = 0
            ic2 = 0
            DO itype2 = 1, atoms%ntype
               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
Matthias Redies's avatar
Matthias Redies committed
505
                                          IF (iy > ix) EXIT lp2 ! Don't cross the diagonal!
Matthias Redies's avatar
Matthias Redies committed
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
                                          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) &
                                                            + EXP(CMPLX(0.0, 1.0)*2*pi_const* &
                                                                  dot_PRODUCT(kpts%bk(:, ikpt), &
                                                                              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
                              END DO
                           END DO lp2

                        END DO
                     END DO
                  END DO
               END DO
            END DO

            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

            coulomb(:hybrid%nbasp*(hybrid%nbasp + 1)/2, ikpt) = packmat(coulmat)

         END IF
         call timestop("MT-MT part")

      END DO
      IF (ANY(calc_mt)) DEALLOCATE (coulmat)

      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

Matthias Redies's avatar
Matthias Redies committed
550
      IF (hybrid%maxgptm == 0) GOTO 1 ! skip calculation of plane-wave contribution if mixed basis does not contain plane waves
Matthias Redies's avatar
Matthias Redies committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564

      !
      !     (2) Case < MT | v | PW >
      !

      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') '< MT | v | PW > contribution...'

      CALL cpu_TIME(time1)

      !     (2a) r in MT, r' everywhere
      !     (2b) r,r' in same MT
      !     (2c) r,r' in different MT

      ALLOCATE (coulmat(hybrid%nbasp, hybrid%maxgptm), stat=ok)
Matthias Redies's avatar
Matthias Redies committed
565
      IF (ok /= 0) STOP 'coulombmatrix: failure allocation coulmat'
Matthias Redies's avatar
Matthias Redies committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579
      coulmat = 0

      call timestart("loop over interst.")
      DO ikpt = ikptmin, ikptmax !1,kpts%nkpt

         coulmat = 0
         ! start to loop over interstitial plane waves
         DO igpt0 = igptmin(ikpt), igptmax(ikpt) !1,hybrid%ngptm1(ikpt)
            igpt = hybrid%pgptm1(igpt0, ikpt)
            igptp = hybrid%pgptm(igpt, ikpt)
            ix = hybrid%nbasp + igpt
            q = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp), cell%bmat)
            qnorm = SQRT(SUM(q**2))
            iqnrm = pqnrm(igpt, ikpt)
Matthias Redies's avatar
Matthias Redies committed
580
            IF (ABS(qnrm(iqnrm) - qnorm) > 1e-12) then
Matthias Redies's avatar
Matthias Redies committed
581 582 583
               STOP 'coulombmatrix: qnorm does not equal corresponding & element in qnrm (bug?)' ! We shouldn't stop here!
            endif

Matthias Redies's avatar
Matthias Redies committed
584
            call timestart("harmonics")
Matthias Redies's avatar
Matthias Redies committed
585 586
            call ylm4(2, MATMUL(kpts%bk(:, kpts%nkpt), cell%bmat), y1)
            call ylm4(2, MATMUL(hybrid%gptm(:, igptp), cell%bmat), y2)
Matthias Redies's avatar
Matthias Redies committed
587 588
            call ylm4(hybrid%lexp, q, y)
            call timestop("harmonics")
Matthias Redies's avatar
Matthias Redies committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
            y1 = CONJG(y1); y2 = CONJG(y2); y = CONJG(y)

            iy = 0
            ic = 0
            DO itype = 1, atoms%ntype
               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
                              cexp = 4*pi_const*EXP(CMPLX(0.0, 1.0)*2*pi_const &
                                                    *(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
                                 cdum = sphbesmoment(l1, itype1, iqnrm)*CMPLX(0.0, 1.0)**(l1)*cexp
                                 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)
Matthias Redies's avatar
Matthias Redies committed
627
                              IF (ikpt == 1 .AND. l <= 2) THEN
Matthias Redies's avatar
Matthias Redies committed
628 629 630 631
                                 cexp = EXP(CMPLX(0.0, 1.0)*2*pi_const*dot_PRODUCT(hybrid%gptm(:, igptp), atoms%taual(:, ic1))) &
                                        *gmat(lm, 1)*4*pi_const/cell%vol
                                 csumf(lm) = csumf(lm) - cexp*SQRT(4*pi_const)* &
                                             CMPLX(0.0, 1.0)**l*sphbesmoment(0, itype1, iqnrm)/facC(l - 1)
Matthias Redies's avatar
Matthias Redies committed
632 633
                                 IF (l == 0) THEN
                                    IF (igpt /= 1) THEN
Matthias Redies's avatar
Matthias Redies committed
634 635 636 637 638
                                       csum = csum - cexp*(sphbesmoment(0, itype1, iqnrm)*atoms%rmt(itype1)**2 - &
                                                           sphbesmoment(2, itype1, iqnrm)*2.0/3)/10
                                    ELSE
                                       csum = csum - cexp*atoms%rmt(itype1)**5/30
                                    END IF
Matthias Redies's avatar
Matthias Redies committed
639
                                 ELSE IF (l == 1) THEN
Matthias Redies's avatar
Matthias Redies committed
640 641 642 643 644 645 646 647 648
                                    csum = csum + cexp*CMPLX(0.0, 1.0)*SQRT(4*pi_const) &
                                           *sphbesmoment(1, itype1, iqnrm)*y(lm)/3
                                 END IF
                              END IF

                           END DO
                        END DO

                        ! add contribution of (2a) to csumf
Matthias Redies's avatar
Matthias Redies committed
649
                        IF (ikpt == 1 .AND. igpt == 1 .AND. l <= 2) THEN
Matthias Redies's avatar
Matthias Redies committed
650 651 652 653 654 655 656 657 658 659 660
                           csumf(lm) = csumf(lm) + (4*pi_const)**2*CMPLX(0.0, 1.0)**l/facC(l)
                        END IF

                        ! finally define coulomb
                        idum = ix*(ix - 1)/2
                        cdum = (4*pi_const)**2*CMPLX(0.0, 1.0)**(l)*y(lm) &
                               *EXP(CMPLX(0.0, 1.0)*2*pi_const &
                                    *dot_PRODUCT(hybrid%gptm(:, igptp), atoms%taual(:, ic)))
                        DO n = 1, hybrid%nindxm1(l, itype)
                           iy = iy + 1

Matthias Redies's avatar
Matthias Redies committed
661 662
                           IF (ikpt == 1 .AND. igpt == 1) THEN
                              IF (l == 0) coulmat(iy, ix - hybrid%nbasp) = &
Matthias Redies's avatar
Matthias Redies committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
                                 -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

                        END DO

                     END DO
                  END DO
               END DO

            END DO
         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
Matthias Redies's avatar
Matthias Redies committed
693
               IF (j <= hybrid%nbasp) coulomb(M, ikpt) = coulmat(j, i)
Matthias Redies's avatar
Matthias Redies committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
            END DO
         END DO
      END DO
      call timestop("loop over interst.")

      DEALLOCATE (coulmat, olap, integral)

      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

      !
      !     (3) Case < PW | v | PW >
      !

      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') '< PW | v | PW > contribution...'

      CALL cpu_TIME(time1)

      !     (3a) r,r' everywhere; r everywhere, r' in MT; r in MT, r' everywhere

      CALL cpu_TIME(time1)
      ! Calculate the hermitian matrix smat(i,j) = sum(a) integral(MT(a)) exp[i(Gj-Gi)r] dr
      call timestart("calc smat")
      ALLOCATE (smat(hybrid%gptmd, hybrid%gptmd))
      smat = 0
      DO igpt2 = 1, hybrid%gptmd
         DO igpt1 = 1, igpt2
            g = hybrid%gptm(:, igpt2) - hybrid%gptm(:, igpt1)
            gnorm = gptnorm(g, cell%bmat)
Matthias Redies's avatar
Matthias Redies committed
726
            IF (gnorm == 0) THEN
Matthias Redies's avatar
Matthias Redies committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
               DO itype = 1, atoms%ntype
                  smat(igpt1, igpt2) = smat(igpt1, igpt2) + atoms%neq(itype)*4*pi_const*atoms%rmt(itype)**3/3
               END DO
            ELSE
               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) &
                                          + rdum*EXP(CMPLX(0.0, 1.0)*2*pi_const*dot_PRODUCT(atoms%taual(:, ic), g))
                  END DO
               END DO
            END IF
            smat(igpt2, igpt1) = CONJG(smat(igpt1, igpt2))
         END DO
      END DO
      call timestop("calc smat")

      ! Coulomb matrix, contribution (3a)
      call timestart("coulomb matrix")
      DO ikpt = ikptmin, ikptmax

         DO igpt0 = igptmin(ikpt), igptmax(ikpt)
            igpt2 = hybrid%pgptm1(igpt0, ikpt)
            igptp2 = hybrid%pgptm(igpt2, ikpt)
            ix = hybrid%nbasp + igpt2
            iy = hybrid%nbasp
            q2 = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp2), cell%bmat)
            rdum2 = SUM(q2**2)
Matthias Redies's avatar
Matthias Redies committed
758
            IF (rdum2 /= 0) rdum2 = 4*pi_const/rdum2
Matthias Redies's avatar
Matthias Redies committed
759 760 761 762 763 764 765

            DO igpt1 = 1, igpt2
               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)
Matthias Redies's avatar
Matthias Redies committed
766
               IF (rdum1 /= 0) rdum1 = 4*pi_const/rdum1
Matthias Redies's avatar
Matthias Redies committed
767

Matthias Redies's avatar
Matthias Redies committed
768 769
               IF (ikpt == 1) THEN
                  IF (igpt1 /= 1) THEN
Matthias Redies's avatar
Matthias Redies committed
770 771
                     coulomb(idum, 1) = -smat(igptp1, igptp2)*rdum1/cell%vol
                  END IF
Matthias Redies's avatar
Matthias Redies committed
772
                  IF (igpt2 /= 1) THEN
Matthias Redies's avatar
Matthias Redies committed
773 774 775 776 777 778
                     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
            END DO
Matthias Redies's avatar
Matthias Redies committed
779
            IF (ikpt /= 1 .OR. igpt2 /= 1) THEN                  !
Matthias Redies's avatar
Matthias Redies committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
               coulomb(idum, ikpt) = coulomb(idum, ikpt) + rdum2 ! diagonal term
            END IF                                            !
         END DO

      END DO
      call timestop("coulomb matrix")
      !     (3b) r,r' in different MT

      call timestart("loop 4:")
      DO ikpt = ikptmin, ikptmax!1,kpts%nkpt

         ! 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)
            igptp = hybrid%pgptm(igpt, ikpt)
            iqnrm = pqnrm(igpt, ikpt)
            q = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp), cell%bmat)
Matthias Redies's avatar
Matthias Redies committed
798 799 800 801 802

            call timestart("harmonics")
            call ylm4(hybrid%lexp, q, y)
            call timestop("harmonics")

Matthias Redies's avatar
Matthias Redies committed
803 804 805 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 831 832 833 834 835 836 837 838 839 840 841
            y = CONJG(y)
            lm = 0
            DO l = 0, hybrid%lexp
               DO M = -l, l
                  lm = lm + 1
                  carr2a(lm, igpt) = 4*pi_const*CMPLX(0.0, 1.0)**(l)*y(lm)
               END DO
            END DO
            DO ic = 1, atoms%nat
               carr2b(ic, igpt) = EXP(-CMPLX(0.0, 1.0)*2*pi_const* &
                                      dot_PRODUCT(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp), atoms%taual(:, ic)))
            END DO
         END DO

         !finally we can loop over the plane waves (G: igpt1,igpt2)
         call timestart("loop over plane waves")
         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)
            igpt2 = hybrid%pgptm1(igpt0, ikpt)
            ix = hybrid%nbasp + igpt2
            igptp2 = hybrid%pgptm(igpt2, ikpt)
            iqnrm2 = pqnrm(igpt2, ikpt)
            ic2 = 0
            carr2 = 0
            DO itype2 = 1, atoms%ntype
               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)
                  END DO
                  DO l2 = 0, hybrid%lexp
                     idum = 1
                     DO m2 = -l2, l2
                        lm2 = lm2 + 1
                        cdum = idum*sphbesmoment(l2, itype2, iqnrm2)*cexp*carr2a(lm2, igpt2)
Matthias Redies's avatar
Matthias Redies committed
842
                        IF (cdum /= 0) THEN
Matthias Redies's avatar
Matthias Redies committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
                           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
            END DO

            iy = hybrid%nbasp
            DO igpt1 = 1, igpt2
               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
                  END DO
               END DO
               idum = ix*(ix - 1)/2 + iy
               coulomb(idum, ikpt) = coulomb(idum, ikpt) + csum/cell%vol
            END DO
         END DO
         DEALLOCATE (carr2, carr2a, carr2b, structconst1)
         call timestop("loop over plane waves")
      END DO !ikpt
      call timestop("loop 4:")
      !     Add corrections from higher orders in (3b) to coulomb(:,1)
      ! (1) igpt1 > 1 , igpt2 > 1  (finite G vectors)
      call timestart("add corrections from higher orders")
896
      rdum = (4*pi_const)**(1.5)/cell%vol**2*gmat(1, 1)
Matthias Redies's avatar
Matthias Redies committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
      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
            iy = iy + 1
            idum = ix*(ix - 1)/2 + iy
            iqnrm1 = pqnrm(igpt1, 1)
            igptp1 = hybrid%pgptm(igpt1, 1)
            q1 = MATMUL(hybrid%gptm(:, igptp1), cell%bmat)
            qnorm1 = SQRT(SUM(q1**2))
            rdum1 = dot_PRODUCT(q1, q2)/(qnorm1*qnorm2)
            ic1 = 0
            DO itype1 = 1, atoms%ntype
               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
                        cdum = EXP(CMPLX(0.0, 1.0)*2*pi_const* &
                                   (-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
                  END DO
               END DO
            END DO
         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
            DO ineq1 = 1, atoms%neq(itype1)
               ic2 = 0
               DO itype2 = 1, atoms%ntype
                  DO ineq2 = 1, atoms%neq(itype2)
                     ic2 = ic2 + 1
                     cdum = EXP(CMPLX(0.0, 1.0)*2*pi_const*dot_PRODUCT(hybrid%gptm(:, igptp2), atoms%taual(:, ic2)))
                     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
            END DO
         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)
            DO itype2 = 1, atoms%ntype
               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
            END DO
         END DO
      END DO
      call timestop("add corrections from higher orders")

      !     (3c) r,r' in same MT

      ! Calculate sphbesintegral
      call timestart("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
            rdum = qnrm(iqnrm)*atoms%rmt(itype)
Matthias Redies's avatar
Matthias Redies committed
994 995
            call sphbes(hybrid%lexp + 2, rdum, sphbes0(0, itype, iqnrm))
            IF (rdum /= 0) sphbes0(-1, itype, iqnrm) = COS(rdum)/rdum
Matthias Redies's avatar
Matthias Redies committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
         END DO
      END DO
      call timestop("sphbesintegral")

      l_warn = (mpi%irank == 0)
      call timestart("loop 2")
      DO ikpt = ikptmin, ikptmax!1,nkpt

         DO igpt = 1, hybrid%ngptm(ikpt)
            igptp = hybrid%pgptm(igpt, ikpt)
            q = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp), cell%bmat)
Matthias Redies's avatar
Matthias Redies committed
1007 1008 1009
            call timestart("harmonics")
            call ylm4(hybrid%lexp, q, carr2(:, igpt))
            call timestop("harmonics")
Matthias Redies's avatar
Matthias Redies committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
         END DO

         DO igpt0 = igptmin(ikpt), igptmax(ikpt)!1,hybrid%ngptm1(ikpt)
            igpt2 = hybrid%pgptm1(igpt0, ikpt)
            ix = hybrid%nbasp + igpt2
            igptp2 = hybrid%pgptm(igpt2, ikpt)
            iqnrm2 = pqnrm(igpt2, ikpt)
            q2 = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp2), cell%bmat)
            y2 = CONJG(carr2(:, igpt2))
            iy = hybrid%nbasp
            DO igpt1 = 1, igpt2
               iy = iy + 1
               igptp1 = hybrid%pgptm(igpt1, ikpt)
               iqnrm1 = pqnrm(igpt1, ikpt)
               q1 = MATMUL(kpts%bk(:, ikpt) + hybrid%gptm(:, igptp1), cell%bmat)
               y1 = carr2(:, igpt1)
               cexp1 = 0
               ic = 0
               DO itype = 1, atoms%ntype
                  DO ineq = 1, atoms%neq(itype)
                     ic = ic + 1
                     cexp1(itype) = cexp1(itype) + &
                                    EXP(CMPLX(0.0, 1.0)*2*pi_const*dot_PRODUCT( &
                                        (hybrid%gptm(:, igptp2) - hybrid%gptm(:, igptp1)), atoms%taual(:, ic)))
                  ENDDO
               ENDDO
               lm = 0
               cdum = 0
               DO l = 0, hybrid%lexp
                  cdum1 = 0
                  DO itype = 1, atoms%ntype
                     cdum1 = cdum1 + cexp1(itype)*sphbessel_integral( &
                             atoms, itype, qnrm, nqnrm, &
                             iqnrm1, iqnrm2, l, hybrid, &
                             sphbes0, l_warn, l_warned) &
                             /(2*l + 1)
                     l_warn = l_warn .AND. .NOT. l_warned ! only warn once
                  END DO
                  DO M = -l, l
                     lm = lm + 1
                     cdum = cdum + cdum1*y1(lm)*y2(lm)
                  ENDDO
               ENDDO
               idum = ix*(ix - 1)/2 + iy
               coulomb(idum, ikpt) = coulomb(idum, ikpt) + (4*pi_const)**3*cdum/cell%vol
            END DO
         END DO

      END DO
      call timestop("loop 2")
      DEALLOCATE (carr2)

      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

      !
      !     Symmetry-equivalent G vectors
      !
1071
#ifndef CPP_NOCOULSYM
1072

Matthias Redies's avatar
Matthias Redies committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') 'Symm.-equiv. matrix elements...'
      CALL cpu_TIME(time1)
      ! All elements are needed so send all data to all processes treating the
      ! respective k-points

      ALLOCATE (carr2(hybrid%maxbasm1, 2), iarr(hybrid%maxgptm))
      ALLOCATE (nsym_gpt(hybrid%gptmd, kpts%nkpt), &
                sym_gpt(MAXVAL(nsym1), hybrid%gptmd, kpts%nkpt))
      nsym_gpt = 0; sym_gpt = 0
      call timestart("loop 3")
      DO ikpt = ikptmin, ikptmax
         carr2 = 0; iarr = 0
         iarr(hybrid%pgptm1(:hybrid%ngptm1(ikpt), ikpt)) = 1
         DO igpt0 = 1, hybrid%ngptm1(ikpt) !igptmin(ikpt),igptmax(ikpt)
            lsym = ((igptmin(ikpt) <= igpt0) .AND. &
                    (igptmax(ikpt) >= igpt0))
            igpt2 = hybrid%pgptm1(igpt0, ikpt)
            j = (hybrid%nbasp + igpt2 - 1)*(hybrid%nbasp + igpt2)/2
            i = hybrid%nbasp + igpt2
            carr2(1:i, 2) = coulomb(j + 1:j + i, ikpt)
            j = j + i
            DO i = hybrid%nbasp + igpt2 + 1, nbasm1(ikpt)
               j = j + i - 1
               IF (sym%invs) THEN
                  carr2(i, 2) = coulomb(j, ikpt)
               ELSE
                  carr2(i, 2) = CONJG(coulomb(j, ikpt))
               ENDIF
            END DO
            IF (lsym) THEN
               ic = 1
               sym_gpt(ic, igpt0, ikpt) = igpt2
            END IF
            DO isym1 = 2, nsym1(ikpt)
               isym = sym1(isym1, ikpt)
               CALL bramat_trafo( &
                  carr2(:, 1), igpt1, &
                  carr2(:, 2), igpt2, ikpt, isym, .FALSE., POINTER(ikpt, :, :, :), &
                  sym, rrot(:, :, isym), invrrot(:, :, isym), hybrid, &
                  kpts, hybrid%maxlcutm1, atoms, hybrid%lcutm1, &
                  hybrid%nindxm1, hybrid%maxindxm1, dwgn(:, :, :, isym), &
                  hybrid%nbasp, nbasm1)
Matthias Redies's avatar
Matthias Redies committed
1115
               IF (iarr(igpt1) == 0) THEN
Matthias Redies's avatar
Matthias Redies committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
                  CALL bramat_trafo( &
                     carr2(:, 1), igpt1, &
                     carr2(:, 2), igpt2, ikpt, isym, .TRUE., POINTER(ikpt, :, :, :), &
                     sym, rrot(:, :, isym), invrrot(:, :, isym), hybrid, &
                     kpts, hybrid%maxlcutm1, atoms, hybrid%lcutm1, &
                     hybrid%nindxm1, hybrid%maxindxm1, &
                     dwgn(:, :, :, isym), hybrid%nbasp, nbasm1)
                  l = (hybrid%nbasp + igpt1 - 1)*(hybrid%nbasp + igpt1)/2
                  coulomb(l + 1:l + hybrid%nbasp + igpt1, ikpt) = carr2(:hybrid%nbasp + igpt1, 1)
                  iarr(igpt1) = 1
                  IF (lsym) THEN
                     ic = ic + 1
                     sym_gpt(ic, igpt0, ikpt) = igpt1
                  END IF
               END IF
            END DO
            nsym_gpt(igpt0, ikpt) = ic
         END DO ! igpt0
      END DO ! ikpt
      call timestop("loop 3")
      call timestart("gap 1:")
      DEALLOCATE (carr2, iarr, hybrid%pgptm1)
      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

      ! no symmetry used
1145
#else
1146

Matthias Redies's avatar
Matthias Redies committed
1147 1148 1149 1150 1151
      ALLOCATE (nsym_gpt(hybrid%gptmd, kpts%nkpt), sym_gpt(1, hybrid%gptmd, kpts%nkpt))
      nsym_gpt = 1
      DO ikpt = 1, kpts%nkpt
         sym_gpt(1, :, ikpt) = (/(igpt0, igpt0=1, hybrid%gptmd)/)
      END DO
1152

1153
#endif
1154

Matthias Redies's avatar
Matthias Redies committed
1155 1156 1157 1158 1159 1160 1161 1162
1     DEALLOCATE (qnrm, pqnrm)

      CALL cpu_TIME(time1)
      IF (xcpot%is_name("hse") .OR. xcpot%is_name("vhse")) THEN
         !
         ! The HSE functional is realized subtracting erf/r from
         ! the normal Coulomb matrix
         !
1163
#ifdef CPP_NOSPMVEC
Matthias Redies's avatar
Matthias Redies committed
1164 1165 1166 1167 1168
         CALL change_coulombmatrix( &
            atoms, kpts, kpts, kpts%nkpt, &
            cell, cell, hybrid%lcutm1, hybrid%maxlcutm1, &
            hybrid%nindxm1, hybrid%maxindxm1, hybrid, &
            hybrid%basm1, hybrid%maxbasm1, nbasm1, sym, mpi, &
Daniel Wortmann's avatar
Daniel Wortmann committed
1169
            coulomb)
1170
#endif
Matthias Redies's avatar
Matthias Redies committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
      ELSE
         IF (ikptmin == 1) CALL subtract_sphaverage(sym, cell, atoms, kpts, hybrid, nbasm1, gridf, coulomb)
      END IF

      ! transform Coulomb matrix to the biorthogonal set
      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') 'Transform to biorthogonal set...'
      CALL cpu_TIME(time1)
      call timestop("gap 1:")
      call timestart("calc eigenvalues olap_pw")
      DO ikpt = ikptmin, ikptmax

         !calculate IR overlap-matrix
         CALL olapm%alloc(sym%invs, hybrid%ngptm(ikpt), hybrid%ngptm(ikpt), 0.0)

         CALL olap_pw(olapm, hybrid%gptm(:, hybrid%pgptm(:hybrid%ngptm(ikpt), ikpt)), hybrid%ngptm(ikpt), atoms, cell)

         !         !calculate eigenvalues of olapm
         !         ALLOCATE( eval(ngptm(ikpt)),evec(ngptm(ikpt),ngptm(ikpt)) )
         !         CALL diagonalize(evec,eval,olapm)
         !
         !         !
         !         ! small eigenvalues lead to inaccuries in the inversion
         !         ! however it seems that these do not play an important role
         !         ! for the HF exchange
         !         ! thus we do not do a SingularValueDecomposition
         !         !
         !
         !         IF( any(eval .le. 1E-06 ) ) THEN
         ! !           WRITE(*,*) count( eval .le. 1E-06 )
         ! !           WRITE(*,*) 'eval .le. 1E-06'
         ! !           ALLOCATE( involapm(ngptm(ikpt),ngptm(ikpt)) )
         !           olapm = 0
         !           DO i = 1,ngptm(ikpt)
         !             IF( eval(i) .le. 1E-06) CYCLE
         !             olapm(i,i) = 1/eval(i)
         !           END DO
         !
         !           ALLOCATE( invevec(ngptm(ikpt),ngptm(ikpt)) )
         ! if (sym%invs) then
         !           invevec =         transpose(evec)
         ! else
         !           invevec = conjg( transpose(evec) )
         ! endif
         !
         !           olapm   = matmul(evec,matmul(olapm,invevec) )
         !
         !           DEALLOCATE(invevec)!,involapm)
         !         ELSE
         !calculate inverse overlap-matrix
         CALL olapm%inverse()
         !         END IF

         !unpack matrix coulomb
         CALL coulhlp%from_packed(sym%invs, nbasm1(ikpt), REAL(coulomb(:, ikpt)), coulomb(:, ikpt))

         if (olapm%l_real) THEN
            !multiply with inverse olap from right hand side
            coulhlp%data_r(:, hybrid%nbasp + 1:) = MATMUL(coulhlp%data_r(:, hybrid%nbasp + 1:), olapm%data_r)
            !multiply with inverse olap from left side
            coulhlp%data_r(hybrid%nbasp + 1:, :) = MATMUL(olapm%data_r, coulhlp%data_r(hybrid%nbasp + 1:, :))
         else
            !multiply with inverse olap from right hand side
            coulhlp%data_c(:, hybrid%nbasp + 1:) = MATMUL(coulhlp%data_c(:, hybrid%nbasp + 1:), olapm%data_c)
            !multiply with inverse olap from left side
            coulhlp%data_c(hybrid%nbasp + 1:, :) = MATMUL(olapm%data_c, coulhlp%data_c(hybrid%nbasp + 1:, :))
         end if
         coulomb(:(nbasm1(ikpt)*(nbasm1(ikpt) + 1))/2, ikpt) = coulhlp%to_packed()

      END DO
      call timestop("calc eigenvalues olap_pw")

      IF (mpi%irank == 0) THEN
         WRITE (6, '(1X,A)', advance='no') 'done'
         CALL cpu_TIME(time2)
         WRITE (6, '(2X,A,F8.2,A)') '( Timing:', time2 - time1, ' )'
      END IF

      !call plot_coulombmatrix() -> code was shifted to plot_coulombmatrix.F90

      IF (mpi%irank == 0) WRITE (6, '(A)', advance='no') 'Writing of data to file...'
      CALL cpu_TIME(time1)
1252
#if( !defined CPP_NOSPMVEC && !defined CPP_IRAPPROX )
Matthias Redies's avatar
Matthias Redies committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
      !
      ! rearrange coulomb matrix
      !

      ALLOCATE (coulomb_mt1(hybrid%maxindxm1 - 1, hybrid%maxindxm1 - 1, 0:hybrid%maxlcutm1, atoms%ntype, 1))
      ic = (hybrid%maxlcutm1 + 1)**2*atoms%nat
      idum = ic + hybrid%maxgptm
      idum = (idum*(idum + 1))/2
      if (sym%invs) THEN
         ALLOCATE (coulomb_mt2_r(hybrid%maxindxm1 - 1, -hybrid%maxlcutm1:hybrid%maxlcutm1, 0:hybrid%maxlcutm1 + 1, atoms%nat, 1))
         ALLOCATE (coulomb_mt3_r(hybrid%maxindxm1 - 1, atoms%nat, atoms%nat, 1))
1264
#ifdef CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1265 1266 1267
         ALLOCATE (coulomb_mtir_r(ic, ic + hybrid%maxgptm, 1))
         coulomb_mtir_r = 0
         ALLOCATE (coulombp_mtir_r(0, 0))
1268
#else
Matthias Redies's avatar
Matthias Redies committed
1269 1270
         ALLOCATE (coulomb_mtir_r(ic + hybrid%maxgptm, ic + hybrid%maxgptm, 1))
         ALLOCATE (coulombp_mtir_r(idum, 1))
1271
#endif
Matthias Redies's avatar
Matthias Redies committed
1272 1273 1274
      else
         ALLOCATE (coulomb_mt2_c(hybrid%maxindxm1 - 1, -hybrid%maxlcutm1:hybrid%maxlcutm1, 0:hybrid%maxlcutm1 + 1, atoms%nat, 1))
         ALLOCATE (coulomb_mt3_c(hybrid%maxindxm1 - 1, atoms%nat, atoms%nat, 1))
1275
#ifdef CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1276 1277
         ALLOCATE (coulomb_mtir_c(ic, ic + hybrid%maxgptm, 1))
         ALLOCATE (coulombp_mtir_c(0, 0))
1278
#else
Matthias Redies's avatar
Matthias Redies committed
1279 1280
         ALLOCATE (coulomb_mtir_c(ic + hybrid%maxgptm, ic + hybrid%maxgptm, 1))
         ALLOCATE (coulombp_mtir_c(idum, 1))
1281
#endif
Matthias Redies's avatar
Matthias Redies committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
      endif
      call timestart("loop bla")
      DO ikpt = ikptmin, ikptmax
         ikpt0 = 1
         ikpt1 = 1
         ! initialize arrays
         if (sym%invs) THEN
            coulomb_mt1 = 0; coulomb_mt2_r = 0
            coulomb_mt3_r = 0; coulombp_mtir_r = 0
         else
            coulomb_mt1 = 0; coulomb_mt2_c = 0
            coulomb_mt3_c = 0; coulombp_mtir_c = 0
         endif
         ! unpack coulomb into coulhlp

         call coulhlp%from_packed(sym%invs, nbasm1(ikpt), real(coulomb(:, ikpt)), coulomb(:, ikpt))

         ! only one processor per k-point calculates MT convolution
         IF (calc_mt(ikpt)) THEN

            !
            ! store m-independent part of Coulomb matrix in MT spheres
            ! in coulomb_mt1(:hybrid%nindxm1(l,itype)-1,:hybrid%nindxm1(l,itype)-1,l,itype)
            !
            call timestart("m-indep. part of coulomb mtx")
            indx1 = 0
            DO itype = 1, atoms%ntype
               DO ineq = 1, atoms%neq(itype)
                  DO l = 0, hybrid%lcutm1(itype)

                     IF (ineq == 1) THEN
                        DO n = 1, hybrid%nindxm1(l, itype) - 1
                           if (coulhlp%l_real) THEN
                              coulomb_mt1(n, 1:hybrid%nindxm1(l, itype) - 1, l, itype, ikpt0) &
                                 = coulhlp%data_r(indx1 + n, indx1 + 1:indx1 + hybrid%nindxm1(l, itype) - 1)
                           else
                              coulomb_mt1(n, 1:hybrid%nindxm1(l, itype) - 1, l, itype, ikpt0) &
                                 = coulhlp%data_c(indx1 + n, indx1 + 1:indx1 + hybrid%nindxm1(l, itype) - 1)
                           end if
                        END DO
                     END IF

                     indx1 = indx1 + (2*l + 1)*hybrid%nindxm1(l, itype)
                  END DO
               END DO
            END DO
            call timestop("m-indep. part of coulomb mtx")

            !
            ! store m-dependent and atom-dependent part of Coulomb matrix in MT spheres
            ! in coulomb_mt2(:hybrid%nindxm1(l,itype)-1,-l:l,l,iatom)
            !
            call timestart("m-dep. part of coulomb mtx")
            indx1 = 0
            iatom = 0
            DO itype = 1, atoms%ntype
               DO ineq = 1, atoms%neq(itype)
                  iatom = iatom + 1
                  DO l = 0, hybrid%lcutm1(itype)
                     DO M = -l, l
                        if (coulhlp%l_real) THEN
                           coulomb_mt2_r(:hybrid%nindxm1(l, itype) - 1, M, l, iatom, ikpt0) &
                              = coulhlp%data_r(indx1 + 1:indx1 + hybrid%nindxm1(l, itype) - 1, indx1 + hybrid%nindxm1(l, itype))
                        else
                           coulomb_mt2_c(:hybrid%nindxm1(l, itype) - 1, M, l, iatom, ikpt0) &
                              = coulhlp%data_c(indx1 + 1:indx1 + hybrid%nindxm1(l, itype) - 1, indx1 + hybrid%nindxm1(l, itype))
                        endif

                        indx1 = indx1 + hybrid%nindxm1(l, itype)

                     END DO
                  END DO
               END DO
            END DO
            call timestop("m-dep. part of coulomb mtx")

            !
            ! due to the subtraction of the divergent part at the Gamma point
            ! additional contributions occur
            !
            call timestart("gamma point treatment")
Matthias Redies's avatar
Matthias Redies committed
1363
            IF (ikpt == 1) THEN
Matthias Redies's avatar
Matthias Redies committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
               !
               ! store the contribution of the G=0 plane wave with the MT l=0 functions in
               ! coulomb_mt2(:hybrid%nindxm1(l=0,itype),0,hybrid%maxlcutm1+1,iatom)
               !
               ic = 0
               iatom = 0
               DO itype = 1, atoms%ntype
                  DO ineq = 1, atoms%neq(itype)
                     iatom = iatom + 1
                     DO n = 1, hybrid%nindxm1(0, itype) - 1
                        if (coulhlp%l_real) THEN
                           coulomb_mt2_r(n, 0, hybrid%maxlcutm1 + 1, iatom, ikpt0) = coulhlp%data_r(ic + n, hybrid%nbasp + 1)
                        else
                           coulomb_mt2_c(n, 0, hybrid%maxlcutm1 + 1, iatom, ikpt0) = coulhlp%data_c(ic + n, hybrid%nbasp + 1)
                        endif
                     END DO
                     ic = ic + SUM((/((2*l + 1)*hybrid%nindxm1(l, itype), l=0, hybrid%lcutm1(itype))/))
                  END DO
               END DO

               !
               ! store the contributions between the MT s-like functions at atom1 and
               ! and the constant function at a different atom2
               !
               iatom = 0
               ic = 0
               DO itype = 1, atoms%ntype
                  ishift = SUM((/((2*l + 1)*hybrid%nindxm1(l, itype), l=0, hybrid%lcutm1(itype))/))
                  DO ineq = 1, atoms%neq(itype)
                     iatom = iatom + 1
                     ic1 = ic + hybrid%nindxm1(0, itype)

                     iatom1 = 0
                     ic2 = 0
                     DO itype1 = 1, atoms%ntype
                        ishift1 = SUM((/((2*l1 + 1)*hybrid%nindxm1(l1, itype1), l1=0, hybrid%lcutm1(itype1))/))
                        DO ineq1 = 1, atoms%neq(itype1)
                           iatom1 = iatom1 + 1
                           ic3 = ic2 + 1
                           ic4 = ic3 + hybrid%nindxm1(0, itype1) - 2

                           IF (sym%invs) THEN
                              coulomb_mt3_r(:hybrid%nindxm1(0, itype1) - 1, iatom, iatom1, ikpt0) = coulhlp%data_r(ic1, ic3:ic4)
                           ELSE
                              coulomb_mt3_c(:hybrid%nindxm1(0, itype1) - 1, iatom, iatom1, ikpt0) &
                                 = CONJG(coulhlp%data_c(ic1, ic3:ic4))
                           ENDIF
                           ic2 = ic2 + ishift1
                        END DO
                     END DO

                     ic = ic + ishift
                  END DO
               END DO

               !test
               iatom = 0
               DO itype = 1, atoms%ntype
                  DO ineq = 1, atoms%neq(itype)
                     iatom = iatom + 1
                     if (sym%invs) THEN
                        IF (MAXVAL(ABS(coulomb_mt2_r(:hybrid%nindxm1(0, itype) - 1, 0, 0, &
                                                     iatom, ikpt0) &
                                       - coulomb_mt3_r(:hybrid%nindxm1(0, itype) - 1, iatom, &
                                                       iatom, ikpt0))) &
Matthias Redies's avatar
Matthias Redies committed
1429
                            > 1E-08) &
Matthias Redies's avatar
Matthias Redies committed
1430 1431 1432 1433 1434 1435 1436
                           call judft_error('coulombmatrix: coulomb_mt2 and coulomb_mt3 are inconsistent')

                     else
                        IF (MAXVAL(ABS(coulomb_mt2_c(:hybrid%nindxm1(0, itype) - 1, 0, 0, &
                                                     iatom, ikpt0) &
                                       - coulomb_mt3_c(:hybrid%nindxm1(0, itype) - 1, iatom, &
                                                       iatom, ikpt0))) &
Matthias Redies's avatar
Matthias Redies committed
1437
                            > 1E-08) &
Daniel Wortmann's avatar
Daniel Wortmann committed
1438
                           call judft_error('coulombmatrix: coulomb_mt2 and coulomb_mt3 are inconsistent')
Matthias Redies's avatar
Matthias Redies committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
                     endif
                  END DO
               END DO
            END IF
            call timestop("gamma point treatment")

         END IF ! calc_mt

         !
         ! add the residual MT contributions, i.e. those functions with an moment,
         ! to the matrix coulomb_mtir, which is fully occupied
         !
         call timestart("residual MT contributions")
         ic = 0
         DO itype = 1, atoms%ntype
            DO ineq = 1, atoms%neq(itype)
               DO l = 0, hybrid%lcutm1(itype)
                  DO M = -l, l
                     ic = ic + 1
                  END DO
               END DO
            END DO
         END DO

         indx1 = 0; indx2 = 0; indx3 = 0; indx4 = 0
         DO itype = 1, atoms%ntype
            DO ineq = 1, atoms%neq(itype)
               DO l = 0, hybrid%lcutm1(itype)
                  DO M = -l, l
                     indx1 = indx1 + 1
                     indx3 = indx3 + hybrid%nindxm1(l, itype)

                     indx2 = 0
                     indx4 = 0
                     DO itype1 = 1, atoms%ntype
                        DO ineq1 = 1, atoms%neq(itype1)
                           DO l1 = 0, hybrid%lcutm1(itype1)
                              DO m1 = -l1, l1
                                 indx2 = indx2 + 1
                                 indx4 = indx4 + hybrid%nindxm1(l1, itype1)
Matthias Redies's avatar
Matthias Redies committed
1479
                                 IF (indx4 < indx3) CYCLE
Matthias Redies's avatar
Matthias Redies committed
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
                                 IF (calc_mt(ikpt)) THEN
                                    IF (sym%invs) THEN
                                       coulomb_mtir_r(indx1, indx2, ikpt1) = coulhlp%data_r(indx3, indx4)
                                       coulomb_mtir_r(indx2, indx1, ikpt1) = coulomb_mtir_r(indx1, indx2, ikpt1)
                                    ELSE
                                       coulomb_mtir_c(indx1, indx2, ikpt1) = coulhlp%data_c(indx3, indx4)
                                       coulomb_mtir_c(indx2, indx1, ikpt1) = CONJG(coulomb_mtir_c(indx1, indx2, ikpt1))
                                    ENDIF
                                 END IF
                              END DO
                           END DO
                        END DO
                     END DO

                     DO igpt = 1, hybrid%ngptm(ikpt)
                        indx2 = indx2 + 1
                        IF (sym%invs) THEN
                           coulomb_mtir_r(indx1, indx2, ikpt1) = coulhlp%data_r(indx3, hybrid%nbasp + igpt)
1498
#if !defined CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1499
                           coulomb_mtir_r(indx2, indx1, ikpt1) = coulomb_mtir_r(indx1, indx2, ikpt1)
1500
#endif
Matthias Redies's avatar
Matthias Redies committed
1501 1502
                        ELSE
                           coulomb_mtir_c(indx1, indx2, ikpt1) = coulhlp%data_c(indx3, hybrid%nbasp + igpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
1503
#if !defined CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1504
                           coulomb_mtir_c(indx2, indx1, ikpt1) = CONJG(coulomb_mtir_c(indx1, indx2, ikpt1))
1505
#endif
Matthias Redies's avatar
Matthias Redies committed
1506
                        ENDIF
1507

Matthias Redies's avatar
Matthias Redies committed
1508
                     END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
1509

Matthias Redies's avatar
Matthias Redies committed
1510 1511 1512 1513 1514
                  END DO
               END DO
            END DO
         END DO
         call timestop("residual MT contributions")
1515

Matthias Redies's avatar
Matthias Redies committed
1516
         IF (indx1 /= ic) STOP 'coulombmatrix: error index counting'
1517

Matthias Redies's avatar
Matthias Redies committed
1518 1519 1520
         !
         ! add ir part to the matrix coulomb_mtir
         !
1521
#ifndef CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
         if (sym%invs) THEN
            coulomb_mtir_r(ic + 1:ic + hybrid%ngptm(ikpt), ic + 1:ic + hybrid%ngptm(ikpt), ikpt1) &
               = coulhlp%data_r(hybrid%nbasp + 1:nbasm1(ikpt), hybrid%nbasp + 1:nbasm1(ikpt))
            ic2 = indx1 + hybrid%ngptm(ikpt)
            coulombp_mtir_r(:ic2*(ic2 + 1)/2, ikpt0) = packmat(coulomb_mtir_r(:ic2, :ic2, ikpt1))
         else
            coulomb_mtir_c(ic + 1:ic + hybrid%ngptm(ikpt), ic + 1:ic + hybrid%ngptm(ikpt), ikpt1) &
               = coulhlp%data_c(hybrid%nbasp + 1:nbasm1(ikpt), hybrid%nbasp + 1:nbasm1(ikpt))
            ic2 = indx1 + hybrid%ngptm(ikpt)
            coulombp_mtir_c(:ic2*(ic2 + 1)/2, ikpt0) = packmat(coulomb_mtir_c(:ic2, :ic2, ikpt1))
         end if
1533
#endif
Matthias Redies's avatar
Matthias Redies committed
1534 1535
         call timestart("write coulomb_spm")
         if (sym%invs) THEN
1536
#ifdef CPP_IRCOULOMBAPPROX
Matthias Redies's avatar
Matthias Redies committed
1537 1538
            call write_coulomb_spm_r(ikpt, coulomb_mt1(:, :, :, :, 1), coulomb_mt2_r(:, :, :, :, 1), &
                                     coulomb_mt3_r(:, :, :, 1), coulomb_mtir_r(:, 1))
1539
#else
Matthias Redies's avatar
Matthias Redies committed
1540 1541
            CALL write_coulomb_spm_r(ikpt, coulomb_mt1(:, :, :, :, 1), coulomb_mt2_r(:, :, :, :, 1), &
                                     coulomb_mt3_r(:, :, :, 1), coulombp_mtir_r(:, 1))
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
!!$       print *,"DEBUG"
!!$       DO n1=1,SIZE(coulomb_mt1,1)
!!$          DO n2=1,SIZE(coulomb_mt1,2)
!!$             DO i=1,SIZE(coulomb_mt1,3)
!!$                DO j=1,SIZE(coulomb_mt1,4)
!!$                   WRITE(732,*) n1,n2,i-1,j,coulomb_mt2_r(n1,n2,i-1,j,1)
!!$                ENDDO
!!$             ENDDO
!!$          ENDDO
!!$       ENDDO
1552
#endif
Matthias Redies's avatar
Matthias Redies committed
1553
         else
1554
#ifdef CPP_IRCOULOMBAPPROX