strgn.f90 26.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
MODULE m_strgn
  USE m_juDFT
  !
  !     *********************************************************
  !     generate two- and three-dimensional stars
  !     for slab geometry
  !     e. wimmer   nov.1984    c.l.fu  1987
  !     implementation of new box-dimension: to treat nonorthogonal
  !     lattice systems
  !     S. Bl"ugel, IFF, 17.Nov.97
11 12 13
  !
  !     OpenMP paralleliation added
  !     U.Alekseeva          Jan.2019
14 15 16 17 18 19 20 21 22 23 24
  !     *********************************************************
CONTAINS
  SUBROUTINE strgn1(&
       &                  stars,sym,atoms,&
       &                  vacuum,sphhar,input,cell,xcpot)
    !
    USE m_spgrot
    USE m_angle
    USE m_types
    USE m_boxdim
    USE m_sort
25
    USE m_cdn_io
26 27 28 29 30 31 32 33
    IMPLICIT NONE
    TYPE(t_stars),INTENT(INOUT)  :: stars
    TYPE(t_sym),INTENT(IN)       :: sym
    TYPE(t_atoms),INTENT(IN)     :: atoms
    TYPE(t_vacuum),INTENT(IN)    :: vacuum
    TYPE(t_sphhar),INTENT(IN)    :: sphhar
    TYPE(t_input),INTENT(IN)     :: input
    TYPE(t_cell),INTENT(IN)      :: cell
34
    CLASS(t_xcpot),INTENT(IN)    :: xcpot
35 36 37 38 39 40 41 42

    !     ..
    !     ..
    !     .. Local Scalars ..
    REAL arltv1,arltv2,arltv3,s
    REAL gmi,gla,eps 
    REAL gfx,gfy,pon,pon2
    INTEGER j,k,k1,k2,k3,m0,mxx1,mxx2,n
43
    INTEGER ned1,nint,kdone,i,i_sym,n_sym
44
    LOGICAL NEW,l_cdn1,l_xcExtended, l_error
45 46 47 48 49 50
    INTEGER kfx,kfy,kfz,kidx,nfftx,nffty,nfftz,kfft
    INTEGER nfftxy,norm,n1,kidx2,k2i
    !     ..
    !     .. Local Arrays ..
    REAL,    ALLOCATABLE :: gsk3(:)
    INTEGER, ALLOCATABLE :: ig2p(:),INDEX(:),index3(:),kv3rev(:,:)
51
    REAL g(3),phi3(stars%ng2),phi
52 53
    COMPLEX phas(sym%nop)
    INTEGER kr(3,sym%nop),kv(3)
54
    INTEGER index2(stars%ng2)
55 56 57 58
    !     ..
    !     ..
    !
    !
59 60 61 62
    nfftx = 3*stars%mx1
    nffty = 3*stars%mx2
    nfftz = 3*stars%mx3
    nfftxy= 9*stars%mx1*stars%mx2
63

64
    ALLOCATE (gsk3(stars%ng3),INDEX(stars%ng3),index3(stars%ng3),kv3rev(stars%ng3,3))
65 66

    !
67
    !WRITE (*,*) ' stars are always ordered '
68

69
    l_xcExtended = xcpot%needs_grad()
70 71 72 73 74
    !--->    read in information if exists
    CALL readStars(stars,l_xcExtended,.TRUE.,l_error)
    IF(.NOT.l_error) THEN
       GOTO 270
    END IF
75
 
76 77 78 79 80 81
    IF (input%film.AND.sym%invs.AND.(.not.sym%zrfs).AND.(.not.sym%symor)) THEN
      n_sym = 2 ! needs reordering of 2d-stars
    ELSE
      n_sym = 1 ! as before...
    ENDIF

82 83 84 85 86
    mxx1 = 0
    mxx2 = 0
    stars%ng2 = 0
    kv(3) = 0

87
    stars%kv2 = 0
88 89 90 91
    DO  k1 = stars%mx1,-stars%mx1,-1
       kv(1) = k1
       k2_loop:DO  k2 = stars%mx2,-stars%mx2,-1
          kv(2) = k2
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

          DO i_sym = 1, n_sym
            IF (i_sym == 2) THEN
               kv(1) = - kv(1) ; kv(2) = - kv(2)
            ENDIF

            DO j = 1,2
               g(j) = kv(1)*cell%bmat(1,j) + kv(2)*cell%bmat(2,j)
            ENDDO
            s = SQRT(g(1)**2+g(2)**2)
            !--->   determine the angle of the G_{||} needed in odim calculations
            !+odim YM cute little 'angle' is written by me to determine the phi2
            phi = angle(g(1),g(2))
            !-odim
            !
            !--->   check if generated vector belongs to a star already
            !--->   stars should be within the g_max-sphere !   (Oct.97) sbluegel
            !
            IF (s.LT.stars%gmax) THEN      
               CALL spgrot(&
                    &                     sym%nop,sym%symor,sym%mrot,sym%tau,sym%invtab,&
                    &                     kv,&
                    &                     kr)
               DO n = 1,sym%nop2
                  IF (mxx1.LT.kr(1,n)) mxx1 = kr(1,n)
                  IF (mxx2.LT.kr(2,n)) mxx2 = kr(2,n)
               ENDDO
               DO k = 1,stars%ng2 
                  DO n = 1,sym%nop2
                     IF (kr(1,n).EQ.stars%kv2(1,k) .AND.&
                          &                   kr(2,n).EQ.stars%kv2(2,k)) CYCLE k2_loop
                  ENDDO
               ENDDO
               !--->    new representative found
               stars%ng2 = stars%ng2 + 1
               IF (stars%ng2.GT.stars%ng2) THEN
                  WRITE (6,8070) stars%ng2,stars%ng2
                  CALL juDFT_error("nq2.GT.n2d",calledby="strgn")
               ENDIF
               DO j = 1,2
                  stars%kv2(j,stars%ng2) = kv(j)
               ENDDO
               stars%sk2(stars%ng2) = s
               stars%phi2(stars%ng2) = phi
            ENDIF
            ENDDO ! i_sym

139 140 141 142
       ENDDO k2_loop
    ENDDO
8070 FORMAT ('nq2 = ',i5,' > n2d =',i5)

143 144
    IF( mxx1.GT.stars%mx1) THEN
       WRITE (6,'(/'' mxx1.gt.k1d. mxx1,k1d='',2i4)')  mxx1,stars%mx1
145 146 147
       CALL juDFT_error("mxx1.gt.k1d",calledby="strgn")
    ENDIF

148 149
    IF (mxx2.GT.stars%mx2) THEN
       WRITE (6,'(/'' mxx2.gt.k2d. mxx2,k2d='',2i4)')  mxx2,stars%mx2
150 151 152 153
       CALL juDFT_error("mxx2.gt.k2d",calledby="strgn")
    ENDIF

    !--->    sort for increasing length sk2
154 155 156 157
    DO  k = 1,stars%ng2
       gsk3(k) = (stars%mx1+stars%kv2(1,k)) + (stars%mx2+stars%kv2(2,k))*(2*stars%mx1+1)
    ENDDO
    CALL sort(INDEX(:stars%ng2),stars%sk2(:stars%ng2),gsk3(:stars%ng2))
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    DO k = 1,stars%ng2
       kv3rev(k,1) = stars%kv2(1,INDEX(k))
       kv3rev(k,2) = stars%kv2(2,INDEX(k))
       gsk3(k) = stars%sk2(INDEX(k))
       phi3(k) = stars%phi2(INDEX(k))
    ENDDO
    DO k = 1,stars%ng2
       stars%kv2(1,k) = kv3rev(k,1)
       stars%kv2(2,k) = kv3rev(k,2)
       stars%sk2(k) = gsk3(k)
       stars%phi2(k) = phi3(k)
    ENDDO


    WRITE (6,'(/'' nq2='',i4/'' k,kv2(1,2), sk2, phi2''&
         &     /(3i4,f10.5,f10.5))')&
         &  stars%ng2,(k,stars%kv2(1,k),stars%kv2(2,k),stars%sk2(k),stars%phi2(k),k=1,stars%ng2)
    !
    !     three dimensional stars
    !
    stars%ng3 = 0
179
    stars%ig = 0
180 181 182
    DO k3 = -stars%mx3,stars%mx3
       DO k2 = -stars%mx2,stars%mx2
          DO k1 = -stars%mx1,stars%mx1
183
             stars%ig(k1,k2,k3) = 0
184
             stars%rgphs(k1,k2,k3) = cmplx(0.0,0.0)
185 186 187 188 189 190 191 192 193 194 195
          ENDDO
       ENDDO
    ENDDO
    !+gu
    stars%igfft2(:,:)=0
    stars%igfft(:,:)=0
    stars%pgfft=0.0
    stars%pgfft2=0.0
    !-gu
    WRITE (6,'(/'' bmat(3,3),mx3='',f10.5,i5)') cell%bmat(3,3),stars%mx3

196 197
    IF (stars%mx3.GT.stars%mx3) THEN
       WRITE ( 6,FMT=8000) stars%mx3,stars%mx3
198 199 200 201 202 203 204 205
       CALL juDFT_error("mx3.gt.k3d",calledby="strgn")
    ENDIF
8000 FORMAT('   mx3.gt.k3d:',2i6)

    m0 = -stars%mx3
    !     zrfs,invs: z-reflection, inversion.
    IF (sym%zrfs .OR. sym%invs) m0 = 0

206 207 208
    stars%ig2 = 0
    stars%sk3 = 0.0
    stars%kv3 = 0
209 210 211 212 213 214 215 216
    DO  k2 = 1,stars%ng2
       DO  k3 = m0,stars%mx3
          s = SQRT(stars%sk2(k2)**2+ (k3*cell%bmat(3,3))**2)
          !
          !--->   stars should be within the g_max-sphere !   (Oct.97) sbluegel
          IF (s.LT.stars%gmax) THEN      
             !
             stars%ng3 = stars%ng3 + 1
217 218
             IF (stars%ng3.GT.stars%ng3) THEN
                WRITE (6,*) stars%ng3,stars%ng3
219 220 221 222 223 224 225 226 227 228 229 230 231
                CALL juDFT_error("nq3.GT.n3d",calledby="strgn")
             ENDIF
             DO j = 1,2
                stars%kv3(j,stars%ng3) = stars%kv2(j,k2)
             ENDDO
             stars%kv3(3,stars%ng3) = k3
             stars%ig2(stars%ng3) = k2
             stars%sk3(stars%ng3) = s
          ENDIF
       ENDDO
    ENDDO

    !--->    sort for increasing length sk3
232 233 234 235 236 237
    ! secondary key for equal length stars
    DO  k = 1,stars%ng3
       gsk3(k) = (stars%mx1+stars%kv3(1,k)) +&
            &           (stars%mx2+stars%kv3(2,k))*(2*stars%mx1+1) +&
            &           (stars%mx3+stars%kv3(3,k))*(2*stars%mx1+1)*(2*stars%mx2+1)
    ENDDO
238
    CALL sort(index(:stars%ng3),stars%sk3,gsk3)
239

240
    ALLOCATE (ig2p(stars%ng3))
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    DO k = 1,stars%ng3
       kv3rev(k,1) = stars%kv3(1,INDEX(k))
       kv3rev(k,2) = stars%kv3(2,INDEX(k))
       kv3rev(k,3) = stars%kv3(3,INDEX(k))
       gsk3(k) = stars%sk3(INDEX(k))
       ig2p(k) = stars%ig2(INDEX(k))
    ENDDO
    DO k = 1,stars%ng3
       stars%kv3(1,k) = kv3rev(k,1)
       stars%kv3(2,k) = kv3rev(k,2)
       stars%kv3(3,k) = kv3rev(k,3)
       stars%sk3(k) = gsk3(k)
       stars%ig2(k) = ig2p(k)
    ENDDO
256
  
257 258 259 260 261 262 263 264 265 266 267 268 269 270
    !
    !--->  determine true gmax and change old gmax to new gmax
    !
    WRITE (6,8060) stars%gmax, stars%sk3(stars%ng3)
    stars%gmax = stars%sk3(stars%ng3)
8060 FORMAT (/,1x,'old stars%gmax    =',f10.5, '(a.u.)**(-1) ==>  new stars%gmax  '&
         &       ,'  =',f10.5,'(a.u.)**(-1) ',/,t38,'==>  new E_cut   =',&
         &            f10.5,' Ry')

    !--->    generate all star members
    !+gu
    kidx=0
    kidx2=0
    !-gu
271
    stars%rgphs(:,:,:) = cmplx(0.0,0.0)
272 273
    stars%ft2_gfx = 0.0
    stars%ft2_gfy = 0.0
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
    DO  k = 1,stars%ng3

       CALL spgrot(&
            &               sym%nop,sym%symor,sym%mrot,sym%tau,sym%invtab,&
            &               stars%kv3(:,k),&
            &               kr,phas)

       IF (stars%kv3(3,k).EQ.0) THEN

          DO  n = 1,sym%nop2
             !+gu
             ! -->       set up the igfft(*,3) array as (1d) fft-pointer:
             !
             !           star ------------> g-vector ------------> fft-grid & phase
             !                igfft(*,1)             igfft(*,2)           igfft(*,3)
             !
             !           size of fft-grid is chosen to be ( 3*k1d x 3*k2d x 3*k3d )
             !
             NEW=.TRUE.

             DO n1=1,n-1
                norm=(kr(1,n)-kr(1,n1))**2 +&
                     &                (kr(2,n)-kr(2,n1))**2 +&
                     &                (kr(3,n)-kr(3,n1))**2
                IF (norm.EQ.0) NEW=.FALSE.
             ENDDO

             IF (NEW) THEN

                kfx = kr(1,n)
                kfy = kr(2,n)
                kfz = kr(3,n)
                !+guta
                gfx = cell%bmat(1,1)*kfx+cell%bmat(2,1)*kfy+cell%bmat(3,1)*kfz 
                gfy = cell%bmat(1,2)*kfx+cell%bmat(2,2)*kfy+cell%bmat(3,2)*kfz
                !-guta
                IF (kfx.LT.0) kfx = kfx+nfftx
                IF (kfy.LT.0) kfy = kfy+nffty
                IF (kfz.LT.0) kfz = kfz+nfftz
                kfft = kfx + kfy*nfftx + kfz*nfftxy
                !
                ! -->            store the number of the star, its position 
                !c                 on fft-grid and phase
                !
                stars%igfft(kidx,1) = k
                stars%igfft(kidx,2) = kfft
                stars%pgfft(kidx)   = phas(n)
                kidx          = kidx+1
                !
                ! -->            now for 2d - stars
                !
                kfft=kfx + kfy*nfftx
                DO k2 = 1,stars%ng2
                   IF ((stars%kv3(1,k).EQ.stars%kv2(1,k2)).AND.&
                        &                 (stars%kv3(2,k).EQ.stars%kv2(2,k2))) k2i = k2
                ENDDO
                stars%igfft2(kidx2,1) = k2i
                stars%igfft2(kidx2,2) = kfft
                stars%pgfft2(kidx2)   = phas(n)
                !+guta
334
                IF (xcpot%needs_grad()) THEN
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
                   !!                   pgft2x: exp(i*(gfx,gfy,gfz)*tau)*gfx.
                   !!                        y                             y.
                   !!                   pgft2xx: exp(i*(gfx,gfy,gfz)*tau)*gfx*gfx.
                   !!                        yy                             y   y
                   !!                        xy                             x   y

                   stars%ft2_gfx(kidx2)  = gfx
                   stars%ft2_gfy(kidx2)  = gfy
                   !                    stars%pgft2xx(kidx2) = phas(n)*gfx*gfx
                   !                    stars%pgft2yy(kidx2) = phas(n)*gfy*gfy
                   !                    stars%pgft2xy(kidx2) = phas(n)*gfx*gfy
                ENDIF
                !-guta
                kidx2=kidx2+1

             ENDIF
             !-gu
             stars%ig(kr(1,n),kr(2,n),kr(3,n)) = k
             stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) = &
                  &         stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) + phas(n)

          ENDDO

       ELSE
          !        here: kv3(3,k) =/= 0 

          DO  n = 1,sym%nop
             !+gu
             NEW=.TRUE.
             DO n1 = 1,n-1
                norm=(kr(1,n)-kr(1,n1))**2 +&
                     &                (kr(2,n)-kr(2,n1))**2 +&
                     &                (kr(3,n)-kr(3,n1))**2
                IF (norm.EQ.0) NEW = .FALSE.
             ENDDO

             IF (NEW) THEN

                kfx = kr(1,n)
                kfy = kr(2,n)
                kfz = kr(3,n)
                IF (kfx.LT.0) kfx = kfx+nfftx
                IF (kfy.LT.0) kfy = kfy+nffty
                IF (kfz.LT.0) kfz = kfz+nfftz

                kfft=kfx + kfy*nfftx + kfz*nfftxy
                stars%igfft(kidx,1)=k
                stars%igfft(kidx,2)=kfft
                stars%pgfft(kidx)=phas(n)
                kidx=kidx+1

             ENDIF
             !-gu
             stars%ig(kr(1,n),kr(2,n),kr(3,n)) = k
             stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) = &
                  &         stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) + phas(n)

          ENDDO

       ENDIF

    ENDDO
    !    
    stars%kimax=kidx-1
    stars%kimax2=kidx2-1
    !
    !     count number of members for each star
    !     nstr2 ... members of 2-dim stars
    !
404 405 406

    stars%nstr2(:) = 0
    stars%nstr(:) = 0
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

    DO k3 = -stars%mx3,stars%mx3
       DO k2 = -mxx2,mxx2
          DO k1 = -mxx1,mxx1
             k = stars%ig(k1,k2,k3)
             IF ( k .NE. 0 ) THEN
                stars%nstr(k) = stars%nstr(k) + 1
                stars%nstr2(stars%ig2(k)) = stars%nstr2(stars%ig2(k)) +(1-min0(iabs(k3),1))
             ENDIF
          ENDDO
       ENDDO
    ENDDO
    !
    ! normalize phases:
    !
    IF (sym%symor) THEN
423
       stars%rgphs(:,:,:) = cmplx(1.0,0.0)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    ELSE
       pon = 1.0 / sym%nop
       pon2 = 1.0 / sym%nop2
       DO k3 = -stars%mx3,stars%mx3
          DO k2 = -mxx2,mxx2
             DO k1 = -mxx1,mxx1

                kfx = k1 ; kfy = k2; kfz = k3
                k = stars%ig(k1,k2,k3)

                IF (kfx.LT.0) kfx = kfx+nfftx
                IF (kfy.LT.0) kfy = kfy+nffty
                IF (kfz.LT.0) kfz = kfz+nfftz
                kfft=kfx + kfy*nfftx + kfz*nfftxy
                IF (k.GT.0) THEN
                   IF (stars%kv3(3,k).EQ.0) THEN
                      stars%rgphs(k1,k2,k3) = stars%rgphs(k1,k2,k3) * stars%nstr(k)*pon2
                   ELSE
                      stars%rgphs(k1,k2,k3) = stars%rgphs(k1,k2,k3) * stars%nstr(k)*pon
                   ENDIF
                   kidx = -90
                   DO i = 1, stars%kimax
                      IF ( stars%igfft(i,2) == kfft ) kidx = i
                   ENDDO
                   IF ( kidx > 0 ) stars%pgfft(kidx)=stars%rgphs(k1,k2,k3)
                ENDIF
             ENDDO
          ENDDO
       ENDDO
    ENDIF

455 456 457 458 459
    
    IF (stars%mx1< mxx1 .or.  stars%mx2< mxx2) then
          print *,stars%mx1, mxx1, stars%mx2, mxx2       
          CALL judft_error("BUG in strgn")
    endif
460 461
    !stars%mx1=mxx1
    !stars%mx2=mxx2
462 463 464

    !--->    write /str0/ and /str1/ to file
    CALL writeStars(stars,l_xcExtended,.TRUE.)
465 466 467 468 469

270 CONTINUE
    !
    !-->  listing
    !
470
8010 FORMAT (' gmax=',f10.6,/,' nq3=  ',i5,/,' nq2=  ',i5,/)
471 472 473 474
8020 FORMAT (' mx1= ',i5,/,' mx2= ',i5,/)
    WRITE (6,FMT=8030)
8030 FORMAT (/,/,/,'   s t a r   l i s t',/)

475
    WRITE (6,FMT=8010) stars%gmax,stars%ng3,stars%ng2
476 477 478 479 480 481
    WRITE (6,'('' mx1,mx2,mx3='',3i3)') stars%mx1,stars%mx2,stars%mx3
    WRITE (6,'('' kimax2,kimax='',2i7,'', (start from 0)'')') stars%kimax2,&
         &  stars%kimax

    WRITE (6,FMT=8040)
8040 FORMAT(/4x,'no.',5x,'kv3',9x,'sk3',9x,'sk2',5x,&
482
         &  'ig2',1x,'nstr',2x,'nstr2'/)
483 484 485 486 487 488

    ned1=9
    nint=30
    DO k = 1,ned1
       WRITE (6,FMT=8050) k,(stars%kv3(j,k),j=1,3),stars%sk3(k),&
            &                     stars%sk2(stars%ig2(k)),&
489
            &                     stars%ig2(k),stars%nstr(k),stars%nstr2(stars%ig2(k))
490
    ENDDO
491
8050 FORMAT (1x,i5,3i4,2f12.6,i4,2i6)
492 493 494 495

    DO k = ned1+1,stars%ng3,nint
       WRITE (6,FMT=8050) k,(stars%kv3(j,k),j=1,3),stars%sk3(k),&
            &                     stars%sk2(stars%ig2(k)),&
496
            &                     stars%ig2(k),stars%nstr(k),stars%nstr2(stars%ig2(k))
497 498 499 500
       kdone = k
    ENDDO

    IF (kdone.LT.stars%ng3) THEN
501 502 503
       WRITE (6,FMT=8050) stars%ng3,(stars%kv3(j,stars%ng3),j=1,3),stars%sk3(stars%ng3),&
            &                     stars%sk2(stars%ig2(stars%ng3)),&
            &                     stars%ig2(stars%ng3),stars%nstr(stars%ng3),stars%nstr2(stars%ig2(stars%ng3))
504 505 506 507 508 509 510 511 512 513 514 515 516 517
    ENDIF

    DEALLOCATE (gsk3,index,index3,kv3rev)

  END SUBROUTINE strgn1
  !----------------------------------------------------------------
  SUBROUTINE strgn2(&
       &                  stars,sym,atoms,&
       &                  vacuum,sphhar,input,cell,xcpot)
    USE m_boxdim
    USE m_sort
    USE m_spgrot
    USE m_constants
    USE m_types
518
    USE m_cdn_io
519 520 521 522 523 524 525 526
    IMPLICIT NONE
    TYPE(t_stars),INTENT(INOUT)  :: stars
    TYPE(t_sym),INTENT(IN)       :: sym
    TYPE(t_atoms),INTENT(IN)     :: atoms
    TYPE(t_vacuum),INTENT(IN)    :: vacuum
    TYPE(t_sphhar),INTENT(IN)    :: sphhar
    TYPE(t_input),INTENT(IN)     :: input
    TYPE(t_cell),INTENT(IN)      :: cell
527
    CLASS(t_xcpot),INTENT(IN)    :: xcpot
528 529 530 531 532 533 534
    !     ..
    !     .. Local Scalars ..
    REAL arltv1,arltv2,arltv3,s
    REAL gmi,gla,eps,gmax2,pon
    REAL gfx,gfy
    INTEGER j,k,k1,k2,k3,m0,mxx1,mxx2,mxx3,n
    INTEGER ned1,nint,kdone,i
535
    LOGICAL NEW,l_cdn1,l_error,l_xcExtended
536
    INTEGER kfx,kfy,kfz,kidx,nfftx,nffty,nfftz,kfft
537
    INTEGER nfftxy,norm,n1,kidx2,k2i
538 539 540 541 542 543 544
    !     ..
    !     .. Local Arrays ..
    REAL, ALLOCATABLE :: gsk3(:)
    INTEGER, ALLOCATABLE :: INDEX(:),index3(:),kv3rev(:,:)
    REAL g(3)
    COMPLEX phas(sym%nop)
    INTEGER kr(3,sym%nop),kv(3)
545 546
    INTEGER index2(stars%ng2)

547 548
    !     ..
    !
549 550 551 552
    nfftx = 3*stars%mx1
    nffty = 3*stars%mx2
    nfftz = 3*stars%mx3
    nfftxy= 9*stars%mx1*stars%mx2
553

554
    ALLOCATE (gsk3(stars%ng3),INDEX(stars%ng3),index3(stars%ng3),kv3rev(stars%ng3,3))
555 556 557 558

    !
    WRITE (*,*) ' stars are always ordered '

559
    l_xcExtended = xcpot%needs_grad()
560
    !--->    read in information if exists
561 562 563 564
    CALL readStars(stars,l_xcExtended,.FALSE.,l_error)
    IF(.NOT.l_error) THEN
       GOTO 270
    END IF
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

    mxx1 = 0
    mxx2 = 0
    mxx3 = 0
    stars%ng3 = 0
    stars%ig = 0
    gmax2 = stars%gmax * stars%gmax

    x_dim: DO k1 = stars%mx1,-stars%mx1,-1
       kv(1) = k1
       y_dim: DO k2 = stars%mx2,-stars%mx2,-1
          kv(2) = k2
          z_dim: DO k3 = stars%mx3,-stars%mx3,-1
             IF ( stars%ig(k1,k2,k3) .NE. 0 ) CYCLE z_dim
             kv(3) = k3

             DO j = 1,3
                g(j) = kv(1)*cell%bmat(1,j) + kv(2)*cell%bmat(2,j) +&
                     &                kv(3)*cell%bmat(3,j)
             ENDDO
             s = g(1)**2 + g(2)**2 + g(3)**2
             !
             !--->   check if generated vector belongs to a star already
             IF (s.LT.gmax2) THEN      
                !
                !--->    new representative found
                CALL spgrot(&
                     &                     sym%nop,sym%symor,sym%mrot,sym%tau,sym%invtab,&
                     &                     kv,&
                     &                     kr)
                stars%ng3 = stars%ng3 + 1
596 597
                IF (stars%ng3.GT.stars%ng3) THEN
                   WRITE (6,'("nq3 = ",i5," > n3d =",i5)') stars%ng3,stars%ng3
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
                   CALL juDFT_error("nq3>n3d",calledby ="strgn")
                ENDIF
                DO j = 1,3
                   stars%kv3(j,stars%ng3) = kv(j)
                ENDDO
                stars%sk3(stars%ng3) = SQRT(s)
                DO n = 1,sym%nop
                   IF (mxx1.LT.kr(1,n)) mxx1 = kr(1,n)
                   IF (mxx2.LT.kr(2,n)) mxx2 = kr(2,n)
                   IF (mxx3.LT.kr(3,n)) mxx3 = kr(3,n)
                   stars%ig(kr(1,n),kr(2,n),kr(3,n)) = stars%ng3
                ENDDO
             ENDIF
          ENDDO z_dim
       ENDDO y_dim
    ENDDO x_dim

615 616
    IF( mxx1.GT.stars%mx1) THEN
       WRITE (6,'(/'' mxx1.gt.k1d. mxx1,k1d='',2i4)')  mxx1,stars%mx1
617 618 619
       CALL juDFT_error("mxx1>k1d",calledby ="strgn")
    ENDIF

620 621
    IF (mxx2.GT.stars%mx2) THEN
       WRITE (6,'(/'' mxx2.gt.k2d. mxx2,k2d='',2i4)')  mxx2,stars%mx2
622 623 624
       CALL juDFT_error("mxx2>k2d",calledby ="strgn")
    ENDIF

625 626
    IF (mxx3.GT.stars%mx3) THEN
       WRITE (6,'(/'' mxx3.gt.k3d. mxx3,k3d='',2i4)')  mxx3,stars%mx3
627 628 629 630
       CALL juDFT_error("mxx3>k3d",calledby ="strgn")
    ENDIF

    !--->    sort for increasing length sk3
631 632 633 634 635 636
    ! secondary key for equal length stars
    DO  k = 1,stars%ng3
       gsk3(k) = (stars%mx1+stars%kv3(1,k)) +&
            &           (stars%mx2+stars%kv3(2,k))*(2*stars%mx1+1) +&
            &           (stars%mx3+stars%kv3(3,k))*(2*stars%mx1+1)*(2*stars%mx2+1)
    ENDDO
637
    CALL sort(index(:stars%ng3),stars%sk3,gsk3)
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    DO k = 1,stars%ng3
       kv3rev(k,1) = stars%kv3(1,INDEX(k))
       kv3rev(k,2) = stars%kv3(2,INDEX(k))
       kv3rev(k,3) = stars%kv3(3,INDEX(k))
       gsk3(k) = stars%sk3(INDEX(k))
    ENDDO
    DO k = 1,stars%ng3
       stars%kv3(1,k) = kv3rev(k,1)
       stars%kv3(2,k) = kv3rev(k,2)
       stars%kv3(3,k) = kv3rev(k,3)
       stars%sk3(k) = gsk3(k)
    ENDDO
    !
    !--->  determine true gmax and change old gmax to new gmax
    !
    WRITE (6,8060) stars%gmax, stars%sk3(stars%ng3)
    stars%gmax = stars%sk3(stars%ng3)
8060 FORMAT (/,1x,'gmax    =',f10.5, '(a.u.)**(-1) ==>  new gmax  '&
         &       ,'  =',f10.5,'(a.u.)**(-1) ',/,t38,'==>  new E_cut   =',&
         &            f10.5,' Ry')
658

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
    !--->    generate all star members
    !+gu
    kidx=0
    kidx2=0
    DO k3 = -mxx3,mxx3
       DO k2 = -mxx2,mxx2
          DO k1 = -mxx1,mxx1
             stars%ig(k1,k2,k3) = 0
          ENDDO
       ENDDO
    ENDDO

    !-gu
    !
    ! sum over phases
    !
675
    stars%rgphs(:,:,:) = cmplx(0.0,0.0)
676 677
    stars%igfft = 0
    stars%pgfft = cmplx(0.0,0.0)
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    starloop: DO k = 1,stars%ng3

       CALL spgrot(&
            &               sym%nop,sym%symor,sym%mrot,sym%tau,sym%invtab,&
            &               stars%kv3(:,k),&
            &               kr,phas)
       !
       ! -->    set up the igfft(*,3) array as (1d) fft-pointer:
       !
       !        star ------------> g-vector ------------> fft-grid & phase
       !             igfft(*,1)             igfft(*,2)           igfft(*,3)
       !
       !        size of fft-grid is chosen to be ( 3*k1d x 3*k2d x 3*k3d )
       !
       ops: DO n = 1,sym%nop

          NEW=.TRUE.
          DO n1 = 1,n-1
             norm=(kr(1,n)-kr(1,n1))**2 +&
                  &             (kr(2,n)-kr(2,n1))**2 +&
                  &             (kr(3,n)-kr(3,n1))**2
             IF (norm.EQ.0) NEW = .FALSE.
          ENDDO

          IF (NEW) THEN

             kfx = kr(1,n)
             kfy = kr(2,n)
             kfz = kr(3,n)
             IF (kfx.LT.0) kfx = kfx+nfftx
             IF (kfy.LT.0) kfy = kfy+nffty
             IF (kfz.LT.0) kfz = kfz+nfftz

             kfft=kfx + kfy*nfftx + kfz*nfftxy
             stars%igfft(kidx,1)=k
             stars%igfft(kidx,2)=kfft
             stars%pgfft(kidx)=phas(n)
             kidx=kidx+1

          ENDIF
          stars%ig(kr(1,n),kr(2,n),kr(3,n)) = k
          stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) = &
               &      stars%rgphs(kr(1,n),kr(2,n),kr(3,n)) + phas(n)

       ENDDO ops
    ENDDO starloop
    !    
    stars%kimax=kidx-1
    !
    !     count number of members for each star
    !
    DO k = 1,stars%ng3
       stars%nstr(k) = 0
    ENDDO

    DO k3 = -mxx3,mxx3
       DO k2 = -mxx2,mxx2
          DO k1 = -mxx1,mxx1
             k = stars%ig(k1,k2,k3)
             IF ( k .NE. 0 ) THEN
                stars%nstr(k) = stars%nstr(k) + 1
             ENDIF
             !               DO k = 1,nq3
             !                  IF (ig(k1,k2,k3).eq.k) THEN
             !                     nstr(k) = nstr(k) + 1
             !                  ENDIF
             !               ENDDO
          ENDDO
       ENDDO
    ENDDO
    !
    ! normalize phases:
    !
    IF (sym%symor) THEN
752
       stars%rgphs(:,:,:) = cmplx(1.0,0.0)
753 754
    ELSE
       pon = 1.0 / sym%nop
755 756 757 758
       !$OMP PARALLEL DO &
       !$OMP DEFAULT(none) &
       !$OMP SHARED(mxx1,mxx2,mxx3,stars,nfftx,nffty,nfftz,nfftxy,pon) &
       !$OMP PRIVATE(k1,k2,k3,k,kfx,kfy,kfz,kfft,kidx,i)
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
       DO k3 = -mxx3,mxx3
          DO k2 = -mxx2,mxx2
             DO k1 = -mxx1,mxx1
                k = stars%ig(k1,k2,k3)
                IF (k.GT.0) THEN
                   stars%rgphs(k1,k2,k3) = stars%rgphs(k1,k2,k3) * stars%nstr(k)*pon
                   kfx = k1 ; kfy = k2; kfz = k3
                   IF (kfx.LT.0) kfx = kfx+nfftx
                   IF (kfy.LT.0) kfy = kfy+nffty
                   IF (kfz.LT.0) kfz = kfz+nfftz
                   kfft=kfx + kfy*nfftx + kfz*nfftxy
                   kidx = -90
                   DO i = 1, stars%kimax
                      IF ( stars%igfft(i,2) == kfft ) kidx = i
                   ENDDO
                   IF (kidx > 0 ) stars%pgfft(kidx)=stars%rgphs(k1,k2,k3)
                ENDIF
             ENDDO
          ENDDO
       ENDDO
779
       !OMP END PARALLLEL DO
780
    ENDIF
781
    if ( stars%mx1 < mxx1 .or. stars%mx2 < mxx2 .or. stars%mx3 < mxx3 ) call &
782
         judft_error("BUG 1 in strgen") 
783
    stars%ng2 = 2 ; stars%kv2 = 0 ; stars%ig2 = 0 ; stars%kimax2= 0 ; stars%igfft2 = 0
784
    stars%sk2 = 0.0 ; stars%pgfft2 = 0.0  ; stars%nstr2 = 0
785
    IF (xcpot%needs_grad()) THEN
786 787
       stars%ft2_gfx = 0.0 ; stars%ft2_gfy = 0.0 
    ENDIF
788 789

    !--->    write /str0/ and /str1/ to file
790
    CALL timestart("writeStars") 
791
    CALL writeStars(stars,l_xcExtended,.FALSE.)
792
    CALL timestop("writeStars") 
793 794

270 CONTINUE
795

796 797
    !
    !-->  listing
798
8010 FORMAT (' gmax=',f10.6,/,' nq3=  ',i7,/)
799 800 801 802 803
8020 FORMAT (' mx1= ',i5,/,' mx2= ',i5,' mx3= ',i5,/)
    WRITE (6,FMT=8030)
8030 FORMAT (/,/,/,'   s t a r   l i s t',/)


804
    WRITE (6,FMT=8010) stars%gmax,stars%ng3
805 806 807 808 809
    WRITE (6,'('' mx1,mx2,mx3='',3i3)') stars%mx1,stars%mx2,stars%mx3
    WRITE (6,'('' kimax2,kimax='',2i7,'', (start from 0)'')') stars%kimax2,&
         &  stars%kimax

    WRITE (6,FMT=8040)
810
8040 FORMAT(/6x,'no.',5x,'kv3',9x,'sk3',7x,'nstr'/)
811 812 813 814 815 816

    ned1=9
    nint=30
    DO k = 1,ned1
       WRITE (6,FMT=8050) k,(stars%kv3(j,k),j=1,3),stars%sk3(k),stars%nstr(k)
    ENDDO
817
8050 FORMAT (1x,i7,3i4,f12.6,i6)
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

    DO k = ned1+1,stars%ng3,nint
       WRITE (6,FMT=8050) k,(stars%kv3(j,k),j=1,3),stars%sk3(k),stars%nstr(k)
       kdone = k
    ENDDO

    IF (kdone.LT.stars%ng3) THEN
       WRITE (6,FMT=8050) stars%ng3,(stars%kv3(j,stars%ng3),j=1,3),&
            &                     stars%sk3(stars%ng3),stars%nstr(stars%ng3)
    ENDIF


    DEALLOCATE (gsk3,index,index3,kv3rev)

  END SUBROUTINE strgn2
END MODULE m_strgn