cdnval.F90 45.4 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,igq_fft,vr,vz,oneD,coreSpecInput,n_mmp,results, qpw,rhtxy,&
6
       rho,rht,cdom,cdomvz,cdomvxy,qvac,qvlay,qa21, chmom,clmom)
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
    !***********************************************************************
    !
47
    USE m_eig66_io,ONLY: write_dos
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    USE m_radfun
    USE m_radflo
    USE m_rhomt
    USE m_rhonmt
    USE m_rhomtlo
    USE m_rhonmtlo
    USE m_mcdinit
    USE m_sphpts
    USE m_points
    USE m_sympsi
    USE m_enpara, ONLY : w_enpara,mix_enpara
    USE m_eparas      ! energy parameters and partial charges
    USE m_qal21       ! off-diagonal part of partial charges
    USE m_abcof
    USE m_topulay
    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
    USE m_checkdop    ! check continuity of density on MT radius R
    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
75
    USE m_cdnmt       ! calculate the density and orbital moments etc.
76 77 78 79 80 81 82 83 84 85 86
    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_slabdim     ! (mt + interstitial)
    USE m_slabgeom    ! (written by Yu.Koroteev, 2003/2004)
    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_cylpts
    USE m_cdnread, ONLY : cdn_read0, cdn_read
87 88 89
    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
90
#ifdef CPP_MPI
91
    USE m_mpi_col_den ! collect density data from parallel nodes
92 93
#endif
    USE m_types
94
    USE m_xmlOutput
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    IMPLICIT NONE
    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
113 114
    TYPE(t_coreSpecInput),INTENT(IN) :: coreSpecInput

115 116 117 118
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: eig_id,jspin

    !     .. Array Arguments ..
119
    COMPLEX, INTENT(INOUT) :: qpw(stars%ng3,dimension%jspd)
120
    COMPLEX, INTENT(INOUT) :: rhtxy(vacuum%nmzxyd,oneD%odi%n2d-1,2,dimension%jspd)
121
    COMPLEX, INTENT(INOUT) :: cdom(stars%ng3)
122 123
    COMPLEX, INTENT(INOUT) :: cdomvz(vacuum%nmzd,2)
    COMPLEX, INTENT(INOUT) :: cdomvxy(vacuum%nmzxyd,oneD%odi%n2d-1,2)
Daniel Wortmann's avatar
Daniel Wortmann committed
124
    COMPLEX, INTENT(INOUT) :: qa21(atoms%ntype)
125
    INTEGER, INTENT (IN) :: igq_fft(0:stars%kq1_fft*stars%kq2_fft*stars%kq3_fft-1)
126
    REAL, INTENT    (IN) :: vz(vacuum%nmzd,2)
Daniel Wortmann's avatar
Daniel Wortmann committed
127 128 129
    REAL, INTENT    (IN) :: vr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,dimension%jspd)
    REAL, INTENT   (OUT) :: chmom(atoms%ntype,dimension%jspd),clmom(3,atoms%ntype,dimension%jspd)
    REAL, INTENT (INOUT) :: rho(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,dimension%jspd)
130
    REAL, INTENT (INOUT) :: rht(vacuum%nmzd,2,dimension%jspd)
131 132
    REAL, INTENT (INOUT) :: qvac(dimension%neigd,2,kpts%nkpt,dimension%jspd)
    REAL, INTENT (INOUT) :: qvlay(dimension%neigd,vacuum%layerd,2,kpts%nkpt,dimension%jspd)
133 134 135 136
    COMPLEX, INTENT(INOUT) :: n_mmp(-3:3,-3:3,atoms%n_u)

#ifdef CPP_MPI
    INCLUDE 'mpif.h'
137
    logical :: mpi_flag, mpi_status
138 139 140 141 142 143
#endif
    !     .. Local Scalars ..
    TYPE(t_lapw):: lapw
    INTEGER :: llpd
    REAL wk,wronk,sign,emcd_lo,emcd_up
    INTEGER i,ie,iv,ivac,j,k,l,l1,lh ,n,ilo,isp,nat,&
144 145
         nbands,noded,nodeu,noccbd,nslibd,na,&
         ikpt,npd ,jsp_start,jsp_end,ispin
146 147 148 149 150
    INTEGER  skip_t,skip_tt
    INTEGER n_size,i_rec,n_rank ,ncored,n_start,n_end,noccbd_l
    COMPLEX,parameter:: czero=(0.0,0.0)
    LOGICAL l_fmpl,l_mcd,l_evp,l_orbcomprot
    !     ...Local Arrays ..
Daniel Wortmann's avatar
Daniel Wortmann committed
151
    INTEGER n_bands(0:dimension%neigd),ncore(atoms%ntype)
Daniel Wortmann's avatar
Daniel Wortmann committed
152
    REAL    cartk(3),xp(3,dimension%nspd),e_mcd(atoms%ntype,input%jspins,dimension%nstd)
Daniel Wortmann's avatar
Daniel Wortmann committed
153 154
    REAL    ello(atoms%nlod,atoms%ntype,dimension%jspd),evac(2,dimension%jspd)
    REAL    epar(0:atoms%lmaxd,atoms%ntype,dimension%jspd),evdu(2,dimension%jspd)
155 156
    REAL    eig(dimension%neigd)
    REAL    vz0(2)
Daniel Wortmann's avatar
Daniel Wortmann committed
157 158
    REAL    uuilon(atoms%nlod,atoms%ntype),duilon(atoms%nlod,atoms%ntype)
    REAL    ulouilopn(atoms%nlod,atoms%nlod,atoms%ntype)
159 160

    INTEGER, PARAMETER :: n2max_nstm3=13
161

162 163 164 165 166
    INTEGER nsld,nsl
    !
    INTEGER, ALLOCATABLE :: nmtsl(:,:),nslat(:,:)
    REAL,    ALLOCATABLE :: zsl(:,:),volsl(:)
    REAL,    ALLOCATABLE :: volintsl(:)
167 168
    REAL,    ALLOCATABLE :: qintsl(:,:),qmtsl(:,:)
    REAL,    ALLOCATABLE :: orbcomp(:,:,:),qmtp(:,:)
169
    REAL,    ALLOCATABLE :: qis(:,:,:)
170 171
    !-new_sl
    !-dw
Daniel Wortmann's avatar
Daniel Wortmann committed
172
    INTEGER, ALLOCATABLE :: gvac1d(:),gvac2d(:)
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
    INTEGER, ALLOCATABLE :: jsym(:),ksym(:)

    REAL,    ALLOCATABLE :: aclo(:,:,:),acnmt(:,:,:,:,:)
    REAL,    ALLOCATABLE :: bclo(:,:,:),bcnmt(:,:,:,:,:)
    REAL,    ALLOCATABLE :: cclo(:,:,:,:),ccnmt(:,:,:,:,:),we(:)
    REAL,    ALLOCATABLE :: f(:,:,:,:),g(:,:,:,:),flo(:,:,:,:)
    REAL,    ALLOCATABLE :: uloulopn(:,:,:,:),uloulopn21(:,:,:)
    REAL,    ALLOCATABLE :: uu(:,:,:),dd(:,:,:),du(:,:,:)
    REAL,    ALLOCATABLE :: uunmt(:,:,:,:),ddnmt(:,:,:,:)
    REAL,    ALLOCATABLE :: dunmt(:,:,:,:),udnmt(:,:,:,:),sqlo(:,:,:)
    REAL,    ALLOCATABLE :: qal(:,:,:,:),sqal(:,:,:),ener(:,:,:)
    REAL,    ALLOCATABLE :: svac(:,:),pvac(:,:),mcd(:,:,:)
    REAL,    ALLOCATABLE :: enerlo(:,:,:),qmat(:,:,:,:)
    COMPLEX, ALLOCATABLE :: acof(:,:,:,:),bcof(:,:,:,:),ccof(:,:,:,:,:)
    COMPLEX, ALLOCATABLE :: acoflo(:,:,:,:),bcoflo(:,:,:,:)
    COMPLEX, ALLOCATABLE :: cveccof(:,:,:,:,:),f_a12(:,:)
    COMPLEX, ALLOCATABLE :: e1cof(:,:,:),e2cof(:,:,:),f_a21(:,:)
    COMPLEX, ALLOCATABLE :: f_b4(:,:),f_b8(:,:)
    COMPLEX, ALLOCATABLE :: aveccof(:,:,:,:),bveccof(:,:,:,:)
    COMPLEX, ALLOCATABLE :: uloulop21(:,:,:)
    COMPLEX, ALLOCATABLE :: uunmt21(:,:,:),ddnmt21(:,:,:)
    COMPLEX, ALLOCATABLE :: dunmt21(:,:,:),udnmt21(:,:,:)
    COMPLEX, ALLOCATABLE :: qstars(:,:,:,:),m_mcd(:,:,:,:)
    TYPE (t_orb),  ALLOCATABLE :: orb(:,:,:,:)
    TYPE (t_orbl), ALLOCATABLE :: orbl(:,:,:,:)
    TYPE (t_orblo),ALLOCATABLE :: orblo(:,:,:,:,:)
    TYPE (t_mt21), ALLOCATABLE :: mt21(:,:)
    TYPE (t_lo21), ALLOCATABLE :: lo21(:,:)
201 202
    TYPE (t_usdus)             :: usdus
    TYPE (t_zMat)              :: zMat
203
    INTEGER :: nkpt_extended
204

205
    zmat%l_real=sym%invs.AND.(.NOT.noco%l_soc).AND.(.NOT.noco%l_noco)
206 207 208 209 210 211 212 213 214 215 216 217 218 219
    !     ..
    !     ..
    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
Daniel Wortmann's avatar
Daniel Wortmann committed
220 221 222 223
       ALLOCATE ( mt21(0:atoms%lmaxd,atoms%ntype),lo21(atoms%nlod,atoms%ntype) )  ! Deallocation at end of subroutine
       ALLOCATE ( uloulopn21(atoms%nlod,atoms%nlod,atoms%ntype) )
       ALLOCATE ( uloulop21(atoms%nlod,atoms%nlod,atoms%ntype) )
       ALLOCATE ( qmat(0:3,atoms%ntype,dimension%neigd,4) )
224
       IF (l_fmpl) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
225 226 227 228
          ALLOCATE ( uunmt21((atoms%lmaxd+1)**2,sphhar%nlhd,atoms%ntype) )
          ALLOCATE ( ddnmt21((atoms%lmaxd+1)**2,sphhar%nlhd,atoms%ntype) )
          ALLOCATE ( dunmt21((atoms%lmaxd+1)**2,sphhar%nlhd,atoms%ntype) )
          ALLOCATE ( udnmt21((atoms%lmaxd+1)**2,sphhar%nlhd,atoms%ntype) )
229 230 231 232 233 234 235 236 237 238 239 240
       ELSE
          ALLOCATE ( uunmt21(1,1,1),ddnmt21(1,1,1) )
          ALLOCATE ( dunmt21(1,1,1),udnmt21(1,1,1) )
       ENDIF
    ELSE
       jsp_start = jspin
       jsp_end   = jspin
       ALLOCATE ( mt21(1,1),lo21(1,1),uunmt21(1,1,1) )
       ALLOCATE ( ddnmt21(1,1,1),dunmt21(1,1,1),udnmt21(1,1,1) )
       ALLOCATE ( uloulopn21(1,1,1),uloulop21(1,1,1),qmat(1,1,1,1) )
    ENDIF
    !
241
    !---> if l_mperp = F, these variables are only needed for one spin
242 243 244 245
    !---> 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) )
Daniel Wortmann's avatar
Daniel Wortmann committed
246 247 248 249 250
    ALLOCATE (   usdus%us(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE (  usdus%uds(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE (  usdus%dus(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%duds(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%ddn(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
251 252
    ALLOCATE ( jsym(dimension%neigd),ksym(dimension%neigd) )
    ALLOCATE ( gvac1d(dimension%nv2d),gvac2d(dimension%nv2d) )
Daniel Wortmann's avatar
Daniel Wortmann committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    ALLOCATE (  usdus%ulos(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%dulos(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%uulon(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%dulon(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( usdus%uloulopn(atoms%nlod,atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( uu(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( dd(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( du(0:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( uunmt(0:llpd,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( ddnmt(0:llpd,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( dunmt(0:llpd,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( udnmt(0:llpd,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( qal(0:3,atoms%ntype,dimension%neigd,jsp_start:jsp_end) )
    ALLOCATE ( sqal(0:3,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( ener(0:3,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE (   sqlo(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
    ALLOCATE ( enerlo(atoms%nlod,atoms%ntype,jsp_start:jsp_end) )
270 271 272 273 274 275 276 277
    ALLOCATE ( svac(2,jsp_start:jsp_end) )
    ALLOCATE ( pvac(2,jsp_start:jsp_end) )
    ALLOCATE ( qstars(vacuum%nstars,dimension%neigd,vacuum%layerd,2) )
    !
    ! --> Initializations
    !
    uu(:,:,:) = 0.0 ; dd(:,:,:) = 0.0 ; du(:,:,:) = 0.0
    IF (noco%l_mperp) THEN
278 279
       mt21(:,:)%uu = czero ; mt21(:,:)%ud = czero
       mt21(:,:)%du = czero ; mt21(:,:)%dd = czero
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
       lo21(:,:)%uulo = czero ; lo21(:,:)%ulou = czero
       lo21(:,:)%dulo = czero ; lo21(:,:)%ulod = czero
       uloulop21(:,:,:) = czero
    ENDIF
    uunmt(:,:,:,:) = 0.0 ; ddnmt(:,:,:,:) = 0.0
    udnmt(:,:,:,:) = 0.0 ; dunmt(:,:,:,:) = 0.0
    IF (l_fmpl) THEN
       IF (.not.noco%l_mperp)  CALL juDFT_error("for fmpl set noco%l_mperp = T!" ,calledby ="cdnval")
       uunmt21(:,:,:) = czero ; ddnmt21(:,:,:) = czero
       udnmt21(:,:,:) = czero ; dunmt21(:,:,:) = czero
    ENDIF
    svac(:,:) = 0.0 ; pvac(:,:) = 0.0
    sqal(:,:,:) = 0.0 ; ener(:,:,:) = 0.0
    !+soc
    IF (noco%l_soc) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
295 296 297
       ALLOCATE ( orb(0:atoms%lmaxd,-atoms%lmaxd:atoms%lmaxd,atoms%ntype,jsp_start:jsp_end) )
       ALLOCATE ( orbl(atoms%nlod,-atoms%llod:atoms%llod,atoms%ntype,jsp_start:jsp_end)     )
       ALLOCATE ( orblo(atoms%nlod,atoms%nlod,-atoms%llod:atoms%llod,atoms%ntype,jsp_start:jsp_end))
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
       orb(:,:,:,:)%uu = 0.0 ; orb(:,:,:,:)%dd = 0.0
       orb(:,:,:,:)%uum = czero ; orb(:,:,:,:)%uup = czero
       orb(:,:,:,:)%ddm = czero ; orb(:,:,:,:)%ddp = czero
       orbl(:,:,:,:)%uulo = 0.0 ; orbl(:,:,:,:)%dulo = 0.0
       orbl(:,:,:,:)%uulom = czero ; orbl(:,:,:,:)%uulop = czero
       orbl(:,:,:,:)%dulom = czero ; orbl(:,:,:,:)%dulop = czero
       orblo(:,:,:,:,:)%z = 0.0
       orblo(:,:,:,:,:)%p = czero ; orblo(:,:,:,:,:)%m = czero
    ELSE
       ALLOCATE ( orb(0:0,-atoms%lmaxd:-atoms%lmaxd,1,jsp_start:jsp_end) )
       ALLOCATE ( orbl(1,-atoms%llod:-atoms%llod,1,jsp_start:jsp_end) )
       ALLOCATE ( orblo(1,1,-atoms%llod:-atoms%llod,1,jsp_start:jsp_end) )
    ENDIF
    !+for
    IF (input%l_f) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
313 314
       ALLOCATE ( f_a12(3,atoms%ntype),f_a21(3,atoms%ntype) )
       ALLOCATE ( f_b4(3,atoms%ntype),f_b8(3,atoms%ntype) )
315 316 317 318 319 320 321 322 323 324 325
       f_b4(:,:) = czero  ; f_a12(:,:) = czero
       f_b8(:,:) = czero  ; f_a21(:,:) = czero
    ELSE
       ALLOCATE ( f_b8(1,1) )
    ENDIF
    !
    INQUIRE (file='mcd_inp',exist=l_mcd)
    IF (l_mcd) THEN
       OPEN (23,file='mcd_inp',STATUS='old',FORM='formatted')
       READ (23,*) emcd_lo,emcd_up
       CLOSE (23)
Daniel Wortmann's avatar
Daniel Wortmann committed
326 327
       ALLOCATE ( m_mcd(dimension%nstd,(3+1)**2,3*atoms%ntype,2) )
       ALLOCATE ( mcd(3*atoms%ntype,dimension%nstd,dimension%neigd) )
328 329 330 331
       IF (.not.banddos%dos) WRITE (*,*) 'For mcd-spectra set banddos%dos=T!'
    ELSE
       ALLOCATE ( m_mcd(1,1,1,1),mcd(1,1,1) )
    ENDIF
332

333
! calculation of core spectra (EELS) initializations -start-
334
    CALL corespec_init(input,atoms,coreSpecInput)
Gregor Michalicek's avatar
Gregor Michalicek committed
335
    IF(l_cs.AND.(mpi%isize.NE.1)) CALL juDFT_error('EELS + MPI not implemented', calledby = 'cdnval')
336 337 338
    IF(l_cs.AND.jspin.EQ.1) CALL corespec_gaunt()
! calculation of core spectra (EELS) initializations -end-

Daniel Wortmann's avatar
Daniel Wortmann committed
339
  
340 341 342
    IF (mpi%irank==0) THEN
       WRITE (6,FMT=8000) jspin
       WRITE (16,FMT=8000) jspin
343
       CALL openXMLElementPoly('mtCharges',(/'spin'/),(/jspin/))
344
    END IF
345 346 347 348 349 350
8000 FORMAT (/,/,10x,'valence density: spin=',i2)

    CALL cdn_read0(&
         eig_id,&
         mpi%irank,mpi%isize,jspin,dimension%jspd,&
         noco%l_noco,&
Daniel Wortmann's avatar
Daniel Wortmann committed
351
         ello,evac,epar,wk,n_bands,n_size)
352 353 354 355
#ifdef CPP_MPI
    ! Sinchronizes the RMA operations
    CALL MPI_BARRIER(mpi%mpi_comm,ie) 
#endif
356 357

    !+lo
358
    !---> if local orbitals are used, the eigenvector has a higher
359
    !---> dimension then nvd
Daniel Wortmann's avatar
Daniel Wortmann committed
360 361 362 363 364 365
    ALLOCATE ( aclo(atoms%nlod,atoms%ntype,jsp_start:jsp_end), &
         bclo(atoms%nlod,atoms%ntype,jsp_start:jsp_end),&
         cclo(atoms%nlod,atoms%nlod,atoms%ntype,jsp_start:jsp_end),&
         acnmt(0:atoms%lmaxd,atoms%nlod,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end), &
         bcnmt(0:atoms%lmaxd,atoms%nlod,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end), &
         ccnmt(atoms%nlod,atoms%nlod,sphhar%nlhd,atoms%ntype,jsp_start:jsp_end) )
366 367 368
    aclo(:,:,:) = 0.0 ; bclo(:,:,:) = 0.0 ; ccnmt(:,:,:,:,:) = 0.0
    acnmt(:,:,:,:,:)=0.0 ; bcnmt(:,:,:,:,:)=0.0 ; cclo(:,:,:,:)=0.0

369
    ALLOCATE ( qis(dimension%neigd,kpts%nkpt,dimension%jspd))
370

371 372 373 374 375 376 377 378
    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
    !-lo
    !---> set up l-dependent m.t. wavefunctions
    na = 1
    ncored = 0

    ALLOCATE ( flo(atoms%jmtd,2,atoms%nlod,dimension%jspd) )
379
    DO n = 1,atoms%ntype
380 381
       IF (input%cdinf.AND.mpi%irank==0) WRITE (6,FMT=8001) n
       DO  l = 0,atoms%lmax(n)
Daniel Wortmann's avatar
Daniel Wortmann committed
382
          DO ispin =jsp_start,jsp_end
383 384 385 386 387 388 389 390
             CALL radfun(&
                  l,n,ispin,epar(l,n,ispin),vr(1,0,n,ispin),atoms,&
                  f(1,1,l,ispin),g(1,1,l,ispin),usdus,&
                  nodeu,noded,wronk)
             IF (input%cdinf.AND.mpi%irank==0) WRITE (6,FMT=8002) l,&
                  epar(l,n,ispin),usdus%us(l,n,ispin),usdus%dus(l,n,ispin),nodeu,&
                  usdus%uds(l,n,ispin),usdus%duds(l,n,ispin),noded,usdus%ddn(l,n,ispin),&
                  wronk
391
          END DO
392 393 394 395 396 397
          IF (noco%l_mperp) THEN
             CALL int_21(&
                  f,g,atoms,n,l,&
                  mt21(l,n)%uun,mt21(l,n)%udn,&
                  mt21(l,n)%dun,mt21(l,n)%ddn)
          END IF
398 399 400 401 402 403 404 405
       END DO
       IF (l_mcd) THEN
          CALL mcd_init(&
               atoms,input,dimension,&
               vr(:,0,:,:),g,f,emcd_up,emcd_lo,n,jspin,&
               ncore,e_mcd,m_mcd)
          ncored = max(ncore(n),ncored)
       END IF
406 407 408 409 410

       IF(l_cs) CALL corespec_rme(atoms,input,n,dimension%nstd,&
                                  input%jspins,jspin,results%ef,&
                                  dimension%msh,vr(:,0,:,:),f,g)

411 412 413 414 415 416 417
       !
       !--->   generate the extra wavefunctions for the local orbitals,
       !--->   if there are any.
       !
       IF ( atoms%nlo(n) > 0 ) THEN
          DO ispin = jsp_start,jsp_end
             CALL radflo(atoms,n,ispin, ello(1,1,ispin),vr(:,0,n,ispin), f(1,1,0,ispin),&
418
                  g(1,1,0,ispin),mpi, usdus, uuilon,duilon,ulouilopn, flo(:,:,:,ispin))
419 420 421 422 423 424
          END DO
       END IF

       DO ilo = 1, atoms%nlo(n)
          IF (noco%l_mperp) THEN
             CALL int_21lo(f,g,atoms,n, flo,ilo,&
425 426 427
                  lo21(ilo,n)%uulon,lo21(ilo,n)%dulon,&
                  lo21(ilo,n)%uloun,lo21(ilo,n)%ulodn,&
                  uloulopn21(1,1,n))
428 429 430 431 432 433 434
          END IF
       END DO

       na = na + atoms%neq(n)
    END DO
    DEALLOCATE (flo)
8001 FORMAT (1x,/,/,' wavefunction parameters for atom type',i3,':',/,&
435 436 437 438
         t32,'radial function',t79,'energy derivative',/,t3,'l',t8,&
         'energy',t26,'value',t39,'derivative',t53,'nodes',t68,&
         'value',t81,'derivative',t95,'nodes',t107,'norm',t119,&
         'wronskian')
439 440 441
8002 FORMAT (i3,f10.5,2 (5x,1p,2e16.7,i5),1p,2e16.7)

    IF (input%film) vz0(:) = vz(vacuum%nmz,:)
442
    nsld=1
443 444 445 446
    !+q_sl
    IF ((banddos%ndir.EQ.-3).AND.banddos%dos) THEN
       IF (oneD%odi%d1)  CALL juDFT_error("layer-resolved feature does not work with 1D",calledby ="cdnval")
       CALL slab_dim(atoms, nsld)
Daniel Wortmann's avatar
Daniel Wortmann committed
447
       ALLOCATE ( nmtsl(atoms%ntype,nsld),nslat(atoms%nat,nsld) )
448 449 450
       ALLOCATE ( zsl(2,nsld),volsl(nsld) )
       ALLOCATE ( volintsl(nsld) )
       CALL slabgeom(&
451 452
            atoms,cell,nsld,&
            nsl,zsl,nmtsl,nslat,volsl,volintsl)
453 454 455

       ALLOCATE ( qintsl(nsld,dimension%neigd))
       ALLOCATE ( qmtsl(nsld,dimension%neigd))
Daniel Wortmann's avatar
Daniel Wortmann committed
456 457
       ALLOCATE ( orbcomp(dimension%neigd,23,atoms%nat) )
       ALLOCATE ( qmtp(dimension%neigd,atoms%nat) )
458
       IF (.NOT.input%film) qvac(:,:,:,jspin) = 0.0
459 460 461
    ELSE
       ALLOCATE(nmtsl(1,1),nslat(1,1),zsl(1,1),volsl(1),volintsl(1))
       ALLOCATE(qintsl(1,1),qmtsl(1,1),orbcomp(1,1,1),qmtp(1,1))
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    END IF
    !-q_sl
    !
    !-->   loop over k-points: each can be a separate task
    !
    IF (kpts%nkpt < mpi%isize) THEN
       l_evp = .true.
       IF (l_mcd) THEN
          mcd(:,:,:) = 0.0
       ENDIF
       ener(:,:,:) = 0.0
       sqal(:,:,:) = 0.0
       qal(:,:,:,:) = 0.0
       enerlo(:,:,:) = 0.0
       sqlo(:,:,:) = 0.0
    ELSE
       l_evp = .false.
    END IF
    ALLOCATE ( we(dimension%neigd) )
    i_rec = 0 ; n_rank = 0
482 483 484 485 486 487 488 489 490

    ! For k-point paralelization: 
    ! the number of iterations is adjusted to the number of MPI processes to synchronize RMA operations
    if (l_evp) then
       nkpt_extended = kpts%nkpt
    else
       nkpt_extended = (kpts%nkpt / mpi%isize + 1) * mpi%isize
    endif
    DO ikpt = 1,nkpt_extended
491 492
       i_rec = i_rec + 1
       IF ((mod(i_rec-1,mpi%isize).EQ.mpi%irank).OR.l_evp) THEN
493
        IF ( ikpt < kpts%nkpt + 1) THEN
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
          !-t3e
          we=0.0
          !--->    determine number of occupied bands and set weights (we)
          noccbd = 0
          DO i = 1,dimension%neigd ! nbands
             we(i) = results%w_iks(n_bands(n_rank)+i,ikpt,jspin)
             IF (noco%l_noco) we(i) = results%w_iks(i,ikpt,1)
             IF ((we(i).GE.1.e-8).OR.input%pallst) THEN
                noccbd = noccbd + 1
             ELSE
                we(i)=0.0
             END IF
          END DO
          ! uncomment this so that cdinf plots works for all states
          ! noccbd = neigd
509 510

          !
511
          ! -> Gu test: distribute ev's among the processors...
512
          !
Daniel Wortmann's avatar
Daniel Wortmann committed
513
          CALL lapw%init(input,noco, kpts,atoms,sym,ikpt,cell,.false., mpi)
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
          skip_t = skip_tt
          IF (l_evp.AND.(mpi%isize.GT.1)) THEN
             IF (banddos%dos) THEN
                noccbd_l = CEILING( real(n_bands(1)) / mpi%isize )
                n_start = mpi%irank*noccbd_l + 1
                n_end   = min( (mpi%irank+1)*noccbd_l , n_bands(1) )
             ELSE
                noccbd_l = CEILING( real(noccbd) / mpi%isize )
                n_start = mpi%irank*noccbd_l + 1
                n_end   = min( (mpi%irank+1)*noccbd_l , noccbd )
             END IF
             noccbd = n_end - n_start + 1
             IF (noccbd<1) THEN
                noccbd=0
             ELSE
                we(1:noccbd) = we(n_start:n_end)
             END IF
             IF (n_start > skip_tt) THEN
                skip_t  = 0
             END IF
             IF (n_end <= skip_tt) THEN
                skip_t  = noccbd
             END IF
             IF ((n_start <= skip_tt).AND.(n_end > skip_tt)) THEN
                skip_t  = mod(skip_tt,noccbd)
             END IF
540
          ELSE
541 542 543 544 545 546 547 548 549 550
             n_start = 1
             IF (banddos%dos) THEN
                noccbd_l = n_bands(1)
                n_end    = n_bands(1)
                noccbd   = n_bands(1)
             ELSE
                noccbd_l = noccbd
                n_end    = noccbd
             END IF
          END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
551 552
          zMat%nbasfcn=lapw%nv(1)+atoms%nlotot
          IF (noco%l_noco) zMat%nbasfcn=zMat%nbasfcn+lapw%nv(2)+atoms%nlotot
553
          IF (zmat%l_real) THEN
554
             IF (.NOT.ALLOCATED(zMat%z_r)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
555
                ALLOCATE (zMat%z_r(zmat%nbasfcn,dimension%neigd))
556 557 558
                zMat%nbands = dimension%neigd
             END IF
             zMat%z_r = 0
559
          ELSE
560
             IF (.NOT.ALLOCATED(zMat%z_c)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
561
                ALLOCATE (zMat%z_c(zmat%nbasfcn,dimension%neigd))
562 563 564
                zMat%nbands = dimension%neigd
             END IF
             zMat%z_c = 0
565 566
          endif
          CALL cdn_read(&
567
               eig_id,dimension%nvd,dimension%jspd,mpi%irank,mpi%isize,&
Daniel Wortmann's avatar
Daniel Wortmann committed
568
               ikpt,jspin,zmat%nbasfcn,noco%l_ss,noco%l_noco,&
569
               noccbd,n_start,n_end,&
Daniel Wortmann's avatar
Daniel Wortmann committed
570
               ello,evdu,epar,&
571
               wk,nbands,eig,zMat)
572
#ifdef CPP_MPI
Daniel Wortmann's avatar
Daniel Wortmann committed
573 574
          ! Sinchronizes the RMA operations
          if (l_evp) CALL MPI_BARRIER(mpi%mpi_comm,ie)
575
#endif
576 577 578 579 580 581
          !IF (l_evp.AND.(isize.GT.1)) THEN
          !  eig(1:noccbd) = eig(n_start:n_end)
          !ENDIF
          !
          IF (vacuum%nstm.EQ.3.AND.input%film) THEN
             CALL nstm3(&
582 583 584 585 586
                  sym,atoms,vacuum,stars,ikpt,lapw%nv(jspin),&
                  input,jspin,kpts,&
                  cell,wk,lapw%k1(:,jspin),lapw%k2(:,jspin),&
                  evac(1,jspin),vz,vz0,&
                  gvac1d,gvac2d)
587
          END IF
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
          IF (noccbd.EQ.0) GO TO 199
          !
          !--->    if slice, only a certain bands are taken into account
          !--->    in order to do this the coresponding eigenvalues, eigenvectors
          !--->    and weights have to be copied to the beginning of the arrays
          !--->    eig, z and we and the number of occupied bands (noccbd) has to
          !--->    changed
          IF (sliceplot%slice) THEN
             IF (mpi%irank==0) WRITE (16,FMT=*) 'NNNE',sliceplot%nnne
             IF (mpi%irank==0) WRITE (16,FMT=*) 'sliceplot%kk',sliceplot%kk
             nslibd = 0
             IF (input%pallst) we(:nbands) = wk
             IF (sliceplot%kk.EQ.0) THEN
                IF (mpi%irank==0) THEN
                   WRITE (16,FMT='(a)') 'ALL K-POINTS ARE TAKEN IN SLICE'
                   WRITE (16,FMT='(a,i2)') ' sliceplot%slice: k-point nr.',ikpt
                END IF
                DO i = 1,nbands
                   IF (eig(i).GE.sliceplot%e1s .AND. eig(i).LE.sliceplot%e2s) THEN
                      nslibd = nslibd + 1
                      eig(nslibd) = eig(i)
                      we(nslibd) = we(i)
611
                      if (zmat%l_real) THEN
612
                         zMat%z_r(:,nslibd) = zMat%z_r(:,i)
613
                      else
614
                         zMat%z_c(:,nslibd) = zMat%z_c(:,i)
615
                      endif
616 617 618 619 620 621 622
                   END IF
                END DO
                IF (mpi%irank==0) WRITE (16,'(a,i3)') ' eigenvalues in sliceplot%slice:',nslibd
             ELSE IF (sliceplot%kk.EQ.ikpt) THEN
                IF (mpi%irank==0) WRITE (16,FMT='(a,i2)') ' sliceplot%slice: k-point nr.',ikpt
                IF ((sliceplot%e1s.EQ.0.0) .AND. (sliceplot%e2s.EQ.0.0)) THEN
                   IF (mpi%irank==0) WRITE (16,FMT='(a,i5,f10.5)') 'slice: eigenvalue nr.',&
623
                        sliceplot%nnne,eig(sliceplot%nnne)
624 625 626
                   nslibd = nslibd + 1
                   eig(nslibd) = eig(sliceplot%nnne)
                   we(nslibd) = we(sliceplot%nnne)
627
                   if (zmat%l_real) Then
628
                      zMat%z_r(:,nslibd) = zMat%z_r(:,sliceplot%nnne)
629
                   else
630
                      zMat%z_c(:,nslibd) = zMat%z_c(:,sliceplot%nnne)
631
                   endif
632 633 634
                ELSE
                   DO i = 1,nbands
                      IF (eig(i).GE.sliceplot%e1s .AND. eig(i).LE.sliceplot%e2s) THEN
635
                         nslibd = nslibd + 1
636 637
                         eig(nslibd) = eig(i)
                         we(nslibd) = we(i)
638
                         if (zmat%l_real) THEN
639
                            zMat%z_r(:,nslibd) = zMat%z_r(:,i)
640
                         else
641
                            zMat%z_c(:,nslibd) = zMat%z_c(:,i)
642
                         endif
643
                      END IF
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
                   END DO
                   IF (mpi%irank==0) WRITE (16,FMT='(a,i3)')' eigenvalues in sliceplot%slice:',nslibd
                END IF
             END IF
             noccbd = nslibd
             IF (nslibd.EQ.0) GO TO 199 !200
          END IF ! sliceplot%slice

          !--->    in normal iterations the charge density of the unoccupied
          !--->    does not need to be calculated (in pwden, vacden and abcof)
          IF (banddos%dos.AND. .NOT.(l_evp.AND.(mpi%isize.GT.1)) ) THEN
             noccbd=nbands
          END IF
          !     ----> add in spin-doubling factor
          we(:noccbd) = 2.*we(:noccbd)/input%jspins

          !---> pk non-collinear
          !--->    valence density in the interstitial and vacuum region
          !--->    has to be called only once (if jspin=1) in the non-collinear
          !--->    case
          !     ----> valence density in the interstitial region
          IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
             CALL timestart("cdnval: pwden")
667
             CALL pwden(stars,kpts,banddos,oneD, input,mpi,noco,cell,atoms,sym,ikpt,&
Daniel Wortmann's avatar
Daniel Wortmann committed
668
                  jspin,lapw,noccbd,igq_fft,we, eig,qpw,cdom,qis,results%force,f_b8,zMat)
669 670 671 672 673 674
             CALL timestop("cdnval: pwden")
          END IF
          !+new
          !--->    charge of each valence state in this k-point of the SBZ
          !--->    in the layer interstitial region of the film
          !
675
          IF (banddos%dos.AND.(banddos%ndir.EQ.-3)) THEN
676
             IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
677
                CALL q_int_sl(jspin,stars,atoms,sym, volsl,volintsl,&
678
                     cell,noccbd,lapw, nsl,zsl,nmtsl,oneD, qintsl(:,:),zMat)
679 680
               
      !
681 682 683 684 685 686 687
             END IF
          END IF
          !-new c
          !--->    valence density in the vacuum region
          IF (input%film) THEN
             IF (.NOT.((jspin.EQ.2) .AND. noco%l_noco)) THEN
                CALL timestart("cdnval: vacden")
688
                CALL vacden(vacuum,dimension,stars,oneD, kpts,input, cell,atoms,noco,banddos,&
Daniel Wortmann's avatar
Daniel Wortmann committed
689
                        gvac1d,gvac2d, we,ikpt,jspin,vz,vz0, noccbd,lapw, evac,eig,&
690
                        rhtxy,rht,qvac,qvlay, qstars,cdomvz,cdomvxy,zMat)
691 692 693 694 695 696 697 698 699 700 701
                CALL timestop("cdnval: vacden")
             END IF
             !--->       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
                   pvac(ivac,ispin)=pvac(ivac,ispin)+dot_product(eig(:noccbd)*qvac(:noccbd,ivac,ikpt,ispin),we(:noccbd))
                   svac(ivac,ispin)=svac(ivac,ispin)+dot_product(qvac(:noccbd,ivac,ikpt,ispin),we(:noccbd))
                END DO
             END DO
          END IF
702

703 704 705
          !--->    valence density in the atomic spheres
          !--->    construct a(tilta) and b(tilta)
          IF (noco%l_mperp) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
706
             ALLOCATE ( acof(noccbd,0:dimension%lmd,atoms%nat,dimension%jspd),&
707
                                ! Deallocated before call to sympsi
Daniel Wortmann's avatar
Daniel Wortmann committed
708 709
                  bcof(noccbd,0:dimension%lmd,atoms%nat,dimension%jspd),                &
                  ccof(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat,dimension%jspd) )
710
          ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
711 712 713
             ALLOCATE ( acof(noccbd,0:dimension%lmd,atoms%nat,jspin:jspin),&
                  bcof(noccbd,0:dimension%lmd,atoms%nat,jspin:jspin),&
                  ccof(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat,jspin:jspin) )
714 715 716 717 718
          END IF

          DO ispin = jsp_start,jsp_end
             IF (input%l_f) THEN
                CALL timestart("cdnval: to_pulay")
Daniel Wortmann's avatar
Daniel Wortmann committed
719
                ALLOCATE (e1cof(noccbd,0:atoms%lmaxd*(atoms%lmaxd+2),atoms%nat),&
720
                                ! Deallocated after call to force_a21
Daniel Wortmann's avatar
Daniel Wortmann committed
721 722 723 724 725 726
                     e2cof(noccbd,0:atoms%lmaxd*(atoms%lmaxd+2),atoms%nat),&
                     acoflo(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat),&
                     bcoflo(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat),&
                     aveccof(3,noccbd,0:atoms%lmaxd*(atoms%lmaxd+2),atoms%nat),&
                     bveccof(3,noccbd,0:atoms%lmaxd*(atoms%lmaxd+2),atoms%nat),&
                     cveccof(3,-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat) )
727 728 729 730 731 732
                CALL abcof(input,atoms,sym, cell,lapw,noccbd,usdus, noco,ispin,oneD,&
                     acof(:,0:,:,ispin),bcof(:,0:,:,ispin),ccof(-atoms%llod:,:,:,:,ispin),zMat,&
                     eig,acoflo,bcoflo,e1cof,e2cof,aveccof,bveccof,cveccof)
!                CALL to_pulay(input,atoms,noccbd,sym, lapw, noco,cell,noccbd,eig,usdus,&
!                        ispin,oneD, acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
!                        e1cof,e2cof,aveccof,bveccof, ccof(-atoms%llod,1,1,1,ispin),acoflo,bcoflo,cveccof,zMat)
733 734 735 736
                CALL timestop("cdnval: to_pulay")

             ELSE
                CALL timestart("cdnval: abcof")
Daniel Wortmann's avatar
Daniel Wortmann committed
737
                CALL abcof(input,atoms,sym, cell,lapw,noccbd,usdus, noco,ispin,oneD,&
738
                     acof(:,0:,:,ispin),bcof(:,0:,:,ispin),ccof(-atoms%llod:,:,:,:,ispin),zMat)
739
                CALL timestop("cdnval: abcof")
740

741
             END IF
742

743 744
             IF (atoms%n_u.GT.0) THEN
                CALL n_mat(atoms,sym,noccbd,usdus,ispin,we, acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
745
                     ccof(-atoms%llod:,:,:,:,ispin), n_mmp)
746 747 748 749 750 751 752 753
             END IF
             !
             !--->       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,ccof,&
754 755 756 757 758
                     skip_t,l_evp,acof(:,0:,:,ispin),bcof(:,0:,:,ispin),usdus,&
                     ncore,l_mcd,m_mcd,&
                     enerlo(1,1,ispin),sqlo(1,1,ispin),&
                     ener(0,1,ispin),sqal(0,1,ispin),&
                     qal(0:,:,:,ispin),mcd)
759 760 761

                IF (noco%l_mperp.AND.(ispin == jsp_end)) THEN
                   CALL qal_21(atoms, input,noccbd,we,ccof,&
762 763
                        noco,acof,bcof,mt21,lo21,uloulopn21,&
                        qal,qmat)
764 765 766 767 768 769 770 771 772
                END IF
             END IF
             !
             !+new
             !--->    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,nsld, ikpt,noccbd,ccof(-atoms%llod,1,1,1,ispin),&
773 774
                     skip_t,noccbd, acof(:,0:,:,ispin),bcof(:,0:,:,ispin),usdus,&
                     nmtsl,nsl, qmtsl(:,:))
775 776 777 778

                INQUIRE (file='orbcomprot',exist=l_orbcomprot)
                IF (l_orbcomprot) THEN                           ! rotate ab-coeffs
                   CALL abcrot2(atoms, noccbd,&
779 780
                        acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
                        ccof(-atoms%llod:,:,:,:,ispin))
781 782 783 784 785 786 787 788 789
                END IF

                CALL orb_comp(ispin,noccbd,atoms,noccbd,usdus,acof(1:,0:,1:,ispin),bcof(1:,0:,1:,ispin),&
                     ccof(-atoms%llod:,1:,1:,1:,ispin), orbcomp, qmtp)
             END IF
             !-new
             !--->          set up coefficients for the spherical and
             CALL timestart("cdnval: rhomt")
             CALL rhomt(atoms,we,noccbd, acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
790
                  uu(0:,:,ispin),dd(0:,:,ispin),du(0:,:,ispin))
791 792 793 794
             CALL timestop("cdnval: rhomt")
             !+soc
             IF (noco%l_soc) THEN
                CALL orbmom(atoms,noccbd, we,acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
795 796
                     ccof(-atoms%llod:,:,:,:,ispin), orb(0:,-atoms%lmaxd:,:,ispin),orbl(:,-atoms%llod:,:,ispin),&
                     orblo(:,:,-atoms%llod:,:,ispin) )
797 798 799 800 801
             END IF
             !     -soc
             !--->          non-spherical m.t. density
             CALL timestart("cdnval: rhonmt")
             CALL rhonmt(atoms,sphhar, we,noccbd,sym, acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
802
                  uunmt(0:,:,:,ispin),ddnmt(0:,:,:,ispin), udnmt(0:,:,:,ispin),dunmt(0:,:,:,ispin))
803 804 805 806 807 808 809
             CALL timestop("cdnval: rhonmt")

             !--->          set up coefficients of the local orbitals and the
             !--->          flapw - lo cross terms for the spherical and
             !--->          non-spherical mt density
             CALL timestart("cdnval: rho(n)mtlo")
             CALL rhomtlo(atoms,&
810 811 812 813
                  noccbd,we,acof(:,0:,:,ispin),bcof(:,0:,:,ispin),&
                  ccof(-atoms%llod:,:,:,:,ispin),&
                  aclo(:,:,ispin),bclo(:,:,ispin),cclo(:,:,:,ispin))
             !
814
             CALL rhonmtlo(&
815 816 817 818 819
                  atoms,sphhar,&
                  noccbd,we,acof(:,0:,:,ispin),&
                  bcof(:,0:,:,ispin),ccof(-atoms%llod:,:,:,:,ispin),&
                  acnmt(0:,:,:,:,ispin),bcnmt(0:,:,:,:,ispin),&
                  ccnmt(:,:,:,:,ispin))
820 821 822 823
             CALL timestop("cdnval: rho(n)mtlo")

             IF (input%l_f) THEN
                CALL timestart("cdnval: force_a12/21")
824 825 826 827 828 829 830 831 832
                IF (.not.input%l_useapw) THEN
                   CALL force_a12(atoms,noccbd,sym, dimension,cell,oneD,&
                        we,ispin,noccbd,usdus,acof(:,0:,:,ispin),&
                        bcof(:,0:,:,ispin),e1cof,e2cof, acoflo,bcoflo, results,f_a12)
                ENDIF
                CALL force_a21(input,atoms,dimension,noccbd,sym,&
                     oneD,cell,we,ispin,epar(0:,:,ispin),noccbd,eig,usdus,acof(:,0:,:,ispin),&
                     bcof(:,0:,:,ispin),ccof(-atoms%llod:,:,:,:,ispin), aveccof,bveccof,cveccof,&
                     results,f_a21,f_b4)
833 834 835 836 837

                DEALLOCATE (e1cof,e2cof,aveccof,bveccof)
                DEALLOCATE (acoflo,bcoflo,cveccof)
                CALL timestop("cdnval: force_a12/21")
             END IF
838 839 840 841 842 843 844

             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,acof(1,0,1,ispin),bcof(1,0,1,ispin),&
                                  ccof(-atoms%llod,1,1,1,ispin))
             END IF
845 846 847 848
          END DO !--->    end loop over ispin

          IF (noco%l_mperp) THEN
             CALL rhomt21(atoms, we,noccbd,acof,bcof, ccof,&
849
                  mt21,lo21,uloulop21)
850 851
             IF (l_fmpl) THEN
                CALL rhonmt21(atoms,llpd,sphhar, we,noccbd,sym, acof,bcof,&
852
                     uunmt21,ddnmt21,udnmt21,dunmt21)
853 854 855 856
             END IF
          END IF

          DEALLOCATE (acof,bcof,ccof)
857
          !
858 859 860 861 862 863 864 865 866 867 868 869 870
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
             !

             !--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!
             !
Daniel Wortmann's avatar
Daniel Wortmann committed
871
             cartk=matmul(lapw%bkpt,cell%bmat)
872
             IF (banddos%ndir.GT.0) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
873
                CALL sympsi(lapw%bkpt,lapw%nv(jspin),lapw%k1(:,jspin),lapw%k2(:,jspin),&
874
                     lapw%k3(:,jspin),sym,dimension,nbands,cell,eig,noco, ksym,jsym,zMat)
875 876 877 878 879
             END IF
             !
             !--dw   now write k-point data to tmp_dos
             !
             CALL write_dos(eig_id,ikpt,jspin,qal(:,:,:,jspin),qvac(:,:,ikpt,jspin),qis(:,ikpt,jspin),&
880 881 882
                  qvlay(:,:,:,ikpt,jspin),qstars,ksym,jsym,mcd,qintsl,&
                  qmtsl(:,:),qmtp(:,:),orbcomp)

883 884 885 886 887
             CALL timestop("cdnval: write_info")
             !-new_sl
          END IF

          !--->  end of loop over PE's
888
          IF (zmat%l_real) THEN
889 890 891 892
             DEALLOCATE (zMat%z_r)
          ELSE
             DEALLOCATE (zMat%z_c)
          END IF
893 894 895 896 897 898
        ELSE !(ikpt < nkpt + 1)
#ifdef CPP_MPI
          ! Synchronizes the RMA operations
          CALL MPI_BARRIER(mpi%mpi_comm,ie)
#endif
        END IF
899 900 901 902
       END IF ! --> end "IF ((mod(i_rec-1,mpi%isize).EQ.mpi%irank).OR.l_evp) THEN"
    END DO !---> end of k-point loop
    DEALLOCATE (we,f,g,usdus%us,usdus%dus,usdus%duds,usdus%uds,usdus%ddn)
    !+t3e
903
#ifdef CPP_MPI
904 905 906
    CALL timestart("cdnval: mpi_col_den")
    DO ispin = jsp_start,jsp_end
       CALL mpi_col_den(mpi,sphhar,atoms,oneD,stars,vacuum,&
907 908 909 910 911 912 913 914 915 916 917
            input,noco,l_fmpl,ispin,llpd, rhtxy(1,1,1,ispin),&
            rht(1,1,ispin),qpw(1,ispin), ener(0,1,ispin),sqal(0,1,ispin),&
            results,svac(1,ispin),pvac(1,ispin),uu(0,1,ispin),&
            dd(0,1,ispin),du(0,1,ispin),uunmt(0,1,1,ispin),ddnmt(0,1,1,ispin),&
            udnmt(0,1,1,ispin),dunmt(0,1,1,ispin),sqlo(1,1,ispin),&
            aclo(1,1,ispin),bclo(1,1,ispin),cclo(1,1,1,ispin),&
            acnmt(0,1,1,1,ispin),bcnmt(0,1,1,1,ispin),&
            ccnmt(1,1,1,1,ispin),enerlo(1,1,ispin),&
            orb(0,-atoms%lmaxd,1,ispin),orbl(1,-atoms%llod,1,ispin),&
            orblo(1,1,-atoms%llod,1,ispin),mt21,lo21,uloulop21,&
            uunmt21,ddnmt21,udnmt21,dunmt21,cdom,cdomvz,cdomvxy,n_mmp)
918 919
    END DO
    CALL timestop("cdnval: mpi_col_den")
920
#endif
921 922 923

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

924 925 926 927
    IF (((jspin.eq.input%jspins).OR.noco%l_mperp) .AND. (banddos%dos.or.banddos%vacdos.or.input%cdinf) ) THEN
       CALL timestart("cdnval: dos")
       IF (mpi%irank==0) THEN
          CALL doswrite(&
928 929 930 931 932
               eig_id,dimension,kpts,atoms,vacuum,&
               input,banddos,&
               sliceplot,noco,sym,&
               cell,&
               l_mcd,ncored,ncore,e_mcd,&
933
               results%ef,results%bandgap,nsld,oneD)
934 935
          IF (banddos%dos.AND.(banddos%ndir.EQ.-3)) THEN
             CALL Ek_write_sl(&
936 937 938 939
                  eig_id,dimension,kpts,atoms,vacuum,&
                  nsld,input,jspin,&
                  sym,cell,&
                  nsl,nslat)
940 941
          END IF
       END IF
942
#ifdef CPP_MPI                
943
       CALL MPI_BARRIER(mpi%mpi_comm,ie)
944
#endif
945 946 947 948 949
       CALL timestop("cdnval: dos")
    END IF

    IF (mpi%irank==0) THEN
       CALL cdnmt(&
950 951 952 953 954 955 956 957
            dimension%jspd,atoms,sphhar,llpd,&
            noco,l_fmpl,jsp_start,jsp_end,&
            epar,ello,vr(:,0,:,:),uu,du,dd,uunmt,udnmt,dunmt,ddnmt,&
            usdus,usdus%uloulopn,aclo,bclo,cclo,acnmt,bcnmt,ccnmt,&
            orb,orbl,orblo,mt21,lo21,uloulopn21,uloulop21,&
            uunmt21,ddnmt21,udnmt21,dunmt21,&
            chmom,clmom,&
            qa21,rho)
958 959 960 961 962

       DO ispin = jsp_start,jsp_end
          WRITE (6,*) 'Energy Parameters for spin:',ispin
          IF (.not.sliceplot%slice) THEN
             CALL mix_enpara(&
963 964 965 966 967 968 969
                  ispin,atoms,vacuum,obsolete,input,&
                  enpara,&
                  vr(:,0,:,:),vz,pvac(1,ispin),&
                  svac(1,ispin),&
                  ener(0,1,ispin),sqal(0,1,ispin),&
                  enerlo(1,1,ispin),&
                  sqlo(1,1,ispin))
970

971
             CALL w_enpara(&
972 973
                  atoms,jspin,input%film,&
                  enpara,16)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
          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)
             IF (input%film .AND. .NOT.oneD%odi%d1) THEN
                !--->             vacuum boundaries
                npd = min(dimension%nspd,25)
                CALL points(xp,npd)
                DO ivac = 1,vacuum%nvac
                   sign = 3. - 2.*ivac
                   DO j = 1,npd
                      xp(3,j) = sign*cell%z1/cell%amat(3,3)
                   END DO
                   CALL checkdop(&
992 993 994 995
                        xp,npd,0,0,ivac,1,ispin,.true.,dimension,atoms,&
                        sphhar,stars,sym,&
                        vacuum,cell,oneD,&
                        qpw,rho,rhtxy,rht)
996 997 998 999 1000 1001
                END DO
             ELSE IF (oneD%odi%d1) THEN
                !-odim
                npd = min(dimension%nspd,25)
                CALL cylpts(xp,npd,cell%z1)
                CALL checkdop(&
1002 1003 1004 1005
                     xp,npd,0,0,ivac,1,ispin,.true.,dimension,atoms,&
                     sphhar,stars,sym,&
                     vacuum,cell,oneD,&
                     qpw,rho,rhtxy,rht)
1006 1007 1008 1009 1010 1011 1012
                !+odim
             END IF
             !--->          m.t. boundaries
             nat = 1
             DO n = 1, atoms%ntype
                CALL sphpts(xp,dimension%nspd,atoms%rmt(n),atoms%pos(1,atoms%nat))
                CALL checkdop(&
1013 1014 1015 1016
                     xp,dimension%nspd,n,nat,0,-1,ispin,.true.,&
                     dimension,atoms,sphhar,stars,sym,&
                     vacuum,cell,oneD,&
                     qpw,rho,rhtxy,rht)
1017 1018 1019 1020 1021 1022 1023 1024 1025
                nat = nat + atoms%neq(n)
             END DO
             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")
Daniel Wortmann's avatar
Daniel Wortmann committed
1026
             CALL force_a8(input,atoms,sphhar, ispin, vr(:,:,:,ispin),rho,&
1027
                  f_a12,f_a21,f_b4,f_b8,results%force)
1028 1029
             CALL timestop("cdnval: force_a8")
          END IF
1030
          !-for
1031
       END DO ! end of loop ispin = jsp_start,jsp_end
1032
       CALL closeXMLElement('mtCharges')
1033 1034 1035
    END IF ! end of (mpi%irank==0)
    !+t3e
    !Note: no deallocation anymore, we rely on Fortran08 :-)
1036

1037 1038 1039 1040 1041 1042
    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
  END SUBROUTINE cdnval
END MODULE m_cdnval