Index: palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- palm/trunk/SOURCE/check_parameters.f90	(revision 2548)
+++ palm/trunk/SOURCE/check_parameters.f90	(revision 2550)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Added checks for complex terrain simulations
+! 
+! 2513 2017-10-04 09:24:39Z kanani
 ! Bugfix for some dopr(_initial)_index values and units connected to 
 ! passive-scalar output
@@ -3901,6 +3904,24 @@
     ENDIF
 
+!
 !-- Vertical nesting: check fine and coarse grid compatibility for data exchange
     IF ( vnested )  CALL vnest_check_parameters
+
+!
+!-- Check if topography is read from file in case of complex terrain simulations
+    IF ( complex_terrain  .AND.  TRIM( topography ) /= 'read_from_file' )  THEN
+       message_string = 'complex_terrain requires topography' //               &
+                        ' = ''read_from_file'''
+       CALL message( 'check_parameters', 'PA0472', 1, 2, 0, 6, 0 )
+    ENDIF
+
+!
+!-- Check if vertical grid stretching is switched off in case of complex 
+!-- terrain simulations
+    IF ( complex_terrain  .AND.  dz_stretch_level < 100000.0_wp )  THEN
+       message_string = 'Vertical grid stretching is not allowed for ' //      &
+                        'complex_terrain = .T.'
+       CALL message( 'check_parameters', 'PA0473', 1, 2, 0, 6, 0 )
+    ENDIF
 
     CALL location_message( 'finished', .TRUE. )
Index: palm/trunk/SOURCE/header.f90
===================================================================
--- palm/trunk/SOURCE/header.f90	(revision 2548)
+++ palm/trunk/SOURCE/header.f90	(revision 2550)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Added output for complex terrain simulations
+! 
+! 2544 2017-10-13 18:09:32Z maronga
 ! Moved initial day of year and time to inipar. 
 ! 
@@ -403,5 +406,5 @@
 
     USE surface_mod,                                                           &
-        ONLY:  surf_def_h
+        ONLY:  surf_def_h, get_topography_top_index
 
     USE synthetic_turbulence_generator_mod,                                    &
@@ -972,4 +975,15 @@
        ELSEIF ( TRIM( topography_grid_convention ) == 'cell_center' )  THEN
           WRITE ( io, 279 )
+       ENDIF
+    ENDIF
+
+!-- Complex terrain
+    IF ( complex_terrain )  THEN
+       WRITE( io, 280 ) 
+       IF ( turbulent_inflow )  THEN
+          WRITE( io, 281 )  zu( get_topography_top_index( 0, 0, 's' ) )
+       ENDIF
+       IF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
+          WRITE( io, 282 )
        ENDIF
     ENDIF
@@ -2051,4 +2065,9 @@
 279 FORMAT (' Topography grid definition convention:'/ &
             ' cell center (scalar grid points)' /)
+280 FORMAT (' Complex terrain simulation is activated.')
+281 FORMAT ('    --> Mean inflow profiles are adjusted.' / &
+            '    --> Elevation of inflow boundary: ', F7.1, ' m' )
+282 FORMAT ('    --> Initial data from 3D-precursor run is shifted' / &
+            '        vertically depending on local surface height.')
 300 FORMAT (//' Boundary conditions:'/ &
              ' -------------------'// &
Index: palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- palm/trunk/SOURCE/init_3d_model.f90	(revision 2548)
+++ palm/trunk/SOURCE/init_3d_model.f90	(revision 2550)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Modifications to cyclic fill method and turbulence recycling method in case of
+! complex terrain simulations
+! 
+! 2513 2017-10-04 09:24:39Z kanani
 ! Bugfix in storing initial scalar profile (wrong index)
 ! 
@@ -436,5 +440,5 @@
     USE surface_mod,                                                           &
         ONLY :  init_surface_arrays, init_surfaces, surf_def_h, surf_lsm_h,    &
-                surf_usm_h
+                surf_usm_h, get_topography_top_index
    
     USE transpose_indices
@@ -477,4 +481,12 @@
     REAL(wp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner_tmp  !<
 
+    INTEGER(iwp) ::  nz_u_shift   !< 
+    INTEGER(iwp) ::  nz_v_shift   !<
+    INTEGER(iwp) ::  nz_w_shift   !<
+    INTEGER(iwp) ::  nz_s_shift   !<
+    INTEGER(iwp) ::  nz_u_shift_l !<
+    INTEGER(iwp) ::  nz_v_shift_l !<
+    INTEGER(iwp) ::  nz_w_shift_l !<
+    INTEGER(iwp) ::  nz_s_shift_l !< 
 
     CALL location_message( 'allocating arrays', .FALSE. )
@@ -1457,4 +1469,32 @@
 
 !
+!--    In case of complex terrain and cyclic fill method as initialization,
+!--    shift initial data in the vertical direction for each point in the 
+!--    x-y-plane depending on local surface height
+       IF ( complex_terrain  .AND.                                             &
+            TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
+                nz_u_shift = get_topography_top_index( j, i, 'u' )
+                nz_v_shift = get_topography_top_index( j, i, 'v' )
+                nz_w_shift = get_topography_top_index( j, i, 'w' )
+                nz_s_shift = get_topography_top_index( j, i, 's' )
+
+                u(nz_u_shift:nzt+1,j,i)  = u(0:nzt+1-nz_u_shift,j,i)                
+
+                v(nz_v_shift:nzt+1,j,i)  = v(0:nzt+1-nz_v_shift,j,i)
+
+                w(nz_w_shift:nzt+1,j,i)  = w(0:nzt+1-nz_w_shift,j,i)
+
+                e(nz_s_shift:nzt+1,j,i)  =  e(0:nzt+1-nz_s_shift,j,i)
+                p(nz_s_shift:nzt+1,j,i)  =  p(0:nzt+1-nz_s_shift,j,i)
+                pt(nz_s_shift:nzt+1,j,i) = pt(0:nzt+1-nz_s_shift,j,i)
+                km(nz_s_shift:nzt+1,j,i) = km(0:nzt+1-nz_s_shift,j,i)
+                kh(nz_s_shift:nzt+1,j,i) = kh(0:nzt+1-nz_s_shift,j,i)
+             ENDDO
+          ENDDO
+       ENDIF
+
+!
 !--    Initialization of the turbulence recycling method
        IF ( TRIM( initializing_actions ) == 'cyclic_fill'  .AND.               &
@@ -1480,4 +1520,46 @@
           IF ( passive_scalar )                                                &
              mean_inflow_profiles(:,7) = hom_sum(:,115,0)   ! s
+!
+!--       In case of complex terrain, determine vertical displacement at inflow 
+!--       boundary and adjust mean inflow profiles
+          IF ( complex_terrain )  THEN
+             IF ( nxlg <= 0 .AND. nxrg >= 0 .AND. nysg <= 0 .AND. nyng >= 0 )  THEN
+                nz_u_shift_l = get_topography_top_index( 0, 0, 'u' )
+                nz_v_shift_l = get_topography_top_index( 0, 0, 'v' )
+                nz_w_shift_l = get_topography_top_index( 0, 0, 'w' )
+                nz_s_shift_l = get_topography_top_index( 0, 0, 's' )
+             ELSE
+                nz_u_shift_l = 0
+                nz_v_shift_l = 0
+                nz_w_shift_l = 0
+                nz_s_shift_l = 0
+             ENDIF
+
+#if defined( __parallel )
+             CALL MPI_ALLREDUCE(nz_u_shift_l, nz_u_shift, 1, MPI_INTEGER,      &
+                                MPI_MAX, comm2d, ierr)
+             CALL MPI_ALLREDUCE(nz_v_shift_l, nz_v_shift, 1, MPI_INTEGER,      &
+                                MPI_MAX, comm2d, ierr)
+             CALL MPI_ALLREDUCE(nz_w_shift_l, nz_w_shift, 1, MPI_INTEGER,      & 
+                                MPI_MAX, comm2d, ierr)
+             CALL MPI_ALLREDUCE(nz_s_shift_l, nz_s_shift, 1, MPI_INTEGER,      &
+                                MPI_MAX, comm2d, ierr)
+#else
+             nz_u_shift = nz_u_shift_l
+             nz_v_shift = nz_v_shift_l
+             nz_w_shift = nz_w_shift_l
+             nz_s_shift = nz_s_shift_l
+#endif
+
+             mean_inflow_profiles(:,1) = 0.0_wp
+             mean_inflow_profiles(nz_u_shift:nzt+1,1) = hom_sum(0:nzt+1-nz_u_shift,1,0)  ! u
+
+             mean_inflow_profiles(:,2) = 0.0_wp
+             mean_inflow_profiles(nz_v_shift:nzt+1,2) = hom_sum(0:nzt+1-nz_v_shift,2,0)  ! v
+
+             mean_inflow_profiles(nz_s_shift:nzt+1,4) = hom_sum(0:nzt+1-nz_s_shift,4,0)  ! pt
+             mean_inflow_profiles(nz_s_shift:nzt+1,5) = hom_sum(0:nzt+1-nz_s_shift,8,0)  ! e
+
+          ENDIF
 
 !
Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 2548)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 2550)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Set lateral boundary conditions for topography on all three ghost layers
+! 
+! 2478 2017-09-18 13:37:24Z suehring
 ! Bugfix, correct flag for use_top
 ! 
@@ -1041,11 +1044,25 @@
           
           IF ( .NOT. bc_ns_cyc )  THEN
-             IF ( nys == 0  )  nzb_local(-1,:)   = nzb_local(0,:)
-             IF ( nyn == ny )  nzb_local(ny+1,:) = nzb_local(ny,:)
+             IF ( nys == 0  )  THEN
+                nzb_local(-1,:)   = nzb_local(0,:)
+                nzb_local(-2,:)   = nzb_local(0,:)
+                nzb_local(-3,:)   = nzb_local(0,:)
+             ELSEIF ( nyn == ny )  THEN
+                nzb_local(ny+1,:) = nzb_local(ny,:)
+                nzb_local(ny+2,:) = nzb_local(ny,:)
+                nzb_local(ny+3,:) = nzb_local(ny,:)
+             ENDIF
           ENDIF
 
           IF ( .NOT. bc_lr_cyc )  THEN
-             IF ( nxl == 0  )  nzb_local(:,-1)   = nzb_local(:,0)
-             IF ( nxr == nx )  nzb_local(:,nx+1) = nzb_local(:,nx)          
+             IF ( nxl == 0  )  THEN
+                nzb_local(:,-1)   = nzb_local(:,0)
+                nzb_local(:,-2)   = nzb_local(:,0)
+                nzb_local(:,-3)   = nzb_local(:,0)
+             ELSEIF ( nxr == nx )  THEN
+                nzb_local(:,nx+1) = nzb_local(:,nx)
+                nzb_local(:,nx+2) = nzb_local(:,nx)          
+                nzb_local(:,nx+3) = nzb_local(:,nx)          
+             ENDIF          
           ENDIF
 
Index: palm/trunk/SOURCE/modules.f90
===================================================================
--- palm/trunk/SOURCE/modules.f90	(revision 2548)
+++ palm/trunk/SOURCE/modules.f90	(revision 2550)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! complex_terrain namelist parameter added
+! 
+! 2508 2017-10-02 08:57:09Z suehring
 ! Change default value for pt/q/s/sa_vertical_gradient_level
 ! 
@@ -1154,4 +1157,5 @@
     LOGICAL ::  cloud_physics = .FALSE.                      !< namelist parameter
     LOGICAL ::  cloud_top_radiation = .FALSE.                !< namelist parameter
+    LOGICAL ::  complex_terrain = .FALSE.                    !< namelist parameter
     LOGICAL ::  conserve_volume_flow = .FALSE.               !< namelist parameter
     LOGICAL ::  constant_diffusion = .FALSE.                 !< diffusion coefficient constant?
@@ -1923,8 +1927,8 @@
 
     CHARACTER (LEN=40) ::  region(0:9)  !< label for statistic region
-    
-    INTEGER(iwp) ::  pr_palm = 130          !< maximum number of output profiles
+ 
+    INTEGER(iwp) ::  pr_palm = 200          !< maximum number of output profiles
     INTEGER(iwp) ::  statistic_regions = 0  !< identifier for statistic regions
-    
+
     INTEGER(iwp) ::  u_max_ijk(3) = -1  !< index values (i,j,k) of location where u_max occurs
     INTEGER(iwp) ::  v_max_ijk(3) = -1  !< index values (i,j,k) of location where v_max occurs
Index: palm/trunk/SOURCE/parin.f90
===================================================================
--- palm/trunk/SOURCE/parin.f90	(revision 2548)
+++ palm/trunk/SOURCE/parin.f90	(revision 2550)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Added complex_terrain
+! 
+! 2544 2017-10-13 18:09:32Z maronga
 ! Moved day_of_year_init and time_utc_init to inipar.
 ! 
@@ -394,5 +397,6 @@
              cloud_physics, cloud_scheme, cloud_top_radiation,                 &
              cloud_water_sedimentation,                                        &
-             collective_wait, collision_turbulence, conserve_volume_flow,      &
+             collective_wait, collision_turbulence, complex_terrain,           &
+             conserve_volume_flow,                                             &
              conserve_volume_flow_mode, constant_flux_layer,                   &
              coupling_start_time, curvature_solution_effects_bulk,             &
