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

Last change on this file since 2839 was 2823, checked in by Giersch, 6 years ago

Set boundary condtions for 3D topography in case of non-cyclic boundary conditions

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