source: palm/trunk/SOURCE/init_grid.f90 @ 3103

Last change on this file since 3103 was 3103, checked in by suehring, 6 years ago

Reference lowest terrain height to zero level; new check for negative terrain heights in static input file

  • Property svn:keywords set to Id
File size: 117.3 KB
RevLine 
[1682]1!> @file init_grid.f90
[2000]2!------------------------------------------------------------------------------!
[2696]3! This file is part of the PALM model system.
[1036]4!
[2000]5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
[1036]9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
[2718]17! Copyright 1997-2018 Leibniz Universitaet Hannover
[2000]18!------------------------------------------------------------------------------!
[1036]19!
[254]20! Current revisions:
[1]21! -----------------
[2233]22!
[3049]23!
[2233]24! Former revisions:
25! -----------------
26! $Id: init_grid.f90 3103 2018-07-04 17:30:52Z suehring $
[3103]27! Reference lowest terrain height to zero level
28!
29! 3068 2018-06-12 14:49:41Z Giersch
[3068]30! New warning message concerning grid stretching has been introduced
31!
32! 3066 2018-06-12 08:55:55Z Giersch
[3066]33! Bugfix in IF statement before error message
34!
35! 3065 2018-06-12 07:03:02Z Giersch
[3065]36! New vertical stretching mechanism introduced
37!
38! 3051 2018-05-30 17:43:55Z suehring
[3051]39! Minor bugfix concerning mapping 3D buildings on top of terrain
40!
41! 3045 2018-05-28 07:55:41Z Giersch
[3045]42! Error messages revised
43!
[3049]44! 3045 2018-05-28 07:55:41Z Giersch
45! Error messages revised
46!
[3045]47! 2968 2018-04-13 11:52:24Z suehring
[2968]48! Bugfix in initialization in case of elevated model surface. Introduce
49! index for minimum topography-top.
50!
51! 2955 2018-04-09 15:14:01Z suehring
[2955]52! Improve topography filter routine and add ghost-point exchange for building
53! ID and building type.
54!
55! 2927 2018-03-23 15:13:00Z suehring
[2927]56! Bugfix, setting boundary conditions for topography index array.
57!
58! 2918 2018-03-21 15:52:14Z gronemeier
[2918]59! Moved init_mixing_length to turbulence_closure_mod.f90
60!
61! 2897 2018-03-15 11:47:16Z suehring
[2897]62! Relax restrictions for topography input, terrain and building heights can be
63! input separately and are not mandatory any more.
64!
65! 2893 2018-03-14 16:20:52Z suehring
[2893]66! Revise informative message concerning filtered topography (1 grid-point
67! holes).
68!
69! 2892 2018-03-14 15:06:29Z suehring
[2892]70! Bugfix, uninitialized array in case of single_building.
71!
72! 2867 2018-03-09 09:40:23Z suehring
[2867]73! Revise mapping of 3D buildings onto onto orography.
74!
75! 2823 2018-02-20 15:31:45Z Giersch
[2823]76! Set boundary conditions for 3D topography in case of non-cyclic boundary
77! conditions
78!
79! 2796 2018-02-08 12:25:39Z suehring
[2796]80! Bugfix in 3D building initialization
81!
82! 2747 2018-01-15 12:44:17Z suehring
[2747]83! Bugfix, topography height is rounded to the nearest discrete grid level
84!
85! 2718 2018-01-02 08:49:38Z maronga
[2716]86! Corrected "Former revisions" section
[2701]87!
[2716]88! 2701 2017-12-15 15:40:50Z suehring
89! Changes from last commit documented
90!
[2701]91! 2698 2017-12-14 18:46:24Z suehring
[2716]92! Bugfix in get_topography_top_index
93!
94! 2696 2017-12-14 17:12:51Z kanani
95! Change in file header (GPL part)
[2696]96! Revised topography input
97! Set nzb_max not for the entire nest domain, only for boundary PEs
98! Re-organize routine, split-up into several subroutines
99! Modularize poismg_noopt
100! Remove setting bit 26, 27, 28 in wall_flags_0, indicating former '_outer'
101! arrays (not required any more). 
102! Bugfix in generic tunnel setup (MS)
103!
104! 2550 2017-10-16 17:12:01Z boeske
[2550]105! Set lateral boundary conditions for topography on all three ghost layers
106!
107! 2478 2017-09-18 13:37:24Z suehring
[2478]108! Bugfix, correct flag for use_top
109!
110! 2365 2017-08-21 14:59:59Z kanani
[2365]111! Vertical nesting implemented (SadiqHuq)
112!
113! 2319 2017-07-20 17:33:17Z suehring
[2319]114! Remove print statements
115!
116! 2318 2017-07-20 17:27:44Z suehring
[2318]117! Get topography top index via Function call
118!
119! 2317 2017-07-20 17:27:19Z suehring
[2302]120! Bugfixes in reading 3D topography from file
121!
122! 2274 2017-06-09 13:27:48Z Giersch
[2274]123! Changed error messages
124!
125! 2233 2017-05-30 18:08:54Z suehring
[2233]126!
127! 2232 2017-05-30 17:47:52Z suehring
[2232]128! - Adjustments according to new topography representation
129! - Bugfix: Move determination of nzb_max behind topography modification in
130!   cell-edge case
131! - Get rid off global arrays required for topography output
132! - Enable topography input via netcdf
133! - Generic tunnel set-up added
[1969]134!
[2201]135! 2200 2017-04-11 11:37:51Z suehring
136! monotonic_adjustment removed
137!
[2170]138! 2169 2017-03-06 18:16:35Z suehring
139! Bugfix, move setting for topography grid convention to init_grid, else, if no
140! value is set, the simulation may abort in case of restarts
141!
[2129]142! 2128 2017-01-23 15:00:03Z suehring
143! Bugfix in setting topography from file in case of ocean simulations
144!
[2089]145! 2088 2016-12-19 16:30:25Z suehring
146! Bugfix in generic topography in case of ocean simulations
147!
[2038]148! 2037 2016-10-26 11:15:40Z knoop
149! Anelastic approximation implemented
150!
[2022]151! 2021 2016-10-07 14:08:57Z suehring
152! Bugfix: setting Neumann boundary conditions for topography required for
153! topography flags in multigrid_noopt solver
154!
[2001]155! 2000 2016-08-20 18:09:15Z knoop
156! Forced header and separation lines into 80 columns
157!
[1995]158! 1994 2016-08-15 09:52:21Z suehring
159! Bugfix in definition of generic topography
160!
[1983]161! 1982 2016-08-01 11:04:48Z suehring
162! Bugfix concering consistency check for topography
163!
[1969]164! 1968 2016-07-18 12:01:49Z suehring
[1968]165! Changed: PE-wise reading of topography file in order to avoid global definition
166! of arrays nzb_local and nzb_tmp. Thereby, topography definition for single
167! buildings and street canyons has changed, as well as flag setting for
168! multigrid scheme.
169!
170! Bugfix in checking l_grid anisotropy.
171! Simplify initial computation of lwall and vertical_influence, i.e. remove
172! nzb_s_inner as it is still zero at this point.
[1932]173!
[1943]174! 1942 2016-06-14 12:18:18Z suehring
175! Topography filter implemented to fill holes resolved by only one grid point.
176! Initialization of flags for ws-scheme moved to advec_ws. 
177!
[1932]178! 1931 2016-06-10 12:06:59Z suehring
179! Rename multigrid into multigrid_noopt and multigrid_fast into multigrid
180!
[1911]181! 1910 2016-05-26 06:49:46Z raasch
182! Bugfix: if topography is read from file, Neumann conditions are used for the
183! nzb_local array (instead of cyclic conditions) in case that non-cyclic
184! boundary conditions are switched on for the run
185!
[1903]186! 1902 2016-05-09 11:18:56Z suehring
[1910]187! Set topography flags for multigrid solver only (not for multigrid_fast)
[1903]188!
[1887]189! 1886 2016-04-21 11:20:47Z suehring
190! Bugfix: setting advection flags near walls
191! reformulated index values for nzb_v_inner
192! variable discriptions added in declaration block
193!
[1846]194! 1845 2016-04-08 08:29:13Z raasch
195! nzb_2d removed
196!
[1805]197! 1804 2016-04-05 16:30:18Z maronga
198! Removed code for parameter file check (__check)
199!
[1780]200! 1779 2016-03-03 08:01:28Z raasch
201! coupling_char is trimmed at every place it occurs, because it can have
202! different length now
203!
[1763]204! 1762 2016-02-25 12:31:13Z hellstea
205! Introduction of nested domain feature
206!
[1744]207! 1743 2016-01-13 10:23:51Z raasch
208! Bugfix for calculation of nzb_s_outer and nzb_u_outer at north boundary of
209! total domain
210!
[1692]211! 1691 2015-10-26 16:17:44Z maronga
212! Renamed prandtl_layer to constant_flux_layer.
213!
[1683]214! 1682 2015-10-07 23:56:08Z knoop
215! Code annotations made doxygen readable
216!
[1678]217! 1677 2015-10-02 13:25:23Z boeske
218! Bugfix: Ghost points are included in wall_flags_0 and wall_flags_00
219!
[1676]220! 1675 2015-10-02 08:28:59Z gronemeier
221! Bugfix: Definition of topography grid levels
222!
[1662]223! 1660 2015-09-21 08:15:16Z gronemeier
224! Bugfix: Definition of topography grid levels if vertical grid stretching
225!         starts below the maximum topography height.
226!
[1581]227! 1580 2015-04-10 13:43:49Z suehring
228! Bugfix: setting flags for 5th order scheme near buildings
229!
[1576]230! 1575 2015-03-27 09:56:27Z raasch
231! adjustments for psolver-queries
232!
[1558]233! 1557 2015-03-05 16:43:04Z suehring
234! Adjustment for monotoinic limiter
235!
[1419]236! 1418 2014-06-06 13:05:08Z fricke
237! Bugfix: Change if-condition for stretched grid in the ocean, with the old
238!          condition and a negative value for dz_stretch_level the condition
239!          was always true for the whole model domain
240!
[1410]241! 1409 2014-05-23 12:11:32Z suehring
242! Bugfix: set wall_flags_0 at inflow and outflow boundary also for i <= nxlu
243! j <= nysv
244!
[1354]245! 1353 2014-04-08 15:21:23Z heinze
246! REAL constants provided with KIND-attribute
247!
[1323]248! 1322 2014-03-20 16:38:49Z raasch
249! REAL constants defined as wp-kind
250!
[1321]251! 1320 2014-03-20 08:40:49Z raasch
[1320]252! ONLY-attribute added to USE-statements,
253! kind-parameters added to all INTEGER and REAL declaration statements,
254! kinds are defined in new module kinds,
255! revision history before 2012 removed,
256! comment fields (!:) to be used for variable explanations added to
257! all variable declaration statements
[1321]258!
[1222]259! 1221 2013-09-10 08:59:13Z raasch
260! wall_flags_00 introduced to hold bits 32-63,
261! additional 3D-flag arrays for replacing the 2D-index array nzb_s_inner in
262! loops optimized for openACC (pres + flow_statistics)
263!
[1093]264! 1092 2013-02-02 11:24:22Z raasch
265! unused variables removed
266!
[1070]267! 1069 2012-11-28 16:18:43Z maronga
[1779]268! bugfix: added coupling_char to TOPOGRAPHY_DATA to allow topography in the
269!         ocean model in case of coupled runs
[1070]270!
[1037]271! 1036 2012-10-22 13:43:42Z raasch
272! code put under GPL (PALM 3.9)
273!
[1017]274! 1015 2012-09-27 09:23:24Z raasch
275! lower index for calculating wall_flags_0 set to nzb_w_inner instead of
276! nzb_w_inner+1
277!
[997]278! 996 2012-09-07 10:41:47Z raasch
279! little reformatting
280!
[979]281! 978 2012-08-09 08:28:32Z fricke
282! Bugfix: nzb_max is set to nzt at non-cyclic lateral boundaries
283! Bugfix: Set wall_flags_0 for inflow boundary
284!
[928]285! 927 2012-06-06 19:15:04Z raasch
286! Wall flags are not set for multigrid method in case of masking method
287!
[865]288! 864 2012-03-27 15:10:33Z gryschka
[927]289! In case of ocean and Dirichlet bottom bc for u and v dzu_mg and ddzu_pres
290! were not correctly defined for k=1.
[865]291!
[863]292! 861 2012-03-26 14:18:34Z suehring
[861]293! Set wall_flags_0. The array is needed for degradation in ws-scheme near walls,
294! inflow and outflow boundaries as well as near the bottom and the top of the
[863]295! model domain.!
[861]296! Initialization of nzb_s_inner and nzb_w_inner.
297! gls has to be at least nbgp to do not exceed the array bounds of nzb_local
298! while setting wall_flags_0
299!
[844]300! 843 2012-02-29 15:16:21Z gryschka
301! In case of ocean and dirichlet bc for u and v at the bottom
302! the first u-level ist defined at same height as the first w-level
303!
[819]304! 818 2012-02-08 16:11:23Z maronga
305! Bugfix: topo_height is only required if topography is used. It is thus now
306! allocated in the topography branch
307!
[810]308! 809 2012-01-30 13:32:58Z maronga
309! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
310!
[808]311! 807 2012-01-25 11:53:51Z maronga
312! New cpp directive "__check" implemented which is used by check_namelist_files
313!
[1]314! Revision 1.1  1997/08/11 06:17:45  raasch
315! Initial revision (Testversion)
316!
317!
318! Description:
[2696]319! -----------------------------------------------------------------------------!
[1682]320!> Creating grid depending constants
[2696]321!> @todo: Rearrange topo flag list
322!> @todo: reference 3D buildings on top of orography is not tested and may need
323!>        further improvement for steep slopes
324!> @todo: Use more advanced setting of building type at filled holes
[1]325!------------------------------------------------------------------------------!
[1682]326 SUBROUTINE init_grid
327 
[1942]328    USE advec_ws,                                                              &
329        ONLY:  ws_init_flags
[1]330
[1320]331    USE arrays_3d,                                                             &
[2696]332        ONLY:  dd2zu, ddzu, ddzu_pres, ddzw, dzu, dzw, zu, zw
[1320]333       
[1353]334    USE control_parameters,                                                    &
[1910]335        ONLY:  bc_lr_cyc, bc_ns_cyc, building_height, building_length_x,       &
[1320]336               building_length_y, building_wall_left, building_wall_south,     &
337               canyon_height, canyon_wall_left, canyon_wall_south,             &
[1691]338               canyon_width_x, canyon_width_y, constant_flux_layer,            &
[3065]339               dp_level_ind_b, dz, dz_max, dz_stretch_factor,                  &   
340               dz_stretch_factor_array, dz_stretch_level, dz_stretch_level_end,&
341               dz_stretch_level_end_index, dz_stretch_level_start_index,       &
342               dz_stretch_level_start, grid_level,                             &
[2696]343               force_bound_l, force_bound_r, force_bound_n, force_bound_s,     &
344               ibc_uv_b, inflow_l, inflow_n, inflow_r, inflow_s,               &
345               masking_method, maximum_grid_level, message_string,             &
[3065]346               momentum_advec, nest_domain, nest_bound_l,                      &
347               nest_bound_n, nest_bound_r, nest_bound_s,                       &
348               number_stretch_level_end, number_stretch_level_start, ocean,    &
349               outflow_l, outflow_n, outflow_r, outflow_s, psolver,            & 
350               scalar_advec, topography, topography_grid_convention,           &
351               tunnel_height, tunnel_length, tunnel_width_x, tunnel_width_y,   &
352               tunnel_wall_depth, use_surface_fluxes, use_top_fluxes,          &
353               wall_adjustment_factor
[2021]354         
[1320]355    USE grid_variables,                                                        &
[2232]356        ONLY:  ddx, ddx2, ddy, ddy2, dx, dx2, dy, dy2, zu_s_inner, zw_w_inner
[1320]357       
358    USE indices,                                                               &
[2696]359        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
[2232]360               nzb, nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer,              &
361               nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,                 &
[1845]362               nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner,             &
[2968]363               nzb_w_outer, nzt, topo_min_level
[1320]364   
365    USE kinds
[2696]366
[1]367    USE pegrid
368
[2696]369    USE poismg_noopt_mod
370
[2232]371    USE surface_mod,                                                           &
[2698]372        ONLY:  get_topography_top_index, get_topography_top_index_ji, init_bc
[2232]373
[2365]374    USE vertical_nesting_mod,                                                  &
375        ONLY:  vnested, vnest_init_grid
376
[1]377    IMPLICIT NONE
378
[3065]379    INTEGER(iwp) ::  i                           !< index variable along x
380    INTEGER(iwp) ::  j                           !< index variable along y
381    INTEGER(iwp) ::  k                           !< index variable along z
382    INTEGER(iwp) ::  k_top                       !< topography top index on local PE
383    INTEGER(iwp) ::  n                           !< loop variable for stretching
384    INTEGER(iwp) ::  number_dz                   !< number of user-specified dz values       
385    INTEGER(iwp) ::  nzb_local_max               !< vertical grid index of maximum topography height
386    INTEGER(iwp) ::  nzb_local_min               !< vertical grid index of minimum topography height
[2232]387                                     
[3065]388    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local  !< index for topography top at cell-center
389    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp    !< dummy to calculate topography indices on u- and v-grid
[1]390
[2696]391    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
[2232]392
[3065]393    REAL(wp) ::  dz_level_end  !< distance between calculated height level for u/v-grid and user-specified end level for stretching
[1886]394    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
[3065]395   
396    REAL(wp), DIMENSION(:), ALLOCATABLE ::  min_dz_stretch_level_end !< Array that contains all minimum heights where the stretching can end
[861]397
[1]398
399!
[709]400!-- Calculation of horizontal array bounds including ghost layers
[667]401    nxlg = nxl - nbgp
402    nxrg = nxr + nbgp
403    nysg = nys - nbgp
404    nyng = nyn + nbgp
[709]405
[667]406!
[1]407!-- Allocate grid arrays
[1353]408    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
[2696]409              dzw(1:nzt+1), zu(nzb:nzt+1), zw(nzb:nzt+1) )
[1]410
411!
412!-- Compute height of u-levels from constant grid length and dz stretch factors
[3065]413    IF ( dz(1) == -1.0_wp )  THEN
[254]414       message_string = 'missing dz'
415       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
[3065]416    ELSEIF ( dz(1) <= 0.0_wp )  THEN
417       WRITE( message_string, * ) 'dz=',dz(1),' <= 0.0'
[254]418       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
[1]419    ENDIF
[94]420
[1]421!
[3065]422!-- Initialize dz_stretch_level_start with the value of dz_stretch_level
423!-- if it was set by the user
424    IF ( dz_stretch_level /= -9999999.9_wp ) THEN
425       dz_stretch_level_start(1) = dz_stretch_level
426    ENDIF
427       
428!
429!-- Determine number of dz values and stretching levels specified by the
430!-- user to allow right controlling of the stretching mechanism and to
431!-- perform error checks
432    number_dz = COUNT( dz /= -1.0_wp )
433    number_stretch_level_start = COUNT( dz_stretch_level_start /=              &
434                                       -9999999.9_wp )
435    number_stretch_level_end = COUNT( dz_stretch_level_end /=                  &
436                                      9999999.9_wp )
437
438!
439!-- The number of specified end levels +1 has to be the same than the number
440!-- of specified dz values
441    IF ( number_dz /= number_stretch_level_end + 1 ) THEN
442       WRITE( message_string, * ) 'The number of values for dz = ',         &
443                                   number_dz, 'has to be the same than& ',  &
444                                   'the number of values for ',             &
445                                   'dz_stretch_level_end + 1 = ',           &
446                                   number_stretch_level_end+1
447          CALL message( 'init_grid', 'PA0156', 1, 2, 0, 6, 0 )
448    ENDIF
449   
450!
451!--    The number of specified start levels has to be the same or one less than
452!--    the number of specified dz values
453    IF ( number_dz /= number_stretch_level_start + 1 .AND.                  &
454         number_dz /= number_stretch_level_start ) THEN
455       WRITE( message_string, * ) 'The number of values for dz = ',         &
456                                   number_dz, 'has to be the same or one ', &
457                                   'more than& the number of values for ',  &
458                                   'dz_stretch_level_start = ',             &
459                                   number_stretch_level_start
460          CALL message( 'init_grid', 'PA0211', 1, 2, 0, 6, 0 )
461    ENDIF
462   
463!--    The number of specified start levels has to be the same or one more than
464!--    the number of specified end levels
465    IF ( number_stretch_level_start /= number_stretch_level_end + 1 .AND.   &
466         number_stretch_level_start /= number_stretch_level_end ) THEN
467       WRITE( message_string, * ) 'The number of values for ',              &
468                                  'dz_stretch_level_start = ',              &
469                                   dz_stretch_level_start, 'has to be the ',&
470                                   'same or one more than& the number of ', &
471                                   'values for dz_stretch_level_end = ',    &
472                                   number_stretch_level_end
473          CALL message( 'init_grid', 'PA0216', 1, 2, 0, 6, 0 )
474    ENDIF
475
476!
477!-- Initialize dz for the free atmosphere with the value of dz_max
478    IF ( dz(number_stretch_level_start+1) == -1.0_wp .AND.                     &
479         number_stretch_level_start /= 0 ) THEN
480       dz(number_stretch_level_start+1) = dz_max
481    ENDIF
482       
483!
484!-- Initialize the stretching factor if (infinitely) stretching in the free
485!-- atmosphere is desired (dz_stretch_level_end was not specified for the
486!-- free atmosphere)
487    IF ( number_stretch_level_start == number_stretch_level_end + 1 ) THEN
488       dz_stretch_factor_array(number_stretch_level_start) =                   &
489       dz_stretch_factor
490    ENDIF
491   
492!
493!-- Allocation of arrays for stretching
494    ALLOCATE( min_dz_stretch_level_end(number_stretch_level_start) )
[3066]495
[3065]496!
[94]497!-- Define the vertical grid levels
498    IF ( .NOT. ocean )  THEN
[3065]499   
[94]500!
[3065]501!--    The stretching region has to be large enough to allow for a smooth
502!--    transition between two different grid spacings
503       DO n = 1, number_stretch_level_start
504          min_dz_stretch_level_end(n) = dz_stretch_level_start(n) +            &
505                                        4 * MAX( dz(n),dz(n+1) )
506       ENDDO
507
[3066]508       IF ( ANY( min_dz_stretch_level_end(1:number_stretch_level_start) >      &
509                 dz_stretch_level_end(1:number_stretch_level_start) ) ) THEN
[3065]510             message_string= 'Eeach dz_stretch_level_end has to be larger ' // &
511                             'than its corresponding value for &' //           &
512                             'dz_stretch_level_start + 4*MAX(dz(n),dz(n+1)) '//&
513                             'to allow for smooth grid stretching'
514             CALL message( 'init_grid', 'PA0224', 1, 2, 0, 6, 0 )
515       ENDIF
516       
517!
518!--    Stretching must not be applied within the prandtl_layer
519!--    (first two grid points). For the default case dz_stretch_level_start
520!--    is negative. Therefore the absolut value is checked here.
521       IF ( ANY( ABS( dz_stretch_level_start ) < dz(1) * 1.5_wp ) ) THEN
522          WRITE( message_string, * ) 'Eeach dz_stretch_level_start has to be ',&
523                                     'larger than ', dz(1) * 1.5
524             CALL message( 'init_grid', 'PA0226', 1, 2, 0, 6, 0 )
525       ENDIF
526
527!
528!--    The stretching has to start and end on a grid level. Therefore
529!--    user-specified values have to ''interpolate'' to the next lowest level
530       IF ( number_stretch_level_start /= 0 ) THEN
531          dz_stretch_level_start(1) = INT( (dz_stretch_level_start(1) -        &
532                                            dz(1)/2.0) / dz(1) )               &
533                                      * dz(1) + dz(1)/2.0
534       ENDIF
535       
536       IF ( number_stretch_level_start > 1 ) THEN
537          DO n = 2, number_stretch_level_start
538             dz_stretch_level_start(n) = INT( dz_stretch_level_start(n) /      &
539                                              dz(n) ) * dz(n)
540          ENDDO
541       ENDIF
542       
543       IF ( number_stretch_level_end /= 0 ) THEN
544          DO n = 1, number_stretch_level_end
545             dz_stretch_level_end(n) = INT( dz_stretch_level_end(n) /          &
546                                            dz(n+1) ) * dz(n+1)
547          ENDDO
548       ENDIF
549 
550!
551!--    Determine stretching factor if necessary
552       IF ( number_stretch_level_end >= 1 ) THEN
553          CALL calculate_stretching_factor( number_stretch_level_end )
554       ENDIF
555
556!
[94]557!--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
[3065]558!--    First compute the u- and v-levels. In case of dirichlet bc for u and v
559!--    the first u/v- and w-level (k=0) are defined at same height (z=0).
[843]560!--    The second u-level (k=1) corresponds to the top of the
[94]561!--    Prandtl-layer.
[667]562       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
[1353]563          zu(0) = 0.0_wp
[667]564       ELSE
[3065]565          zu(0) = - dz(1) * 0.5_wp
[667]566       ENDIF
[3065]567         
568       zu(1) =   dz(1) * 0.5_wp
569       
570!
571!--    Determine u and v height levels considering the possibility of grid
572!--    stretching in several heights.
573       n = 1
574       dz_stretch_level_start_index = nzt+1
575       dz_stretch_level_end_index = nzt+1
576       dz_stretched = dz(1)
[1]577
[3065]578!--    The default value of dz_stretch_level_start is negative, thus the first
579!--    condition is always true. Hence, the second condition is necessary.
[94]580       DO  k = 2, nzt+1
[3065]581          IF ( dz_stretch_level_start(n) <= zu(k-1) .AND.                      &
582               dz_stretch_level_start(n) /= -9999999.9_wp ) THEN
583             dz_stretched = dz_stretched * dz_stretch_factor_array(n)
584             
585             IF ( dz(n) > dz(n+1) ) THEN
586                dz_stretched = MAX( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (higher) dz
587             ELSE
588                dz_stretched = MIN( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (lower) dz
589             ENDIF
590             
591             IF ( dz_stretch_level_start_index(n) == nzt+1 )                         &
592             dz_stretch_level_start_index(n) = k-1
593             
[94]594          ENDIF
[3065]595         
[94]596          zu(k) = zu(k-1) + dz_stretched
[3065]597         
598!
599!--       Make sure that the stretching ends exactly at dz_stretch_level_end
600          dz_level_end = ABS( zu(k) - dz_stretch_level_end(n) ) 
601         
602          IF ( dz_level_end  < dz(n+1)/3.0 ) THEN
603             zu(k) = dz_stretch_level_end(n)
604             dz_stretched = dz(n+1)
605             dz_stretch_level_end_index(n) = k
606             n = n + 1             
607          ENDIF
[94]608       ENDDO
[1]609
610!
[94]611!--    Compute the w-levels. They are always staggered half-way between the
[843]612!--    corresponding u-levels. In case of dirichlet bc for u and v at the
613!--    ground the first u- and w-level (k=0) are defined at same height (z=0).
614!--    The top w-level is extrapolated linearly.
[1353]615       zw(0) = 0.0_wp
[94]616       DO  k = 1, nzt
[1353]617          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]618       ENDDO
[1353]619       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
[1]620
[94]621    ELSE
[3065]622
[1]623!
[3065]624!--    The stretching region has to be large enough to allow for a smooth
625!--    transition between two different grid spacings
626       DO n = 1, number_stretch_level_start
627          min_dz_stretch_level_end(n) = dz_stretch_level_start(n) -            &
628                                        4 * MAX( dz(n),dz(n+1) )
629       ENDDO
630       
[3066]631       IF ( ANY( min_dz_stretch_level_end (1:number_stretch_level_start) <     &
632                 dz_stretch_level_end(1:number_stretch_level_start) ) ) THEN
[3065]633             message_string= 'Eeach dz_stretch_level_end has to be less ' //   &
634                             'than its corresponding value for &' //           &
635                             'dz_stretch_level_start - 4*MAX(dz(n),dz(n+1)) '//&
636                             'to allow for smooth grid stretching'
637             CALL message( 'init_grid', 'PA0224', 1, 2, 0, 6, 0 )
638       ENDIF
639       
640!
[3068]641!--    Stretching must not be applied close to the surface (last two grid
642!--    points). For the default case dz_stretch_level_start is negative.
643       IF ( ANY( dz_stretch_level_start > - dz(1) * 1.5_wp ) ) THEN
[3065]644          WRITE( message_string, * ) 'Eeach dz_stretch_level_start has to be ',&
645                                     'less than ', dz(1) * 1.5
646             CALL message( 'init_grid', 'PA0226', 1, 2, 0, 6, 0 )
647       ENDIF
648
649!
650!--    The stretching has to start and end on a grid level. Therefore
651!--    user-specified values have to ''interpolate'' to the next highest level
652       IF ( number_stretch_level_start /= 0 ) THEN
653          dz_stretch_level_start(1) = INT( (dz_stretch_level_start(1) +        &
654                                            dz(1)/2.0) / dz(1) )               &
655                                      * dz(1) - dz(1)/2.0
656       ENDIF
657       
658       IF ( number_stretch_level_start > 1 ) THEN
659          DO n = 2, number_stretch_level_start
660             dz_stretch_level_start(n) = INT( dz_stretch_level_start(n) /      &
661                                              dz(n) ) * dz(n)
662          ENDDO
663       ENDIF
664       
665       IF ( number_stretch_level_end /= 0 ) THEN
666          DO n = 1, number_stretch_level_end
667             dz_stretch_level_end(n) = INT( dz_stretch_level_end(n) /          &
668                                            dz(n+1) ) * dz(n+1)
669          ENDDO
670       ENDIF
671       
672!
673!--    Determine stretching factor if necessary
674       IF ( number_stretch_level_end >= 1 ) THEN
675          CALL calculate_stretching_factor( number_stretch_level_end )
676       ENDIF
677
678!
[843]679!--    Grid for ocean with free water surface is at k=nzt (w-grid).
680!--    In case of neumann bc at the ground the first first u-level (k=0) lies
681!--    below the first w-level (k=0). In case of dirichlet bc the first u- and
682!--    w-level are defined at same height, but staggered from the second level.
683!--    The second u-level (k=1) corresponds to the top of the Prandtl-layer.
[3065]684!--    z values are negative starting from z=0 (surface)
685       zu(nzt+1) =   dz(1) * 0.5_wp
686       zu(nzt)   = - dz(1) * 0.5_wp
[94]687
[3065]688!
689!--    Determine u and v height levels considering the possibility of grid
690!--    stretching in several heights.
691       n = 1
692       dz_stretch_level_start_index = 0
693       dz_stretch_level_end_index = 0
694       dz_stretched = dz(1)
695
[94]696       DO  k = nzt-1, 0, -1
[3065]697         
698          IF ( dz_stretch_level_start(n) >= zu(k+1) ) THEN
699             dz_stretched = dz_stretched * dz_stretch_factor_array(n)
700
701             IF ( dz(n) > dz(n+1) ) THEN
702                dz_stretched = MAX( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (higher) dz
703             ELSE
704                dz_stretched = MIN( dz_stretched, dz(n+1) ) !Restrict dz_stretched to the user-specified (lower) dz
705             ENDIF
706             
707             IF ( dz_stretch_level_start_index(n) == 0 )                             &
708             dz_stretch_level_start_index(n) = k+1
709             
710          ENDIF
711         
712          zu(k) = zu(k+1) - dz_stretched
713         
[1418]714!
[3065]715!--       Make sure that the stretching ends exactly at dz_stretch_level_end
716          dz_level_end = ABS( zu(k) - dz_stretch_level_end(n) ) 
717         
718          IF ( dz_level_end  < dz(n+1)/3.0 ) THEN
719             zu(k) = dz_stretch_level_end(n)
720             dz_stretched = dz(n+1)
721             dz_stretch_level_end_index(n) = k
722             n = n + 1             
[94]723          ENDIF
724       ENDDO
[3065]725       
[94]726!
727!--    Compute the w-levels. They are always staggered half-way between the
[843]728!--    corresponding u-levels, except in case of dirichlet bc for u and v
729!--    at the ground. In this case the first u- and w-level are defined at
730!--    same height. The top w-level (nzt+1) is not used but set for
731!--    consistency, since w and all scalar variables are defined up tp nzt+1.
[3065]732       zw(nzt+1) = dz(1)
[1353]733       zw(nzt)   = 0.0_wp
[94]734       DO  k = 0, nzt
[1353]735          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]736       ENDDO
737
[843]738!
739!--    In case of dirichlet bc for u and v the first u- and w-level are defined
740!--    at same height.
741       IF ( ibc_uv_b == 0 ) THEN
742          zu(0) = zw(0)
743       ENDIF
744
[94]745    ENDIF
746
747!
[1]748!-- Compute grid lengths.
749    DO  k = 1, nzt+1
750       dzu(k)  = zu(k) - zu(k-1)
[1353]751       ddzu(k) = 1.0_wp / dzu(k)
[1]752       dzw(k)  = zw(k) - zw(k-1)
[1353]753       ddzw(k) = 1.0_wp / dzw(k)
[1]754    ENDDO
755
756    DO  k = 1, nzt
[1353]757       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
[1]758    ENDDO
[667]759   
760!   
[709]761!-- The FFT- SOR-pressure solvers assume grid spacings of a staggered grid
762!-- everywhere. For the actual grid, the grid spacing at the lowest level
763!-- is only dz/2, but should be dz. Therefore, an additional array
764!-- containing with appropriate grid information is created for these
765!-- solvers.
[1575]766    IF ( psolver(1:9) /= 'multigrid' )  THEN
[667]767       ALLOCATE( ddzu_pres(1:nzt+1) )
768       ddzu_pres = ddzu
[864]769       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
[1]770    ENDIF
771
772!
773!-- Compute the reciprocal values of the horizontal grid lengths.
[1353]774    ddx = 1.0_wp / dx
775    ddy = 1.0_wp / dy
[1]776    dx2 = dx * dx
777    dy2 = dy * dy
[1353]778    ddx2 = 1.0_wp / dx2
779    ddy2 = 1.0_wp / dy2
[1]780
781!
[2696]782!-- Allocate 3D array to set topography
783    ALLOCATE( topo(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
784    topo = 0
785!
786!-- Initialize topography by generic topography or read from topography from file. 
787    CALL init_topo( topo )
788!
789!-- Set flags to mask topography on the grid.
790    CALL set_topo_flags( topo )   
791!
792!-- Calculate wall flag arrays for the multigrid method.
793!-- Please note, wall flags are only applied in the non-optimized version.
794    IF ( psolver == 'multigrid_noopt' )  CALL poismg_noopt_init 
795
796!
797!-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e.
798!-- to decrease the numerical stencil appropriately.
799    IF ( momentum_advec == 'ws-scheme'  .OR.  scalar_advec == 'ws-scheme' )    &
800       CALL ws_init_flags
801
802!
803!-- Determine the maximum level of topography. It is used for
804!-- steering the degradation of order of the applied advection scheme,
805!-- as well in the lpm.
806!-- In case of non-cyclic lateral boundaries, the order of the advection
807!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
808    k_top = 0
809    DO  i = nxl, nxr
810       DO  j = nys, nyn
811          DO  k = nzb, nzt + 1
812             k_top = MAX( k_top, MERGE( k, 0,                                  &
813                                        .NOT. BTEST( topo(k,j,i), 0 ) ) )
814          ENDDO
815       ENDDO
[1]816    ENDDO
[2696]817#if defined( __parallel )
818    CALL MPI_ALLREDUCE( k_top + 1, nzb_max, 1, MPI_INTEGER,                    & !is +1 really necessary here?
819                        MPI_MAX, comm2d, ierr )
820#else
821    nzb_max = k_top + 1
822#endif
823    IF ( inflow_l  .OR.  outflow_l  .OR.  force_bound_l  .OR.  nest_bound_l  .OR.&
824         inflow_r  .OR.  outflow_r  .OR.  force_bound_r  .OR.  nest_bound_r  .OR.&
825         inflow_n  .OR.  outflow_n  .OR.  force_bound_n  .OR.  nest_bound_n  .OR.&
826         inflow_s  .OR.  outflow_s  .OR.  force_bound_s  .OR.  nest_bound_s )    &
827         nzb_max = nzt
828!   
829!-- Finally, if topography extents up to the model top, limit nzb_max to nzt.
[2968]830    nzb_max = MIN( nzb_max, nzt )
[1]831!
[2968]832!-- Determine minimum index of topography. Usually, this will be nzb. In case
833!-- there is elevated topography, however, the lowest topography will be higher.
834!-- This index is e.g. used to calculate mean first-grid point atmosphere
835!-- temperature, surface pressure and density, etc. .
836    topo_min_level   = 0
837#if defined( __parallel )
838    CALL MPI_ALLREDUCE( MINVAL( get_topography_top_index( 's' ) ),             &
839                        topo_min_level, 1, MPI_INTEGER, MPI_MIN, comm2d, ierr )
840#else
841    topo_min_level = MINVAL( get_topography_top_index( 's' ) )
842#endif
843!
[2696]844!-- Initialize boundary conditions via surface type
845    CALL init_bc
846!
847!-- Allocate and set topography height arrays required for data output
848    IF ( TRIM( topography ) /= 'flat' )  THEN
849!
850!--    Allocate and set the arrays containing the topography height
851       IF ( nxr == nx  .AND.  nyn /= ny )  THEN
852          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn),                             &
853                    zw_w_inner(nxl:nxr+1,nys:nyn) )
854       ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
855          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn+1),                             &
856                    zw_w_inner(nxl:nxr,nys:nyn+1) )
857       ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
858          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn+1),                           &
859                    zw_w_inner(nxl:nxr+1,nys:nyn+1) )
860       ELSE
861          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn),                               &
862                    zw_w_inner(nxl:nxr,nys:nyn) )
863       ENDIF
864
865       zu_s_inner   = 0.0_wp
866       zw_w_inner   = 0.0_wp
867!
868!--    Determine local topography height on scalar and w-grid. Note, setting
869!--    lateral boundary values is not necessary, realized via wall_flags_0
870!--    array. Further, please note that loop bounds are different from
871!--    nxl to nxr and nys to nyn on south and right model boundary, hence,
872!--    use intrinsic lbound and ubound functions to infer array bounds.
873       DO  i = LBOUND(zu_s_inner, 1), UBOUND(zu_s_inner, 1)
874          DO  j = LBOUND(zu_s_inner, 2), UBOUND(zu_s_inner, 2)
875!
876!--          Topography height on scalar grid. Therefore, determine index of
877!--          upward-facing surface element on scalar grid.
[2698]878             zu_s_inner(i,j) = zu( get_topography_top_index_ji( j, i, 's' ) )
[2696]879!
880!--          Topography height on w grid. Therefore, determine index of
881!--          upward-facing surface element on w grid.
[2698]882             zw_w_inner(i,j) = zw( get_topography_top_index_ji( j, i, 's' ) )
[2696]883          ENDDO
884       ENDDO
885    ENDIF
886
887!
888!-- In the following, calculate 2D index arrays. Note, these will be removed
889!-- soon.
[1]890!-- Allocate outer and inner index arrays for topography and set
[2232]891!-- defaults.                   
[2696]892    ALLOCATE( nzb_s_inner(nysg:nyng,nxlg:nxrg),                                &
893              nzb_s_outer(nysg:nyng,nxlg:nxrg),                                &
894              nzb_u_inner(nysg:nyng,nxlg:nxrg),                                &
895              nzb_u_outer(nysg:nyng,nxlg:nxrg),                                &
896              nzb_v_inner(nysg:nyng,nxlg:nxrg),                                &
897              nzb_v_outer(nysg:nyng,nxlg:nxrg),                                &
898              nzb_w_inner(nysg:nyng,nxlg:nxrg),                                &
899              nzb_w_outer(nysg:nyng,nxlg:nxrg),                                &
900              nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                           &
901              nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                           &
902              nzb_local(nysg:nyng,nxlg:nxrg),                                  &
903              nzb_tmp(nysg:nyng,nxlg:nxrg) )
904!
905!-- Initialize 2D-index arrays. Note, these will be removed soon!
906    nzb_local(nys:nyn,nxl:nxr) = get_topography_top_index( 's' )
907    CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
[2968]908!
909!-- Check topography for consistency with model domain. Therefore, use
910!-- maximum and minium topography-top indices. Note, minimum topography top
911!-- index is already calculated. 
[2696]912    IF ( TRIM( topography ) /= 'flat' )  THEN
913#if defined( __parallel )
914       CALL MPI_ALLREDUCE( MAXVAL( get_topography_top_index( 's' ) ),          &
[2968]915                           nzb_local_max, 1, MPI_INTEGER, MPI_MAX, comm2d, ierr )             
[2696]916#else
917       nzb_local_max = MAXVAL( get_topography_top_index( 's' ) )
918#endif
[2968]919       nzb_local_min = topo_min_level
[2696]920!
921!--    Consistency checks
922       IF ( nzb_local_min < 0  .OR.  nzb_local_max  > nz + 1 )  THEN
923          WRITE( message_string, * ) 'nzb_local values are outside the',       &
[3045]924                                ' model domain',                               &
[3046]925                                '&MINVAL( nzb_local ) = ', nzb_local_min,      &
926                                '&MAXVAL( nzb_local ) = ', nzb_local_max
[2696]927          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
928       ENDIF
929    ENDIF
[1]930
931    nzb_s_inner = nzb;  nzb_s_outer = nzb
932    nzb_u_inner = nzb;  nzb_u_outer = nzb
933    nzb_v_inner = nzb;  nzb_v_outer = nzb
934    nzb_w_inner = nzb;  nzb_w_outer = nzb
935
936!
[19]937!-- Define vertical gridpoint from (or to) which on the usual finite difference
[1]938!-- form (which does not use surface fluxes) is applied
[1691]939    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
[1]940       nzb_diff = nzb + 2
941    ELSE
942       nzb_diff = nzb + 1
943    ENDIF
944
945    nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
[2696]946!
947!-- Set Neumann conditions for topography. Will be removed soon.
948    IF ( .NOT. bc_ns_cyc )  THEN
949       IF ( nys == 0  )  THEN
[2927]950          DO  i = 1, nbgp 
951             nzb_local(nys-i,:)   = nzb_local(nys,:)
952          ENDDO
[2696]953       ELSEIF ( nyn == ny )  THEN
[2927]954          DO  i = 1, nbgp 
955             nzb_local(ny+i,:) = nzb_local(ny,:)
956          ENDDO
[2696]957       ENDIF
958    ENDIF
[1]959
[2696]960    IF ( .NOT. bc_lr_cyc )  THEN
961       IF ( nxl == 0  )  THEN
[2927]962          DO  i = 1, nbgp 
963             nzb_local(:,nxl-i)   = nzb_local(:,nxl)
964          ENDDO
[2696]965       ELSEIF ( nxr == nx )  THEN
[2927]966          DO  i = 1, nbgp 
967             nzb_local(:,nx+i) = nzb_local(:,nx)
968          ENDDO 
[2696]969       ENDIF         
970    ENDIF
[1]971!
[2696]972!-- Initialization of 2D index arrays, will be removed soon!
973!-- Initialize nzb_s_inner and nzb_w_inner
974    nzb_s_inner = nzb_local
975    nzb_w_inner = nzb_local
976
977!
978!-- Initialize remaining index arrays:
979!-- first pre-initialize them with nzb_s_inner...
980    nzb_u_inner = nzb_s_inner
981    nzb_u_outer = nzb_s_inner
982    nzb_v_inner = nzb_s_inner
983    nzb_v_outer = nzb_s_inner
984    nzb_w_outer = nzb_s_inner
985    nzb_s_outer = nzb_s_inner
986
987!
988!-- nzb_s_outer:
989!-- extend nzb_local east-/westwards first, then north-/southwards
990    nzb_tmp = nzb_local
991    DO  j = nys, nyn
992       DO  i = nxl, nxr
993          nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
994                              nzb_local(j,i+1) )
995       ENDDO
996    ENDDO
997       
998    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
999     
1000    DO  i = nxl, nxr
1001       DO  j = nys, nyn
1002          nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
1003                                  nzb_tmp(j+1,i) )
1004       ENDDO
1005!
1006!--    non-cyclic boundary conditions (overwritten by call of
1007!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
1008       IF ( nys == 0 )  THEN
1009          j = -1
1010          nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
1011       ENDIF
1012       IF ( nyn == ny )  THEN
1013          j = ny + 1
1014          nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
1015       ENDIF
1016    ENDDO
1017!
1018!-- nzb_w_outer:
1019!-- identical to nzb_s_outer
1020    nzb_w_outer = nzb_s_outer
1021!
1022!-- nzb_u_inner:
1023!-- extend nzb_local rightwards only
1024    nzb_tmp = nzb_local
1025    DO  j = nys, nyn
1026       DO  i = nxl, nxr
1027          nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
1028       ENDDO
1029    ENDDO
1030       
1031    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
1032       
1033    nzb_u_inner = nzb_tmp
1034!
1035!-- nzb_u_outer:
1036!-- extend current nzb_tmp (nzb_u_inner) north-/southwards
1037    DO  i = nxl, nxr
1038       DO  j = nys, nyn
1039          nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
1040                                  nzb_tmp(j+1,i) )
1041       ENDDO
1042!
1043!--    non-cyclic boundary conditions (overwritten by call of
1044!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
1045       IF ( nys == 0 )  THEN
1046          j = -1
1047          nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
1048       ENDIF
1049       IF ( nyn == ny )  THEN
1050          j = ny + 1
1051          nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
1052       ENDIF
1053    ENDDO
1054
1055!
1056!-- nzb_v_inner:
1057!-- extend nzb_local northwards only
1058    nzb_tmp = nzb_local
1059    DO  i = nxl, nxr
1060       DO  j = nys, nyn
1061          nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
1062       ENDDO
1063    ENDDO
1064       
1065    CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )     
1066    nzb_v_inner = nzb_tmp
1067
1068!
1069!-- nzb_v_outer:
1070!-- extend current nzb_tmp (nzb_v_inner) right-/leftwards
1071    DO  j = nys, nyn
1072       DO  i = nxl, nxr
1073          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),                &
1074                                  nzb_tmp(j,i+1) )
1075       ENDDO
1076!
1077!--    non-cyclic boundary conditions (overwritten by call of
1078!--    exchange_horiz_2d_int below in case of cyclic boundary conditions)
1079       IF ( nxl == 0 )  THEN
1080          i = -1
1081          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) )
1082       ENDIF
1083       IF ( nxr == nx )  THEN
1084          i = nx + 1
1085          nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) )
1086       ENDIF
1087    ENDDO
1088
1089!
1090!-- Exchange of lateral boundary values (parallel computers) and cyclic
1091!-- boundary conditions, if applicable.
1092!-- Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local
1093!-- they do not require exchange and are not included here.
1094    CALL exchange_horiz_2d_int( nzb_u_inner, nys, nyn, nxl, nxr, nbgp )
1095    CALL exchange_horiz_2d_int( nzb_u_outer, nys, nyn, nxl, nxr, nbgp )
1096    CALL exchange_horiz_2d_int( nzb_v_inner, nys, nyn, nxl, nxr, nbgp )
1097    CALL exchange_horiz_2d_int( nzb_v_outer, nys, nyn, nxl, nxr, nbgp )
1098    CALL exchange_horiz_2d_int( nzb_w_outer, nys, nyn, nxl, nxr, nbgp )
1099    CALL exchange_horiz_2d_int( nzb_s_outer, nys, nyn, nxl, nxr, nbgp )
1100
1101!
1102!-- Set the individual index arrays which define the k index from which on
1103!-- the usual finite difference form (which does not use surface fluxes) is
1104!-- applied
1105    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1106       nzb_diff_s_inner   = nzb_s_inner + 2
1107       nzb_diff_s_outer   = nzb_s_outer + 2
1108    ELSE
1109       nzb_diff_s_inner   = nzb_s_inner + 1
1110       nzb_diff_s_outer   = nzb_s_outer + 1
1111    ENDIF
1112!
1113!-- Vertical nesting: communicate vertical grid level arrays between fine and
1114!-- coarse grid
1115    IF ( vnested )  CALL vnest_init_grid
1116
1117 END SUBROUTINE init_grid
1118
[3065]1119
[2696]1120! Description:
1121! -----------------------------------------------------------------------------!
[3065]1122!> Calculation of the stretching factor through an iterative method. Ideas were
1123!> taken from the paper "Regional stretched grid generation and its application
1124!> to the NCAR RegCM (1999)". Normally, no analytic solution exists because the
1125!> system of equations has two variables (r,l) but four requirements
1126!> (l=integer, r=[0,88;1,2], Eq(6), Eq(5) starting from index j=1) which
1127!> results into an overdetermined system.
1128!------------------------------------------------------------------------------!
1129 SUBROUTINE calculate_stretching_factor( number_end )
1130 
1131    USE control_parameters,                                                    &
1132        ONLY:  dz, dz_stretch_factor, dz_stretch_factor_array,                 &   
1133               dz_stretch_level_end, dz_stretch_level_start, message_string
1134 
1135    USE kinds
1136   
1137    IMPLICIT NONE
1138   
1139    INTEGER(iwp) ::  iterations  !< number of iterations until stretch_factor_lower/upper_limit is reached 
1140    INTEGER(iwp) ::  l_rounded   !< after l_rounded grid levels dz(n) is strechted to dz(n+1) with stretch_factor_2
1141    INTEGER(iwp) ::  n           !< loop variable for stretching
1142   
1143    INTEGER(iwp), INTENT(IN) ::  number_end !< number of user-specified end levels for stretching
1144       
1145    REAL(wp) ::  delta_l               !< absolute difference between l and l_rounded
1146    REAL(wp) ::  delta_stretch_factor  !< absolute difference between stretch_factor_1 and stretch_factor_2
1147    REAL(wp) ::  delta_total_new       !< sum of delta_l and delta_stretch_factor for the next iteration (should be as small as possible)
1148    REAL(wp) ::  delta_total_old       !< sum of delta_l and delta_stretch_factor for the last iteration
1149    REAL(wp) ::  distance              !< distance between dz_stretch_level_start and dz_stretch_level_end (stretching region)
1150    REAL(wp) ::  l                     !< value that fulfil Eq. (5) in the paper mentioned above together with stretch_factor_1 exactly
1151    REAL(wp) ::  numerator             !< numerator of the quotient
1152    REAL(wp) ::  stretch_factor_1      !< stretching factor that fulfil Eq. (5) togehter with l exactly
1153    REAL(wp) ::  stretch_factor_2      !< stretching factor that fulfil Eq. (6) togehter with l_rounded exactly
1154   
[3068]1155    REAL(wp) ::  dz_stretch_factor_array_2(9) = 1.08_wp  !< Array that contains all stretch_factor_2 that belongs to stretch_factor_1
1156   
[3065]1157    REAL(wp), PARAMETER ::  stretch_factor_interval = 1.0E-06  !< interval for sampling possible stretching factors
1158    REAL(wp), PARAMETER ::  stretch_factor_lower_limit = 0.88  !< lowest possible stretching factor
1159    REAL(wp), PARAMETER ::  stretch_factor_upper_limit = 1.12  !< highest possible stretching factor
1160 
1161 
[3068]1162    l = 0
1163    DO  n = 1, number_end
1164   
1165       iterations = 1
1166       stretch_factor_1 = 1.0 
1167       stretch_factor_2 = 1.0
1168       delta_total_old = 1.0
[3065]1169       
[3068]1170       IF ( dz(n) > dz(n+1) ) THEN
1171          DO WHILE ( stretch_factor_1 >= stretch_factor_lower_limit ) 
1172             
1173             stretch_factor_1 = 1.0 - iterations * stretch_factor_interval
1174             distance = ABS( dz_stretch_level_end(n) -                   &
1175                        dz_stretch_level_start(n) ) 
1176             numerator = distance*stretch_factor_1/dz(n) +               &
1177                         stretch_factor_1 - distance/dz(n)
1178             
1179             IF ( numerator > 0.0 ) THEN
1180                l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
1181                l_rounded = NINT( l )
1182                delta_l = ABS( l_rounded - l ) / l
1183             ENDIF
1184             
1185             stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
1186             
1187             delta_stretch_factor = ABS( stretch_factor_1 -              &
1188                                         stretch_factor_2 ) /            &
1189                                    stretch_factor_2
1190             
1191             delta_total_new = delta_l + delta_stretch_factor
[3065]1192
1193!
1194!--                stretch_factor_1 is taken to guarantee that the stretching
1195!--                procedure ends as close as possible to dz_stretch_level_end.
1196!--                stretch_factor_2 would guarantee that the stretched dz(n) is
1197!--                equal to dz(n+1) after l_rounded grid levels.
[3068]1198             IF (delta_total_new < delta_total_old) THEN
1199                dz_stretch_factor_array(n) = stretch_factor_1
1200                dz_stretch_factor_array_2(n) = stretch_factor_2
1201                delta_total_old = delta_total_new
1202             ENDIF
1203             
1204             iterations = iterations + 1
1205           
1206          ENDDO
1207             
1208       ELSEIF ( dz(n) < dz(n+1) ) THEN
1209          DO WHILE ( stretch_factor_1 <= stretch_factor_upper_limit )
1210                     
1211             stretch_factor_1 = 1.0 + iterations * stretch_factor_interval
1212             distance = ABS( dz_stretch_level_end(n) -                      &
1213                        dz_stretch_level_start(n) ) 
1214             numerator = distance*stretch_factor_1/dz(n) +                  &
1215                         stretch_factor_1 - distance/dz(n)
1216             
1217             l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
1218             l_rounded = NINT( l )
1219             delta_l = ABS( l_rounded - l ) / l
1220             
1221             stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
[3065]1222
[3068]1223             delta_stretch_factor = ABS( stretch_factor_1 -                 &
1224                                        stretch_factor_2 ) /                &
1225                                        stretch_factor_2
1226             
1227             delta_total_new = delta_l + delta_stretch_factor
1228             
[3065]1229!
1230!--                stretch_factor_1 is taken to guarantee that the stretching
1231!--                procedure ends as close as possible to dz_stretch_level_end.
1232!--                stretch_factor_2 would guarantee that the stretched dz(n) is
1233!--                equal to dz(n+1) after l_rounded grid levels.
[3068]1234             IF (delta_total_new < delta_total_old) THEN
1235                dz_stretch_factor_array(n) = stretch_factor_1
1236                dz_stretch_factor_array_2(n) = stretch_factor_2
1237                delta_total_old = delta_total_new
1238             ENDIF
[3065]1239             
[3068]1240             iterations = iterations + 1
1241          ENDDO
1242         
1243       ELSE
1244          message_string= 'Two adjacent values of dz must be different'
1245          CALL message( 'init_grid', 'PA0228', 1, 2, 0, 6, 0 )
1246         
1247       ENDIF
1248
1249!
1250!--    Check if also the second stretching factor fits into the allowed
1251!--    interval. If not, print a warning for the user.
1252       IF ( dz_stretch_factor_array_2(n) < stretch_factor_lower_limit .OR.     & 
1253            dz_stretch_factor_array_2(n) > stretch_factor_upper_limit ) THEN
1254          WRITE( message_string, * ) 'stretch_factor_2 = ',                    &
1255                                     dz_stretch_factor_array_2(n), ' which is',&
1256                                     ' responsible for exactly reaching& dz =',&
1257                                      dz(n+1), 'after a specific amount of',   & 
1258                                     ' grid levels& exceeds the upper',        &
1259                                     ' limit =', stretch_factor_upper_limit,   &
1260                                     ' &or lower limit = ',                    &
1261                                     stretch_factor_lower_limit
1262          CALL message( 'init_grid', 'PA0499', 0, 1, 0, 6, 0 )
1263           
1264       ENDIF
1265    ENDDO
[3065]1266       
1267 END SUBROUTINE calculate_stretching_factor
1268 
1269 
1270! Description:
1271! -----------------------------------------------------------------------------!
[2696]1272!> Set temporary topography flags and reference buildings on top of underlying
1273!> orography.
1274!------------------------------------------------------------------------------!
1275 SUBROUTINE process_topography( topo_3d )
1276
1277    USE arrays_3d,                                                             &
[2747]1278        ONLY:  zu, zw
[2696]1279
1280    USE control_parameters,                                                    &
[3103]1281        ONLY:  bc_lr_cyc, bc_ns_cyc, land_surface, message_string, ocean,      &
1282               urban_surface
[2696]1283
1284    USE indices,                                                               &
1285        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb,  &
1286               nzt
1287
1288    USE netcdf_data_input_mod,                                                 &
1289        ONLY:  buildings_f, building_id_f, input_pids_static,                  &
1290               terrain_height_f
1291
1292    USE kinds
1293
1294    USE pegrid
1295
1296    IMPLICIT NONE
1297
[2867]1298    INTEGER(iwp) ::  i                !< running index along x-direction
1299    INTEGER(iwp) ::  j                !< running index along y-direction
1300    INTEGER(iwp) ::  k                !< running index along z-direction with respect to numeric grid
1301    INTEGER(iwp) ::  k2               !< running index along z-direction with respect to netcdf grid
1302    INTEGER(iwp) ::  nr               !< index variable indication maximum terrain height for respective building ID
1303    INTEGER(iwp) ::  num_build        !< counter for number of buildings
1304    INTEGER(iwp) ::  topo_top_index   !< orography top index, used to map 3D buildings onto terrain
[2696]1305
1306    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  displace_dum        !< displacements of start addresses, used for MPI_ALLGATHERV
1307    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids           !< building IDs on entire model domain
1308    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_final     !< building IDs on entire model domain, multiple occurences are sorted out
1309    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_final_tmp !< temporary array used for resizing
1310    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_l         !< building IDs on local subdomain
1311    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  build_ids_l_tmp     !< temporary array used to resize array of building IDs
1312
1313    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  num_buildings     !< number of buildings with different ID on entire model domain
1314    INTEGER(iwp), DIMENSION(0:numprocs-1) ::  num_buildings_l   !< number of buildings with different ID on local subdomain
1315
1316    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo_3d !< input array for 3D topography and dummy array for setting "outer"-flags
1317
1318    REAL(wp)                            ::  ocean_offset        !< offset to consider inverse vertical coordinate at topography definition
[3103]1319    REAL(wp)                            ::  oro_min = 0.0_wp    !< minimum terrain height in entire model domain, used to reference terrain to zero
[2696]1320    REAL(wp), DIMENSION(:), ALLOCATABLE ::  oro_max             !< maximum terrain height occupied by an building with certain id
1321    REAL(wp), DIMENSION(:), ALLOCATABLE ::  oro_max_l           !< maximum terrain height occupied by an building with certain id, on local subdomain
1322
[3103]1323
[2696]1324!
[3103]1325!-- Reference lowest terrain height to zero. In case the minimum terrain height
1326!-- is non-zero, all grid points of the lower vertical grid levels might be
1327!-- entirely below the surface, meaning a waste of computational resources.
1328!-- In order to avoid this, remove the lowest terrain height. Please note,
1329!-- in case of a nested run, the global minimum from all parent and childs
1330!-- need to be remove to avoid steep edges at the child-domain boundaries.
1331    IF ( input_pids_static )  THEN
1332
1333       CALL MPI_ALLREDUCE( MINVAL( terrain_height_f%var ), oro_min, 1,         &
1334                           MPI_REAL, MPI_MIN, MPI_COMM_WORLD, ierr )
1335                           
1336       terrain_height_f%var = terrain_height_f%var - oro_min
1337!                           
1338!--    Give an informative message that terrain height is referenced to zero   
1339       IF ( oro_min > 0.0_wp )  THEN
1340          WRITE( message_string, * ) 'Terrain height was internally shifted '//&
1341                          'downwards by ', oro_min, 'meter(s) to save ' //     &
1342                          'computational resources.'
1343          CALL message( 'init_grid', 'PA0505', 0, 0, 0, 6, 0 )
1344       ENDIF
1345    ENDIF   
1346   
1347!
[2696]1348!-- In the following, buildings and orography are further preprocessed
1349!-- before they are mapped on the LES grid.
1350!-- Buildings are mapped on top of the orography by maintaining the roof
1351!-- shape of the building. This can be achieved by referencing building on
1352!-- top of the maximum terrain height within the area occupied by the
1353!-- respective building. As buildings and terrain height are defined PE-wise,
1354!-- parallelization of this referencing is required (a building can be
1355!-- distributed between different PEs). 
1356!-- In a first step, determine the number of buildings with different
1357!-- building id on each PE. In a next step, all building ids are gathered
1358!-- into one array which is present to all PEs. For each building ID,
1359!-- the maximum terrain height occupied by the respective building is
1360!-- computed and distributed to each PE. 
1361!-- Finally, for each building id and its respective reference orography,
1362!-- builidings are mapped on top.   
1363!--
1364!-- First, pre-set topography flags, bit 1 indicates orography, bit 2
1365!-- buildings
1366!-- classify the respective surfaces.
1367    topo_3d          = IBSET( topo_3d, 0 )
1368    topo_3d(nzb,:,:) = IBCLR( topo_3d(nzb,:,:), 0 )
1369!
[3051]1370!-- In order to map topography on PALM grid also in case of ocean simulations,
1371!-- pre-calculate an offset value.
1372    ocean_offset = MERGE( zw(0), 0.0_wp, ocean )
1373!
[2696]1374!-- Reference buildings on top of orography. This is not necessary
1375!-- if topography is read from ASCII file as no distinction between buildings
1376!-- and terrain height can be made. Moreover, this is also not necessary if
1377!-- urban-surface and land-surface model are used at the same time.
[2897]1378    IF ( input_pids_static )  THEN
1379
1380       IF ( buildings_f%from_file )  THEN
1381          num_buildings_l = 0
1382          num_buildings   = 0
[2696]1383!
[2897]1384!--       Allocate at least one element for building ids,
1385          ALLOCATE( build_ids_l(1) )
1386          DO  i = nxl, nxr
1387             DO  j = nys, nyn
1388                IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
1389                   IF ( num_buildings_l(myid) > 0 )  THEN
1390                      IF ( ANY( building_id_f%var(j,i) .EQ.  build_ids_l ) )   &
1391                      THEN
1392                         CYCLE
1393                      ELSE
1394                         num_buildings_l(myid) = num_buildings_l(myid) + 1
[2696]1395!
1396!--                   Resize array with different local building ids
1397                      ALLOCATE( build_ids_l_tmp(1:SIZE(build_ids_l)) )
1398                      build_ids_l_tmp = build_ids_l
1399                      DEALLOCATE( build_ids_l )
1400                      ALLOCATE( build_ids_l(1:num_buildings_l(myid)) )
1401                      build_ids_l(1:num_buildings_l(myid)-1) =                 &
1402                                  build_ids_l_tmp(1:num_buildings_l(myid)-1)
1403                      build_ids_l(num_buildings_l(myid)) = building_id_f%var(j,i)
1404                      DEALLOCATE( build_ids_l_tmp )
1405                   ENDIF
1406!
[2897]1407!--                First occuring building id on PE
1408                   ELSE
1409                      num_buildings_l(myid) = num_buildings_l(myid) + 1
1410                      build_ids_l(1) = building_id_f%var(j,i)
1411                   ENDIF
[2696]1412                ENDIF
[2897]1413             ENDDO
[2696]1414          ENDDO
1415!
[2897]1416!--       Determine number of different building ids for the entire domain
[2696]1417#if defined( __parallel ) 
[2897]1418          CALL MPI_ALLREDUCE( num_buildings_l, num_buildings, numprocs,              &
1419                              MPI_INTEGER, MPI_SUM, comm2d, ierr ) 
[2696]1420#else
[2897]1421          num_buildings = num_buildings_l
[2696]1422#endif
1423!
[2897]1424!--       Gather all buildings ids on each PEs.
1425!--       First, allocate array encompassing all building ids in model domain. 
1426          ALLOCATE( build_ids(1:SUM(num_buildings)) )
[2696]1427#if defined( __parallel ) 
1428!
[2897]1429!--       Allocate array for displacements.
1430!--       As each PE may has a different number of buildings, so that
1431!--       the block sizes send by each PE may not be equal. Hence,
1432!--       information about the respective displacement is required, indicating
1433!--       the respective adress where each MPI-task writes into the receive
1434!--       buffer array 
1435          ALLOCATE( displace_dum(0:numprocs-1) )
1436          displace_dum(0) = 0
1437          DO i = 1, numprocs-1
1438             displace_dum(i) = displace_dum(i-1) + num_buildings(i-1)
1439          ENDDO
[2696]1440
[2897]1441          CALL MPI_ALLGATHERV( build_ids_l(1:num_buildings_l(myid)),                 &
1442                               num_buildings(myid),                                  &
1443                               MPI_INTEGER,                                          &
1444                               build_ids,                                            &
1445                               num_buildings,                                        &
1446                               displace_dum,                                         & 
1447                               MPI_INTEGER,                                          &
1448                               comm2d, ierr )   
[2696]1449
[2897]1450          DEALLOCATE( displace_dum )
[2696]1451
1452#else
[2897]1453          build_ids = build_ids_l
[2696]1454#endif
1455
1456!
[2897]1457!--       Note, in parallel mode building ids can occure mutliple times, as
1458!--       each PE has send its own ids. Therefore, sort out building ids which
1459!--       appear more than one time.
1460          num_build = 0
1461          DO  nr = 1, SIZE(build_ids)
[2696]1462
[2897]1463             IF ( ALLOCATED(build_ids_final) )  THEN
1464                IF ( ANY( build_ids(nr) .EQ. build_ids_final ) )  THEN
1465                   CYCLE
1466                ELSE
1467                   num_build = num_build + 1
1468!
1469!--                Resize
1470                   ALLOCATE( build_ids_final_tmp(1:num_build) )
1471                   build_ids_final_tmp(1:num_build-1) = build_ids_final(1:num_build-1)
1472                   DEALLOCATE( build_ids_final )
1473                   ALLOCATE( build_ids_final(1:num_build) )
1474                   build_ids_final(1:num_build-1) = build_ids_final_tmp(1:num_build-1)
1475                   build_ids_final(num_build) = build_ids(nr)
1476                   DEALLOCATE( build_ids_final_tmp )
1477                ENDIF             
[2696]1478             ELSE
1479                num_build = num_build + 1
1480                ALLOCATE( build_ids_final(1:num_build) )
1481                build_ids_final(num_build) = build_ids(nr)
[2897]1482             ENDIF
1483          ENDDO
[2696]1484
1485!
[3051]1486!--       Determine maximumum terrain height occupied by the respective
1487!--       building and temporalily store on oro_max
[2897]1488          ALLOCATE( oro_max_l(1:SIZE(build_ids_final)) )
1489          ALLOCATE( oro_max(1:SIZE(build_ids_final))   )
1490          oro_max_l = 0.0_wp
[2696]1491
[2897]1492          DO  nr = 1, SIZE(build_ids_final)
1493             oro_max_l(nr) = MAXVAL(                                              &
1494                              MERGE( terrain_height_f%var, 0.0_wp,                &
1495                                     building_id_f%var(nys:nyn,nxl:nxr) .EQ.      &
1496                                     build_ids_final(nr) ) )
1497          ENDDO
[2696]1498   
1499#if defined( __parallel )   
[2897]1500          IF ( SIZE(build_ids_final) >= 1 ) THEN
1501             CALL MPI_ALLREDUCE( oro_max_l, oro_max, SIZE( oro_max ), MPI_REAL,   &
1502                                 MPI_MAX, comm2d, ierr ) 
1503          ENDIF
[2696]1504#else
[2897]1505          oro_max = oro_max_l
[2696]1506#endif
[3051]1507!
1508!--       Finally, determine discrete grid height of maximum orography occupied
1509!--       by a building. Use all-or-nothing approach, i.e. a grid box is either
1510          oro_max_l = 0.0
1511          DO  nr = 1, SIZE(build_ids_final)
1512             DO  k = nzb, nzt
1513                IF ( zu(k) - ocean_offset <= oro_max(nr) )                     &
1514                   oro_max_l = zw(k) - ocean_offset
1515             ENDDO
1516             oro_max = oro_max_l
1517          ENDDO
[2897]1518       ENDIF
[2696]1519!
[2867]1520!--    Map orography as well as buildings onto grid.
[2696]1521       DO  i = nxl, nxr
1522          DO  j = nys, nyn
[2867]1523             topo_top_index = 0
[2696]1524             DO  k = nzb, nzt
1525!
1526!--             In a first step, if grid point is below or equal the given
1527!--             terrain height, grid point is flagged to be of type natural.
1528!--             Please note, in case there is also a building which is lower
1529!--             than the vertical grid spacing, initialization of surface
1530!--             attributes will not be correct as given surface information
1531!--             will not be in accordance to the classified grid points.
1532!--             Hence, in this case, de-flag the grid point and give it
1533!--             urban type instead.
[2747]1534                IF ( zu(k) - ocean_offset <= terrain_height_f%var(j,i) )  THEN
[2696]1535                    topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
[2867]1536                    topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 1 )
[3051]1537                    topo_top_index = k ! topo_top_index + 1
[2696]1538                ENDIF
1539!
1540!--             Set building grid points. Here, only consider 2D buildings.
1541!--             3D buildings require separate treatment.
[2897]1542                IF ( buildings_f%from_file  .AND.  buildings_f%lod == 1 )  THEN
[2696]1543                   IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
1544!
1545!--                   Determine index where maximum terrain height occupied by
1546!--                   the respective building height is stored.
1547                      nr = MINLOC( ABS( build_ids_final -                      &
1548                                        building_id_f%var(j,i) ), DIM = 1 )
1549       
[2747]1550                      IF ( zu(k) - ocean_offset <=                             &
[2696]1551                           oro_max(nr) + buildings_f%var_2d(j,i) )  THEN
1552                         topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
1553                         topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
1554!
1555!--                      De-flag grid point of type natural. See comment above.
1556                         topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 1 ) 
1557                      ENDIF
1558                   ENDIF
1559                ENDIF
1560             ENDDO
1561!
1562!--          Map 3D buildings onto terrain height. 
[2867]1563!--          In case of any slopes, map building on top of maximum terrain
1564!--          height covered by the building. In other words, extend
1565!--          building down to the respective local terrain-surface height.
[2897]1566             IF ( buildings_f%from_file  .AND.  buildings_f%lod == 2 )  THEN
[2696]1567                IF ( building_id_f%var(j,i) /= building_id_f%fill )  THEN
1568!
[2867]1569!--                Determine index for maximum-terrain-height array.
1570                   nr = MINLOC( ABS( build_ids_final -                         &
1571                                     building_id_f%var(j,i) ), DIM = 1 )
1572!
[3051]1573!--                Extend building down to the terrain surface, i.e. fill-up
1574!--                surface irregularities below a building. Note, oro_max
1575!--                is already a discrete height according to the all-or-nothing
1576!--                approach, i.e. grid box is either topography or atmosphere,
1577!--                terrain top is defined at upper bound of the grid box.
1578!--                Hence, check for zw in this case.
[2867]1579                   DO k = topo_top_index + 1, nzt + 1     
[3051]1580                      IF ( zw(k) - ocean_offset <= oro_max(nr) )  THEN
[2867]1581                         topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
1582                         topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
1583                      ENDIF
[3051]1584                   ENDDO       
[2867]1585!
[3051]1586!--                After surface irregularities are smoothen, determine lower
1587!--                start index where building starts.
1588                   DO  k = nzb, nzt
1589                      IF ( zw(k) - ocean_offset <= oro_max(nr) )               &
1590                         topo_top_index = k
1591                   ENDDO
1592!
1593!--                Finally, map building on top.
[2867]1594                   k2 = 0
1595                   DO k = topo_top_index, nzt + 1
[2796]1596                      IF ( k2 <= buildings_f%nz-1 )  THEN
[2696]1597                         IF ( buildings_f%var_3d(k2,j,i) == 1 )  THEN
1598                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
[2867]1599                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 1 )
[2696]1600                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
1601                         ENDIF
1602                      ENDIF
1603                      k2 = k2 + 1
1604                   ENDDO
1605                ENDIF
1606             ENDIF
1607          ENDDO
1608       ENDDO
1609!
1610!--    Deallocate temporary arrays required for processing and reading data
1611       IF ( ALLOCATED( oro_max         ) )  DEALLOCATE( oro_max         )
1612       IF ( ALLOCATED( oro_max_l       ) )  DEALLOCATE( oro_max_l       )
1613       IF ( ALLOCATED( build_ids_final ) )  DEALLOCATE( build_ids_final )
1614!
1615!-- Topography input via ASCII format.
1616    ELSE
1617       ocean_offset     = MERGE( zw(0), 0.0_wp, ocean )
1618       topo_3d          = IBSET( topo_3d, 0 )
1619       topo_3d(nzb,:,:) = IBCLR( topo_3d(nzb,:,:), 0 )
1620       DO  i = nxl, nxr
1621          DO  j = nys, nyn
1622             DO  k = nzb, nzt
[2747]1623                IF ( zu(k) - ocean_offset <= buildings_f%var_2d(j,i) )  THEN
[2696]1624                    topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
1625                    topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 ) !indicates terrain
1626                ENDIF
1627             ENDDO
1628          ENDDO
1629       ENDDO
1630    ENDIF
1631
1632    CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
1633
1634    IF ( .NOT. bc_ns_cyc )  THEN
1635       IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
1636       IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
1637    ENDIF
1638
1639    IF ( .NOT. bc_lr_cyc )  THEN
1640       IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
1641       IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)         
1642    ENDIF
1643
1644 END SUBROUTINE process_topography
1645
1646
1647! Description:
1648! -----------------------------------------------------------------------------!
1649!> Filter topography, i.e. fill holes resolved by only one grid point. 
1650!> Such holes are suspected to lead to velocity blow-ups as continuity
1651!> equation on discrete grid cannot be fulfilled in such case.
1652!------------------------------------------------------------------------------!
1653 SUBROUTINE filter_topography( topo_3d )
1654
1655    USE control_parameters,                                                    &
1656        ONLY:  bc_lr_cyc, bc_ns_cyc, message_string
1657
1658    USE indices,                                                               &
1659        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nzb, nzt
1660
1661    USE netcdf_data_input_mod,                                                 &
1662        ONLY:  building_id_f, building_type_f 
1663
1664    USE  pegrid
1665
1666    IMPLICIT NONE
1667
[2893]1668    LOGICAL      ::  filled = .FALSE. !< flag indicating if holes were filled
1669
[2696]1670    INTEGER(iwp) ::  i          !< running index along x-direction
1671    INTEGER(iwp) ::  j          !< running index along y-direction
1672    INTEGER(iwp) ::  k          !< running index along z-direction
1673    INTEGER(iwp) ::  num_hole   !< number of holes (in topography) resolved by only one grid point
1674    INTEGER(iwp) ::  num_hole_l !< number of holes (in topography) resolved by only one grid point on local PE     
1675    INTEGER(iwp) ::  num_wall   !< number of surrounding vertical walls for a single grid point
1676
[2955]1677    INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg)           ::  var_exchange_int  !< dummy array for exchanging ghost-points
1678    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE            ::  topo_tmp          !< temporary 3D-topography used to fill holes
1679    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo_3d           !< 3D-topography array merging buildings and orography
[2696]1680!
1681!-- Before checking for holes, set lateral boundary conditions for
1682!-- topography. After hole-filling, boundary conditions must be set again.
1683!-- Several iterations are performed, in order to fill holes which might
1684!-- emerge by the filling-algorithm itself.
1685    ALLOCATE( topo_tmp(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
1686    topo_tmp = 0
1687
1688    num_hole = 99999
1689    DO WHILE ( num_hole > 0 )       
1690
1691       num_hole = 0   
1692       CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
[2955]1693!
1694!--    Exchange also building ID and type. Note, building_type is an one-byte
1695!--    variable.
1696       IF ( building_id_f%from_file )                                          &
1697          CALL exchange_horiz_2d_int( building_id_f%var, nys, nyn, nxl, nxr, nbgp )
1698       IF ( building_type_f%from_file )  THEN
1699          var_exchange_int = INT( building_type_f%var, KIND = 4 )
1700          CALL exchange_horiz_2d_int( var_exchange_int, nys, nyn, nxl, nxr, nbgp )
1701          building_type_f%var = INT( var_exchange_int, KIND = 1 )
1702       ENDIF
[2696]1703
1704       topo_tmp = topo_3d
1705!
1706!--    In case of non-cyclic lateral boundaries, assume lateral boundary to be
1707!--    a solid wall. Thus, intermediate spaces of one grid point between
1708!--    boundary and some topographic structure will be filled.           
1709       IF ( .NOT. bc_ns_cyc )  THEN
1710          IF ( nys == 0  )  topo_tmp(:,-1,:)   = IBCLR( topo_tmp(:,0,:),  0 )
1711          IF ( nyn == ny )  topo_tmp(:,ny+1,:) = IBCLR( topo_tmp(:,ny,:), 0 )
1712       ENDIF
1713
1714       IF ( .NOT. bc_lr_cyc )  THEN
1715          IF ( nxl == 0  )  topo_tmp(:,:,-1)   = IBCLR( topo_tmp(:,:,0),  0 )
1716          IF ( nxr == nx )  topo_tmp(:,:,nx+1) = IBCLR( topo_tmp(:,:,nx), 0 )         
1717       ENDIF
1718
1719       num_hole_l = 0
1720       DO i = nxl, nxr
1721          DO j = nys, nyn
1722             DO  k = nzb+1, nzt
1723                IF ( BTEST( topo_tmp(k,j,i), 0 ) )  THEN
1724                   num_wall = 0
1725                   IF ( .NOT. BTEST( topo_tmp(k,j-1,i), 0 ) )                  &
1726                      num_wall = num_wall + 1
1727                   IF ( .NOT. BTEST( topo_tmp(k,j+1,i), 0 ) )                  &
1728                      num_wall = num_wall + 1
1729                   IF ( .NOT. BTEST( topo_tmp(k,j,i-1), 0 ) )                  &
1730                      num_wall = num_wall + 1
1731                   IF ( .NOT. BTEST( topo_tmp(k,j,i+1), 0 ) )                  &
1732                      num_wall = num_wall + 1
1733                   IF ( .NOT. BTEST( topo_tmp(k-1,j,i), 0 ) )                  &
1734                      num_wall = num_wall + 1   
1735                   IF ( .NOT. BTEST( topo_tmp(k+1,j,i), 0 ) )                  &
1736                      num_wall = num_wall + 1
1737
1738                   IF ( num_wall >= 4 )  THEN
1739                      num_hole_l     = num_hole_l + 1
1740!
1741!--                   Clear flag 0 and set special flag ( bit 3) to indicate
1742!--                   that new topography point is a result of filtering process.
1743                      topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
1744                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 3 )
1745!
1746!--                   If filled grid point is occupied by a building, classify
1747!--                   it as building grid point.
1748                      IF ( building_type_f%from_file )  THEN
1749                         IF ( building_type_f%var(j,i)   /=                    & 
1750                              building_type_f%fill            .OR.             &       
1751                              building_type_f%var(j+1,i) /=                    & 
1752                              building_type_f%fill            .OR.             &               
1753                              building_type_f%var(j-1,i) /=                    &               
1754                              building_type_f%fill            .OR.             &               
1755                              building_type_f%var(j,i+1) /=                    &               
1756                              building_type_f%fill            .OR.             &               
1757                              building_type_f%var(j,i-1) /=                    &               
1758                              building_type_f%fill )  THEN
1759!
1760!--                         Set flag indicating building surfaces
1761                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 2 )
1762!
1763!--                         Set building_type and ID at this position if not
1764!--                         already set. This is required for proper
1765!--                         initialization of urban-surface energy balance
1766!--                         solver.
1767                            IF ( building_type_f%var(j,i) ==                   &
1768                                 building_type_f%fill )  THEN
1769
1770                               IF ( building_type_f%var(j+1,i) /=              &
1771                                    building_type_f%fill )  THEN
1772                                  building_type_f%var(j,i) =                   &
1773                                                    building_type_f%var(j+1,i)
1774                                  building_id_f%var(j,i) =                     &
1775                                                    building_id_f%var(j+1,i)
1776                               ELSEIF ( building_type_f%var(j-1,i) /=          &
1777                                        building_type_f%fill )  THEN
1778                                  building_type_f%var(j,i) =                   &
1779                                                    building_type_f%var(j-1,i)
1780                                  building_id_f%var(j,i) =                     &
1781                                                    building_id_f%var(j-1,i)
1782                               ELSEIF ( building_type_f%var(j,i+1) /=          &
1783                                        building_type_f%fill )  THEN
1784                                  building_type_f%var(j,i) =                   &
1785                                                    building_type_f%var(j,i+1)
1786                                  building_id_f%var(j,i) =                     &
1787                                                    building_id_f%var(j,i+1)
1788                               ELSEIF ( building_type_f%var(j,i-1) /=          &
1789                                        building_type_f%fill )  THEN
1790                                  building_type_f%var(j,i) =                   &
1791                                                    building_type_f%var(j,i-1)
1792                                  building_id_f%var(j,i) =                     &
1793                                                    building_id_f%var(j,i-1)
1794                               ENDIF
1795                            ENDIF
1796                         ENDIF
1797                      ENDIF
1798!
1799!--                   If filled grid point is already classified as building
1800!--                   everything is fine, else classify this grid point as
1801!--                   natural type grid point. This case, values for the
1802!--                   surface type are already set.
1803                      IF ( .NOT. BTEST( topo_3d(k,j,i), 2 ) )  THEN
1804                         topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 1 )
1805                      ENDIF
1806                   ENDIF
1807                ENDIF
1808             ENDDO
1809          ENDDO
1810       ENDDO
1811!
1812!--    Count the total number of holes, required for informative message.
1813#if defined( __parallel )
1814       CALL MPI_ALLREDUCE( num_hole_l, num_hole, 1, MPI_INTEGER, MPI_SUM,      &
1815                           comm2d, ierr )
1816#else
1817       num_hole = num_hole_l
1818#endif   
[2893]1819       IF ( num_hole > 0  .AND.  .NOT. filled )  filled = .TRUE.
[2696]1820
[2893]1821    ENDDO
[2696]1822!
[2893]1823!-- Create an informative message if any holes were filled.
1824    IF ( filled )  THEN
1825       WRITE( message_string, * ) 'Topography was filtered, i.e. holes ' //    &
1826                                  'resolved by only one grid point '     //    &
1827                                  'were filled during initialization.'
1828       CALL message( 'init_grid', 'PA0430', 0, 0, 0, 6, 0 )
1829    ENDIF
[2696]1830
1831    DEALLOCATE( topo_tmp )
1832!
1833!-- Finally, exchange topo_3d array again and if necessary set Neumann boundary
1834!-- condition in case of non-cyclic lateral boundaries.
1835    CALL exchange_horiz_int( topo_3d, nys, nyn, nxl, nxr, nzt, nbgp )
1836
1837    IF ( .NOT. bc_ns_cyc )  THEN
1838       IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
1839       IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
1840    ENDIF
1841
1842    IF ( .NOT. bc_lr_cyc )  THEN
1843       IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
1844       IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)         
1845    ENDIF
[2955]1846!
1847!-- Exchange building ID and type. Note, building_type is an one-byte variable.
1848    IF ( building_id_f%from_file )                                             &
1849       CALL exchange_horiz_2d_int( building_id_f%var, nys, nyn, nxl, nxr, nbgp )
1850    IF ( building_type_f%from_file )  THEN
1851       var_exchange_int = INT( building_type_f%var, KIND = 4 )
1852       CALL exchange_horiz_2d_int( var_exchange_int, nys, nyn, nxl, nxr, nbgp )
1853       building_type_f%var = INT( var_exchange_int, KIND = 1 )
1854    ENDIF
[2696]1855
1856 END SUBROUTINE filter_topography
1857
1858
1859! Description:
1860! -----------------------------------------------------------------------------!
1861!> Reads topography information from file or sets generic topography. Moreover,
1862!> all topography-relevant topography arrays are initialized, and grid flags
1863!> are set. 
1864!------------------------------------------------------------------------------!
1865 SUBROUTINE init_topo( topo )
1866
1867    USE arrays_3d,                                                             &
1868        ONLY:  zw
1869       
1870    USE control_parameters,                                                    &
1871        ONLY:  bc_lr_cyc, bc_ns_cyc, building_height, building_length_x,       &
1872               building_length_y, building_wall_left, building_wall_south,     &
1873               canyon_height, canyon_wall_left, canyon_wall_south,             &
1874               canyon_width_x, canyon_width_y, dp_level_ind_b, dz,             &
1875               message_string, ocean, topography, topography_grid_convention,  &
1876               tunnel_height, tunnel_length, tunnel_width_x, tunnel_width_y,   &
1877               tunnel_wall_depth
1878         
1879    USE grid_variables,                                                        &
1880        ONLY:  dx, dy
1881       
1882    USE indices,                                                               &
1883        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
1884               nzb, nzt
1885   
1886    USE kinds
1887
1888    USE pegrid
1889
1890    USE surface_mod,                                                           &
[2698]1891        ONLY:  get_topography_top_index, get_topography_top_index_ji
[2696]1892
1893    IMPLICIT NONE
1894
1895    INTEGER(iwp) ::  bh            !< temporary vertical index of building height
1896    INTEGER(iwp) ::  blx           !< grid point number of building size along x
1897    INTEGER(iwp) ::  bly           !< grid point number of building size along y
1898    INTEGER(iwp) ::  bxl           !< index for left building wall
1899    INTEGER(iwp) ::  bxr           !< index for right building wall
1900    INTEGER(iwp) ::  byn           !< index for north building wall
1901    INTEGER(iwp) ::  bys           !< index for south building wall
1902    INTEGER(iwp) ::  ch            !< temporary vertical index for canyon height
1903    INTEGER(iwp) ::  cwx           !< grid point number of canyon size along x
1904    INTEGER(iwp) ::  cwy           !< grid point number of canyon size along y
1905    INTEGER(iwp) ::  cxl           !< index for left canyon wall
1906    INTEGER(iwp) ::  cxr           !< index for right canyon wall
1907    INTEGER(iwp) ::  cyn           !< index for north canyon wall
1908    INTEGER(iwp) ::  cys           !< index for south canyon wall
1909    INTEGER(iwp) ::  i             !< index variable along x
1910    INTEGER(iwp) ::  j             !< index variable along y
1911    INTEGER(iwp) ::  k             !< index variable along z
1912    INTEGER(iwp) ::  hv_in         !< heavyside function to model inner tunnel surface
1913    INTEGER(iwp) ::  hv_out        !< heavyside function to model outer tunnel surface
1914    INTEGER(iwp) ::  txe_out       !< end position of outer tunnel wall in x
1915    INTEGER(iwp) ::  txs_out       !< start position of outer tunnel wall in x
1916    INTEGER(iwp) ::  tye_out       !< end position of outer tunnel wall in y
1917    INTEGER(iwp) ::  tys_out       !< start position of outer tunnel wall in y
1918    INTEGER(iwp) ::  txe_in        !< end position of inner tunnel wall in x
1919    INTEGER(iwp) ::  txs_in        !< start position of inner tunnel wall in x
1920    INTEGER(iwp) ::  tye_in        !< end position of inner tunnel wall in y
1921    INTEGER(iwp) ::  tys_in        !< start position of inner tunnel wall in y
1922    INTEGER(iwp) ::  td            !< tunnel wall depth
1923    INTEGER(iwp) ::  th            !< height of outer tunnel wall
1924
1925    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local         !< index for topography top at cell-center
1926    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
1927
1928
1929!
[1]1930!-- Set outer and inner index arrays for non-flat topography.
1931!-- Here consistency checks concerning domain size and periodicity are
1932!-- necessary.
1933!-- Within this SELECT CASE structure only nzb_local is initialized
1934!-- individually depending on the chosen topography type, all other index
1935!-- arrays are initialized further below.
1936    SELECT CASE ( TRIM( topography ) )
1937
1938       CASE ( 'flat' )
[2696]1939!   
[2232]1940!--       Initialilize 3D topography array, used later for initializing flags
[2696]1941          topo(nzb+1:nzt+1,:,:) = IBSET( topo(nzb+1:nzt+1,:,:), 0 ) 
[1]1942
1943       CASE ( 'single_building' )
1944!
1945!--       Single rectangular building, by default centered in the middle of the
1946!--       total domain
1947          blx = NINT( building_length_x / dx )
1948          bly = NINT( building_length_y / dy )
[2232]1949          bh  = MINLOC( ABS( zw - building_height ), 1 ) - 1
1950          IF ( ABS( zw(bh)   - building_height ) == &
[1675]1951               ABS( zw(bh+1) - building_height )    )  bh = bh + 1
[1322]1952          IF ( building_wall_left == 9999999.9_wp )  THEN
[1]1953             building_wall_left = ( nx + 1 - blx ) / 2 * dx
1954          ENDIF
1955          bxl = NINT( building_wall_left / dx )
1956          bxr = bxl + blx
1957
[1322]1958          IF ( building_wall_south == 9999999.9_wp )  THEN
[2696]1959              building_wall_south = ( ny + 1 - bly ) / 2 * dy
[1]1960          ENDIF
1961          bys = NINT( building_wall_south / dy )
1962          byn = bys + bly
1963
1964!
1965!--       Building size has to meet some requirements
[2696]1966          IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR.       &
[1]1967               ( bys < 1 ) .OR. ( byn > ny-1 ) .OR. ( byn < bys+3 ) )  THEN
[274]1968             WRITE( message_string, * ) 'inconsistent building parameters:',   &
[3046]1969                                      '&bxl=', bxl, 'bxr=', bxr, 'bys=', bys,  &
[274]1970                                      'byn=', byn, 'nx=', nx, 'ny=', ny
[254]1971             CALL message( 'init_grid', 'PA0203', 1, 2, 0, 6, 0 )
[1]1972          ENDIF
1973
[2696]1974          ALLOCATE( nzb_local(nysg:nyng,nxlg:nxrg) )
[2892]1975          nzb_local = 0
[1]1976!
[1968]1977!--       Define the building.
1978          IF ( bxl <= nxr  .AND.  bxr >= nxl  .AND.                            &
[2696]1979               bys <= nyn  .AND.  byn >= nys )                                 & 
[1968]1980             nzb_local(MAX(nys,bys):MIN(nyn,byn),MAX(nxl,bxl):MIN(nxr,bxr)) = bh
[2232]1981!
[2696]1982!--       Set bit array on basis of nzb_local
1983          DO  i = nxl, nxr
1984             DO  j = nys, nyn
1985                topo(nzb_local(j,i)+1:nzt+1,j,i) =                             &
1986                                 IBSET( topo(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
[2232]1987             ENDDO
1988          ENDDO
[2696]1989       
1990          DEALLOCATE( nzb_local )
[2232]1991
[2696]1992          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2823]1993!
1994!--       Set boundary conditions also for flags. Can be interpreted as Neumann
1995!--       boundary conditions for topography.
1996          IF ( .NOT. bc_ns_cyc )  THEN
1997             IF ( nys == 0  )  THEN
1998                DO  i = 1, nbgp     
1999                   topo(:,nys-i,:)   = topo(:,nys,:)
2000                ENDDO
2001             ENDIF
2002             IF ( nyn == ny )  THEN
2003                DO  i = 1, nbgp 
2004                   topo(:,nyn+i,:) = topo(:,nyn,:)
2005                ENDDO
2006             ENDIF
2007          ENDIF
2008          IF ( .NOT. bc_lr_cyc )  THEN
2009             IF ( nxl == 0  )  THEN
2010                DO  i = 1, nbgp   
2011                   topo(:,:,nxl-i)   = topo(:,:,nxl)
2012                ENDDO
2013             ENDIF
2014             IF ( nxr == nx )  THEN
2015                DO  i = 1, nbgp   
2016                   topo(:,:,nxr+i) = topo(:,:,nxr)     
2017                ENDDO
2018             ENDIF     
2019          ENDIF
[2232]2020
[240]2021       CASE ( 'single_street_canyon' )
2022!
2023!--       Single quasi-2D street canyon of infinite length in x or y direction.
2024!--       The canyon is centered in the other direction by default.
[1322]2025          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[240]2026!
2027!--          Street canyon in y direction
2028             cwx = NINT( canyon_width_x / dx )
[1322]2029             IF ( canyon_wall_left == 9999999.9_wp )  THEN
[240]2030                canyon_wall_left = ( nx + 1 - cwx ) / 2 * dx
2031             ENDIF
2032             cxl = NINT( canyon_wall_left / dx )
2033             cxr = cxl + cwx
[1322]2034          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[240]2035!
2036!--          Street canyon in x direction
2037             cwy = NINT( canyon_width_y / dy )
[1322]2038             IF ( canyon_wall_south == 9999999.9_wp )  THEN
[240]2039                canyon_wall_south = ( ny + 1 - cwy ) / 2 * dy
2040             ENDIF
2041             cys = NINT( canyon_wall_south / dy )
2042             cyn = cys + cwy
[2696]2043     
[240]2044          ELSE
[254]2045             
2046             message_string = 'no street canyon width given'
2047             CALL message( 'init_grid', 'PA0204', 1, 2, 0, 6, 0 )
2048 
[240]2049          ENDIF
2050
[2232]2051          ch  = MINLOC( ABS( zw - canyon_height ), 1 ) - 1
2052          IF ( ABS( zw(ch)   - canyon_height ) == &
[1675]2053               ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
[240]2054          dp_level_ind_b = ch
2055!
2056!--       Street canyon size has to meet some requirements
[1322]2057          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[1353]2058             IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR.        &
[2696]2059                  ( ch < 3 ) )  THEN
[1353]2060                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
[3046]2061                                           '&cxl=', cxl, ' cxr=', cxr,         &
[3045]2062                                           ' cwx=', cwx,                       &
2063                                           ' ch=', ch, ' nx=', nx, ' ny=', ny
[254]2064                CALL message( 'init_grid', 'PA0205', 1, 2, 0, 6, 0 ) 
[240]2065             ENDIF
[1322]2066          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[1353]2067             IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR.        &
[2696]2068                  ( ch < 3 ) )  THEN
[1353]2069                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
[3046]2070                                           '&cys=', cys, ' cyn=', cyn,         &
[3045]2071                                           ' cwy=', cwy,                       &
2072                                           ' ch=', ch, ' nx=', nx, ' ny=', ny
[254]2073                CALL message( 'init_grid', 'PA0206', 1, 2, 0, 6, 0 ) 
[240]2074             ENDIF
2075          ENDIF
[1353]2076          IF ( canyon_width_x /= 9999999.9_wp .AND.                            &                 
2077               canyon_width_y /= 9999999.9_wp )  THEN
2078             message_string = 'inconsistent canyon parameters:' //             &   
[3046]2079                              '&street canyon can only be oriented' //         &
[3045]2080                              ' either in x- or in y-direction'
[254]2081             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
[240]2082          ENDIF
2083
[2696]2084          ALLOCATE( nzb_local(nysg:nyng,nxlg:nxrg) )
[240]2085          nzb_local = ch
[1322]2086          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[1968]2087             IF ( cxl <= nxr  .AND.  cxr >= nxl )                              &
2088                nzb_local(:,MAX(nxl,cxl+1):MIN(nxr,cxr-1)) = 0
[1322]2089          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[1968]2090             IF ( cys <= nyn  .AND.  cyn >= nys )                              &         
2091                nzb_local(MAX(nys,cys+1):MIN(nyn,cyn-1),:) = 0
[240]2092          ENDIF
[2232]2093!
[2696]2094!--       Set bit array on basis of nzb_local
2095          DO  i = nxl, nxr
2096             DO  j = nys, nyn
2097                topo(nzb_local(j,i)+1:nzt+1,j,i) =                             &
2098                                 IBSET( topo(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
[2232]2099             ENDDO
2100          ENDDO
[2696]2101          DEALLOCATE( nzb_local )
[1994]2102
[2696]2103          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2823]2104!
2105!--       Set boundary conditions also for flags. Can be interpreted as Neumann
2106!--       boundary conditions for topography.
2107          IF ( .NOT. bc_ns_cyc )  THEN
2108             IF ( nys == 0  )  THEN
2109                DO  i = 1, nbgp     
2110                   topo(:,nys-i,:)   = topo(:,nys,:)
2111                ENDDO
2112             ENDIF
2113             IF ( nyn == ny )  THEN
2114                DO  i = 1, nbgp 
2115                   topo(:,nyn+i,:) = topo(:,nyn,:)
2116                ENDDO
2117             ENDIF
2118          ENDIF
2119          IF ( .NOT. bc_lr_cyc )  THEN
2120             IF ( nxl == 0  )  THEN
2121                DO  i = 1, nbgp   
2122                   topo(:,:,nxl-i)   = topo(:,:,nxl)
2123                ENDDO
2124             ENDIF
2125             IF ( nxr == nx )  THEN
2126                DO  i = 1, nbgp   
2127                   topo(:,:,nxr+i) = topo(:,:,nxr)     
2128                ENDDO
2129             ENDIF     
2130          ENDIF
[2232]2131
2132       CASE ( 'tunnel' )
2133
2134!
2135!--       Tunnel height
2136          IF ( tunnel_height == 9999999.9_wp )  THEN
2137             th = zw( INT( 0.2 * nz) )
2138          ELSE
2139             th = tunnel_height
2140          ENDIF
2141!
2142!--       Tunnel-wall depth
[2696]2143          IF ( tunnel_wall_depth == 9999999.9_wp )  THEN 
[3065]2144             td = MAX ( dx, dy, dz(1) )
[2232]2145          ELSE
2146             td = tunnel_wall_depth
2147          ENDIF
2148!
2149!--       Check for tunnel width
2150          IF ( tunnel_width_x == 9999999.9_wp  .AND.                           &
2151               tunnel_width_y == 9999999.9_wp  )  THEN
2152             message_string = 'No tunnel width is given. '
[2274]2153             CALL message( 'init_grid', 'PA0280', 1, 2, 0, 6, 0 )
[2232]2154          ENDIF
2155          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
2156               tunnel_width_y /= 9999999.9_wp  )  THEN
2157             message_string = 'Inconsistent tunnel parameters:' //             &   
2158                              'tunnel can only be oriented' //                 &
2159                              'either in x- or in y-direction.'
[2274]2160             CALL message( 'init_grid', 'PA0281', 1, 2, 0, 6, 0 )
[2232]2161          ENDIF
2162!
2163!--       Tunnel axis along y
2164          IF ( tunnel_width_x /= 9999999.9_wp )  THEN
2165             IF ( tunnel_width_x > ( nx + 1 ) * dx )  THEN
2166                message_string = 'Tunnel width too large'
[2274]2167                CALL message( 'init_grid', 'PA0282', 1, 2, 0, 6, 0 )
[2232]2168             ENDIF
2169
2170             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_width_x * 0.5_wp )
2171             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_width_x * 0.5_wp )
2172             txs_in  = INT( ( nx + 1 ) * 0.5_wp * dx -                         &
2173                                      ( tunnel_width_x * 0.5_wp - td ) )
2174             txe_in  = INT( ( nx + 1 ) * 0.5_wp * dx +                         &
[2696]2175                                   ( tunnel_width_x * 0.5_wp - td ) )
[2232]2176
2177             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_length * 0.5_wp )
2178             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_length * 0.5_wp )
2179             tys_in  = tys_out
2180             tye_in  = tye_out
2181          ENDIF
[2696]2182          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &   
2183               tunnel_width_x - 2.0_wp * td <= 2.0_wp * dx )                   &
2184          THEN
[2232]2185             message_string = 'Tunnel width too small'
[2274]2186             CALL message( 'init_grid', 'PA0175', 1, 2, 0, 6, 0 )
[2232]2187          ENDIF
2188          IF ( tunnel_width_y /= 9999999.9_wp  .AND.                           &
[2696]2189               tunnel_width_y - 2.0_wp * td <= 2.0_wp * dy )                   &
2190          THEN
[2232]2191             message_string = 'Tunnel width too small'
[2274]2192             CALL message( 'init_grid', 'PA0455', 1, 2, 0, 6, 0 )
[2232]2193          ENDIF
2194!
2195!--       Tunnel axis along x
2196          IF ( tunnel_width_y /= 9999999.9_wp )  THEN
2197             IF ( tunnel_width_y > ( ny + 1 ) * dy )  THEN
2198                message_string = 'Tunnel width too large'
[2274]2199                CALL message( 'init_grid', 'PA0456', 1, 2, 0, 6, 0 )
[2232]2200             ENDIF
2201
2202             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_length * 0.5_wp )
2203             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_length * 0.5_wp )
2204             txs_in  = txs_out
2205             txe_in  = txe_out
2206
2207             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_width_y * 0.5_wp )
2208             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_width_y * 0.5_wp )
2209             tys_in  = INT( ( ny + 1 ) * 0.5_wp * dy -                         &
[2696]2210                                        ( tunnel_width_y * 0.5_wp - td ) )
[2232]2211             tye_in  = INT( ( ny + 1 ) * 0.5_wp * dy +                         &
2212                                     ( tunnel_width_y * 0.5_wp - td ) )
2213          ENDIF
2214
[2696]2215          topo = 0
[2232]2216          DO  i = nxl, nxr
2217             DO  j = nys, nyn
2218!
2219!--             Use heaviside function to model outer tunnel surface
2220                hv_out = th * 0.5_wp *                                         &
2221                              ( ( SIGN( 1.0_wp, i * dx - txs_out ) + 1.0_wp )  &
2222                              - ( SIGN( 1.0_wp, i * dx - txe_out ) + 1.0_wp ) )
2223
2224                hv_out = hv_out * 0.5_wp *                                     &
2225                            ( ( SIGN( 1.0_wp, j * dy - tys_out ) + 1.0_wp )    &
2226                            - ( SIGN( 1.0_wp, j * dy - tye_out ) + 1.0_wp ) )
[2696]2227!   
[2232]2228!--             Use heaviside function to model inner tunnel surface
2229                hv_in  = ( th - td ) * 0.5_wp *                                &
2230                                ( ( SIGN( 1.0_wp, i * dx - txs_in ) + 1.0_wp ) &
2231                                - ( SIGN( 1.0_wp, i * dx - txe_in ) + 1.0_wp ) )
2232
2233                hv_in = hv_in * 0.5_wp *                                       &
2234                                ( ( SIGN( 1.0_wp, j * dy - tys_in ) + 1.0_wp ) &
2235                                - ( SIGN( 1.0_wp, j * dy - tye_in ) + 1.0_wp ) )
2236!
2237!--             Set flags at x-y-positions without any tunnel surface
2238                IF ( hv_out - hv_in == 0.0_wp )  THEN
[2696]2239                   topo(nzb+1:nzt+1,j,i) = IBSET( topo(nzb+1:nzt+1,j,i), 0 )
[2232]2240!
2241!--             Set flags at x-y-positions with tunnel surfaces
2242                ELSE
2243                   DO  k = nzb + 1, nzt + 1
2244!
2245!--                   Inner tunnel
2246                      IF ( hv_out - hv_in == th )  THEN
2247                         IF ( zw(k) <= hv_out )  THEN
[2696]2248                            topo(k,j,i) = IBCLR( topo(k,j,i), 0 )
[2232]2249                         ELSE
[2696]2250                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
[2232]2251                         ENDIF
2252                      ENDIF
2253!
2254!--                   Lateral tunnel walls
2255                      IF ( hv_out - hv_in == td )  THEN
2256                         IF ( zw(k) <= hv_in )  THEN
[2696]2257                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
[2232]2258                         ELSEIF ( zw(k) > hv_in  .AND.  zw(k) <= hv_out )  THEN
[2696]2259                            topo(k,j,i) = IBCLR( topo(k,j,i), 0 )
[2232]2260                         ELSEIF ( zw(k) > hv_out )  THEN
[2696]2261                            topo(k,j,i) = IBSET( topo(k,j,i), 0 )
[2232]2262                         ENDIF
2263                      ENDIF
2264                   ENDDO
2265                ENDIF
2266             ENDDO
2267          ENDDO
2268
[2696]2269          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2823]2270!
2271!--       Set boundary conditions also for flags. Can be interpreted as Neumann
2272!--       boundary conditions for topography.
2273          IF ( .NOT. bc_ns_cyc )  THEN
2274             IF ( nys == 0  )  THEN
2275                DO  i = 1, nbgp     
2276                   topo(:,nys-i,:)   = topo(:,nys,:)
2277                ENDDO
2278             ENDIF
2279             IF ( nyn == ny )  THEN
2280                DO  i = 1, nbgp 
2281                   topo(:,nyn+i,:) = topo(:,nyn,:)
2282                ENDDO
2283             ENDIF
2284          ENDIF
2285          IF ( .NOT. bc_lr_cyc )  THEN
2286             IF ( nxl == 0  )  THEN
2287                DO  i = 1, nbgp   
2288                   topo(:,:,nxl-i)   = topo(:,:,nxl)
2289                ENDDO
2290             ENDIF
2291             IF ( nxr == nx )  THEN
2292                DO  i = 1, nbgp   
2293                   topo(:,:,nxr+i) = topo(:,:,nxr)     
2294                ENDDO
2295             ENDIF     
2296          ENDIF
[2232]2297
[1]2298       CASE ( 'read_from_file' )
2299!
[2696]2300!--       Note, topography information have been already read. 
2301!--       If required, further process topography, i.e. reference buildings on
2302!--       top of orography and set temporary 3D topography array, which is
2303!--       used later to set grid flags. Calling of this rouinte is also
2304!--       required in case of ASCII input, even though no distinction between
2305!--       terrain- and building height is made in this case. 
2306          CALL process_topography( topo )
[1968]2307!
[2696]2308!--       Filter holes resolved by only one grid-point
2309          CALL filter_topography( topo )
[1968]2310!
[2696]2311!--       Exchange ghost-points, as well as add cyclic or Neumann boundary
2312!--       conditions.
2313          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2232]2314!
[2696]2315!--       Set lateral boundary conditions for topography on all ghost layers         
[1968]2316          IF ( .NOT. bc_ns_cyc )  THEN
[2550]2317             IF ( nys == 0  )  THEN
[2696]2318                DO  i = 1, nbgp         
2319                   topo(:,nys-i,:) = topo(:,nys,:)
2320                ENDDO
[2550]2321             ENDIF
[2696]2322             IF ( nyn == ny )  THEN
2323                DO  i = 1, nbgp         
2324                   topo(:,nyn+i,:) = topo(:,nyn,:)
2325                ENDDO
2326             ENDIF
[1942]2327          ENDIF
[1910]2328
[1968]2329          IF ( .NOT. bc_lr_cyc )  THEN
[2550]2330             IF ( nxl == 0  )  THEN
[2696]2331                DO  i = 1, nbgp 
2332                   topo(:,:,nxl-i) = topo(:,:,nxl)
[2232]2333                ENDDO
[2696]2334             ENDIF
2335             IF ( nxr == nx )  THEN
2336                DO  i = 1, nbgp 
2337                   topo(:,:,nxr+i) = topo(:,:,nxr)
2338                ENDDO
2339             ENDIF
[2232]2340          ENDIF
2341
[667]2342
[1]2343       CASE DEFAULT
[2696]2344!   
[1]2345!--       The DEFAULT case is reached either if the parameter topography
[217]2346!--       contains a wrong character string or if the user has defined a special
[1]2347!--       case in the user interface. There, the subroutine user_init_grid
2348!--       checks which of these two conditions applies.
[2696]2349          CALL user_init_grid( topo )
2350          CALL filter_topography( topo )
[1]2351
2352    END SELECT
2353!
2354!-- Consistency checks and index array initialization are only required for
[2696]2355!-- non-flat topography.
[1]2356    IF ( TRIM( topography ) /= 'flat' )  THEN
2357!
[2232]2358!--    In case of non-flat topography, check whether the convention how to
2359!--    define the topography grid has been set correctly, or whether the default
2360!--    is applicable. If this is not possible, abort.
2361       IF ( TRIM( topography_grid_convention ) == ' ' )  THEN
2362          IF ( TRIM( topography ) /= 'single_building' .AND.                   &
2363               TRIM( topography ) /= 'single_street_canyon' .AND.              &
2364               TRIM( topography ) /= 'tunnel'  .AND.                           &
2365               TRIM( topography ) /= 'read_from_file')  THEN
2366!--          The default value is not applicable here, because it is only valid
[3045]2367!--          for the four standard cases 'single_building',
2368!--          'single_street_canyon', 'tunnel' and 'read_from_file'
[2232]2369!--          defined in init_grid.
2370             WRITE( message_string, * )                                        &
[2696]2371               'The value for "topography_grid_convention" ',                  &
[3046]2372               'is not set. Its default value is & only valid for ',           &
[3045]2373               '"topography" = ''single_building'', ''tunnel'' ',              &
[3046]2374               '''single_street_canyon'' & or ''read_from_file''.',            &
2375               '& Choose ''cell_edge'' or ''cell_center''.'
[2232]2376             CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
2377          ELSE
2378!--          The default value is applicable here.
2379!--          Set convention according to topography.
2380             IF ( TRIM( topography ) == 'single_building' .OR.                 &
2381                  TRIM( topography ) == 'single_street_canyon' )  THEN
2382                topography_grid_convention = 'cell_edge'
2383             ELSEIF ( TRIM( topography ) == 'read_from_file'  .OR.             &
2384                      TRIM( topography ) == 'tunnel')  THEN
2385                topography_grid_convention = 'cell_center'
2386             ENDIF
2387          ENDIF
2388       ELSEIF ( TRIM( topography_grid_convention ) /= 'cell_edge' .AND.        &
2389                TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
2390          WRITE( message_string, * )                                           &
[2696]2391            'The value for "topography_grid_convention" is ',                  &
[3046]2392            'not recognized.& Choose ''cell_edge'' or ''cell_center''.'
[2232]2393          CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
2394       ENDIF
[1]2395
[2169]2396
[217]2397       IF ( topography_grid_convention == 'cell_edge' )  THEN
[134]2398!
[217]2399!--       The array nzb_local as defined using the 'cell_edge' convention
2400!--       describes the actual total size of topography which is defined at the
2401!--       cell edges where u=0 on the topography walls in x-direction and v=0
2402!--       on the topography walls in y-direction. However, PALM uses individual
2403!--       arrays nzb_u|v|w|s_inner|outer that are based on nzb_s_inner.
2404!--       Therefore, the extent of topography in nzb_local is now reduced by
2405!--       1dx at the E topography walls and by 1dy at the N topography walls
[1968]2406!--       to form the basis for nzb_s_inner.
2407!--       Note, the reverse memory access (i-j instead of j-i) is absolutely
2408!--       required at this point.
2409          DO  j = nys+1, nyn+1
2410             DO  i = nxl-1, nxr
[2232]2411                DO  k = nzb, nzt+1
[2696]2412                   IF ( BTEST( topo(k,j,i), 0 )  .OR.                          &
2413                        BTEST( topo(k,j,i+1), 0 ) )                            &
2414                       topo(k,j,i) = IBSET( topo(k,j,i), 0 )
[2232]2415                ENDDO
2416             ENDDO
2417          ENDDO     
[2696]2418          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2232]2419
2420          DO  i = nxl, nxr+1
2421             DO  j = nys-1, nyn
2422                DO  k = nzb, nzt+1
[2696]2423                   IF ( BTEST( topo(k,j,i), 0 )  .OR.                          &
2424                        BTEST( topo(k,j+1,i), 0 ) )                            &
2425                      topo(k,j,i) = IBSET( topo(k,j,i), 0 )
[2232]2426                ENDDO
2427             ENDDO
2428          ENDDO 
[2696]2429          CALL exchange_horiz_int( topo, nys, nyn, nxl, nxr, nzt, nbgp )
[2232]2430   
[217]2431       ENDIF
[2696]2432    ENDIF
[2232]2433
[1]2434
[2696]2435 END SUBROUTINE init_topo
[1]2436
[2696]2437 SUBROUTINE set_topo_flags(topo)
[1]2438
[2696]2439    USE control_parameters,                                                    &
2440        ONLY:  bc_lr_cyc, bc_ns_cyc, constant_flux_layer, land_surface,        &
2441               use_surface_fluxes, use_top_fluxes, urban_surface
[1]2442
[2696]2443    USE indices,                                                               &
2444        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg, nz,   &
2445               nzb, nzt, wall_flags_0
[1]2446
[2696]2447    USE kinds
[1]2448
[2696]2449    IMPLICIT NONE
[1804]2450
[2696]2451    INTEGER(iwp) ::  i             !< index variable along x
2452    INTEGER(iwp) ::  j             !< index variable along y
2453    INTEGER(iwp) ::  k             !< index variable along z
[1]2454
[2696]2455    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  topo !< input array for 3D topography and dummy array for setting "outer"-flags
[2232]2456
[2696]2457    ALLOCATE( wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2458    wall_flags_0 = 0
[2232]2459!
[2696]2460!-- Set-up topography flags. First, set flags only for s, u, v and w-grid.
2461!-- Further special flags will be set in following loops.
[2232]2462    DO  i = nxl, nxr
2463       DO  j = nys, nyn
2464          DO  k = nzb, nzt+1
2465!
2466!--          scalar grid
[2696]2467             IF ( BTEST( topo(k,j,i), 0 ) )                                 &
[2232]2468                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
2469!
[2696]2470!--          u grid
2471             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
2472                  BTEST( topo(k,j,i-1), 0 ) )                               &
2473                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
2474!
[2232]2475!--          v grid
[2696]2476             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
2477                  BTEST( topo(k,j-1,i), 0 ) )                               &
2478                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
2479
[2232]2480          ENDDO
[1]2481
[2232]2482          DO k = nzb, nzt
[1]2483!
[2232]2484!--          w grid
[2696]2485             IF ( BTEST( topo(k,j,i),   0 )  .AND.                          &
2486                  BTEST( topo(k+1,j,i), 0 ) )                               &
[2232]2487                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
2488          ENDDO
2489          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 3 )
2490
2491       ENDDO
2492    ENDDO
[2696]2493
[2867]2494    CALL exchange_horiz_int( wall_flags_0, nys, nyn, nxl, nxr, nzt, nbgp )
[1]2495!
[2696]2496!-- Set outer array for scalars to mask near-surface grid points in
2497!-- production_e
2498    DO i = nxl, nxr
2499       DO j = nys, nyn
[2232]2500          DO k = nzb, nzt+1
[2696]2501             IF ( BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.                       &
2502                  BTEST( wall_flags_0(k,j+1,i), 0 )  .AND.                       &
2503                  BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.                       &
2504                  BTEST( wall_flags_0(k,j-1,i-1), 0 )  .AND.                       &
2505                  BTEST( wall_flags_0(k,j+1,i-1), 0 )  .AND.                       &
2506                  BTEST( wall_flags_0(k,j-1,i+1), 0 )  .AND.                       &
2507                  BTEST( wall_flags_0(k,j+1,i+1), 0 ) )                            &
2508                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
[2232]2509          ENDDO
2510       ENDDO
2511    ENDDO
[1]2512!
[2232]2513!-- Set further special flags
2514    DO i = nxl, nxr
2515       DO j = nys, nyn
2516          DO k = nzb, nzt+1
[1]2517!
[2232]2518!--          scalar grid, former nzb_diff_s_inner.
2519!--          Note, use this flag also to mask topography in diffusion_u and
2520!--          diffusion_v along the vertical direction. In case of
2521!--          use_surface_fluxes, fluxes are calculated via MOST, else, simple
2522!--          gradient approach is applied. Please note, in case of u- and v-
2523!--          diffuison, a small error is made at edges (on the east side for u,
2524!--          at the north side for v), since topography on scalar grid point
2525!--          is used instead of topography on u/v-grid. As number of topography grid
2526!--          points on uv-grid is different than s-grid, different number of
2527!--          surface elements would be required. In order to avoid this,
2528!--          treat edges (u(k,j,i+1)) simply by a gradient approach, i.e. these
2529!--          points are not masked within diffusion_u. Tests had shown that the
2530!--          effect on the flow is negligible.
2531             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
2532                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
2533                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
2534             ELSE
2535                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
2536             ENDIF
[1]2537
[2232]2538          ENDDO
2539!
2540!--       Special flag to control vertical diffusion at model top - former
2541!--       nzt_diff
2542          wall_flags_0(:,j,i) = IBSET( wall_flags_0(:,j,i), 9 )
2543          IF ( use_top_fluxes )                                                &
[2478]2544             wall_flags_0(nzt+1,j,i) = IBCLR( wall_flags_0(nzt+1,j,i), 9 )
[1]2545
[2696]2546
[2232]2547          DO k = nzb+1, nzt
2548!
2549!--          Special flag on u grid, former nzb_u_inner + 1, required   
2550!--          for disturb_field and initialization. Do not disturb directly at
2551!--          topography, as well as initialize u with zero one grid point outside
2552!--          of topography.
2553             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
2554                  BTEST( wall_flags_0(k,j,i),   1 )  .AND.                     &
2555                  BTEST( wall_flags_0(k+1,j,i), 1 ) )                          &
2556                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
2557!
2558!--          Special flag on v grid, former nzb_v_inner + 1, required   
2559!--          for disturb_field and initialization. Do not disturb directly at
2560!--          topography, as well as initialize v with zero one grid point outside
2561!--          of topography.
2562             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
2563                  BTEST( wall_flags_0(k,j,i),   2 )  .AND.                     &
2564                  BTEST( wall_flags_0(k+1,j,i), 2 ) )                          &
2565                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
2566!
2567!--          Special flag on scalar grid, former nzb_s_inner+1. Used for
2568!--          lpm_sgs_tke
2569             IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                     &
2570                  BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
2571                  BTEST( wall_flags_0(k+1,j,i), 0 ) )                          &
2572                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
2573!
2574!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in
2575!--          in production_e
2576             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
2577                IF ( BTEST( wall_flags_0(k,j,i),   24 )  .AND.                 &
2578                     BTEST( wall_flags_0(k-1,j,i), 24 )  .AND.                 &
2579                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
2580                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
2581             ELSE
2582                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
2583                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
[1]2584             ENDIF
[2232]2585!
2586!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in
2587!--          in production_e
2588             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
2589                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
2590                     BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                  &
2591                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
2592                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
2593             ELSE
2594                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
2595                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
2596             ENDIF
2597          ENDDO
2598!
2599!--       Flags indicating downward facing walls
2600          DO k = nzb+1, nzt
2601!
2602!--          Scalar grid
2603             IF ( BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
2604            .NOT. BTEST( wall_flags_0(k,j,i), 0   ) )                          & 
[2696]2605                 wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 ) 
[2232]2606!
2607!--          Downward facing wall on u grid
2608             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
2609            .NOT. BTEST( wall_flags_0(k,j,i), 1   ) )                          & 
2610                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
2611!
2612!--          Downward facing wall on v grid
2613             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
2614            .NOT. BTEST( wall_flags_0(k,j,i), 2   ) )                          & 
2615                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
2616!
2617!--          Downward facing wall on w grid
2618             IF ( BTEST( wall_flags_0(k-1,j,i), 3 )  .AND.                     &
2619            .NOT. BTEST( wall_flags_0(k,j,i), 3 ) )                            & 
2620                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
2621          ENDDO
2622!
2623!--       Flags indicating upward facing walls
2624          DO k = nzb, nzt
2625!
2626!--          Upward facing wall on scalar grid
2627             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   0 )  .AND.               &
2628                        BTEST( wall_flags_0(k+1,j,i), 0 ) )                    & 
2629                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
2630!
2631!--          Upward facing wall on u grid
2632             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   1 )  .AND.               &
2633                        BTEST( wall_flags_0(k+1,j,i), 1 ) )                    & 
2634                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
[1]2635
[2696]2636!   
[2232]2637!--          Upward facing wall on v grid
2638             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   2 )  .AND.               &
2639                        BTEST( wall_flags_0(k+1,j,i), 2 ) )                    & 
2640                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
[2696]2641   
[2232]2642!
2643!--          Upward facing wall on w grid
2644             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   3 )  .AND.               &
2645                        BTEST( wall_flags_0(k+1,j,i), 3 ) )                    & 
2646                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
2647!
2648!--          Special flag on scalar grid, former nzb_s_inner
2649             IF ( BTEST( wall_flags_0(k,j,i), 0 )  .OR.                        &
2650                  BTEST( wall_flags_0(k,j,i), 12 ) .OR.                        &
2651                  BTEST( wall_flags_0(k,j,i), 13 ) )                           &
[2696]2652                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
[2232]2653!
2654!--          Special flag on scalar grid, nzb_diff_s_inner - 1, required for
2655!--          flow_statistics
2656             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
2657                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
2658                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
[2696]2659                  wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
[2232]2660             ELSE
2661                IF ( BTEST( wall_flags_0(k,j,i), 22 ) )                        &
2662                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
[1]2663             ENDIF
[2696]2664   
[1]2665
[2232]2666          ENDDO
2667          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 22 )
2668          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 23 )
2669       ENDDO
2670    ENDDO
2671!
[2696]2672!-- Finally, set identification flags indicating natural terrain or buildings.
2673!-- Natural terrain grid points.
2674    IF ( land_surface )  THEN
2675       DO i = nxl, nxr
2676          DO j = nys, nyn
2677             DO k = nzb, nzt+1
2678!
2679!--             Natural terrain grid point
2680                IF ( BTEST( topo(k,j,i), 1 ) )                                 &
2681                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 5 )
2682             ENDDO
2683          ENDDO
2684       ENDDO
2685    ENDIF
2686!
2687!-- Building grid points.
2688    IF ( urban_surface )  THEN
2689       DO i = nxl, nxr
2690          DO j = nys, nyn
2691             DO k = nzb, nzt+1
2692                IF ( BTEST( topo(k,j,i), 2 ) )                                 &
2693                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 6 )
2694             ENDDO
2695          ENDDO
2696       ENDDO
2697    ENDIF
2698!
[2232]2699!-- Exchange ghost points for wall flags
[2696]2700    CALL exchange_horiz_int( wall_flags_0, nys, nyn, nxl, nxr, nzt, nbgp )
[2232]2701!
2702!-- Set boundary conditions also for flags. Can be interpreted as Neumann
2703!-- boundary conditions for topography.
2704    IF ( .NOT. bc_ns_cyc )  THEN
[2696]2705       IF ( nys == 0  )  THEN
2706          DO  i = 1, nbgp     
2707             wall_flags_0(:,nys-i,:)   = wall_flags_0(:,nys,:)
2708          ENDDO
2709       ENDIF
2710       IF ( nyn == ny )  THEN
2711          DO  i = 1, nbgp 
2712             wall_flags_0(:,nyn+i,:) = wall_flags_0(:,nyn,:)
2713          ENDDO
2714       ENDIF
[2232]2715    ENDIF
2716    IF ( .NOT. bc_lr_cyc )  THEN
[2696]2717       IF ( nxl == 0  )  THEN
2718          DO  i = 1, nbgp   
2719             wall_flags_0(:,:,nxl-i)   = wall_flags_0(:,:,nxl)
2720          ENDDO
[2232]2721       ENDIF
[2696]2722       IF ( nxr == nx )  THEN
2723          DO  i = 1, nbgp   
2724             wall_flags_0(:,:,nxr+i) = wall_flags_0(:,:,nxr)     
[2232]2725          ENDDO
[2696]2726       ENDIF     
[2232]2727    ENDIF
[1]2728
[1968]2729
[2696]2730 END SUBROUTINE set_topo_flags
[114]2731
2732
2733
Note: See TracBrowser for help on using the repository browser.