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

Last change on this file since 4245 was 4245, checked in by pavelkrc, 4 years ago

Merge branch resler into trunk

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