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

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

relational operators .EQ., .NE., etc. replaced by ==, /=, etc.

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