cdnval.F90 21.2 KB
Newer Older
1 2 3 4
MODULE m_cdnval
  use m_juDFT
CONTAINS
  SUBROUTINE cdnval(eig_id, mpi,kpts,jspin,sliceplot,noco, input,banddos,cell,atoms,enpara,stars,&
5
                    vacuum,dimension,sphhar,sym,obsolete,vTot,oneD,coreSpecInput,cdnvalKLoop,den,regCharges,results,&
6
                    moments)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    !
    !     ***********************************************************
    !         this subroutin is a modified version of cdnval.F.
    !         it calculates a layer charge distribution and an orbital
    !         composition for each state in a film.
    !         this information is written in the  'ek_orbcomp'  file
    !                                    Yu.Koroteev  01.12.2003
    !     ***********************************************************
    !     flapw7 valence density generator
    !                                         c.l.fu
    !     zelec used to calculate ne - 6.12.95 r.pentcheva
    !
    !     changed subroutine to allow parallel writing of vacdos&dosinp
    !     used temporary direct access file 84,tmp_dos to store data used
    !     in cdninf
    !     call of cdninf changed, sympsi is called from cdnval now
    !     look for 'ifdef CPP_MPI' -blocks!               d.wortmann 6.5.99
    !
    !******** ABBREVIATIONS ************************************************
    !     nbands   : number of bands in the energy window
    !     noccbd   : number of occupied bands
    !     slice    : when set to .true. the charge density of a enery range
    !                (slice) or a single state is calculated
    !     e1s,e2s  : (used if slice) energy range for the slice. if both
    !                are set to 0.0 the charge density of the band nr. nnne
    !                is calculated
    !     pallst   : (used if slice) if set to .true. bands above the
    !                Fermi-Energy are taken into account
    !     nnne     : (used if slice) number of the band of which the charge
    !                density is calculated if e1s and e2s are 0.0
    !     kk       : (used if slice) if set to 0 all k-points are used to
    !                calculate the charge density of the slice, otherwise
    !                only k-points kk is taken into account
    !     nslibd   : number of bands in slice
    !     ener     : band energy averaged over all bands and k-points,
    !                wheighted with the l-like charge of each atom type
    !     sqal     : l-like charge of each atom type. sum over all k-points
    !                and bands
    !***********************************************************************
    !
Gregor Michalicek's avatar
Gregor Michalicek committed
47
    USE m_constants
48
    USE m_eig66_io,ONLY: write_dos
49
    USE m_genMTBasis
50
    USE m_calcDenCoeffs
51 52 53 54 55 56 57 58 59 60 61 62
    USE m_mcdinit
    USE m_sympsi
    USE m_eparas      ! energy parameters and partial charges
    USE m_qal21       ! off-diagonal part of partial charges
    USE m_abcof
    USE m_nmat        ! calculate density matrix for LDA + U
    USE m_vacden
    USE m_nstm3
    USE m_pwden
    USE m_forcea8
    USE m_forcea12
    USE m_forcea21
63
    USE m_checkdopall
64 65 66 67
    USE m_int21       ! integrate (spin) off-diagonal radial functions
    USE m_int21lo     ! -"- for u_lo
    USE m_rhomt21     ! calculate (spin) off-diagonal MT-density coeff's
    USE m_rhonmt21    ! -"-                       non-MT-density coeff's
68
    USE m_cdnmt       ! calculate the density and orbital moments etc.
69 70 71 72 73 74 75 76
    USE m_orbmom      ! coeffd for orbital moments
    USE m_qmtsl       ! These subroutines divide the input%film into vacuum%layers
    USE m_qintsl      ! (slabs) and intergate the DOS in these vacuum%layers
    USE m_orbcomp     ! calculate corbital composition (like p_x,p_y,p_z)
    USE m_Ekwritesl   ! and write to file.
    USE m_abcrot2
    USE m_doswrite
    USE m_cdnread, ONLY : cdn_read0, cdn_read
77 78 79
    USE m_corespec, only : l_cs    ! calculation of core spectra (EELS)
    USE m_corespec_io, only : corespec_init
    USE m_corespec_eval, only : corespec_gaunt,corespec_rme,corespec_dos,corespec_ddscs
80
#ifdef CPP_MPI
81
    USE m_mpi_col_den ! collect density data from parallel nodes
82 83
#endif
    USE m_types
84
    USE m_xmlOutput
85
    IMPLICIT NONE
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    TYPE(t_results),       INTENT(INOUT) :: results
    TYPE(t_mpi),           INTENT(IN)    :: mpi
    TYPE(t_dimension),     INTENT(IN)    :: dimension
    TYPE(t_oneD),          INTENT(IN)    :: oneD
    TYPE(t_enpara),        INTENT(INOUT) :: enpara
    TYPE(t_obsolete),      INTENT(IN)    :: obsolete
    TYPE(t_banddos),       INTENT(IN)    :: banddos
    TYPE(t_sliceplot),     INTENT(IN)    :: sliceplot
    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_stars),         INTENT(IN)    :: stars
    TYPE(t_cell),          INTENT(IN)    :: cell
    TYPE(t_kpts),          INTENT(IN)    :: kpts
    TYPE(t_sphhar),        INTENT(IN)    :: sphhar
    TYPE(t_atoms),         INTENT(IN)    :: atoms
    TYPE(t_coreSpecInput), INTENT(IN)    :: coreSpecInput
    TYPE(t_potden),        INTENT(IN)    :: vTot
105
    TYPE(t_cdnvalKLoop),   INTENT(IN)    :: cdnvalKLoop
106 107 108
    TYPE(t_potden),        INTENT(INOUT) :: den
    TYPE(t_regionCharges), INTENT(INOUT) :: regCharges
    TYPE(t_moments),       INTENT(INOUT) :: moments
109

110
    !     .. Scalar Arguments ..
111
    INTEGER, INTENT(IN)              :: eig_id,jspin
112 113 114

#ifdef CPP_MPI
    INCLUDE 'mpif.h'
115
    LOGICAL :: mpi_flag, mpi_status
116 117
#endif
    !     .. Local Scalars ..
118 119 120 121
    INTEGER :: llpd,ikpt,jsp_start,jsp_end,ispin,jsp
    INTEGER :: i,ie,iv,ivac,j,k,l,n,ilo,isp,nbands,noccbd
    INTEGER :: skip_t,skip_tt
    INTEGER :: nStart,nEnd,nbasfcn
122
    LOGICAL :: l_fmpl,l_evp,l_orbcomprot,l_real, l_write
123
    !     ...Local Arrays ..
124 125 126
    INTEGER :: noccbd_in(kpts%nkpt)
    INTEGER :: nStart_in(kpts%nkpt)
    INTEGER :: nEnd_in(kpts%nkpt)
127
    REAL    :: eig(dimension%neigd)
128

Daniel Wortmann's avatar
Daniel Wortmann committed
129
    INTEGER, ALLOCATABLE :: gvac1d(:),gvac2d(:)
130
    INTEGER, ALLOCATABLE :: jsym(:),ksym(:)
131
    REAL,    ALLOCATABLE :: we(:)
132
    REAL,    ALLOCATABLE :: f(:,:,:,:),g(:,:,:,:),flo(:,:,:,:) ! radial functions
133

134
    TYPE (t_lapw)             :: lapw
135 136 137
    TYPE (t_orb)              :: orb
    TYPE (t_denCoeffs)        :: denCoeffs
    TYPE (t_denCoeffsOffdiag) :: denCoeffsOffdiag
138
    TYPE (t_force)            :: force
139
    TYPE (t_slab)             :: slab
140
    TYPE (t_eigVecCoeffs)     :: eigVecCoeffs
141
    TYPE (t_mcd)              :: mcd
142 143
    TYPE (t_usdus)            :: usdus
    TYPE (t_zMat)             :: zMat
144
    TYPE (t_orbcomp)          :: orbcomp
145

146
    l_real = sym%invs.AND.(.NOT.noco%l_soc).AND.(.NOT.noco%l_noco)
Gregor Michalicek's avatar
Gregor Michalicek committed
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    llpd=(atoms%lmaxd*(atoms%lmaxd+3))/2
    !---> l_fmpl is meant as a switch to to a plot of the full magnet.
    !---> density without the atomic sphere approximation for the magnet.
    !---> density. It is not completely implemented (lo's missing).
    l_fmpl = .false.
    IF (noco%l_mperp) THEN
       !--->    when the off-diag. part of the desinsity matrix, i.e. m_x and
       !--->    m_y, is calculated inside the muffin-tins (l_mperp = T), cdnval
       !--->    is called only once. therefore, several spin loops have been
       !--->    added. if l_mperp = F, these loops run only from jspin - jspin.
       jsp_start = 1
       jsp_end   = 2
    ELSE
       jsp_start = jspin
       jsp_end   = jspin
    ENDIF
164
    !---> if l_mperp = F, these variables are only needed for one spin
165 166 167
    !---> at a time, otherwise for both spins:
    ALLOCATE ( f(atoms%jmtd,2,0:atoms%lmaxd,jsp_start:jsp_end) )      ! Deallocation before mpi_col_den
    ALLOCATE ( g(atoms%jmtd,2,0:atoms%lmaxd,jsp_start:jsp_end) )
168
    ALLOCATE ( flo(atoms%jmtd,2,atoms%nlod,dimension%jspd) )
169 170
    ALLOCATE ( jsym(dimension%neigd),ksym(dimension%neigd) )
    ALLOCATE ( gvac1d(dimension%nv2d),gvac2d(dimension%nv2d) )
171

172
    ! --> Initializations
Gregor Michalicek's avatar
Gregor Michalicek committed
173

174
    CALL usdus%init(atoms,input%jspins)
175 176
    CALL denCoeffs%init(atoms,sphhar,jsp_start,jsp_end)
    CALL denCoeffsOffdiag%init(atoms,noco,sphhar,l_fmpl)
177
    CALL force%init1(input,atoms)
178
    CALL orb%init(atoms,noco,jsp_start,jsp_end)
179
    CALL mcd%init1(banddos,dimension,input,atoms)
180 181
    CALL slab%init(banddos,dimension,atoms,cell)
    CALL orbcomp%init(banddos,dimension,atoms)
182

183
    IF ((l_fmpl).AND.(.not.noco%l_mperp)) CALL juDFT_error("for fmpl set noco%l_mperp = T!" ,calledby ="cdnval")
184
    IF ((banddos%ndir.EQ.-3).AND.banddos%dos.AND.oneD%odi%d1) CALL juDFT_error("layer-resolved feature does not work with 1D",calledby ="cdnval")
185

186
! calculation of core spectra (EELS) initializations -start-
187
    CALL corespec_init(input,atoms,coreSpecInput)
Gregor Michalicek's avatar
Gregor Michalicek committed
188
    IF(l_cs.AND.(mpi%isize.NE.1)) CALL juDFT_error('EELS + MPI not implemented', calledby = 'cdnval')
189 190
    IF(l_cs.AND.jspin.EQ.1) CALL corespec_gaunt()
! calculation of core spectra (EELS) initializations -end-
191

192 193 194
    IF (mpi%irank==0) THEN
       WRITE (6,FMT=8000) jspin
       WRITE (16,FMT=8000) jspin
195
       CALL openXMLElementPoly('mtCharges',(/'spin'/),(/jspin/))
196
    END IF
197 198
8000 FORMAT (/,/,10x,'valence density: spin=',i2)

199 200
    l_write = input%cdinf.AND.mpi%irank==0

201
    DO n = 1,atoms%ntype
202 203 204

       DO ispin = jsp_start, jsp_end
          CALL genMTBasis(atoms,enpara,vTot,mpi,n,ispin,l_write,usdus,f(:,:,0:,ispin),g(:,:,0:,ispin),flo(:,:,:,ispin))
205
       END DO
206 207 208 209 210 211 212 213 214 215

       IF (noco%l_mperp) THEN
          DO l = 0,atoms%lmax(n)
             CALL int_21(f,g,atoms,n,l,denCoeffsOffdiag)
          END DO
          DO ilo = 1, atoms%nlo(n)
             CALL int_21lo(f,g,atoms,n,flo,ilo,denCoeffsOffdiag)
          END DO
       END IF

216
       IF (banddos%l_mcd) CALL mcd_init(atoms,input,dimension,vTot%mt(:,0,:,:),g,f,mcd,n,jspin)
217

218
       IF(l_cs) CALL corespec_rme(atoms,input,n,dimension%nstd,input%jspins,jspin,results%ef,&
219
                                  dimension%msh,vTot%mt(:,0,:,:),f,g)
220
    END DO
221
    DEALLOCATE (f,g,flo)
222

223 224 225
    skip_tt = dot_product(enpara%skiplo(:atoms%ntype,jspin),atoms%neq(:atoms%ntype))
    IF (noco%l_soc.OR.noco%l_noco)  skip_tt = 2 * skip_tt
    ALLOCATE (we(MAXVAL(cdnvalKLoop%noccbd(:))))
226
    jsp = MERGE(1,jspin,noco%l_noco)
227

228
    DO ikpt = cdnvalKLoop%ikptStart, cdnvalKLoop%nkptExtended, cdnvalKLoop%ikptIncrement
229 230 231 232 233 234 235 236 237 238 239
       IF (ikpt.GT.kpts%nkpt) THEN
#ifdef CPP_MPI
          ! Synchronizes the RMA operations
          CALL MPI_BARRIER(mpi%mpi_comm,ie)
#endif
          EXIT
       END IF

       ! -> Gu test: distribute ev's among the processors...
       CALL lapw%init(input,noco, kpts,atoms,sym,ikpt,cell,.false., mpi)
       skip_t = skip_tt
240 241 242 243 244 245 246 247 248 249 250
       noccbd = cdnvalKLoop%noccbd(ikpt)
       nStart = cdnvalKLoop%nStart(ikpt)
       nEnd = cdnvalKLoop%nEnd(ikpt)

       we=0.0
       IF(noccbd.GT.0) THEN
          we(1:noccbd) = results%w_iks(nStart:nEnd,ikpt,jsp)
       END IF
       IF ((sliceplot%slice).AND.(input%pallst)) THEN
          we(:) = kpts%wtkpt(ikpt)
       END IF
251

252 253 254 255
       IF (cdnvalKLoop%l_evp) THEN
          IF (nStart > skip_tt) skip_t = 0
          IF (nEnd <= skip_tt) skip_t = noccbd
          IF ((nStart <= skip_tt).AND.(nEnd > skip_tt)) skip_t = mod(skip_tt,noccbd)
256
       END IF
257

258
       nbasfcn = MERGE(lapw%nv(1)+lapw%nv(2)+2*atoms%nlotot,lapw%nv(1)+atoms%nlotot,noco%l_noco)
259
       CALL zMat%init(l_real,nbasfcn,noccbd)
260 261
       CALL cdn_read(eig_id,dimension%nvd,dimension%jspd,mpi%irank,mpi%isize,&
                     ikpt,jspin,zmat%nbasfcn,noco%l_ss,noco%l_noco,&
262
                     noccbd,nStart,nEnd,nbands,eig,zMat)
263
#ifdef CPP_MPI
264 265
       ! Synchronizes the RMA operations
       CALL MPI_BARRIER(mpi%mpi_comm,ie)
266
#endif
267

268
       eig(1:noccbd) = results%eig(nStart:nEnd,ikpt,jsp)
269 270 271 272 273 274

       IF (vacuum%nstm.EQ.3.AND.input%film) THEN
          CALL nstm3(sym,atoms,vacuum,stars,ikpt,lapw%nv(jspin),input,jspin,kpts,&
                     cell,kpts%wtkpt(ikpt),lapw%k1(:,jspin),lapw%k2(:,jspin),&
                     enpara%evac0(1,jspin),vTot%vacz(:,:,jspin),gvac1d,gvac2d)
       END IF
275

276 277
       IF (noccbd.EQ.0) GO TO 199

278 279 280 281
       !     ----> add in spin-doubling factor
       we(:noccbd) = 2.0 * we(:noccbd) / input%jspins

       !--->    valence density in the interstitial and vacuum region
282 283
       !--->    has to be called only once (if jspin=1) in the non-collinear case

284 285 286 287 288 289 290
       !     ----> valence density in the interstitial region
       IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
          CALL timestart("cdnval: pwden")
          CALL pwden(stars,kpts,banddos,oneD,input,mpi,noco,cell,atoms,sym,ikpt,&
                     jspin,lapw,noccbd,we,eig,den,regCharges%qis,results,force%f_b8,zMat)
          CALL timestop("cdnval: pwden")
       END IF
291

292 293 294
       !--->    charge of each valence state in this k-point of the SBZ
       !--->    in the layer interstitial region of the film
       IF (banddos%dos.AND.(banddos%ndir.EQ.-3)) THEN
295
          IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
296
             CALL q_int_sl(jspin,stars,atoms,sym,cell,noccbd,lapw,slab,oneD,zMat)
297
          END IF
298
       END IF
299

300 301 302 303 304 305 306 307
       !--->    valence density in the vacuum region
       IF (input%film) THEN
          IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
             CALL timestart("cdnval: vacden")
             CALL vacden(vacuum,dimension,stars,oneD, kpts,input, cell,atoms,noco,banddos,&
                         gvac1d,gvac2d, we,ikpt,jspin,vTot%vacz(:,:,jspin),noccbd,lapw,enpara%evac0,eig,&
                         den,regCharges%qvac,regCharges%qvlay,regCharges%qstars,zMat)
             CALL timestop("cdnval: vacden")
308
          END IF
309 310 311 312 313 314
          !--->       perform Brillouin zone integration and summation over the
          !--->       bands in order to determine the vacuum energy parameters.
          DO ispin = jsp_start,jsp_end
             DO ivac = 1,vacuum%nvac
                regCharges%pvac(ivac,ispin)=regCharges%pvac(ivac,ispin)+dot_product(eig(:noccbd)*regCharges%qvac(:noccbd,ivac,ikpt,ispin),we(:noccbd))
                regCharges%svac(ivac,ispin)=regCharges%svac(ivac,ispin)+dot_product(regCharges%qvac(:noccbd,ivac,ikpt,ispin),we(:noccbd))
315
             END DO
316 317
          END DO
       END IF
318

319 320
       !--->    valence density in the atomic spheres
       CALL eigVecCoeffs%init(dimension,atoms,noco,jspin,noccbd)
321

322
       DO ispin = jsp_start,jsp_end
323 324
          IF (input%l_f) CALL force%init2(noccbd,input,atoms)

325 326 327 328 329 330
          CALL timestart("cdnval: abcof")
          CALL abcof(input,atoms,sym,cell,lapw,noccbd,usdus,noco,ispin,oneD,&
                     eigVecCoeffs%acof(:,0:,:,ispin),eigVecCoeffs%bcof(:,0:,:,ispin),&
                     eigVecCoeffs%ccof(-atoms%llod:,:,:,:,ispin),zMat,eig,force)
          CALL timestop("cdnval: abcof")

331
          IF (atoms%n_u.GT.0) CALL n_mat(atoms,sym,noccbd,usdus,ispin,we,eigVecCoeffs,den%mmpMat(:,:,:,jspin))
332

333 334 335 336 337
          !--->       perform Brillouin zone integration and summation over the
          !--->       bands in order to determine the energy parameters for each
          !--->       atom and angular momentum
          IF (.not.sliceplot%slice) THEN
             CALL eparas(ispin,atoms,noccbd,mpi,ikpt,noccbd,we,eig,&
338
                         skip_t,cdnvalKLoop%l_evp,eigVecCoeffs,usdus,regCharges,mcd,banddos%l_mcd)
339

340 341
             IF (noco%l_mperp.AND.(ispin == jsp_end)) THEN
                CALL qal_21(dimension,atoms,input,noccbd,noco,eigVecCoeffs,denCoeffsOffdiag,regCharges)
342
             END IF
343
          END IF
344

345 346 347 348
          !--->    layer charge of each valence state in this k-point of the SBZ
          !--->    from the mt-sphere region of the film
          IF (banddos%dos.AND.(banddos%ndir.EQ.-3))  THEN
             CALL q_mt_sl(ispin,atoms,noccbd,ikpt,noccbd,skip_t,noccbd,eigVecCoeffs,usdus,slab)
349

350
             INQUIRE (file='orbcomprot',exist=l_orbcomprot)
351
             IF (l_orbcomprot) CALL abcrot2(atoms,noccbd,eigVecCoeffs,ispin) ! rotate ab-coeffs
352

353 354
             CALL orb_comp(ispin,noccbd,atoms,noccbd,usdus,eigVecCoeffs,orbcomp)
          END IF
355 356

          CALL calcDenCoeffs(atoms,sphhar,sym,we,noccbd,eigVecCoeffs,ispin,denCoeffs)
357 358 359 360 361 362 363 364 365 366 367 368

          IF (noco%l_soc) CALL orbmom(atoms,noccbd,we,ispin,eigVecCoeffs,orb)

          IF (input%l_f) THEN
             CALL timestart("cdnval: force_a12/21")
             IF (.not.input%l_useapw) THEN
                CALL force_a12(atoms,noccbd,sym,dimension,cell,oneD,&
                               we,ispin,noccbd,usdus,eigVecCoeffs,force,results)
             ENDIF
             CALL force_a21(input,atoms,dimension,noccbd,sym,oneD,cell,we,ispin,&
                            enpara%el0(0:,:,ispin),noccbd,eig,usdus,eigVecCoeffs,force,results)
             CALL timestop("cdnval: force_a12/21")
369 370
          END IF

371 372 373 374 375
          IF(l_cs) THEN
             CALL corespec_dos(atoms,usdus,ispin,dimension%lmd,kpts%nkpt,ikpt,dimension%neigd,&
                               noccbd,results%ef,banddos%sig_dos,eig,we,eigVecCoeffs)
          END IF
       END DO !--->    end loop over ispin
376

377 378 379 380
       IF (noco%l_mperp) THEN
          CALL rhomt21(atoms,we,noccbd,eigVecCoeffs,denCoeffsOffdiag)
          IF (l_fmpl) CALL rhonmt21(atoms,sphhar,we,noccbd,sym,eigVecCoeffs,denCoeffsOffdiag)
       END IF
381

382 383 384 385 386 387
199    CONTINUE
       IF ((banddos%dos .OR. banddos%vacdos .OR. input%cdinf)  ) THEN
          CALL timestart("cdnval: write_info")
          !--->    calculate charge distribution of each state (l-character ...)
          !--->    and write the information to the files dosinp and vacdos
          !--->    for dos and bandstructure plots
388

389 390 391
          !--dw    parallel writing of vacdos,dosinp....
          !        write data to direct access file first, write to formated file later by PE 0 only!
          !--dw    since z is no longer an argument of cdninf sympsi has to be called here!
392

393 394 395
          IF (banddos%ndir.GT.0) THEN
             CALL sympsi(lapw%bkpt,lapw%nv(jspin),lapw%k1(:,jspin),lapw%k2(:,jspin),&
                         lapw%k3(:,jspin),sym,dimension,nbands,cell,eig,noco, ksym,jsym,zMat)
396 397
          END IF

398 399 400 401 402 403 404
          !--dw   now write k-point data to tmp_dos
          CALL write_dos(eig_id,ikpt,jspin,regCharges%qal(:,:,:,jspin),regCharges%qvac(:,:,ikpt,jspin),regCharges%qis(:,ikpt,jspin),&
                         regCharges%qvlay(:,:,:,ikpt,jspin),regCharges%qstars,ksym,jsym,mcd%mcd,slab%qintsl,&
                         slab%qmtsl(:,:),orbcomp%qmtp(:,:),orbcomp%comp)

          CALL timestop("cdnval: write_info")
          !-new_sl
405
       END IF
406
    END DO !---> end of k-point loop
407
    DEALLOCATE (we)
408
    !+t3e
409
#ifdef CPP_MPI
410 411 412
    CALL timestart("cdnval: mpi_col_den")
    DO ispin = jsp_start,jsp_end
       CALL mpi_col_den(mpi,sphhar,atoms,oneD,stars,vacuum,&
413
                        input,noco,l_fmpl,ispin,llpd, den%vacxy(1,1,1,ispin),&
414 415
                        den%vacz(1,1,ispin),den%pw(1,ispin),regCharges,&
                        results,denCoeffs,orb,denCoeffsOffdiag,den,den%mmpMat(:,:,:,jspin))
416 417
    END DO
    CALL timestop("cdnval: mpi_col_den")
418
#endif
419

420 421 422 423 424 425
    IF (mpi%irank==0) THEN
       CALL cdnmt(dimension%jspd,atoms,sphhar,llpd,noco,l_fmpl,jsp_start,jsp_end,&
                  enpara%el0,enpara%ello0,vTot%mt(:,0,:,:),denCoeffs,&
                  usdus,orb,denCoeffsOffdiag,moments,den%mt)

       IF(l_cs) CALL corespec_ddscs(jspin,input%jspins)
426

427 428
       IF (((jspin.eq.input%jspins).OR.noco%l_mperp) .AND. (banddos%dos.or.banddos%vacdos.or.input%cdinf) ) THEN
          CALL timestart("cdnval: dos")
429
          CALL doswrite(eig_id,dimension,kpts,atoms,vacuum,input,banddos,&
430
                        sliceplot,noco,sym,cell,mcd,results,slab%nsld,oneD)
431
          IF (banddos%dos.AND.(banddos%ndir.EQ.-3)) THEN
432
             CALL Ek_write_sl(eig_id,dimension,kpts,atoms,vacuum,input,jspin,sym,cell,slab)
433
          END IF
434
          CALL timestop("cdnval: dos")
435 436 437
       END IF

       DO ispin = jsp_start,jsp_end
438 439
          IF (.NOT.sliceplot%slice) THEN
             DO n=1,atoms%ntype
440 441
                enpara%el1(0:3,n,ispin)=regCharges%ener(0:3,n,ispin)/regCharges%sqal(0:3,n,ispin)
                IF (atoms%nlo(n)>0) enpara%ello1(:atoms%nlo(n),n,ispin)=regCharges%enerlo(:atoms%nlo(n),n,ispin)/regCharges%sqlo(:atoms%nlo(n),n,ispin)
442
             END DO
443
             IF (input%film) enpara%evac1(:vacuum%nvac,ispin)=regCharges%pvac(:vacuum%nvac,ispin)/regCharges%svac(:vacuum%nvac,ispin)
444 445 446 447 448 449 450
          END IF

          !--->      check continuity of charge density
          IF (input%cdinf) THEN
             CALL timestart("cdnval: cdninf-stuff")
             WRITE (6,FMT=8210) ispin
8210         FORMAT (/,5x,'check continuity of cdn for spin=',i2)
451
             CALL checkDOPAll(input,dimension,sphhar,stars,atoms,sym,vacuum,oneD,cell,den,ispin)
452 453 454 455 456 457
             CALL timestop("cdnval: cdninf-stuff")
          END IF
          !+for
          !--->      forces of equ. A8 of Yu et al.
          IF ((input%l_f)) THEN
             CALL timestart("cdnval: force_a8")
458
             CALL force_a8(input,atoms,sphhar,ispin,vTot%mt(:,:,:,ispin),den%mt,force,results)
459 460
             CALL timestop("cdnval: force_a8")
          END IF
461
          !-for
462
       END DO ! end of loop ispin = jsp_start,jsp_end
463
       CALL closeXMLElement('mtCharges')
464 465 466 467 468 469 470 471 472 473

       IF(vacuum%nvac.EQ.1) THEN
          den%vacz(:,2,:) = den%vacz(:,1,:)
          IF (sym%invs) THEN
             den%vacxy(:,:,2,:) = CONJG(den%vacxy(:,:,1,:))
          ELSE
             den%vacxy(:,:,2,:) = den%vacxy(:,:,1,:)
          END IF
       END IF

474
    END IF ! end of (mpi%irank==0)
475 476 477
#ifdef CPP_MPI
    CALL MPI_BARRIER(mpi%mpi_comm,ie)
#endif
478

479 480 481 482
    IF ((jsp_end.EQ.input%jspins)) THEN
       IF ((banddos%dos.OR.banddos%vacdos).AND.(banddos%ndir/=-2))  CALL juDFT_end("DOS OK",mpi%irank)
       IF (vacuum%nstm.EQ.3)  CALL juDFT_end("VACWAVE OK",mpi%irank)
    END IF
483

484 485
  END SUBROUTINE cdnval
END MODULE m_cdnval