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

Last change on this file since 3115 was 3115, checked in by suehring, 6 years ago

Separate bridges as 3D building objects from normal surface-mounted buildings in terms of correct referencing onto the terrain

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