kptgen_hybrid.f 8.77 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
      MODULE m_kptgen_hybrid

9 10
      USE m_juDFT

11 12
      CONTAINS
      
Daniel Wortmann's avatar
Daniel Wortmann committed
13 14
! this programm generates an aequdistant kpoint set including the
! Gamma point; it is reduced to IBZ and written in kpts (M.B.)
15

Daniel Wortmann's avatar
Daniel Wortmann committed
16
      !Modified for types D.W.
17 18


19 20
      SUBROUTINE kptgen_hybrid(input,cell,sym,kpts,l_soc)

Daniel Wortmann's avatar
Daniel Wortmann committed
21
      USE m_types
22 23
      USE m_divi

24 25
      IMPLICIT NONE

26 27 28 29
      TYPE(t_input), INTENT(IN)    :: input
      TYPE(t_cell),  INTENT(IN)    :: cell
      TYPE(t_sym),   INTENT(IN)    :: sym
      TYPE(t_kpts),  INTENT(INOUT) :: kpts
30 31 32
      ! - scalars -
      LOGICAL, INTENT(IN)   ::  l_soc
      ! - local scalars -
Daniel Wortmann's avatar
Daniel Wortmann committed
33 34 35
      INTEGER               ::  i,j,k,nkpt
      INTEGER               ::  ikpt,ikpt0,nkpti
      INTEGER               ::  nsym
36 37 38 39 40 41 42 43 44
      ! - local arrays -
      INTEGER,ALLOCATABLE   ::  rot(:,:,:),rrot(:,:,:)
      INTEGER,ALLOCATABLE   ::  invtab(:)
      INTEGER,ALLOCATABLE   ::  neqkpt(:)
      INTEGER,ALLOCATABLE   ::  pkpt(:,:,:),kptp(:),symkpt(:),iarr(:),
     &                          iarr2(:)
      REAL,ALLOCATABLE      ::  rtau(:,:)
      REAL,ALLOCATABLE      ::  bk(:,:),bkhlp(:,:)
      REAL,ALLOCATABLE      ::  rarr(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
45
      LOGICAL               ::  ldum
46

47 48 49 50 51
      IF (sum(kpts%nkpt3).EQ.0) THEN
         CALL divi(kpts%nkpt,cell%bmat,input%film,sym%nop,
     &             sym%nop2,kpts%nkpt3)
      END IF

Daniel Wortmann's avatar
Daniel Wortmann committed
52
      nkpt=kpts%nkpt3(1)*kpts%nkpt3(2)*kpts%nkpt3(3)
53 54 55
      ALLOCATE( bk(3,nkpt),bkhlp(3,nkpt) )

      ikpt = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
56 57 58
      DO i=0,kpts%nkpt3(1)-1
        DO j=0,kpts%nkpt3(2)-1
          DO k=0,kpts%nkpt3(3)-1
59
            ikpt       = ikpt + 1
Daniel Wortmann's avatar
Daniel Wortmann committed
60 61
            bk(:,ikpt) = (/ 1.0*i/kpts%nkpt3(1),1.0*j/kpts%nkpt3(2),
     &                                     1.0*k/kpts%nkpt3(3) /)
62 63 64 65 66 67
          END DO
        END DO
      END DO
      
      IF( ikpt .ne. nkpt) STOP 'failure: number of k-points'

68 69
      IF( sym%invs .or. l_soc ) THEN
        nsym = sym%nop
70
      ELSE
71
        nsym = 2*sym%nop
72 73 74 75
      END IF
      
      ALLOCATE( rot(3,3,nsym),rtau(3,nsym) )

76 77 78
      DO i=1,sym%nop
         rot(:,:,i) = sym%mrot(:,:,i)
        rtau(  :,i) = sym%tau(:,i)
79 80
      END DO

81 82 83
      DO i = sym%nop+1,nsym
         rot(:,:,i) =  rot(:,:,i-sym%nop)
        rtau(  :,i)   = rtau(  :,i-sym%nop)
84 85 86 87 88 89 90 91 92
      END DO

      IF(any(rot(:,:,1)-reshape((/1,0,0,0,1,0,0,0,1/),(/3,3/)).ne.0))
     &  STOP 'kptgen: First symmetry operation is not the identity.'

      ALLOCATE( rrot(3,3,nsym),invtab(nsym) )    

      invtab = 0

93 94
      DO i = 1,sym%nop
        DO j = 1,sym%nop
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

          IF(    all(    matmul(rot(:,:,i),rot(:,:,j))
     &               .eq.reshape((/1,0,0,0,1,0,0,0,1/),(/3,3/)))
     &     .and.all(modulo(matmul(rot(:,:,i),rtau(:,j))+rtau(:,i),1.0)
     &               .lt.1d-10)                                  )THEN
            IF(invtab(i).ne.0) STOP 'kptgen: inverse operation
     &                              & already defined.'
            invtab(i)   = j
            rrot(:,:,i) = transpose_int ( rot(:,:,j) ) ! temporary fix for ifc
          END IF
        END DO
        IF(invtab(i).eq.0) STOP 'kptgen: inverse operation not found.'
        
      END DO


111 112
      DO i = sym%nop+1,nsym
         rrot(:,:,i) = - rrot(:,:,i-sym%nop)
113 114 115
      END DO

      ALLOCATE ( kptp(nkpt),symkpt(nkpt),rarr(3),iarr2(3),iarr(nkpt) )
Daniel Wortmann's avatar
Daniel Wortmann committed
116
      ALLOCATE ( pkpt(kpts%nkpt3(1)+1,kpts%nkpt3(2)+1,kpts%nkpt3(3)+1) )
117 118
      pkpt = 0
      DO ikpt = 1,nkpt
Daniel Wortmann's avatar
Daniel Wortmann committed
119
        iarr2 = nint ( bk(:,ikpt) * kpts%nkpt3 ) + 1
120 121 122
        pkpt(iarr2(1),iarr2(2),iarr2(3)) = ikpt
      END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
123 124 125
      pkpt(kpts%nkpt3(1)+1,    :     ,    :     ) = pkpt(1,:,:)
      pkpt(    :     ,kpts%nkpt3(2)+1,    :     ) = pkpt(:,1,:)
      pkpt(    :     ,    :     ,kpts%nkpt3(3)+1) = pkpt(:,:,1)
126
      
127 128 129 130
      IF(any(pkpt.eq.0)) THEN
         CALL juDFT_error('kptgen: Definition of pkpt-pointer failed.',
     &                    calledby='kptgen_hybrid')
      END IF
131 132 133 134 135 136 137
      iarr = 1
      ldum = .false.
      DO i = 1,nkpt
        IF(iarr(i).eq.0) CYCLE
        kptp(i)   = i
        symkpt(i) = 1
        DO k = 2,nsym
Daniel Wortmann's avatar
Daniel Wortmann committed
138
          rarr  = matmul(rrot(:,:,k),bk(:,i)) * kpts%nkpt3
139 140 141 142 143 144
          iarr2 = nint(rarr)
          IF(any(abs(iarr2-rarr).gt.1d-10)) THEN
            WRITE(6,'(A,I3,A)') 'kptgen: Symmetry operation',k,
     &                        ' incompatible with k-point set.'
            ldum = .true.
          END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
145 146
          iarr2 = modulo(iarr2,kpts%nkpt3) + 1
          IF(any(iarr2.gt.kpts%nkpt3)) 
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
     &    STOP 'kptgen: pointer indices exceed pointer dimensions.'
          j     = pkpt(iarr2(1),iarr2(2),iarr2(3))
          IF(j.eq.0) STOP 'kptgen: k-point index is zero (bug?)'
          IF(iarr(j).eq.0.or.j.eq.i) CYCLE
          iarr(j)   = 0
          kptp(j)   = i
          symkpt(j) = k
        END DO
      END DO
      IF(ldum) 
     &STOP 'kptgen: Some symmetry operations are incompatible
     & with k-point set.'
      i = 0
      DO ikpt = 1,nkpt
        IF(iarr(ikpt).eq.1) THEN
          i          = i + 1
          iarr(ikpt) = i
        END IF
      END DO
      nkpti = i
      DO ikpt = 1,nkpt
        IF(iarr(ikpt).eq.0) THEN
          i          = i + 1
          iarr(ikpt) = i
        END IF
      END DO
      bk(:,iarr)   = bk
      kptp         = iarr(kptp)
      kptp(iarr)   = kptp
      symkpt(iarr) = symkpt
Daniel Wortmann's avatar
Daniel Wortmann committed
177 178 179
      DO i=1,kpts%nkpt3(1)+1 
        DO j=1,kpts%nkpt3(2)+1
          DO k=1,kpts%nkpt3(3)+1 
180 181 182 183
            pkpt(i,j,k) = iarr(pkpt(i,j,k))
          END DO
        END DO
      END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
184
   
185 186 187 188 189 190 191 192
      ALLOCATE( neqkpt(nkpti) )
      neqkpt = 0
      DO ikpt0 = 1,nkpti
        DO ikpt = 1,nkpt
          IF( kptp(ikpt) .eq. ikpt0 ) neqkpt(ikpt0) = neqkpt(ikpt0) + 1
        END DO
      END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
193 194 195 196 197
!     Do not do any IO, but store in kpts
      kpts%nkpt=nkpti
      if (allocated(kpts%bk)) deallocate(kpts%bk)
      if (allocated(kpts%wtkpt)) deallocate(kpts%wtkpt)
      ALLOCATE(kpts%bk(3,kpts%nkpt),kpts%wtkpt(kpts%nkpt))
198

Daniel Wortmann's avatar
Daniel Wortmann committed
199
      
200
      DO ikpt=1,nkpti
Daniel Wortmann's avatar
Daniel Wortmann committed
201 202
         kpts%bk(:,ikpt)=bk(:,ikpt)
         kpts%wtkpt(ikpt)=neqkpt(ikpt)
203
      END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
204 205
      kpts%posScale=1.0
      
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
      CONTAINS

      ! Returns least common multiple of the integers iarr(1:n).
      FUNCTION kgv(iarr,n)
      IMPLICIT NONE
      INTEGER              :: kgv
      INTEGER, INTENT(IN)  :: n,iarr(n)
      LOGICAL              :: lprim(2:maxval(iarr))
      INTEGER, ALLOCATABLE :: prim(:),expo(:)
      INTEGER              :: nprim,marr
      INTEGER              :: i,j,ia,k
      ! Determine prime numbers
      marr  = maxval(iarr)
      lprim = .true.
      DO i = 2,marr
        j = 2
        DO WHILE (i*j.le.marr)
          lprim(i*j) = .false.
          j          = j + 1
        END DO
      END DO
      nprim = count(lprim)
      ALLOCATE ( prim(nprim),expo(nprim) )
      j = 0
      DO i = 2,marr
        IF(lprim(i)) THEN
          j       = j + 1
          prim(j) = i
        END IF
      END DO
      ! Determine least common multiple
      expo = 0
      DO i = 1,n
        ia = iarr(i)
        IF(ia.eq.0) CYCLE
        DO j = 1,nprim
          k = 0
          DO WHILE(ia/prim(j)*prim(j).eq.ia)
            k  = k + 1
            ia = ia / prim(j)
          END DO
          expo(j) = max(expo(j),k)
        END DO
      END DO
      kgv = 1
      DO j = 1,nprim
        kgv = kgv * prim(j)**expo(j)
      END DO
      DEALLOCATE ( prim,expo )
      END FUNCTION kgv
      

c     ifc seems to have problems transposing integer arrays. this is a fix.
      FUNCTION transpose_int ( a )
      IMPLICIT NONE
      integer transpose_int(3,3),a(3,3)
      integer i,j
      DO i = 1,3
        DO j = 1,3
          transpose_int(i,j) = a(j,i)
        END DO
      END DO
      END FUNCTION transpose_int

c     function modulo1 maps kpoint into first BZ
      FUNCTION modulo1(kpoint,nkpt,a,b,c)

      IMPLICIT NONE

      INTEGER,INTENT(IN)  :: nkpt,a,b,c
      REAL, INTENT(IN)    :: kpoint(3)
      REAL                :: modulo1(3)
      INTEGER             :: help(3),nkpt3(3)

      nkpt3 = (/a,b,c/)
      modulo1 = kpoint*nkpt3
      help    = nint(modulo1)
      IF(any(abs(help-modulo1).gt.1d-8)) THEN
        modulo1 = kpoint*nkpt3
        WRITE(*,*) modulo1
        help    = nint(modulo1)
        WRITE(*,*) help
        WRITE(6,'(A,F5.3,2('','',F5.3),A)') 'modulo1: argument (',
     &           kpoint,') is not an element of the k-point set.'
290 291 292
        CALL juDFT_error(
     +     'modulo1: argument not an element of k-point set.', 
     +     calledby = 'kptgen_hybrid:modulo1')
293 294 295 296 297 298 299 300
      END IF
      modulo1 = modulo(help,nkpt3)*1d0/nkpt3

      END FUNCTION modulo1

      END SUBROUTINE kptgen_hybrid
      
      END MODULE m_kptgen_hybrid