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

Last change on this file since 4444 was 4444, checked in by raasch, 4 years ago

bugfix: cpp-directives for serial mode added

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