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

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

Split loops in advec_ws in order to reduce bit queries; Introduce new parameter to better control order degradation of advection scheme at non-cyclic boundaries; Remove setting of Neumann conditions for horizontal velocity variances; Minor bugfix in divergence correction in advection scheme (only has implications at downward-facing wall surfaces)

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