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

Last change on this file since 3847 was 3763, checked in by suehring, 5 years ago

Replace work-around for ghost point exchange of 1-byte arrays with specific subroutine

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