kptgen_hybrid.f 8.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
      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


Daniel Wortmann's avatar
Daniel Wortmann committed
19
      SUBROUTINE kptgen_hybrid(film,grid,cell,sym,kpts,l_soc)
20

Daniel Wortmann's avatar
Daniel Wortmann committed
21
      USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
22
  
23 24
      IMPLICIT NONE

Daniel Wortmann's avatar
Daniel Wortmann committed
25 26
      LOGICAL,INTENT(IN)           :: film
      INTEGER,INTENT(IN)           :: grid(3)
27 28
      TYPE(t_cell),  INTENT(IN)    :: cell
      TYPE(t_sym),   INTENT(IN)    :: sym
Daniel Wortmann's avatar
Daniel Wortmann committed
29
      TYPE(t_kpts),  INTENT(OUT)   :: 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

Daniel Wortmann's avatar
Daniel Wortmann committed
47 48
  
      nkpt=grid(1)*grid(2)*grid(3)
49 50 51
      ALLOCATE( bk(3,nkpt),bkhlp(3,nkpt) )

      ikpt = 0
Daniel Wortmann's avatar
Daniel Wortmann committed
52 53 54
      DO i=0,grid(1)-1
        DO j=0,grid(2)-1
          DO k=0,grid(3)-1
55
            ikpt       = ikpt + 1
Daniel Wortmann's avatar
Daniel Wortmann committed
56 57
            bk(:,ikpt) = (/ 1.0*i/grid(1),1.0*j/grid(2),
     &                                     1.0*k/grid(3) /)
58 59 60 61 62 63
          END DO
        END DO
      END DO
      
      IF( ikpt .ne. nkpt) STOP 'failure: number of k-points'

64 65
      IF( sym%invs .or. l_soc ) THEN
        nsym = sym%nop
66
      ELSE
67
        nsym = 2*sym%nop
68 69 70 71
      END IF
      
      ALLOCATE( rot(3,3,nsym),rtau(3,nsym) )

72 73 74
      DO i=1,sym%nop
         rot(:,:,i) = sym%mrot(:,:,i)
        rtau(  :,i) = sym%tau(:,i)
75 76
      END DO

77 78 79
      DO i = sym%nop+1,nsym
         rot(:,:,i) =  rot(:,:,i-sym%nop)
        rtau(  :,i)   = rtau(  :,i-sym%nop)
80 81 82 83 84 85 86 87 88
      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

89 90
      DO i = 1,sym%nop
        DO j = 1,sym%nop
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

          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


107 108
      DO i = sym%nop+1,nsym
         rrot(:,:,i) = - rrot(:,:,i-sym%nop)
109 110 111
      END DO

      ALLOCATE ( kptp(nkpt),symkpt(nkpt),rarr(3),iarr2(3),iarr(nkpt) )
Daniel Wortmann's avatar
Daniel Wortmann committed
112
      ALLOCATE ( pkpt(grid(1)+1,grid(2)+1,grid(3)+1) )
113 114
      pkpt = 0
      DO ikpt = 1,nkpt
Daniel Wortmann's avatar
Daniel Wortmann committed
115
        iarr2 = nint ( bk(:,ikpt) * grid ) + 1
116 117 118
        pkpt(iarr2(1),iarr2(2),iarr2(3)) = ikpt
      END DO

Daniel Wortmann's avatar
Daniel Wortmann committed
119 120 121
      pkpt(grid(1)+1,    :     ,    :     ) = pkpt(1,:,:)
      pkpt(    :     ,grid(2)+1,    :     ) = pkpt(:,1,:)
      pkpt(    :     ,    :     ,grid(3)+1) = pkpt(:,:,1)
122
      
123 124 125 126
      IF(any(pkpt.eq.0)) THEN
         CALL juDFT_error('kptgen: Definition of pkpt-pointer failed.',
     &                    calledby='kptgen_hybrid')
      END IF
127 128 129 130 131 132 133
      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
134
          rarr  = matmul(rrot(:,:,k),bk(:,i)) * grid
135 136 137 138 139 140
          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
141 142
          iarr2 = modulo(iarr2,grid) + 1
          IF(any(iarr2.gt.grid)) 
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
     &    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
173 174 175
      DO i=1,grid(1)+1 
        DO j=1,grid(2)+1
          DO k=1,grid(3)+1 
176 177 178 179
            pkpt(i,j,k) = iarr(pkpt(i,j,k))
          END DO
        END DO
      END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
180
   
181 182 183 184 185 186 187 188
      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
189 190 191 192 193
!     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))
194

Daniel Wortmann's avatar
Daniel Wortmann committed
195
      
196
      DO ikpt=1,nkpti
Daniel Wortmann's avatar
Daniel Wortmann committed
197 198
         kpts%bk(:,ikpt)=bk(:,ikpt)
         kpts%wtkpt(ikpt)=neqkpt(ikpt)
199
      END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
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
      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.'
285 286 287
        CALL juDFT_error(
     +     'modulo1: argument not an element of k-point set.', 
     +     calledby = 'kptgen_hybrid:modulo1')
288 289 290 291 292 293 294 295
      END IF
      modulo1 = modulo(help,nkpt3)*1d0/nkpt3

      END FUNCTION modulo1

      END SUBROUTINE kptgen_hybrid
      
      END MODULE m_kptgen_hybrid