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

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

Revision of topography processing to have a consistent treatment of 2D and 3D buildings (init_grid, surface_mod); Bugfix in indoor model in case of non grid-resolved buildings

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