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

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

Bugfix in referencing buildings on orography top; minor bugfix for last LSM commit

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