tetcon.f 11.8 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 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 333 334 335 336 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
      MODULE m_tetcon
      use m_juDFT
!
! This subroutine constructs the tetrahedra for the
! Brillouin zone integration
!
      CONTAINS
      SUBROUTINE tetcon(
     >                  iofile,ibfile,mkpt,ndiv3,
     >                  nkpt,omega,kvc,nsym,
     <                  nt,voltet,ntetra)

      IMPLICIT NONE
c
      INTEGER, INTENT (IN) :: iofile,ibfile
      INTEGER, INTENT (IN) :: mkpt,ndiv3,nkpt,nsym
      REAL,    INTENT (IN) :: omega
      REAL,    INTENT (IN) :: kvc(3,mkpt)
      INTEGER, INTENT (OUT) :: nt
      INTEGER, INTENT (OUT) :: ntetra(4,ndiv3)
      REAL,    INTENT (OUT) :: voltet(ndiv3)

      INTEGER i,it,j,ic,icom,ik,nkp,jj,nnt,nsid,nkq
      INTEGER i2,i3,i4,nk2,k,l,kk,is1,is2,is3,js1,js2,js3
      INTEGER nside(4),nside2(4),nkadd(mkpt)
      REAL dnorm,pi,vav,vsq,sav,ssq,dm,dist,dl,vect
      REAL vol,vt,length,minlen,eps,eps1
      REAL vmin,vmax,smin,smax,volnew,sum,sss
      REAL ddist(mkpt),kcorn(3,4),norm(3),d(3,16),dnm(3),cn(3)
C
C----->  Intrinsic Functions
C
       INTRINSIC abs,max,min,sqrt
C
C*************************************************************************
C
C     All commons have been removed from the historical program and re-
C     placed by direct subroutine calls.  Those statements immediately
C     below preceded by stars your removed prior to my changes
C
C                                          June 9, 1992
C                                                   Fred Hutson
C
C*************************************************************************
C
      data eps/1.0d-10/
      data eps1/1.0d-5/
      pi = 4.0d0*atan(1.0d0)
C
C CONSTRUCT THE FIRST TETRAHEDRON
C
      ntetra(1,1)=1
C
C FIND K-POINT NEAREST TO 1
C
      dm=1.0d9
      i2=0
      do 100 i=2,nkpt
      dist=0.0d0
      do 110 icom=1,3
      dist=dist+(kvc(icom,1)-kvc(icom,i))**2
  110 continue
      if ( dist.gt.dm ) goto 100
      i2=i
      dm=dist
  100 continue
      IF ( i2==0 )  CALL juDFT_error(" tetcon1 ",calledby ="tetcon")
      dnorm=sqrt(dm)
      ntetra(2,1)=i2
C
C FIND POINT NEAREST TO (1+I2)/2 , NOT ON THE LINE
C CONNECTING 1 AND I2
C
      dm=1.0d9
      i3=0
      do 200 i=2,nkpt
      if ( i.eq.i2 ) goto 200
      dl=0.0d0
      dist=0.0d0
      do 210 icom=1,3
      vect=kvc(icom,i)-0.5d0*(kvc(icom,1)+kvc(icom,i2))
      dist=dist+vect*vect
      dl=dl+vect*(kvc(icom,1)-kvc(icom,i2))
  210 continue
      dl=dl/dnorm
      if ( abs(dist-dl*dl).lt.0.01d0*dist ) goto 200
      if ( dist.gt.dm ) goto 200
      dm=dist
      i3=i
  200 continue
      IF ( i3==0 )  CALL juDFT_error(" tetcon2 ",calledby ="tetcon")
      ntetra(3,1)=i3
C
C FIND POINT NEAREST TO (1+I2+I3)/3 , NOT IN THE
C PLANE (1,I2,I3)
C
      cn(1)=(kvc(2,1)-kvc(2,i2))*(kvc(3,i2)-kvc(3,i3))-
     $      (kvc(3,1)-kvc(3,i2))*(kvc(2,i2)-kvc(2,i3))
      cn(2)=(kvc(3,1)-kvc(3,i2))*(kvc(1,i2)-kvc(1,i3))-
     $      (kvc(1,1)-kvc(1,i2))*(kvc(3,i2)-kvc(3,i3))
      cn(3)=(kvc(1,1)-kvc(1,i2))*(kvc(2,i2)-kvc(2,i3))-
     $      (kvc(2,1)-kvc(2,i2))*(kvc(1,i2)-kvc(1,i3))
      dnorm=0.0d0
      do 300 icom=1,3
      dnorm=dnorm+cn(icom)**2
  300 continue
      dnorm=1.0d0/sqrt(dnorm)
      do 310 icom=1,3
      cn(icom)=cn(icom)*dnorm
  310 continue
      i4=0
      dm=1.0d9
      do 400 i=2,nkpt
      if ( (i.eq.i2).or.(i.eq.i3) ) goto 400
      dist=0.0d0
      dl=0.0d0
      do 410 icom=1,3
      vect=kvc(icom,i)-(kvc(icom,1)+kvc(icom,i2)+
     $     kvc(icom,i3))/3.0d0
      dist=dist+vect**2
      dl=dl+vect*cn(icom)
  410 continue
      if ( dl*dl.lt.0.01d0*dist ) goto 400
      if ( dist.gt.dm ) goto 400
      i4=i
      dm=dist
      vt=dl/(dnorm*6.0d0)
  400 continue
      IF ( i4==0 )  CALL juDFT_error(" tetcon3 ",calledby ="tetcon")
      ntetra(4,1)=i4
      voltet(1)=abs(vt)
C
C ENTER LOOP FOR CONSTRUCTION OF TETRAHEDRA:
C
      nt=1
      it=0
 1000 continue
      it=it+1
C
C CHECK THE SIDES OF TETRAHEDRON IT
C
      do 1100 j=1,4
C
C CHECK SIDE OPPOSITE TO CORNER J:
C
      ic=0
      do 1200 i=1,4
      if ( i.eq.j ) goto 1200
      ic=ic+1
      nside(ic)=ntetra(i,it)
      do 1300 icom=1,3
      kcorn(icom,ic)=kvc(icom,ntetra(i,it))
 1300 continue
 1200 continue
      nside(4)=ntetra(j,it)
      is1=min(nside(1),nside(2),nside(3))
      is3=max(nside(1),nside(2),nside(3))
      is2=nside(1)+nside(2)+nside(3)-is1-is3
C
C CHECK IF THERE IS ALREADY A TETRAHEDRON CONNECTED
C TO THIS SIDE:
C
      do nnt=1,nt
        do 1310 nsid=1,4
          if ( nnt.eq.it ) goto 1310
          ic=0
          do 1320 i=1,4
            if ( i.eq.nsid ) goto 1320
            ic=ic+1
            nside2(ic)=ntetra(i,nnt)
 1320     continue
          js1=min(nside2(1),nside2(2),nside2(3))
          js3=max(nside2(1),nside2(2),nside2(3))
          js2=nside2(1)+nside2(2)+nside2(3)-js1-js3
          if ( (is1.eq.js1).and.(is2.eq.js2).and.(is3.eq.js3) )
     $      goto 1100
 1310   continue
      end do
C
C CONSTRUCT THE OUTWARD NORMAL ON THIS SIDE
C
      norm(1)=(kcorn(2,2)-kcorn(2,1))*(kcorn(3,3)-kcorn(3,1))-
     $        (kcorn(3,2)-kcorn(3,1))*(kcorn(2,3)-kcorn(2,1))
      norm(2)=(kcorn(3,2)-kcorn(3,1))*(kcorn(1,3)-kcorn(1,1))-
     $        (kcorn(1,2)-kcorn(1,1))*(kcorn(3,3)-kcorn(3,1))
      norm(3)=(kcorn(1,2)-kcorn(1,1))*(kcorn(2,3)-kcorn(2,1))-
     $        (kcorn(2,2)-kcorn(2,1))*(kcorn(1,3)-kcorn(1,1))
      vol=0.0d0
      do 1400 i=1,3
      vol=vol+norm(i)*(kvc(i,ntetra(j,it))-kcorn(i,1))
 1400 continue
      vol=vol/6.0d0
      if ( vol.lt.0.0d0 ) goto 1500
      do 1600 i=1,3
      norm(i)=-norm(i)
 1600 continue
 1500 continue
      vol=abs(vol)
C
C STORE THE K-POINT ADDRESS IN NKADD ARRAY ACCORDING TO THE
C ORDER OF DISTANCE BETWEEN THE K-POINT AND THIS FACE.
C
      do 1800 nkp=1,nkpt
      length=0.0d0
      do 1850 i=1,3
      length=length+(kvc(i,nkp)
     &     -(kcorn(i,1)+kcorn(i,2)+kcorn(i,3))/3.0d0)**2
 1850 continue
      ddist(nkp)=length
      nkadd(nkp)=nkp
 1800 continue
      do 1900 nkp=1,nkpt-1
      minlen=ddist(nkp)
      ik=nkp
      do 1950 nkq=nkp+1,nkpt
      if(ddist(nkq).gt.minlen-eps) goto 1950
      minlen=ddist(nkq)
      ik=nkq
 1950 continue
      ddist(ik)=ddist(nkp)
      k=nkadd(nkp)
      nkadd(nkp)=nkadd(ik)
      nkadd(ik)=k
 1900 continue
C
C CONSTRUCT A TETRAHEDRON WHICH CONNECT THIS FACE TO
C A K-POINT.
C
      ik=0
      do 2000 nkp=1,nkpt
      do 2010 i=1,3
      if ( nkadd(nkp).eq.nside(i) ) goto 2000
 2010 continue
      do 2050 i=1,3
      kcorn(i,4)=kvc(i,nkadd(nkp))
 2050 continue
      nside(4)=nkadd(nkp)
      vt=0.0d0
      do 2100 i=1,3
      vt=vt+norm(i)*(kcorn(i,4)-kcorn(i,1))
 2100 continue
      vt=vt/6.0d0
C
C REJECT POINT NKP IF IT IS ON THE WRONG SIDE .
C
      if ( vt.lt.eps*vol ) goto 2000
C
C CHECK IF THIS TETRAHEDRON INTERSECTS AN EXISTING ONE
C
      do 3100 nk2=1,nt
      if(nk2.eq.it) goto 3100
C
C FIRST CHECK IF TETRAHEDRON NK2 IS ON THE DANGEROUS SIDE
C
      do 3150 i=1,4
      sum=0.0d0
      do 3160 jj=1,3
      sum=sum+norm(jj)*(kvc(jj,ntetra(i,nk2))-kcorn(jj,1))
 3160 continue
      if ( sum.gt.eps ) goto 3170
 3150 continue
      goto 3100
 3170 continue
C
C TETRAHEDRON NK2 IS POTENTIALLY SUSPECT
C
      l=0
      do i=1,4
        do 3200 jj=1,4
          if(ntetra(i,nk2).eq.nside(jj)) goto 3200
          sum=0.0d0
          do icom=1,3
            cn(icom)=kvc(icom,ntetra(i,nk2))-kcorn(icom,jj)
            sum=sum+cn(icom)*cn(icom)
          end do
          do icom=1,3
            cn(icom)=cn(icom)/sqrt(sum)
          end do
          do k=1,l
            sum=0.0d0
            do icom=1,3
              sum=sum+cn(icom)*d(icom,k)
            end do
            if((1.0d0-sum).lt.eps) goto 3200
C
C EXCLUDE THE VECTOR WHICH HAS THE SAME DIRECTION AS AN EXISTING ONE.
C
          end do
          l=l+1
          do icom=1,3
            d(icom,l)=cn(icom)
          end do
 3200   continue
      end do
      IF(l<4)  CALL juDFT_error(" tetcon9 ",calledby ="tetcon")
C
C HERE, WE HAVE A SET OF D-VECTORS WHICH CONNECT THE CORNER POINTS
C OF ONE TETRAHEDRON NK2 WITH THOSE OF THE CURRENT TETRAHEDRON TO
C BE CHECKED.
C FIND A PLANE OF WHICH ALL D-VECTORS EXIST IN ONE SIDE.
C
      do 3510 i=1,l-1
        do 3500 jj=i+1,l
          dnm(1)=d(2,i)*d(3,jj)-d(3,i)*d(2,jj)
          dnm(2)=d(3,i)*d(1,jj)-d(1,i)*d(3,jj)
          dnm(3)=d(1,i)*d(2,jj)-d(2,i)*d(1,jj)
C
C DNM IS THE NORMAL VECTOR TO THE PLANE GIVEN BY
C I-TH AND JJ-TH D-VECTORS.
C
          if((abs(dnm(1)).lt.eps).and.(abs(dnm(2)).lt.eps)
     &       .and.(abs(dnm(3)).lt.eps)) goto 3500
          do 3600 k=1,l
            if((k.eq.i).or.(k.eq.jj)) goto 3600
            sum=0.0d0
            do icom=1,3
              sum=sum+d(icom,k)*dnm(icom)
            end do
            if(abs(sum).lt.eps) goto 3600
            do 3700 kk=1,l
              if((kk.eq.i).or.(kk.eq.jj).or.(kk.eq.k)) goto 3700
              sss=0.0d0
              do icom=1,3
                sss=sss+d(icom,kk)*dnm(icom)
              end do
              if(abs(sss).lt.eps) goto 3700
              if(sss*sum.lt.0.0d0) goto 3500
C
C IF K-TH AND KK-TH D-VECTORS EXIST IN OPPOSITE SIDE WITH
C RESPECT TO THE PLANE GIVEN BY I-TH AND JJ-TH D-VECTORS,
C WE WILL ATTEMPT THE NEXT PLANE. (GOTO 3500)
C
 3700       continue
            goto 3100
C
C WE SUCCEED WHEN THE PLANE SATISFIES THE CONDITION,
C I.E., THE CURRENT TETRAHEDRON DOES NOT INTERSECTS NK2-TH ONE:
C WE TRY TO CHECK THE NEXT TETRAHEDRON. (GOTO 3100)
C
 3600     continue
 3500   continue
 3510 continue
      goto 2000
C
C HERE, A TETRAHEDRON MADE OF J-TH FACE AND NKADD(NKP) K-POINT
C INTERSECTS AT LEAST ONE OF THE EXISTING TETRAHEDRONS:
C WE TRY THE NEXT K-POINT. (GOTO 2000)
C
 3100 continue
      ik=nkadd(nkp)
      volnew=vt
      goto 2500
C
C HERE, WE GET THE NEW TETRAHEDRON WHICH IS MADE OF J-TH FACE
C AND IK-TH K-POINT.
C
 2000 continue
      goto 1100
C
C HERE, WE COULD NOT FIND ANY NEW TETRAHEDRON USING J-TH FACE.
C WE TRY THE NEXT FACE. (GOTO 1100)
C
 2500 continue
      nt=nt+1
      do 2300 i=1,4
      ntetra(i,nt)=ntetra(i,it)
 2300 continue
      ntetra(j,nt)=ik
      voltet(nt)=abs(volnew)
 1100 continue
      if ( it.lt.nt ) goto 1000
      IF (nt>ndiv3 )  CALL juDFT_error(" nt>ndiv3",calledby ="tetcon")
C
C DETERMINE THE CHARACTERISTICS OF THIS DIVISION INTO
C TETRAHEDRA .
C
      vav=0.0d0
      vsq=0.0d0
      sav=0.0d0
      ssq=0.0d0
      vmin=1.0d9
      vmax=-vmin
      smin=vmin
      smax=vmax
      do it=1,nt
        vt=voltet(it)
        vav=vav+vt
        vsq=vsq+vt**2
        if ( vt.gt.vmax ) vmax=vt
        if ( vt.lt.vmin ) vmin=vt
        do i=1,3
          do j=i+1,4
            dist=0.0d0
            do icom=1,3
              dist=dist+(kvc(icom,ntetra(i,it))-
     $             kvc(icom,ntetra(j,it)))**2
            end do
            dist=sqrt(dist)
            sav=sav+dist
            ssq=ssq+dist*dist
            if ( dist.gt.smax ) smax=dist
            if ( dist.lt.smin ) smin=dist
          end do
        end do
      end do
      vav=vav/nt
      sav=sav/(6*nt)
      vsq=vsq/nt
      ssq=ssq/(6*nt)
      vsq=sqrt(vsq-vav*vav)
      ssq=sqrt(ssq-sav*sav)
      write(iofile,5000) nt,vav,vsq,vmin,vmax,sav,ssq,smin,smax
 5000 format(/,'   division into tetrahedra  ',/,
     $  '  there are      ',i5,'  tetrahedra ',/,
     $  '  volume         ',f15.10,'  +/-  ',f10.5,3x,2f10.5,/,
     $  '  side           ',f15.10,'  +/-  ',f10.5,3x,2f10.5,/)
c     write(ibfile,5000) nt,vav,vsq,vmin,vmax,sav,ssq,smin,smax
      write(iofile,5100) ((ntetra(j,i),j=1,4),i=1,nt)
 5100 format(4(4x,4i4))
c     write(ibfile,5100) ((ntetra(j,i),j=1,4),i=1,nt)
C CHECK IF WE HAVE THE CORRECT TOTAL VOLUME
      vt=omega*vav*nt*nsym/(2*pi)**3-1.0d0
      write(iofile,5200) vt
c     write(ibfile,5200) vt
      do 5300 i =1,nt
c     write(ibfile,'('' tetrahedra # '',i5,'' is '',d12.4)') i,voltet(i)
 5300 continue
 5200 format(/,'  voltetsum/volBZ - 1  ',d12.4)
C
C     The following statement used to have a stop in it.
C     If the word TETCON5 appears you have failed the < 1.0D-5 test.
C
      if ( abs(vt).gt.eps1 ) write(iofile,'(''  tetcon5  '')')
      RETURN
      END SUBROUTINE tetcon
      END MODULE m_tetcon