Index: palm/trunk/SOURCE/pmc_handle_communicator_mod.f90
===================================================================
--- palm/trunk/SOURCE/pmc_handle_communicator_mod.f90	(revision 3818)
+++ palm/trunk/SOURCE/pmc_handle_communicator_mod.f90	(revision 3819)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Adjustable anterpolation buffer introduced on all nest boundaries, it is controlled
+! by the new nesting_parameters parameter anterpolation_buffer_width.
+! 
+! 3655 2019-01-07 16:51:22Z knoop
 ! nestpar renamed to nesting_parameters
 ! 
@@ -185,5 +189,5 @@
 
  SUBROUTINE pmc_init_model( comm, nesting_datatransfer_mode, nesting_mode,      &
-                            pmc_status )
+                            anterpolation_buffer_width, pmc_status )
 
     USE control_parameters,                                                     &
@@ -198,4 +202,5 @@
     CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode  !<
 
+    INTEGER, INTENT(INOUT) ::  anterpolation_buffer_width          !< Boundary buffer width for anterpolation
     INTEGER, INTENT(INOUT) ::  comm        !<
     INTEGER, INTENT(INOUT) ::  pmc_status  !<
@@ -226,5 +231,5 @@
 
        CALL read_coupling_layout( nesting_datatransfer_mode, nesting_mode,      &
-                                  pmc_status )
+                                  anterpolation_buffer_width, pmc_status )
 
        IF ( pmc_status /= pmc_no_namelist_found  .AND.                          &
@@ -473,5 +478,5 @@
 
  SUBROUTINE read_coupling_layout( nesting_datatransfer_mode, nesting_mode,      &
-      pmc_status )
+      anterpolation_buffer_width, pmc_status )
 
     IMPLICIT NONE
@@ -479,5 +484,6 @@
     CHARACTER(LEN=8), INTENT(INOUT) ::  nesting_mode
     CHARACTER(LEN=7), INTENT(INOUT) ::  nesting_datatransfer_mode
-
+    
+    INTEGER, INTENT(INOUT)      ::  anterpolation_buffer_width     !< Boundary buffer width for anterpolation
     INTEGER(iwp), INTENT(INOUT) ::  pmc_status
     INTEGER(iwp)                ::  bad_llcorner
@@ -488,5 +494,5 @@
 
     NAMELIST /nesting_parameters/  domain_layouts, nesting_datatransfer_mode,  &
-                                   nesting_mode
+                                   nesting_mode, anterpolation_buffer_width
     
 !
Index: palm/trunk/SOURCE/pmc_interface_mod.f90
===================================================================
--- palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 3818)
+++ palm/trunk/SOURCE/pmc_interface_mod.f90	(revision 3819)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Adjustable anterpolation buffer introduced on all nest boundaries, it is controlled
+! by the new nesting_parameters parameter anterpolation_buffer_width.
+! 
+! 3804 2019-03-19 13:46:20Z hellstea
 ! Anterpolation domain is lowered from kct-1 to kct-3 to avoid exessive       
 ! kinetic energy from building up in CBL flows.
@@ -463,6 +467,6 @@
     INTEGER(iwp), PARAMETER ::  child_to_parent = 2   !<
     INTEGER(iwp), PARAMETER ::  parent_to_child = 1   !<
-    INTEGER(iwp), PARAMETER ::  interpolation_scheme_lrsn = 2  !< Interpolation scheme to be used on lateral boundaries (to be made user parameter)
-    INTEGER(iwp), PARAMETER ::  interpolation_scheme_t    = 3  !< Interpolation scheme to be used on top boundary (to be made user parameter)
+    INTEGER(iwp), PARAMETER ::  interpolation_scheme_lrsn  = 2  !< Interpolation scheme to be used on lateral boundaries (maybe to be made user parameter)
+    INTEGER(iwp), PARAMETER ::  interpolation_scheme_t     = 3  !< Interpolation scheme to be used on top boundary (maybe to be made user parameter)
 !
 !-- Coupler setup
@@ -476,5 +480,6 @@
     CHARACTER(LEN=7), SAVE ::  nesting_datatransfer_mode = 'mixed'  !< steering parameter for data-transfer mode
     CHARACTER(LEN=8), SAVE ::  nesting_mode = 'two-way'             !< steering parameter for 1- or 2-way nesting
-
+    INTEGER(iwp),     SAVE ::  anterpolation_buffer_width = 2       !< Boundary buffer width for anterpolation
+    
     LOGICAL, SAVE ::  nested_run = .FALSE.  !< general switch
     LOGICAL       ::  rans_mode_parent = .FALSE. !< mode of parent model (.F. - LES mode, .T. - RANS mode)
@@ -511,5 +516,15 @@
     INTEGER(iwp), SAVE ::  igsr     !< Integer grid-spacing ratio in i-direction 
     INTEGER(iwp), SAVE ::  jgsr     !< Integer grid-spacing ratio in j-direction 
-    INTEGER(iwp), SAVE ::  kgsr     !< Integer grid-spacing ratio in k-direction 
+    INTEGER(iwp), SAVE ::  kgsr     !< Integer grid-spacing ratio in k-direction
+!
+!-- Global parent-grid index bounds
+    INTEGER(iwp), SAVE ::  iplg     !< Leftmost parent-grid array ip index of the whole child domain
+    INTEGER(iwp), SAVE ::  iprg     !< Rightmost parent-grid array ip index of the whole child domain
+    INTEGER(iwp), SAVE ::  jpsg     !< Southmost parent-grid array jp index of the whole child domain
+    INTEGER(iwp), SAVE ::  jpng     !< Northmost parent-grid array jp index of the whole child domain
+!
+!-- Local parent-grid index bounds (to be moved here from pmci_setup_child)
+!-- EXPLAIN WHY SEVERAL SETS OF PARENT-GRID INDEX BOUNDS ARE NEEDED.
+    
 !
 !-- Highest prognostic parent-grid k-indices.
@@ -551,5 +566,5 @@
     REAL(wp), DIMENSION(2)              ::  zmax_coarse             !<
 
-    TYPE coarsegrid_def
+    TYPE parentgrid_def
        INTEGER(iwp)                        ::  nx                 !<
        INTEGER(iwp)                        ::  ny                 !<
@@ -568,7 +583,7 @@
        REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu                 !<
        REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw                 !<
-    END TYPE coarsegrid_def
-
-    TYPE(coarsegrid_def), SAVE, PUBLIC     ::  cg   !<
+    END TYPE parentgrid_def
+
+    TYPE(parentgrid_def), SAVE, PUBLIC     ::  cg                 !< change to pg
 !
 !-- Variables for particle coupling
@@ -677,5 +692,5 @@
 
     CALL pmc_init_model( world_comm, nesting_datatransfer_mode, nesting_mode,  &
-                         pmc_status )
+                         anterpolation_buffer_width, pmc_status )
 
     IF ( pmc_status == pmc_no_namelist_found )  THEN
@@ -1072,5 +1087,5 @@
 !--       Compute size of index_list.
           ic = 0         
-          DO  k = 1, size_of_array(2)
+          DO  k = 1, size_of_array(2)           ! Replace k by some other letter
              ic = ic + ( coarse_bound_all(4,k) - coarse_bound_all(3,k) + 1 ) * &
                        ( coarse_bound_all(2,k) - coarse_bound_all(1,k) + 1 )
@@ -1089,5 +1104,5 @@
 !
 !--       Loop over all children PEs
-          DO  k = 1, size_of_array(2)
+          DO  k = 1, size_of_array(2)           ! Replace k by some other letter
 !
 !--          Area along y required by actual child PE
@@ -1386,6 +1401,7 @@
        IMPLICIT NONE
 
-       INTEGER(iwp), DIMENSION(5,numprocs) ::  coarse_bound_all   !<
-       INTEGER(iwp), DIMENSION(2)          ::  size_of_array      !<
+       INTEGER(iwp), DIMENSION(5,numprocs) ::  coarse_bound_all     !< Transfer array for parent-grid index bounds
+       INTEGER(iwp), DIMENSION(4)          ::  parent_bound_global  !< Transfer array for global parent-grid index bounds
+       INTEGER(iwp), DIMENSION(2)          ::  size_of_array        !<
        INTEGER(iwp) :: i        !< 
        INTEGER(iwp) :: iauxl    !<
@@ -1496,5 +1512,5 @@
 #endif       
 
-       WRITE(9,"('Pmci_map_fine_to_coarse_grid. parent-grid array bounds: ',4(i3,2x))") icl, icr, jcs, jcn
+       WRITE(9,"('Pmci_map_fine_to_coarse_grid. Parent-grid array bounds: ',4(i4,2x))") icl, icr, jcs, jcn
        FLUSH(9)
 
@@ -1512,5 +1528,5 @@
 !
 !--    Note that MPI_Gather receives data from all processes in the rank order
-!--    TO_DO: refer to the line where this fact becomes important
+!--    This fact is exploited in creating the index list in pmci_create_index_list
        CALL MPI_GATHER( coarse_bound, 5, MPI_INTEGER, coarse_bound_all, 5,     &
                         MPI_INTEGER, 0, comm2d, ierr )
@@ -1521,7 +1537,22 @@
           CALL pmc_send_to_parent( size_of_array, 2, 0, 40, ierr )
           CALL pmc_send_to_parent( coarse_bound_all, SIZE( coarse_bound_all ), &
-                                   0, 41, ierr )
-       ENDIF
-
+               0, 41, ierr )
+!
+!--       Determine the global parent-grid index bounds       
+          parent_bound_global(1) = MINVAL( coarse_bound_all(1,:) )
+          parent_bound_global(2) = MAXVAL( coarse_bound_all(2,:) )
+          parent_bound_global(3) = MINVAL( coarse_bound_all(3,:) )
+          parent_bound_global(4) = MAXVAL( coarse_bound_all(4,:) )
+       ENDIF
+!
+!--    Broadcat the global parent-grid index bounds to all current child processes
+       CALL MPI_BCAST( parent_bound_global, 4, MPI_INTEGER, 0, comm2d, ierr )
+       iplg = parent_bound_global(1)
+       iprg = parent_bound_global(2)
+       jpsg = parent_bound_global(3)
+       jpng = parent_bound_global(4)
+       WRITE(9,"('Pmci_map_fine_to_coarse_grid. Global parent-grid index bounds iplg, iprg, jpsg, jpng: ',4(i4,2x))") iplg, iprg, jpsg, jpng
+       FLUSH(9)
+       
     END SUBROUTINE pmci_map_fine_to_coarse_grid
 
@@ -1623,5 +1654,5 @@
 !
 !--       Print out the index bounds for checking and debugging purposes
-          WRITE(9,"('pmci_init_anterp_tophat, ii, iflu, ifuu: ', 3(i4,2x))")    &
+          WRITE(9,"('pmci_define_index_mapping, ii, iflu, ifuu: ', 3(i4,2x))")    &
                ii, iflu(ii), ifuu(ii)
           FLUSH(9)
@@ -1648,5 +1679,5 @@
 !
 !--       Print out the index bounds for checking and debugging purposes
-          WRITE(9,"('pmci_init_anterp_tophat, ii, iflo, ifuo: ', 3(i4,2x))")    &
+          WRITE(9,"('pmci_define_index_mapping, ii, iflo, ifuo: ', 3(i4,2x))")    &
                ii, iflo(ii), ifuo(ii)
           FLUSH(9)
@@ -1669,5 +1700,5 @@
 !
 !--       Print out the index bounds for checking and debugging purposes
-          WRITE(9,"('pmci_init_anterp_tophat, jj, jflv, jfuv: ', 3(i4,2x))")    &
+          WRITE(9,"('pmci_define_index_mapping, jj, jflv, jfuv: ', 3(i4,2x))")    &
                jj, jflv(jj), jfuv(jj)
           FLUSH(9)
@@ -1694,5 +1725,5 @@
 !
 !--       Print out the index bounds for checking and debugging purposes
-          WRITE(9,"('pmci_init_anterp_tophat, jj, jflo, jfuo: ', 3(i4,2x))")    &
+          WRITE(9,"('pmci_define_index_mapping, jj, jflo, jfuo: ', 3(i4,2x))")    &
                jj, jflo(jj), jfuo(jj)
           FLUSH(9)
@@ -1719,5 +1750,5 @@
 !
 !--       Print out the index bounds for checking and debugging purposes
-          WRITE(9,"('pmci_init_anterp_tophat, kk, kflw, kfuw: ', 4(i4,2x), 2(e12.5,2x))") &
+          WRITE(9,"('pmci_define_index_mapping, kk, kflw, kfuw: ', 4(i4,2x), 2(e12.5,2x))") &
                kk, kflw(kk), kfuw(kk), nzt,  cg%zu(kk), cg%zw(kk)
           FLUSH(9)
@@ -1755,5 +1786,5 @@
 !--    Print out the index bounds for checking and debugging purposes
        DO  kk = 1, cg%nz+1
-          WRITE(9,"('pmci_init_anterp_tophat, kk, kflo, kfuo: ', 4(i4,2x), 2(e12.5,2x))") &
+          WRITE(9,"('pmci_define_index_mapping, kk, kflo, kfuo: ', 4(i4,2x), 2(e12.5,2x))") &
                kk, kflo(kk), kfuo(kk), nzt,  cg%zu(kk), cg%zw(kk)
           FLUSH(9)
@@ -2242,5 +2273,5 @@
 
 
-SUBROUTINE  get_child_gridspacing (m, dx,dy,dz)
+SUBROUTINE  get_child_gridspacing( m, dx,dy,dz )
 
    IMPLICIT NONE
@@ -2868,4 +2899,6 @@
 !-- an argument to allow for example to force one-way initialization 
 !-- phase.
+!-- Note that interpolation ( parent_to_child ) must always be carried 
+!-- out before anterpolation ( child_to_parent ).
 
     IMPLICIT NONE
@@ -4161,11 +4194,10 @@
 !--   Define the index bounds iclant, icrant, jcsant and jcnant.
 !--   Note that kcb is simply zero and kct enters here as a parameter and it is
-!--   determined in pmci_init_anterp_tophat.
-!--   Please note, grid points used also for interpolation (from parent to
+!--   determined in pmci_define_index_mapping.
+!--   Note that the grid points used also for interpolation (from parent to
 !--   child) are excluded in anterpolation, e.g. anterpolation is only from 
-!--   nzb:kct-1, as kct is used for interpolation.
-!--
-!--   Note that kctant is changed from kct-1 to kct-3 to avoid exessive       
-!--   kinetic energy from building up in CBL flows.
+!--   nzb:kct-1, as kct is used for interpolation. An additional buffer is
+!--   also applied (default value for anterpolation_buffer_width = 2) in order
+!--   to avoid unphysical accumulation of kinetic energy.
       iclant = icl
       icrant = icr
@@ -4173,21 +4205,32 @@
       jcnant = jcn
 !      kctant = kct - 1
-      kctant = kct - 3
-      
+      kctant = kct - 1 - anterpolation_buffer_width
       kcb  = 0
       IF ( nesting_mode /= 'vertical' )  THEN
-         IF ( bc_dirichlet_l )  THEN
-            iclant = icl + 3
-         ENDIF
-         IF ( bc_dirichlet_r )  THEN
-            icrant = icr - 3
-         ENDIF
-
-         IF ( bc_dirichlet_s )  THEN
-            jcsant = jcs + 3
-         ENDIF
-         IF ( bc_dirichlet_n )  THEN
-            jcnant = jcn - 3
-         ENDIF
+!         IF ( bc_dirichlet_l )  THEN
+!!            iclant = icl + 3
+!            iclant = icl + 3 + anterpolation_buffer_width
+!         ENDIF
+!         IF ( bc_dirichlet_r )  THEN
+!!            icrant = icr - 3
+!            icrant = icr - 3 - anterpolation_buffer_width
+!         ENDIF
+!
+!         IF ( bc_dirichlet_s )  THEN
+!!            jcsant = jcs + 3
+!            jcsant = jcs + 3 + anterpolation_buffer_width
+!         ENDIF
+!         IF ( bc_dirichlet_n )  THEN
+!!            jcnant = jcn - 3
+!            jcnant = jcn - 3 - anterpolation_buffer_width
+!         ENDIF
+
+!
+!--      New method: 
+         iclant = MAX( icl, iplg + 3 + anterpolation_buffer_width )
+         icrant = MIN( icr, iprg - 3 - anterpolation_buffer_width )
+         jcsant = MAX( jcs, jpsg + 3 + anterpolation_buffer_width )
+         jcnant = MIN( jcn, jpng - 3 - anterpolation_buffer_width )
+         
       ENDIF
 !
@@ -4203,6 +4246,6 @@
       ENDIF
 !
-!--   Note that ii, jj, and kk are coarse-grid indices and i,j, and k 
-!--   are fine-grid indices.
+!--   Note that l, m, and n are parent-grid indices and i,j, and k 
+!--   are child-grid indices.
       DO  l = iclant, icrant
          DO  m = jcsant, jcnant
