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

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

init_grid: always set bit 5 and 6 of wall_flags, indicating terrain- and building surfaces in all cases, in order to enable terrain-following output also when no land- or urban-surface model is applied

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