Index: palm/trunk/SOURCE/pmc_interface_mod.f90
===================================================================
--- palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 2016)
+++ palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 2019)
@@ -21,5 +21,6 @@
 ! Current revisions:
 ! ------------------
-! 
+! Bugfixes mainly in determining the anterpolation index bounds. These errors 
+! were detected when first time tested using 3:1 grid-spacing.
 ! 
 ! Former revisions:
@@ -126,6 +127,6 @@
    USE arrays_3d,                                                               &
         ONLY:  dzu, dzw, e, e_p, e_1, e_2, pt, pt_p, pt_1, pt_2, q, q_p, q_1,   &
-               q_2, s, s_2, u, u_p, u_1, u_2, v, v_p, v_1, v_2, w, w_p, w_1, w_2,  &
-               zu, zw, z0
+               q_2, s, s_2, u, u_p, u_1, u_2, v, v_p, v_1, v_2, w, w_p, w_1,    &
+               w_2, zu, zw, z0
 #endif
 
@@ -1090,5 +1091,7 @@
 !--    This is needed in the anterpolation phase
        nhlr = CEILING( xexr / cg%dx )
-       xce  = coord_x(nxr) + xexr
+       xce  = coord_x(nxr+1) + xexr
+!--    One "extra" layer is taken behind the right boundary 
+!--    because it may be needed in cases of non-integer grid-spacing ratio
        DO  i = cg%nx, 0 , -1
           IF ( cg%coord_x(i) < xce )  THEN
@@ -1118,5 +1121,7 @@
 !--    This is needed in the anterpolation phase
        nhln = CEILING( yexn / cg%dy )
-       yce  = coord_y(nyn) + yexn
+       yce  = coord_y(nyn+1) + yexn
+!--    One "extra" layer is taken behind the north boundary 
+!--    because it may be needed in cases of non-integer grid-spacing ratio
        DO  j = cg%ny, 0, -1
           IF ( cg%coord_y(j) < yce )  THEN
@@ -1358,5 +1363,6 @@
 
 !
-!--    First horizontal walls
+!--    First horizontal walls. Note that also logc_w_? and logc_ratio_w_? need to 
+!--    be allocated and initialized here.
 !--    Left boundary
        IF ( nest_bound_l )  THEN
@@ -1364,10 +1370,14 @@
           ALLOCATE( logc_u_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
           ALLOCATE( logc_v_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
+          ALLOCATE( logc_w_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
           ALLOCATE( logc_ratio_u_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,nys:nyn) )
           ALLOCATE( logc_ratio_v_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,nys:nyn) )
+          ALLOCATE( logc_ratio_w_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,nys:nyn) )
           logc_u_l       = 0
           logc_v_l       = 0
+          logc_w_l       = 0
           logc_ratio_u_l = 1.0_wp
           logc_ratio_v_l = 1.0_wp
+          logc_ratio_w_l = 1.0_wp
           direction      = 1
           inc            = 1
@@ -1404,15 +1414,20 @@
 !--    Right boundary
        IF ( nest_bound_r )  THEN
-
+           
           ALLOCATE( logc_u_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
           ALLOCATE( logc_v_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
+          ALLOCATE( logc_w_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
           ALLOCATE( logc_ratio_u_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,nys:nyn) )
           ALLOCATE( logc_ratio_v_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,nys:nyn) )
+          ALLOCATE( logc_ratio_w_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,nys:nyn) )
           logc_u_r       = 0
           logc_v_r       = 0
+          logc_w_r       = 0
           logc_ratio_u_r = 1.0_wp
           logc_ratio_v_r = 1.0_wp
+          logc_ratio_w_r = 1.0_wp
           direction      = 1
           inc            = 1
+
           DO  j = nys, nyn
 !
@@ -1449,10 +1464,14 @@
           ALLOCATE( logc_u_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
           ALLOCATE( logc_v_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
+          ALLOCATE( logc_w_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
           ALLOCATE( logc_ratio_u_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,nxl:nxr) )
           ALLOCATE( logc_ratio_v_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,nxl:nxr) )
+          ALLOCATE( logc_ratio_w_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,nxl:nxr) )
           logc_u_s       = 0
           logc_v_s       = 0
+          logc_w_s       = 0
           logc_ratio_u_s = 1.0_wp
           logc_ratio_v_s = 1.0_wp
+          logc_ratio_w_s = 1.0_wp
           direction      = 1
           inc            = 1
@@ -1492,10 +1511,14 @@
           ALLOCATE( logc_u_n(1:2,nzb:nzt_topo_nestbc_n,nxl:nxr) )
           ALLOCATE( logc_v_n(1:2,nzb:nzt_topo_nestbc_n,nxl:nxr) )
+          ALLOCATE( logc_w_n(1:2,nzb:nzt_topo_nestbc_n,nxl:nxr) )
           ALLOCATE( logc_ratio_u_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,nxl:nxr) )
           ALLOCATE( logc_ratio_v_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,nxl:nxr) )
+          ALLOCATE( logc_ratio_w_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,nxl:nxr) )
           logc_u_n       = 0
           logc_v_n       = 0
+          logc_w_n       = 0
           logc_ratio_u_n = 1.0_wp
           logc_ratio_v_n = 1.0_wp
+          logc_ratio_w_n = 1.0_wp
           direction      = 1
           inc            = 1
@@ -1538,11 +1561,6 @@
           IF ( nest_bound_l )  THEN
 
-             ALLOCATE( logc_w_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
-             ALLOCATE( logc_ratio_w_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,      &
-                                      nys:nyn) )
-
-             logc_w_l       = 0
-             logc_ratio_w_l = 1.0_wp
-             direction      = 2
+             direction  = 2
+
              DO  j = nys, nyn
                 DO  k = nzb, nzt_topo_nestbc_l
@@ -1625,10 +1643,5 @@
           IF ( nest_bound_r )  THEN
 
-             ALLOCATE( logc_w_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
-             ALLOCATE( logc_ratio_w_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,      &
-                                      nys:nyn) )
-             logc_w_r       = 0
-             logc_ratio_w_r = 1.0_wp
-             direction      = 2
+             direction  = 2
              i  = nxr + 1
 
@@ -1709,10 +1722,5 @@
           IF ( nest_bound_s )  THEN
 
-             ALLOCATE( logc_w_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
-             ALLOCATE( logc_ratio_w_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,      &
-                                      nxl:nxr) )
-             logc_w_s       = 0
-             logc_ratio_w_s = 1.0_wp
-             direction      = 3
+             direction  = 3
 
              DO  i = nxl, nxr
@@ -1795,10 +1803,5 @@
           IF ( nest_bound_n )  THEN
 
-             ALLOCATE( logc_w_n(1:2,nzb:nzt_topo_nestbc_n, nxl:nxr) )
-             ALLOCATE( logc_ratio_w_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,      &
-                                      nxl:nxr) )
-             logc_w_n       = 0
-             logc_ratio_w_n = 1.0_wp
-             direction      = 3
+             direction  = 3
              j  = nyn + 1
 
@@ -2152,5 +2155,5 @@
 !--    index k
        kk = 0
-       DO  WHILE ( cg%zu(kk) < zu(nzt) )
+       DO  WHILE ( cg%zu(kk) <= zu(nzt) )
           kk = kk + 1
        ENDDO
@@ -2158,5 +2161,5 @@
 
        kk = 0
-       DO  WHILE ( cg%zw(kk) < zw(nzt-1) )
+       DO  WHILE ( cg%zw(kk) <= zw(nzt-1) )
           kk = kk + 1
        ENDDO
@@ -2182,6 +2185,7 @@
 !
 !--    i-indices of u for each ii-index value
+!--    ii=icr is redundant for anterpolation
        istart = nxlg
-       DO  ii = icl, icr
+       DO  ii = icl, icr-1
           i = istart
           DO  WHILE ( ( coord_x(i) < cg%coord_x(ii) - 0.5_wp * cg%dx )  .AND.   &
@@ -2190,16 +2194,19 @@
           ENDDO
           iflu(ii) = MIN( MAX( i, nxlg ), nxrg )
-          DO  WHILE ( ( coord_x(i) < cg%coord_x(ii) + 0.5_wp * cg%dx )  .AND.   &
-                      ( i < nxrg ) )
+          DO  WHILE ( ( coord_x(i) <= cg%coord_x(ii) + 0.5_wp * cg%dx )  .AND.  &
+                      ( i < nxrg+1 ) )
              i = i + 1
           ENDDO
-          ifuu(ii) = MIN( MAX( i, nxlg ), nxrg )
+          ifuu(ii) = MIN( MAX( i-1, iflu(ii) ), nxrg )
           istart = iflu(ii)
        ENDDO
+       iflu(icr) = nxrg
+       ifuu(icr) = nxrg
 
 !
 !--    i-indices of others for each ii-index value
+!--    ii=icr is redundant for anterpolation
        istart = nxlg
-       DO  ii = icl, icr
+       DO  ii = icl, icr-1
           i = istart
           DO  WHILE ( ( coord_x(i) + 0.5_wp * dx < cg%coord_x(ii) )  .AND.      &
@@ -2208,16 +2215,19 @@
           ENDDO
           iflo(ii) = MIN( MAX( i, nxlg ), nxrg )
-          DO  WHILE ( ( coord_x(i) + 0.5_wp * dx < cg%coord_x(ii) + cg%dx )     &
-                      .AND.  ( i < nxrg ) )
+          DO  WHILE ( ( coord_x(i) + 0.5_wp * dx <= cg%coord_x(ii) + cg%dx )    &
+                      .AND.  ( i < nxrg+1 ) )
              i = i + 1
           ENDDO
-          ifuo(ii) = MIN(MAX(i,nxlg),nxrg)
+          ifuo(ii) = MIN( MAX( i-1, iflo(ii) ), nxrg )
           istart = iflo(ii)
        ENDDO
+       iflo(icr) = nxrg
+       ifuo(icr) = nxrg
 
 !
 !--    j-indices of v for each jj-index value
+!--    jj=jcn is redundant for anterpolation
        jstart = nysg
-       DO  jj = jcs, jcn
+       DO  jj = jcs, jcn-1
           j = jstart
           DO  WHILE ( ( coord_y(j) < cg%coord_y(jj) - 0.5_wp * cg%dy )  .AND.   &
@@ -2226,16 +2236,18 @@
           ENDDO
           jflv(jj) = MIN( MAX( j, nysg ), nyng )
-          DO  WHILE ( ( coord_y(j) < cg%coord_y(jj) + 0.5_wp * cg%dy )  .AND.   &
-                      ( j < nyng ) )
+          DO  WHILE ( ( coord_y(j) <= cg%coord_y(jj) + 0.5_wp * cg%dy )  .AND.  &
+                      ( j < nyng+1 ) )
              j = j + 1
           ENDDO
-          jfuv(jj) = MIN( MAX( j, nysg ), nyng )
+          jfuv(jj) = MIN( MAX( j-1, jflv(jj) ), nyng )
           jstart = jflv(jj)
        ENDDO
-
+       jflv(jcn) = nyng
+       jfuv(jcn) = nyng
 !
 !--    j-indices of others for each jj-index value
+!--    jj=jcn is redundant for anterpolation
        jstart = nysg
-       DO  jj = jcs, jcn
+       DO  jj = jcs, jcn-1
           j = jstart
           DO  WHILE ( ( coord_y(j) + 0.5_wp * dy < cg%coord_y(jj) )  .AND.      &
@@ -2244,11 +2256,13 @@
           ENDDO
           jflo(jj) = MIN( MAX( j, nysg ), nyng )
-          DO  WHILE ( ( coord_y(j) + 0.5_wp * dy < cg%coord_y(jj) + cg%dy )     &
-                      .AND.  ( j < nyng ) )
+          DO  WHILE ( ( coord_y(j) + 0.5_wp * dy <= cg%coord_y(jj) + cg%dy )    &
+                      .AND.  ( j < nyng+1 ) )
              j = j + 1
           ENDDO
-          jfuo(jj) = MIN( MAX( j, nysg ), nyng )
-          jstart = jflv(jj)
+          jfuo(jj) = MIN( MAX( j-1, jflo(jj) ), nyng )
+          jstart = jflo(jj)
        ENDDO
+       jflo(jcn) = nyng
+       jfuo(jcn) = nyng
 
 !
@@ -2259,14 +2273,12 @@
        DO  kk = 1, kctw
           k = kstart
-          DO  WHILE ( ( zw(k) < cg%zw(kk) - 0.5_wp * cg%dzw(kk) )  .AND.        &
-                      ( k < nzt ) )
+          DO  WHILE ( ( zw(k) < cg%zu(kk) )  .AND.  ( k < nzt ) )
              k = k + 1
           ENDDO
           kflw(kk) = MIN( MAX( k, 1 ), nzt + 1 )
-          DO  WHILE ( ( zw(k) < cg%zw(kk) + 0.5_wp * cg%dzw(kk+1) )  .AND.      &
-                      ( k < nzt ) )
+          DO  WHILE ( ( zw(k) <= cg%zu(kk+1) )  .AND.  ( k < nzt+1 ) )
              k = k + 1
           ENDDO
-          kfuw(kk) = MIN( MAX( k, 1 ), nzt + 1 )
+          kfuw(kk) = MIN( MAX( k-1, kflw(kk) ), nzt + 1 )
           kstart = kflw(kk)
        ENDDO
@@ -2279,14 +2291,12 @@
        DO  kk = 1, kctu
           k = kstart
-          DO  WHILE ( ( zu(k) < cg%zu(kk) - 0.5_wp * cg%dzu(kk) )  .AND.        &
-                      ( k < nzt ) )
+          DO  WHILE ( ( zu(k) < cg%zw(kk-1) )  .AND.  ( k < nzt ) )
              k = k + 1
           ENDDO
           kflo(kk) = MIN( MAX( k, 1 ), nzt + 1 )
-          DO  WHILE ( ( zu(k) < cg%zu(kk) + 0.5_wp * cg%dzu(kk+1) )  .AND.      &
-                      ( k < nzt ) )
+          DO  WHILE ( ( zu(k) <= cg%zw(kk) )  .AND.  ( k < nzt+1 ) )
              k = k + 1
           ENDDO
-          kfuo(kk) = MIN( MAX( k-1, 1 ), nzt + 1 )
+          kfuo(kk) = MIN( MAX( k-1, kflo(kk) ), nzt + 1 )
           kstart = kflo(kk)
        ENDDO
