hsvac.F90 12 KB
Newer Older
1 2 3 4 5 6 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 68 69 70 71 72 73 74 75 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 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
MODULE m_hsvac
  use m_juDFT
CONTAINS
  SUBROUTINE hsvac(&
       vacuum,stars,dimension, atoms, jsp,input,vxy,vz,evac,cell,&
       bkpt,lapw,sym, noco,jij, n_size,n_rank, aa,bb, nv2)
    !*********************************************************************
    !     adds in the vacuum contributions to the the hamiltonian and
    !     overlap matrices. as written, each k-point calculates the
    !     vacuum functions again since there is only a single vacuum
    !     parameter per vacuum.
    !                m. weinert
    !*********************************************************************
    !     modified by R.Podloucky for speeding up and microtaskin
    !*********************************************************************

    USE m_vacfun
    USE m_types
    IMPLICIT NONE
    TYPE(t_dimension),INTENT(IN):: dimension
    TYPE(t_input),INTENT(IN)    :: input
    TYPE(t_vacuum),INTENT(IN)   :: vacuum
    TYPE(t_noco),INTENT(IN)     :: noco
    TYPE(t_jij),INTENT(IN)      :: jij
    TYPE(t_sym),INTENT(IN)      :: sym
    TYPE(t_stars),INTENT(IN)    :: stars
    TYPE(t_cell),INTENT(IN)     :: cell
    TYPE(t_atoms),INTENT(IN)    :: atoms
    TYPE(t_lapw),INTENT(IN)     :: lapw
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: jsp   ,n_size,n_rank
    !     ..
    !     .. Array Arguments ..
    COMPLEX, INTENT (INOUT) :: vxy(vacuum%nmzxyd,stars%n2d-1,2)
    INTEGER, INTENT (OUT):: nv2(dimension%jspd)
    REAL,    INTENT (INOUT) :: vz(vacuum%nmzd,2,4)
    REAL,    INTENT (IN) :: evac(2,dimension%jspd)
    REAL,    INTENT (IN) :: bkpt(3)
#ifdef CPP_INVERSION
    REAL,    INTENT (INOUT) :: aa(:),bb(:)!(matsize)
#else
    COMPLEX, INTENT (INOUT) :: aa(:),bb(:)
#endif
    !     ..
    !     .. Local Scalars ..
    COMPLEX hij,sij,apw_lo,c_1
    REAL d2,gz,sign,th,wronk,fac1
    INTEGER i,i2,ii,ik,j,jk,k,jspin,ipot,npot,ii0
    INTEGER ivac,irec,imz,igvm2,igvm2i
    INTEGER jspin1,jspin2,jmax,jsp_start,jsp_end
    INTEGER i_start,nc,nc_0
    !     ..
    !     .. Local Arrays ..
    INTEGER kvac1(dimension%nv2d,dimension%jspd),kvac2(dimension%nv2d,dimension%jspd)
    INTEGER map2(dimension%nvd,dimension%jspd)
    COMPLEX tddv(dimension%nv2d,dimension%nv2d),tduv(dimension%nv2d,dimension%nv2d)
    COMPLEX tudv(dimension%nv2d,dimension%nv2d),tuuv(dimension%nv2d,dimension%nv2d)
    COMPLEX vxy_help(stars%n2d-1)
    COMPLEX a(dimension%nvd,dimension%jspd),b(dimension%nvd,dimension%jspd)
    REAL ddnv(dimension%nv2d,dimension%jspd),dudz(dimension%nv2d,dimension%jspd)
    REAL duz(dimension%nv2d,dimension%jspd), udz(dimension%nv2d,dimension%jspd)
    REAL uz(dimension%nv2d,dimension%jspd)
    ! l_J auxiliary potential array
    COMPLEX, ALLOCATABLE :: vxy1(:,:,:)
    !     ..

    d2 = sqrt(cell%omtil/cell%area)

    IF (jij%l_J) ALLOCATE (vxy1(vacuum%nmzxyd,stars%n2d-1,2))

    !--->    set up mapping function from 3d-->2d lapws

    DO jspin = 1,input%jspins
       nv2(jspin) = 0
       k_loop:DO  k = 1,lapw%nv(jspin)
          DO  j = 1,nv2(jspin)
             IF (lapw%k1(k,jspin).EQ.kvac1(j,jspin)&
                  .AND. lapw%k2(k,jspin).EQ.kvac2(j,jspin)) THEN
                map2(k,jspin) = j
                CYCLE k_loop
             END IF
          enddo
          nv2(jspin) = nv2(jspin) + 1
          IF (nv2(jspin)>dimension%nv2d)  CALL juDFT_error("hsvac:dimension%nv2d",calledby ="hsvac")
          kvac1(nv2(jspin),jspin) = lapw%k1(k,jspin)
          kvac2(nv2(jspin),jspin) = lapw%k2(k,jspin)
          map2(k,jspin) = nv2(jspin)
       enddo k_loop
    ENDDO
    !--->    loop over the two vacuua (1: upper; 2: lower)
    DO ivac = 1,2
       sign = 3. - 2.*ivac
       npot = 1
       !---> pk non-collinear
       IF (noco%l_noco) THEN
          !--->       if the two vacuua are equivalent, the potential file has to
          !--->       be backspaced, because the potential is the same at both
          !--->       surfaces of the film
          IF ((ivac.EQ.2) .AND. (vacuum%nvac.EQ.1)) THEN
             DO irec = 1,4
                BACKSPACE (25)
             ENDDO
          ENDIF
          !--->       load the non-warping part of the potential
          READ (25)((vz(imz,ivac,ipot),imz=1,vacuum%nmzd),ipot=1,4)
          npot = 3
          ! for J-coeff. we average the up-up and down-down parts and off-diagonal elements of the
          ! potential matrix to zero
          IF (jij%l_J) THEN
             vz(:,ivac,1) = (vz(:,ivac,1) + vz(:,ivac,2))/2.
             vz(:,ivac,2) =  vz(:,ivac,1)
             vz(:,ivac,3) = 0.0
             vz(:,ivac,4) = 0.0
          END IF
       ENDIF
       !---> pk non-collinear

       DO ipot = 1,npot
          !--->       get the wavefunctions and set up the tuuv, etc matrices
          IF (noco%l_noco) THEN
             IF (.NOT.jij%l_J) THEN
                READ (25)((vxy(imz,igvm2,ivac), imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
             END IF
             ! l_J we want to average the diagonal elements of the potential matrix
             IF (jij%l_J .AND. ipot.EQ.1) THEN
                READ (25)((vxy(imz,igvm2,ivac), imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
                READ (25)((vxy1(imz,igvm2,ivac), imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
                vxy(:,:,ivac) = (vxy(:,:,ivac)+vxy1(:,:,ivac))/2.
             END IF

             IF (jij%l_J .AND. ipot.EQ.3) THEN
                READ (25)((vxy(imz,igvm2,ivac), imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
             END IF

             IF (vacuum%nvac.EQ.1 .AND. ivac.EQ.2 .AND.(.NOT.sym%zrfs) ) THEN
                !--->          In this case (inversion, no z-reflection and thus no
                !--->          2d-inversion) the coeff. of a 2d-star (containing G) of
                !--->          the second vacuum is equal to the invers 2d-star
                !--->          (containing -G) of the first vacuum.
                ! l_J no need to do this symmetrizaion twice, if the potential vxy is the same
                IF (.NOT.jij%l_J .OR. (jij%l_J .AND. ipot.EQ.1)) THEN
                   DO imz = 1,vacuum%nmzxy
                      DO igvm2 = 2,stars%ng2
                         !--->                   find the index of the invers 2d-star
                         igvm2i = stars%ig2(stars%ig(-stars%kv2(1,igvm2),-stars%kv2(2,igvm2),0))
                         vxy_help(igvm2-1) = vxy(imz,igvm2i-1,2)
                      ENDDO
                      DO igvm2 = 2,stars%ng2
                         vxy(imz,igvm2-1,2) = vxy_help(igvm2-1)
                      ENDDO
                   ENDDO
                END IF ! jij%l_J
             ENDIF ! ivac-vacuum%nvac
             ! l_J we want the off-diagonal potential matrix elements to be zero
             IF (jij%l_J .AND. ipot.EQ.3) vxy(:,:,ivac)=cmplx(0.,0.)
          ENDIF
          CALL vacfun(&
               vacuum,dimension,stars,&
               jsp,input,noco,ipot,&
               sym, cell,ivac,evac(1,1),bkpt,vxy(1,1,ivac),vz,kvac1,kvac2,nv2,&
               tuuv,tddv,tudv,tduv,uz,duz,udz,dudz,ddnv,wronk)
          fac1 = 1.0 / (d2*wronk)
          !
          !--->       generate a and b coeffficients
          !
          IF (noco%l_noco) THEN
             DO jspin = 1,input%jspins
                DO k = 1,lapw%nv(jspin)
                   gz = sign*cell%bmat(3,3)*lapw%k3(k,jspin)
                   i2 = map2(k,jspin)
                   th = gz*cell%z1
                   c_1 = fac1 * cmplx( cos(th), sin(th) )
                   a(k,jspin) = - c_1 * cmplx(dudz(i2,jspin), gz*udz(i2,jspin) )
                   b(k,jspin) =   c_1 * cmplx(duz(i2,jspin), gz* uz(i2,jspin) )
                ENDDO
             ENDDO
          ELSE
             DO k = 1,lapw%nv(jsp)
                gz = sign*cell%bmat(3,3)*lapw%k3(k,jsp)
                i2 = map2(k,jsp)
                th = gz*cell%z1
                c_1 = fac1 * cmplx( cos(th), sin(th) )
                a(k,1) = - c_1 * cmplx(dudz(i2,jsp), gz*udz(i2,jsp) )
                b(k,1) =   c_1 * cmplx(duz(i2,jsp), gz* uz(i2,jsp) )
             ENDDO
          ENDIF
          !--->       update hamiltonian and overlap matrices
          IF (ipot.EQ.1 .OR. ipot.EQ.2) THEN
             jspin = ipot
             !+gb||
             IF (ipot.EQ.1) THEN
                nc = 0
                i_start = n_rank
             ELSE
                nc = nc + atoms%nlotot
                nc_0 = nc
                i_start = mod(mod(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
             ENDIF
             !-gb||
             DO  i = i_start+1, lapw%nv(jspin), n_size
                ik = map2(i,jspin)
                nc = nc + 1
                IF (ipot.EQ.1) THEN
                   jspin = 1
                   ii0 = nc*(nc-1)/2*n_size- (nc-1)*(n_size-n_rank-1)
                ELSEIF (ipot.EQ.2) THEN
                   jspin = 2
                   ii0=nc*(nc-1)/2*n_size-(nc-1)*(n_size-n_rank-1)+ lapw%nv(1)+atoms%nlotot
                ENDIF
                jspin1 = jsp
                IF (noco%l_noco) jspin1 = jspin
                DO j = 1,i - 1
                   ii = ii0 + j
                   !--->             overlap: only  (g-g') parallel=0       '
                   IF (map2(j,jspin).EQ.ik) THEN
                      sij = conjg(a(i,jspin))*a(j,jspin) + &
                           conjg(b(i,jspin))*b(j,jspin)*ddnv(ik,jspin1)
                      !+APW_LO
#ifdef CPP_APW
                      apw_lo = conjg(a(i,jspin)*  uz(ik,jspin1) + b(i,jspin)* udz(ik,jspin1) ) &
                           * (a(j,jspin)* duz(ik,jspin1) + b(j,jspin)*dudz(ik,jspin1) )&
                           +      (a(j,jspin)*  uz(ik,jspin1) + b(j,jspin)* udz(ik,jspin1) ) &
                           * conjg(a(i,jspin)* duz(ik,jspin1) + b(i,jspin)*dudz(ik,jspin1) )
                      !            IF (i.lt.10) write (3,'(2i4,2f20.10)') i,j,apw_lo
#ifdef CPP_INVERSION
                      aa(ii) = aa(ii) + 0.25 * real(apw_lo) 
#else 
                      aa(ii) = aa(ii) + 0.25 * apw_lo
#endif
#endif
                      !+APW_LO
#ifdef CPP_INVERSION
                      bb(ii) = bb(ii) + real(sij)
#else 
                      bb(ii) = bb(ii) + sij
#endif
                   END IF
                ENDDO
                ii = ii0 + i
                sij = conjg(a(i,jspin))*a(i,jspin) + conjg(b(i,jspin))*b(i,jspin)*ddnv(ik,jspin1)
#ifdef CPP_INVERSION
                bb(ii) = bb(ii) + real(sij)
#else
                bb(ii) = bb(ii) + sij
#endif
             enddo
          ENDIF

          !--->    hamiltonian update
          IF (ipot.EQ.1) THEN
             jspin1 = 1
             jspin2 = 1
             nc = 0
             i_start = n_rank
          ELSEIF (ipot.EQ.2) THEN
             jspin1 = 2
             jspin2 = 2
             nc = nc_0
             i_start = mod(mod(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
          ELSEIF (ipot.EQ.3) THEN
             jspin1 = 2
             jspin2 = 1
             nc = nc_0
             i_start = mod(mod(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
          ENDIF
          DO i = i_start+1, lapw%nv(jspin1), n_size
             ik = map2(i,jspin1)
             nc = nc + 1
             IF (ipot.EQ.1) THEN
                ii0 = nc*(nc-1)/2*n_size- (nc-1)*(n_size-n_rank-1)
                jmax = i
             ELSEIF (ipot.EQ.2) THEN
                ii0 = nc*(nc-1)/2*n_size- (nc-1)*(n_size-n_rank-1) + lapw%nv(1)+atoms%nlotot
                jmax = i
             ELSEIF (ipot.EQ.3) THEN
                ii0 = nc*(nc-1)/2*n_size- (nc-1)*(n_size-n_rank-1)
                jmax = lapw%nv(jspin2)
             ENDIF
             DO j = 1,jmax
                ii = ii0 + j
                jk = map2(j,jspin2)
                hij = conjg(a(i,jspin1))* (tuuv(ik,jk)*a(j,jspin2) +tudv(ik,jk)*b(j,jspin2))&
                     + conjg(b(i,jspin1))* (tddv(ik,jk)*b(j,jspin2) +tduv(ik,jk)*a(j,jspin2))
#ifdef CPP_INVERSION
                aa(ii) = aa(ii) + real(hij)
#else
                aa(ii) = aa(ii) + hij
#endif
             ENDDO
          ENDDO

          !--->    end of loop over different parts of the potential matrix
       ENDDO

       !---> end of loop over vacua
    ENDDO

    IF (jij%l_J) DEALLOCATE (vxy1)

  END SUBROUTINE hsvac
END MODULE m_hsvac