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

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

Fix bad commit

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