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

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

Minor bugfix in init_grid, exchange of oro_max information only if the array is also allocated

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