source: palm/tags/release-6.0/SOURCE/init_grid.f90 @ 3800

Last change on this file since 3800 was 3294, checked in by raasch, 5 years ago

modularization of the ocean code

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