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

Last change on this file since 4653 was 4653, checked in by pavelkrc, 4 years ago

Radiation Transfer Model version 4.0

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