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

Last change on this file since 4328 was 4328, checked in by suehring, 5 years ago

Minor change in nzb_max computation; commentation added; minor formatting adjustments in advec_ws

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