vacden.F90 60.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
MODULE m_vacden
  USE m_juDFT
  !     *************************************************************
  !     determines the 2-d star function expansion coefficients of
  !     vacuum charge density. speed up by r. wu 1992
  !     *************************************************************
CONTAINS
  SUBROUTINE vacden(&
       vacuum,DIMENSION,stars,oneD,&
       kpts,input,cell,atoms,noco,banddos,&
       gvac1,gvac2,&
12
       we,ikpt,jspin,vz,&
13
       ne,lapw,&
Gregor Michalicek's avatar
Gregor Michalicek committed
14 15
       evac,eig,den,qvac,qvlay,&
       stcoeff,zMat)
16 17 18 19 20 21 22 23

    !***********************************************************************
    !     ****** change vacden(....,q) for vacuum density of states shz Jan.96
    !     ****** change vacden(......,stcoeff) for starcoefficients, shz. Jan.99
    !     ****** changed for fleur dw
    !     In non-collinear calculations the density becomes a hermitian 2x2
    !     matrix. This subroutine generates this density matrix in the 
    !     vacuum region. The diagonal elements of this matrix (n_11 & n_22)
Gregor Michalicek's avatar
Gregor Michalicek committed
24
    !     are store in den%vacz and den%vacxy, while the real and imaginary part
Gregor Michalicek's avatar
Gregor Michalicek committed
25
    !     of the off-diagonal element are stored in den%vacz(:,:,3:4) and den%vacxy(:,:,:,3). 
26 27 28 29 30 31 32 33 34 35
    !
    !     Philipp Kurz 99/07
    !***********************************************************************

    !******** ABBREVIATIONS ************************************************
    !     qvac     : vacuum charge of each eigenstate, needed in in cdnval
    !                to determine the vacuum energy parameters
    !     vz       : non-warping part of the vacuum potential (matrix)
    !                collinear    : 2. index = ivac (# of vaccum)
    !                non-collinear: 2. index = ipot (comp. of pot. matr.)
Gregor Michalicek's avatar
Gregor Michalicek committed
36
    !     den%vacz : non-warping part of the vacuum density matrix,
37
    !                diagonal elements n_11 and n_22
Gregor Michalicek's avatar
Gregor Michalicek committed
38
    !     den%vacxy: warping part of the vacuum density matrix,
39
    !                diagonal elements n_11 and n_22
Gregor Michalicek's avatar
Gregor Michalicek committed
40 41
    !     den%vacz(:,:,3:4): non-warping part of the vacuum density matrix,
    !                off-diagonal elements n_21 (real part in (:,:,3), imaginary part in (:,:,4))
Gregor Michalicek's avatar
Gregor Michalicek committed
42
    !     den%vacxy(:,:,:,3): warping part of the vacuum density matrix,
43 44 45
    !                off-diagonal elements n_21
    !***********************************************************************
    !
Daniel Wortmann's avatar
Daniel Wortmann committed
46
    USE m_constants
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    USE m_grdchlh
    USE m_qsf
    USE m_cylbes
    USE m_dcylbs
    USE m_od_abvac
    USE m_vacuz
    USE m_vacudz
    USE m_types
    IMPLICIT NONE
    TYPE(t_lapw),INTENT(INOUT)    :: lapw !for some reason the second spin data is reset in noco case
    TYPE(t_dimension),INTENT(IN)  :: DIMENSION
    TYPE(t_oneD),INTENT(IN)       :: oneD
    TYPE(t_banddos),INTENT(IN)    :: banddos
    TYPE(t_input),INTENT(IN)      :: input
    TYPE(t_vacuum),INTENT(IN)     :: vacuum
    TYPE(t_noco),INTENT(IN)       :: noco
    TYPE(t_stars),INTENT(IN)      :: stars
    TYPE(t_cell),INTENT(IN)       :: cell
    TYPE(t_kpts),INTENT(IN)       :: kpts
    TYPE(t_atoms),INTENT(IN)      :: atoms
67
    TYPE(t_zMat),INTENT(IN)       :: zMat
Gregor Michalicek's avatar
Gregor Michalicek committed
68
    TYPE(t_potden),INTENT(INOUT)  :: den
69 70 71 72 73
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: jspin      
    INTEGER, INTENT (IN) :: ne    
    INTEGER, INTENT (IN) :: ikpt
    INTEGER,PARAMETER    :: n2max=13
Daniel Wortmann's avatar
Daniel Wortmann committed
74
    REAL,PARAMETER        :: emax=2.0/hartree_to_ev_const
75
    !     .. Array Arguments ..
76 77 78 79 80
    REAL,    INTENT(IN)    :: evac(2,DIMENSION%jspd)
    REAL,    INTENT(OUT)   :: qvlay(DIMENSION%neigd,vacuum%layerd,2,kpts%nkpt,DIMENSION%jspd)
    REAL,    INTENT(INOUT) :: qvac(DIMENSION%neigd,2,kpts%nkpt,DIMENSION%jspd)
    REAL,    INTENT(IN)    :: we(DIMENSION%neigd)
    REAL                   :: vz(vacuum%nmzd,2) ! Note this breaks the INTENT(IN) from cdnval. It may be read from a file in this subroutine.
81 82
    !     STM-Arguments
    REAL,    INTENT (IN) :: eig(DIMENSION%neigd)  
83
    INTEGER, INTENT (IN) :: gvac1(DIMENSION%nv2d),gvac2(DIMENSION%nv2d)
84 85 86 87 88 89 90 91 92
    COMPLEX, INTENT (OUT):: stcoeff(vacuum%nstars,DIMENSION%neigd,vacuum%layerd,2)
    !
    !     local STM variables
    INTEGER nv2(DIMENSION%jspd)
    INTEGER kvac1(DIMENSION%nv2d,DIMENSION%jspd),kvac2(DIMENSION%nv2d,DIMENSION%jspd),map2(DIMENSION%nvd,DIMENSION%jspd)
    INTEGER kvac3(DIMENSION%nv2d,DIMENSION%jspd),map1(DIMENSION%nvd,DIMENSION%jspd)
    INTEGER mapg2k(DIMENSION%nv2d)
    !     .. Local Scalars ..
    COMPLEX aa,ab,av,ba,bb,bv,t1,aae,bbe,abe,bae,aaee,bbee,abee,baee,&
Gregor Michalicek's avatar
Gregor Michalicek committed
93
         &     factorx,factory,c_1,aa_1,ab_1,ba_1,bb_1,ic,av_1,bv_1,d,tempCmplx
94 95 96 97 98 99 100 101 102 103 104

    REAL arg,const,ddui,dduj,dduei,dduej,eps,ev,evacp,phs,phsp,qout,&
         &     scale,sign,uei,uej,ui,uj,wronk,zks,RESULT(1),ui2,uei2,&
         &     k_diff,k_d1,k_d2,ui_1,uei_1,uj_1,uej_1,wronk_1

    INTEGER i,ii,i1,i2,i3,ig3,ig3p,ik,ind2,ind2p,istar ,m1,m3,&
         &        ivac,j,jj,jz,k,l,ll,l1,n,n2,ispin,kspin,jsp_start,jsp_end,&
         &        ipot,ie,imz,isp_start,isp_end,&
         &        ind1,ind1p,irec2,irec3,m
    !
    !     .. Local Arrays ..
105
    REAL qssbti(3,2),qssbtii,vz0(2)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    REAL bess(-oneD%odi%mb:oneD%odi%mb),dbss(-oneD%odi%mb:oneD%odi%mb)
    COMPLEX, ALLOCATABLE :: ac(:,:,:),bc(:,:,:)
    REAL,    ALLOCATABLE :: dt(:),dte(:),du(:),ddu(:,:),due(:)
    REAL,    ALLOCATABLE :: ddue(:,:),t(:),te(:),tei(:,:),dummy(:)
    REAL,    ALLOCATABLE :: u(:,:,:),ue(:,:,:),v(:),yy(:)
    !-odim
    COMPLEX, ALLOCATABLE :: ac_1(:,:,:,:),bc_1(:,:,:,:)
    REAL,    ALLOCATABLE :: dt_1(:,:),dte_1(:,:)
    REAL,    ALLOCATABLE :: du_1(:,:),ddu_1(:,:,:),due_1(:,:)
    REAL,    ALLOCATABLE :: ddue_1(:,:,:),t_1(:,:)
    REAL,    ALLOCATABLE :: tei_1(:,:,:),te_1(:,:)
    REAL,    ALLOCATABLE :: u_1(:,:,:,:),ue_1(:,:,:,:)
    !+odim
    !     ..
    !     ..

    !     *******************************************************************************
    !

    !
    !    layers: no. of layers to be calculated (in vertical direction with z-values as given by izlay)
    !    izlay : defines vertical position of layers in delz (=0.1 a.u.) units from begining of vacuum region
    !    vacdos: =T: calculate vacuum dos in layers as given by the above
    !    integ : =T: integrate in vertical position between izlay(layer,1)..izlay(layer,2)
    !    nstm  : 0: s-Tip, 1: p_z-Tip, 2: d_z^2-Tip (following Chen's derivative rule) ->rhzgrd.f is used 
    !                 to calculate derivatives 
    !    tworkf: Workfunction of Tip (in hartree units) is needed for d_z^2-Orbital)
    !    starcoeff: =T: star coefficients are calculated at values of izlay for 0th (=q) to nstars-1. star 
    !                (stcoeff(1..nstars-1))
    !    nstars: number of star functions to be used (0th star is given by value of q=charge integrated in 2D) 
    !
    !    further possibility: (readin of locx, locy has to be implemented in flapw7.f or they have to be set explicitly)
    !
    !     locx and locy can be used to calculate local DOS at a certain vertical position z (or integrated in z)
    !     within a restricted area of the 2D unit cell, the corners of this area is given by locx and locy
    !     they are defined in internal coordinates, i.e. \vec{r}_1=locx(1)*\vec{a}_1+locy(1)*\vec{a}_2
    !                                                    \vec{r}_2=locx(2)*\vec{a}_1+locy(2)*\vec{a}_2
    !                 \vec{a}_1,2 are the 2D lattice vectors           
    !  
    !     **************************************************************************************************
    ! 
    ALLOCATE ( ac(DIMENSION%nv2d,DIMENSION%neigd,DIMENSION%jspd),bc(DIMENSION%nv2d,DIMENSION%neigd,DIMENSION%jspd),dt(DIMENSION%nv2d),&
         &           dte(DIMENSION%nv2d),du(vacuum%nmzd),ddu(vacuum%nmzd,DIMENSION%nv2d),due(vacuum%nmzd),&
         &           ddue(vacuum%nmzd,DIMENSION%nv2d),t(DIMENSION%nv2d),te(DIMENSION%nv2d),&
         &           tei(DIMENSION%nv2d,DIMENSION%jspd),u(vacuum%nmzd,DIMENSION%nv2d,DIMENSION%jspd),ue(vacuum%nmzd,DIMENSION%nv2d,DIMENSION%jspd),&
         &           v(3),yy(vacuum%nmzd))
    IF (oneD%odi%d1) THEN
       ALLOCATE (      ac_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb,DIMENSION%neigd,DIMENSION%jspd),&
            &                  bc_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb,DIMENSION%neigd,DIMENSION%jspd),&
            &                  dt_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                 dte_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                  du_1(vacuum%nmzd,-oneD%odi%mb:oneD%odi%mb),&
            &            ddu_1(vacuum%nmzd,DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                 due_1(vacuum%nmzd,-oneD%odi%mb:oneD%odi%mb),&
            &           ddue_1(vacuum%nmzd,DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                   t_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                  te_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb),&
            &                 tei_1(DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb,DIMENSION%jspd),&
            &              u_1(vacuum%nmzd,DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb,DIMENSION%jspd),&
            &             ue_1(vacuum%nmzd,DIMENSION%nv2d,-oneD%odi%mb:oneD%odi%mb,DIMENSION%jspd) )
    END IF ! oneD%odi%d1
    !

169
    vz0(:) = vz(vacuum%nmz,:)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    eps=0.01
    ic = CMPLX(0.,1.)
    !    ------------------
    !     WRITE (16,'(a,i2)') 'nstars=',nstars

    stcoeff(:,:,:,:) = CMPLX(0.0,0.0)

    !     -----> set up mapping arrays
    IF (noco%l_ss) THEN
       jsp_start = 1
       jsp_end   = 2
    ELSE
       jsp_start = jspin
       jsp_end   = jspin
    ENDIF
    DO ispin = jsp_start,jsp_end
       IF (oneD%odi%d1) THEN
          n2 = 0
          k_loop:DO  k = 1,lapw%nv(ispin)
             DO  j = 1,n2
                IF (lapw%k3(k,ispin).EQ.kvac3(j,ispin)) THEN
                   map1(k,ispin) = j
                   CYCLE k_loop
                END IF
             ENDDO
             n2 = n2 + 1
             IF (n2>DIMENSION%nv2d)  CALL juDFT_error("vacden0",calledby ="vacden")
             kvac3(n2,ispin) =  lapw%k3(k,ispin)
             map1(k,ispin) = n2
          ENDDO k_loop
          nv2(ispin) = n2
       ELSE
          n2 = 0
          k_loop2:DO  k = 1,lapw%nv(ispin)
             DO  j = 1,n2
205 206
                IF ( lapw%gvec(1,k,ispin).EQ.kvac1(j,ispin) .AND.&
                     lapw%gvec(2,k,ispin).EQ.kvac2(j,ispin) ) THEN
207 208 209 210 211 212
                   map2(k,ispin) = j
                   CYCLE k_loop2
                END IF
             ENDDO
             n2 = n2 + 1
             IF (n2>DIMENSION%nv2d)  CALL juDFT_error("vacden0","vacden")
213 214
             kvac1(n2,ispin) = lapw%gvec(1,k,ispin)
             kvac2(n2,ispin) = lapw%gvec(2,k,ispin)
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 292 293 294 295 296 297 298 299 300 301 302
             map2(k,ispin) = n2
          ENDDO k_loop2
          nv2(ispin) = n2
       END IF
    ENDDO
    IF ( noco%l_noco .AND. (.NOT. noco%l_ss) ) THEN
       lapw%nv(2)  = lapw%nv(1)
       nv2(2) = nv2(1)
       DO k = 1,nv2(1)
          kvac1(k,2) = kvac1(k,1)
          kvac2(k,2) = kvac2(k,1)
          IF(oneD%odi%d1) kvac3(k,2) =  kvac3(k,1)
       ENDDO
       DO k = 1,lapw%nv(1)
          lapw%k3(k,2) = lapw%k3(k,1)
          map2(k,2) = map2(k,1)
          IF(oneD%odi%d1)THEN
             lapw%k1(k,2) = lapw%k1(k,1)
             lapw%k2(k,2) = lapw%k2(k,1)
             map1(k,2) = map1(k,1)
          ENDIF
       ENDDO
    ENDIF

    !+dw
    !    if tunneling current should be calculated we need to write out 
    !     info on electronic structure: --> mapping from kvac to gvac by mapg2k
    !                                             shz, Jan.99
    IF (vacuum%nstm.EQ.3) THEN
       DO j=1, n2max
          mapg2k(j)=j
          DO i=1, nv2(jspin)
             IF ( kvac1(i,jspin).EQ.gvac1(j) .AND.&
                  &              kvac2(i,jspin).EQ.gvac2(j) ) &
                  &              mapg2k(j)=i
          END DO
       END DO
    END IF
    !
    !-dw
    IF (noco%l_noco) THEN
       OPEN (25,FILE='potmat',FORM='unformatted',&
            &         STATUS='old')
       !--->    skip the four components of the interstitial potential matrix
       DO ipot = 1,3
          READ (25)
       ENDDO
    ENDIF

    wronk = 2.0
    const = 1.0 / ( SQRT(cell%omtil)*wronk )
    !-odim
    IF (oneD%odi%d1) THEN
       ac_1(:,:,:,:) = CMPLX(0.0,0.0)
       bc_1(:,:,:,:) = CMPLX(0.0,0.0)
    END IF
    !+odim
    DO  ivac = 1,vacuum%nvac
       ac(:,:,:) = CMPLX(0.0,0.0)
       bc(:,:,:) = CMPLX(0.0,0.0)
       sign = 3. - 2.*ivac
       IF (noco%l_noco) THEN
          !--->       read the non-warping potential matrix, it is needed to 
          !--->       calculate the vacuum basis functions
          READ (25)((vz(imz,ipot),imz=1,vacuum%nmzd),ipot=1,2)
          !--->       skip the warping potential matrix
          DO ipot = 1,3
             READ (25)
          ENDDO
       ENDIF
       IF (noco%l_noco) THEN
          !--->    In a non-collinear calculation vacden is only called once.
          !--->    Thus, the vaccum wavefunctions and the A- and B-coeff. (ac bc)
          !--->    have to be calculated for both spins on that call.
          !--->       setup the spin-spiral q-vector
          qssbti(1,1) = - noco%qss(1)/2
          qssbti(2,1) = - noco%qss(2)/2
          qssbti(1,2) = + noco%qss(1)/2
          qssbti(2,2) = + noco%qss(2)/2
          qssbti(3,1) = - noco%qss(3)/2
          qssbti(3,2) = + noco%qss(3)/2
          DO ispin = 1,input%jspins
             !     -----> set up vacuum wave functions
             IF (oneD%odi%d1) THEN
                CALL od_abvac(&
                     cell,vacuum,DIMENSION,stars,&
                     oneD,qssbti(3,ispin),&
                     oneD%odi%n2d,&
303
                     wronk,evacp,lapw%bkpt,oneD%odi%M,oneD%odi%mb,&
304 305 306 307 308 309 310 311
                     vz(1,ispin),kvac3(1,ispin),nv2(ispin),&
                     t_1(1,-oneD%odi%mb),dt_1(1,-oneD%odi%mb),u_1(1,1,-oneD%odi%mb,ispin),&
                     te_1(1,-oneD%odi%mb),dte_1(1,-oneD%odi%mb),&
                     tei_1(1,-oneD%odi%mb,ispin),&
                     ue_1(1,1,-oneD%odi%mb,ispin))
                DO k = 1,lapw%nv(ispin)
                   kspin = (lapw%nv(1)+atoms%nlotot)*(ispin-1) + k
                   l = map1(k,ispin) 
312
                   irec3 = stars%ig(lapw%gvec(1,k,ispin),lapw%gvec(2,k,ispin),lapw%gvec(3,k,ispin))
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                   IF (irec3.NE.0) THEN
                      irec2 = stars%ig2(irec3)
                      zks = stars%sk2(irec2)*cell%z1
                      arg = stars%phi2(irec2)
                      CALL cylbes(oneD%odi%mb,zks,bess)
                      CALL dcylbs(oneD%odi%mb,zks,bess,dbss)
                      DO m = -oneD%odi%mb,oneD%odi%mb
                         wronk_1 = t_1(l,m)*dte_1(l,m) -&
                              te_1(l,m)*dt_1(l,m)
                         av_1 = EXP(-CMPLX(0.0,m*arg))*(ic**m)*&
                              CMPLX(dte_1(l,m)*bess(m) -&
                              te_1(l,m)*stars%sk2(irec2)*dbss(m),0.0)/&
                              ((wronk_1)*SQRT(cell%omtil))
                         bv_1 = EXP(-CMPLX(0.0,m*arg))*(ic**m)*&
                              CMPLX(-dt_1(l,m)*bess(m) +&
                              t_1(l,m)*stars%sk2(irec2)*dbss(m),0.0)/&
                              ((wronk_1)*SQRT(cell%omtil))
330
                         IF (zmat%l_real) THEN
331 332
                            ac_1(l,m,:ne,ispin) = ac_1(l,m,:ne,ispin) + zMat%z_r(kspin,:ne)*av_1
                            bc_1(l,m,:ne,ispin) = bc_1(l,m,:ne,ispin) + zMat%z_r(kspin,:ne)*bv_1
333
                         ELSE
334 335
                            ac_1(l,m,:ne,ispin) = ac_1(l,m,:ne,ispin) + zMat%z_c(kspin,:ne)*av_1
                            bc_1(l,m,:ne,ispin) = bc_1(l,m,:ne,ispin) + zMat%z_c(kspin,:ne)*bv_1
336
                         END IF
337 338 339 340 341 342 343
                      END DO      ! -mb:mb
                   END IF
                END DO
             ELSE ! 1-dimensional
                vz0(ispin) = vz(vacuum%nmz,ispin)
                evacp = evac(ivac,ispin)
                DO ik = 1,nv2(ispin)
344 345
                   v(1) = lapw%bkpt(1) + kvac1(ik,ispin) + qssbti(1,ispin)
                   v(2) = lapw%bkpt(2) + kvac2(ik,ispin) + qssbti(2,ispin)
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                   v(3) = 0.
                   ev = evacp - 0.5*DOT_PRODUCT(v,MATMUL(v,cell%bbmat))
                   CALL vacuz(ev,vz(1,ispin),vz0(ispin),vacuum%nmz,vacuum%delz,t(ik),&
                        dt(ik),u(1,ik,ispin))
                   CALL vacudz(ev,vz(1,ispin),vz0(ispin),vacuum%nmz,vacuum%delz,te(ik),&
                        dte(ik),tei(ik,ispin),ue(1,ik,ispin),dt(ik),&
                        u(1,ik,ispin))
                   scale = wronk/ (te(ik)*dt(ik)-dte(ik)*t(ik))
                   te(ik) = scale*te(ik)
                   dte(ik) = scale*dte(ik)
                   tei(ik,ispin) = scale*tei(ik,ispin)
                   DO j = 1,vacuum%nmz
                      ue(j,ik,ispin) = scale*ue(j,ik,ispin)
                   ENDDO
                ENDDO
                !     -----> construct a and b coefficients
                DO k = 1,lapw%nv(ispin)
                   !--->          the coefficients of the spin-down basis functions are
                   !--->          stored in the second half of the eigenvector
                   kspin = (lapw%nv(1)+atoms%nlotot)*(ispin-1) + k
                   l = map2(k,ispin)
                   zks = lapw%k3(k,ispin)*cell%bmat(3,3)*sign
                   arg = zks*cell%z1
                   c_1 = CMPLX(COS(arg),SIN(arg)) * const
                   av = -c_1 * CMPLX( dte(l),zks*te(l) ) 
                   bv =  c_1 * CMPLX(  dt(l),zks* t(l) ) 
                   !     -----> loop over basis functions
373
                   IF (zmat%l_real) THEN
374 375
                      ac(l,:ne,ispin) = ac(l,:ne,ispin) + zMat%z_r(kspin,:ne)*av
                      bc(l,:ne,ispin) = bc(l,:ne,ispin) + zMat%z_r(kspin,:ne)*bv
376
                   ELSE
377 378
                      ac(l,:ne,ispin) = ac(l,:ne,ispin) + zMat%z_c(kspin,:ne)*av
                      bc(l,:ne,ispin) = bc(l,:ne,ispin) + zMat%z_c(kspin,:ne)*bv
379
                   ENDIF
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                ENDDO
                !--->       end of spin loop
             ENDIF
             !---Y       end of geometry 1d/film
          ENDDO
          !--->       output for testing
          !            DO k = 1,10
          !               DO n = 1,5
          !                  DO ispin = 1,jspins
          !                     write(*,9000)k,n,ispin,ac(k,n,ispin),bc(k,n,ispin)
          !                  ENDDO
          !               ENDDO
          !            ENDDO
          ! 9000       FORMAT('k=',i3,' ie=',i3,' isp=',i3,
          !     +             ' ac= (',e12.6,',',e12.6,')',
          !     +             ' bc= (',e12.6,',',e12.6,')')
       ELSE
          !     -----> set up vacuum wave functions
          IF (oneD%odi%d1) THEN
             qssbtii = 0.
             evacp = evac(ivac,jspin)
             CALL od_abvac(&
                  &           cell,vacuum,DIMENSION,stars,&
                  &           oneD,qssbtii,&
                  &           oneD%odi%n2d,&
405
                  &           wronk,evacp,lapw%bkpt,oneD%odi%M,oneD%odi%mb,&
406 407 408 409 410 411 412
                  &           vz(1,ivac),kvac3(1,jspin),nv2(jspin),&
                  &           t_1(1,-oneD%odi%mb),dt_1(1,-oneD%odi%mb),u_1(1,1,-oneD%odi%mb,jspin),&
                  &           te_1(1,-oneD%odi%mb),dte_1(1,-oneD%odi%mb),&
                  &           tei_1(1,-oneD%odi%mb,jspin),&
                  &           ue_1(1,1,-oneD%odi%mb,jspin))
             DO k = 1,lapw%nv(jspin)
                l = map1(k,jspin)
413
                irec3 = stars%ig(lapw%gvec(1,k,jspin),lapw%gvec(2,k,jspin),lapw%gvec(3,k,jspin))
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                IF (irec3.NE.0) THEN
                   irec2 = stars%ig2(irec3)
                   zks = stars%sk2(irec2)*cell%z1
                   arg = stars%phi2(irec2)
                   CALL cylbes(oneD%odi%mb,zks,bess)
                   CALL dcylbs(oneD%odi%mb,zks,bess,dbss)
                   DO m = -oneD%odi%mb,oneD%odi%mb
                      wronk_1 = t_1(l,m)*dte_1(l,m) -te_1(l,m)*dt_1(l,m)
                      av_1 = EXP(-CMPLX(0.0,m*arg))*(ic**m)*&
                           CMPLX(dte_1(l,m)*bess(m) -&
                           te_1(l,m)*stars%sk2(irec2)*dbss(m),0.0)/&
                           ((wronk_1)*SQRT(cell%omtil))
                      bv_1 = EXP(-CMPLX(0.0,m*arg))*(ic**m)*&
                           CMPLX(-dt_1(l,m)*bess(m) +&
                           t_1(l,m)*stars%sk2(irec2)*dbss(m),0.0)/&
                           ((wronk_1)*SQRT(cell%omtil))
430
                      IF (zmat%l_real) THEN
431 432
                         ac_1(l,m,:ne,jspin) = ac_1(l,m,:ne,jspin) + zMat%z_r(k,:ne)*av_1
                         bc_1(l,m,:ne,jspin) = bc_1(l,m,:ne,jspin) + zMat%z_r(k,:ne)*bv_1
433
                      ELSE
434 435
                         ac_1(l,m,:ne,jspin) = ac_1(l,m,:ne,jspin) + zMat%z_c(k,:ne)*av_1
                         bc_1(l,m,:ne,jspin) = bc_1(l,m,:ne,jspin) + zMat%z_c(k,:ne)*bv_1
436
                      ENDIF
437 438 439 440 441 442
                   END DO      ! -mb:mb
                END IF
             END DO         ! k = 1,lapw%nv
          ELSE     !oneD%odi%d1
             evacp = evac(ivac,jspin)
             DO ik = 1,nv2(jspin)
443 444
                v(1) = lapw%bkpt(1) + kvac1(ik,jspin)
                v(2) = lapw%bkpt(2) + kvac2(ik,jspin)
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                v(3) = 0.
                ev = evacp - 0.5*DOT_PRODUCT(v,MATMUL(v,cell%bbmat))
                CALL vacuz(ev,vz(1,ivac),vz0(ivac),vacuum%nmz,vacuum%delz,t(ik),dt(ik),u(1,ik,jspin))
                CALL vacudz(ev,vz(1,ivac),vz0(ivac),vacuum%nmz,vacuum%delz,te(ik),&
                     &              dte(ik),tei(ik,jspin),ue(1,ik,jspin),dt(ik),&
                     &              u(1,ik,jspin))
                scale = wronk/ (te(ik)*dt(ik)-dte(ik)*t(ik))
                te(ik) = scale*te(ik)
                dte(ik) = scale*dte(ik)
                tei(ik,jspin) = scale*tei(ik,jspin)
                DO j = 1,vacuum%nmz
                   ue(j,ik,jspin) = scale*ue(j,ik,jspin)
                ENDDO
             ENDDO
             !     -----> construct a and b coefficients
             DO k = 1,lapw%nv(jspin)
                l = map2(k,jspin)
                zks = lapw%k3(k,jspin)*cell%bmat(3,3)*sign
                arg = zks*cell%z1
                c_1 = CMPLX(COS(arg),SIN(arg)) * const
                av = -c_1 * CMPLX( dte(l),zks*te(l) ) 
                bv =  c_1 * CMPLX(  dt(l),zks* t(l) ) 
                !     -----> loop over basis functions
468
                IF (zmat%l_real) THEN
469 470
                   ac(l,:ne,jspin) = ac(l,:ne,jspin) + zMat%z_r(k,:ne)*av
                   bc(l,:ne,jspin) = bc(l,:ne,jspin) + zMat%z_r(k,:ne)*bv
471
                ELSE
472 473
                   ac(l,:ne,jspin) = ac(l,:ne,jspin) + zMat%z_c(k,:ne)*av
                   bc(l,:ne,jspin) = bc(l,:ne,jspin) + zMat%z_c(k,:ne)*bv
474
                ENDIF
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
             ENDDO
          END IF ! D1
       ENDIF
       !
       !   ----> calculate first and second derivative of u,ue 
       !        in order to simulate p_z or d_z^2 Tip in Chen's model , shz. 97
       ! 
       IF (vacuum%nstm.GT.0) THEN
          DO  ik = 1,nv2(jspin)
             !               CALL rhzgrd(nmz,delz,u(1,ik,jspin),4,du,ddu(1,ik))
             !               CALL rhzgrd(nmz,delz,ue(1,ik,jspin),4,due,ddue(1,ik))   

             ALLOCATE ( dummy(vacuum%nmz) )
             CALL grdchlh(&
                  0,1,vacuum%nmz,vacuum%delz,dummy,u(1,ik,jspin),4,&
                  du,ddu(1,ik))
             CALL grdchlh(&
                  0,1,vacuum%nmz,vacuum%delz,dummy,ue(1,ik,jspin),4,&
                  due,ddue(1,ik))
             DEALLOCATE ( dummy )

             IF (vacuum%nstm.EQ.1) THEN
                u(:vacuum%nmz,ik,jspin)=du(:vacuum%nmz)
                ue(:vacuum%nmz,ik,jspin)=due(:vacuum%nmz)
             END IF
          ENDDO
       END IF

       !+dw

       !
       !       --> to calculate Tunneling Current between two systems
       !           within Bardeens Approach one needs ac(l,n), bc(l,n);
       !           they are written to the file vacwave 
       !                           IF nstm=3
       !                              tworkf is then the fermi energy (in hartree)
       !
       IF (vacuum%nstm.EQ.3) THEN
#ifdef CPP_MPI
514
          CALL judft_error("nstm==3 does not work in parallel",calledby="vacden")
515 516 517 518 519
#else
          i=0
          DO n = 1, ne
             IF (ABS(eig(n)-vacuum%tworkf).LE.emax) i=i+1
          END DO
520
          WRITE (87,FMT=990) lapw%bkpt(1),lapw%bkpt(2), i, n2max
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
          DO n = 1, ne
             IF (ABS(eig(n)-vacuum%tworkf).LE.emax) THEN
                WRITE (87,FMT=1000) eig(n)
                DO j=1,n2max
                   WRITE (87,FMT=1010) ac(mapg2k(j),n,jspin),&
                        bc(mapg2k(j),n,jspin)
                END DO
             END IF
          END DO
#endif



       END IF
990    FORMAT(2(f8.4,1x),i3,1x,i3)
1000   FORMAT(e10.4)
1010   FORMAT(2(2e20.8,1x))
       !
       !        ------------------------------------------------------------
       !-dw
       !
       !---->   non-warping part of the density (g=0 star)
       !
       IF (vacuum%nstm.EQ.2) THEN
          !
          !  ----> d_z^2-Tip needs: |d^2(psi)/dz^2 - kappa^2/3 psi|^2
          !
          DO l = 1,nv2(jspin)
             aa = 0.0
             bb = 0.0
             ba = 0.0
             ab = 0.0
             DO n = 1,ne
                aa = aa + we(n)*CONJG(ac(l,n,jspin))*ac(l,n,jspin)
                bb = bb + we(n)*CONJG(bc(l,n,jspin))*bc(l,n,jspin)
                ab = ab + we(n)*CONJG(ac(l,n,jspin))*bc(l,n,jspin)
                ba = ba + we(n)*CONJG(bc(l,n,jspin))*ac(l,n,jspin)
                qout = REAL(CONJG(ac(l,n,jspin))*ac(l,n,jspin)+&
                     &                 tei(l,jspin)*CONJG(bc(l,n,jspin))*bc(l,n,jspin))
                qvac(n,ivac,ikpt,jspin) = qvac(n,ivac,ikpt,jspin) + &
                     &                                                      qout*cell%area
             END DO
             aae=-aa*vacuum%tworkf*2/3
             bbe=-bb*vacuum%tworkf*2/3
             abe=-ab*vacuum%tworkf*2/3
             bae=-ba*vacuum%tworkf*2/3
             aaee=aa*vacuum%tworkf*vacuum%tworkf*4/9
             bbee=bb*vacuum%tworkf*vacuum%tworkf*4/9
             abee=ab*vacuum%tworkf*vacuum%tworkf*4/9
             baee=ba*vacuum%tworkf*vacuum%tworkf*4/9  
             DO  jz = 1,vacuum%nmz
                ui = u(jz,l,jspin)
                uei = ue(jz,l,jspin)
                ddui = ddu(jz,l) 
                dduei = ddue(jz,l)
Gregor Michalicek's avatar
Gregor Michalicek committed
576
                den%vacz(jz,ivac,jspin) = den%vacz(jz,ivac,jspin) +&
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
                     REAL(aaee*ui*ui+bbee*uei*uei+&
                     (abee+baee)*ui*uei+aa*ddui*ddui+&
                     bb*dduei*dduei+(ab+ba)*ddui*dduei+&
                     2*aae*ui*ddui+2*bbe*uei*dduei+&
                     (abe+bae)*(ui*dduei+uei*ddui))

             ENDDO
          END DO
          !
          !    -----> s-Tip: |psi|^2 and p-Tip: |d(psi)/dz|^2 
          !
       ELSE
          IF (noco%l_noco) THEN
             !--->          diagonal elements of the density matrix, n_11 and n_22
             !--->          the non-warping part of n_21 is calculated together with
             !--->          the warping part of n_21
             DO ispin = 1,input%jspins
                IF (oneD%odi%d1) THEN
                   DO  l = 1,nv2(ispin)
                      DO  m = -oneD%odi%mb,oneD%odi%mb
                         aa = CMPLX(0.0,0.0)
                         bb = CMPLX(0.0,0.0)
                         ba = CMPLX(0.0,0.0)
                         ab = CMPLX(0.0,0.0)
                         DO  n = 1,ne
                            aa = aa + we(n)*CONJG(ac_1(l,m,n,ispin))*ac_1(l,m,n,ispin)
                            bb = bb + we(n)*CONJG(bc_1(l,m,n,ispin))*bc_1(l,m,n,ispin)
                            ab = ab + we(n)*CONJG(ac_1(l,m,n,ispin))*bc_1(l,m,n,ispin)
                            ba = ba + we(n)*CONJG(bc_1(l,m,n,ispin))*ac_1(l,m,n,ispin)
                            qout = REAL(CONJG(ac_1(l,m,n,ispin))*ac_1(l,m,n,ispin) +&
                                 tei_1(l,m,ispin)*CONJG(bc_1(l,m,n,ispin))*&
                                 bc_1(l,m,n,ispin))
                            qvac(n,ivac,ikpt,ispin) = qvac(n,ivac,ikpt,ispin)+qout*cell%area
                         END DO
                         DO  jz = 1,vacuum%nmz
                            ui = u_1(jz,l,m,ispin)
                            uei = ue_1(jz,l,m,ispin)
Gregor Michalicek's avatar
Gregor Michalicek committed
614
                            den%vacz(jz,ivac,ispin) = den%vacz(jz,ivac,ispin) +REAL(aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
                         END DO
                      END DO
                   END DO
                ELSE 
                   DO l = 1,nv2(ispin)
                      aa = 0.0
                      bb = 0.0
                      ba = 0.0
                      ab = 0.0
                      DO n = 1,ne
                         aa=aa + we(n)*CONJG(ac(l,n,ispin))*ac(l,n,ispin)
                         bb=bb + we(n)*CONJG(bc(l,n,ispin))*bc(l,n,ispin)
                         ab=ab + we(n)*CONJG(ac(l,n,ispin))*bc(l,n,ispin)
                         ba=ba + we(n)*CONJG(bc(l,n,ispin))*ac(l,n,ispin)
                         qout = REAL(CONJG(ac(l,n,ispin))*ac(l,n,ispin)+tei(l,ispin)*CONJG(bc(l,n,ispin))*bc(l,n,ispin))
                         qvac(n,ivac,ikpt,ispin) = qvac(n,ivac,ikpt,ispin) + qout*cell%area
                      END DO
                      DO jz = 1,vacuum%nmz
                         ui = u(jz,l,ispin)
                         uei = ue(jz,l,ispin)
Gregor Michalicek's avatar
Gregor Michalicek committed
635
                         den%vacz(jz,ivac,ispin) = den%vacz(jz,ivac,ispin) +REAL(aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
                      ENDDO
                   ENDDO
                END IF ! one-dimensional
             ENDDO
          ELSE
             IF (oneD%odi%d1) THEN
                DO  l = 1,nv2(jspin)
                   DO  m = -oneD%odi%mb,oneD%odi%mb
                      aa = CMPLX(0.0,0.0)
                      bb = CMPLX(0.0,0.0)
                      ba = CMPLX(0.0,0.0)
                      ab = CMPLX(0.0,0.0)
                      DO  n = 1,ne
                         aa = aa + we(n)*CONJG(ac_1(l,m,n,jspin))*ac_1(l,m,n,jspin)
                         bb = bb + we(n)*CONJG(bc_1(l,m,n,jspin))*bc_1(l,m,n,jspin)
                         ab = ab + we(n)*CONJG(ac_1(l,m,n,jspin))*bc_1(l,m,n,jspin)
                         ba = ba + we(n)*CONJG(bc_1(l,m,n,jspin))*ac_1(l,m,n,jspin)
                         qout = REAL(CONJG(ac_1(l,m,n,jspin))*ac_1(l,m,n,jspin)+&
                              tei_1(l,m,jspin)*CONJG(bc_1(l,m,n,jspin))*bc_1(l,m,n,jspin))
                         qvac(n,ivac,ikpt,jspin) = qvac(n,ivac,ikpt,jspin)+qout*cell%area
                      END DO
                      DO  jz = 1,vacuum%nmz
                         ui = u_1(jz,l,m,jspin)
                         uei = ue_1(jz,l,m,jspin)
Gregor Michalicek's avatar
Gregor Michalicek committed
660
                         den%vacz(jz,ivac,jspin) = den%vacz(jz,ivac,jspin) +REAL(aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
                      END DO
                   END DO
                END DO
             ELSE          ! D1
                DO l = 1,nv2(jspin)
                   aa = CMPLX(0.0,0.0)
                   bb = CMPLX(0.0,0.0)
                   ba = CMPLX(0.0,0.0)
                   ab = CMPLX(0.0,0.0)
                   DO n = 1,ne
                      aa = aa + we(n)*CONJG(ac(l,n,jspin))*ac(l,n,jspin)
                      bb = bb + we(n)*CONJG(bc(l,n,jspin))*bc(l,n,jspin)
                      ab = ab + we(n)*CONJG(ac(l,n,jspin))*bc(l,n,jspin)
                      ba = ba + we(n)*CONJG(bc(l,n,jspin))*ac(l,n,jspin)
                      qout = REAL(CONJG(ac(l,n,jspin))*ac(l,n,jspin)+tei(l,jspin)*CONJG(bc(l,n,jspin))*bc(l,n,jspin))
                      qvac(n,ivac,ikpt,jspin) = qvac(n,ivac,ikpt,jspin) + qout*cell%area
                   END DO
                   DO  jz = 1,vacuum%nmz
                      ui = u(jz,l,jspin)
                      uei = ue(jz,l,jspin)
Gregor Michalicek's avatar
Gregor Michalicek committed
681
                      den%vacz(jz,ivac,jspin) = den%vacz(jz,ivac,jspin) +REAL(aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
                   ENDDO
                END DO
             END IF ! D1
          ENDIF
       END IF
       !
       !     ****************** change for vacuum density of states shz Jan.96 ***
       !
       IF (banddos%vacdos) THEN                       
          !
          !  ----> d_z^2-Tip needs: |d^2(psi)/dz^2 - kappa^2/3 psi|^2
          !
          IF (vacuum%nstm.EQ.2) THEN
             DO l=1,nv2(jspin)
                DO n = 1,ne
                   aa = CONJG(ac(l,n,jspin))*ac(l,n,jspin)
                   bb = CONJG(bc(l,n,jspin))*bc(l,n,jspin)
                   ab = CONJG(ac(l,n,jspin))*bc(l,n,jspin)
                   ba = CONJG(bc(l,n,jspin))*ac(l,n,jspin)
                   aae = -vacuum%tworkf*aa*2/3
                   bbe = -vacuum%tworkf*bb*2/3
                   abe = -vacuum%tworkf*ab*2/3
                   bae = -vacuum%tworkf*ba*2/3
                   aaee = aa*vacuum%tworkf*vacuum%tworkf*4/9
                   bbee = bb*vacuum%tworkf*vacuum%tworkf*4/9
                   abee = ab*vacuum%tworkf*vacuum%tworkf*4/9
                   baee = ba*vacuum%tworkf*vacuum%tworkf*4/9
                   DO jj = 1,vacuum%layers
                      !
                      !     ----> either integrated LDOS(z1,z2) or LDOS(z1)
                      !     
                      IF (input%integ) THEN
                         ll = 1
                         DO ii = vacuum%izlay(jj,1),vacuum%izlay(jj,2)
                            ui = u(ii,l,jspin)
                            uei = ue(ii,l,jspin)
                            ddui = ddu(ii,l)
                            dduei = ddue(ii,l)
                            yy(ll) = REAL(aaee*ui*ui+bbee*uei*uei+(abee+baee)*ui*uei+aa*ddui*ddui+bb*&
                                 dduei*dduei+(ab+ba)*ddui*dduei+2*aae*ui*ddui+2*bbe*uei*dduei+&
                                 (abe+bae)*(ui*dduei+uei*ddui))*cell%area
                            ll = ll+1
                         END DO
                         CALL qsf(vacuum%delz,yy,RESULT,ll-1,0)
                         qvlay(n,jj,ivac,ikpt,jspin) =&
                              &                           qvlay(n,jj,ivac,ikpt,jspin) + RESULT(1)
                      ELSE
                         ui = u(vacuum%izlay(jj,1),l,jspin)       
                         uei = ue(vacuum%izlay(jj,1),l,jspin)
                         ddui = ddu(vacuum%izlay(jj,1),l)
                         dduei = ddue(vacuum%izlay(jj,1),l)
                         yy(1) = REAL(aaee*ui*ui+bbee*uei*uei+&
                              (abee+baee)*ui*uei+aa*ddui*ddui+&
                              bb*dduei*dduei+(ab+ba)*ddui*dduei+&
                              2*aae*ui*ddui+2*bbe*uei*dduei+&
                              (abe+bae)*(ui*dduei+uei*ddui))
                         qvlay(n,jj,ivac,ikpt,jspin) = qvlay(n,jj,ivac,ikpt,jspin) +yy (1)
                      END IF
                   END DO
                END DO
             END DO
             !     
             !     ----> s-Tip = calculate LDOS and(!) p_z-Tip (since u->du/dz, ue->due/dz)
             !     
          ELSE 
             IF (ABS(vacuum%locx(1)-vacuum%locx(2)).LE.eps) THEN
                !     
                !     ----> integrated over 2D-unit cell
                !
                IF (noco%l_noco) THEN
                   isp_start = 1
                   isp_end   = input%jspins
                ELSE
                   isp_start = jspin
                   isp_end   = jspin
                ENDIF
                DO ispin = isp_start, isp_end
                   DO l=1,nv2(ispin)
                      DO n = 1,ne
                         aa = CONJG(ac(l,n,ispin))*ac(l,n,ispin)
                         bb = CONJG(bc(l,n,ispin))*bc(l,n,ispin)
                         ab = CONJG(ac(l,n,ispin))*bc(l,n,ispin)
                         ba = CONJG(bc(l,n,ispin))*ac(l,n,ispin) 
                         DO jj = 1,vacuum%layers
                            !     
                            !     ---> either integrated (z1,z2) or slice (z1)
                            !     
                            IF (input%integ) THEN
                               ll = 1
                               DO ii = vacuum%izlay(jj,1),vacuum%izlay(jj,2)
                                  ui = u(ii,l,ispin)
                                  uei = ue(ii,l,ispin)
                                  yy(ll) = REAL(aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)
                                  ll = ll+1
                               END DO
                               CALL qsf(vacuum%delz,yy,RESULT,ll-1,0)
                               qvlay(n,jj,ivac,ikpt,ispin) = qvlay(n,jj,ivac,ikpt,ispin) + RESULT(1)
                            ELSE
                               ui = u(vacuum%izlay(jj,1),l,ispin)       
                               uei = ue(vacuum%izlay(jj,1),l,ispin)
                               qvlay(n,jj,ivac,ikpt,ispin) =qvlay(n,jj,ivac,ikpt,ispin) + REAL(&
                                    aa*ui*ui+bb*uei*uei+(ab+ba)*ui*uei)

                            END IF
                         END DO
                      END DO
                   END DO
                ENDDO
             ELSE
                !     
                !     ----> if LDOS should be calculated over restricted area of the 2D-unit cell
                !     lower left corner: (locx(1), locy(1))   }  in internal
                !     upper right corner: (locx(2), locy(2))  }  coordinates
                !     
                DO l=1, nv2(jspin)
                   DO l1=1, nv2(jspin)
                      IF (kvac1(l,jspin).EQ.kvac1(l1,jspin)) THEN
                         factorx = CMPLX((vacuum%locx(2)-vacuum%locx(1)), 0.)
                      ELSE
                         k_diff=tpi_const*(kvac1(l,jspin)-kvac1(l1,jspin))
                         k_d1 = k_diff*vacuum%locx(1)
                         k_d2 = k_diff*vacuum%locx(2)
                         factorx=( CMPLX( COS(k_d2), SIN(k_d2)) -&
                              CMPLX( COS(k_d1), SIN(k_d1)) ) /&
                              CMPLX( 0.,k_diff )
                      END IF
                      IF (kvac2(l,jspin).EQ.kvac2(l1,jspin)) THEN
                         factory = CMPLX((vacuum%locy(2)-vacuum%locy(1)), 0.)
                      ELSE
                         k_diff=tpi_const*(kvac2(l,jspin)-kvac2(l1,jspin))
                         k_d1 = k_diff*vacuum%locy(1)
                         k_d2 = k_diff*vacuum%locy(2)
                         factory=( CMPLX( COS(k_d2), SIN(k_d2)) -&
                              CMPLX( COS(k_d1), SIN(k_d1)) ) /&
                              CMPLX( 0.,k_diff )
                      END IF
                      DO n=1, ne
                         aa = CONJG(ac(l1,n,jspin))*ac(l,n,jspin)
                         bb = CONJG(bc(l1,n,jspin))*bc(l,n,jspin)
                         ab = CONJG(ac(l1,n,jspin))*bc(l,n,jspin)
                         ba = CONJG(bc(l1,n,jspin))*ac(l,n,jspin)   
                         DO jj = 1,vacuum%layers
                            !     
                            !     ---> either integrated (z1,z2) or slice (z1)
                            !     
                            IF (input%integ) THEN
                               ll = 1
                               DO ii = vacuum%izlay(jj,1), vacuum%izlay(jj,2)
                                  ui = u(ii,l,jspin)
                                  uei = ue(ii,l,jspin)
                                  uj = u(ii,l1,jspin)
                                  uej = ue(ii,l1,jspin)
                                  yy(ll) = REAL((aa*ui*uj+bb*uei*uej+ab*uei*uj+ba*ui*uej)*factorx*factory)
                                  ll = ll+1
                               END DO
                               CALL qsf(vacuum%delz,yy,RESULT,ll-1,0)
                               qvlay(n,jj,ivac,ikpt,jspin) = qvlay(n,jj,ivac,ikpt,jspin) + RESULT(1)
                            ELSE
                               ui = u(vacuum%izlay(jj,1),l,jspin)       
                               uei = ue(vacuum%izlay(jj,1),l,jspin)
                               uj = u(vacuum%izlay(jj,1),l1,jspin)
                               uej = ue(vacuum%izlay(jj,1),l1,jspin)
                               qvlay(n,jj,ivac,ikpt,jspin) = REAL((aa*ui*uj + bb*uei*uej+ab*uei*uj+ba*ui**uej)*factorx*factory)
                            END IF
                         END DO
                      END DO
                   END DO
                END DO
             END IF
          END IF
       END IF

       !
       !     **********************************************************************
       !
       !--->    warping part of the density (g.ne.0 stars)
       !
       !   ---> d_z^2-Tip
       !
       IF (vacuum%nstm.EQ.2) THEN
          DO l = 1,nv2(jspin)
             DO  l1 = 1,l - 1
                i1 = kvac1(l,jspin) - kvac1(l1,jspin)
                i2 = kvac2(l,jspin) - kvac2(l1,jspin)
                i3 = 0
                IF (iabs(i1).GT.stars%mx1) CYCLE
                IF (iabs(i2).GT.stars%mx2) CYCLE
                ig3 = stars%ig(i1,i2,i3)
                IF (ig3.EQ.0)  CYCLE
                phs = stars%rgphs(i1,i2,i3)
                ig3p = stars%ig(-i1,-i2,i3)
                phsp = stars%rgphs(-i1,-i2,i3)
                ind2 = stars%ig2(ig3)
                ind2p = stars%ig2(ig3p)
                aa = 0.0
                bb = 0.0
                ba = 0.0
                ab = 0.0
                DO n = 1,ne
                   aa = aa + we(n)*CONJG(ac(l1,n,jspin))*ac(l,n,jspin)
                   bb = bb + we(n)*CONJG(bc(l1,n,jspin))*bc(l,n,jspin)
                   ab = ab + we(n)*CONJG(ac(l1,n,jspin))*bc(l,n,jspin)
                   ba = ba + we(n)*CONJG(bc(l1,n,jspin))*ac(l,n,jspin)
                END DO
                aae=-aa*2/3*vacuum%tworkf
                bbe=-bb*2/3*vacuum%tworkf
                abe=-ab*2/3*vacuum%tworkf
                bae=-ba*2/3*vacuum%tworkf
                aaee=aa*4/9*vacuum%tworkf*vacuum%tworkf
                bbee=bb*4/9*vacuum%tworkf*vacuum%tworkf
                abee=ab*4/9*vacuum%tworkf*vacuum%tworkf
                baee=ba*4/9*vacuum%tworkf*vacuum%tworkf
                DO  jz = 1,vacuum%nmzxy
                   ui = u(jz,l,jspin)
                   uj = u(jz,l1,jspin)
                   ddui = ddu(jz,l)
                   dduj = ddu(jz,l1)
                   uei = ue(jz,l,jspin)
                   uej = ue(jz,l1,jspin)
                   dduei = ddue(jz,l)
                   dduej = ddue(jz,l1)
                   t1=aaee*ui*uj+bbee*uei*uej+baee*ui*uej+abee*uei*uj&
                        + aae*(ui*dduj+uj*ddui)+bbe*(uei*dduej+uej*dduei)&
                        + abe*(ui*dduej+uj*dduei)+bae*(ddui*uej+dduj*uei)&
                        + aa*ddui*dduj+bb*dduei*dduej+ba*ddui*dduej&
                        + ab*dduei*dduj  
Gregor Michalicek's avatar
Gregor Michalicek committed
908 909
                   den%vacxy(jz,ind2-1,ivac,jspin) = den%vacxy(jz,ind2-1,ivac,jspin) + t1*phs/stars%nstr2(ind2)
                   den%vacxy(jz,ind2p-1,ivac,jspin)= den%vacxy(jz,ind2p-1,ivac,jspin) + CONJG(t1)*phsp/stars%nstr2(ind2p)
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
                ENDDO
             ENDDO
          END DO
          !
          ! ---> s-Tip and p_z-Tip
          !
       ELSE
          !=============================================================
          !           continuation of vacden....
          !=============================================================
          IF (noco%l_noco) THEN
             !--->       diagonal elements of the density matrix, n_11 and n_22
             DO ispin = 1,input%jspins
                IF (oneD%odi%d1) THEN
                   DO l = 1,nv2(ispin)
                      DO m = -oneD%odi%mb,oneD%odi%mb
                         lprimee: DO l1 = 1,l-1
                            mprimee: DO m1 = -oneD%odi%mb,m-1
                               i3 = kvac3(l,ispin) - kvac3(l1,ispin)
                               m3 = m-m1
                               IF (m3.EQ.0 .AND. i3.EQ.0) CYCLE mprimee
                               IF (iabs(m3).GT.oneD%odi%M) CYCLE mprimee
932
                               IF (iabs(i3).GT.stars%mx3) CYCLE lprimee
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
                               ind1 = oneD%odi%ig(i3,m3)
                               ind1p = oneD%odi%ig(-i3,-m3)
                               IF (ind1.NE.0 .OR. ind1p.NE.0) THEN
                                  aa = CMPLX(0.,0.)
                                  bb = CMPLX(0.,0.)
                                  ba = CMPLX(0.,0.)
                                  ab = CMPLX(0.,0.)
                                  DO n = 1,ne 
                                     aa=aa+we(n)*CONJG(ac_1(l1,m1,n,ispin))*ac_1(l,m,n,ispin)
                                     bb=bb+we(n)*CONJG(bc_1(l1,m1,n,ispin))*bc_1(l,m,n,ispin)
                                     ab=ab+we(n)*CONJG(ac_1(l1,m1,n,ispin))*bc_1(l,m,n,ispin)
                                     ba=ba+we(n)*CONJG(bc_1(l1,m1,n,ispin))*ac_1(l,m,n,ispin)
                                  END DO
                                  xys1: DO jz = 1,vacuum%nmzxy
                                     ui = u_1(jz,l,m,ispin)
                                     uj = u_1(jz,l1,m1,ispin)
                                     uei = ue_1(jz,l,m,ispin)
                                     uej = ue_1(jz,l1,m1,ispin)
                                     t1 = aa*ui*uj + bb*uei*uej + ba*ui*uej + ab*uei*uj
                                     IF (ind1.NE.0) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
953
                                        den%vacxy(jz,ind1-1,ivac,ispin) = den%vacxy(jz,ind1-1,ivac,ispin) + t1/ oneD%odi%nst2(ind1)
954 955
                                     END IF
                                     IF (ind1p.NE.0) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
956
                                        den%vacxy(jz,ind1p-1,ivac,ispin) = den%vacxy(jz,ind1p-1,ivac,ispin) + CONJG(t1)/ oneD%odi%nst2(ind1p)
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
                                     END IF

                                  END DO xys1
                               END IF   ! ind1 and ind1p =0
                            END DO mprimee
                         END DO lprimee
                      END DO  ! m
                   END DO   ! l
                ELSE
                   DO l = 1,nv2(ispin)
                      DO  l1 = 1,l - 1
                         i1 = kvac1(l,ispin) - kvac1(l1,ispin)
                         i2 = kvac2(l,ispin) - kvac2(l1,ispin)
                         i3 = 0
                         IF (iabs(i1).GT.stars%mx1) CYCLE
                         IF (iabs(i2).GT.stars%mx2) CYCLE
                         ig3 = stars%ig(i1,i2,i3)
                         IF (ig3.EQ.0)  CYCLE
                         phs = stars%rgphs(i1,i2,i3)
                         ig3p = stars%ig(-i1,-i2,i3)
                         phsp = stars%rgphs(-i1,-i2,i3)
                         ind2 = stars%ig2(ig3)
                         ind2p = stars%ig2(ig3p)
                         aa = 0.0
                         bb = 0.0
                         ba = 0.0
                         ab = 0.0
                         DO n = 1,ne
                            aa=aa+we(n)*CONJG(ac(l1,n,ispin))*ac(l,n,ispin)
                            bb=bb+we(n)*CONJG(bc(l1,n,ispin))*bc(l,n,ispin)
                            ab=ab+we(n)*CONJG(ac(l1,n,ispin))*bc(l,n,ispin)
                            ba=ba+we(n)*CONJG(bc(l1,n,ispin))*ac(l,n,ispin)
                         END DO
                         DO jz = 1,vacuum%nmzxy
                            ui = u(jz,l,ispin)
                            uj = u(jz,l1,ispin)
                            uei = ue(jz,l,ispin)
                            uej = ue(jz,l1,ispin)
                            t1 = aa*ui*uj+bb*uei*uej+ba*ui*uej+ab*uei*uj
Gregor Michalicek's avatar
Gregor Michalicek committed
996 997
                            den%vacxy(jz,ind2-1,ivac,ispin) = den%vacxy(jz,ind2-1,ivac,ispin) + t1*phs/stars%nstr2(ind2)
                            den%vacxy(jz,ind2p-1,ivac,ispin) = den%vacxy(jz,ind2p-1,ivac,ispin) + CONJG(t1)*phsp/stars%nstr2(ind2p)
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
                         ENDDO
                      ENDDO
                   ENDDO
                END IF ! oneD%odi%d1
             END DO
             !--->          off-diagonal element of the density matrix, n_21
             IF (oneD%odi%d1) THEN
                DO l = 1,nv2(1)
                   DO m = -oneD%odi%mb,oneD%odi%mb
                      lprimea: DO l1 = 1,nv2(2)
                         mprimea: DO m1 = -oneD%odi%mb,oneD%odi%mb
                            i3 = kvac3(l,1) - kvac3(l1,2)
                            m3 = m-m1
                            IF (iabs(m3).GT.oneD%odi%M) CYCLE mprimea
1012
                            IF (iabs(i3).GT.stars%mx3) CYCLE lprimea
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                            ind1 = oneD%odi%ig(i3,m3)
                            IF (ind1.NE.0) THEN
                               IF (m3.EQ.0 .AND. i3.EQ.0) THEN
                                  aa = CMPLX(0.,0.)
                                  bb = CMPLX(0.,0.)
                                  ba = CMPLX(0.,0.)
                                  ab = CMPLX(0.,0.)
                                  DO n = 1,ne
                                     aa=aa+we(n)*CONJG(ac_1(l1,m1,n,2))* ac_1(l,m,n,1)
                                     bb=bb+we(n)*CONJG(bc_1(l1,m1,n,2))* bc_1(l,m,n,1)
                                     ab=ab+we(n)*CONJG(ac_1(l1,m1,n,2))* bc_1(l,m,n,1)
                                     ba=ba+we(n)*CONJG(bc_1(l1,m1,n,2))* ac_1(l,m,n,1)
                                  END DO
                                  xys3: DO jz = 1,vacuum%nmzxy
                                     ui = u_1(jz,l,m,1)
                                     uj = u_1(jz,l1,m1,2)
                                     uei = ue_1(jz,l,m,1)
                                     uej = ue_1(jz,l1,m1,2)
Gregor Michalicek's avatar
Gregor Michalicek committed
1031 1032 1033
                                     tempCmplx = aa*ui*uj+bb*uei*uej+ba*ui*uej+ab*uei*uj
                                     den%vacz(jz,ivac,3) = den%vacz(jz,ivac,3) + REAL(tempCmplx)
                                     den%vacz(jz,ivac,4) = den%vacz(jz,ivac,4) + AIMAG(tempCmplx)
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
                                  END DO xys3
                               ELSE ! the warped part (ind1 > 1)
                                  aa = CMPLX(0.,0.)
                                  bb = CMPLX(0.,0.)
                                  ba = CMPLX(0.,0.)
                                  ab = CMPLX(0.,0.)
                                  DO n = 1,ne
                                     aa=aa+we(n)*CONJG(ac_1(l1,m1,n,2))* ac_1(l,m,n,1)
                                     bb=bb+we(n)*CONJG(bc_1(l1,m1,n,2))* bc_1(l,m,n,1)
                                     ab=ab+we(n)*CONJG(ac_1(l1,m1,n,2))* bc_1(l,m,n,1)
                                     ba=ba+we(n)*CONJG(bc_1(l1,m1,n,2))* ac_1(l,m,n,1)
                                  END DO
                                  xys2: DO jz = 1,vacuum%nmzxy
                                     ui = u_1(jz,l,m,1)
                                     uj = u_1(jz,l1,m1,2)
                                     uei = ue_1(jz,l,m,1)
                                     uej = ue_1(jz,l1,m1,2)
                                     t1 = aa*ui*uj+bb*uei*uej+ba*ui*uej+ab*uei*uj
Gregor Michalicek's avatar
Gregor Michalicek committed
1052
                                     den%vacxy(jz,ind1-1,ivac,3) = den%vacxy(jz,ind1-1,ivac,3) + t1/oneD%odi%nst2(ind1)
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                                  END DO xys2
                               END IF  ! the non-warped (ind1 = 1)
                            END IF   ! ind1.ne.0
                         END DO mprimea
                      END DO lprimea
                   END DO  ! m
                END DO   ! l
             ELSE ! oneD%odi%d1
                DO l = 1,nv2(1)
                   DO  l1 = 1,nv2(2)
                      i1 = kvac1(l,1) - kvac1(l1,2)
                      i2 = kvac2(l,1) - kvac2(l1,2)
                      i3 = 0
                      !--->                treat only the warping part
                      IF (iabs(i1).GT.stars%mx1) CYCLE
                      IF (iabs(i2).GT.stars%mx2) CYCLE
                      ig3 = stars%ig(i1,i2,i3)
                      IF (ig3.EQ.0)  CYCLE
                      phs = stars%rgphs(i1,i2,i3)
                      ind2 = stars%ig2(ig3)
                      IF ( ind2.EQ.1) THEN
                         !--->                non-warping part (1st star G=0)
                         aa = 0.0
                         bb = 0.0
                         ba = 0.0
                         ab = 0.0
                         DO ie = 1,ne
                            aa=aa+we(ie)*CONJG(ac(l1,ie,2))*ac(l,ie,1)
                            bb=bb+we(ie)*CONJG(bc(l1,ie,2))*bc(l,ie,1)
                            ab=ab+we(ie)*CONJG(ac(l1,ie,2))*bc(l,ie,1)
                            ba=ba+we(ie)*CONJG(bc(l1,ie,2))*ac(l,ie,1)
                         END DO
                         DO jz = 1,vacuum%nmz
                            ui = u(jz,l,1)
                            ui2 = u(jz,l1,2)
                            uei = ue(jz,l,1)
                            uei2 = ue(jz,l1,2)
Gregor Michalicek's avatar
Gregor Michalicek committed
1090 1091 1092
                            tempCmplx = aa*ui2*ui + bb*uei2*uei + ab*ui2*uei + ba*uei2*ui
                            den%vacz(jz,ivac,3) = den%vacz(jz,ivac,3) + REAL(tempCmplx)
                            den%vacz(jz,ivac,4) = den%vacz(jz,ivac,4) + AIMAG(tempCmplx)
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
                         ENDDO
                      ELSE
                         !--->                warping part
                         aa = 0.0
                         bb = 0.0
                         ba = 0.0
                         ab = 0.0
                         DO ie = 1,ne
                            aa=aa + we(ie)*CONJG(ac(l1,ie,2))*ac(l,ie,1)
                            bb=bb + we(ie)*CONJG(bc(l1,ie,2))*bc(l,ie,1)
                            ab=ab + we(ie)*CONJG(ac(l1,ie,2))*bc(l,ie,1)
                            ba=ba + we(ie)*CONJG(bc(l1,ie,2))*ac(l,ie,1)
                         END DO
                         DO  jz = 1,vacuum%nmzxy
                            ui = u(jz,l,1)
                            uj = u(jz,l1,2)
                            uei = ue(jz,l,1)
                            uej = ue(jz,l1,2)
                            t1 = aa*ui*uj+bb*uei*uej+ba*ui*uej+ab*uei*uj
Gregor Michalicek's avatar
Gregor Michalicek committed
1112
                            den%vacxy(jz,ind2-1,ivac,3) = den%vacxy(jz, ind2-1,ivac,3) + t1*phs/stars%nstr2(ind2)
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
                         ENDDO
                      ENDIF
                   ENDDO
                END DO
             END IF ! oneD%odi%d1
          ELSE                                ! collinear part

             IF (oneD%odi%d1) THEN
                DO l = 1,nv2(jspin)
                   DO m = -oneD%odi%mb,oneD%odi%mb
                      lprime: DO l1 = 1,l-1
                         mprime: DO m1 = -oneD%odi%mb,m-1
                            i3 = kvac3(l,jspin) - kvac3(l1,jspin)
                            m3 = m-m1
                            IF (m3.EQ.0 .AND. i3.EQ.0) CYCLE mprime
                            IF (iabs(m3).GT.oneD%odi%M) CYCLE mprime
1129
                            IF (iabs(i3).GT.stars%mx3) CYCLE lprime
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
                            ind1 = oneD%odi%ig(i3,m3)
                            ind1p = oneD%odi%ig(-i3,-m3)
                            IF (ind1.NE.0 .OR. ind1p.NE.0) THEN
                               aa = CMPLX(0.,0.)
                               bb = CMPLX(0.,0.)
                               ba = CMPLX(0.,0.)
                               ab = CMPLX(0.,0.)
                               DO n = 1,ne
                                  aa=aa+we(n)*CONJG(ac_1(l1,m1,n,jspin))* ac_1(l,m,n,jspin)
                                  bb=bb+we(n)*CONJG(bc_1(l1,m1,n,jspin))* bc_1(l,m,n,jspin)
                                  ab=ab+we(n)*CONJG(ac_1(l1,m1,n,jspin))* bc_1(l,m,n,jspin)
                                  ba=ba+we(n)*CONJG(bc_1(l1,m1,n,jspin))* ac_1(l,m,n,jspin)
                               END DO
                               xys: DO jz = 1,vacuum%nmzxy
                                  ui = u_1(jz,l,m,jspin)
                                  uj = u_1(jz,l1,m1,jspin)
                                  uei = ue_1(jz,l,m,jspin)
                                  uej = ue_1(jz,l1,m1,jspin)
                                  t1 = aa*ui*uj + bb*uei*uej + ba*ui*uej + ab*uei*uj
                                  IF (ind1.NE.0) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
1150
                                     den%vacxy(jz,ind1-1,ivac,jspin) = den%vacxy(jz,ind1-1,ivac,jspin) + t1/ oneD%odi%nst2(ind1)
1151 1152
                                  END IF
                                  IF (ind1p.NE.0) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
1153
                                     den%vacxy(jz,ind1p-1,ivac,jspin) = den%vacxy(jz,ind1p-1,ivac,jspin) + CONJG(t1)/ oneD%odi%nst2(ind1p)
1154 1155 1156 1157 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 1193 1194
                                  END IF

                               END DO xys
                            END IF   ! ind1 and ind1p =0
                         END DO mprime
                      END DO lprime
                   END DO  ! m
                END DO   ! l

             ELSE         !D1

                DO l = 1,nv2(jspin)
                   DO  l1 = 1,l - 1
                      i1 = kvac1(l,jspin) - kvac1(l1,jspin)
                      i2 = kvac2(l,jspin) - kvac2(l1,jspin)
                      i3 = 0
                      IF (iabs(i1).GT.stars%mx1) CYCLE
                      IF (iabs(i2).GT.stars%mx2) CYCLE
                      ig3 = stars%ig(i1,i2,i3)
                      IF (ig3.EQ.0)  CYCLE
                      phs = stars%rgphs(i1,i2,i3)
                      ig3p = stars%ig(-i1,-i2,i3)
                      phsp = stars%rgphs(-i1,-i2,i3)
                      ind2 = stars%ig2(ig3)
                      ind2p = stars%ig2(ig3p)
                      aa = 0.0
                      bb = 0.0
                      ba = 0.0
                      ab = 0.0
                      DO n = 1,ne
                         aa=aa+we(n)*CONJG(ac(l1,n,jspin))*ac(l,n,jspin)
                         bb=bb+we(n)*CONJG(bc(l1,n,jspin))*bc(l,n,jspin)
                         ab=ab+we(n)*CONJG(ac(l1,n,jspin))*bc(l,n,jspin)
                         ba=ba+we(n)*CONJG(bc(l1,n,jspin))*ac(l,n,jspin)
                      END DO
                      DO  jz = 1,vacuum%nmzxy
                         ui = u(jz,l,jspin)
                         uj = u(jz,l1,jspin)
                         uei = ue(jz,l,jspin)
                         uej = ue(jz,l1,jspin)
                         t1 = aa*ui*uj+bb*uei*uej+ba*ui*uej+ab*uei*uj
Gregor Michalicek's avatar
Gregor Michalicek committed
1195 1196
                         den%vacxy(jz,ind2-1,ivac,jspin) = den%vacxy(jz,ind2-1, ivac,jspin) + t1*phs/stars%nstr2(ind2)
                         den%vacxy(jz,ind2p-1,ivac,jspin) = den%vacxy(jz,ind2p-1, ivac,jspin) + CONJG(t1)*phsp/stars%nstr2(ind2p)
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
                      ENDDO
                   ENDDO
                END DO
             END IF ! D1
          ENDIF
       END IF
       !=============================================================
       !
       !       calculate 1. to nstars. starcoefficient for each k and energy eigenvalue 
       !           to stcoeff(ne,layer,ivac) if starcoeff=T (the star coefficient values are written to vacdos)
       !
       IF (vacuum%starcoeff .AND. banddos%vacdos) THEN
          DO  n=1,ne
             DO l = 1,nv2(jspin)
                DO  l1 = 1,l - 1
                   i1 = kvac1(l,jspin) - kvac1(l1,jspin)
                   i2 = kvac2(l,jspin) - kvac2(l1,jspin)
                   i3 = 0
                   IF (iabs(i1).GT.stars%mx1) CYCLE
                   IF (iabs(i2).GT.stars%mx2) CYCLE
                   ig3 = stars%ig(i1,i2,i3)
                   IF (ig3.EQ.0)  CYCLE
                   ind2 = stars%ig2(ig3)
                   ig3p = stars%ig(-i1,-i2,i3)
                   ind2p = stars%ig2(ig3p)
                   IF ((ind2.GE.2.AND.ind2.LE.vacuum%nstars).OR.&
                        (ind2p.GE.2.AND.ind2p.LE.vacuum%nstars)) THEN
                      phs = stars%rgphs(i1,i2,i3)
                      phsp = stars%rgphs(-i1,-i2,i3)
                      aa = CONJG(ac(l1,n,jspin))*ac(l,n,jspin)
                      bb = CONJG(bc(l1,n,jspin))*bc(l,n,jspin)
                      ab = CONJG(ac(l1,n,jspin))*bc(l,n,jspin)
                      ba = CONJG(bc(l1,n,jspin))*ac(l,n,jspin)
                      DO jj = 1,vacuum%layers
                         ui = u(vacuum%izlay(jj,1),l,jspin)
                         uj = u(vacuum%izlay(jj,1),l1,jspin)
                         uei = ue(vacuum%izlay(jj,1),l,jspin)
                         uej = ue(vacuum%izlay(jj,1),l1,jspin)
                         t1 = aa*ui*uj + bb*uei*uej +ba*ui*uej + ab*uei*uj
                         IF (ind2.GE.2.AND.ind2.LE.vacuum%nstars) &
                              stcoeff(ind2-1,n,jj,ivac) = stcoeff(ind2-1,n,jj,ivac)+ t1*phs/stars%nstr2(ind2)
                         IF (ind2p.GE.2.AND.ind2p.LE.vacuum%nstars) &
                              stcoeff(ind2p-1,n,jj,ivac) = stcoeff(ind2p-1,n,jj,ivac) +CONJG(t1)*phs/stars%nstr2(ind2p)
                      END DO
                   END IF
                ENDDO
             END DO
          ENDDO
       END IF
    ENDDO
    IF (noco%l_noco) CLOSE (25)
    DEALLOCATE (ac,bc,dt,dte,du,ddu,due,ddue,t,te,tei,u,ue,v,yy )

    IF (oneD%odi%d1) THEN
       DEALLOCATE (ac_1,bc_1,dt_1,dte_1,du_1,ddu_1,due_1,ddue_1)
       DEALLOCATE (t_1,te_1,tei_1,u_1,ue_1)
    END IF ! oneD%odi%d1

  END SUBROUTINE vacden
END MODULE m_vacden