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

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

Bugfixes, missing coupling_char for child-domain ASCII file; uninitialized array in single_building setup

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