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

Last change on this file since 3761 was 3761, checked in by raasch, 5 years ago

unused variables removed, OpenACC directives re-formatted, statements added to avoid compiler warnings

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