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

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

pointer attribute removed from scalar 3d-array for performance reasons

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