od_vvacis.f90 22.6 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 9
MODULE m_od_vvacis
CONTAINS
  SUBROUTINE od_vvacis(&
10
       &     n2d_1,input,vacuum,nq2_1,&
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
       &     kv2_1,cell,MM,stars,&
       &     nstr2_1,&
       &     oneD,&
       &     rht,rhtxy,psq,vz,sym,&
       &     vxy,vpw)

    !     generates m^2+gz^2.neq.0 coefficients of the vacuum potential
    !     and fourier coefficients of the interstitial potential
    !     in the case of 1-dimensional calculations
    !     based on the using of the green's function for the 
    !     radial equation for the potential:
    !     [1/r(d/dr(r*d/dr))-m^2/r^2-gz^2]V(r) = 4pi*rho(r)
    !     when gz.neq.0 green's function is represented in
    !     the following way: 
    !          gf = 4pi*I_m(gz*r<)*K_m(gz*r>)
    !     where I and K are modified bessel funcions
    !     in the case of gz.eq.0 the green's function is:
    !            gf = 2pi*((r</r>)^m)/m
    !                 Y.Mokrousov, autumn 2002

    !     Fully symmetrized version, Mai 2003

    USE m_constants
    USE m_od_cylbes
    USE m_modcyli
    USE m_modcylk
    USE m_vacp5_0
    USE m_vacp5_z
    USE m_visp5_0
    USE m_visp5_z
    USE m_angle
    USE m_qsf
    USE m_fft2d


    USE m_types
    IMPLICIT NONE

49
    TYPE(t_input),INTENT(IN)   :: INPUT
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    TYPE(t_oneD),INTENT(IN)   :: oneD

    TYPE(t_vacuum),INTENT(IN)   :: vacuum

    TYPE(t_sym),INTENT(IN)   :: sym

    TYPE(t_stars),INTENT(IN)   :: stars

    TYPE(t_cell),INTENT(IN)   :: cell

    INTEGER, INTENT (IN) :: n2d_1  ,MM 
    INTEGER, INTENT (IN) :: nq2_1  
    INTEGER, INTENT (IN) :: nstr2_1(n2d_1)
    INTEGER, INTENT (IN) :: kv2_1(2,n2d_1) 
65
    COMPLEX, INTENT (INOUT) :: psq(stars%ng3)
66 67 68 69 70
    REAL,    INTENT (IN) :: vz(vacuum%nmzd,2) 
    REAL,    INTENT (IN) :: rht(vacuum%nmzd,2)
    COMPLEX, INTENT (IN) :: rhtxy(vacuum%nmzxyd,n2d_1-1,2)
    COMPLEX, INTENT (OUT):: vxy(vacuum%nmzxyd,n2d_1-1,2)
    COMPLEX, INTENT (OUT):: vpw(stars%ng3)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    !     local
    INTEGER :: m
    !------> reciprocal vectors staff

    INTEGER irec2,irec3,k1,k2,gzi,gzi1,k3,gzmin
    REAL    gz,gx,gy,g,g2 

    !------> different garbage

    REAL, PARAMETER :: tol_21 = 1.0e-21
    INTEGER  imz,imz1,i,ivac,iirec1,m1,j,irc1,im,irc
    INTEGER ix,iy,iz,s,n
    REAL    x,y,r,z,b,q,zf
    REAL    mult
    REAL    ani1,ani2,rhti
    INTEGER ivfft1,ivfft2,ivfft2d
    COMPLEX ic

    !------> different staff with special functions

92
    REAL  IR(1:stars%mx3,0:MM)
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
    REAL, ALLOCATABLE :: II(:),KK(:)
    REAL  fJ,fJ2,fJ1,iJ2,IIIR,fJ3
    REAL, ALLOCATABLE :: III(:),IIII(:,:,:)
    REAL, ALLOCATABLE :: fJJ(:,:),iJJ(:,:)

    !------> used for the optimization 

    INTEGER irec1(4),l,lmin ,l1

    !------> some factors used for the construction of the ch. density

    REAL, ALLOCATABLE :: fact(:)
    COMPLEX aa,a

    !------> values of the potential on the vacuum boundary

    COMPLEX val_help
    COMPLEX, ALLOCATABLE :: val(:),val_m(:,:)

    !------> Charge density

    COMPLEX, ALLOCATABLE :: rxy(:)

    !---------------> POTENTIALS

    !-> interstitial potential by its gz,m - components and total on the 
    !-> real grid 

    COMPLEX, ALLOCATABLE :: vis(:,:,:),vis_tot(:,:)

    !-> potential in the vacuum caused by the vacuum charge density      

    COMPLEX, ALLOCATABLE :: pvac(:)

    !-> potential in the vacuum caused by the interst. density

    COMPLEX, ALLOCATABLE :: pint(:)

    !-> radial components of the potential caused by the vacuum  
    !-> density on the real grid  

    COMPLEX, ALLOCATABLE :: vis_help(:,:),vpw_help(:)

    !-> real grid vis_z,vis_0 for the fft2d

    REAL,ALLOCATABLE :: af2(:),bf2(:)

    !-> radial grids

    INTEGER, ALLOCATABLE :: rmap(:,:)
    REAL   , ALLOCATABLE :: rr(:)
    INTEGER nrd

    !--> time

    REAL    gxy0,fxy0,phi
149 150
    COMPLEX gxy(stars%ng2-1)
    COMPLEX fxy(stars%ng2-1)
151 152 153 154 155

    INTRINSIC REAL,aimag

    !--------- preparations ---------->

156 157 158 159 160 161 162 163 164 165 166
    ALLOCATE ( KK(vacuum%nmzxyd),II(vacuum%nmzxyd),III(9*stars%mx1*stars%mx2),&
         &     IIII(9*stars%mx1*stars%mx3,1:stars%mx3,0:MM),&
         &     rmap(0:3*stars%mx1-1,0:3*stars%mx2-1),rr(1:9*stars%mx1*stars%mx2),&
         &     fact(vacuum%nmzxyd),val(n2d_1),fJJ(0:MM+1,stars%ng2),&
         &     val_m(-stars%mx3:stars%mx3,-MM:MM),rxy(vacuum%nmzxyd),iJJ(0:MM+1,1:stars%mx3),&
         &     vis(0:3*stars%mx1-1,0:3*stars%mx2-1,n2d_1),&
         &     vis_tot(0:3*stars%mx1-1,0:3*stars%mx2-1),pvac(vacuum%nmzxyd),&
         &     pint(vacuum%nmzxyd),vis_help(0:3*stars%mx1-1,0:3*stars%mx2-1),&
         &     af2(0:9*stars%mx1*stars%mx2-1),bf2(0:9*stars%mx1*stars%mx2-1),vpw_help(stars%ng3) )

    ivfft2d = 9*stars%mx1*stars%mx2
167
    ic = CMPLX(0.,1.)
168 169
    ivfft1 = 3*stars%mx1
    ivfft2 = 3*stars%mx2
170 171 172 173 174 175 176
    ani1 = 1./REAL(ivfft1)
    ani2 = 1./REAL(ivfft2)

    !--------- initializations -------->

    !----> vpw in the '1st aproximation' (V - tilde)

177
    vpw(1) = CMPLX(0.,0.)
178 179 180 181 182

    DO irec3 = 2,stars%ng3

       g = stars%sk3(irec3)

183
       vpw(irec3) = fpi_const*psq(irec3)/(g*g)
184 185 186 187 188

    ENDDO

    DO irc1 = 2,nq2_1
       DO i = 1,vacuum%nmzxy
189
          vxy(i,irc1-1,1) = CMPLX(0.,0.)
190 191 192 193 194 195 196
       END DO
    END DO

    !----> values of the potential in the 1st approximation on the boundary
    !----> if nstr2.ne.1 then it should be changed!!!

    DO m = 0,MM+1
197
       DO k3 = 1,stars%mx3
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
          CALL modcyli(m,cell%bmat(3,3)*k3*cell%z1,iJJ(m,k3))
       END DO
       DO irec2 = 1,stars%ng2
          g2 = stars%sk2(irec2)
          IF (irec2.NE.0) THEN
             CALL od_cylbes(m,g2*cell%z1,fJJ(m,irec2))
          END IF
       END DO
    END DO

    DO irc1 = 1,nq2_1
       val(irc1) = CMPLX(0.,0.)
       m = kv2_1(2,irc1)
       IF (m.LT.0) THEN
          mult = REAL((-1)**m)
       ELSE
          mult = 1.
       END IF
       k3 = kv2_1(1,irc1)
       DO irec2 = 1,stars%ng2
          phi = stars%phi2(irec2)
          irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),k3)
          IF (irec3.NE.0) THEN
             val(irc1) = val(irc1) +&
222
                  &              (ic**m)*vpw(irec3)*EXP(-ic*&
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
                  &              m*phi)*fJJ(iabs(m),irec2)*&
                  &              stars%nstr2(irec2)*mult
          END IF
       END DO
    END DO

    !-----> preparing arrays for radial grids:selecting from
    !-----> x & y only the radius

    nrd = 0

    DO ix = 0,ivfft1 - 1
       iy_loop: DO  iy = 0,ivfft2 - 1
          x = ix*ani1
          IF (x.GT.0.5) x = x - 1.
          y = iy*ani2
          IF (y.GT.0.5) y = y - 1.
          r = SQRT((x*cell%amat(1,1) + y*cell%amat(1,2))**2 +&
               &               (x*cell%amat(2,1) + y*cell%amat(2,2))**2)
          DO i = 1,nrd
             IF (ABS(r-rr(i)).LE.1.e-6) THEN
                rmap(ix,iy) = i
                CYCLE iy_loop
             END IF
          END DO
          nrd = nrd + 1
          rmap(ix,iy) = nrd
          rr(nrd) = r
       ENDDO iy_loop
    END DO

254
    DO gzi = -stars%mx3,stars%mx3
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
       DO m = -MM,MM
          IF (m.LT.0) THEN
             mult = REAL((-1)**m)
          ELSE
             mult = 1.
          END IF
          val_m(gzi,m) = CMPLX(0.,0.)
          irc1 = oneD%ig1(gzi,m)
          IF (irc1.NE.0) THEN
             val_m(gzi,m) = val(irc1)
          ELSE
             DO irec2 = 1,stars%ng2
                phi = stars%phi2(irec2)
                irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),gzi)
                IF (irec3.NE.0) THEN
                   val_m(gzi,m) = val_m(gzi,m) +&
271
                        &                    (ic**m)*vpw(irec3)*EXP(-ic*&
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                        &                    m*phi)*fJJ(iabs(m),irec2)*&
                        &                    stars%nstr2(irec2)*mult
                END IF
             END DO
          END IF
       END DO
    END DO

    !-------  During the following staff we miss the m=0,gz=0 -> irec1 = 1
    !-------  component of the potential which should be also added
    !-------  in order to get the total potential for the FFT's

    DO ix = 0,ivfft1 - 1
       DO iy = 0,ivfft2 - 1
          irc = rmap(ix,iy)
          r = rr(irc)
          IF (r.GT.cell%z1) THEN
             zf = (r-cell%z1)/vacuum%delz + 1.0
             im = zf
             q = zf - im
             vis(ix,iy,1) = 0.5*(q-1.)*&
293 294 295
                  &              (q-2.)*vz(im,1) -&
                  &              q*(q-2.)*vz(im+1,1) +&
                  &              0.5*q*(q-1.)*vz(im+2,1)
296 297
          ELSE
             vis(ix,iy,1) = &
298
                  &              vz(1,1) - val(1) + tpi_const*&
299 300 301 302 303 304 305 306 307
                  &              psq(1)*(cell%z1*cell%z1 - r*r)/2.
          END IF
          DO irc1 = 2,nq2_1
             vis(ix,iy,irc1) = CMPLX(0.,0.)
          END DO
       END DO
    END DO

    DO m = 0,MM
308
       DO k3 = 1,stars%mx3
309 310 311 312 313 314
          CALL modcyli(m,cell%bmat(3,3)*k3*cell%z1,IR(k3,m))
       END DO
    END DO

    DO i = 1,nrd
       DO m = 0,MM
315
          DO k3 = 1,stars%mx3
316 317 318 319 320 321 322
             CALL modcyli(m,cell%bmat(3,3)*k3*rr(i),IIII(i,k3,m))
          END DO
       END DO
    END DO

    !------- cycle by positive gz---------->

323
    DO gzi = 0,stars%mx3                        ! gz
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

       !------- cycle by positive m ---------->

       m_loop: DO m = 0,MM

          !-------------------------------------->

          IF (m.NE.0 .OR. gzi.NE.0) THEN ! m^2 + gz^2.ne.0

             irec1(1) = oneD%ig1(gzi,m)
             irec1(2) = oneD%ig1(gzi,-m)
             irec1(3) = oneD%ig1(-gzi,m)
             irec1(4) = oneD%ig1(-gzi,-m)

             DO l = 1,3
                DO l1 = l+1,4
                   IF (irec1(l).EQ.irec1(l1)) irec1(l1) = 0
                END DO
             END DO

             !---> if all the irec1 not equal to zero

             s = 0

             DO l = 1,4
                s = s + irec1(l)*irec1(l)
             END DO

             !---> preparing special functions which depend only on the 
             !---> absolute value of m and gz

             IF (s.NE.0 .AND. gzi.NE.0) THEN
                gz = cell%bmat(3,3)*gzi
                DO  imz = 1,vacuum%nmzxy
                   z = cell%z1 + vacuum%delz*(imz-1)  
                   CALL modcylk(m,gz*z,KK(imz))
                   CALL modcyli(m,gz*z,II(imz))
                END DO
                IIIR = II(1)
                DO irc = 1,nrd
                   III(irc) = IIII(irc,gzi,m)
                END DO
             ELSEIF (s.EQ.0) THEN
                CYCLE m_loop
             ELSEIF (s.NE.0 .AND. gzi.EQ.0) THEN  
                gz = 0.
             END IF

             !---> now we start the cycle by +-m,gz

             DO  l = 1,4

                IF (irec1(l).NE.0) THEN

                   !--------------------------------------->   

                   DO ix = 0,ivfft1-1
                      DO iy = 0,ivfft2-1
                         vis_help(ix,iy) = CMPLX(0.,0.)
                      END DO
                   END DO

                   DO i = 1,vacuum%nmzxy
                      fact(i) = 0.
                      rxy(i) = CMPLX(0.,0.)
                      pvac(i) = CMPLX(0.,0.)
                      pint(i) = CMPLX(0.,0.)
                   END DO

                   !-------------- gz = 0 ------------------------------------->
                   !----------------------------------------------------------->

                   IF (gzi.EQ.0) THEN

                      !----- this form of the density is just more easy to use

                      DO imz = 1,vacuum%nmzxy
401
                         rxy(imz) = rhtxy(imz,irec1(l)-1,1)
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 437
                      END DO

                      !----- vacuum potential caused by the vacuum density

                      CALL vacp5_0(&
                           &        vacuum%nmzxyd,vacuum%nmzxy,cell%z1,tpi_const,rxy,m,vacuum%delz,&
                           &        pvac,fact)

                      !----- vacuum potential caused by the interstitial density

                      aa = CMPLX(0.,0.)
                      m1 = kv2_1(2,irec1(l))
                      gzi1 = kv2_1(1,irec1(l))

                      IF (m1.LT.0) THEN
                         mult = REAL((-1)**m)
                      ELSE
                         mult = 1.
                      END IF

                      DO irec2 = 1,stars%ng2
                         irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),0)
                         IF (irec3.NE.0 .AND. irec2.NE.1) THEN
                            phi = stars%phi2(irec2)
                            g2 = stars%sk2(irec2)
                            aa = aa +    &
                                 &              (ic**m1)*psq(irec3)*&
                                 &              EXP(CMPLX(0.,-m1*phi))*&
                                 &              CMPLX(mult*fJJ(m+1,irec2)/g2,0.)*stars%nstr2(irec2)
                         END IF
                      END DO

                      !----- total vacuum potential

                      pint(:vacuum%nmzxy) =  fact(:vacuum%nmzxy)*aa 

438
                      vxy(:vacuum%nmzxy,irec1(l)-1,1) = pvac(:vacuum%nmzxy) + pint(:vacuum%nmzxy)
439 440 441 442 443 444 445 446

                      !----- array val further is a boundary values of the
                      !----- potential V- \tilde \tilde which is created to compensate 
                      !----- the influence of the outer 'noice' charge density - which 
                      !----- is just a periodical continuation of the interstitial charge
                      !----- density, V - \tilde and V - \tilde\tilde are then added in
                      !----- order to obtain the real interstitial potential           

447
                      val_help = vxy(1,irec1(l)-1,1) - val(irec1(l))
448 449 450 451 452 453 454 455 456 457 458

                      !----- potential \tilde\tilde{V} is a solution of the Laplase equation
                      !----- in the interstitial with the boundary conditions val_0 and val_z
                      !----- further, it is generated on the uniform grid in the unit cell
                      !----- \tilde{\Omega}, in the space between the cylindrical 
                      !----- interstitial boundary and the squre boundaries it is put to
                      !----- the vacuum potential

                      CALL visp5_0(&
                           &        vacuum%nmzxyd,vacuum%nmzxy,vacuum%delz,m,ivfft1,ivfft2,l,&
                           &        rxy,ani1,ani2,cell%z1,cell%amat,&
459
                           &        pvac,pint,tpi_const,input%jspins,val_help,&
460 461 462 463 464 465 466 467 468 469 470 471 472 473
                           &        vis_help)

                      DO ix = 0,ivfft1 - 1
                         DO iy = 0,ivfft2 - 1
                            vis(ix,iy,irec1(l)) = vis_help(ix,iy)
                         END DO
                      END DO


                      !------- gz.NEQ.0--------------------------------------------->
                   ELSE   
                      !------------------------------------------------------------->

                      DO  imz = 1,vacuum%nmzxy
474
                         rxy(imz) = rhtxy(imz,irec1(l)-1,1)
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
                      END DO

                      !----- vacuum potential caused by the vacuum density        

                      CALL vacp5_z(&
                           &        vacuum%nmzxyd,vacuum%nmzxyd,cell%z1,vacuum%delz,fpi_const,II,KK,rxy,m,&
                           &        pvac)

                      !----- vacuum potential caused by the intst. density

                      a = CMPLX(0.,0.)
                      m1 = kv2_1(2,irec1(l))
                      gzi1 = kv2_1(1,irec1(l))

                      IF (m1.LT.0) THEN
                         mult = REAL((-1)**m)
                      ELSE
                         mult = 1.
                      END IF

                      DO irec2 = 1,stars%ng2
                         irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),gzi1)
                         IF (irec3.NE.0) THEN
                            g = stars%sk3(irec3)
                            g2 = stars%sk2(irec2)
                            phi = stars%phi2(irec2)
                            b = cell%z1*( gz*iJJ(m+1,gzi)*fJJ(m,irec2) + &
                                 &              g2*II(1)*fJJ(m+1,irec2))/(g*g)
                            a = a +    &
                                 &              (ic**m1)*mult*EXP(-ic*m1*phi)&
                                 &              *psq(irec3)*b*stars%nstr2(irec2)
                         END IF
                      END DO

                      !----- total vacuum potential ---------------

                      DO imz = 1,vacuum%nmzxy
                         pint(imz) = fpi_const*a*KK(imz) 
513
                         vxy(imz,irec1(l)-1,1) =  pint(imz) + pvac(imz)  
514 515
                      END DO

516
                      val_help = vxy(1,irec1(l)-1,1) - val(irec1(l))
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

                      CALL visp5_z(&
                           &        vacuum%nmzxyd,vacuum%nmzxyd,vacuum%delz,m,ivfft1,ivfft2,IIIR,&
                           &        rxy,ani1,ani2,cell%z1,cell%amat,pvac,pint,tpi_const,l,&
                           &        fpi_const,val_help,III,m1,gz,rmap,rr,&
                           &        vis_help)

                      DO ix = 0,ivfft1 - 1
                         DO iy = 0,ivfft2 - 1
                            vis(ix,iy,irec1(l)) = vis_help(ix,iy)
                         END DO
                      END DO

                      !---  end of vacuum contibution to vis ---------------------->

                   END IF                    ! gz.atoms%neq.0

                   !---- > finishing the cycle by +-m,gz

                END IF

             ENDDO
          END IF                    ! m^2+gz^2.atoms%neq.0

       ENDDO m_loop                  ! m

    ENDDO                  ! gz 

    !*************************************************************
    !-------> finding the Fourier coefficients of the potential
    !-------  in the interstitial
    !-------  the scheme (making the potential continuous) is as follows:
    !-------  1. transforming the \tilde{V} from pw-representation
    !-------     to the real grid, putting to zero outside the
    !-------     cylindrical interstitial
    !-------  2. Adding the \tilde\tilde{V} on the real grid 
    !-------  3. Transforming back to the pw-representation 
    !-------  Everything is done in \tilda {\Omega}

556
    gzmin = -stars%mx3
557 558 559

    IF (sym%zrfs.OR.sym%invs) gzmin = 0

560
    DO k3 = gzmin,stars%mx3          ! collect the Fourier components
561 562 563 564 565 566 567 568 569 570 571 572 573

       fxy0 = 0.

       rhti = 0.

       DO irec2 = 1,stars%ng2 - 1
          fxy(irec2) = CMPLX(0.,0.)
       END DO

       DO irec2 = 1,stars%ng2
          irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),k3)
          IF (irec3.NE.0) THEN
             IF (irec2.EQ.1) THEN
574 575
                fxy0 = REAL(vpw(irec3))
                rhti = AIMAG(vpw(irec3))
576
             ELSE
577
                fxy(irec2-1) = vpw(irec3)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
             END IF
          END IF
       END DO

       af2=0.0
       bf2=0.0

       CALL fft2d(&
            &        stars,&
            &        af2(0),bf2,&
            &        fxy0,rhti,fxy,&
            &        1,+1)


       !--> although some harmonics of the vacuum potential are equal to zero
       !--> the same harmonics of the interstitial potential \Tilde{V} are not,
       !--> because this potential is determined from pw coefficients of the
       !--> charge density, which has the periodicity of the rectangular 
       !--> unit cell, hence, these harmonics should be extracted from the 
       !--> final interstitial potential

       i = 0

601 602
       DO iy = 0,3*stars%mx2-1
          DO ix = 0,3*stars%mx1-1
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
             x = ix*ani1
             IF (x.GT.0.5) x = x - 1.
             y = iy*ani2
             IF (y.GT.0.5) y = y - 1.
             r = SQRT((x*cell%amat(1,1) + y*cell%amat(1,2))**2 +&
                  &                  (x*cell%amat(2,1) + y*cell%amat(2,2))**2)               
             phi = angle(x*cell%amat(1,1) + y*cell%amat(1,2),&
                  &                     x*cell%amat(2,1) + y*cell%amat(2,2))
             vis_tot(ix,iy) = CMPLX(0.,0.)
             j = rmap(ix,iy)
             DO m = -MM,MM
                irc1 = oneD%ig1(k3,m)
                IF (irc1.NE.0) THEN
                   vis_tot(ix,iy) = vis_tot(ix,iy) +&
                        &                    vis(ix,iy,irc1)
                ELSE
                   IF (k3.EQ.0) THEN
                      IF (r.LE.cell%z1) THEN
                         vis_tot(ix,iy) = vis_tot(ix,iy) - &
                              &                          val_m(k3,m)*&
                              &                          EXP(CMPLX(0.,m*phi))*&
                              &                          (r**(iabs(m)))/(cell%z1**(iabs(m)))
                      END IF
                   ELSE
                      IF (r.LE.cell%z1) THEN
                         vis_tot(ix,iy) = vis_tot(ix,iy) -&
                              &                    val_m(k3,m)*IIII(j,iabs(k3),iabs(m))*&
                              &                    EXP(CMPLX(0.,m*phi))/IR(iabs(k3),iabs(m))
                      END IF
                   END IF
                END IF
             END DO

             IF (r.LE.cell%z1) THEN
                af2(i) = af2(i) + REAL(vis_tot(ix,iy))
                bf2(i) = bf2(i) + AIMAG(vis_tot(ix,iy))
                !$$$                 af2(i) = real(vis_tot(ix,iy))
                !$$$                 bf2(i) = aimag(vis_tot(ix,iy))
             ELSE
                af2(i) = REAL(vis_tot(ix,iy))
                bf2(i) = AIMAG(vis_tot(ix,iy))
             END IF
             i = i+1
          END DO
       END DO

       rhti = 0.

       CALL fft2d(&
            &        stars,&
            &        af2(0),bf2,&
            &        gxy0,rhti,gxy,&
            &        1,-1) 

       DO irec2 = 1,stars%ng2
          irec3 = stars%ig(stars%kv2(1,irec2),stars%kv2(2,irec2),k3)
          IF (irec3.NE.0) THEN
             IF (irec2.EQ.1) THEN
                vpw_help(irec3) = CMPLX(gxy0,rhti)
             ELSE
                vpw_help(irec3) = gxy(irec2-1)
             END IF
          END IF
       END DO

    END DO                    ! gz -> Vpw(.,.,gz)

    DO irec3 = 1,stars%ng3
671
       vpw(irec3) = vpw_help(irec3)
672 673 674 675 676 677
       !$$$         vpw(irec3,1) = vpw(irec3,1) + vpw_help(irec3)
    END DO


    DO irc1 = 2,nq2_1
       DO imz = 1,vacuum%nmzxy
678 679
          IF (ABS(vxy(imz,irc1-1,1)).LE.tol_21)&
               &          vxy(imz,irc1-1,1) = CMPLX(0.,0.)
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
       END DO
    END DO



    DEALLOCATE ( KK,II,III,IIII,fact,val,val_m,rxy,&
         &     vis,vis_tot,pvac,pint,vis_help,rmap,rr,&
         &     af2,bf2,vpw_help,fJJ,iJJ )

    RETURN
  END SUBROUTINE od_vvacis
END MODULE m_od_vvacis