force_sf.F90 20.5 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      MODULE m_force_sf
!     *****************************************************************
!     This routine calculates a contribution to the forces stemming
!     from the discontinuity of density and potential at the muffin-tin
!     boundary oint n [ rho V (IS) - rho V (MT) ] dS
!     Klueppelberg May 13
!     *****************************************************************

!     To enable debug code that compares potential and density on the
!     muffin-tin boundary, uncomment the following line:
!#define debug

      IMPLICIT NONE

      COMPLEX, PRIVATE, SAVE, ALLOCATABLE :: force_mt(:,:)
      COMPLEX, PRIVATE, SAVE, ALLOCATABLE :: force_is(:,:)
      LOGICAL, PRIVATE, SAVE :: isdone=.false.,mtdone=.false.

      CONTAINS

      SUBROUTINE force_sf_is(atoms_in,stars,sym,jsp,cell,qpw,vpw,excpw,vxcpw )
!     *****************************************************************
!     This subroutine calculates the contribution evaluated with
!     quantities from the interstital oint n rho V dS
!     The Fourier transform of density and potential is first calculated
!     as an expansion in spherical harmonics. Then the normal vector,
!     which is proportional to Y_1t connects the l component of rho
!     with the l+-1 components of V. This is done up to a cutoff lmax.
!     It is called in a spin loop at the end of vgen.F
!     *****************************************************************

      USE m_constants, ONLY : tpi_const
      USE m_sphbes
      USE m_phasy1
      USE m_gaunt
#ifdef debug
      USE m_ylm
#endif
    USE m_types
      IMPLICIT NONE
      TYPE(t_sym),INTENT(IN)     :: sym
      TYPE(t_stars),INTENT(IN)   :: stars
      TYPE(t_cell),INTENT(IN)    :: cell
      TYPE(t_atoms),INTENT(IN)   :: atoms_in
      TYPE(t_atoms)              :: atoms !copy with modified data
!     .. Scalar Arguments ..
      INTEGER, INTENT (IN) :: jsp

!     .. Array Arguments ..
      COMPLEX, INTENT (IN) :: qpw(:,:) !(stars%n3d,dimension%jspd)
      COMPLEX, INTENT (IN) :: vpw(:,:)!(n3d,jspd)
      COMPLEX, INTENT (IN) :: excpw(stars%n3d)
      COMPLEX, INTENT (IN) :: vxcpw(:,:)!(stars%n3d,dimension%jspd)

!     .. Local Scalars ..
      INTEGER :: n,j,itype,s,l ,lm,t,lp,mp,lmp,jp,natom,m
      REAL    :: r,r2
      COMPLEX :: img,rhoprep,Vprep
      LOGICAL :: isthere

!     .. Local Arrays ..
68
      INTEGER :: lmaxb(atoms_in%ntype)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      COMPLEX :: coeff(3,-1:1),qpw2(stars%n3d,size(qpw,2)),qpwcalc(stars%n3d,size(qpw,2))
      REAL   , ALLOCATABLE :: bsl(:,:,:)
      COMPLEX, ALLOCATABLE :: pylm(:,:,:),rho(:),V(:),pylm2(:,:)
!       COMPLEX, ALLOCATABLE :: qpw2(:,:),qpwcalc(:,:)
#ifdef debug
      REAL    :: vec(3)
      COMPLEX :: factorrho,factorv
      COMPLEX, ALLOCATABLE :: ylm(:),testrho(:,:),testV(:,:)
#endif
      atoms=atoms_in
      atoms%lmax = 2*atoms_in%lmaxd!60!
      lmaxb = atoms%lmax
      img = cmplx(0.0,1.0)

      CALL init_sf(sym,cell,atoms)
      isdone = .true.

Daniel Wortmann's avatar
Daniel Wortmann committed
86
      ALLOCATE ( bsl(stars%n3d,0:atoms%lmaxd,atoms%ntype) )
87 88 89 90 91

      ALLOCATE ( pylm2((atoms%lmaxd+1)**2,atoms%ntype ))
      ALLOCATE ( rho((atoms%lmaxd+1)**2),V((atoms%lmaxd+1)**2) )

#ifdef debug
Daniel Wortmann's avatar
Daniel Wortmann committed
92 93
      ALLOCATE ( ylm((atoms%lmaxd+1)**2),testrho((atoms%lmaxd+1)**2,atoms%ntype) )
      ALLOCATE ( testV((atoms%lmaxd+1)**2,atoms%ntype) )
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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
#endif

      coeff(:, :) =   cmplx(0.0,0.0)
      coeff(1,-1) =     sqrt(tpi_const/3.)
      coeff(1, 1) =    -sqrt(tpi_const/3.)
      coeff(2,-1) = img*sqrt(tpi_const/3.)
      coeff(2, 1) = img*sqrt(tpi_const/3.)
      coeff(3, 0) =  sqrt(2.*tpi_const/3.)
      WRITE (1704,*) 'excpw:',excpw
      WRITE (1704,*) 'vxcpw:',vxcpw
!     load in density without coretails
      INQUIRE (FILE='qpw',EXIST=isthere)
      IF (isthere.and..false.) THEN
        qpw2 = 0.0
        OPEN (15,file='qpw',form='formatted',status='unknown')
        DO jp = 1,size(qpw,2)
        DO s = 1,stars%n3d
          READ (15,'(i2,i10,2f20.14)') n,j,qpw2(s,jp)
        END DO ! s
        END DO ! jp
        CLOSE (15)
        IF (any(abs(qpw2).gt.10**(-6))) THEN
          qpwcalc = qpw2
        ELSE
          qpwcalc = qpw
        END IF
      ELSE
        qpwcalc = qpw
      END IF

!     prepare star quantities
!       DO s = 1,ng3
!         DO itype = 1,ntype
!           r = sk3(s)*rmt(itype)
!           CALL sphbes(lmax,r,bsl(s,:,itype))
!         END DO ! itype
!       END DO ! s
      DO itype = 1,atoms%ntype
        CALL sphbes(atoms%lmax(itype),0.0,bsl(1,:,itype))
        DO s = 2,stars%ng3
!         Only call sphbes if the length of the star changed
          IF (abs(stars%sk3(s)-stars%sk3(s-1)).gt.1.0e-14) THEN
            r = stars%sk3(s)*atoms%rmt(itype)
            CALL sphbes(atoms%lmax(itype),r,bsl(s,:,itype))
          ELSE
            bsl(s,:,itype) = bsl(s-1,:,itype)
          END IF
        END DO ! s
      END DO ! itype


      force_is = 0.0

      natom = 1
      DO itype = 1,atoms%ntype
        r2  = atoms%rmt(itype)**2
        rho = 0.0
        V   = 0.0
!         DO l = 0,lmax-1
!           DO s = 1,ng3
! !           calculate phase factors for the current atom type to prevent
! !           total overhead. it is still calculated lmax times even 
! !           though phasy1 generates pylm for all values of l at once.
! !           but this loop sequence is more convenient.
! !           allocating pylm as pylm(lm,s,itype) and precalculating it
! !           leads to exhaustive use of memory in larger systems
! !             CALL phasy1(
! ! !      >              ntypd,n3d,natd,nop,lmaxd,ntype,neq,lmax,
! !      >              1,n3d,1,nop,lmax,1,neq(itype),lmaxb(itype),0,
! !      >              2.*tpi,taual(1:3,natom),bmat,kv3,!1+sum(neq(1:itype-1))),bmat,kv3,
! !      >              tau,mrot,symor,s,invtab,
! !      <              pylm2(:))
!             pylm2 = 1.
! !             IF ((s.eq.1).and.(l.eq.0)) WRITE (851,*) pylm2(:)
!             rhoprep = nstr(s) * bsl(s,l,itype) * qpwcalc(s,jsp)
!             IF (l.eq.0) THEN
!               Vprep = nstr(s) * bsl(s,l,itype)
!      *              * (1*vpw(s,jsp)-1*vxcpw(s,jsp)+1*excpw(s))
! !      *              * vpw(s,jsp)
!             END IF
! !           for l = 0 we calculate rho_00 and V_00...
!             DO m = -l,l
!               lm = l*(l+1) + m + 1
!               rho(lm) = rho(lm) + rhoprep * pylm2(lm)!pylm(lm,s,itype)!
!               IF (l.gt.0) CYCLE
!                 V(lm) =   V(lm) +   Vprep * pylm2(lm)!pylm(lm,s,itype)!
!             END DO ! m
! !           ... and V_1mp, for l > 0, we calculate rho_lm, V_l+1,mp ...
!               Vprep = nstr(s) * bsl(s,l+1,itype)
!      *              * (1*vpw(s,jsp)-1*vxcpw(s,jsp)+1*excpw(s))
! !      *              * vpw(s,jsp)
!             DO m = -l-1,l+1
!               lm = (l+1)*(l+2) + m + 1
!                 V(lm) =   V(lm) +   Vprep * pylm2(lm)!pylm(lm,s,itype)!
!             END DO ! m
!           END DO ! s

        DO s = 1,stars%ng3 !l = 0,atoms%lmax-1
!         calculate phase factors for the current atom type to prevent
!         total overhead. it is still calculated lmax times even 
!         though phasy1 generates pylm for all values of l at once.
!         but this loop sequence is more convenient.
!         allocating pylm as pylm(lm,s,itype) and precalculating it
!         leads to exhaustive use of memory in larger systems
          CALL phasy1(atoms,stars,sym,cell,s,pylm2(:,:))

          DO l = 0,atoms%lmax(itype)-1 !s = 1,stars%ng3
!           calculate phase factors for the current atom type to prevent
!           total overhead. it is still calculated lmax times even 
!           though phasy1 generates pylm for all values of l at once.
!           but this loop sequence is more convenient.
!           allocating pylm as pylm(lm,s,itype) and precalculating it
!           leads to exhaustive use of memory in larger systems
!             CALL phasy1(
! !      >              ntypd,n3d,natd,nop,lmaxd,ntype,neq,lmax,
!      >              1,n3d,1,nop,lmax,1,neq(itype),lmaxb(itype),0,
!      >              2.*tpi,taual(1:3,natom),bmat,kv3,!1+sum(neq(1:itype-1))),bmat,kv3,
!      >              tau,mrot,symor,s,invtab,
!      <              pylm2(:))
!             IF ((s.eq.1).and.(l.eq.0)) WRITE (851,*) pylm2(:)
            rhoprep = stars%nstr(s) * bsl(s,l,itype) * qpwcalc(s,jsp)
            IF (l.eq.0) THEN
              Vprep = stars%nstr(s) * bsl(s,l,itype) * (1*vpw(s,jsp)-1*vxcpw(s,jsp)+1*excpw(s)) ! Switching between Veff and VCoul + exc
!      *              * vpw(s,jsp)
            END IF
!           for l = 0 we calculate rho_00 and V_00...
            DO m = -l,l
              lm = l*(l+1) + m + 1
              rho(lm) = rho(lm) + rhoprep * pylm2(lm,itype)!pylm(lm,s,itype)!
              IF (l.gt.0) CYCLE
                V(lm) =   V(lm) +   Vprep * pylm2(lm,itype)!pylm(lm,s,itype)!
            END DO ! m
!           ... and V_1mp, for l > 0, we calculate rho_lm, V_l+1,mp ...
              Vprep = stars%nstr(s) * bsl(s,l+1,itype) * (1*vpw(s,jsp)-1*vxcpw(s,jsp)+1*excpw(s))
!      *              * vpw(s,jsp)
            DO m = -l-1,l+1
              lm = (l+1)*(l+2) + m + 1
                V(lm) =   V(lm) +   Vprep * pylm2(lm,itype)!pylm(lm,s,itype)!
            END DO ! m
          END DO ! l
          END DO ! s

!           V = 0.0
!           V(1) = sqrt(2.*tpi)
!           rho = 0.0
!           rho(1) = sqrt(2.*tpi)

        DO l = 0,atoms%lmax(itype)-1 ! new: altered s and l loop above

          DO m = -l,l
            lm = l*(l+1) + m + 1
            WRITE (1705,*) itype,l,m,lm-1,rho(lm),V(lm)
!           ... because rho_lm occurs with V_l-1,mp and V_l+1,mp
            DO lp = abs(l-1),l+1,2
              DO t = -1,1
                mp = t-m
                IF (lp.lt.abs(mp)) CYCLE
                lmp = lp*(lp+1) + mp + 1
                force_is(:,itype) = force_is(:,itype) + r2&
                     * rho(lm) * V(lmp) * conjg(coeff(:,t)) * gaunt1(1,l,lp,t,m,mp,atoms%lmax(itype))
              END DO ! t
            END DO ! lp
          END DO ! m
        END DO ! l
#ifdef debug
        testrho(:,itype) = rho(:)
          testV(:,itype) =   V(:)
#endif
        WRITE (849,'(3(2(f20.14),1x))') (force_is(s,itype),s=1,3)
        natom = natom + atoms%neq(itype)
      END DO ! itype

#ifdef debug
!     test output
!     Evaluate plane wave density in spherical harmonic representation
!     on MT surface along a vector (1,1,1)
      DO itype = 1,atoms%ntype
        WRITE (180,'(2i3)') jsp,itype
        WRITE (180,'(3f20.14)') real(force_is(1,itype)), real(force_is(2,itype)), real(force_is(3,itype))
        vec(1) =-0.730! 1.0
        vec(2) = 0.938! 1.0
        vec(3) =-1.625! 1.0
        CALL ylm4(atoms%lmax,vec,ylm)
        factorrho = 0.0
        factorv   = 0.0
        DO l = 0,atoms%lmax
        DO m = -l,l
          lm = l*(l+1) + m + 1
          factorrho = factorrho + testrho(lm,itype) * ylm(lm)
          factorv   = factorv   +   testV(lm,itype) * ylm(lm)
          WRITE (180,'(3i4,2f20.14)') l,m,lm,testV(lm,itype)
        END DO ! jp
        END DO ! lapw%kp
        WRITE (180,'(a5,2f20.14)') 'den: ',factorrho
        WRITE (180,'(a5,2f20.14)') 'pot: ',factorv
      END DO ! itype
      DEALLOCATE ( ylm,testrho,testV )
#endif

      DEALLOCATE ( bsl,rho,V )
!       DEALLOCATE ( pylm )
      DEALLOCATE ( pylm2 )
!       DEALLOCATE ( qpw2 )
!       DEALLOCATE ( qpwcalc )

      END SUBROUTINE force_sf_is



      SUBROUTINE force_sf_mt(&
                            atoms,sphhar,jspin,&
                            ispin,mpi,&
                            vr,excr,&
                            vxcr,rho,&
                            sym,cell )
!     *****************************************************************
!     This subroutine calculates the contribution evaluated with
!     quantities from the muffin tin
!     n rho V = sum(nu,nup) rho(nu)V(nup)
!             * sum(m,mu,mup) c_1m* Y_1m* c_lnu Y_numu c_lnup Y_nupmup
!     It is called in a spin loop at the end of cdnval.F
!     *****************************************************************

      USE m_constants, ONLY : tpi_const,sfp_const
      USE m_gaunt
      USE m_ylm
      USE m_types
      IMPLICIT NONE
      TYPE(t_mpi),INTENT(IN)   :: mpi
      TYPE(t_sym),INTENT(IN)   :: sym
      TYPE(t_cell),INTENT(IN)  :: cell
      TYPE(t_sphhar),INTENT(IN):: sphhar
      TYPE(t_atoms),INTENT(IN) :: atoms

!     .. Scalar Arguments ..
      INTEGER, INTENT (IN) :: jspin
      INTEGER, INTENT (IN) :: ispin 

!     .. Array Arguments ..
Daniel Wortmann's avatar
Daniel Wortmann committed
333 334 335 336
      REAL   , INTENT (IN) :: vr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype) ! 
      REAL   , INTENT (IN) :: rho(:,0:,:,:)!(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,dimension%jspd)
      REAL   , INTENT (IN) :: excr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype)
      REAL   , INTENT (IN) :: vxcr(:,0:,:,:)!(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,dimension%jspd)
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

!     .. Local Scalars ..
      INTEGER :: natom,itype,nd,lh,l,lhp,lp,mem,m,memp,mp,t,i,lmp
      REAL    :: pot,den
      COMPLEX :: img,factor

!     .. Local Arrays ..
      COMPLEX :: coeff(3,-1:1)
      COMPLEX :: d1((atoms%lmaxd+1)**2,atoms%ntype ),d2((atoms%lmaxd+1)**2,atoms%ntype )
#ifdef debug
      COMPLEX :: testrho((atoms%lmaxd+1)**2 ,atoms%ntype),testv((atoms%lmaxd+1)**2,atoms%ntype )
      COMPLEX :: ylm((atoms%lmaxd+1)**2),factorrho,factorv
      REAL    :: r,Gext(3)
      INTEGER :: lm
      testrho = 0.0
      testv   = 0.0

      WRITE (181,'(a2)') 'MT'
#endif
      WRITE (1704,*) 'vxcr:',vxcr
      WRITE (1704,*) 'excr:',excr

      CALL init_sf(sym,cell,atoms)
      mtdone = .true.

      img = cmplx(0.0,1.0)
      force_mt = 0.0

      coeff(:, :) =   cmplx(0.0,0.0)
      coeff(1,-1) =     sqrt(tpi_const/3.)
      coeff(1, 1) =    -sqrt(tpi_const/3.)
      coeff(2,-1) = img*sqrt(tpi_const/3.)
      coeff(2, 1) = img*sqrt(tpi_const/3.)
      coeff(3, 0) =  sqrt(2.*tpi_const/3.)

      d1 = 0
      d2 = 0

!     Calculate forces: For each atom, loop over all lattice harmonics.
      natom = 1
      DO itype = 1,atoms%ntype

        nd = atoms%ntypsy(natom)

        DO lh = 0,sphhar%nlh(nd)
          l = sphhar%llh(lh,nd)

!         The l=0 component of the potential array is saved with a
!         factor r/sfp in front of it. For this calculation, we need
!         the pure potential
          pot = vr(atoms%jri(itype),lh,itype)
          IF (lh.eq.0) THEN
            pot = pot*sfp_const/atoms%rmt(itype)
          END IF
          pot = +1*excr(atoms%jri(itype),lh,itype) -1*vxcr(atoms%jri(itype),lh,itype,ispin) +1*pot
!           pot = 0
!           IF (l.eq.0) pot = sfp

          WRITE (400,'(3(i4,1x),f20.14)') itype,lh,l,vr(atoms%jri(itype),lh,itype)
          WRITE (401,'(3(i4,1x),f20.14)') itype,lh,l,rho(atoms%jri(itype),lh,itype,ispin)
          DO mem = 1,sphhar%nmem(lh,nd)
            m = sphhar%mlh(mem,lh,nd)
            lmp = l*(l+1) + m + 1
            d1(lmp,itype) = d1(lmp,itype) + sphhar%clnu(mem,lh,nd)&
                 *rho(atoms%jri(itype),lh,itype,ispin)/atoms%rmt(itype)**2
            d2(lmp,itype) = d2(lmp,itype) + sphhar%clnu(mem,lh,nd) * pot
          END DO ! mem

          DO lhp = 0,sphhar%nlh(nd)
            lp = sphhar%llh(lhp,nd)
            IF (abs(l-lp).ne.1) CYCLE

            den = rho(atoms%jri(itype),lhp,itype,ispin) ! jspin ?
!             den = 0
!             IF (lp.eq.0) den = sfp*rmt(itype)**2

            DO mem = 1,sphhar%nmem(lh,nd)
              m = sphhar%mlh(mem,lh,nd)
            DO memp = 1,sphhar%nmem(lhp,nd)
              mp = sphhar%mlh(memp,lhp,nd)
              IF (abs(m+mp).gt.1) CYCLE

!             Due to the normal vector n, the lattice harmonics meet
!             with a Y_1m resulting in a gaunt coefficient.
              factor = pot * den * sphhar%clnu(mem,lh,nd) * sphhar%clnu(memp,lhp,nd)&
                    * gaunt1(1,l,lp,m+mp,m,mp,atoms%lmaxd)
              force_mt(:,itype) = force_mt(:,itype) + factor * conjg(coeff(:,m+mp))

            END DO ! memp
            END DO ! mem

          END DO ! lhp


#ifdef debug
!         testrho/v debug code
!         construct spherical harmonic expansion of lattice harmonic
!         density and potential on the muffin-tin boundary
          DO mem = 1,sphhar%nmem(lh,nd)
            m = sphhar%mlh(mem,lh,nd)
          DO lp = 0,atoms%lmaxd
            IF (l.ne.lp) CYCLE
          DO mp = -lp,lp
            IF (m.ne.mp) CYCLE
            lm = lp*(lp+1) + mp + 1
            testrho(lm,itype) = testrho(lm,itype)&
                 + rho(atoms%jri(itype),lh,itype,ispin) &
                 * sphhar%clnu(mem,lh,nd) / atoms%rmt(itype)**2
            testv(lm,itype)   = testv(lm,itype) + pot * sphhar%clnu(mem,lh,nd)
          END DO ! mp
          END DO ! lp
          END DO ! mem
#endif

        END DO ! lh

        DO l = 0,atoms%lmaxd
        DO m = -l,l
          lmp = l*(l+1) + m + 1
          WRITE (1706,*) itype,l,m,lmp-1,d1(lmp,itype),d2(lmp,itype)
        END DO ! m
        END DO ! l

        IF (mpi%irank.eq.0) THEN
          WRITE (850,'(3(2(f20.14),1x))') (force_mt(t,itype),t=1,3)
        END IF
        natom = natom + atoms%neq(itype)
      END DO ! itype

!     debug
      WRITE (*,*) 'sanity check'
      WRITE (*,*) 'look is',force_is
      WRITE (*,*) 'look mt',force_mt

#ifdef debug
!     test output
!     Evaluate lattice harmonic density in spherical harmonic
!     representation in along a reciprocal vector (1,1,1)
      DO itype = 1,atoms%ntype
        WRITE (181,'(2i3)') jspin,itype
        WRITE (181,'(3f20.14)') real(force_mt(1,itype)), real(force_mt(2,itype)), real(force_mt(3,itype))
        Gext(1) =-0.730! 1.0
        Gext(2) = 0.938! 1.0
        Gext(3) =-1.625! 1.0
        CALL ylm4(atoms%lmaxd,Gext,ylm)
        factorrho = 0.0
        factorv   = 0.0
        DO lp = 0,atoms%lmaxd
        DO mp = -lp,lp
          lm = lp*(lp+1) + mp + 1
          factorrho = factorrho + testrho(lm,itype) * ylm(lm)
          factorv   = factorv   + testv(lm,itype)   * ylm(lm)
          WRITE (181,'(3i4,2f20.14)') lp,mp,lm,testv(lm,itype)
        END DO ! mp
        END DO ! lp
        WRITE (181,'(a5,2f20.14)') 'den: ',factorrho
        WRITE (181,'(a5,2f20.14)') 'pot: ',factorv
      END DO ! itype
#endif

      END SUBROUTINE force_sf_mt

      SUBROUTINE init_sf(sym,cell,atoms)
!     Initialize results arrays if neither force_sf_is nor force_sf_mt
!     were executed up until now.
!     Called at the beginning of cdnval.F to once fill the wigner array
!     Also called in force_sf_is/mt to guarantee that
!     all arrays are allocated.

    USE m_types
      IMPLICIT NONE
      TYPE(t_sym),INTENT(IN)   :: sym
      TYPE(t_cell),INTENT(IN)  :: cell
      TYPE(t_atoms),INTENT(IN) :: atoms

!     debug
      IF (ALLOCATED(force_is)) THEN
        WRITE (*,*) 'init is:',force_is
        WRITE (*,*) 'init mt:',force_mt
      ELSE
        WRITE (*,*) 'init: not initialized'
      END IF

      IF (isdone.OR.mtdone) RETURN
      IF (.not.ALLOCATED(force_is)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
522
        ALLOCATE ( force_is(3,atoms%ntype),force_mt(3,atoms%ntype) )
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
      END IF
      force_is = 0.0
      force_mt = 0.0

        WRITE (*,*) 'init: end'
      END SUBROUTINE init_sf

      SUBROUTINE exit_sf(isp,atoms,force)
!     Write out force contribution from surface and deallocate arrays if
!     all force_sf_is and force_sf_mt were executed
!     Called at the end of cdnval.F and totale.f for writing purposes
!     and to deallocate the arrays.

    USE m_types
      IMPLICIT NONE
      INTEGER,INTENT(IN)         :: isp
      TYPE(t_atoms),INTENT(IN)   :: atoms
      REAL,INTENT(INOUT)         :: force(:,:,:)

      INTEGER :: itype,dir
Daniel Wortmann's avatar
Daniel Wortmann committed
543
      COMPLEX :: force_sf(3,atoms%ntype)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

!     debug
      IF (ALLOCATED(force_is)) THEN
        WRITE (*,*) 'exit is:',force_is
        WRITE (*,*) 'exit mt:',force_mt
      ELSE
        WRITE (*,*) 'exit: not initialized'
      END IF

      IF (isdone.AND.mtdone) THEN
        force_sf(:,:) = force_is(:,:) - force_mt(:,:)
        force(:,:,isp) = force(:,:,isp) + real(force_sf(:,:))
        WRITE (6,*)
        WRITE (16,*)
        DO itype = 1,atoms%ntype
          WRITE (6,FMT=8010) itype
          WRITE (16,FMT=8010) itype
          WRITE (6,FMT=8020) (force_sf(dir,itype),dir=1,3)
          WRITE (16,FMT=8020) (force_sf(dir,itype),dir=1,3)
        END DO ! itype
        isdone = .false.
        mtdone = .false.
        DEALLOCATE ( force_is,force_mt )
      END IF

8010  FORMAT (' FORCES: SURFACE CORRECTION FOR ATOM TYPE',i4)
8020  FORMAT (' FX_SF=',2f10.6,' FY_SF=',2f10.6,' FZ_SF=',2f10.6)

      END SUBROUTINE exit_sf

      END MODULE m_force_sf