stden.f90 15 KB
Newer Older
1 2 3
MODULE m_stden

USE m_juDFT
4 5 6 7 8 9 10 11
!     ************************************************************
!     generate flapw starting density by superposition of
!     atomic densities. the non-spherical terms inside
!     the spheres are obtained by a least squares fit
!     and the interstitial and vacuum warping terms are calculated
!     by a fast fourier transform.
!     e. wimmer   nov. 1984       c.l.fu diagonized 1987
!     *************************************************************
12 13 14 15

CONTAINS

SUBROUTINE stden(mpi,sphhar,stars,atoms,sym,DIMENSION,vacuum,&
16
                 input,cell,xcpot,obsolete,noco,oneD)
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

   USE m_sphpts
   USE m_constants
   USE m_enpara,    ONLY : w_enpara
   USE m_xcall,     ONLY : vxcall
   USE m_qsf
   USE m_checkdop
   USE m_cdnovlp
   USE m_cdn_io
   USE m_qfix
   USE m_atom2
   USE m_types
   USE m_cylpts
   USE m_points
   USE m_juDFT_init

   IMPLICIT NONE

   TYPE(t_mpi),INTENT(IN)      :: mpi
   TYPE(t_atoms),INTENT(IN)    :: atoms
   TYPE(t_dimension),INTENT(IN):: DIMENSION
   TYPE(t_sphhar),INTENT(IN)   :: sphhar
   TYPE(t_obsolete),INTENT(IN) :: obsolete
   TYPE(t_sym),INTENT(IN)      :: sym
   TYPE(t_stars),INTENT(IN)    :: stars
42
   TYPE(t_noco),INTENT(IN)     :: noco
43 44 45 46 47 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
   TYPE(t_oneD),INTENT(IN)     :: oneD
   TYPE(t_input),INTENT(IN)    :: input
   TYPE(t_vacuum),INTENT(IN)   :: vacuum
   TYPE(t_cell),INTENT(IN)     :: cell
   TYPE(t_xcpot),INTENT(IN)    :: xcpot

   ! Local type instances
   TYPE(t_potden)   :: den
   TYPE(t_enpara)   :: enpara
   TYPE(t_xcpot)    :: xcpot_dummy

   ! Local Scalars
   REAL d,del,fix,h,r,rnot,sign,z,bm,qdel
   REAL denz1(1),vacxpot(1),vacpot(1) 
   INTEGER i,ivac,iza,j,jr,k,n,n1,npd,ispin 
   INTEGER nw,ilo,natot,nat 

   ! Local Arrays
   REAL,    ALLOCATABLE :: vbar(:,:)
   REAL,    ALLOCATABLE :: xp(:,:),rat(:,:),eig(:,:,:),sigm(:)
   REAL,    ALLOCATABLE :: rh(:,:,:),rh1(:,:,:),rhoss(:,:)
   REAL,    ALLOCATABLE :: vacpar(:)
   INTEGER lnum(DIMENSION%nstd,atoms%ntype),nst(atoms%ntype) 
   INTEGER jrc(atoms%ntype)
   LOGICAL l_found(0:3),llo_found(atoms%nlod),l_enpara,l_st

   ! Data statements
   DATA del/1.e-6/
   PARAMETER (l_st=.true.)

   IF (input%jspins > DIMENSION%jspd) CALL juDFT_error("input%jspins > dimension%jspd", calledby = "stden")

75
   CALL den%init(stars,atoms,sphhar,vacuum,noco,oneD,input%jspins,.FALSE.,POTDEN_TYPE_DEN)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

   ALLOCATE ( xp(3,DIMENSION%nspd),rat(DIMENSION%msh,atoms%ntype),eig(DIMENSION%nstd,DIMENSION%jspd,atoms%ntype) )
   ALLOCATE ( rh(DIMENSION%msh,atoms%ntype,DIMENSION%jspd),rh1(DIMENSION%msh,atoms%ntype,DIMENSION%jspd) )
   ALLOCATE ( enpara%ello0(atoms%nlod,atoms%ntype,input%jspins),vacpar(2) )
   ALLOCATE ( enpara%el0(0:3,atoms%ntype,input%jspins))   
   ALLOCATE ( enpara%lchange(0:3,atoms%ntype,input%jspins))
   ALLOCATE ( enpara%skiplo(atoms%ntype,input%jspins))
   ALLOCATE ( enpara%llochg(atoms%nlod,atoms%ntype,input%jspins))
   ALLOCATE ( enpara%enmix(input%jspins))
   ALLOCATE ( enpara%evac0(2,dimension%jspd))
   ALLOCATE ( enpara%lchg_v(2,dimension%jspd))
   ALLOCATE ( vbar(2,atoms%ntype),sigm(vacuum%nmzd) )
   ALLOCATE ( rhoss(DIMENSION%msh,DIMENSION%jspd) )

   enpara%enmix=1.0

   IF (mpi%irank == 0) THEN
      ! if sigma is not 0.0, then divide this charge among all atoms
      IF ( ABS(input%efield%sigma).LT. 1.e-6) THEN
         qdel = 0.0
      ELSE
         natot = 0
         DO n = 1, atoms%ntype
            IF (atoms%zatom(n).GE.1.0) natot = natot + atoms%neq(n)
         END DO
         qdel = 2.*input%efield%sigma/natot
      END IF

      WRITE (6,FMT=8000)
      8000 FORMAT (/,/,/,' superposition of atomic densities',/,/,' original atomic densities:',/)
      DO n = 1,atoms%ntype
         r = atoms%rmsh(1,n)
         d = EXP(atoms%dx(n))
         jrc(n) = 0
         DO WHILE (r < atoms%rmt(n) + 20.0) 
            IF (jrc(n) > DIMENSION%msh) CALL juDFT_error("increase msh in fl7para!",calledby ="stden")
            jrc(n) = jrc(n) + 1
            rat(jrc(n),n) = r
            r = r*d
         END DO
      END DO

      ! Generate the atomic charge densities
      DO n = 1,atoms%ntype
         z = atoms%zatom(n)
         r = atoms%rmt(n)
         h = atoms%dx(n)
         jr = atoms%jri(n)
         IF (input%jspins.EQ.2) THEN
            bm = atoms%bmu(n)
         ELSE
            bm = 0.
         END IF
         ! check whether this atom has been done already
         DO n1 = 1, n - 1
            IF (ABS(z-atoms%zatom(n1)).GT.del) CYCLE
            IF (ABS(r-atoms%rmt(n1)).GT.del) CYCLE
            IF (ABS(h-atoms%dx(n1)).GT.del) CYCLE
            IF (ABS(bm-atoms%bmu(n1)).GT.del) CYCLE
            IF (jr.NE.atoms%jri(n1)) CYCLE
            DO ispin = 1, input%jspins
               DO i = 1,jrc(n) ! dimension%msh
                  rh(i,n,ispin) = rh(i,n1,ispin)
               END DO
            END DO
            nst(n) = nst(n1)
            vbar(1,n) = vbar(1,n1)
            vbar(input%jspins,n) = vbar(input%jspins,n1)
            DO i = 1, nst(n1)
               lnum(i,n)  = lnum(i,n1)
               eig(i,1,n) = eig(i,1,n1)
               eig(i,input%jspins,n) = eig(i,input%jspins,n1)
            END DO
            GO TO 70
         END DO
         !--->    new atom
         rnot = atoms%rmsh(1,n)
         IF (z.LT.1.0) THEN
            DO ispin = 1, input%jspins
               DO i = 1,jrc(n) ! dimension%msh
                  rh(i,n,ispin) = 1.e-10
               END DO
            END DO
         ELSE
            CALL atom2(DIMENSION,atoms,xcpot,input,n,jrc(n),rnot,qdel,&
                       rhoss,nst(n),lnum(1,n),eig(1,1,n),vbar(1,n))
            DO ispin = 1, input%jspins
               DO i = 1, jrc(n) ! dimension%msh
                  rh(i,n,ispin) = rhoss(i,ispin)
               END DO
            END DO
         END IF
         ! list atomic density
         iza = atoms%zatom(n) + 0.0001
         WRITE (6,FMT=8030) namat_const(iza)
         8030 FORMAT (/,/,' atom: ',a2,/)
         8040 FORMAT (4 (3x,i5,f8.5,f12.6))
         70 CONTINUE
      END DO

      !roa+
      ! use cdnovlp to generate total density out of atom densities
      DO ispin = 1, input%jspins
         nat = 1
         DO n = 1,atoms%ntype
            DO i = 1, jrc(n)
               rh1(i,n,ispin) = rh(i,n,ispin)*fpi_const*rat(i,n)**2
            END DO
            rh1(jrc(n):DIMENSION%msh,n,ispin) = 0.0
            ! prepare spherical mt charge
            DO i = 1,atoms%jri(n)
               den%mt(i,0,n,ispin) = rh(i,n,ispin)*sfp_const*atoms%rmsh(i,n)**2
            END DO
            ! reset nonspherical mt charge
            DO k = 1,sphhar%nlh(atoms%ntypsy(nat))
               DO j = 1,atoms%jri(n)
                  den%mt(j,k,n,ispin) = 0.e0
               END DO
            END DO
            nat = nat + atoms%neq(n)
         END DO
      END DO ! ispin
   END IF ! mpi%irank == 0

   DO ispin = 1, input%jspins
      CALL cdnovlp(mpi,sphhar,stars,atoms,sym,DIMENSION,vacuum,&
                   cell,input,oneD,l_st,ispin,rh1(:,:,ispin),&
                   den%pw,den%vacxy,den%mt,den%vacz)
      !roa-
   END DO

   IF (mpi%irank == 0) THEN

      ! Check the normalization of total density
      CALL qfix(stars,atoms,sym,vacuum,sphhar,input,cell,oneD,&
                den%pw,den%vacxy,den%mt,den%vacz,.FALSE.,.true.,fix)
      z=SUM(atoms%neq(:)*atoms%zatom(:))
      IF (ABS(fix*z-z)>0.5) THEN
         CALL judft_warn("Starting density not charge neutral",hint= &
                         "Your electronic configuration might be broken",calledby="stden.f90")
      END IF

      ! Write superposed density onto density file
      den%iter = 0
      CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,CDN_ARCHIVE_TYPE_CDN1_const,CDN_INPUT_DEN_const,&
221
                        1,-1.0,0.0,.TRUE.,den)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

      ! Check continuity
      IF (input%vchk) THEN
         DO ispin = 1, input%jspins
            WRITE (6,'(a8,i2)') 'spin No.',ispin
            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(xp,npd,0,0,ivac,1,ispin,.TRUE.,DIMENSION,atoms,&
                                sphhar,stars,sym,vacuum,cell,oneD,&
                                den%pw,den%mt,den%vacxy,den%vacz)
               END DO
            ELSE IF (oneD%odi%d1) THEN
               !-odim
               npd = MIN(DIMENSION%nspd,25)
               CALL cylpts(xp,npd,cell%z1)
               CALL checkdop(xp,npd,0,0,vacuum%nvac,1,ispin,.TRUE.,DIMENSION,atoms,&
                             sphhar,stars,sym,vacuum,cell,oneD,den%pw,den%mt,den%vacxy,den%vacz)
               !+odim
            END IF
            ! m.t. boundaries
            nat = 1
            DO n = 1,atoms%ntype
               CALL sphpts(xp,DIMENSION%nspd,atoms%rmt(n),atoms%pos(1,nat))
               CALL checkdop(xp,DIMENSION%nspd,n,nat,0,-1,ispin,.TRUE.,&
                             dimension,atoms,sphhar,stars,sym,vacuum,cell,oneD,&
                             den%pw,den%mt,den%vacxy,den%vacz)
               nat = nat + atoms%neq(n)
            END DO
         END DO ! ispin = 1, input%jspins
      END IF ! input%vchk

      l_enpara = .FALSE.
      INQUIRE (file='enpara',exist=l_enpara)
      l_enpara = l_enpara.OR.input%l_inpXML

      ! set up parameters for enpara-file
      IF ((juDFT_was_argument("-genEnpara")).AND..NOT.l_enpara) THEN
         OPEN (40,file='enpara',form='formatted',status='unknown')
         enpara%lchange = .TRUE.
         enpara%llochg = .TRUE.
269
                
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 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
         DO ispin = 1, input%jspins
            ! vacpar is taken as highest occupied level from atomic eigenvalues
            ! vacpar (+0.3)  serves as fermi level for film or bulk
            vacpar(1) = -999.9
            DO n = 1,atoms%ntype
               vacpar(1) = MAX( vacpar(1),eig(nst(n),ispin,n) )
            END DO
            IF (.NOT.input%film) vacpar(1) = vacpar(1) + 0.4
            vacpar(2) = vacpar(1)
            DO n = 1,atoms%ntype
               enpara%skiplo(n,ispin) = 0
               DO i = 0, 3
                  l_found(i) = .FALSE.
                  enpara%el0(i,n,ispin) = vacpar(1)
               END DO
               DO i = 1, atoms%nlo(n)
                  llo_found(i) = .FALSE.
                  enpara%ello0(i,n,ispin) = vacpar(1) - 1.5
               END DO

               ! take energy-parameters from atomic calculation
               DO i = nst(n), 1, -1 
                  IF (.NOT.input%film) eig(i,ispin,n) = eig(i,ispin,n) + 0.4
                  IF (.NOT.l_found(lnum(i,n)).AND.(lnum(i,n).LE.3)) THEN
                     enpara%el0(lnum(i,n),n,ispin) = eig(i,ispin,n)
                     IF (enpara%el0(lnum(i,n),n,ispin).LT.input%ellow) THEN
                        enpara%el0(lnum(i,n),n,ispin) = vacpar(1)
                        l_found(lnum(i,n))  = .TRUE.
                     END IF
                     IF (enpara%el0(lnum(i,n),n,ispin).LT.input%elup) THEN
                        l_found(lnum(i,n))  = .TRUE.
                     END IF
                  ELSE
                     IF (l_found(lnum(i,n)).AND.(atoms%nlo(n).GT.0)) THEN
                        DO ilo = 1, atoms%nlo(n)
                           IF (atoms%llo(ilo,n).EQ.lnum(i,n)) THEN
                              IF (.NOT.llo_found(ilo)) THEN
                                 enpara%ello0(ilo,n,ispin) = eig(i,ispin,n)
                                 IF ((enpara%ello0(ilo,n,ispin).GT.input%elup).OR.&
                                     (enpara%ello0(ilo,n,ispin).LT.input%ellow)) THEN
                                    enpara%ello0(ilo,n,ispin)= vacpar(1)
                                 ELSE IF (atoms%l_dulo(ilo,n)) THEN
                                    enpara%ello0(ilo,n,ispin)= enpara%el0(atoms%llo(ilo,n),n,ispin)
                                 ELSE
                                    enpara%skiplo(n,ispin) = enpara%skiplo(n,ispin) + 2*atoms%llo(ilo,n)+1
                                 END IF
                                 llo_found(ilo) = .TRUE.
                                 IF ((enpara%el0(atoms%llo(ilo,n),n,ispin)-enpara%ello0(ilo,n,ispin).LT.0.5)&
                                     .AND.(atoms%llo(ilo,n).GE.0)) THEN
                                    enpara%el0(atoms%llo(ilo,n),n,ispin) = vacpar(1)
                                    IF (atoms%l_dulo(ilo,n)) enpara%ello0(ilo,n,ispin)= enpara%el0(atoms%llo(ilo,n),n,ispin)
                                 END IF
                              END IF
                           END IF
                        END DO ! ilo = 1, atoms%nlo(n)
                     END IF
                  END IF ! .NOT.l_found(lnum(i,n)).AND.(lnum(i,n).LE.3)
               END DO ! i = nst(n), 1, -1 
               IF (obsolete%lepr.EQ.1) THEN
                  DO i = 0, 3
                     enpara%el0(i,n,ispin) = enpara%el0(i,n,ispin) - vbar(ispin,n)
                  END DO
                  DO ilo = 1,atoms%nlo(n)
                     enpara%ello0(ilo,n,ispin) = enpara%ello0(ilo,n,ispin) - vbar(ispin,n)
                  END DO
               END IF
            END DO ! atom types

            IF (input%film) THEN
               ! get guess for vacuum parameters
               ! YM : in 1D case should be modified, but also not that bad like this
               !
               ! generate coulomb potential by integrating inward to z1

               DO ivac = 1, vacuum%nvac
                  CALL xcpot_dummy%init("vwn",.false.)
                  DO i=1,vacuum%nmz
                     sigm(i) = (i-1)*vacuum%delz*den%vacz(i,ivac,ispin)
                  END DO
                  CALL qsf(vacuum%delz,sigm,vacpar(ivac),vacuum%nmz,0)
                  denz1(1) = den%vacz(1,ivac,ispin)          ! get estimate for potential at vacuum boundary
                  CALL  vxcall(6,xcpot_dummy,1,1,1,denz1,vacxpot,vacpot)
                  ! seems to be the best choice for 1D not to substract vacpar
                  IF (.NOT.oneD%odi%d1) THEN
                     vacpot(1) = vacpot(1) - fpi_const*vacpar(ivac)
                  END IF
                  IF (obsolete%lepr.EQ.1) THEN
                     vacpar(ivac) = -0.2 - vacpot(1)
                     WRITE (6,'(" vacuum",i2," reference energy =",f12.6)') ivac,vacpot
                  ELSE
                     vacpar(ivac) = vacpot(1)
                  END IF
               END DO
               IF (vacuum%nvac.EQ.1) vacpar(2) = vacpar(1)
            END IF

            IF (obsolete%lepr.EQ.1) THEN
               enpara%enmix = 0.3
            ELSE
               enpara%enmix = 1.0
            END IF

            ! write enpara-file
            enpara%evac0(:,ispin)=vacpar(:SIZE(enpara%evac0,1))
            CALL w_enpara(atoms,ispin,input%film,enpara,16)
         END DO ! ispin

         CLOSE (40) ! enpara file
      END IF
      DEALLOCATE (xp,rat,eig,rh,rh1)
      DEALLOCATE (rhoss,vacpar,vbar,sigm)
      DEALLOCATE (enpara%ello0,enpara%el0,enpara%lchange)
      DEALLOCATE (enpara%skiplo,enpara%llochg,enpara%enmix,enpara%evac0)
   END IF ! mpi%irank == 0

END SUBROUTINE stden

END MODULE m_stden