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

Last change on this file since 4346 was 4346, checked in by motisi, 4 years ago

Introduction of wall_flags_total_0, which currently sets bits based on static topography information used in wall_flags_static_0

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