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

Last change on this file since 4122 was 4115, checked in by suehring, 5 years ago

Bugfix in setting flags inidicating wall-bounded grid-points (used for control TKE production near walls); Bugfix in chemistry decycling initialization

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