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

Last change on this file since 4751 was 4722, checked in by suehring, 4 years ago

Bugfix in building mapping when static driver is available but no actual building is present within the model domain

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