wavefproducts.F90 115 KB
Newer Older
1 2 3 4 5
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------
6 7 8 9 10 11 12 13 14 15 16
MODULE m_wavefproducts
   USE m_judft
   PRIVATE
   PUBLIC wavefproducts_noinv, wavefproducts_noinv5
   PUBLIC wavefproducts_inv, wavefproducts_inv5
CONTAINS

   SUBROUTINE wavefproducts_noinv(bandi, bandf, nk, iq, dimension, input, jsp,&                  !cprod,&
  &                 cell, atoms, hybrid, hybdat,&
  &                 kpts, mnobd,&
  &                 lapw, sym, noco, nbasm_mt, nkqpt, cprod)
17 18

      USE m_constants
19
      USE m_util, ONLY: modulo1
20 21
      USE m_wrapper
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
22
      USE m_io_hybrid
23 24
      IMPLICIT NONE

25 26 27 28 29 30 31 32 33 34
      TYPE(t_input), INTENT(IN)       :: input
      TYPE(t_dimension), INTENT(IN)   :: dimension
      TYPE(t_hybrid), INTENT(IN)      :: hybrid
      TYPE(t_sym), INTENT(IN)         :: sym
      TYPE(t_noco), INTENT(IN)        :: noco
      TYPE(t_cell), INTENT(IN)        :: cell
      TYPE(t_kpts), INTENT(IN)        :: kpts
      TYPE(t_atoms), INTENT(IN)       :: atoms
      TYPE(t_lapw), INTENT(IN)        :: lapw
      TYPE(t_hybdat), INTENT(INOUT)   :: hybdat
35 36

!     - scalars -
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      INTEGER, INTENT(IN)      ::  nk, iq, jsp
      INTEGER, INTENT(IN)      :: mnobd
      INTEGER, INTENT(IN)      :: nbasm_mt
      INTEGER, INTENT(IN)      ::  bandi, bandf
      INTEGER, INTENT(OUT)     ::  nkqpt

!     - arrays -

      COMPLEX, INTENT(OUT)    ::  cprod(hybrid%maxbasm1, mnobd, bandf - bandi + 1)

!     - local scalars -
      INTEGER                 ::  ic, l, n, l1, l2, n1, n2, lm_0, lm1_0, lm2_0, lm, lm1, lm2, m1, m2, i, j, ll
      INTEGER                 ::  itype, ieq, ikpt, ikpt1, ikpt2, igpt, igptp, igpt1, igpt2, iband, iband1, iband2
      INTEGER                 ::  k, ic1, ioffset, ibando
      INTEGER                 ::  q, idum, m
52
      INTEGER                 ::  nbasm_ir
53
      INTEGER                 ::  nbasmmt, nbasfcn
54
      INTEGER                 ::  ok
55 56 57 58
      REAL                    ::  rdum, svol, s2, pi
      REAL                    ::  mtthr = 0
      COMPLEX                 ::  cdum, cdum0
      COMPLEX, PARAMETER       ::  img = (0.0, 1.0)
Daniel Wortmann's avatar
Daniel Wortmann committed
59

60
      LOGICAL                 ::  offdiag
61 62
!      - local arrays -
      INTEGER                 ::  iarr(lapw%nv(jsp))
63 64 65
      INTEGER                 ::  g(3), ghelp(3), lmstart(0:atoms%lmaxd, atoms%ntype)
      INTEGER                 ::  gpthlp(3, dimension%nvd), nvhlp(input%jspins)
      INTEGER                 ::  gpt_nk(3, lapw%nv(jsp))
66 67 68 69
      INTEGER                 ::  g_t(3)
      INTEGER                 ::  gsum(3)
      REAL                    ::  bkpt(3)
      REAL                    ::  bkhlp(3)
70 71
      REAL                    ::  kqpt(3), kqpthlp(3)
      COMPLEX                 ::  carr(1:mnobd, bandf - bandi + 1)
72 73 74
!      COMPLEX                 :: chelp(maxbasm,mnobd,bandf-bandi+1,nkpt_EIBZ)
      COMPLEX                 ::  cexp
      COMPLEX                 ::  z_help(lapw%nv(jsp))
75 76 77 78
      COMPLEX                 ::  cmt(dimension%neigd, hybrid%maxlmindx, atoms%nat)
      COMPLEX                 ::  cmt_nk(dimension%neigd, hybrid%maxlmindx, atoms%nat)
      COMPLEX, ALLOCATABLE     ::  cprod_ir(:, :, :)
      TYPE(t_mat)             :: z_nk, z_kqpt
79
      TYPE(t_lapw)            :: lapw_nkqpt
80
      CALL timestart("wavefproducts_noinv")
81

82 83 84
      ! preparations

      svol = sqrt(cell%omtil)
85
      s2 = sqrt(2.0)
86

87 88 89
      gpt_nk(1, :) = lapw%k1(:lapw%nv(jsp), jsp)
      gpt_nk(2, :) = lapw%k2(:lapw%nv(jsp), jsp)
      gpt_nk(3, :) = lapw%k3(:lapw%nv(jsp), jsp)
90 91 92 93

      !
      ! compute k+q point for given q point in EIBZ(k)
      !
94
      kqpthlp = kpts%bkf(:, nk) + kpts%bkf(:, iq)
95 96
      ! k+q can lie outside the first BZ, transfer
      ! it back into the 1. BZ
97 98
      kqpt = modulo1(kqpthlp, kpts%nkpt3)
      g_t(:) = nint(kqpt - kqpthlp)
99
      ! determine number of kqpt
100 101 102 103 104 105
      nkqpt = 0
      DO ikpt = 1, kpts%nkptf
         IF (maxval(abs(kqpt - kpts%bkf(:, ikpt))) <= 1E-06) THEN
            nkqpt = ikpt
            EXIT
         END IF
106
      END DO
107
      IF (nkqpt == 0) STOP 'wavefproducts: k-point not found'
108 109

      ! lmstart = lm start index for each l-quantum number and atom type (for cmt-coefficients)
110 111 112 113
      DO itype = 1, atoms%ntype
         DO l = 0, atoms%lmax(itype)
            lmstart(l, itype) = sum((/(hybrid%nindx(ll, itype)*(2*ll + 1), ll=0, l - 1)/))
         END DO
114 115 116
      END DO

      nbasm_ir = maxval(hybrid%ngptm)
117 118
      ALLOCATE (cprod_ir(bandf - bandi + 1, mnobd, nbasm_ir), stat=ok)
      IF (ok /= 0) STOP 'wavefproducts: failure allocation cprod_ir'
119
      cprod_ir = 0
120

121
      cprod = 0
122 123 124 125 126 127 128

      CALL lapw_nkqpt%init(input, noco, kpts, atoms, sym, nkqpt, cell, sym%zrfs)
      nbasfcn = MERGE(lapw%nv(1) + lapw%nv(2) + 2*atoms%nlotot, lapw%nv(1) + atoms%nlotot, noco%l_noco)
      call z_nk%alloc(.false., nbasfcn, dimension%neigd)
      nbasfcn = MERGE(lapw_nkqpt%nv(1) + lapw_nkqpt%nv(2) + 2*atoms%nlotot, lapw_nkqpt%nv(1) + atoms%nlotot, noco%l_noco)
      call z_kqpt%alloc(.false., nbasfcn, dimension%neigd)

129 130
      call read_z(z_nk, kpts%nkptf*(jsp - 1) + nk)
      call read_z(z_kqpt, kpts%nkptf*(jsp - 1) + nkqpt)
131

132
      ! read in cmt coefficients from direct access file cmt
133 134
      call read_cmt(cmt_nk, nk)

135
      ! IR contribution
136

137 138
      CALL timestart("wavefproducts_noinv IR")

139 140 141 142 143 144 145 146 147 148
      DO igpt = 1, hybrid%ngptm(iq)
         igptp = hybrid%pgptm(igpt, iq)
         ghelp = hybrid%gptm(:, igptp) - g_t(:)
         DO i = 1, lapw%nv(jsp)
            gsum(:) = ghelp + gpt_nk(:, i)
            IF (all(abs(gsum) <= hybdat%pntgptd)) THEN
               iarr(i) = hybdat%pntgpt(gsum(1), gsum(2), gsum(3), nkqpt)
            ELSE
               iarr(i) = 0
            END IF
149

150
         END DO
151

152
         DO iband1 = 1, hybrid%nobd(nkqpt,jsp)
153 154 155 156 157
            where (iarr > 0)
            z_help(:) = z_kqpt%data_c(iarr(:), iband1)
            elsewhere
            z_help = 0.0
            end where
158

159 160 161
            DO iband = bandi, bandf
               cprod_ir(iband, iband1, igpt) = 1/svol*dotprod(z_nk%data_c(:lapw%nv(jsp), iband), z_help)
            END DO !iband
162

163
         END DO  !iband1
164 165 166 167 168 169

      END DO !igpt
      CALL timestop("wavefproducts_noinv IR")

      !
      ! MT contribution
170 171 172 173
      !
      call read_cmt(cmt, nkqpt)
      lm_0 = 0
      ic = 0
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      DO itype = 1, atoms%ntype
         DO ieq = 1, atoms%neq(itype)
            ic = ic + 1
            ic1 = 0

            cexp = exp(-2*img*pi_const*dot_product(kpts%bkf(:, iq), atoms%taual(:, ic)))

            DO l = 0, hybrid%lcutm1(itype)
               DO n = 1, hybdat%nindxp1(l, itype) ! loop over basis-function products

                  l1 = hybdat%prod(n, l, itype)%l1 !
                  l2 = hybdat%prod(n, l, itype)%l2 ! current basis-function product
                  n1 = hybdat%prod(n, l, itype)%n1 ! = bas(:,n1,l1,itype)*bas(:,n2,l2,itype) = b1*b2
                  n2 = hybdat%prod(n, l, itype)%n2 !

                  IF (mod(l1 + l2 + l, 2) /= 0) cycle
191

192 193
                  offdiag = l1 /= l2 .or. n1 /= n2 ! offdiag=true means that b1*b2 and b2*b1 are different combinations
                  !(leading to the same basis-function product)
194

195 196
                  lm1_0 = lmstart(l1, itype) ! start at correct lm index of cmt-coefficients
                  lm2_0 = lmstart(l2, itype) ! (corresponding to l1 and l2)
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                  lm = lm_0
                  DO m = -l, l

                     carr = 0.0

                     lm1 = lm1_0 + n1 ! go to lm index for m1=-l1
                     DO m1 = -l1, l1
                        m2 = m1 + m ! Gaunt condition -m1+m2-m=0
                        IF (abs(m2) <= l2) THEN
                           lm2 = lm2_0 + n2 + (m2 + l2)*hybrid%nindx(l2, itype)
                           rdum = hybdat%gauntarr(1, l1, l2, l, m1, m) ! precalculated Gaunt coefficient
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 cdum = rdum*conjg(cmt_nk(iband, lm1, ic)) !nk
                                 DO iband1 = 1, mnobd
                                    carr(iband1, iband) = carr(iband1, iband) + cdum*cmt(iband1, lm2, ic) !ikpt

                                 END DO
                              END DO
                           END IF
                        END IF

                        m2 = m1 - m ! switch role of b1 and b2
                        IF (abs(m2) <= l2 .and. offdiag) THEN
                           lm2 = lm2_0 + n2 + (m2 + l2)*hybrid%nindx(l2, itype)
                           rdum = hybdat%gauntarr(2, l1, l2, l, m1, m) ! precalculated Gaunt coefficient
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 cdum = rdum*conjg(cmt_nk(iband, lm2, ic)) !nk
                                 DO iband1 = 1, mnobd
                                    carr(iband1, iband) = carr(iband1, iband) + cdum*cmt(iband1, lm1, ic)
                                 END DO
                              END DO
                           END IF
                        END IF

                        lm1 = lm1 + hybrid%nindx(l1, itype) ! go to lm start index for next m1-quantum number

                     END DO  !m1

                     DO iband = bandi, bandf
                        DO iband1 = 1, mnobd
                           cdum = carr(iband1, iband)*cexp
                           DO i = 1, hybrid%nindxm1(l, itype)
                              j = lm + i
                              cprod(j, iband1, iband) = cprod(j, iband1, iband) + hybdat%prodm(i, n, l, itype)*cdum
                           END DO

                        END DO
                     END DO

                     lm = lm + hybrid%nindxm1(l, itype) ! go to lm start index for next m-quantum number
250 251 252

                  END DO

253 254 255
               END DO
               lm_0 = lm_0 + hybrid%nindxm1(l, itype)*(2*l + 1) ! go to the lm start index of the next l-quantum number
               IF (lm /= lm_0) STOP 'wavefproducts: counting of lm-index incorrect (bug?)'
256
            END DO
257
         END DO
258
      END DO
259

260 261 262
      CALL timestop("wavefproducts_noinv")

      ic = nbasm_mt
263 264 265 266 267 268 269
      DO igpt = 1, hybrid%ngptm(iq)
         ic = ic + 1
         DO ibando = 1, mnobd
            DO iband = bandi, bandf
               cprod(ic, ibando, iband) = cprod_ir(iband, ibando, igpt)
            END DO
         END DO
270 271
      END DO

272
   END SUBROUTINE wavefproducts_noinv
273

274 275 276 277 278 279 280
   SUBROUTINE wavefproducts_inv(&
  &                  bandi, bandf, dimension, input, jsp, atoms,&
  &                  lapw, kpts,&
  &                  nk, iq, hybdat, mnobd, hybrid,&
  &                  parent, cell,&
  &                  nbasm_mt, sym, noco,&
  &                  nkqpt, cprod)
281

282
      USE m_util, ONLY: modulo1
283
      USE m_wrapper
284
      USE m_constants
285
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
286
      USE m_io_hybrid
287
      IMPLICIT NONE
288 289 290 291 292 293 294 295 296 297
      TYPE(t_hybdat), INTENT(IN)   :: hybdat
      TYPE(t_dimension), INTENT(IN)   :: dimension
      TYPE(t_input), INTENT(IN)   :: input
      TYPE(t_hybrid), INTENT(IN)   :: hybrid
      TYPE(t_sym), INTENT(IN)   :: sym
      TYPE(t_noco), INTENT(IN)   :: noco
      TYPE(t_cell), INTENT(IN)   :: cell
      TYPE(t_kpts), INTENT(IN)   :: kpts
      TYPE(t_atoms), INTENT(IN)   :: atoms
      TYPE(t_lapw), INTENT(IN)   :: lapw
298 299

      ! - scalars -
300 301 302 303 304
      INTEGER, INTENT(IN)      ::    bandi, bandf
      INTEGER, INTENT(IN)      ::    jsp, nk, iq
      INTEGER, INTENT(IN)      ::    mnobd
      INTEGER, INTENT(IN)      ::    nbasm_mt
      INTEGER, INTENT(OUT)     ::    nkqpt
305 306

      ! - arrays -
307
      INTEGER, INTENT(IN)      ::    parent(kpts%nkptf)
308

309
      REAL, INTENT(OUT)       ::    cprod(hybrid%maxbasm1, mnobd, bandf - bandi + 1)
310 311

      ! - local scalars -
312 313 314 315
      INTEGER                 ::    i, ikpt, ic, iband, iband1, igpt, igptp, ibando, iatom, iiatom, itype, ieq, ishift, ioffset, iatom1, iatom2
      INTEGER                 ::    l, p, l1, m1, l2, m2, p1, p2, n, ok
      INTEGER                 ::    lm, lm1, lm2, lm_0, lm_00, lm1_0, lm2_0, lmp1, lmp2, lmp3, lmp4, lp1, lp2
      INTEGER                 ::    j, ll, m, nbasfcn
316
      INTEGER                 :: nbasm_ir
317 318 319 320 321 322
      REAL                    ::    svol, sr2
      REAL                    ::    rdum, rfac, rfac1, rfac2, rdum1, rdum2
      REAL                    ::    sin1, sin2, cos1, cos2, add1, add2
      REAL                    ::    fac, fac1, fac2
      REAL                    ::    monepl1, monepl2, monepl, monepm1, monepm, moneplm, monepl1m1
      COMPLEX, PARAMETER       ::    img = (0.0, 1.0)
323
      COMPLEX                 ::    fexp
324
      COMPLEX                 ::    cdum, cconst, cfac
325 326 327 328
      LOGICAL                 ::    offdiag

      ! - local arrays -
      INTEGER                 ::    iarr(lapw%nv(jsp))
329
      INTEGER                 ::    gpt_nk(3, lapw%nv(jsp)), ghelp(3)
330 331
      INTEGER                 ::    gsum(3)
      INTEGER                 ::    g_t(3)
332 333 334
      INTEGER                 ::    lmstart(0:atoms%lmaxd, atoms%ntype)
      INTEGER                 ::    lmstart2(0:hybrid%maxlcutm1, atoms%nat)
      REAL                    ::    kqpt(3), kqpthlp(3)
335

336
      REAL, ALLOCATABLE        ::    cprod_ir(:, :, :)
337 338 339

      REAL                    ::    z_help(lapw%nv(jsp))

340 341
      REAL                    ::    cmt_nk(dimension%neigd, hybrid%maxlmindx, atoms%nat)
      REAL                    ::    cmt(dimension%neigd, hybrid%maxlmindx, atoms%nat)
342

343 344
      COMPLEX                 ::    ccmt_nk(dimension%neigd, hybrid%maxlmindx, atoms%nat)
      COMPLEX                 ::    ccmt(dimension%neigd, hybrid%maxlmindx, atoms%nat)
345

346 347 348 349 350
      REAL                    ::    rarr1(1:mnobd, bandf - bandi + 1)
      REAL                    ::    rarr(2, 1:mnobd, bandf - bandi + 1)
      COMPLEX                 ::    cmthlp(dimension%neigd), cmthlp1(dimension%neigd)
      COMPLEX                 ::    cexp(atoms%nat), cexp_nk(atoms%nat)
      TYPE(t_mat)             :: z_nk, z_kqpt
351
      TYPE(t_lapw)            :: lapw_nkqpt
352 353 354

      CALL timestart("wavefproducts_inv")
      CALL timestart("wavefproducts_inv IR")
355 356 357
      svol = sqrt(cell%omtil)
      sr2 = sqrt(2.0)

358
      nbasm_ir = maxval(hybrid%ngptm)
359
      ALLOCATE (cprod_ir(bandf - bandi + 1, mnobd, nbasm_ir))
360
      cprod_ir = 0
361 362 363
      gpt_nk(1, :) = lapw%k1(:lapw%nv(jsp), jsp)
      gpt_nk(2, :) = lapw%k2(:lapw%nv(jsp), jsp)
      gpt_nk(3, :) = lapw%k3(:lapw%nv(jsp), jsp)
364 365 366 367

      !
      ! compute k+q point for q (iq) in EIBZ(k)
      !
368 369

      kqpthlp = kpts%bkf(:, nk) + kpts%bkf(:, iq)
370
      ! kqpt can lie outside the first BZ, transfer it back
371 372
      kqpt = modulo1(kqpthlp, kpts%nkpt3)
      g_t(:) = nint(kqpt - kqpthlp)
373 374
      ! determine number of kqpt
      nkqpt = 0
375 376 377 378 379
      DO ikpt = 1, kpts%nkptf
         IF (maxval(abs(kqpt - kpts%bkf(:, ikpt))) <= 1E-06) THEN
            nkqpt = ikpt
            EXIT
         END IF
380
      END DO
381
      IF (nkqpt == 0) STOP 'wavefproducts: k-point not found'
382 383 384

      ! read in z at current k-point nk

385 386 387 388 389 390
      CALL lapw_nkqpt%init(input, noco, kpts, atoms, sym, nkqpt, cell, sym%zrfs)
      nbasfcn = MERGE(lapw%nv(1) + lapw%nv(2) + 2*atoms%nlotot, lapw%nv(1) + atoms%nlotot, noco%l_noco)
      call z_nk%alloc(.true., nbasfcn, dimension%neigd)
      nbasfcn = MERGE(lapw_nkqpt%nv(1) + lapw_nkqpt%nv(2) + 2*atoms%nlotot, lapw_nkqpt%nv(1) + atoms%nlotot, noco%l_noco)
      call z_kqpt%alloc(.true., nbasfcn, dimension%neigd)

391 392
      call read_z(z_nk, kpts%nkptf*(jsp - 1) + nk)
      call read_z(z_kqpt, kpts%nkptf*(jsp - 1) + nkqpt)
393

394
      DO igpt = 1, hybrid%ngptm(iq)
395 396 397 398 399 400 401 402 403 404 405 406
         igptp = hybrid%pgptm(igpt, iq)
         ghelp = hybrid%gptm(:, igptp) - g_t(:)
         DO i = 1, lapw%nv(jsp)
            gsum(:) = ghelp + gpt_nk(:, i)
            IF (all(abs(gsum) <= hybdat%pntgptd)) THEN
               iarr(i) = hybdat%pntgpt(gsum(1), gsum(2), gsum(3), nkqpt)
            ELSE
               iarr(i) = 0
            END IF

         END DO

407
         DO iband1 = 1, hybrid%nobd(nkqpt,jsp)
408 409 410 411 412 413 414 415 416 417 418
            where (iarr > 0)
            z_help(:) = z_kqpt%data_r(iarr(:), iband1)
            elsewhere
            z_help = 0.0
            end where
            DO iband = bandi, bandf
               cprod_ir(iband, iband1, igpt) = 1/svol*dotprod(z_nk%data_r(:lapw%nv(jsp), iband), z_help)

            END DO !iband

         END DO  !iband1
419 420 421 422

      END DO !igpt

      CALL timestop("wavefproducts_inv IR")
423

424
      ! lmstart = lm start index for each l-quantum number and atom type (for cmt-coefficients)
425 426 427 428
      DO itype = 1, atoms%ntype
         DO l = 0, atoms%lmax(itype)
            lmstart(l, itype) = sum((/(hybrid%nindx(ll, itype)*(2*ll + 1), ll=0, l - 1)/))
         END DO
429 430 431
      END DO

      ! read in cmt coefficient at k-point nk
Daniel Wortmann's avatar
Daniel Wortmann committed
432

433
      CALL read_cmt(ccmt_nk, nk)
434 435

      !read in cmt coefficients at k+q point
436
      call read_cmt(ccmt, nkqpt)
437 438

      iatom = 0
439 440 441 442 443 444 445 446
      DO itype = 1, atoms%ntype
         DO ieq = 1, atoms%neq(itype)
            iatom = iatom + 1

            cexp(iatom) = exp((-img)*tpi_const*dotprod(kpts%bkf(:, iq) + kpts%bkf(:, nk), atoms%taual(:, iatom)))

            cexp_nk(iatom) = exp((-img)*tpi_const*dotprod(kpts%bkf(:, nk), atoms%taual(:, iatom)))
         END DO
447 448 449 450 451
      END DO

      rfac = 1./sr2
      cfac = -img/sr2
      iatom = 0
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
      DO itype = 1, atoms%ntype
         DO ieq = 1, atoms%neq(itype)
            iatom = iatom + 1
            ! determine the number of the inverse symmetric atom belonging to iatom
            IF (sym%invsatnr(iatom) == 0) THEN
               iiatom = iatom
            ELSE
               iiatom = sym%invsatnr(iatom)
            END IF
            ! the cmt coefficients at iatom and iiatom are made real in one step
            IF (iiatom < iatom) CYCLE
            lm1 = 0
            DO l = 0, atoms%lmax(itype)
               DO m = -l, l
                  DO p = 1, hybrid%nindx(l, itype)
                     lm1 = lm1 + 1
                     ! lm index at l,-m
                     lm2 = lm1 - 2*m*hybrid%nindx(l, itype)

                     IF (iatom == iiatom) THEN
                        IF (m < 0) THEN
                           cmt(:, lm1, iatom) = (ccmt(:, lm1, iatom) + (-1)**(l + m)*ccmt(:, lm2, iiatom))*cexp(iatom)*rfac

                           cmt_nk(:, lm1, iatom) = (ccmt_nk(:, lm1, iatom) + (-1)**(l + m)*ccmt_nk(:, lm2, iiatom))*cexp_nk(iatom)*rfac
                        ELSE IF (m > 0) THEN

                           cmt(:, lm1, iatom) = (ccmt(:, lm1, iatom) - (-1)**(l + m)*ccmt(:, lm2, iiatom))*cexp(iatom)*cfac

                           cmt_nk(:, lm1, iatom) = (ccmt_nk(:, lm1, iatom) - (-1)**(l + m)*ccmt_nk(:, lm2, iiatom))*cexp_nk(iatom)*cfac
                        ELSE
                           IF (mod(l, 2) == 0) THEN
                              cmt(:, lm1, iatom) = ccmt(:, lm1, iatom)*cexp(iatom)
                              cmt_nk(:, lm1, iatom) = ccmt_nk(:, lm1, iatom)*cexp_nk(iatom)
                           ELSE
                              cmt(:, lm1, iatom) = ccmt(:, lm1, iatom)*(-img)*cexp(iatom)
                              cmt_nk(:, lm1, iatom) = ccmt_nk(:, lm1, iatom)*(-img)*cexp_nk(iatom)
                           END IF
                        END IF
                     ELSE
                        cmt(:, lm1, iatom) = (ccmt(:, lm1, iatom) + (-1)**(l + m)*ccmt(:, lm2, iiatom))*rfac
492

493
                        cmt(:, lm1, iiatom) = (ccmt(:, lm1, iatom) - (-1)**(l + m)*ccmt(:, lm2, iiatom))*cfac
494

495 496 497 498 499 500 501 502
                        cmt_nk(:, lm1, iatom) = (ccmt_nk(:, lm1, iatom) + (-1)**(l + m)*ccmt_nk(:, lm2, iiatom))*rfac

                        cmt_nk(:, lm1, iiatom) = (ccmt_nk(:, lm1, iatom) - (-1)**(l + m)*ccmt_nk(:, lm2, iiatom))*cfac
                     END IF

                  END DO
               END DO
            END DO
503

504 505
         END DO
      END DO
506

507
      cprod = 0.0
508

509 510
      lm_0 = 0
      lm_00 = 0
511 512 513
      iatom1 = 0
      iiatom = 0

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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
      DO itype = 1, atoms%ntype
         ioffset = sum((/((2*ll + 1)*hybrid%nindxm1(ll, itype), ll=0, hybrid%lcutm1(itype))/))
         lm_0 = lm_00
         DO ieq = 1, atoms%neq(itype)
            iatom1 = iatom1 + 1
            IF (sym%invsatnr(iatom1) == 0) THEN
               iatom2 = iatom1
            ELSE
               iatom2 = sym%invsatnr(iatom1)
            END IF
            IF (iatom1 > iatom2) CYCLE

            IF (iatom1 /= iatom2) THEN
               ! loop over l of mixed basis
               DO l = 0, hybrid%lcutm1(itype)
                  ! loop over basis functions products, which belong to l
                  DO n = 1, hybdat%nindxp1(l, itype)

                     ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
                     l1 = hybdat%prod(n, l, itype)%l1
                     l2 = hybdat%prod(n, l, itype)%l2
                     p1 = hybdat%prod(n, l, itype)%n1
                     p2 = hybdat%prod(n, l, itype)%n2

                     ! condition for Gaunt coefficients
                     IF (mod(l + l1 + l2, 2) /= 0) CYCLE

                     offdiag = l1 /= l2 .or. p1 /= p2 ! offdiag=true means that b1*b2 and b2*b1 are different combinations
                     !(leading to the same basis-function product)

                     lm1_0 = lmstart(l1, itype) ! start at correct lm index of cmt-coefficients
                     lm2_0 = lmstart(l2, itype) ! (corresponding to l1 and l2)

                     lm = lm_0
                     lp1 = lm1_0 + p1
                     lp2 = lm2_0 + p2

                     ! sum over different m of mixed basis functions with qn l
                     DO m = -l, l
                        rarr = 0.0

                        ! go to lm index for m1=-l1
                        lmp1 = lm1_0 + p1

                        DO m1 = -l1, l1
                           ! Gaunt condition -m1+m2-m=0
                           m2 = m1 + m
                           IF (abs(m2) <= l2) THEN
                              lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                              ! precalculated Gaunt coefficient
                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, m)
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)
                                    rdum2 = rdum*cmt_nk(iband, lmp1, iatom2)
                                    ! loop over occupied bands
                                    DO ibando = 1, mnobd!hybrid%nobd(peibz(ikpt))

                                       rarr(1, ibando, iband) = rarr(1, ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1) + rdum2*cmt(ibando, lmp2, iatom2)

                                       rarr(2, ibando, iband) = rarr(2, ibando, iband) + rdum1*cmt(ibando, lmp2, iatom2) - rdum2*cmt(ibando, lmp2, iatom1)

                                    END DO  !ibando
                                 END DO  !iband
                              END IF  ! rdum
                           END IF  ! abs(m2) .le. l2

                           m2 = m1 - m ! switch role of b1 and b2
                           IF (abs(m2) <= l2 .and. offdiag) THEN
                              lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                              rdum = hybdat%gauntarr(2, l1, l2, l, m1, m) ! precalculated Gaunt coefficient
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)
                                    rdum2 = rdum*cmt_nk(iband, lmp2, iatom2)
                                    ! loop over occupied bands
                                    DO ibando = 1, mnobd!hybrid%nobd(peibz(ikpt)
                                       rarr(1, ibando, iband) = rarr(1, ibando, iband) + rdum1*cmt(ibando, lmp1, iatom1) + rdum2*cmt(ibando, lmp1, iatom2)

                                       rarr(2, ibando, iband) = rarr(2, ibando, iband) + rdum1*cmt(ibando, lmp1, iatom2) - rdum2*cmt(ibando, lmp1, iatom1)
                                    END DO  !ibando
                                 END DO  !iband
                              END IF  ! rdum .ne. 0
                           END IF  ! abs(m2) .le. l2 .and. offdiag

                           ! go to lmp start index for next m1-quantum number
                           lmp1 = lmp1 + hybrid%nindx(l1, itype)

                        END DO  !m1

                        ishift = -2*m*hybrid%nindxm1(l, itype)

                        ! go to lm mixed basis startindx for l and m
                        lm1 = lm + (iatom1 - 1 - iiatom)*ioffset
                        lm2 = lm + (iatom2 - 1 - iiatom)*ioffset + ishift

                        rdum = tpi_const*dotprod(kpts%bkf(:, iq), atoms%taual(:, iatom1))
                        rfac1 = sin(rdum)/sr2
                        rfac2 = cos(rdum)/sr2
                        DO iband = bandi, bandf
                           DO ibando = 1, mnobd
                              rdum1 = rarr(1, ibando, iband)
                              rdum2 = rarr(2, ibando, iband)
617 618 619 620
!                       sin1  = rdum1*rfac1
!                       cos1  = rdum1*rfac2
!                       sin2  = rdum2*rfac1
!                       cos2  = rdum2*rfac2
621 622 623 624 625 626 627 628 629 630 631
                              add1 = rdum1*rfac2 + rdum2*rfac1
                              add2 = rdum2*rfac2 - rdum1*rfac1
                              DO i = 1, hybrid%nindxm1(l, itype)
                                 j = lm1 + i
                                 cprod(j, ibando, iband) = cprod(j, ibando, iband) + hybdat%prodm(i, n, l, itype)*add1!( cos1 + sin2 )
                                 j = lm2 + i
                                 cprod(j, ibando, iband) = cprod(j, ibando, iband) + hybdat%prodm(i, n, l, itype)*add2!( cos2 - sin1 )

                              END DO  !i -> loop over mixed basis functions
                           END DO  !ibando
                        END DO  !iband
632

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 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 693 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 726 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 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 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 842 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 896 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 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 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 1071 1072 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 1115 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 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
                        ! go to lm start index for next m-quantum number
                        lm = lm + hybrid%nindxm1(l, itype)

                     END DO  !m

                  END DO !n
                  lm_0 = lm_0 + hybrid%nindxm1(l, itype)*(2*l + 1) ! go to the lm start index of the next l-quantum number
                  IF (lm /= lm_0) STOP 'wavefproducts: counting of lm-index incorrect (bug?)'
               END DO !l

            ELSE !case: iatom1==iatom2

               ! loop over l of mixed basis
               monepl = -1
               DO l = 0, hybrid%lcutm1(itype)
                  monepl = -monepl
                  ! loop over basis functions products, which belong to l
                  DO n = 1, hybdat%nindxp1(l, itype)

                     ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
                     l1 = hybdat%prod(n, l, itype)%l1
                     l2 = hybdat%prod(n, l, itype)%l2
                     p1 = hybdat%prod(n, l, itype)%n1
                     p2 = hybdat%prod(n, l, itype)%n2

                     ! condition for Gaunt coefficients
                     IF (mod(l + l1 + l2, 2) /= 0) CYCLE

                     offdiag = l1 /= l2 .or. p1 /= p2 ! offdiag=true means that b1*b2 and b2*b1 are different combinations
                     !(leading to the same basis-function product)

                     lm1_0 = lmstart(l1, itype) ! start at correct lm index of cmt-coefficients
                     lm2_0 = lmstart(l2, itype) ! (corresponding to l1 and l2)

                     lm = lm_0
                     lp1 = lm1_0 + p1
                     lp2 = lm2_0 + p2

                     ! calculate (-1)**l1 and (-1)**l2 (monepl = minus one power l)
                     monepl1 = (-1)**l1
                     monepl2 = (-1)**l2

                     ! sum over different m of mixed basis functions with qn l

                     !
                     !case m<0
                     !

                     monepm = -monepl
                     DO m = -l, -1
                        monepm = -monepm
                        moneplm = monepl*monepm

                        ! calculate the contributions which are identical for m>0 and m <0
                        rarr1 = 0.0
                        IF (abs(m) <= l2) THEN
                           lmp1 = lp1 + l1*hybrid%nindx(l1, itype)
                           IF (mod(l1, 2) == 0) THEN
                              lmp2 = lp2 + (m + l2)*hybrid%nindx(l2, itype)
                           ELSE
                              lmp2 = lp2 + (-m + l2)*hybrid%nindx(l2, itype)
                           END IF

                           rdum = hybdat%gauntarr(1, l1, l2, l, 0, m)
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)
                                 IF (mod(l1, 2) /= 0) rdum1 = moneplm*rdum1
                                 DO ibando = 1, mnobd
                                    rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                 END DO  ! ibando
                              END DO  ! iband
                           END IF  ! rdum .ne. 0

                           IF (offdiag) THEN
                              rdum = hybdat%gauntarr(1, l2, l1, l, -m, m)
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)
                                    IF (mod(l1, 2) == 0) rdum1 = moneplm*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp1, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0
                           END IF  ! offdiag

                        END IF  ! abs(m) .le. l2

                        IF (abs(m) <= l1) THEN
                           IF (mod(l2, 2) == 0) THEN
                              lmp3 = lp1 + (m + l1)*hybrid%nindx(l1, itype)
                           ELSE
                              lmp3 = lp1 + (-m + l1)*hybrid%nindx(l1, itype)
                           END IF
                           lmp2 = lp2 + l2*hybrid%nindx(l2, itype)

                           rdum = hybdat%gauntarr(1, l1, l2, l, -m, m)
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 rdum1 = rdum*cmt_nk(iband, lmp3, iatom1)
                                 IF (mod(l2, 2) == 0) rdum1 = moneplm*rdum1
                                 DO ibando = 1, mnobd
                                    rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                 END DO  ! ibando
                              END DO  ! iband
                           END IF  ! rdum .ne. 0

                           IF (offdiag) THEN
                              rdum = hybdat%gauntarr(1, l2, l1, l, 0, m)
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)
                                    IF (mod(l2, 2) /= 0) rdum1 = moneplm*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0
                           END IF  ! offdiag

                        END IF  ! abs(m) .le. l2

                        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                        !go to lm index for m1=-l1
                        lmp1 = lp1
                        monepm1 = -monepl1
                        DO m1 = -l1, l1
                           monepm1 = -monepm1
                           IF (m1 == 0) THEN
                              lmp1 = lmp1 + hybrid%nindx(l1, itype)
                              CYCLE
                           END IF
                           ! (-1)**(l1+m1)
                           monepl1m1 = monepl1*monepm1
                           m2 = m1 + m
                           IF (abs(m2) <= l2 .and. m2 /= 0) THEN
                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, m)
                              IF (rdum /= 0) THEN
                                 IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                 ELSE
                                    lmp2 = lp2 + (-m2 + l2)*hybrid%nindx(l2, itype)
                                    fac = 1/2.*moneplm*monepl1m1*(sign(1, m2) - sign(1, m1))
                                 END IF
                                 rdum = rdum/sr2
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)!rdum*cmt_nk(iband,lmp1,iatom1)/sr2
                                    IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0

                              IF (offdiag) THEN
                                 rdum = hybdat%gauntarr(1, l2, l1, l, m2, -m)
                                 IF (rdum /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                       lmp3 = lmp1
                                    ELSE
                                       lmp3 = lmp1 - 2*m1*hybrid%nindx(l1, itype)
                                       fac = 1/2.*monepl1m1*(sign(1, m1) - sign(1, m2))
                                    END IF
                                    rdum = moneplm*rdum/sr2
                                    DO iband = bandi, bandf
                                       rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)!moneplm*rdum*cmt_nk(iband,lmp2,iatom1)/sr2
                                       IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                       DO ibando = 1, mnobd
                                          rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                       END DO  ! ibando
                                    END DO  ! iband
                                 END IF  ! rdum .ne. 0
                              END IF  ! offdiag

                           END IF  ! abs(m2) .le. l2 .and. m2 .ne. 0

                           m2 = m1 - m
                           IF (abs(m2) <= l2 .and. m2 /= 0) THEN

                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, -m)
                              IF (rdum /= 0) THEN

                                 IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                 ELSE
                                    lmp2 = lp2 + (-m2 + l2)*hybrid%nindx(l2, itype)
                                    fac = 1/2.*moneplm*monepl1m1*(sign(1, m2) - sign(1, m1))
                                 END IF
                                 rdum = moneplm*rdum/sr2
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)!moneplm*rdum*cmt_nk(iband,lmp1,iatom1)/sr2
                                    IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband

                              END IF  ! rdum .ne. 0

                              IF (offdiag) THEN
                                 rdum = hybdat%gauntarr(1, l2, l1, l, m2, m)
                                 IF (rdum /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    IF (sign(1, m1) + sign(1, m2) /= 0) THEN
                                       lmp3 = lmp1
                                    ELSE
                                       lmp3 = lmp1 - 2*m1*hybrid%nindx(l1, itype)
                                       fac = 1/2.*monepl1m1*(sign(1, m1) - sign(1, m2))
                                    END IF
                                    rdum = rdum/sr2
                                    DO iband = bandi, bandf
                                       rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)!rdum*cmt_nk(iband,lmp2,iatom1)/sr2
                                       IF (sign(1, m1) + sign(1, m2) == 0) rdum1 = fac*rdum1
                                       DO ibando = 1, mnobd
                                          rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                       END DO  ! ibando
                                    END DO  ! iband
                                 END IF  ! rdum .ne. 0
                              END IF  ! offdiag

                           END IF  ! abs(m2) .le. l2 .and. m1 .ne. 0

                           !go to lmp start index for next m1-quantum number
                           lmp1 = lmp1 + hybrid%nindx(l1, itype)
                        END DO  ! m1

                        ! go to lm mixed basis startindx for l and m
                        lm1 = lm + (iatom1 - 1 - iiatom)*ioffset
                        DO iband = bandi, bandf
                           DO ibando = 1, mnobd
                              rdum = rarr1(ibando, iband)
                              DO i = 1, hybrid%nindxm1(l, itype)
                                 j = lm1 + i
                                 cprod(j, ibando, iband) = cprod(j, ibando, iband) + hybdat%prodm(i, n, l, itype)*rdum
                              END DO  !i -> loop over mixed basis functions
                           END DO  !ibando
                        END DO  !iband

                        ! go to lm start index for next m-quantum number
                        lm = lm + hybrid%nindxm1(l, itype)

                     END DO  ! m=-l,-1

                     !
                     !case m=0
                     !

                     m = 0
                     rarr1 = 0.0
                     lmp1 = lp1

                     monepm1 = -monepl1

                     DO m1 = -l1, l1
                        m2 = m1
                        monepm1 = -monepm1
                        IF (abs(m2) <= l2) THEN

                           IF (mod(l, 2) == 0) THEN
                              lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                              !lmp3 and lmp4 are variables, which avoid an if clause in the loop
                              lmp3 = lmp2
                              lmp4 = lmp1
                           ELSE
                              lmp2 = lp2 + (-m2 + l2)*hybrid%nindx(l2, itype)
                              !lmp3 and lmp3 are variables, which avoid an if clause in the loop
                              lmp3 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                              lmp4 = lmp1 - 2*m1*hybrid%nindx(l1, itype)

                              fac1 = monepl1*monepm1 ! (-1)**(l1+m1)
                              fac2 = monepl2*monepm1 ! (-1)**(l2+m1)
                           END IF

                           !precalculated Gaunt coefficient
                           IF (mod(l, 2) == 0) THEN
                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, m)
                           ELSE
                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, m)*fac1
                           END IF
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)
                                 DO ibando = 1, mnobd
                                    rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                 END DO  ! ibando
                              END DO  ! iband
                           END IF  ! rdum.ne.0

                           !change role of b1 and b2
                           IF (offdiag) THEN
                              IF (mod(l, 2) == 0) THEN
                                 rdum = hybdat%gauntarr(2, l1, l2, l, m1, m)
                              ELSE
                                 rdum = hybdat%gauntarr(2, l1, l2, l, m1, m)*fac2
                              END IF
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp3, iatom1)
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp4, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum.ne.0
                           END IF  ! offdiag

                        END IF  ! abs(m2).le.l2

                        ! go to lmp start index for next m1-quantum number
                        lmp1 = lmp1 + hybrid%nindx(l1, itype)
                     END DO  !m1

                     ! go to lm mixed basis startindx for l and m
                     lm1 = lm + (iatom1 - 1 - iiatom)*ioffset
                     DO iband = bandi, bandf
                        DO ibando = 1, mnobd
                           rdum = rarr1(ibando, iband)
                           DO i = 1, hybrid%nindxm1(l, itype)
                              j = lm1 + i
                              cprod(j, ibando, iband) = cprod(j, ibando, iband) + hybdat%prodm(i, n, l, itype)*rdum
                           END DO  !i -> loop over mixed basis functions
                        END DO  !ibando
                     END DO  !iband

                     ! go to lm start index for next m-quantum number
                     lm = lm + hybrid%nindxm1(l, itype)

                     !
                     ! case: m>0
                     !

                     rarr1 = 0.0
                     monepm = 1
                     DO m = 1, l
                        monepm = -monepm
                        moneplm = monepl*monepm

                        ! calculate the contributions which are identical for m>0 and m <0
                        rarr1 = 0.0
                        IF (abs(m) <= l2) THEN
                           lmp1 = lp1 + l1*hybrid%nindx(l1, itype)
                           IF (mod(l1, 2) == 0) THEN
                              lmp2 = lp2 + (m + l2)*hybrid%nindx(l2, itype)
                           ELSE
                              lmp2 = lp2 + (-m + l2)*hybrid%nindx(l2, itype)
                           END IF

                           rdum = hybdat%gauntarr(1, l1, l2, l, 0, m)
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)
                                 IF (mod(l1, 2) /= 0) rdum1 = moneplm*rdum1
                                 DO ibando = 1, mnobd
                                    rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                 END DO  ! ibando
                              END DO  ! iband
                           END IF  ! rdum .ne. 0

                           IF (offdiag) THEN
                              rdum = hybdat%gauntarr(1, l2, l1, l, -m, m)
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)
                                    IF (mod(l1, 2) == 0) rdum1 = moneplm*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp1, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0
                           END IF  ! offdiag

                        END IF  ! abs(m) .le. l2

                        IF (abs(m) <= l1) THEN
                           IF (mod(l2, 2) == 0) THEN
                              lmp3 = lp1 + (m + l1)*hybrid%nindx(l1, itype)
                           ELSE
                              lmp3 = lp1 + (-m + l1)*hybrid%nindx(l1, itype)
                           END IF
                           lmp2 = lp2 + l2*hybrid%nindx(l2, itype)

                           rdum = hybdat%gauntarr(1, l1, l2, l, -m, m)
                           IF (rdum /= 0) THEN
                              DO iband = bandi, bandf
                                 rdum1 = rdum*cmt_nk(iband, lmp3, iatom1)
                                 IF (mod(l2, 2) == 0) rdum1 = moneplm*rdum1
                                 DO ibando = 1, mnobd
                                    rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                 END DO  ! ibando
                              END DO  ! iband
                           END IF  ! rdum .ne. 0

                           IF (offdiag) THEN
                              rdum = hybdat%gauntarr(1, l2, l1, l, 0, m)
                              IF (rdum /= 0) THEN
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)
                                    IF (mod(l2, 2) /= 0) rdum1 = moneplm*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0
                           END IF  ! offdiag

                        END IF  ! abs(m) .le. l2

                        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                        !go to lm index for m1=-l1
                        lmp1 = lp1
                        monepm1 = -monepl1
                        DO m1 = -l1, l1
                           monepm1 = -monepm1
                           IF (m1 == 0) THEN
                              lmp1 = lmp1 + hybrid%nindx(l1, itype)
                              CYCLE
                           END IF
                           m2 = m1 + m
                           ! (-1)**(l1+m1)
                           monepl1m1 = monepl1*monepm1
                           IF (abs(m2) <= l2 .and. m2 /= 0) THEN
                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, m)
                              IF (rdum /= 0) THEN

                                 IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                    lmp2 = lp2 + (-m2 + l2)*hybrid%nindx(l2, itype)
                                 ELSE
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    fac = -moneplm*monepl1m1*(sign(1, m2) - sign(1, m1))/2
                                 END IF

                                 rdum = -moneplm*monepl1m1*rdum/sr2
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)!-moneplm*monepl1m1*rdum*cmt_nk(iband,lmp1,iatom1)/sr2
                                    IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband

                              END IF  ! rdum .ne. 0

                              IF (offdiag) THEN
                                 rdum = hybdat%gauntarr(2, l1, l2, l, m1, -m)
                                 IF (rdum /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                       lmp3 = lmp1 - 2*m1*hybrid%nindx(l1, itype)
                                    ELSE
                                       lmp3 = lmp1
                                       fac = 1/2.*monepl1m1*(sign(1, m2) - sign(1, m1))
                                    END IF
                                    rdum = monepl1m1*moneplm*rdum/sr2
                                    DO iband = bandi, bandf
                                       rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)!monepl1m1*moneplm*rdum*cmt_nk(iband,lmp2,iatom1)/sr2
                                       IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                       DO ibando = 1, mnobd
                                          rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                       END DO  ! ibando
                                    END DO  ! iband

                                 END IF  ! rdum

                              END IF  ! offdiag
                           END IF  ! abs(m2) .le. l2 .and. m2 .ne. 0

                           m2 = m1 - m
                           IF (abs(m2) <= l2 .and. m2 /= 0) THEN

                              rdum = hybdat%gauntarr(1, l1, l2, l, m1, -m)
                              IF (rdum /= 0) THEN

                                 IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                    lmp2 = lp2 + (-m2 + l2)*hybrid%nindx(l2, itype)
                                 ELSE
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    fac = 1/2.*moneplm*monepl1m1*(sign(1, m1) - sign(1, m2))
                                 END IF
                                 rdum = monepl1m1*rdum/sr2
                                 DO iband = bandi, bandf
                                    rdum1 = rdum*cmt_nk(iband, lmp1, iatom1)!monepl1m1*rdum*cmt_nk(iband,lmp1,iatom1)/sr2
                                    IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = rdum1*fac
                                    DO ibando = 1, mnobd
                                       rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp2, iatom1)
                                    END DO  ! ibando
                                 END DO  ! iband
                              END IF  ! rdum .ne. 0

                              IF (offdiag) THEN
                                 rdum = hybdat%gauntarr(2, l1, l2, l, m1, m)
                                 IF (rdum /= 0) THEN
                                    lmp2 = lp2 + (m2 + l2)*hybrid%nindx(l2, itype)
                                    IF (sign(1, m2) + sign(1, m1) /= 0) THEN
                                       lmp3 = lmp1 - 2*m1*hybrid%nindx(l1, itype)
                                    ELSE
                                       lmp3 = lmp1
                                       fac = -monepl1m1*(sign(1, m1) - sign(1, m2))/2
                                    END IF
                                    rdum = -monepl1m1*rdum/sr2
                                    DO iband = bandi, bandf
                                       rdum1 = rdum*cmt_nk(iband, lmp2, iatom1)!-monepl1m1*rdum*cmt_nk(iband,lmp2,iatom1)/sr2
                                       IF (sign(1, m2) + sign(1, m1) == 0) rdum1 = fac*rdum1
                                       DO ibando = 1, mnobd
                                          rarr1(ibando, iband) = rarr1(ibando, iband) + rdum1*cmt(ibando, lmp3, iatom1)
                                       END DO  ! ibando
                                    END DO  ! iband

                                 END IF  ! rdum .ne. 0
                              END IF  ! offdiag

                           END IF  !  abs(m2) .le. l2 .and. m2 .ne. 0

                           !go to lmp start index for next m1-quantum number
                           lmp1 = lmp1 + hybrid%nindx(l1, itype)
                        END DO  ! m1

                        ! multiply rarr1 by (-1)**(l+m+1)
                        rarr1(:, :) = (-1)*moneplm*rarr1(:, :)

                        ! go to lm mixed basis startindx for l and m
                        lm1 = lm + (iatom1 - 1 - iiatom)*ioffset

                        DO iband = bandi, bandf
                           DO ibando = 1, mnobd
                              rdum = rarr1(ibando, iband)
                              DO i = 1, hybrid%nindxm1(l, itype)
                                 j = lm1 + i
                                 cprod(j, ibando, iband) = cprod(j, ibando, iband) + hybdat%prodm(i, n, l, itype)*rdum
                              END DO  !i -> loop over mixed basis functions
                           END DO  !ibando
                        END DO  !iband
1167

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
                        ! go to lm start index for next m-quantum number
                        lm = lm + hybrid%nindxm1(l, itype)

                     END DO  ! m=1,l

                  END DO !n
                  lm_0 = lm_0 + hybrid%nindxm1(l, itype)*(2*l + 1) ! go to the m start index of the next l-quantum number
                  IF (lm /= lm_0) STOP 'wavefproducts: counting of lm-index incorrect (bug?)'
               END DO !l

            END IF  ! iatom1 .ne. iatom2

            lm_0 = lm_00
         END DO !ieq
         iiatom = iiatom + atoms%neq(itype)
         lm_00 = lm_00 + atoms%neq(itype)*ioffset
      END DO  !itype
1185 1186

      ic = nbasm_mt
1187 1188 1189 1190 1191 1192 1193
      DO igpt = 1, hybrid%ngptm(iq)
         ic = ic + 1
         DO ibando = 1, mnobd
            DO iband = bandi, bandf
               cprod(ic, ibando, iband) = cprod_ir(iband, ibando, igpt)
            END DO
         END DO
1194 1195 1196
      END DO

      CALL timestop("wavefproducts_inv")
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

   END SUBROUTINE wavefproducts_inv

   SUBROUTINE wavefproducts_inv5(&
  &                    bandi, bandf, bandoi, bandof,&
  &                    dimension, input, jsp, atoms,&
  &                    lapw, kpts,&
  &                    nk, iq, hybdat, mnobd, hybrid,&
  &                    parent, cell,&
  &                    nbasm_mt, sym,&
  &                    noco,&
  &                    nkqpt, cprod)

      USE m_util, ONLY: modulo1
      USE m_olap, ONLY: gptnorm
1212 1213 1214
      USE m_wrapper
      USE m_constants
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
1215
      USE m_io_hybrid
1216
      IMPLICIT NONE
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
      TYPE(t_dimension), INTENT(IN) :: dimension
      TYPE(t_hybrid), INTENT(IN)    :: hybrid
      TYPE(t_input), INTENT(IN)     :: input
      TYPE(t_noco), INTENT(IN)      :: noco
      TYPE(t_sym), INTENT(IN)       :: sym
      TYPE(t_cell), INTENT(IN)      :: cell
      TYPE(t_kpts), INTENT(IN)      :: kpts
      TYPE(t_atoms), INTENT(IN)     :: atoms
      TYPE(t_lapw), INTENT(IN)      :: lapw
      TYPE(t_hybdat), INTENT(INOUT) :: hybdat
1227 1228

      ! - scalars -
1229 1230 1231 1232 1233
      INTEGER, INTENT(IN)      :: bandi, bandf, bandoi, bandof
      INTEGER, INTENT(IN)      :: jsp, nk, iq
      INTEGER, INTENT(IN)      :: mnobd
      INTEGER, INTENT(IN)      :: nbasm_mt
      INTEGER, INTENT(OUT)     :: nkqpt
1234 1235

      ! - arrays -
1236
      INTEGER, INTENT(IN)      ::    parent(kpts%nkptf)
1237

1238
      REAL, INTENT(OUT)        ::    cprod(hybrid%maxbasm1, bandoi:bandof, bandf - bandi + 1)
1239 1240

      ! - local scalars -
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
      INTEGER                 ::    i, ikpt, ic, iband, iband1, igpt, igptp, ig, ig2, ig1
      INTEGER                 ::    iatom, iiatom, itype, ieq, ishift
      INTEGER                 ::    ibando, iatom1, iatom2, ioffset
      INTEGER                 ::    k, l, p, l1, m1, l2, m2, p1, p2, n, ok
      INTEGER                 ::    igptm, iigptm
      INTEGER                 ::    lm, lm1, lm2, lm_0, lm_00, lm1_0, lm2_0, lmp1, lmp2, lmp3, lmp4, lp1, lp2
      INTEGER                 ::    j, ll, m
      INTEGER                 ::    nbasm_ir, ngpt0
      INTEGER                 ::    n1, n2, nbasfcn
      REAL                    ::    svol, sr2
      REAL                    ::    rdum, rfac, rfac1, rfac2, rdum1, rdum2
      REAL                    ::    sin1, sin2, cos1, cos2, add1, add2
      REAL                    ::    fac, fac1, fac2
      REAL                    ::    monepl1, monepl2, monepl, monepm1, monepm, moneplm, monepl1m1
1255
      COMPLEX                 ::    fexp
1256 1257
      COMPLEX                 ::    cdum, cconst, cfac
      COMPLEX, PARAMETER       ::    img = (0.0, 1.0)
1258
      LOGICAL                 ::    offdiag
Daniel Wortmann's avatar
Daniel Wortmann committed
1259
      TYPE(t_lapw)            ::    lapw_nkqpt
1260

1261
      ! - local arrays -
1262 1263 1264 1265
      INTEGER                 ::    g(3), g_t(3)
      INTEGER                 ::    lmstart(0:atoms%lmaxd, atoms%ntype)
      INTEGER, ALLOCATABLE    ::    gpt0(:, :)
      INTEGER, ALLOCATABLE    ::    pointer(:, :, :)
1266

1267
      REAL                    ::    kqpt(3), kqpthlp(3)
1268
      REAL                    ::    bkpt(3)
1269 1270
      REAL                    ::    cmt_nk(dimension%neigd, hybrid%maxlmindx, atoms%nat)
      REAL                    ::    cmt(dimension%neigd, hybrid%maxlmindx, atoms%nat)
1271
      REAL                    ::    rarr1(bandoi:bandof)
1272 1273 1274
      REAL                    ::    rarr2(bandoi:bandof, bandf - bandi + 1)
      REAL                    ::    rarr3(2, bandoi:bandof, bandf - bandi + 1)
      REAL, ALLOCATABLE       ::    z0(:, :)
1275

1276 1277 1278 1279
      COMPLEX                 ::    cexp(atoms%nat), cexp_nk(atoms%nat)
      COMPLEX, ALLOCATABLE     ::    ccmt_nk(:, :, :)
      COMPLEX, ALLOCATABLE     ::    ccmt(:, :, :)
      TYPE(t_mat)             :: z_nk, z_kqpt
1280 1281 1282

      CALL timestart("wavefproducts_inv5")
      CALL timestart("wavefproducts_inv5 IR")
1283

1284
      cprod = 0
1285 1286 1287
      svol = sqrt(cell%omtil)
      sr2 = sqrt(2.0)

1288
      nbasm_ir = maxval(hybrid%ngptm)
1289

1290 1291 1292
      !
      ! compute k+q point for q (iq) in EIBZ(k)
      !
1293 1294

      kqpthlp = kpts%bkf(:, nk) + kpts%bkf(:, iq)
1295
      ! kqpt can lie outside the first BZ, transfer it back
1296 1297
      kqpt = modulo1(kqpthlp, kpts%nkpt3)
      g_t(:) = nint(kqpt - kqpthlp)
1298 1299
      ! determine number of kqpt
      nkqpt = 0
1300 1301 1302 1303 1304
      DO ikpt = 1, kpts%nkptf
         IF (maxval(abs(kqpt - kpts%bkf(:, ikpt))) <= 1E-06) THEN
            nkqpt = ikpt
            EXIT
         END IF
1305
      END DO
1306 1307
      IF (nkqpt == 0) STOP 'wavefproducts_inv5: k-point not found'

1308 1309 1310
      !
      ! compute G's fulfilling |bk(:,nkqpt) + G| <= rkmax
      !
1311
      CALL lapw_nkqpt%init(input, noco, kpts, atoms, sym, nkqpt, cell, sym%zrfs)
1312

1313 1314 1315 1316
      nbasfcn = MERGE(lapw%nv(1) + lapw%nv(2) + 2*atoms%nlotot, lapw%nv(1) + atoms%nlotot, noco%l_noco)
      call z_nk%alloc(.true., nbasfcn, dimension%neigd)
      nbasfcn = MERGE(lapw_nkqpt%nv(1) + lapw_nkqpt%nv(2) + 2*atoms%nlotot, lapw_nkqpt%nv(1) + atoms%nlotot, noco%l_noco)
      call z_kqpt%alloc(.true., nbasfcn, dimension%neigd)
1317

Daniel Wortmann's avatar
Daniel Wortmann committed
1318
      ! read in z at k-point nk and nkqpt
1319
      call timestart("read_z")
1320 1321
      CALL read_z(z_nk, kpts%nkptf*(jsp - 1) + nk)
      call read_z(z_kqpt, kpts%nkptf*(jsp - 1) + nkqpt)
1322
      call timestop("read_z")
1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
      g(1) = maxval(abs(lapw%k1(:lapw%nv(jsp), jsp))) &
     &     + maxval(abs(lapw_nkqpt%k1(:lapw_nkqpt%nv(jsp), jsp)))&
     &     + maxval(abs(hybrid%gptm(1, hybrid%pgptm(:hybrid%ngptm(iq), iq)))) + 1
      g(2) = maxval(abs(lapw%k2(:lapw%nv(jsp), jsp)))&
     &     + maxval(abs(lapw_nkqpt%k2(:lapw_nkqpt%nv(jsp), jsp)))&
     &     + maxval(abs(hybrid%gptm(2, hybrid%pgptm(:hybrid%ngptm(iq), iq)))) + 1
      g(3) = maxval(abs(lapw%k3(:lapw%nv(jsp), jsp)))&
     &     + maxval(abs(lapw_nkqpt%k3(:lapw_nkqpt%nv(jsp), jsp)))&
     &     + maxval(abs(hybrid%gptm(3, hybrid%pgptm(:hybrid%ngptm(iq), iq)))) + 1

      ALLOCATE (pointer(-g(1):g(1), -g(2):g(2), -g(3):g(3)), stat=ok)
      IF (ok /= 0) STOP 'wavefproducts_inv5: error allocation pointer'
      ALLOCATE (gpt0(3, size(pointer)), stat=ok)
      IF (ok /= 0) STOP 'wavefproducts_inv5: error allocation gpt0'

      if (.not. allocated(hybdat%stepfunc_r)) then
1340
         call timestart("setup stepfunction")
1341 1342
         ALLOCATE (hybdat%stepfunc_r(-g(1):g(1), -g(2):g(2), -g(3):g(3)), stat=ok)
         IF (ok /= 0) then
1343 1344 1345
            call juDFT_error('wavefproducts_inv5: error allocation stepfunc_r')
         endif

1346 1347 1348 1349 1350 1351
         DO i = -g(1), g(1)
            DO j = -g(2), g(2)
               DO k = -g(3), g(3)
                  hybdat%stepfunc_r(i, j, k) = stepfunction(cell, atoms, (/i, j, k/))
               END DO
            END DO
1352 1353 1354
         END DO
         call timestop("setup stepfunction")
      endif
1355 1356 1357 1358

      !
      ! convolute phi(n,k) with the step function and store in cpw0
      !
1359

1360
      !(1) prepare list of G vectors
1361
      call timestart("prep list of Gvec")
1362
      pointer = 0
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
      ic = 0
      DO ig1 = 1, lapw%nv(jsp)
         DO igptm = 1, hybrid%ngptm(iq)
            iigptm = hybrid%pgptm(igptm, iq)
            g(1) = lapw%k1(ig1, jsp) + hybrid%gptm(1, iigptm) - g_t(1)
            g(2) = lapw%k2(ig1, jsp) + hybrid%gptm(2, iigptm) - g_t(2)
            g(3) = lapw%k3(ig1, jsp) + hybrid%gptm(3, iigptm) - g_t(3)
            IF (pointer(g(1), g(2), g(3)) == 0) THEN
               ic = ic + 1
               gpt0(:, ic) = g
               pointer(g(1), g(2), g(3)) = ic
            END IF
         END DO
1376 1377
      END DO
      ngpt0 = ic
1378
      call timestop("prep list of Gvec")
1379 1380

      !(2) calculate convolution
1381
      call timestart("calc convolution")
1382 1383
      ALLOCATE (z0(bandoi:bandof, ngpt0), stat=ok)
      IF (ok /= 0) STOP 'wavefproducts_inv5: error allocation z0'
1384
      z0 = 0
1385
      call timestart("step function")
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
      DO ig2 = 1, lapw_nkqpt%nv(jsp)
         rarr1 = z_kqpt%data_r(ig2, bandoi:bandof)
         DO ig = 1, ngpt0
            g(1) = gpt0(1, ig) - lapw_nkqpt%k1(ig2, jsp)
            g(2) = gpt0(2, ig) - lapw_nkqpt%k2(ig2, jsp)
            g(3) = gpt0(3, ig) - lapw_nkqpt%k3(ig2, jsp)
            rdum = hybdat%stepfunc_r(g(1), g(2), g(3))/svol
            DO n2 = bandoi, bandof
               z0(n2, ig) = z0(n2, ig) + rarr1(n2)*rdum
            END DO
         END DO
1397
      END DO
1398
      call timestop("step function")
1399

1400
      call timestart("hybrid gptm")
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
      ic = nbasm_mt
      DO igptm = 1, hybrid%ngptm(iq)
         rarr2 = 0
         ic = ic + 1
         iigptm = hybrid%pgptm(igptm, iq)

         DO ig1 = 1, lapw%nv(jsp)
            g(1) = lapw%k1(ig1, jsp) + hybrid%gptm(1, iigptm) - g_t(1)
            g(2) = lapw%k2(ig1, jsp) + hybrid%gptm(2, iigptm) - g_t(2)
            g(3) = lapw%k3(ig1, jsp) + hybrid%gptm(3, iigptm) - g_t(3)

            ig2 = pointer(g(1), g(2), g(3))

            IF (ig2 == 0) THEN
               STOP 'wavefproducts_inv5: pointer undefined'
            END IF

            DO n1 = 1, bandf - bandi + 1
               rdum1 = z_nk%data_r(ig1, n1)
               DO n2 = bandoi, bandof
                  rarr2(n2, n1) = rarr2(n2, n1) + rdum1*z0(n2, ig2)
               END DO
1423 1424
            END DO

1425 1426
         END DO
         cprod(ic, :, :) = rarr2(:, :)
1427
      END DO
1428 1429
      call timestop("hybrid gptm")
      call timestop("calc convolution")
1430

1431
      WRITE (2005, *) 'Point B'
1432 1433 1434
      DO n2 = 1, 1
         DO n1 = 1, 2
            DO ic = 1, 20
1435
               WRITE (2010, '(3i7,f15.8)') ic, n1, n2, cprod(ic, n1, n2)
1436 1437 1438 1439
            END DO
         END DO
      END DO

1440
      DEALLOCATE (z0, pointer, gpt0)
1441 1442 1443
      CALL timestop("wavefproducts_inv5 IR")

      ! lmstart = lm start index for each l-quantum number and atom type (for cmt-coefficients)
1444 1445 1446 1447
      DO itype = 1, atoms%ntype
         DO l = 0, atoms%lmax(itype)
            lmstart(l, itype) = sum((/(hybrid%nindx(ll, itype)*(2*ll + 1), ll=0, l - 1)/))
         END DO
1448 1449 1450
      END DO

      ! read in cmt coefficient at k-point nk