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

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

In projection of non-building 3D objects onto numerical grid remove dependency on building_type

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