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

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

Minor bugfix in building mapping when all building IDs in the model domain are missing

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