evaldos.f90 19.9 KB
Newer Older
1 2
      MODULE m_evaldos
      CONTAINS
3 4
      SUBROUTINE evaldos(eig_id,input,banddos,vacuum,kpts,atoms,sym,noco,oneD,cell,results,dos,&
                         dimension,efermiarg,bandgap,l_mcd,ncore,e_mcd,nsld)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
!----------------------------------------------------------------------
!
!     vk: k-vectors
!     wk weight of k-point (not used)
!     nevk no of eigenvalues
!     ev eigenvalue
!     qal partial charges
!             partial charge interstitial: qal(lmax*ntype+1...
!             partial charge vacuum      : qal(lmax*ntype+2...
!     qlay,qstar read in vacuum charges
!     qval partial charges in the vacuum
!             qval(m*nstars,neigd,nkptd):charge in m'th layer
!             qval(m*nstars+nstars,... ):star-resolved charge of that layer
!             qval(layers*nstars+....  ):same for second vacuum
!     ntb=max(nevk)
!
!----------------------------------------------------------------------
22
      USE m_eig66_io,ONLY:read_dos,read_eig
23 24 25 26 27 28 29
      USE m_triang
      USE m_maketetra
      USE m_tetrados
      USE m_dosbin
      USE m_ptdos
      USE m_smooth
      USE m_types
30 31
      USE m_constants
      USE m_cdn_io
32
      IMPLICIT NONE
33
      INTEGER,INTENT(IN)             :: eig_id
34 35 36 37 38 39 40 41
      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_sym),INTENT(IN)         :: sym
      TYPE(t_cell),INTENT(IN)        :: cell
42 43
      TYPE(t_results),INTENT(IN)     :: results
      TYPE(t_dos),INTENT(IN)         :: dos
44 45 46 47
      TYPE(t_kpts),INTENT(IN)        :: kpts
      TYPE(t_atoms),INTENT(IN)       :: atoms

      INTEGER, INTENT(IN) :: nsld
48
      REAL,    INTENT(IN) :: efermiarg, bandgap
49 50
      LOGICAL, INTENT(IN) :: l_mcd 

51 52
      INTEGER, INTENT(IN) :: ncore(atoms%ntype)!(ntype)
      REAL,    INTENT(IN) :: e_mcd(atoms%ntype,input%jspins,dimension%nstd) 
53 54 55 56 57
!-odim
!+odim 
!    locals
      INTEGER, PARAMETER ::  lmax= 4, ned = 1301
      INTEGER  i,s,v,index,jspin,k,l,l1,l2,ln,n,nl,ntb,ntria,ntetra
58
      INTEGER  icore,qdim,n_orb,ncored
59 60 61
      REAL     as,de,efermi,emax,emin,qmt,sigma,totdos,efermiPrev
      REAL     e_up,e_lo,e_test1,e_test2,fac,sumwei,dk,eFermiCorrection
      LOGICAL  l_tria,l_orbcomp,l_error
62

63
      INTEGER  itria(3,2*kpts%nkpt),itetra(4,6*kpts%nkpt)
64
      REAL     voltet(6*kpts%nkpt),kx(kpts%nkpt),vkr(3,kpts%nkpt)
Daniel Wortmann's avatar
Daniel Wortmann committed
65 66
      REAL     ev(dimension%neigd,kpts%nkpt),e(ned),gpart(ned,atoms%ntype),atr(2*kpts%nkpt)
      REAL     e_grid(ned+1),spect(ned,3*atoms%ntype),ferwe(dimension%neigd,kpts%nkpt)
67
      REAL,    ALLOCATABLE :: qal(:,:,:),qval(:,:,:),qlay(:,:,:),g(:,:)
68 69 70 71 72 73
      REAL,    ALLOCATABLE :: mcd(:,:,:),orbcomp(:,:,:),qmtp(:,:)
      REAL,    ALLOCATABLE :: qintsl(:,:),qmtsl(:,:),qvac(:,:)
      CHARACTER(len=2) :: spin12(2),ch_mcd(3)
      CHARACTER(len=8) :: chntype*2,chform*19
      DATA spin12/'.1' , '.2'/
      DATA ch_mcd/'.+' , '.-' , '.0'/
74 75

      ncored =  MAX(0,MAXVAL(ncore))
Daniel Wortmann's avatar
Daniel Wortmann committed
76 77 78
      qdim = lmax*atoms%ntype+3
      l_orbcomp = banddos%l_orb
      IF (banddos%ndir.EQ.-3) THEN
79 80
        qdim = 2*nsld 
        n_orb = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
81 82 83 84 85
        IF (banddos%l_orb) THEN
           n_orb = banddos%orbCompAtom
           WRITE (*,*) 'DOS: orbcomp',n_orb
           qdim = 23
        END IF
86
      ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
87 88
      ALLOCATE( qal(qdim,dimension%neigd,kpts%nkpt),&
     &          qval(vacuum%nstars*vacuum%layers*vacuum%nvac,dimension%neigd,kpts%nkpt),&
89
     &          qlay(dimension%neigd,vacuum%layerd,2))
90 91 92 93 94
      IF (l_mcd) THEN 
         ALLOCATE( mcd(3*atoms%ntype*ncored,dimension%neigd,kpts%nkpt) )
      ELSE
         ALLOCATE(mcd(0,0,0))
      ENDIF
95 96
!
! scale energies
97 98 99 100
      sigma = banddos%sig_dos*hartree_to_ev_const
      emin =min(banddos%e1_dos*hartree_to_ev_const,banddos%e2_dos*hartree_to_ev_const)
      emax =max(banddos%e1_dos*hartree_to_ev_const,banddos%e2_dos*hartree_to_ev_const)
      efermi = efermiarg*hartree_to_ev_const
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 
      WRITE (6,'(a)') 'DOS-Output is generated!'

      IF ( NINT((emax - emin)/sigma) > ned ) THEN
        WRITE(6,*) 'sig_dos too small for DOS smoothing:'   
        WRITE(6,*) 'Reduce energy window or enlarge banddos%sig_dos!'
        WRITE(6,*) 'For now: setting sigma to zero !'
        sigma = 0.0
      ENDIF

      WRITE (6,*) 'sigma=   ' , sigma
      WRITE (6,*) 'emax=   ' , emax
      WRITE (6,*) 'emin=   ' , emin
      WRITE (6,*) 'ef_inp=   ' , efermi
!
!     create energy grid
      emax = emax - efermi
      emin = emin - efermi
      de = (emax-emin)/(ned-1)
      DO i=1,ned
         e(i) = emin + (i-1)*de
      ENDDO
 
      IF ( l_mcd ) THEN ! create an energy grid for mcd-spectra
        e_lo =  9.9d+9 
        e_up = -9.9d+9     
        DO jspin = 1,input%jspins
          DO n = 1,atoms%ntype
            DO icore = 1 , ncore(n)
              e_lo = min(e_mcd(n,jspin,icore),e_lo)
              e_up = max(e_mcd(n,jspin,icore),e_up)
            ENDDO
          ENDDO
        ENDDO
135 136
        e_lo = e_lo*hartree_to_ev_const - efermi - emax 
        e_up = e_up*hartree_to_ev_const - efermi
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        de = (e_up-e_lo)/(ned-1)
        DO i=1,ned
          e_grid(i) = e_lo + (i-1)*de
          spect(i,:) = 0.0
        ENDDO
        e_grid(ned+1) = e_lo + ned*de
      ENDIF

      DO jspin = 1,input%jspins
         ntb = 0
         DO k = 1,kpts%nkpt
!
!     initialize arrays
!
            DO n = 1,dimension%neigd
               DO i = 1,qdim
                  qal(i,n,k) = 0.
               ENDDO
               DO i = 1,vacuum%nstars*vacuum%layers*vacuum%nvac
                  qval(i,n,k) = 0.
               ENDDO
            ENDDO
!
160
!     read data from file!
161
!
Daniel Wortmann's avatar
Daniel Wortmann committed
162
            ALLOCATE( orbcomp(dimension%neigd,23,atoms%nat),qintsl(nsld,dimension%neigd))
163 164
            ALLOCATE( qmtsl(nsld,dimension%neigd),qmtp(dimension%neigd,atoms%nat))
            CALL read_dos(eig_id,k,jspin,mcd,qintsl,qmtsl,qmtp,orbcomp)
Daniel Wortmann's avatar
Daniel Wortmann committed
165
            IF (.NOT.l_orbcomp) THEN
166 167 168 169
               qal(1:lmax*atoms%ntype,:,k)=reshape(dos%qal(0:,:,:,k,jspin),(/lmax*atoms%ntype,size(dos%qal,3)/))
               qal(lmax*atoms%ntype+2,:,k)=dos%qvac(:,1,k,jspin) ! vacuum 1
               qal(lmax*atoms%ntype+3,:,k)=dos%qvac(:,2,k,jspin) ! vacuum 2
               qal(lmax*atoms%ntype+1,:,k)=dos%qis(:,k,jspin)    ! interstitial
Daniel Wortmann's avatar
Daniel Wortmann committed
170
            ELSE
171 172 173 174 175
               IF (n_orb == 0) THEN
                  qal(1:nsld,:,k)        = qintsl(:,:)
                  qal(nsld+1:2*nsld,:,k) = qmtsl(:,:)
               ELSE 
                  DO i = 1, 23
176
                     DO l = 1, results%neig(k,jspin)
177
                        qal(i,l,k) = orbcomp(l,i,n_orb)*qmtp(l,n_orb)/10000.
Daniel Wortmann's avatar
Daniel Wortmann committed
178
                     END DO
179
                     DO l = results%neig(k,jspin)+1, dimension%neigd
180
                        qal(i,l,k) = 0.0
Daniel Wortmann's avatar
Daniel Wortmann committed
181 182 183 184
                     END DO
                  END DO
               END IF
            END IF
185 186
            DEALLOCATE( orbcomp,qintsl,qmtsl,qmtp)
            ntb = max(ntb,results%neig(k,jspin))
187 188 189 190 191 192 193 194 195 196
!
!     set vacuum partial charge zero, if bulk calculation
!     otherwise, write vacuum charge in correct arrays
!
            IF ((.NOT.input%film).AND.(banddos%ndir.NE.-3)) THEN
               DO n = 1,dimension%neigd
                  qal(lmax*atoms%ntype+2,n,k) = 0.0
                  qal(lmax*atoms%ntype+3,n,k) = 0.0
               ENDDO
            ELSEIF ( banddos%vacdos .and. input%film ) THEN
197
               DO i = 1,results%neig(k,jspin)
198 199 200 201 202
                  DO v = 1,vacuum%nvac
                     DO l = 1,vacuum%layers
                        index = (l-1)*vacuum%nstars + (v-1)*(vacuum%nstars*vacuum%layers) + 1
                        qval(index,i,k) = qlay(i,l,v)
                        DO s = 1,vacuum%nstars - 1
203
                           qval(index+s,i,k) = real(dos%qstars(s,i,l,v,k,jspin))
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                        ENDDO
                     ENDDO
                  ENDDO
               ENDDO
            ENDIF
!
!     calculate interstitial dos if not noco
!     in the noco case, qis has been calculated in pwden and is read in from tmp_dos
!
            IF ((.NOT.noco%l_noco).AND.(banddos%ndir.NE.-3)) THEN
               DO i = 1 , dimension%neigd
                  qal(lmax*atoms%ntype+1,i,k) = 1.
                  DO nl = 1 , atoms%ntype
                     l1 = lmax*(nl-1) + 1
                     l2 = lmax*nl
                     qmt=0.0
                     DO l = l1 , l2
                        qmt = qmt + qal(l,i,k)*atoms%neq(nl)
                     ENDDO
                     qal(lmax*atoms%ntype+1,i,k) = qal(lmax*atoms%ntype+1,i,k) - qmt
                  ENDDO
                  qal(lmax*atoms%ntype+1,i,k) = qal(lmax*atoms%ntype+1,i,k)&
                       -qal(lmax*atoms%ntype+2,i,k)*(3-vacuum%nvac) -qal(lmax*atoms%ntype+3,i,k)*(vacuum%nvac-1) 
               ENDDO
            ENDIF
!
!---- >     convert eigenvalues to ev and shift them by efermi
!
232 233
            DO i = 1 , results%neig(k,jspin)
               ev(i,k) = results%eig(i,k,jspin)*hartree_to_ev_const - efermi
234
            ENDDO
235
            DO i = results%neig(k,jspin) + 1, dimension%neigd
236 237 238 239 240 241 242 243 244 245 246
               ev(i,k) = 9.9e+99
            ENDDO
!
!
         ENDDO                                                 ! end of k-point loop
!
!     calculate the triangles!
!
         IF ( jspin.EQ.1 ) THEN
           l_tria=.true.
           IF (input%film .AND. .NOT.oneD%odi%d1) THEN
247
             CALL triang(kpts%bk,kpts%nkpt,itria,ntria,atr,as,l_tria)
248 249 250 251 252 253 254 255
             IF (sym%invs) THEN
               IF (abs(sym%nop2*as-0.5).GT.0.000001) l_tria=.false.
             ELSE
               IF (abs(sym%nop2*as-1.0).GT.0.000001) l_tria=.false.
             ENDIF
             write(*,*) as,sym%nop2,l_tria
!             l_tria=.true.
           ELSE
Gregor Michalicek's avatar
Gregor Michalicek committed
256 257 258 259 260 261 262 263 264 265 266 267 268
             IF (input%l_inpXML) THEN
                IF (input%tria) THEN
                   ntetra = kpts%ntet
                   DO i = 1, ntetra
                      itetra(1:4,i) = kpts%ntetra(1:4,i)
                      voltet(i) = kpts%voltet(i) / ntetra
                   END DO
                   l_tria = input%tria
                   GOTO 67
                ELSE
                   GOTO 66
                END IF
             END IF
269 270 271 272 273 274 275 276 277 278 279 280
             OPEN (41,file='kpts',FORM='formatted',STATUS='old')
             DO i = 1, kpts%nkpt+1
                READ (41,*,END=66,ERR=66)
             ENDDO
             READ (41,'(i5)',END=66,ERR=66) ntetra
             READ (41,'(4(4i6,4x))') ((itetra(i,k),i=1,4),k=1,ntetra)
             READ (41,'(4f20.13)') (voltet(k),k=1,ntetra)
             CLOSE(41)
             voltet(1:ntetra) = voltet(1:ntetra) / ntetra
             l_tria=.true.
             GOTO 67
 66          CONTINUE                       ! no tetrahedron-information of file
281
             CALL triang(kpts%bk,kpts%nkpt,itria,ntria,atr,as,l_tria)
282 283 284 285 286 287 288 289 290 291
             l_tria=.true.
! YM: tetrahedrons is not the way in 1D
             IF (oneD%odi%d1) as = 0.0         
             IF (sym%invs) THEN
               IF (abs(sym%nop2*as-1.0).GT.0.000001) l_tria=.false.
             ELSE
               IF (abs(sym%nop2*as-0.5).GT.0.000001) l_tria=.false.
             ENDIF

             IF (l_tria) THEN
292
               CALL make_tetra(kpts%nkpt,kpts%bk,ntria,itria,atr,&
293 294 295 296 297 298 299 300 301 302 303 304
                    ntetra,itetra,voltet)
             ELSE
               WRITE (6,*) 'no tetrahedron method with these k-points!'
               WRITE (6,*) sym%nop2,as
             ENDIF
 67          CONTINUE                       ! tetrahedron-information read or created
           ENDIF
         ENDIF
!
        IF ( .not.l_mcd ) THEN
         ALLOCATE (g(ned,qdim))
        ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
305
         ALLOCATE (g(ned,3*atoms%ntype*ncored))
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        ENDIF
!
         IF ( l_tria.and.(.not.l_mcd).and.(banddos%ndir.NE.-3) ) THEN
!
!     DOS calculation: use triangular method!!
!
            IF ( input%film ) THEN
!             CALL ptdos(
!    >                  emin,emax,jspins,ned,qdim,neigd,
!    >                  ntria,as,atr,2*nkpt,itria,nkpt,ev,qal,e,
!    <                  g)
              CALL ptdos(emin,emax,input%jspins,ned,qdim,ntb,ntria,as,&
                        atr,2*kpts%nkpt,itria,kpts%nkpt,ev(1:ntb,1:kpts%nkpt),&
                        qal(:,1:ntb,1:kpts%nkpt),e, g)
            ELSE
              write(*,*) efermi
              CALL tetra_dos(lmax,atoms%ntype,dimension%neigd,ned,ntetra,kpts%nkpt,&
323
                            itetra,efermi,voltet,e,results%neig(:,jspin), ev,qal, g)
324 325 326 327 328 329 330 331
              IF (input%jspins.EQ.1) g(:,:) = 2 * g(:,:)
            ENDIF
         ELSE
!
!     DOS calculation: use histogram method
!
            IF ( .not.l_mcd ) THEN
            CALL dos_bin(input%jspins,qdim,ned,emin,emax,dimension%neigd,kpts%nkpt,&
332
                 results%neig(:,jspin),kpts%wtkpt(1:kpts%nkpt),ev,qal, g)
333
            ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
334
            CALL dos_bin(input%jspins,3*atoms%ntype*ncored,ned,emin,emax,ntb,kpts%nkpt,&
335
                 results%neig(:,jspin),kpts%wtkpt(1:kpts%nkpt),ev(1:ntb,1:kpts%nkpt), mcd(1:3*atoms%ntype*ncored,1:ntb,1:kpts%nkpt), g)
336 337 338 339 340 341 342 343 344 345 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 373 374 375 376 377 378 379 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 405 406 407 408 409 410 411 412 413
            ENDIF
         ENDIF
!
!---- >     smoothening
!
         IF ( .not.l_mcd ) THEN
            IF ( sigma.GT.0.0 ) THEN
              DO ln = 1 , qdim
                CALL smooth(e,g(1,ln),sigma,ned)
              ENDDO
            ENDIF
 
!*** sum up for all atoms
 
         IF (banddos%ndir.NE.-3) THEN
            DO l = 1 , atoms%ntype
               l1 = lmax*(l-1) + 1
               l2 = lmax*l
               DO i = 1 , ned
                  gpart(i,l) = 0.0
                  DO nl = l1 , l2
                     gpart(i,l) = gpart(i,l) + g(i,nl)
                  ENDDO
               ENDDO
            ENDDO
         ELSEIF (n_orb == 0) THEN
            DO l = 1 , nsld
               nl = nsld+l
               DO i = 1 , ned
                  gpart(i,l) = g(i,l) + g(i,nl)
               ENDDO
            ENDDO
         ENDIF
    
!**** write out DOS
         OPEN (18,FILE='DOS'//spin12(jspin))

         DO i = 1 , ned
           totdos = 0.0
           IF (banddos%ndir.NE.-3) THEN
             DO nl = 1 , atoms%ntype
                totdos = totdos + gpart(i,nl)*atoms%neq(nl)
             ENDDO
             totdos = totdos + g(i,lmax*atoms%ntype+1) + g(i,lmax*atoms%ntype+2) *&
                  (3 - vacuum%nvac) + g(i,lmax*atoms%ntype+3)*(vacuum%nvac - 1)
             IF (atoms%ntype < 20) THEN
                WRITE (18,99001)  e(i),totdos,g(i,lmax*atoms%ntype+1), &
                     g(i,lmax*atoms%ntype+2),g(i,lmax*atoms%ntype+3),&
                     (gpart(i,l),l=1,atoms%ntype), (g(i,l),l=1,atoms%ntype*lmax)
             ELSE
             WRITE (18,99001)  e(i),totdos,g(i,lmax*atoms%ntype+1), &
                  g(i,lmax*atoms%ntype+2),g(i,lmax*atoms%ntype+3), (gpart(i,l),l=1,atoms%ntype)
          ENDIF
       ELSEIF (n_orb == 0) THEN
          DO nl = 1 , nsld
             totdos = totdos + gpart(i,nl)
          ENDDO
          WRITE (18,99001)  e(i),totdos,(gpart(i,nl),nl=1,nsld), (g(i,l),l=1,2*nsld)
           ELSE
             DO nl = 1 , 23
                totdos = totdos + g(i,nl)
             ENDDO
             WRITE (18,99001)  e(i),totdos,(g(i,l),l=1,23)
           ENDIF
         ENDDO
         CLOSE (18)

         ELSE
           write(*,'(4f15.8)') ((e_mcd(n,jspin,i),n=1,atoms%ntype),i=1,ncored)
           write(*,*)
           write(*,'(4f15.8)') (g(800,n),n=1,3*atoms%ntype*ncored)
           write(*,*)
           write(*,'(4f15.8)') (mcd(n,10,8),n=1,3*atoms%ntype*ncored)
           DO n = 1,atoms%ntype
             DO l = 1 , ned
               DO icore = 1 , ncore(n)
                 DO i = 1 , ned-1
                   IF (e(i).GT.0) THEN     ! take unoccupied part only
414 415
                   e_test1 = -e(i) - efermi +e_mcd(n,jspin,icore)*hartree_to_ev_const
                   e_test2 = -e(i+1)-efermi +e_mcd(n,jspin,icore)*hartree_to_ev_const
416 417 418
                   IF ((e_test2.LE.e_grid(l)).AND. (e_test1.GT.e_grid(l))) THEN
                     fac = (e_grid(l)-e_test1)/(e_test2-e_test1)
                     DO k = 3*(n-1)+1,3*(n-1)+3
Daniel Wortmann's avatar
Daniel Wortmann committed
419 420
                       spect(l,k) = spect(l,k)+ g(i,3*atoms%ntype*(icore-1)+k)&
                           *(1.-fac) + fac * g(i+1,3*atoms%ntype*(icore-1)+k)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                     ENDDO
                   ENDIF
                   ENDIF
                 ENDDO
               ENDDO
             ENDDO
           ENDDO
           CLOSE (18) 
         ENDIF
         DEALLOCATE (g)
!         
!------------------------------------------------------------------------------
!     now calculate the VACOS
!------------------------------------------------------------------------------
            
         IF ( banddos%vacdos .and. input%film ) THEN
            ALLOCATE(g(ned,vacuum%nstars*vacuum%layers*vacuum%nvac))
!            CALL ptdos(
!     >                 emin,emax,jspins,ned,nstars*nvac*layers,neigd,
!     >                 ntria,as,atr,2*nkpt,itria,nkptd,ev,qval,e,
!     <                 g)
            CALL ptdos(emin,emax,input%jspins,ned,vacuum%nstars*vacuum%nvac*vacuum%layers,ntb,ntria&
Daniel Wortmann's avatar
Daniel Wortmann committed
443
                ,as,atr,2*kpts%nkpt,itria,kpts%nkpt,ev(1:ntb,1:kpts%nkpt), qval(:,1:ntb,1:kpts%nkpt),e,g)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
            
!---- >     smoothening
            IF ( sigma.GT.0.0 ) THEN
               DO ln = 1 , vacuum%nstars*vacuum%nvac*vacuum%layers
                  CALL smooth(e,g(1,ln),sigma,ned)
               ENDDO
            ENDIF
            
!     write VACDOS
            
            OPEN (18,FILE='VACDOS'//spin12(jspin))
!            WRITE (18,'(i2,25(2x,i3))') Layers , (Zlay(l),l=1,Layers)
            DO i = 1 , ned
             WRITE (18,99001) e(i) , (g(i,l),l=1,vacuum%Layers*vacuum%Nstars*vacuum%Nvac)
            ENDDO
            CLOSE (18)
            DEALLOCATE(g)
         ENDIF
!
!------------------------------------------------------------------------------
!     for bandstructures
!------------------------------------------------------------------------------

         IF (banddos%ndir == -4) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
468
            eFermiCorrection = 0.0
469 470 471 472 473 474 475 476 477 478 479 480 481
            IF(bandgap.LT.(8.0*input%tkb*hartree_to_ev_const)) THEN
               CALL readPrevEFermi(eFermiPrev,l_error)
               IF(.NOT.l_error) THEN
                  WRITE(*,*) 'Fermi energy is automatically corrected in bands.* files.'
                  WRITE(*,*) 'It is consistent with last calculated density!'
                  WRITE(*,*) 'No manual correction (e.g. in band.gnu file) required.'
                  eFermiCorrection = (eFermiPrev-efermiarg)*hartree_to_ev_const
               ELSE
                  WRITE(*,*) 'Fermi energy in bands.* files may not be consistent with last density.'
                  WRITE(*,*) 'Please correct it manually (e.g. in band.gnu file).'
               END IF
            END IF

482
            OPEN (18,FILE='bands'//spin12(jspin))
483
            ntb = minval(results%neig(:,jspin))    
484
            kx(1) = 0.0
485
            vkr(:,1)=matmul(kpts%bk(:,1),cell%bmat)
486 487
            DO k = 2, kpts%nkpt
              
488
               vkr(:,k)=matmul(kpts%bk(:,k),cell%bmat)
489 490 491
               dk = (vkr(1,k)-vkr(1,k-1))**2 + (vkr(2,k)-vkr(2,k-1) )**2 + &
                    (vkr(3,k)-vkr(3,k-1))**2
               kx(k) = kx(k-1) + sqrt(dk)
492 493
            ENDDO
            DO i = 1, ntb
494
               DO k = 1, kpts%nkpt
495
                  write(18,'(2f15.9)') kx(k),ev(i,k)-eFermiCorrection
496
               ENDDO
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
            ENDDO
            CLOSE (18)
         ENDIF

      ENDDO
!         
!------------------------------------------------------------------------------
!     for MCD calculations ...
!------------------------------------------------------------------------------

      IF (l_mcd) THEN
        WRITE (chntype,'(i2)') atoms%ntype+1
        chform = '('//chntype//'f15.8)'
        IF ( sigma.GT.0.0 ) THEN
           IF ( l_mcd ) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
512
             DO ln = 1 , 3*atoms%ntype
513 514 515 516 517 518 519 520 521 522 523 524 525
               CALL smooth(e_grid,spect(1,ln),sigma,ned)
             ENDDO
           ENDIF
        ENDIF
        DO l = 1,3
          OPEN (18,FILE='MCD_SPEC'//ch_mcd(l))
          DO i = 1 , ned
          WRITE (18,FMT=chform) e_grid(i),(spect(i,3*(n-1)+l),n=1,atoms%ntype)
          ENDDO
          CLOSE (18)
        ENDDO
      ENDIF

526
      DEALLOCATE(qal,qval,qlay)
527
      IF (l_mcd) DEALLOCATE( mcd )
528
99001 FORMAT (f10.5,110(1x,e10.3))
529 530 531

      END SUBROUTINE evaldos
      END MODULE m_evaldos