hlomat.F90 22.4 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_hlomat
!***********************************************************************
! updates the hamiltonian  matrix with the contributions from the local
! orbitals.
! p.kurz sept. 1996
!***********************************************************************
CONTAINS
14
  SUBROUTINE hlomat(input,atoms,usp,tsp,&
15
       n_size,n_rank, ntyp,na,lapw,ar,br,ai,bi, el,alo,blo,clo,ud,&
16
       noco,iintsp,jintsp,chi11,chi22,chi21, iilo,locol,nkvecprevat,tlmplm,l_real,aa_r,aa_c)
17 18 19 20
#include"cpp_double.h"
    !
    USE m_types
    IMPLICIT NONE
21
    TYPE(t_input),INTENT(IN)  :: input
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    TYPE(t_noco),INTENT(IN)   :: noco
    TYPE(t_atoms),INTENT(IN)  :: atoms
    TYPE(t_lapw),INTENT(IN)   :: lapw
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: n_size,n_rank
    INTEGER, INTENT (IN) :: na,ntyp  
    INTEGER, INTENT (IN) :: usp,tsp !spin for usdus and tlmplm
    INTEGER, INTENT (IN) :: iintsp,jintsp
    COMPLEX, INTENT (IN) :: chi11,chi22,chi21
    INTEGER, INTENT (INOUT) :: iilo,nkvecprevat,locol
    !     ..
    !     .. Array Arguments ..
    COMPLEX, INTENT (IN) :: alo(-atoms%llod:,:,:,:)
    COMPLEX, INTENT (IN) :: blo(-atoms%llod:,:,:,:)
    COMPLEX, INTENT (IN) :: clo(-atoms%llod:,:,:,:)
    REAL,    INTENT (IN) :: ar(:,0:),br(:,0:) !(nvd,0:lmd)
    REAL,    INTENT (IN) :: ai(:,0:),bi(:,0:)
    REAL,    INTENT (IN) :: el(0:)!(0:atoms%lmax)
    TYPE(t_usdus),INTENT(IN)     ::ud
    TYPE(t_tlmplm),INTENT(INOUT) :: tlmplm
43 44

    LOGICAL,INTENT(IN)      :: l_real
45 46
    REAL,    OPTIONAL,ALLOCATABLE,INTENT (INOUT) :: aa_r(:)!(matsize)
    COMPLEX, OPTIONAL,ALLOCATABLE,INTENT (INOUT) :: aa_c(:)
47 48 49 50 51 52 53 54
    !     ..
    !     .. Local Scalars ..
    COMPLEX axx,bxx,cxx,dtd,dtu,dtulo,ulotd,ulotu,ulotulo,utd,utu, utulo,chihlp
    INTEGER im,in,invsfct ,l,lm,lmp,lo,lolo,lolop,lop,lp ,matel0 
    INTEGER locol0,mp,nkvec,nkvecp,nkvecprevlo,lmplm , loplo,kp,m
    INTEGER iilo_s,locol_s,nkvecprevat_s,ic,ii,ij,n,k,mlo,mlolo
    !     ..
    !     .. Local Arrays ..
55
    COMPLEX ax(SIZE(ar,1)),bx(SIZE(ar,1)),cx(SIZE(ar,1))
56
    COMPLEX, ALLOCATABLE :: ahelp(:)
57
    INTEGER indt(0:SIZE(ar,2)-1)
58
    !     ..
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    !     .. External Subroutines ..
    EXTERNAL CPP_BLAS_caxpy
    !     ..

    mlo=0;mlolo=0
    DO m=1,ntyp-1
       mlo=mlo+atoms%nlo(m)
       mlolo=mlolo+atoms%nlo(m)*(atoms%nlo(m)+1)/2
    ENDDO


    !$OMP MASTER
    IF ((atoms%invsat(na).EQ.0) .OR. (atoms%invsat(na).EQ.1)) THEN
       !--->    if this atom is the first of two atoms related by inversion,
       !--->    the contributions to the overlap matrix of both atoms are added
       !--->    at once. where it is made use of the fact, that the sum of
       !--->    these contributions is twice the real part of the contribution
       !--->    of each atom. note, that in this case there are twice as many
       !--->    (2*(2*l+1)) k-vectors (compare abccoflo and comments there).
       IF (atoms%invsat(na).EQ.0) invsfct = 1
       IF (atoms%invsat(na).EQ.1) invsfct = 2
       nkvecprevlo = 0
       !-noco
       IF (noco%l_ss) THEN
          iilo_s = iilo
          locol_s = locol
          nkvecprevat_s = nkvecprevat ! save for other interstitial spin loops
          ic = 0                                        ! update b-matrix
          DO lo = 1,atoms%nlo(ntyp)
             ic = ic + invsfct* (2*atoms%llo(lo,ntyp)+1)
          ENDDO
          k = ic*(lapw%nv(jintsp)+nkvecprevat) + (ic+1)*ic/2
          ALLOCATE ( ahelp(k) )       ! initialize help-array
93
          ahelp=CMPLX(0.,0.)
94 95 96 97 98 99
          iilo = 0
       ENDIF
       !-noco
       !
       ! temporarily update the diagonal elements
       !
100
       DO l = 0,atoms%lnonsph(ntyp)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
          DO  m = -l,l
             lm = l* (l+1) + m
             lmplm = (lm* (lm+3))/2
             tlmplm%tuu(lmplm,ntyp,tsp)=tlmplm%tuu(lmplm,ntyp,tsp) + el(l)
             tlmplm%tdd(lmplm,ntyp,tsp)=tlmplm%tdd(lmplm,ntyp,tsp) + el(l)*ud%ddn(l,ntyp,usp)
             tlmplm%tud(lmplm,ntyp,tsp)=tlmplm%tud(lmplm,ntyp,tsp) + 0.5
             tlmplm%tdu(lmplm,ntyp,tsp)=tlmplm%tdu(lmplm,ntyp,tsp) + 0.5
             indt(lm) = tlmplm%ind(lm,lm,ntyp,tsp)
             tlmplm%ind(lm,lm,ntyp,tsp) = lmplm
          ENDDO
       ENDDO
       !
       DO lo = 1,atoms%nlo(ntyp)
          l = atoms%llo(lo,ntyp)
          matel0 = iilo
          locol0 = locol
          !--->       calculate the hamiltonian matrix elements with the regular
          !--->       flapw basis-functions
          DO m = -l,l
             lm = l* (l+1) + m
             iilo = matel0
             locol = locol0
             DO kp = 1,lapw%nv(jintsp)
124 125 126
                ax(kp) = CMPLX(0.0,0.0)
                bx(kp) = CMPLX(0.0,0.0)
                cx(kp) = CMPLX(0.0,0.0)
127
             END DO
128
             DO lp = 0,atoms%lnonsph(ntyp)
129 130 131 132 133 134 135 136 137 138 139
                DO mp = -lp,lp
                   lmp = lp* (lp+1) + mp
                   in = tlmplm%ind(lmp,lm,ntyp,tsp)
                   IF (in.NE.-9999) THEN
                      IF (in.GE.0) THEN
                         utu = tlmplm%tuu(in,ntyp,tsp)
                         dtu = tlmplm%tdu(in,ntyp,tsp)
                         utd = tlmplm%tud(in,ntyp,tsp)
                         dtd = tlmplm%tdd(in,ntyp,tsp)
                      ELSE
                         im = -in
140 141 142 143
                         utu = CONJG(tlmplm%tuu(im,ntyp,tsp))
                         dtu = CONJG(tlmplm%tud(im,ntyp,tsp))
                         utd = CONJG(tlmplm%tdu(im,ntyp,tsp))
                         dtd = CONJG(tlmplm%tdd(im,ntyp,tsp))
144 145 146 147 148 149 150 151 152
                      END IF
                      utulo = tlmplm%tuulo(lmp,m,lo+mlo,tsp)
                      dtulo = tlmplm%tdulo(lmp,m,lo+mlo,tsp)
                      !--->                   note, that utu,dtu... are the t-matrices and
                      !--->                   not their complex conjugates as in hssphn
                      !--->                   and that a,b,alo... are the complex
                      !--->                   conjugates of the a,b...-coefficients
                      DO kp = 1,lapw%nv(jintsp)
                         ax(kp) = ax(kp) + &
153 154
                              CMPLX(ar(kp,lmp),ai(kp,lmp))*utu +&
                              CMPLX(br(kp,lmp),bi(kp,lmp))*dtu
155
                         bx(kp) = bx(kp) + &
156 157
                              CMPLX(ar(kp,lmp),ai(kp,lmp))*utd +&
                              CMPLX(br(kp,lmp),bi(kp,lmp))*dtd
158
                         cx(kp) = cx(kp) + &
159 160
                              CMPLX(ar(kp,lmp),ai(kp,lmp))*utulo +&
                              CMPLX(br(kp,lmp),bi(kp,lmp))*dtulo
161 162 163 164 165 166 167
                      END DO
                   END IF
                END DO
             END DO
             !+t3e
             DO nkvec = 1,invsfct* (2*l+1)
                locol = locol + 1
168
                IF (MOD(locol-1,n_size).EQ.n_rank) THEN
169
                   !-t3e
170 171 172 173 174 175 176 177 178 179
                   IF (l_real) THEN
                      DO kp = 1,lapw%nv(jintsp)
                         iilo = iilo + 1
                         aa_r(iilo) = aa_r(iilo) + invsfct * (&
                              REAL(alo(m,nkvec,lo,iintsp))* REAL(ax(kp)) +&
                              AIMAG(alo(m,nkvec,lo,iintsp))*AIMAG(ax(kp)) +&
                              REAL(blo(m,nkvec,lo,iintsp))* REAL(bx(kp)) +&
                              AIMAG(blo(m,nkvec,lo,iintsp))*AIMAG(bx(kp)) +&
                              REAL(clo(m,nkvec,lo,iintsp))* REAL(cx(kp)) +&
                              AIMAG(clo(m,nkvec,lo,iintsp))*AIMAG(cx(kp)) )
180 181
                         IF (input%l_useapw) THEN
                            !---> APWlo
182 183 184 185 186 187 188 189 190 191
                            aa_r(iilo) = aa_r(iilo) + 0.25 * atoms%rmt(ntyp)**2 * invsfct * (&
                                 (ar(kp,lm)*ud%us(l,ntyp,usp)+&
                                 br(kp,lm)*ud%uds(l,ntyp,usp))*&
                                 REAL( alo(m,nkvec,lo,iintsp)*  ud%dus(l,ntyp,usp) +&
                                 blo(m,nkvec,lo,iintsp)* ud%duds(l,ntyp,usp)  +&
                                 clo(m,nkvec,lo,iintsp)*ud%dulos(lo,ntyp,usp) ) +&
                                 (ai(kp,lm)*ud%us(l,ntyp,usp)+bi(kp,lm)*ud%uds(l,ntyp,usp))*&
                                 AIMAG( alo(m,nkvec,lo,iintsp)*  ud%dus(l,ntyp,usp) +&
                                 blo(m,nkvec,lo,iintsp)* ud%duds(l,ntyp,usp) +&
                                 clo(m,nkvec,lo,iintsp)*ud%dulos(lo,ntyp,usp) ) )
192
                         ENDIF
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
                      ENDDO
                   ELSE
                      DO kp = 1,lapw%nv(jintsp)
                         iilo = iilo + 1
                         IF (.NOT.noco%l_ss) THEN
                            aa_c(iilo) = aa_c(iilo) + invsfct * (&
                                 alo(m,nkvec,lo,iintsp) * CONJG( ax(kp) ) +&
                                 blo(m,nkvec,lo,iintsp) * CONJG( bx(kp) ) +&
                                 clo(m,nkvec,lo,iintsp) * CONJG( cx(kp) ) )
                            IF (input%l_useapw) THEN
                               !---> APWlo
                               aa_c(iilo)=aa_c(iilo) + 0.25 * atoms%rmt(ntyp)**2 * invsfct*(&
                                    (CMPLX(ar(kp,lm),-ai(kp,lm))* ud%us(l,ntyp,usp)+&
                                    CMPLX(br(kp,lm),-bi(kp,lm))*ud%uds(l,ntyp,usp))*&
                                    (alo(m,nkvec,lo,iintsp)*  ud%dus(l,ntyp,usp)&
                                    +blo(m,nkvec,lo,iintsp)* ud%duds(l,ntyp,usp)&
                                    +clo(m,nkvec,lo,iintsp)*ud%dulos(lo,ntyp,usp) ))
                            ENDIF
                         ELSE
                            ahelp(iilo) = ahelp(iilo) + invsfct * (&
                                 alo(m,nkvec,lo,iintsp) * CONJG( ax(kp) ) +&
                                 blo(m,nkvec,lo,iintsp) * CONJG( bx(kp) ) +&
                                 clo(m,nkvec,lo,iintsp) * CONJG( cx(kp) ) )
                            IF (input%l_useapw) THEN
217

218 219 220 221 222 223 224 225
                               !---> APWlo
                               ahelp(iilo)=ahelp(iilo) + 0.25 * atoms%rmt(ntyp)**2 * invsfct*(&
                                    (CMPLX(ar(kp,lm),-ai(kp,lm))* ud%us(l,ntyp,usp)+&
                                    CMPLX(br(kp,lm),-bi(kp,lm))*ud%uds(l,ntyp,usp))*&
                                    (alo(m,nkvec,lo,iintsp)*  ud%dus(l,ntyp,usp)&
                                    +blo(m,nkvec,lo,iintsp)* ud%duds(l,ntyp,usp)&
                                    +clo(m,nkvec,lo,iintsp)*ud%dulos(lo,ntyp,usp) ))
                            ENDIF
226
                         ENDIF
227 228
                      ENDDO
                   ENDIF
229 230 231 232 233 234 235 236 237 238 239 240
                   !--->             jump to the last matrixelement of the current row
                   iilo = iilo + nkvecprevat + nkvecprevlo + nkvec
                ENDIF
             END DO
          END DO
          !--->       calculate the hamiltonian matrix elements with other
          !--->       local orbitals at the same atom and with itself
          iilo = matel0
          !+t3e
          locol = locol0
          DO nkvec = 1,invsfct* (2*l+1)
             locol = locol + 1
241
             IF (MOD(locol-1,n_size).EQ.n_rank) THEN
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                !-t3e
                !--->          skip the matrixelements with regular flapw-fcn. and
                !--->          with local orbitals at other atoms
                iilo = iilo + lapw%nv(jintsp) + nkvecprevat
                !--->          calculate the hamiltonian matrix elements with other
                !--->          local orbitals at the same atom, if they have the same l
                DO lop = 1, (lo-1)
                   lp = atoms%llo(lop,ntyp)
                   DO nkvecp = 1,invsfct* (2*lp+1)
                      iilo = iilo + 1
                      DO m = -l,l
                         lm = l* (l+1) + m
                         DO mp = -lp,lp
                            lmp = lp* (lp+1) + mp
                            in = tlmplm%ind(lmp,lm,ntyp,tsp)
                            IF (in.NE.-9999) THEN
                               IF (in.GE.0) THEN
                                  utu = tlmplm%tuu(in,ntyp,tsp)
                                  dtu = tlmplm%tdu(in,ntyp,tsp)
                                  utd = tlmplm%tud(in,ntyp,tsp)
                                  dtd = tlmplm%tdd(in,ntyp,tsp)
                               ELSE
                                  im = -in
265 266 267 268
                                  utu = CONJG(tlmplm%tuu(im,ntyp,tsp))
                                  dtu = CONJG(tlmplm%tud(im,ntyp,tsp))
                                  utd = CONJG(tlmplm%tdu(im,ntyp,tsp))
                                  dtd = CONJG(tlmplm%tdd(im,ntyp,tsp))
269 270 271
                               END IF
                               utulo = tlmplm%tuulo(lmp,m,lo+mlo,tsp)
                               dtulo = tlmplm%tdulo(lmp,m,lo+mlo,tsp)
272 273
                               ulotu=CONJG(tlmplm%tuulo(lm,mp,lop+mlo,tsp))
                               ulotd=CONJG(tlmplm%tdulo(lm,mp,lop+mlo,tsp))
274 275
                               !--->                         note that lo > lop
                               lolop = ((lo-1)*lo)/2 + lop
276 277 278 279 280 281 282
                               ulotulo = CONJG(tlmplm%tuloulo (m,mp,lolop+mlolo,tsp))
                               axx=CONJG(alo(m,nkvec,lo,iintsp))*utu +&
                                    CONJG(blo(m,nkvec,lo,iintsp))*utd +&
                                    CONJG(clo(m,nkvec,lo,iintsp))*utulo
                               bxx=CONJG(alo(m,nkvec,lo,iintsp))*dtu +&
                                    CONJG(blo(m,nkvec,lo,iintsp))*dtd +&
                                    CONJG(clo(m,nkvec,lo,iintsp))*dtulo
283
                               cxx = &
284 285 286
                                    CONJG(alo(m,nkvec,lo,iintsp))*ulotu +&
                                    CONJG(blo(m,nkvec,lo,iintsp))*ulotd +&
                                    CONJG(clo(m,nkvec,lo,iintsp))*ulotulo
287 288 289 290 291 292 293 294
                               IF (l_real) THEN
                                  aa_r(iilo) = aa_r(iilo) + invsfct * (&
                                       REAL(alo(mp,nkvecp,lop,jintsp))* REAL(axx) -&
                                       AIMAG(alo(mp,nkvecp,lop,jintsp))*AIMAG(axx) +&
                                       REAL(blo(mp,nkvecp,lop,jintsp))* REAL(bxx) -&
                                       AIMAG(blo(mp,nkvecp,lop,jintsp))*AIMAG(bxx) +&
                                       REAL(clo(mp,nkvecp,lop,jintsp))* REAL(cxx) -&
                                       AIMAG(clo(mp,nkvecp,lop,jintsp))*AIMAG(cxx) )
295
                               ELSE
296 297 298 299 300 301 302 303 304 305 306
                                  IF (.NOT.noco%l_ss) THEN
                                     aa_c(iilo) = aa_c(iilo) + invsfct * CONJG(&
                                          alo(mp,nkvecp,lop,jintsp) * axx +&
                                          blo(mp,nkvecp,lop,jintsp) * bxx +&
                                          clo(mp,nkvecp,lop,jintsp) * cxx )
                                  ELSE
                                     ahelp(iilo)=ahelp(iilo)+invsfct*CONJG(&
                                          alo(mp,nkvecp,lop,jintsp) * axx +&
                                          blo(mp,nkvecp,lop,jintsp) * bxx +&
                                          clo(mp,nkvecp,lop,jintsp) * cxx )
                                  ENDIF
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                               ENDIF
                            END IF
                         END DO
                      END DO
                   END DO
                END DO
                !--->          calculate the hamiltonian matrix elements of one local
                !--->          orbital with itself
                DO nkvecp = 1,nkvec
                   iilo = iilo + 1
                   DO m = -l,l
                      lm = l* (l+1) + m
                      DO mp = -l,l
                         lmp = l* (l+1) + mp
                         in = tlmplm%ind(lmp,lm,ntyp,tsp)
                         IF (in.NE.-9999) THEN
                            IF (in.GE.0) THEN
                               utu = tlmplm%tuu(in,ntyp,tsp)
                               dtu = tlmplm%tdu(in,ntyp,tsp)
                               utd = tlmplm%tud(in,ntyp,tsp)
                               dtd = tlmplm%tdd(in,ntyp,tsp)
                            ELSE
                               im = -in
330 331 332 333
                               utu = CONJG(tlmplm%tuu(im,ntyp,tsp))
                               dtu = CONJG(tlmplm%tud(im,ntyp,tsp))
                               utd = CONJG(tlmplm%tdu(im,ntyp,tsp))
                               dtd = CONJG(tlmplm%tdd(im,ntyp,tsp))
334 335 336
                            END IF
                            utulo = tlmplm%tuulo(lmp,m,lo+mlo,tsp)
                            dtulo = tlmplm%tdulo(lmp,m,lo+mlo,tsp)
337 338
                            ulotu = CONJG(tlmplm%tuulo(lm,mp,lo+mlo,tsp))
                            ulotd = CONJG(tlmplm%tdulo(lm,mp,lo+mlo,tsp))
339
                            lolo = ((lo-1)*lo)/2 + lo
340 341 342 343 344 345 346 347 348 349
                            ulotulo =CONJG(tlmplm%tuloulo(m,mp,lolo+mlolo,tsp))
                            axx = CONJG(alo(m,nkvec,lo,iintsp))*utu +&
                                 CONJG(blo(m,nkvec,lo,iintsp))*utd +&
                                 CONJG(clo(m,nkvec,lo,iintsp))*utulo
                            bxx = CONJG(alo(m,nkvec,lo,iintsp))*dtu +&
                                 CONJG(blo(m,nkvec,lo,iintsp))*dtd +&
                                 CONJG(clo(m,nkvec,lo,iintsp))*dtulo
                            cxx = CONJG(alo(m,nkvec,lo,iintsp))*ulotu +&
                                 CONJG(blo(m,nkvec,lo,iintsp))*ulotd +&
                                 CONJG(clo(m,nkvec,lo,iintsp))*ulotulo
350 351 352 353 354 355 356 357
                            IF (l_real) THEN
                               aa_r(iilo) = aa_r(iilo) + invsfct* (&
                                    REAL(alo(mp,nkvecp,lo,jintsp))* REAL(axx) -&
                                    AIMAG(alo(mp,nkvecp,lo,jintsp))*AIMAG(axx) +&
                                    REAL(blo(mp,nkvecp,lo,jintsp))* REAL(bxx) -&
                                    AIMAG(blo(mp,nkvecp,lo,jintsp))*AIMAG(bxx) +&
                                    REAL(clo(mp,nkvecp,lo,jintsp))* REAL(cxx) -&
                                    AIMAG(clo(mp,nkvecp,lo,jintsp))*AIMAG(cxx) )
358
                            ELSE
359 360 361 362 363 364 365 366 367 368 369
                               IF (.NOT.noco%l_ss) THEN
                                  aa_c(iilo) = aa_c(iilo) + invsfct* CONJG(&
                                       alo(mp,nkvecp,lo,jintsp)*axx +&
                                       blo(mp,nkvecp,lo,jintsp)*bxx +&
                                       clo(mp,nkvecp,lo,jintsp)*cxx )
                               ELSE
                                  ahelp(iilo) = ahelp(iilo) + invsfct* CONJG(&
                                       alo(mp,nkvecp,lo,jintsp)*axx +&
                                       blo(mp,nkvecp,lo,jintsp)*bxx +&
                                       clo(mp,nkvecp,lo,jintsp)*cxx )
                               ENDIF
370 371 372 373 374 375 376 377 378 379 380 381 382
                            ENDIF
                         END IF
                      END DO
                   END DO
                END DO
             ENDIF
             !-t3e
          END DO
          nkvecprevlo = nkvecprevlo + invsfct* (2*l+1)
       END DO ! end of lo = 1,atoms%nlo loop
       !
       ! remove the temporary update of the diagonal elements
       !
383
       DO l = 0,atoms%lnonsph(ntyp)
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
          DO  m = -l,l
             lm = l* (l+1) + m
             lmplm = (lm* (lm+3))/2
             tlmplm%tuu(lmplm,ntyp,tsp)=tlmplm%tuu(lmplm,ntyp,tsp) - el(l)
             tlmplm%tdd(lmplm,ntyp,tsp)=tlmplm%tdd(lmplm,ntyp,tsp) - el(l)*ud%ddn(l,ntyp,usp)
             tlmplm%tud(lmplm,ntyp,tsp)=tlmplm%tud(lmplm,ntyp,tsp) - 0.5
             tlmplm%tdu(lmplm,ntyp,tsp)=tlmplm%tdu(lmplm,ntyp,tsp) - 0.5
             tlmplm%ind(lm,lm,ntyp,tsp) = indt(lm)
          ENDDO
       ENDDO
       !
       nkvecprevat = nkvecprevat + nkvecprevlo
       !+noco
       IF (noco%l_ss) THEN
          IF ( iintsp.EQ.1 .AND. jintsp.EQ.1 ) THEN     !---> spin-up spin-up part
             chihlp = chi11
             n = lapw%nv(1) + nkvecprevat_s
             ij = (n+1)*n/2
          ELSEIF ( iintsp.EQ.2 .AND. jintsp.EQ.2 ) THEN !---> spin-down spin-down part
             chihlp = chi22
             n = lapw%nv(1) + lapw%nv(iintsp) + atoms%nlotot + nkvecprevat_s
             ij = (n+1)*n/2 + lapw%nv(1) + atoms%nlotot
          ELSE                                          !---> spin-down spin-up part
             chihlp = chi21
             n = lapw%nv(1) + lapw%nv(iintsp) + atoms%nlotot + nkvecprevat_s
             ij = (n+1)*n/2
          ENDIF

          ic = 0                                        ! update b-matrix
          DO lo = 1,atoms%nlo(ntyp)
             ic = ic + invsfct* (2*atoms%llo(lo,ntyp)+1)
          ENDDO
416
          IF (.NOT.( iintsp.EQ.1 .AND. jintsp.EQ.2 )) THEN
417 418 419
             ii = 0
             DO k = 1, ic
                n = k + lapw%nv(jintsp) + nkvecprevat_s
420 421 422 423 424 425
                IF (.NOT.l_real) THEN
                   IF (iintsp.EQ.2 .AND. jintsp.EQ.1 ) THEN
                      CALL CPP_BLAS_caxpy(n-1,chihlp,ahelp(ii+1),1,aa_c(ij+1),1)
                   ELSE
                      CALL CPP_BLAS_caxpy(n,chihlp,ahelp(ii+1),1,aa_c(ij+1),1)
                   ENDIF
426 427 428 429 430 431 432 433 434 435 436
                ENDIF
                ii = ii + n
                ij = ij + n + (lapw%nv(3-jintsp)+atoms%nlotot)*(iintsp-1)
             ENDDO
          ELSE                                         ! special treatment for up-down:
             n = lapw%nv(1) + atoms%nlotot
             ii = 0
             DO k = 1, ic
                ij = (n+1)*n/2 + lapw%nv(1) + k + nkvecprevat_s
                DO kp = 1, k + lapw%nv(jintsp) + nkvecprevat_s
                   ii = ii + 1
437
                   aa_c(ij) = aa_c(ij) + chihlp * CONJG( ahelp(ii) )
438 439 440 441 442
                   ij = ij + lapw%nv(1) + kp + atoms%nlotot
                ENDDO
             ENDDO
          ENDIF
          DEALLOCATE ( ahelp )
443
          IF (.NOT.( iintsp.EQ.2 .AND. jintsp.EQ.2 )) THEN
444 445 446 447 448 449 450 451 452 453 454 455 456
             iilo = iilo_s
             locol = locol_s             ! restore for other loops
             nkvecprevat = nkvecprevat_s
          ENDIF
       ELSE
          k = lapw%nv(1) * ( lapw%nv(1) + 1 ) / 2
       ENDIF
       !-noco
    END IF
    !$OMP END MASTER
    !$OMP barrier
  END SUBROUTINE hlomat
      END MODULE m_hlomat