wavefproducts.F90 105 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------

7 8 9 10 11 12 13
      MODULE m_wavefproducts
        USE m_judft
        PRIVATE
      PUBLIC  wavefproducts_noinv, wavefproducts_noinv5
      PUBLIC  wavefproducts_inv, wavefproducts_inv5
      CONTAINS

14
      SUBROUTINE wavefproducts_noinv(bandi,bandf,nk,iq,dimension,input,jsp,&                  !cprod,&
Daniel Wortmann's avatar
Daniel Wortmann committed
15 16
     &                 cell,atoms,hybrid,hybdat,&
     &                 kpts,mnobd,&
17
     &                 lapw,sym,noco,nbasm_mt,nkqpt,cprod)
18 19 20 21 22 23


      USE m_constants
      USE m_util     , ONLY : modulo1
      USE m_wrapper
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
24
      USE m_io_hybrid
25 26
      IMPLICIT NONE

27
      TYPE(t_input),INTENT(IN)   :: input
Daniel Wortmann's avatar
Daniel Wortmann committed
28
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
29 30 31
      TYPE(t_dimension),INTENT(IN)   :: dimension
      TYPE(t_hybrid),INTENT(IN)   :: hybrid
      TYPE(t_sym),INTENT(IN)   :: sym
32
      TYPE(t_noco),INTENT(IN)   :: noco
33 34 35 36 37 38 39
      TYPE(t_cell),INTENT(IN)   :: cell
      TYPE(t_kpts),INTENT(IN)   :: kpts
      TYPE(t_atoms),INTENT(IN)   :: atoms
      TYPE(t_lapw),INTENT(IN)   :: lapw

!     - scalars -
      INTEGER,INTENT(IN)      ::  nk,iq ,jsp
Daniel Wortmann's avatar
Daniel Wortmann committed
40 41
      INTEGER,INTENT(IN)      :: mnobd 
      INTEGER,INTENT(IN)      :: nbasm_mt
42 43 44 45 46 47 48 49
      INTEGER,INTENT(IN)      ::  bandi,bandf
      INTEGER,INTENT(OUT)     ::  nkqpt
      

      
!     - arrays -      
      

Daniel Wortmann's avatar
Daniel Wortmann committed
50 51
      
      COMPLEX ,INTENT(OUT)    ::  cprod(hybrid%maxbasm1,mnobd,bandf-bandi+1)
52 53 54 55 56 57 58 59


!     - 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
      INTEGER                 ::  nbasm_ir
60
      INTEGER                 ::  nbasmmt, nbasfcn
61 62 63 64 65
      INTEGER                 ::  ok
      REAL                    ::  rdum,svol,s2,pi
      REAL                    ::  mtthr= 0
      COMPLEX                 ::  cdum,cdum0
      COMPLEX,PARAMETER       ::  img = (0d0,1d0)  
Daniel Wortmann's avatar
Daniel Wortmann committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      LOGICAL                 ::  offdiag      
!      - local arrays -
      INTEGER                 ::  iarr(lapw%nv(jsp))
      INTEGER                 ::  g(3),ghelp(3),lmstart(0:atoms%lmaxd,atoms%ntype)
      INTEGER                 ::  gpthlp(3,dimension%nvd),nvhlp(dimension%jspd)
      INTEGER                 ::  gpt_nk(3,lapw%nv(jsp))
      INTEGER                 ::  g_t(3)
      INTEGER                 ::  gsum(3)
      REAL                    ::  bkpt(3)
      REAL                    ::  bkhlp(3)
      REAL                    ::  kqpt(3),kqpthlp(3)
      COMPLEX                 ::  carr(1:mnobd,bandf-bandi+1)
!      COMPLEX                 :: chelp(maxbasm,mnobd,bandf-bandi+1,nkpt_EIBZ)
      COMPLEX                 ::  cexp
      COMPLEX                 ::  z_help(lapw%nv(jsp))
      COMPLEX                 ::  cmt   (dimension%neigd,hybrid%maxlmindx,atoms%nat)
      COMPLEX                 ::  cmt_nk(dimension%neigd,hybrid%maxlmindx,atoms%nat)
      COMPLEX,ALLOCATABLE     ::  cprod_ir(:,:,:)
Daniel Wortmann's avatar
Daniel Wortmann committed
85
      TYPE(t_mat)             :: z_nk,z_kqpt
86
      TYPE(t_lapw)            :: lapw_nkqpt
87
      CALL timestart("wavefproducts_noinv")
88

89 90 91 92 93 94 95 96 97 98 99 100
      ! preparations

      svol = sqrt(cell%omtil)
      s2   = sqrt(2d0)

      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)

      !
      ! compute k+q point for given q point in EIBZ(k)
      !
Daniel Wortmann's avatar
Daniel Wortmann committed
101
      kqpthlp         = kpts%bkf(:,nk) + kpts%bkf(:,iq)
102 103 104 105 106 107
      ! k+q can lie outside the first BZ, transfer
      ! it back into the 1. BZ
      kqpt        = modulo1(kqpthlp,kpts%nkpt3)
      g_t(:) = nint( kqpt - kqpthlp )
      ! determine number of kqpt
      nkqpt   = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
108 109
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
          nkqpt = ikpt
          EXIT
        END IF
      END DO
      IF ( nkqpt .eq. 0) STOP 'wavefproducts: k-point not found'
      
      

      ! lmstart = lm start index for each l-quantum number and atom type (for cmt-coefficients)
      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
      END DO


      nbasm_ir = maxval(hybrid%ngptm)
      ALLOCATE( cprod_ir(bandf-bandi+1,mnobd,nbasm_ir),stat=ok )
      IF( ok .ne. 0) STOP 'wavefproducts: failure allocation cprod_ir'
      cprod_ir = 0
      
      cprod = 0
      
133 134 135 136 137
      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)
138

Daniel Wortmann's avatar
Daniel Wortmann committed
139
      call read_z(z_nk,nk)
140
      call read_z(z_kqpt,nkqpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
141
      
142
      ! read in cmt coefficients from direct access file cmt
Daniel Wortmann's avatar
Daniel Wortmann committed
143
      call read_cmt(cmt_nk,nk)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      
      ! IR contribution
          
      CALL timestart("wavefproducts_noinv IR")

      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) .le. hybdat%pntgptd ) ) THEN 
            iarr(i) =  hybdat%pntgpt(gsum(1),gsum(2),gsum(3),nkqpt)
          ELSE
            iarr(i) =  0
          END IF

        END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
162
        DO iband1 = 1, hybrid%nobd(nkqpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
163 164 165 166 167
           where (iarr>0)
              z_help(:) = z_kqpt%data_c(iarr(:),iband1)
           elsewhere
              z_help=0.0
           end where
168 169

          DO iband = bandi,bandf
Daniel Wortmann's avatar
Daniel Wortmann committed
170
             cprod_ir(iband,iband1,igpt) = 1/svol * dotprod( z_nk%data_c(:lapw%nv(jsp),iband),z_help )
171 172 173 174 175 176 177 178 179 180 181 182 183
          END DO !iband

        END DO  !iband1

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

          
      !
      ! MT contribution
      ! 
Daniel Wortmann's avatar
Daniel Wortmann committed
184
      call read_cmt(cmt,nkqpt)
185 186 187 188 189 190 191 192
      lm_0     = 0      
      ic       = 0
        
      DO itype = 1,atoms%ntype
        DO ieq = 1,atoms%neq(itype)
          ic =  ic + 1
          ic1 = 0

Daniel Wortmann's avatar
Daniel Wortmann committed
193
          cexp = exp ( -2*img*pi_const* dot_product( kpts%bkf(:,iq),atoms%taual(:,ic) ) )
194 195
          

Daniel Wortmann's avatar
Daniel Wortmann committed
196
          DO l = 0,hybrid%lcutm1(itype)
197
            DO n = 1,hybdat%nindxp1(l,itype) ! loop over basis-function products
198

Daniel Wortmann's avatar
Daniel Wortmann committed
199 200 201 202
              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 !
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

              IF( mod(l1+l2+l,2) .ne. 0 ) cycle

              offdiag = l1.ne.l2.or.n1.ne.n2 ! 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
              DO m = -l,l

                carr = 0d0 

                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).le.l2) THEN
                    lm2  = lm2_0 + n2 + (m2+l2)*hybrid%nindx(l2,itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
222
                    rdum = hybdat%gauntarr(1,l1,l2,l,m1,m) ! precalculated Gaunt coefficient
223 224 225 226 227 228 229 230 231 232 233 234 235 236
                    IF(rdum.ne.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).le.l2.and.offdiag) THEN
                    lm2  = lm2_0 + n2 + (m2+l2)*hybrid%nindx(l2,itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
237
                    rdum = hybdat%gauntarr(2,l1,l2,l,m1,m) ! precalculated Gaunt coefficient
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
                    IF(rdum.ne.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
Daniel Wortmann's avatar
Daniel Wortmann committed
256
                    DO i = 1,hybrid%nindxm1(l,itype)
257
                      j = lm + i
Daniel Wortmann's avatar
Daniel Wortmann committed
258
                      cprod(j,iband1,iband) = cprod(j,iband1,iband) + hybdat%prodm(i,n,l,itype) * cdum
259 260 261 262 263
                    END DO

                  END DO
                END DO
                
Daniel Wortmann's avatar
Daniel Wortmann committed
264
                lm = lm + hybrid%nindxm1(l,itype) ! go to lm start index for next m-quantum number
265 266 267 268

              END DO
 
            END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
269
            lm_0 = lm_0 + hybrid%nindxm1(l,itype) * (2*l+1) ! go to the lm start index of the next l-quantum number
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            IF(lm.ne.lm_0) STOP 'wavefproducts: counting of lm-index incorrect (bug?)'
          END DO
        END DO
      END DO
        
      CALL timestop("wavefproducts_noinv")
   

      ic = nbasm_mt
      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
      END DO
      



      END SUBROUTINE wavefproducts_noinv


      SUBROUTINE wavefproducts_inv(&
295
     &                  bandi,bandf,dimension,input,jsp,atoms,&
Daniel Wortmann's avatar
Daniel Wortmann committed
296
     &                  lapw,kpts,&
Daniel Wortmann's avatar
Daniel Wortmann committed
297
     &                  nk,iq,hybdat,mnobd,hybrid,&
298
     &                  parent,cell,&
299
     &                  nbasm_mt,sym,noco,&
300 301 302 303 304 305
     &                  nkqpt,cprod)

      USE m_util     , ONLY : modulo1
      USE m_wrapper
      USE m_constants 
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
306
      USE m_io_hybrid
307
      IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
308
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
309
      TYPE(t_dimension),INTENT(IN)   :: dimension
310
      TYPE(t_input),INTENT(IN)   :: input
311 312
      TYPE(t_hybrid),INTENT(IN)   :: hybrid
      TYPE(t_sym),INTENT(IN)   :: sym
313
      TYPE(t_noco),INTENT(IN)   :: noco
314 315 316 317 318 319 320
      TYPE(t_cell),INTENT(IN)   :: cell
      TYPE(t_kpts),INTENT(IN)   :: kpts
      TYPE(t_atoms),INTENT(IN)   :: atoms
      TYPE(t_lapw),INTENT(IN)   :: lapw

      ! - scalars -
      INTEGER,INTENT(IN)      ::    bandi,bandf
Daniel Wortmann's avatar
Daniel Wortmann committed
321
      INTEGER,INTENT(IN)      ::    jsp  ,nk,iq 
Daniel Wortmann's avatar
Daniel Wortmann committed
322
      INTEGER,INTENT(IN)      ::    mnobd 
323 324 325 326 327
      INTEGER,INTENT(IN)      ::    nbasm_mt
      INTEGER,INTENT(OUT)     ::    nkqpt

  
      ! - arrays -
Daniel Wortmann's avatar
Daniel Wortmann committed
328
      INTEGER,INTENT(IN)      ::    parent(kpts%nkptf)
329 330


Daniel Wortmann's avatar
Daniel Wortmann committed
331
      REAL ,INTENT(OUT)       ::    cprod(hybrid%maxbasm1,mnobd,bandf-bandi+1)
332 333 334 335 336 337


      ! - local scalars -
      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
338
      INTEGER                 ::    j,ll,m, nbasfcn
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
      INTEGER                 :: nbasm_ir
      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=(0d0,1d0)
      COMPLEX                 ::    fexp
      COMPLEX                 ::    cdum,cconst,cfac
      LOGICAL                 ::    offdiag

      ! - local arrays -
      INTEGER                 ::    iarr(lapw%nv(jsp))
      INTEGER                 ::    gpt_nk(3,lapw%nv(jsp)),ghelp(3)
      INTEGER                 ::    gsum(3)
      INTEGER                 ::    g_t(3)
      INTEGER                 ::    lmstart (0:atoms%lmaxd,atoms%ntype)
Daniel Wortmann's avatar
Daniel Wortmann committed
356
      INTEGER                 ::    lmstart2(0:hybrid%maxlcutm1,atoms%nat)
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
      REAL                    ::    kqpt(3),kqpthlp(3)

      
      REAL,ALLOCATABLE        ::    cprod_ir(:,:,:)

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


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

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

      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)
Daniel Wortmann's avatar
Daniel Wortmann committed
375
      TYPE(t_mat)             :: z_nk,z_kqpt
376
      TYPE(t_lapw)            :: lapw_nkqpt
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

      CALL timestart("wavefproducts_inv")
      CALL timestart("wavefproducts_inv IR")
      svol  = sqrt(cell%omtil)
      sr2   = sqrt(2d0)
      
      nbasm_ir = maxval(hybrid%ngptm)
      ALLOCATE( cprod_ir(bandf-bandi+1,mnobd,nbasm_ir) )
      cprod_ir = 0
      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)

      
      !
      ! compute k+q point for q (iq) in EIBZ(k)
      !
     
Daniel Wortmann's avatar
Daniel Wortmann committed
395
      kqpthlp = kpts%bkf(:,nk) + kpts%bkf(:,iq)
396 397 398 399 400
      ! kqpt can lie outside the first BZ, transfer it back
      kqpt         = modulo1(kqpthlp,kpts%nkpt3)
      g_t(:)  = nint( kqpt - kqpthlp )
      ! determine number of kqpt
      nkqpt = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
401 402
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
403 404 405 406 407 408 409 410 411
          nkqpt = ikpt
          EXIT
        END IF
      END DO
      IF ( nkqpt .eq. 0) STOP 'wavefproducts: k-point not found'
      

      ! read in z at current k-point nk

412 413 414 415 416
      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)
Daniel Wortmann's avatar
Daniel Wortmann committed
417 418 419
     
      call read_z(z_nk,nk)
      call read_z(z_kqpt,nkqpt)
420

421 422 423 424 425 426 427 428 429 430 431 432 433
      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) .le. hybdat%pntgptd ) ) THEN 
            iarr(i) =  hybdat%pntgpt(gsum(1),gsum(2),gsum(3),nkqpt)
          ELSE
            iarr(i) =  0
          END IF

        END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
434
        DO iband1 = 1, hybrid%nobd(nkqpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
435 436 437 438 439
           where (iarr>0)
              z_help(:) = z_kqpt%data_r(iarr(:),iband1)
           elsewhere
              z_help=0.0
           end where
440
          DO iband = bandi,bandf
Daniel Wortmann's avatar
Daniel Wortmann committed
441 442
              cprod_ir(iband,iband1,igpt) = 1/svol * dotprod( z_nk%data_r(:lapw%nv(jsp),iband),z_help )
            
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
          END DO !iband

        END DO  !iband1

      END DO !igpt

      CALL timestop("wavefproducts_inv IR")
   
      ! lmstart = lm start index for each l-quantum number and atom type (for cmt-coefficients)
      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
      END DO

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

      CALL read_cmt(ccmt_nk,nk)
461 462

      !read in cmt coefficients at k+q point
Daniel Wortmann's avatar
Daniel Wortmann committed
463
      call read_cmt(ccmt,nkqpt)
464 465 466 467 468 469 470
      

      iatom = 0
      DO itype = 1,atoms%ntype
        DO ieq = 1,atoms%neq(itype)
          iatom = iatom + 1
          
Daniel Wortmann's avatar
Daniel Wortmann committed
471
          cexp   (iatom) = exp( (-img)*tpi_const * dotprod(kpts%bkf(:,iq)+kpts%bkf(:,nk),atoms%taual(:,iatom)) )
472

Daniel Wortmann's avatar
Daniel Wortmann committed
473
          cexp_nk(iatom) = exp( (-img)*tpi_const * dotprod(kpts%bkf(:,nk),atoms%taual(:,iatom)) )
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
        END DO
      END DO

      rfac = 1./sr2
      cfac = -img/sr2
      iatom = 0
      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) .eq. 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 .lt. 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 .eq. iiatom ) THEN
                  IF( m .lt. 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.gt.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) .eq. 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

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

                  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

        END DO
      END DO 



      cprod  = 0d0

      lm_0   = 0
      lm_00  = 0
      iatom1 = 0
      iiatom = 0

      DO itype = 1,atoms%ntype
Daniel Wortmann's avatar
Daniel Wortmann committed
546
        ioffset = sum( (/ ( (2*ll+1)*hybrid%nindxm1(ll,itype),ll=0,hybrid%lcutm1(itype) ) /)  )
547 548 549 550 551 552 553 554 555 556 557 558
        lm_0 = lm_00
        DO ieq = 1,atoms%neq(itype)
          iatom1 = iatom1 + 1
          IF( sym%invsatnr(iatom1) .eq. 0 ) THEN
            iatom2 = iatom1
          ELSE
            iatom2 = sym%invsatnr(iatom1)
          END IF
          IF( iatom1 .gt. iatom2 ) CYCLE

          IF( iatom1 .ne. iatom2 ) THEN
            ! loop over l of mixed basis
Daniel Wortmann's avatar
Daniel Wortmann committed
559
            DO l = 0,hybrid%lcutm1(itype)
560
              ! loop over basis functions products, which belong to l
561
              DO n = 1,hybdat%nindxp1(l,itype)
562 563

                ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
Daniel Wortmann's avatar
Daniel Wortmann committed
564 565 566 567
                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
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

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

                offdiag = l1.ne.l2.or.p1.ne.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 = 0d0 

                  ! 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).le.l2) THEN
                      lmp2  = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
                      ! precalculated Gaunt coefficient
Daniel Wortmann's avatar
Daniel Wortmann committed
596
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m) 
597 598 599 600 601
                      IF(rdum.ne.0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum * cmt_nk(iband,lmp1,iatom1)
                          rdum2 = rdum * cmt_nk(iband,lmp1,iatom2) 
                          ! loop over occupied bands
Daniel Wortmann's avatar
Daniel Wortmann committed
602
                          DO ibando = 1,mnobd!hybrid%nobd(peibz(ikpt))
603 604 605 606 607 608 609 610 611 612 613 614 615

                            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).le.l2.and.offdiag) THEN
                      lmp2  = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
Daniel Wortmann's avatar
Daniel Wortmann committed
616
                      rdum = hybdat%gauntarr(2,l1,l2,l,m1,m) ! precalculated Gaunt coefficient
617 618 619 620 621
                      IF(rdum.ne.0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum * cmt_nk(iband,lmp2,iatom1)
                          rdum2 = rdum * cmt_nk(iband,lmp2,iatom2)
                          ! loop over occupied bands
Daniel Wortmann's avatar
Daniel Wortmann committed
622
                          DO ibando = 1,mnobd!hybrid%nobd(peibz(ikpt)
623 624 625 626 627 628 629 630 631 632 633 634 635
                            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

Daniel Wortmann's avatar
Daniel Wortmann committed
636
                  ishift = -2*m*hybrid%nindxm1(l,itype)
637 638 639 640 641

                  ! go to lm mixed basis startindx for l and m
                  lm1   =  lm + ( iatom1-1 - iiatom ) * ioffset
                  lm2   =  lm + ( iatom2-1 - iiatom ) * ioffset + ishift
                  
Daniel Wortmann's avatar
Daniel Wortmann committed
642
                  rdum  = tpi_const*dotprod(kpts%bkf(:,iq),atoms%taual(:,iatom1))
643 644 645 646 647 648 649 650 651 652 653 654
                  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)
!                       sin1  = rdum1*rfac1
!                       cos1  = rdum1*rfac2
!                       sin2  = rdum2*rfac1
!                       cos2  = rdum2*rfac2
                      add1 = rdum1*rfac2 + rdum2*rfac1
                      add2 = rdum2*rfac2 - rdum1*rfac1
Daniel Wortmann's avatar
Daniel Wortmann committed
655
                      DO i = 1,hybrid%nindxm1(l,itype)
656
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
657
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*add1!( cos1 + sin2 )
658
                        j = lm2 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
659
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*add2!( cos2 - sin1 )
660 661 662 663 664 665

                      END DO  !i -> loop over mixed basis functions
                    END DO  !ibando
                  END DO  !iband
                  
                  ! go to lm start index for next m-quantum number
Daniel Wortmann's avatar
Daniel Wortmann committed
666
                  lm = lm + hybrid%nindxm1(l,itype) 
667 668 669 670

                END DO  !m

              END DO !n
Daniel Wortmann's avatar
Daniel Wortmann committed
671
              lm_0 = lm_0 + hybrid%nindxm1(l,itype) * (2*l+1) ! go to the lm start index of the next l-quantum number
672 673 674 675 676 677 678
              IF(lm.ne.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
Daniel Wortmann's avatar
Daniel Wortmann committed
679
            DO l = 0,hybrid%lcutm1(itype)
680 681
              monepl = -monepl
              ! loop over basis functions products, which belong to l
682
              DO n = 1,hybdat%nindxp1(l,itype)
683 684

                ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
Daniel Wortmann's avatar
Daniel Wortmann committed
685 686 687 688
                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
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

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

                offdiag = l1.ne.l2.or.p1.ne.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 = 0d0
                  IF( abs(m) .le. l2) THEN
                    lmp1 = lp1 + l1*hybrid%nindx(l1,itype)
                    IF( mod(l1,2) .eq. 0 ) THEN
                      lmp2 = lp2 + ( m+l2)*hybrid%nindx(l2,itype)
                    ELSE
                      lmp2 = lp2 + (-m+l2)*hybrid%nindx(l2,itype)
                    END IF

Daniel Wortmann's avatar
Daniel Wortmann committed
729
                    rdum = hybdat%gauntarr(1,l1,l2,l,0,m)
730 731 732 733 734 735 736 737 738 739 740
                    IF( rdum .ne. 0 ) THEN
                      DO iband = bandi,bandf
                        rdum1 = rdum*cmt_nk(iband,lmp1,iatom1)
                        IF( mod(l1,2) .ne. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
741
                      rdum = hybdat%gauntarr(1,l2,l1,l,-m,m)
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
                      IF(rdum .ne. 0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum*cmt_nk(iband,lmp2,iatom1)
                          IF( mod(l1,2) .eq. 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) .le. l1) THEN
                    IF( mod(l2,2) .eq. 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)

Daniel Wortmann's avatar
Daniel Wortmann committed
763
                    rdum = hybdat%gauntarr(1,l1,l2,l,-m,m)
764 765 766 767 768 769 770 771 772 773 774
                    IF(rdum .ne. 0) THEN
                      DO iband = bandi,bandf
                        rdum1 = rdum*cmt_nk(iband,lmp3,iatom1)
                        IF( mod(l2,2) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
775
                      rdum = hybdat%gauntarr(1,l2,l1,l,0,m)
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
                      IF(rdum .ne. 0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum*cmt_nk(iband,lmp2,iatom1)
                          IF( mod(l2,2) .ne. 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 .eq. 0 ) THEN
                      lmp1 = lmp1 + hybrid%nindx(l1,itype) 
                      CYCLE
                    END IF
                    ! (-1)**(l1+m1)
                    monepl1m1 = monepl1*monepm1
                    m2 = m1 + m
                    IF( abs(m2) .le. l2 .and. m2 .ne. 0) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
804
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
                      IF( rdum .ne. 0 ) THEN
                        IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
823
                        rdum = hybdat%gauntarr(1,l2,l1,l,m2,-m)
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
                        IF( rdum .ne. 0 ) THEN
                          lmp2 = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
                          IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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) .le. l2 .and. m2 .ne. 0 ) THEN

Daniel Wortmann's avatar
Daniel Wortmann committed
848
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,-m)
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
                      IF( rdum .ne. 0 ) THEN

                        IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
870
                        rdum = hybdat%gauntarr(1,l2,l1,l,m2,m)
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
                        IF( rdum .ne. 0 ) THEN
                          lmp2 = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
                          IF( sign(1,m1) + sign(1,m2) .ne. 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) .eq. 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)
Daniel Wortmann's avatar
Daniel Wortmann committed
901
                      DO i = 1,hybrid%nindxm1(l,itype)
902
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
903
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
904 905 906 907 908
                      END DO  !i -> loop over mixed basis functions
                    END DO  !ibando
                  END DO  !iband

                  ! go to lm start index for next m-quantum number
Daniel Wortmann's avatar
Daniel Wortmann committed
909
                  lm = lm + hybrid%nindxm1(l,itype) 
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

                END DO  ! m=-l,-1

                !
                !case m=0
                !

                m = 0
                rarr1 = 0d0
                lmp1  = lp1

                monepm1 = - monepl1

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

                    IF( mod(l,2) .eq. 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) .eq. 0 ) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
945
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
946
                    ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
947
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)*fac1
948 949 950 951 952 953 954 955 956 957 958 959 960
                    END IF
                    IF(rdum.ne.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) .eq. 0 ) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
961
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)
962
                      ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
963
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)*fac2
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
                      END IF
                      IF(rdum.ne.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)
Daniel Wortmann's avatar
Daniel Wortmann committed
986
                    DO i = 1,hybrid%nindxm1(l,itype)
987
                      j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
988
                      cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
989 990 991 992 993
                    END DO  !i -> loop over mixed basis functions
                  END DO  !ibando
                END DO  !iband

                ! go to lm start index for next m-quantum number
Daniel Wortmann's avatar
Daniel Wortmann committed
994
                lm = lm + hybrid%nindxm1(l,itype) 
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

                !
                ! case: m>0
                !

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

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

Daniel Wortmann's avatar
Daniel Wortmann committed
1016
                    rdum = hybdat%gauntarr(1,l1,l2,l,0,m)
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                    IF( rdum .ne. 0 ) THEN
                      DO iband = bandi,bandf
                        rdum1 = rdum*cmt_nk(iband,lmp1,iatom1)
                        IF( mod(l1,2) .ne. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
1028
                      rdum = hybdat%gauntarr(1,l2,l1,l,-m,m)
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
                      IF(rdum .ne. 0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum*cmt_nk(iband,lmp2,iatom1)
                          IF( mod(l1,2) .eq. 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) .le. l1) THEN
                    IF( mod(l2,2) .eq. 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)

Daniel Wortmann's avatar
Daniel Wortmann committed
1050
                    rdum = hybdat%gauntarr(1,l1,l2,l,-m,m)
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
                    IF(rdum .ne. 0) THEN
                      DO iband = bandi,bandf
                        rdum1 = rdum*cmt_nk(iband,lmp3,iatom1)
                        IF( mod(l2,2) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
1062
                      rdum = hybdat%gauntarr(1,l2,l1,l,0,m)
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
                      IF(rdum .ne. 0) THEN
                        DO iband = bandi,bandf
                          rdum1 = rdum*cmt_nk(iband,lmp2,iatom1)
                          IF( mod(l2,2) .ne. 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 .eq. 0 ) THEN
                      lmp1 = lmp1 + hybrid%nindx(l1,itype)
                      CYCLE
                    END IF
                    m2 = m1 + m
                    ! (-1)**(l1+m1)
                    monepl1m1 = monepl1*monepm1
                    IF( abs(m2) .le. l2 .and. m2 .ne. 0) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
1094
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
                      IF( rdum .ne. 0 ) THEN

                        IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
1116
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,-m)
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
                        IF( rdum .ne. 0 ) THEN
                          lmp2 = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
                          IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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) .le. l2 .and. m2 .ne. 0 ) THEN

Daniel Wortmann's avatar
Daniel Wortmann committed
1143
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,-m)
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
                      IF( rdum .ne. 0 ) THEN

                        IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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
Daniel Wortmann's avatar
Daniel Wortmann committed
1163
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)
1164 1165 1166 1167 1168 1169 1170 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
                        IF( rdum .ne. 0 ) THEN
                          lmp2 = lp2 + (m2+l2)*hybrid%nindx(l2,itype)
                          IF( sign(1,m2) + sign(1,m1) .ne. 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) .eq. 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)
Daniel Wortmann's avatar
Daniel Wortmann committed
1199
                      DO i = 1,hybrid%nindxm1(l,itype)
1200
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
1201
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
1202 1203 1204 1205 1206
                      END DO  !i -> loop over mixed basis functions
                    END DO  !ibando
                  END DO  !iband

                  ! go to lm start index for next m-quantum number
Daniel Wortmann's avatar
Daniel Wortmann committed
1207
                  lm = lm + hybrid%nindxm1(l,itype) 
1208 1209 1210 1211

                END DO  ! m=1,l

              END DO !n
Daniel Wortmann's avatar
Daniel Wortmann committed
1212
              lm_0 = lm_0 + hybrid%nindxm1(l,itype) * (2*l+1) ! go to the m start index of the next l-quantum number
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
              IF(lm.ne.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


      ic = nbasm_mt
      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
      END DO

  
      CALL timestop("wavefproducts_inv")
  
      END SUBROUTINE wavefproducts_inv

      SUBROUTINE wavefproducts_inv5(&
     &                    bandi,bandf,bandoi,bandof,&
     &                    dimension,input,jsp,atoms,&
Daniel Wortmann's avatar
Daniel Wortmann committed
1243
     &                    lapw,kpts,&
Daniel Wortmann's avatar
Daniel Wortmann committed
1244
     &                    nk,iq,hybdat,mnobd,hybrid,&
1245
     &                    parent,cell,&
Daniel Wortmann's avatar
Daniel Wortmann committed
1246
     &                    nbasm_mt,sym,&
1247 1248 1249 1250 1251 1252 1253 1254
     &                    noco,&
     &                    nkqpt,cprod)

      USE m_util     , ONLY : modulo1
      USE m_olap     , ONLY : gptnorm
      USE m_wrapper
      USE m_constants
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
1255
      USE m_io_hybrid
1256
      IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
1257
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
      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

      ! - scalars -
      INTEGER,INTENT(IN)      :: bandi,bandf,bandoi,bandof
Daniel Wortmann's avatar
Daniel Wortmann committed
1270
      INTEGER,INTENT(IN)      :: jsp    ,nk,iq 
Daniel Wortmann's avatar
Daniel Wortmann committed
1271
      INTEGER,INTENT(IN)      :: mnobd 
1272 1273 1274 1275 1276 1277
      INTEGER,INTENT(IN)      :: nbasm_mt
      INTEGER,INTENT(OUT)     :: nkqpt

      

      ! - arrays -
Daniel Wortmann's avatar
Daniel Wortmann committed
1278
      INTEGER,INTENT(IN)      ::    parent(kpts%nkptf)
1279

Daniel Wortmann's avatar
Daniel Wortmann committed
1280 1281
    
      REAL,INTENT(OUT)        ::    cprod(hybrid%maxbasm1,bandoi:bandof, bandf-bandi+1)
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292


      ! - local scalars -
      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
1293
      INTEGER                 ::    n1,n2, nbasfcn
1294 1295 1296 1297 1298 1299 1300 1301 1302
      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                 ::    fexp
      COMPLEX                 ::    cdum,cconst,cfac
      COMPLEX,PARAMETER       ::    img=(0d0,1d0)
      LOGICAL                 ::    offdiag
Daniel Wortmann's avatar
Daniel Wortmann committed
1303 1304
      TYPE(t_lapw)            ::    lapw_nkqpt
      
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
      ! - local arrays -
      INTEGER                 ::    g(3),g_t(3)
      INTEGER                 ::    lmstart (0:atoms%lmaxd,atoms%ntype)
      INTEGER, ALLOCATABLE    ::    gpt0(:,:)
      INTEGER, ALLOCATABLE    ::    pointer(:,:,:)

      REAL                    ::    kqpt(3),kqpthlp(3)
      REAL                    ::    bkpt(3)
      REAL                    ::    cmt_nk(dimension%neigd,hybrid%maxlmindx,atoms%nat)
      REAL                    ::    cmt   (dimension%neigd,hybrid%maxlmindx,atoms%nat)
      REAL                    ::    rarr1(bandoi:bandof)
      REAL                    ::    rarr2(bandoi:bandof,bandf-bandi+1)
      REAL                    ::    rarr3(2,bandoi:bandof,bandf-bandi+1)
      REAL, ALLOCATABLE       ::    stepfunc(:,:,:)
      REAL, ALLOCATABLE       ::    z0(:,:)

      COMPLEX                 ::    cexp(atoms%nat),cexp_nk(atoms%nat)
      COMPLEX,ALLOCATABLE     ::    ccmt_nk(:,:,:)
      COMPLEX,ALLOCATABLE     ::    ccmt   (:,:,:)
Daniel Wortmann's avatar
Daniel Wortmann committed
1324
      TYPE(t_mat)             :: z_nk,z_kqpt
1325

Daniel Wortmann's avatar
Daniel Wortmann committed
1326
      
1327 1328
      CALL timestart("wavefproducts_inv5")
      CALL timestart("wavefproducts_inv5 IR")
Daniel Wortmann's avatar
Daniel Wortmann committed
1329
      
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
      cprod = 0
      svol  = sqrt(cell%omtil)
      sr2   = sqrt(2d0)
      
      
      
      nbasm_ir = maxval(hybrid%ngptm)
      
           
      !
      ! compute k+q point for q (iq) in EIBZ(k)
      !
     
Daniel Wortmann's avatar
Daniel Wortmann committed
1343
      kqpthlp = kpts%bkf(:,nk) + kpts%bkf(:,iq)
1344 1345 1346 1347 1348
      ! kqpt can lie outside the first BZ, transfer it back
      kqpt    = modulo1(kqpthlp,kpts%nkpt3)
      g_t(:)  = nint( kqpt - kqpthlp )
      ! determine number of kqpt
      nkqpt = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
1349 1350
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
1351 1352 1353 1354 1355 1356 1357 1358 1359
          nkqpt = ikpt
          EXIT
        END IF
      END DO
      IF ( nkqpt .eq. 0) STOP 'wavefproducts_inv5: k-point not found'
      
      !
      ! compute G's fulfilling |bk(:,nkqpt) + G| <= rkmax
      !
1360
      CALL lapw_nkqpt%init(input,noco,kpts,atoms,sym,nkqpt,cell,sym%zrfs)
1361

1362 1363 1364 1365
      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)
1366

Daniel Wortmann's avatar
Daniel Wortmann committed
1367 1368 1369
      ! read in z at k-point nk and nkqpt
      CALL read_z(z_nk,nk)
      call read_z(z_kqpt,nkqpt)
1370 1371

      g(1) = maxval(abs(lapw%k1      (:lapw%nv(jsp)      ,jsp))) &
Daniel Wortmann's avatar
Daniel Wortmann committed
1372
     &     + maxval(abs(lapw_nkqpt%k1(:lapw_nkqpt%nv(jsp),jsp)))&
1373 1374
     &     + maxval(abs(hybrid%gptm(1,hybrid%pgptm(:hybrid%ngptm(iq),iq) ))) + 1
      g(2) = maxval(abs(lapw%k2      (:lapw%nv(jsp)      ,jsp)))&
Daniel Wortmann's avatar
Daniel Wortmann committed
1375
     &     + maxval(abs(lapw_nkqpt%k2(:lapw_nkqpt%nv(jsp),jsp)))&
1376 1377
     &     + maxval(abs(hybrid%gptm(2,hybrid%pgptm(:hybrid%ngptm(iq),iq) ))) + 1
      g(3) = maxval(abs(lapw%k3      (:lapw%nv(jsp)      ,jsp)))&
Daniel Wortmann's avatar
Daniel Wortmann committed
1378
     &     + maxval(abs(lapw_nkqpt%k3(:lapw_nkqpt%nv(jsp),jsp)))&
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
     &     + maxval(abs(hybrid%gptm(3,hybrid%pgptm(:hybrid%ngptm(iq),iq) ))) + 1


      ALLOCATE ( stepfunc(-g(1):g(1),-g(2):g(2),-g(3):g(3)),stat=ok)
      IF( ok .ne. 0 ) STOP 'wavefproducts_inv5: error allocation stepfunc'
      ALLOCATE ( pointer(-g(1):g(1),-g(2):g(2),-g(3):g(3)),stat=ok )
      IF( ok .ne. 0 ) STOP 'wavefproducts_inv5: error allocation pointer'
      ALLOCATE ( gpt0(3,size(pointer)),stat=ok )
      IF( ok .ne. 0 ) STOP 'wavefproducts_inv5: error allocation gpt0'
                  
      DO i = -g(1),g(1)
        DO j = -g(2),g(2)
          DO k = -g(3),g(3)
            stepfunc(i,j,k) = stepfunction(cell,atoms,(/i,j,k/))
          END DO
        END DO
      END DO

      

      !
      ! convolute phi(n,k) with the step function and store in cpw0
      !
      
      !(1) prepare list of G vectors
      pointer = 0
      ic      = 0
      DO ig1