wavefproducts.F90 103 KB
Newer Older
1 2 3 4 5 6 7 8
      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,jsp,&                  !cprod,&
Daniel Wortmann's avatar
Daniel Wortmann committed
9 10 11
     &                 cell,atoms,hybrid,hybdat,&
     &                 kpts,mnobd,&
     &                 lapw,sym,nbasm_mt,nkqpt,cprod)
12 13 14 15 16 17


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

Daniel Wortmann's avatar
Daniel Wortmann committed
21
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
22 23 24 25 26 27 28 29 30 31
      TYPE(t_dimension),INTENT(IN)   :: dimension
      TYPE(t_hybrid),INTENT(IN)   :: hybrid
      TYPE(t_sym),INTENT(IN)   :: sym
      TYPE(t_cell),INTENT(IN)   :: cell
      TYPE(t_kpts),INTENT(IN)   :: kpts
      TYPE(t_atoms),INTENT(IN)   :: atoms
      TYPE(t_lapw),INTENT(IN)   :: lapw

!     - scalars -
      INTEGER,INTENT(IN)      ::  nk,iq ,jsp
Daniel Wortmann's avatar
Daniel Wortmann committed
32 33
      INTEGER,INTENT(IN)      :: mnobd 
      INTEGER,INTENT(IN)      :: nbasm_mt
34 35 36 37 38 39 40 41
      INTEGER,INTENT(IN)      ::  bandi,bandf
      INTEGER,INTENT(OUT)     ::  nkqpt
      

      
!     - arrays -      
      

Daniel Wortmann's avatar
Daniel Wortmann committed
42 43
      
      COMPLEX ,INTENT(OUT)    ::  cprod(hybrid%maxbasm1,mnobd,bandf-bandi+1)
44 45 46 47 48 49 50 51


!     - 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
52
      INTEGER                 ::  nbasmmt
53 54 55 56 57
      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
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      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
77
      TYPE(t_mat)             :: z_nk,z_kqpt
78
      CALL timestart("wavefproducts_noinv")
Daniel Wortmann's avatar
Daniel Wortmann committed
79 80
      call z_nk%alloc(.false.,dimension%nbasfcn,dimension%neigd)
      call z_kqpt%alloc(.false.,dimension%nbasfcn,dimension%neigd)
81 82 83 84 85 86 87 88 89 90 91 92 93 94
      !
      ! 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
95
      kqpthlp         = kpts%bkf(:,nk) + kpts%bkf(:,iq)
96 97 98 99 100 101
      ! 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
102 103
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
          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
      
      ! read in z at current k-point nk    

Daniel Wortmann's avatar
Daniel Wortmann committed
129 130
      call read_z(z_nk,nk)
      
131
      ! read in cmt coefficients from direct access file cmt
Daniel Wortmann's avatar
Daniel Wortmann committed
132
      call read_cmt(cmt_nk,nk)
133 134 135 136 137 138 139 140 141
      
        
      !
      ! IR contribution
      !
          
      CALL timestart("wavefproducts_noinv IR")
      
      ! read in z_kqpt
Daniel Wortmann's avatar
Daniel Wortmann committed
142 143
     
      call read_z(z_kqpt,nkqpt)
144 145 146 147 148 149 150 151 152 153 154 155 156 157

      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
158
        DO iband1 = 1, hybrid%nobd(nkqpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
159 160 161 162 163
           where (iarr>0)
              z_help(:) = z_kqpt%data_c(iarr(:),iband1)
           elsewhere
              z_help=0.0
           end where
164 165

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

        END DO  !iband1

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

          
      !
      ! MT contribution
      ! 
Daniel Wortmann's avatar
Daniel Wortmann committed
180
      call read_cmt(cmt,nkqpt)
181 182 183 184 185 186 187 188
      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
189
          cexp = exp ( -2*img*pi_const* dot_product( kpts%bkf(:,iq),atoms%taual(:,ic) ) )
190 191
          

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

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

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

                  END DO
                END DO
                
Daniel Wortmann's avatar
Daniel Wortmann committed
260
                lm = lm + hybrid%nindxm1(l,itype) ! go to lm start index for next m-quantum number
261 262 263 264

              END DO
 
            END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
265
            lm_0 = lm_0 + hybrid%nindxm1(l,itype) * (2*l+1) ! go to the lm start index of the next l-quantum number
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            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(&
     &                  bandi,bandf,dimension,jsp,atoms,&
Daniel Wortmann's avatar
Daniel Wortmann committed
292
     &                  lapw,kpts,&
Daniel Wortmann's avatar
Daniel Wortmann committed
293
     &                  nk,iq,hybdat,mnobd,hybrid,&
294
     &                  parent,cell,&
Daniel Wortmann's avatar
Daniel Wortmann committed
295
     &                  nbasm_mt,sym,&
296 297 298 299 300 301
     &                  nkqpt,cprod)

      USE m_util     , ONLY : modulo1
      USE m_wrapper
      USE m_constants 
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
302
      USE m_io_hybrid
303
      IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
304
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
305 306 307 308 309 310 311 312 313 314
      TYPE(t_dimension),INTENT(IN)   :: dimension
      TYPE(t_hybrid),INTENT(IN)   :: hybrid
      TYPE(t_sym),INTENT(IN)   :: sym
      TYPE(t_cell),INTENT(IN)   :: cell
      TYPE(t_kpts),INTENT(IN)   :: kpts
      TYPE(t_atoms),INTENT(IN)   :: atoms
      TYPE(t_lapw),INTENT(IN)   :: lapw

      ! - scalars -
      INTEGER,INTENT(IN)      ::    bandi,bandf
Daniel Wortmann's avatar
Daniel Wortmann committed
315
      INTEGER,INTENT(IN)      ::    jsp  ,nk,iq 
Daniel Wortmann's avatar
Daniel Wortmann committed
316
      INTEGER,INTENT(IN)      ::    mnobd 
317 318 319 320 321
      INTEGER,INTENT(IN)      ::    nbasm_mt
      INTEGER,INTENT(OUT)     ::    nkqpt

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


Daniel Wortmann's avatar
Daniel Wortmann committed
325
      REAL ,INTENT(OUT)       ::    cprod(hybrid%maxbasm1,mnobd,bandf-bandi+1)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349


      ! - 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
      INTEGER                 ::    j,ll,m
      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
350
      INTEGER                 ::    lmstart2(0:hybrid%maxlcutm1,atoms%nat)
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
      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
369
      TYPE(t_mat)             :: z_nk,z_kqpt
370 371 372

      CALL timestart("wavefproducts_inv")
      CALL timestart("wavefproducts_inv IR")
Daniel Wortmann's avatar
Daniel Wortmann committed
373 374
      call z_nk%alloc(.true.,dimension%nbasfcn,dimension%neigd)
      call z_kqpt%alloc(.true.,dimension%nbasfcn,dimension%neigd)
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
      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
390
      kqpthlp = kpts%bkf(:,nk) + kpts%bkf(:,iq)
391 392 393 394 395
      ! 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
396 397
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
398 399 400 401 402 403 404 405 406
          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

Daniel Wortmann's avatar
Daniel Wortmann committed
407 408 409 410
     
      call read_z(z_nk,nk)
      
     
411 412 413


      ! read in z_kqpt
Daniel Wortmann's avatar
Daniel Wortmann committed
414
      call read_z(z_kqpt,nkqpt)
415 416 417 418 419 420 421 422 423 424 425 426 427
      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
428
        DO iband1 = 1, hybrid%nobd(nkqpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
429 430 431 432 433
           where (iarr>0)
              z_help(:) = z_kqpt%data_r(iarr(:),iband1)
           elsewhere
              z_help=0.0
           end where
434
          DO iband = bandi,bandf
Daniel Wortmann's avatar
Daniel Wortmann committed
435 436
              cprod_ir(iband,iband1,igpt) = 1/svol * dotprod( z_nk%data_r(:lapw%nv(jsp),iband),z_help )
            
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
          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
453 454

      CALL read_cmt(ccmt_nk,nk)
455 456

      !read in cmt coefficients at k+q point
Daniel Wortmann's avatar
Daniel Wortmann committed
457
      call read_cmt(ccmt,nkqpt)
458 459 460 461 462 463 464
      

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

Daniel Wortmann's avatar
Daniel Wortmann committed
467
          cexp_nk(iatom) = exp( (-img)*tpi_const * dotprod(kpts%bkf(:,nk),atoms%taual(:,iatom)) )
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        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
540
        ioffset = sum( (/ ( (2*ll+1)*hybrid%nindxm1(ll,itype),ll=0,hybrid%lcutm1(itype) ) /)  )
541 542 543 544 545 546 547 548 549 550 551 552
        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
553
            DO l = 0,hybrid%lcutm1(itype)
554
              ! loop over basis functions products, which belong to l
555
              DO n = 1,hybdat%nindxp1(l,itype)
556 557

                ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
Daniel Wortmann's avatar
Daniel Wortmann committed
558 559 560 561
                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
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

                ! 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
590
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m) 
591 592 593 594 595
                      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
596
                          DO ibando = 1,mnobd!hybrid%nobd(peibz(ikpt))
597 598 599 600 601 602 603 604 605 606 607 608 609

                            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
610
                      rdum = hybdat%gauntarr(2,l1,l2,l,m1,m) ! precalculated Gaunt coefficient
611 612 613 614 615
                      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
616
                          DO ibando = 1,mnobd!hybrid%nobd(peibz(ikpt)
617 618 619 620 621 622 623 624 625 626 627 628 629
                            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
630
                  ishift = -2*m*hybrid%nindxm1(l,itype)
631 632 633 634 635

                  ! 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
636
                  rdum  = tpi_const*dotprod(kpts%bkf(:,iq),atoms%taual(:,iatom1))
637 638 639 640 641 642 643 644 645 646 647 648
                  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
649
                      DO i = 1,hybrid%nindxm1(l,itype)
650
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
651
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*add1!( cos1 + sin2 )
652
                        j = lm2 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
653
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*add2!( cos2 - sin1 )
654 655 656 657 658 659

                      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
660
                  lm = lm + hybrid%nindxm1(l,itype) 
661 662 663 664

                END DO  !m

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

                ! determine l1,p1 and l2,p2 for the basis functions, which can generate l
Daniel Wortmann's avatar
Daniel Wortmann committed
679 680 681 682
                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
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

                ! 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
723
                    rdum = hybdat%gauntarr(1,l1,l2,l,0,m)
724 725 726 727 728 729 730 731 732 733 734
                    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
735
                      rdum = hybdat%gauntarr(1,l2,l1,l,-m,m)
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
                      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
757
                    rdum = hybdat%gauntarr(1,l1,l2,l,-m,m)
758 759 760 761 762 763 764 765 766 767 768
                    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
769
                      rdum = hybdat%gauntarr(1,l2,l1,l,0,m)
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
                      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
798
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
                      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
817
                        rdum = hybdat%gauntarr(1,l2,l1,l,m2,-m)
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
                        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
842
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,-m)
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
                      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
864
                        rdum = hybdat%gauntarr(1,l2,l1,l,m2,m)
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
                        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
895
                      DO i = 1,hybrid%nindxm1(l,itype)
896
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
897
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
898 899 900 901 902
                      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
903
                  lm = lm + hybrid%nindxm1(l,itype) 
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

                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
939
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
940
                    ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
941
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)*fac1
942 943 944 945 946 947 948 949 950 951 952 953 954
                    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
955
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)
956
                      ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
957
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)*fac2
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
                      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
980
                    DO i = 1,hybrid%nindxm1(l,itype)
981
                      j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
982
                      cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
983 984 985 986 987
                    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
988
                lm = lm + hybrid%nindxm1(l,itype) 
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

                !
                ! 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
1010
                    rdum = hybdat%gauntarr(1,l1,l2,l,0,m)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                    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
1022
                      rdum = hybdat%gauntarr(1,l2,l1,l,-m,m)
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
                      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
1044
                    rdum = hybdat%gauntarr(1,l1,l2,l,-m,m)
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
                    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
1056
                      rdum = hybdat%gauntarr(1,l2,l1,l,0,m)
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
                      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
1088
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,m)
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
                      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
1110
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,-m)
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
                        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
1137
                      rdum = hybdat%gauntarr(1,l1,l2,l,m1,-m)
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
                      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
1157
                        rdum = hybdat%gauntarr(2,l1,l2,l,m1,m)
1158 1159 1160 1161 1162 1163 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
                        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
1193
                      DO i = 1,hybrid%nindxm1(l,itype)
1194
                        j = lm1 + i
Daniel Wortmann's avatar
Daniel Wortmann committed
1195
                        cprod(j,ibando,iband) = cprod(j,ibando,iband) + hybdat%prodm(i,n,l,itype)*rdum
1196 1197 1198 1199 1200
                      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
1201
                  lm = lm + hybrid%nindxm1(l,itype) 
1202 1203 1204 1205

                END DO  ! m=1,l

              END DO !n
Daniel Wortmann's avatar
Daniel Wortmann committed
1206
              lm_0 = lm_0 + hybrid%nindxm1(l,itype) * (2*l+1) ! go to the m start index of the next l-quantum number
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
              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
1237
     &                    lapw,kpts,&
Daniel Wortmann's avatar
Daniel Wortmann committed
1238
     &                    nk,iq,hybdat,mnobd,hybrid,&
1239
     &                    parent,cell,&
Daniel Wortmann's avatar
Daniel Wortmann committed
1240
     &                    nbasm_mt,sym,&
1241 1242 1243 1244 1245 1246 1247 1248
     &                    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
1249
      USE m_io_hybrid
1250
      IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
1251
      TYPE(t_hybdat),INTENT(IN)   :: hybdat
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
      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
1264
      INTEGER,INTENT(IN)      :: jsp    ,nk,iq 
Daniel Wortmann's avatar
Daniel Wortmann committed
1265
      INTEGER,INTENT(IN)      :: mnobd 
1266 1267 1268 1269 1270 1271
      INTEGER,INTENT(IN)      :: nbasm_mt
      INTEGER,INTENT(OUT)     :: nkqpt

      

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

Daniel Wortmann's avatar
Daniel Wortmann committed
1274 1275
    
      REAL,INTENT(OUT)        ::    cprod(hybrid%maxbasm1,bandoi:bandof, bandf-bandi+1)
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286


      ! - 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
1287
      INTEGER                 ::    n1,n2
1288 1289 1290 1291 1292 1293 1294 1295 1296
      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
1297 1298
      TYPE(t_lapw)            ::    lapw_nkqpt
      
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
      ! - 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
1318
      TYPE(t_mat)             :: z_nk,z_kqpt
1319

Daniel Wortmann's avatar
Daniel Wortmann committed
1320
      
1321 1322
      CALL timestart("wavefproducts_inv5")
      CALL timestart("wavefproducts_inv5 IR")
Daniel Wortmann's avatar
Daniel Wortmann committed
1323 1324 1325 1326
      call z_nk%alloc(.true.,dimension%nbasfcn,dimension%neigd)
      call z_kqpt%alloc(.true.,dimension%nbasfcn,dimension%neigd)
      
      
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
      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
1340
      kqpthlp = kpts%bkf(:,nk) + kpts%bkf(:,iq)
1341 1342 1343 1344 1345
      ! 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
1346 1347
      DO ikpt = 1,kpts%nkptf
        IF ( maxval( abs( kqpt - kpts%bkf(:,ikpt) ) ) .le. 1E-06 ) THEN
1348 1349 1350 1351 1352 1353 1354 1355 1356
          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
      !
1357
      CALL lapw_nkqpt%init(input,noco,kpts,atoms,sym,nkqpt,cell,sym%zrfs)
1358 1359


Daniel Wortmann's avatar
Daniel Wortmann committed
1360 1361 1362
      ! read in z at k-point nk and nkqpt
      CALL read_z(z_nk,nk)
      call read_z(z_kqpt,nkqpt)
1363 1364

      g(1) = maxval(abs(lapw%k1      (:lapw%nv(jsp)      ,jsp))) &
Daniel Wortmann's avatar
Daniel Wortmann committed
1365
     &     + maxval(abs(lapw_nkqpt%k1(:lapw_nkqpt%nv(jsp),jsp)))&
1366 1367
     &     + 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
1368
     &     + maxval(abs(lapw_nkqpt%k2(:lapw_nkqpt%nv(jsp),jsp)))&
1369 1370
     &     + 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
1371
     &     + maxval(abs(lapw_nkqpt%k3(:lapw_nkqpt%nv(jsp),jsp)))&
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
     &     + 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 = 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)) .eq. 0 ) THEN
            ic                      = ic + 1
            gpt0(:,ic)              = g
            pointer(g(1),g(2),g(3)) = ic
          END IF
        END DO
      END DO
      ngpt0 = ic

      !(2) calculate convolution
      ALLOCATE ( z0(bandoi:bandof,ngpt0), stat=ok )
      IF( ok .ne. 0 ) STOP 'wavefproducts_inv5: error allocation z0'
      z0 = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
1418 1419
      DO ig2 = 1,lapw_nkqpt%nv(jsp)
        rarr1 = z_kqpt%data_r(ig2,bandoi:bandof)
1420
        DO ig = 1,ngpt0
Daniel Wortmann's avatar
Daniel Wortmann committed
1421 1422 1423
          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)
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
          rdum    = stepfunc(g(1),g(2),g(3)) / svol
          DO n2 = bandoi,bandof
            z0(n2,ig) = z0(n2,ig) + rarr1(n2)*rdum
          END DO
        END DO
      END DO
      
      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 .eq. 0 ) THEN
           STOP 'wavefproducts_inv5: pointer undefined'
          END IF
        

          DO n1 = 1,bandf-bandi+1
Daniel Wortmann's avatar
Daniel Wortmann committed
1451
            rdum1 = z_nk%data_r(ig1,n1)
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
            DO