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

Last change on this file since 3291 was 3241, checked in by raasch, 6 years ago

various changes to avoid compiler warnings (mainly removal of unused variables)

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