hsvac.F90 12.4 KB
Newer Older
1
MODULE m_hsvac
2
  USE m_juDFT
3 4
CONTAINS
  SUBROUTINE hsvac(&
5
       vacuum,stars,DIMENSION, atoms, jsp,input,vxy,vz,evac,cell,&
6
       bkpt,lapw,sym, noco,jij, n_size,n_rank,nv2,l_real,hamOvlp)
7 8 9 10 11 12 13 14 15 16 17 18 19
    !*********************************************************************
    !     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
20 21 22 23 24 25 26 27 28 29 30
    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
    TYPE(t_hamOvlp),INTENT(INOUT) :: hamOvlp
31 32 33 34 35
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: jsp   ,n_size,n_rank
    !     ..
    !     .. Array Arguments ..
36
    COMPLEX, INTENT (INOUT) :: vxy(vacuum%nmzxyd,stars%ng2-1,2)
37
    INTEGER, INTENT (OUT):: nv2(DIMENSION%jspd)
38
    REAL,    INTENT (INOUT) :: vz(vacuum%nmzd,2,4)
39
    REAL,    INTENT (IN) :: evac(2,DIMENSION%jspd)
40
    REAL,    INTENT (IN) :: bkpt(3)
41 42

    LOGICAL,INTENT(IN)    :: l_real
43 44 45 46 47 48 49 50 51 52
    !     ..
    !     .. 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 ..
53 54 55 56
    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)
57
    COMPLEX vxy_help(stars%ng2-1)
58 59 60 61
    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)
62 63 64 65
    ! l_J auxiliary potential array
    COMPLEX, ALLOCATABLE :: vxy1(:,:,:)
    !     ..

66
    d2 = SQRT(cell%omtil/cell%area)
67

68
    IF (jij%l_J) ALLOCATE (vxy1(vacuum%nmzxyd,stars%ng2-1,2))
69 70 71 72 73 74 75 76 77 78 79 80

    !--->    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
81
          ENDDO
82
          nv2(jspin) = nv2(jspin) + 1
83
          IF (nv2(jspin)>DIMENSION%nv2d)  CALL juDFT_error("hsvac:dimension%nv2d",calledby ="hsvac")
84 85 86
          kvac1(nv2(jspin),jspin) = lapw%k1(k,jspin)
          kvac2(nv2(jspin),jspin) = lapw%k2(k,jspin)
          map2(k,jspin) = nv2(jspin)
87
       ENDDO k_loop
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
    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
154
             IF (jij%l_J .AND. ipot.EQ.3) vxy(:,:,ivac)=CMPLX(0.,0.)
155 156
          ENDIF
          CALL vacfun(&
157
               vacuum,DIMENSION,stars,&
158 159 160 161 162 163 164 165 166 167 168 169 170
               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
171 172 173
                   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) )
174 175 176 177 178 179 180
                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
181 182 183
                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) )
184 185 186 187 188 189 190 191 192 193 194 195
             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
196
                i_start = MOD(MOD(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
             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
215 216
                      sij = CONJG(a(i,jspin))*a(j,jspin) + &
                           CONJG(b(i,jspin))*b(j,jspin)*ddnv(ik,jspin1)
217
                      !+APW_LO
218 219 220 221 222 223
                      IF (input%l_useapw) THEN
                         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
224
                         IF (l_real) THEN
225
                            hamOvlp%a_r(ii) = hamOvlp%a_r(ii) + 0.25 * REAL(apw_lo) 
226
                         ELSE 
227
                            hamOvlp%a_c(ii) = hamOvlp%a_c(ii) + 0.25 * apw_lo
228
                         ENDIF
229
                      ENDIF
230
                      !+APW_LO
231
                      IF (l_real) THEN
232
                         hamOvlp%b_r(ii) = hamOvlp%b_r(ii) + REAL(sij)
233
                      ELSE 
234
                         hamOvlp%b_c(ii) = hamOvlp%b_c(ii) + sij
235
                      ENDIF
236 237 238
                   END IF
                ENDDO
                ii = ii0 + i
239
                sij = CONJG(a(i,jspin))*a(i,jspin) + CONJG(b(i,jspin))*b(i,jspin)*ddnv(ik,jspin1)
240 241
                IF (l_real) THEN

242
                   hamOvlp%b_r(ii) = hamOvlp%b_r(ii) + REAL(sij)
243
                ELSE
244
                   hamOvlp%b_c(ii) = hamOvlp%b_c(ii) + sij
245
                ENDIF
246
             ENDDO
247 248 249 250 251 252 253 254 255 256 257 258
          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
259
             i_start = MOD(MOD(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
260 261 262 263
          ELSEIF (ipot.EQ.3) THEN
             jspin1 = 2
             jspin2 = 1
             nc = nc_0
264
             i_start = MOD(MOD(n_rank - (lapw%nv(1)+atoms%nlotot),n_size) + n_size,n_size) 
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
          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)
282 283
                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))
284 285
                IF (l_real) THEN

286
                   hamOvlp%a_r(ii) = hamOvlp%a_r(ii) + REAL(hij)
287
                ELSE
288
                   hamOvlp%a_c(ii) = hamOvlp%a_c(ii) + hij
289
                ENDIF
290 291 292 293 294 295 296 297 298 299 300 301 302
             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