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

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

New vertical stretching procedure has been introduced

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