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

Last change on this file since 1886 was 1886, checked in by suehring, 8 years ago

Fixed bugs concerning advection flags, variable descriptions added in declaration block

  • Property svn:keywords set to Id
File size: 75.2 KB
RevLine 
[1682]1!> @file init_grid.f90
[1743]2!-------------------------------------------------------------------------------!
[1036]3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the terms
6! of the GNU General Public License as published by the Free Software Foundation,
7! either version 3 of the License, or (at your option) any later version.
8!
9! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
10! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12!
13! You should have received a copy of the GNU General Public License along with
14! PALM. If not, see <http://www.gnu.org/licenses/>.
15!
[1818]16! Copyright 1997-2016 Leibniz Universitaet Hannover
[1743]17!-------------------------------------------------------------------------------!
[1036]18!
[254]19! Current revisions:
[1]20! -----------------
[1886]21! Bugfix: setting advection flags near walls
22! reformulated index values for nzb_v_inner
23! variable discriptions added in declaration block
[1805]24!
[1321]25! Former revisions:
26! -----------------
27! $Id: init_grid.f90 1886 2016-04-21 11:20:47Z suehring $
28!
[1846]29! 1845 2016-04-08 08:29:13Z raasch
30! nzb_2d removed
31!
[1805]32! 1804 2016-04-05 16:30:18Z maronga
33! Removed code for parameter file check (__check)
34!
[1780]35! 1779 2016-03-03 08:01:28Z raasch
36! coupling_char is trimmed at every place it occurs, because it can have
37! different length now
38!
[1763]39! 1762 2016-02-25 12:31:13Z hellstea
40! Introduction of nested domain feature
41!
[1744]42! 1743 2016-01-13 10:23:51Z raasch
43! Bugfix for calculation of nzb_s_outer and nzb_u_outer at north boundary of
44! total domain
45!
[1692]46! 1691 2015-10-26 16:17:44Z maronga
47! Renamed prandtl_layer to constant_flux_layer.
48!
[1683]49! 1682 2015-10-07 23:56:08Z knoop
50! Code annotations made doxygen readable
51!
[1678]52! 1677 2015-10-02 13:25:23Z boeske
53! Bugfix: Ghost points are included in wall_flags_0 and wall_flags_00
54!
[1676]55! 1675 2015-10-02 08:28:59Z gronemeier
56! Bugfix: Definition of topography grid levels
57!
[1662]58! 1660 2015-09-21 08:15:16Z gronemeier
59! Bugfix: Definition of topography grid levels if vertical grid stretching
60!         starts below the maximum topography height.
61!
[1581]62! 1580 2015-04-10 13:43:49Z suehring
63! Bugfix: setting flags for 5th order scheme near buildings
64!
[1576]65! 1575 2015-03-27 09:56:27Z raasch
66! adjustments for psolver-queries
67!
[1558]68! 1557 2015-03-05 16:43:04Z suehring
69! Adjustment for monotoinic limiter
70!
[1419]71! 1418 2014-06-06 13:05:08Z fricke
72! Bugfix: Change if-condition for stretched grid in the ocean, with the old
73!          condition and a negative value for dz_stretch_level the condition
74!          was always true for the whole model domain
75!
[1410]76! 1409 2014-05-23 12:11:32Z suehring
77! Bugfix: set wall_flags_0 at inflow and outflow boundary also for i <= nxlu
78! j <= nysv
79!
[1354]80! 1353 2014-04-08 15:21:23Z heinze
81! REAL constants provided with KIND-attribute
82!
[1323]83! 1322 2014-03-20 16:38:49Z raasch
84! REAL constants defined as wp-kind
85!
[1321]86! 1320 2014-03-20 08:40:49Z raasch
[1320]87! ONLY-attribute added to USE-statements,
88! kind-parameters added to all INTEGER and REAL declaration statements,
89! kinds are defined in new module kinds,
90! revision history before 2012 removed,
91! comment fields (!:) to be used for variable explanations added to
92! all variable declaration statements
[1321]93!
[1222]94! 1221 2013-09-10 08:59:13Z raasch
95! wall_flags_00 introduced to hold bits 32-63,
96! additional 3D-flag arrays for replacing the 2D-index array nzb_s_inner in
97! loops optimized for openACC (pres + flow_statistics)
98!
[1093]99! 1092 2013-02-02 11:24:22Z raasch
100! unused variables removed
101!
[1070]102! 1069 2012-11-28 16:18:43Z maronga
[1779]103! bugfix: added coupling_char to TOPOGRAPHY_DATA to allow topography in the
104!         ocean model in case of coupled runs
[1070]105!
[1037]106! 1036 2012-10-22 13:43:42Z raasch
107! code put under GPL (PALM 3.9)
108!
[1017]109! 1015 2012-09-27 09:23:24Z raasch
110! lower index for calculating wall_flags_0 set to nzb_w_inner instead of
111! nzb_w_inner+1
112!
[997]113! 996 2012-09-07 10:41:47Z raasch
114! little reformatting
115!
[979]116! 978 2012-08-09 08:28:32Z fricke
117! Bugfix: nzb_max is set to nzt at non-cyclic lateral boundaries
118! Bugfix: Set wall_flags_0 for inflow boundary
119!
[928]120! 927 2012-06-06 19:15:04Z raasch
121! Wall flags are not set for multigrid method in case of masking method
122!
[865]123! 864 2012-03-27 15:10:33Z gryschka
[927]124! In case of ocean and Dirichlet bottom bc for u and v dzu_mg and ddzu_pres
125! were not correctly defined for k=1.
[865]126!
[863]127! 861 2012-03-26 14:18:34Z suehring
[861]128! Set wall_flags_0. The array is needed for degradation in ws-scheme near walls,
129! inflow and outflow boundaries as well as near the bottom and the top of the
[863]130! model domain.!
[861]131! Initialization of nzb_s_inner and nzb_w_inner.
132! gls has to be at least nbgp to do not exceed the array bounds of nzb_local
133! while setting wall_flags_0
134!
[844]135! 843 2012-02-29 15:16:21Z gryschka
136! In case of ocean and dirichlet bc for u and v at the bottom
137! the first u-level ist defined at same height as the first w-level
138!
[819]139! 818 2012-02-08 16:11:23Z maronga
140! Bugfix: topo_height is only required if topography is used. It is thus now
141! allocated in the topography branch
142!
[810]143! 809 2012-01-30 13:32:58Z maronga
144! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
145!
[808]146! 807 2012-01-25 11:53:51Z maronga
147! New cpp directive "__check" implemented which is used by check_namelist_files
148!
[1]149! Revision 1.1  1997/08/11 06:17:45  raasch
150! Initial revision (Testversion)
151!
152!
153! Description:
154! ------------
[1682]155!> Creating grid depending constants
[1]156!------------------------------------------------------------------------------!
[1682]157 SUBROUTINE init_grid
158 
[1]159
[1320]160    USE arrays_3d,                                                             &
161        ONLY:  dd2zu, ddzu, ddzu_pres, ddzw, dzu, dzu_mg, dzw, dzw_mg, f1_mg,  &
162               f2_mg, f3_mg, l_grid, l_wall, zu, zw
163       
[1353]164    USE control_parameters,                                                    &
[1320]165        ONLY:  bc_lr, bc_ns, building_height, building_length_x,               &
166               building_length_y, building_wall_left, building_wall_south,     &
167               canyon_height, canyon_wall_left, canyon_wall_south,             &
[1691]168               canyon_width_x, canyon_width_y, constant_flux_layer,            &
169               coupling_char, dp_level_ind_b, dz, dz_max, dz_stretch_factor,   &
170               dz_stretch_level, dz_stretch_level_index, ibc_uv_b, io_blocks,  &
171               io_group, inflow_l, inflow_n, inflow_r, inflow_s,               &
172               masking_method, maximum_grid_level, message_string,             &
[1762]173               momentum_advec, nest_domain, nest_bound_l, nest_bound_n,        &
174               nest_bound_r, nest_bound_s, ocean, outflow_l, outflow_n,        &
175               outflow_r, outflow_s, psolver, scalar_advec, topography,        &
[1691]176               topography_grid_convention, use_surface_fluxes, use_top_fluxes, &
177               wall_adjustment_factor 
[1320]178       
179    USE grid_variables,                                                        &
180        ONLY:  ddx, ddx2, ddx2_mg, ddy, ddy2, ddy2_mg, dx, dx2, dy, dy2, fwxm, &
181               fwxp, fwym, fwyp, fxm, fxp, fym, fyp, wall_e_x, wall_e_y,       &
182               wall_u, wall_v, wall_w_x, wall_w_y, zu_s_inner, zw_w_inner
183       
184    USE indices,                                                               &
185        ONLY:  flags, nbgp, nx, nxl, nxlg, nxlu, nxl_mg, nxr, nxrg, nxr_mg,    &
186               ny, nyn, nyng, nyn_mg, nys, nysv, nys_mg, nysg, nz, nzb,        &
[1845]187               nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer, nzb_diff_u,       &
188               nzb_diff_v, nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,     &
189               nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner,             &
190               nzb_w_outer, nzt, nzt_diff, nzt_mg, rflags_invers,              &
[1320]191               rflags_s_inner, wall_flags_0, wall_flags_00, wall_flags_1,      &
192               wall_flags_10, wall_flags_2, wall_flags_3,  wall_flags_4,       &
193               wall_flags_5, wall_flags_6, wall_flags_7, wall_flags_8,         &
194               wall_flags_9
195   
196    USE kinds
197   
[1]198    USE pegrid
199
200    IMPLICIT NONE
201
[1886]202    INTEGER(iwp) ::  bh      !< temporary vertical index of building height
203    INTEGER(iwp) ::  blx     !< grid point number of building size along x
204    INTEGER(iwp) ::  bly     !< grid point number of building size along y
205    INTEGER(iwp) ::  bxl     !< index for left building wall
206    INTEGER(iwp) ::  bxr     !< index for right building wall
207    INTEGER(iwp) ::  byn     !< index for north building wall
208    INTEGER(iwp) ::  bys     !< index for south building wall
209    INTEGER(iwp) ::  ch      !< temporary vertical index for canyon height
210    INTEGER(iwp) ::  cwx     !< grid point number of canyon size along x
211    INTEGER(iwp) ::  cwy     !< grid point number of canyon size along y
212    INTEGER(iwp) ::  cxl     !< index for left canyon wall
213    INTEGER(iwp) ::  cxr     !< index for right canyon wall
214    INTEGER(iwp) ::  cyn     !< index for north canyon wall
215    INTEGER(iwp) ::  cys     !< index for south canyon wall
216    INTEGER(iwp) ::  gls     !< number of lateral ghost points at total model
217                             !< domain boundaries required for multigrid solver
218    INTEGER(iwp) ::  i       !< index variable along x
219    INTEGER(iwp) ::  ii      !< loop variable for reading topography file
220    INTEGER(iwp) ::  inc     !< incremental parameter for coarsening grid level
221    INTEGER(iwp) ::  j       !< index variable along y
222    INTEGER(iwp) ::  k       !< index variable along z
223    INTEGER(iwp) ::  l       !< loop variable
224    INTEGER(iwp) ::  nxl_l   !< index of left PE boundary for multigrid level
225    INTEGER(iwp) ::  nxr_l   !< index of right PE boundary for multigrid level
226    INTEGER(iwp) ::  nyn_l   !< index of north PE boundary for multigrid level
227    INTEGER(iwp) ::  nys_l   !< index of south PE boundary for multigrid level
228    INTEGER(iwp) ::  nzb_si  !< dummy index for local nzb_s_inner
229    INTEGER(iwp) ::  nzt_l   !< index of top PE boundary for multigrid level
230    INTEGER(iwp) ::  vi      !< dummy for vertical influence
[1]231
[1886]232    INTEGER(iwp), DIMENSION(:), ALLOCATABLE   ::                               &
233                     vertical_influence  !< number of vertical grid points above
234                                         !< obstacle where adjustment of near-
235                                         !< wall mixing length is required
236                                         
237    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_nl  !< index of
238                                         !< north-left corner location to limit
239                                         !< near-wall mixing length
240    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_nr  !< north-right
241    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_sl  !< south-left
242    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_sr  !< south-right
243    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_l     !< distance to
244                                                             !< adjacent left-facing
245                                                             !< wall
246    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_n     !< north-facing
247    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_r     !< right-facing
248    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_s     !< right-facing
249    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local  !< index for topography
250                                                             !< top at cell-center
251    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp    !< dummy to calculate
252                                                             !< topography indices
253                                                             !< on u- and v-grid
[1]254
[1886]255    LOGICAL  :: flag_set = .FALSE.  !< steering variable for advection flags
[1]256
[1886]257    REAL(wp) ::  dx_l          !< grid spacing along x on different multigrid level
258    REAL(wp) ::  dy_l          !< grid spacing along y on different multigrid level
259    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
[861]260
[1886]261    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  topo_height  !< input variable for
262                                                           !< topography height
[1]263
[667]264   
[1]265!
[709]266!-- Calculation of horizontal array bounds including ghost layers
[667]267    nxlg = nxl - nbgp
268    nxrg = nxr + nbgp
269    nysg = nys - nbgp
270    nyng = nyn + nbgp
[709]271
[667]272!
[1]273!-- Allocate grid arrays
[1353]274    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
[667]275              dzw(1:nzt+1), l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1) )
[1]276
277!
278!-- Compute height of u-levels from constant grid length and dz stretch factors
[1353]279    IF ( dz == -1.0_wp )  THEN
[254]280       message_string = 'missing dz'
281       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
[1353]282    ELSEIF ( dz <= 0.0_wp )  THEN
[254]283       WRITE( message_string, * ) 'dz=',dz,' <= 0.0'
284       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
[1]285    ENDIF
[94]286
[1]287!
[94]288!-- Define the vertical grid levels
289    IF ( .NOT. ocean )  THEN
290!
291!--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
[843]292!--    The second u-level (k=1) corresponds to the top of the
[94]293!--    Prandtl-layer.
[667]294
295       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
[1353]296          zu(0) = 0.0_wp
297      !    zu(0) = - dz * 0.5_wp
[667]298       ELSE
[1353]299          zu(0) = - dz * 0.5_wp
[667]300       ENDIF
[1353]301       zu(1) =   dz * 0.5_wp
[1]302
[94]303       dz_stretch_level_index = nzt+1
304       dz_stretched = dz
305       DO  k = 2, nzt+1
306          IF ( dz_stretch_level <= zu(k-1)  .AND.  dz_stretched < dz_max )  THEN
307             dz_stretched = dz_stretched * dz_stretch_factor
308             dz_stretched = MIN( dz_stretched, dz_max )
309             IF ( dz_stretch_level_index == nzt+1 ) dz_stretch_level_index = k-1
310          ENDIF
311          zu(k) = zu(k-1) + dz_stretched
312       ENDDO
[1]313
314!
[94]315!--    Compute the w-levels. They are always staggered half-way between the
[843]316!--    corresponding u-levels. In case of dirichlet bc for u and v at the
317!--    ground the first u- and w-level (k=0) are defined at same height (z=0).
318!--    The top w-level is extrapolated linearly.
[1353]319       zw(0) = 0.0_wp
[94]320       DO  k = 1, nzt
[1353]321          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]322       ENDDO
[1353]323       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
[1]324
[94]325    ELSE
[1]326!
[843]327!--    Grid for ocean with free water surface is at k=nzt (w-grid).
328!--    In case of neumann bc at the ground the first first u-level (k=0) lies
329!--    below the first w-level (k=0). In case of dirichlet bc the first u- and
330!--    w-level are defined at same height, but staggered from the second level.
331!--    The second u-level (k=1) corresponds to the top of the Prandtl-layer.
[1353]332       zu(nzt+1) =   dz * 0.5_wp
333       zu(nzt)   = - dz * 0.5_wp
[94]334
335       dz_stretch_level_index = 0
336       dz_stretched = dz
337       DO  k = nzt-1, 0, -1
[1418]338!
339!--       The default value of dz_stretch_level is positive, thus the first
340!--       condition is always true. Hence, the second condition is necessary.
341          IF ( dz_stretch_level >= zu(k+1)  .AND.  dz_stretch_level <= 0.0  &
342               .AND.  dz_stretched < dz_max )  THEN
[94]343             dz_stretched = dz_stretched * dz_stretch_factor
344             dz_stretched = MIN( dz_stretched, dz_max )
345             IF ( dz_stretch_level_index == 0 ) dz_stretch_level_index = k+1
346          ENDIF
347          zu(k) = zu(k+1) - dz_stretched
348       ENDDO
349
350!
351!--    Compute the w-levels. They are always staggered half-way between the
[843]352!--    corresponding u-levels, except in case of dirichlet bc for u and v
353!--    at the ground. In this case the first u- and w-level are defined at
354!--    same height. The top w-level (nzt+1) is not used but set for
355!--    consistency, since w and all scalar variables are defined up tp nzt+1.
[94]356       zw(nzt+1) = dz
[1353]357       zw(nzt)   = 0.0_wp
[94]358       DO  k = 0, nzt
[1353]359          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]360       ENDDO
361
[843]362!
363!--    In case of dirichlet bc for u and v the first u- and w-level are defined
364!--    at same height.
365       IF ( ibc_uv_b == 0 ) THEN
366          zu(0) = zw(0)
367       ENDIF
368
[94]369    ENDIF
370
371!
[1]372!-- Compute grid lengths.
373    DO  k = 1, nzt+1
374       dzu(k)  = zu(k) - zu(k-1)
[1353]375       ddzu(k) = 1.0_wp / dzu(k)
[1]376       dzw(k)  = zw(k) - zw(k-1)
[1353]377       ddzw(k) = 1.0_wp / dzw(k)
[1]378    ENDDO
379
380    DO  k = 1, nzt
[1353]381       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
[1]382    ENDDO
[667]383   
384!   
[709]385!-- The FFT- SOR-pressure solvers assume grid spacings of a staggered grid
386!-- everywhere. For the actual grid, the grid spacing at the lowest level
387!-- is only dz/2, but should be dz. Therefore, an additional array
388!-- containing with appropriate grid information is created for these
389!-- solvers.
[1575]390    IF ( psolver(1:9) /= 'multigrid' )  THEN
[667]391       ALLOCATE( ddzu_pres(1:nzt+1) )
392       ddzu_pres = ddzu
[864]393       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
[667]394    ENDIF   
[1]395
396!
397!-- In case of multigrid method, compute grid lengths and grid factors for the
398!-- grid levels
[1575]399    IF ( psolver(1:9) == 'multigrid' )  THEN
[1]400
401       ALLOCATE( ddx2_mg(maximum_grid_level), ddy2_mg(maximum_grid_level), &
402                 dzu_mg(nzb+1:nzt+1,maximum_grid_level),                   &
403                 dzw_mg(nzb+1:nzt+1,maximum_grid_level),                   &
404                 f1_mg(nzb+1:nzt,maximum_grid_level),                      &
405                 f2_mg(nzb+1:nzt,maximum_grid_level),                      &
406                 f3_mg(nzb+1:nzt,maximum_grid_level) )
407
408       dzu_mg(:,maximum_grid_level) = dzu
[667]409!       
[864]410!--    Next line to ensure an equally spaced grid.
411       dzu_mg(1,maximum_grid_level) = dzu(2)
[709]412
[1]413       dzw_mg(:,maximum_grid_level) = dzw
414       nzt_l = nzt
415       DO  l = maximum_grid_level-1, 1, -1
[1353]416           dzu_mg(nzb+1,l) = 2.0_wp * dzu_mg(nzb+1,l+1)
417           dzw_mg(nzb+1,l) = 2.0_wp * dzw_mg(nzb+1,l+1)
[1]418           nzt_l = nzt_l / 2
419           DO  k = 2, nzt_l+1
420              dzu_mg(k,l) = dzu_mg(2*k-2,l+1) + dzu_mg(2*k-1,l+1)
421              dzw_mg(k,l) = dzw_mg(2*k-2,l+1) + dzw_mg(2*k-1,l+1)
422           ENDDO
423       ENDDO
424
425       nzt_l = nzt
426       dx_l  = dx
427       dy_l  = dy
428       DO  l = maximum_grid_level, 1, -1
[1353]429          ddx2_mg(l) = 1.0_wp / dx_l**2
430          ddy2_mg(l) = 1.0_wp / dy_l**2
[1]431          DO  k = nzb+1, nzt_l
[1353]432             f2_mg(k,l) = 1.0_wp / ( dzu_mg(k+1,l) * dzw_mg(k,l) )
433             f3_mg(k,l) = 1.0_wp / ( dzu_mg(k,l)   * dzw_mg(k,l) )
434             f1_mg(k,l) = 2.0_wp * ( ddx2_mg(l) + ddy2_mg(l) ) + &
[1]435                          f2_mg(k,l) + f3_mg(k,l)
436          ENDDO
437          nzt_l = nzt_l / 2
[1353]438          dx_l  = dx_l * 2.0_wp
439          dy_l  = dy_l * 2.0_wp
[1]440       ENDDO
441
442    ENDIF
443
444!
445!-- Compute the reciprocal values of the horizontal grid lengths.
[1353]446    ddx = 1.0_wp / dx
447    ddy = 1.0_wp / dy
[1]448    dx2 = dx * dx
449    dy2 = dy * dy
[1353]450    ddx2 = 1.0_wp / dx2
451    ddy2 = 1.0_wp / dy2
[1]452
453!
454!-- Compute the grid-dependent mixing length.
455    DO  k = 1, nzt
[1322]456       l_grid(k)  = ( dx * dy * dzw(k) )**0.33333333333333_wp
[1]457    ENDDO
458
459!
460!-- Allocate outer and inner index arrays for topography and set
[114]461!-- defaults.
462!-- nzb_local has to contain additional layers of ghost points for calculating
463!-- the flag arrays needed for the multigrid method
464    gls = 2**( maximum_grid_level )
[861]465    IF ( gls < nbgp )  gls = nbgp
[667]466
[114]467    ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr),       &
468              corner_sl(nys:nyn,nxl:nxr), corner_sr(nys:nyn,nxl:nxr),       &
[667]469              nzb_local(-gls:ny+gls,-gls:nx+gls),                                   &
470              nzb_tmp(-nbgp:ny+nbgp,-nbgp:nx+nbgp),                         &
[114]471              wall_l(nys:nyn,nxl:nxr), wall_n(nys:nyn,nxl:nxr),             &
[1]472              wall_r(nys:nyn,nxl:nxr), wall_s(nys:nyn,nxl:nxr) )
[667]473    ALLOCATE( fwxm(nysg:nyng,nxlg:nxrg), fwxp(nysg:nyng,nxlg:nxrg),         &
474              fwym(nysg:nyng,nxlg:nxrg), fwyp(nysg:nyng,nxlg:nxrg),         &
475              fxm(nysg:nyng,nxlg:nxrg), fxp(nysg:nyng,nxlg:nxrg),           &
476              fym(nysg:nyng,nxlg:nxrg), fyp(nysg:nyng,nxlg:nxrg),           &
477              nzb_s_inner(nysg:nyng,nxlg:nxrg),                             &
478              nzb_s_outer(nysg:nyng,nxlg:nxrg),                             &
479              nzb_u_inner(nysg:nyng,nxlg:nxrg),                             &
480              nzb_u_outer(nysg:nyng,nxlg:nxrg),                             &
481              nzb_v_inner(nysg:nyng,nxlg:nxrg),                             &
482              nzb_v_outer(nysg:nyng,nxlg:nxrg),                             &
483              nzb_w_inner(nysg:nyng,nxlg:nxrg),                             &
484              nzb_w_outer(nysg:nyng,nxlg:nxrg),                             &
485              nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                        &
486              nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                        &
487              nzb_diff_u(nysg:nyng,nxlg:nxrg),                              &
488              nzb_diff_v(nysg:nyng,nxlg:nxrg),                              &
[1221]489              rflags_s_inner(nzb:nzt+2,nysg:nyng,nxlg:nxrg),                &
490              rflags_invers(nysg:nyng,nxlg:nxrg,nzb:nzt+2),                 &
[667]491              wall_e_x(nysg:nyng,nxlg:nxrg),                                &
492              wall_e_y(nysg:nyng,nxlg:nxrg),                                &
493              wall_u(nysg:nyng,nxlg:nxrg),                                  &
494              wall_v(nysg:nyng,nxlg:nxrg),                                  &
495              wall_w_x(nysg:nyng,nxlg:nxrg),                                &
496              wall_w_y(nysg:nyng,nxlg:nxrg) )
[1]497
498
[667]499
500    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
501
[818]502
[1]503    nzb_s_inner = nzb;  nzb_s_outer = nzb
504    nzb_u_inner = nzb;  nzb_u_outer = nzb
505    nzb_v_inner = nzb;  nzb_v_outer = nzb
506    nzb_w_inner = nzb;  nzb_w_outer = nzb
507
[1353]508    rflags_s_inner = 1.0_wp
509    rflags_invers  = 1.0_wp
[1221]510
[1]511!
[19]512!-- Define vertical gridpoint from (or to) which on the usual finite difference
[1]513!-- form (which does not use surface fluxes) is applied
[1691]514    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
[1]515       nzb_diff = nzb + 2
516    ELSE
517       nzb_diff = nzb + 1
518    ENDIF
[19]519    IF ( use_top_fluxes )  THEN
520       nzt_diff = nzt - 1
521    ELSE
522       nzt_diff = nzt
523    ENDIF
[1]524
525    nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
526    nzb_diff_u = nzb_diff;  nzb_diff_v = nzb_diff
527
[1353]528    wall_e_x = 0.0_wp;  wall_e_y = 0.0_wp;  wall_u = 0.0_wp;  wall_v = 0.0_wp
529    wall_w_x = 0.0_wp;  wall_w_y = 0.0_wp
530    fwxp = 1.0_wp;  fwxm = 1.0_wp;  fwyp = 1.0_wp;  fwym = 1.0_wp
531    fxp  = 1.0_wp;  fxm  = 1.0_wp;  fyp  = 1.0_wp;  fym  = 1.0_wp
[1]532
533!
534!-- Initialize near-wall mixing length l_wall only in the vertical direction
535!-- for the moment,
536!-- multiplication with wall_adjustment_factor near the end of this routine
537    l_wall(nzb,:,:)   = l_grid(1)
538    DO  k = nzb+1, nzt
539       l_wall(k,:,:)  = l_grid(k)
540    ENDDO
541    l_wall(nzt+1,:,:) = l_grid(nzt)
542
543    ALLOCATE ( vertical_influence(nzb:nzt) )
544    DO  k = 1, nzt
545       vertical_influence(k) = MIN ( INT( l_grid(k) / &
[1353]546                     ( wall_adjustment_factor * dzw(k) ) + 0.5_wp ), nzt - k )
[1]547    ENDDO
548
549    DO  k = 1, MAXVAL( nzb_s_inner )
[1353]550       IF ( l_grid(k) > 1.5_wp * dx * wall_adjustment_factor .OR.  &
551            l_grid(k) > 1.5_wp * dy * wall_adjustment_factor )  THEN
[254]552          WRITE( message_string, * ) 'grid anisotropy exceeds ', &
553                                     'threshold given by only local', &
554                                     ' &horizontal reduction of near_wall ', &
555                                     'mixing length l_wall', &
556                                     ' &starting from height level k = ', k, '.'
557          CALL message( 'init_grid', 'PA0202', 0, 1, 0, 6, 0 )
[1]558          EXIT
559       ENDIF
560    ENDDO
561    vertical_influence(0) = vertical_influence(1)
562
[667]563    DO  i = nxlg, nxrg
564       DO  j = nysg, nyng
[1]565          DO  k = nzb_s_inner(j,i) + 1, &
566                  nzb_s_inner(j,i) + vertical_influence(nzb_s_inner(j,i))
567             l_wall(k,j,i) = zu(k) - zw(nzb_s_inner(j,i))
568          ENDDO
569       ENDDO
570    ENDDO
571
572!
573!-- Set outer and inner index arrays for non-flat topography.
574!-- Here consistency checks concerning domain size and periodicity are
575!-- necessary.
576!-- Within this SELECT CASE structure only nzb_local is initialized
577!-- individually depending on the chosen topography type, all other index
578!-- arrays are initialized further below.
579    SELECT CASE ( TRIM( topography ) )
580
581       CASE ( 'flat' )
582!
[555]583!--       nzb_local is required for the multigrid solver
584          nzb_local = 0
[1]585
586       CASE ( 'single_building' )
587!
588!--       Single rectangular building, by default centered in the middle of the
589!--       total domain
590          blx = NINT( building_length_x / dx )
591          bly = NINT( building_length_y / dy )
[1675]592          bh  = MINLOC( ABS( zw - building_height ), 1 ) - 1
593          IF ( ABS( zw(bh  ) - building_height ) == &
594               ABS( zw(bh+1) - building_height )    )  bh = bh + 1
[1]595
[1322]596          IF ( building_wall_left == 9999999.9_wp )  THEN
[1]597             building_wall_left = ( nx + 1 - blx ) / 2 * dx
598          ENDIF
599          bxl = NINT( building_wall_left / dx )
600          bxr = bxl + blx
601
[1322]602          IF ( building_wall_south == 9999999.9_wp )  THEN
[1]603             building_wall_south = ( ny + 1 - bly ) / 2 * dy
604          ENDIF
605          bys = NINT( building_wall_south / dy )
606          byn = bys + bly
607
608!
609!--       Building size has to meet some requirements
610          IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR.  &
611               ( bys < 1 ) .OR. ( byn > ny-1 ) .OR. ( byn < bys+3 ) )  THEN
[274]612             WRITE( message_string, * ) 'inconsistent building parameters:',   &
613                                      '& bxl=', bxl, 'bxr=', bxr, 'bys=', bys, &
614                                      'byn=', byn, 'nx=', nx, 'ny=', ny
[254]615             CALL message( 'init_grid', 'PA0203', 1, 2, 0, 6, 0 )
[1]616          ENDIF
617
618!
[217]619!--       Define the building.
[1]620          nzb_local = 0
[134]621          nzb_local(bys:byn,bxl:bxr) = bh
[1]622
[240]623       CASE ( 'single_street_canyon' )
624!
625!--       Single quasi-2D street canyon of infinite length in x or y direction.
626!--       The canyon is centered in the other direction by default.
[1322]627          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[240]628!
629!--          Street canyon in y direction
630             cwx = NINT( canyon_width_x / dx )
[1322]631             IF ( canyon_wall_left == 9999999.9_wp )  THEN
[240]632                canyon_wall_left = ( nx + 1 - cwx ) / 2 * dx
633             ENDIF
634             cxl = NINT( canyon_wall_left / dx )
635             cxr = cxl + cwx
636
[1322]637          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[240]638!
639!--          Street canyon in x direction
640             cwy = NINT( canyon_width_y / dy )
[1322]641             IF ( canyon_wall_south == 9999999.9_wp )  THEN
[240]642                canyon_wall_south = ( ny + 1 - cwy ) / 2 * dy
643             ENDIF
644             cys = NINT( canyon_wall_south / dy )
645             cyn = cys + cwy
646
647          ELSE
[254]648             
649             message_string = 'no street canyon width given'
650             CALL message( 'init_grid', 'PA0204', 1, 2, 0, 6, 0 )
651 
[240]652          ENDIF
653
[1675]654          ch  = MINLOC( ABS( zw - canyon_height ), 1 ) - 1
655          IF ( ABS( zw(ch  ) - canyon_height ) == &
656               ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
657
[240]658          dp_level_ind_b = ch
659!
660!--       Street canyon size has to meet some requirements
[1322]661          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[1353]662             IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR.        &
[240]663               ( ch < 3 ) )  THEN
[1353]664                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
665                                           '&cxl=', cxl, 'cxr=', cxr,          &
666                                           'cwx=', cwx,                        &
[254]667                                           'ch=', ch, 'nx=', nx, 'ny=', ny
668                CALL message( 'init_grid', 'PA0205', 1, 2, 0, 6, 0 ) 
[240]669             ENDIF
[1322]670          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[1353]671             IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR.        &
[240]672               ( ch < 3 ) )  THEN
[1353]673                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
674                                           '&cys=', cys, 'cyn=', cyn,          &
675                                           'cwy=', cwy,                        &
[254]676                                           'ch=', ch, 'nx=', nx, 'ny=', ny
677                CALL message( 'init_grid', 'PA0206', 1, 2, 0, 6, 0 ) 
[240]678             ENDIF
679          ENDIF
[1353]680          IF ( canyon_width_x /= 9999999.9_wp .AND.                            &                 
681               canyon_width_y /= 9999999.9_wp )  THEN
682             message_string = 'inconsistent canyon parameters:' //             &   
683                              '&street canyon can only be oriented' //         &
[254]684                              '&either in x- or in y-direction'
685             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
[240]686          ENDIF
687
688          nzb_local = ch
[1322]689          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[240]690             nzb_local(:,cxl+1:cxr-1) = 0
[1322]691          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[240]692             nzb_local(cys+1:cyn-1,:) = 0
693          ENDIF
694
[1]695       CASE ( 'read_from_file' )
[759]696
[818]697          ALLOCATE ( topo_height(0:ny,0:nx) )
698
[759]699          DO  ii = 0, io_blocks-1
700             IF ( ii == io_group )  THEN
701
[1]702!
[759]703!--             Arbitrary irregular topography data in PALM format (exactly
704!--             matching the grid size and total domain size)
[1779]705                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
706                          STATUS='OLD', FORM='FORMATTED', ERR=10 )
[759]707                DO  j = ny, 0, -1
708                   READ( 90, *, ERR=11, END=11 )  ( topo_height(j,i), i = 0,nx )
709                ENDDO
710
711                GOTO 12
712         
[1779]713 10             message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )//    &
714                                 ' does not exist'
[759]715                CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
716
[1779]717 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
718                                 TRIM( coupling_char )
[759]719                CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
720
721 12             CLOSE( 90 )
722
723             ENDIF
[1804]724#if defined( __parallel )
[759]725             CALL MPI_BARRIER( comm2d, ierr )
726#endif
[559]727          ENDDO
[759]728
[1]729!
730!--       Calculate the index height of the topography
731          DO  i = 0, nx
732             DO  j = 0, ny
[1675]733                nzb_local(j,i) = MINLOC( ABS( zw - topo_height(j,i) ), 1 ) - 1
734                IF ( ABS( zw(nzb_local(j,i)  ) - topo_height(j,i) ) == &
735                     ABS( zw(nzb_local(j,i)+1) - topo_height(j,i) )    )  &
736                   nzb_local(j,i) = nzb_local(j,i) + 1
[1]737             ENDDO
738          ENDDO
[818]739
740          DEALLOCATE ( topo_height )
[114]741!
[759]742!--       Add cyclic boundaries (additional layers are for calculating
743!--       flag arrays needed for the multigrid sover)
[114]744          nzb_local(-gls:-1,0:nx)     = nzb_local(ny-gls+1:ny,0:nx)
745          nzb_local(ny+1:ny+gls,0:nx) = nzb_local(0:gls-1,0:nx)
746          nzb_local(:,-gls:-1)        = nzb_local(:,nx-gls+1:nx)
747          nzb_local(:,nx+1:nx+gls)    = nzb_local(:,0:gls-1)
[667]748
[1]749       CASE DEFAULT
750!
751!--       The DEFAULT case is reached either if the parameter topography
[217]752!--       contains a wrong character string or if the user has defined a special
[1]753!--       case in the user interface. There, the subroutine user_init_grid
754!--       checks which of these two conditions applies.
[114]755          CALL user_init_grid( gls, nzb_local )
[1]756
757    END SELECT
758!
[861]759!-- Determine the maximum level of topography. Furthermore it is used for
760!-- steering the degradation of order of the applied advection scheme.
[978]761!-- In case of non-cyclic lateral boundaries, the order of the advection
[996]762!-- scheme have to be reduced up to nzt (required at the lateral boundaries).
[1677]763    nzb_max = MAXVAL( nzb_local ) + 1
[1353]764    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
[1762]765         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
766         nest_domain )                                                         &
767    THEN
768       nzb_max = nzt
[978]769    ENDIF
770
[861]771!
[1]772!-- Consistency checks and index array initialization are only required for
[217]773!-- non-flat topography, also the initialization of topography height arrays
[49]774!-- zu_s_inner and zw_w_inner
[1]775    IF ( TRIM( topography ) /= 'flat' )  THEN
776
777!
778!--    Consistency checks
779       IF ( MINVAL( nzb_local ) < 0  .OR.  MAXVAL( nzb_local ) > nz + 1 )  THEN
[1353]780          WRITE( message_string, * ) 'nzb_local values are outside the',       &
781                                'model domain',                                &
782                                '&MINVAL( nzb_local ) = ', MINVAL(nzb_local),  &
[274]783                                '&MAXVAL( nzb_local ) = ', MAXVAL(nzb_local)
[254]784          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
[1]785       ENDIF
786
[722]787       IF ( bc_lr == 'cyclic' )  THEN
[1353]788          IF ( ANY( nzb_local(:,-1) /= nzb_local(:,nx)   )  .OR.               &
[1]789               ANY( nzb_local(:,0)  /= nzb_local(:,nx+1) ) )  THEN
[1353]790             message_string = 'nzb_local does not fulfill cyclic' //           &
[254]791                              ' boundary condition in x-direction'
792             CALL message( 'init_grid', 'PA0211', 1, 2, 0, 6, 0 )
[1]793          ENDIF
794       ENDIF
[722]795       IF ( bc_ns == 'cyclic' )  THEN
[1353]796          IF ( ANY( nzb_local(-1,:) /= nzb_local(ny,:)   )  .OR.               &
[1]797               ANY( nzb_local(0,:)  /= nzb_local(ny+1,:) ) )  THEN
[1353]798             message_string = 'nzb_local does not fulfill cyclic' //           &
[254]799                              ' boundary condition in y-direction'
800             CALL message( 'init_grid', 'PA0212', 1, 2, 0, 6, 0 )
[1]801          ENDIF
802       ENDIF
803
[217]804       IF ( topography_grid_convention == 'cell_edge' )  THEN
[134]805!
[217]806!--       The array nzb_local as defined using the 'cell_edge' convention
807!--       describes the actual total size of topography which is defined at the
808!--       cell edges where u=0 on the topography walls in x-direction and v=0
809!--       on the topography walls in y-direction. However, PALM uses individual
810!--       arrays nzb_u|v|w|s_inner|outer that are based on nzb_s_inner.
811!--       Therefore, the extent of topography in nzb_local is now reduced by
812!--       1dx at the E topography walls and by 1dy at the N topography walls
813!--       to form the basis for nzb_s_inner.
814          DO  j = -gls, ny + gls
815             DO  i = -gls, nx
816                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j,i+1) )
817             ENDDO
[134]818          ENDDO
[217]819!--       apply cyclic boundary conditions in x-direction
820!(ist das erforderlich? Ursache von Seung Bus Fehler?)
821          nzb_local(:,nx+1:nx+gls) = nzb_local(:,0:gls-1)
822          DO  i = -gls, nx + gls
823             DO  j = -gls, ny
824                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) )
825             ENDDO
[134]826          ENDDO
[217]827!--       apply cyclic boundary conditions in y-direction
828!(ist das erforderlich? Ursache von Seung Bus Fehler?)
829          nzb_local(ny+1:ny+gls,:) = nzb_local(0:gls-1,:)
830       ENDIF
[134]831
[1]832!
833!--    Initialize index arrays nzb_s_inner and nzb_w_inner
[861]834       nzb_s_inner = nzb_local(nysg:nyng,nxlg:nxrg)
835       nzb_w_inner = nzb_local(nysg:nyng,nxlg:nxrg)
[1]836
837!
838!--    Initialize remaining index arrays:
839!--    first pre-initialize them with nzb_s_inner...
840       nzb_u_inner = nzb_s_inner
841       nzb_u_outer = nzb_s_inner
842       nzb_v_inner = nzb_s_inner
843       nzb_v_outer = nzb_s_inner
844       nzb_w_outer = nzb_s_inner
845       nzb_s_outer = nzb_s_inner
846
847!
848!--    ...then extend pre-initialized arrays in their according directions
849!--    based on nzb_local using nzb_tmp as a temporary global index array
850
851!
852!--    nzb_s_outer:
853!--    extend nzb_local east-/westwards first, then north-/southwards
[667]854       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
[1]855       DO  j = -1, ny + 1
856          DO  i = 0, nx
[1353]857             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
[1]858                                 nzb_local(j,i+1) )
859          ENDDO
860       ENDDO
861       DO  i = nxl, nxr
862          DO  j = nys, nyn
[1353]863             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
[1]864                                     nzb_tmp(j+1,i) )
865          ENDDO
866!
867!--       non-cyclic boundary conditions (overwritten by call of
868!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
869          IF ( nys == 0 )  THEN
870             j = -1
871             nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
872          ENDIF
[1743]873          IF ( nyn == ny )  THEN
[1]874             j = ny + 1
875             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
876          ENDIF
877       ENDDO
878!
879!--    nzb_w_outer:
880!--    identical to nzb_s_outer
881       nzb_w_outer = nzb_s_outer
882
883!
884!--    nzb_u_inner:
885!--    extend nzb_local rightwards only
[667]886       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
[1]887       DO  j = -1, ny + 1
888          DO  i = 0, nx + 1
889             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
890          ENDDO
891       ENDDO
[667]892       nzb_u_inner = nzb_tmp(nysg:nyng,nxlg:nxrg)
[1]893
894!
895!--    nzb_u_outer:
896!--    extend current nzb_tmp (nzb_u_inner) north-/southwards
897       DO  i = nxl, nxr
898          DO  j = nys, nyn
[1353]899             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
[1]900                                     nzb_tmp(j+1,i) )
901          ENDDO
902!
903!--       non-cyclic boundary conditions (overwritten by call of
904!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
905          IF ( nys == 0 )  THEN
906             j = -1
907             nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
908          ENDIF
[1743]909          IF ( nyn == ny )  THEN
[1]910             j = ny + 1
911             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
912          ENDIF
913       ENDDO
914
915!
916!--    nzb_v_inner:
917!--    extend nzb_local northwards only
[667]918       nzb_tmp = nzb_local(-nbgp:ny+nbgp,-nbgp:nx+nbgp)
[1]919       DO  i = -1, nx + 1
920          DO  j = 0, ny + 1
921             nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
922          ENDDO
923       ENDDO
[1886]924       nzb_v_inner = nzb_tmp(nysg:nyng,nxlg:nxrg)
[1]925
926!
927!--    nzb_v_outer:
928!--    extend current nzb_tmp (nzb_v_inner) right-/leftwards
929       DO  j = nys, nyn
930          DO  i = nxl, nxr
[1353]931             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),             &
[1]932                                     nzb_tmp(j,i+1) )
933          ENDDO
934!
935!--       non-cyclic boundary conditions (overwritten by call of
936!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
937          IF ( nxl == 0 )  THEN
938             i = -1
939             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) )
940          ENDIF
941          IF ( nxr == nx )  THEN
942             i = nx + 1
943             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) )
944          ENDIF
945       ENDDO
[1804]946
[1]947!
948!--    Exchange of lateral boundary values (parallel computers) and cyclic
949!--    boundary conditions, if applicable.
950!--    Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local
951!--    they do not require exchange and are not included here.
952       CALL exchange_horiz_2d_int( nzb_u_inner )
953       CALL exchange_horiz_2d_int( nzb_u_outer )
954       CALL exchange_horiz_2d_int( nzb_v_inner )
955       CALL exchange_horiz_2d_int( nzb_v_outer )
956       CALL exchange_horiz_2d_int( nzb_w_outer )
957       CALL exchange_horiz_2d_int( nzb_s_outer )
958
[49]959!
960!--    Allocate and set the arrays containing the topography height
961       IF ( myid == 0 )  THEN
962
963          ALLOCATE( zu_s_inner(0:nx+1,0:ny+1), zw_w_inner(0:nx+1,0:ny+1) )
964
965          DO  i = 0, nx + 1
966             DO  j = 0, ny + 1
967                zu_s_inner(i,j) = zu(nzb_local(j,i))
968                zw_w_inner(i,j) = zw(nzb_local(j,i))
969             ENDDO
970          ENDDO
971         
972       ENDIF
[1221]973!
974!--    Set flag arrays to be used for masking of grid points
975       DO  i = nxlg, nxrg
976          DO  j = nysg, nyng
977             DO  k = nzb, nzt+1
[1353]978                IF ( k <= nzb_s_inner(j,i) )  rflags_s_inner(k,j,i) = 0.0_wp
979                IF ( k <= nzb_s_inner(j,i) )  rflags_invers(j,i,k)  = 0.0_wp
[1221]980             ENDDO
981          ENDDO
982       ENDDO
[1804]983
[1]984    ENDIF
985
986!
987!-- Set the individual index arrays which define the k index from which on
988!-- the usual finite difference form (which does not use surface fluxes) is
989!-- applied
[1691]990    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
[1]991       nzb_diff_u         = nzb_u_inner + 2
992       nzb_diff_v         = nzb_v_inner + 2
993       nzb_diff_s_inner   = nzb_s_inner + 2
994       nzb_diff_s_outer   = nzb_s_outer + 2
995    ELSE
996       nzb_diff_u         = nzb_u_inner + 1
997       nzb_diff_v         = nzb_v_inner + 1
998       nzb_diff_s_inner   = nzb_s_inner + 1
999       nzb_diff_s_outer   = nzb_s_outer + 1
1000    ENDIF
1001
1002!
1003!-- Calculation of wall switches and factors required by diffusion_u/v.f90 and
1004!-- for limitation of near-wall mixing length l_wall further below
1005    corner_nl = 0
1006    corner_nr = 0
1007    corner_sl = 0
1008    corner_sr = 0
1009    wall_l    = 0
1010    wall_n    = 0
1011    wall_r    = 0
1012    wall_s    = 0
1013
1014    DO  i = nxl, nxr
1015       DO  j = nys, nyn
1016!
1017!--       u-component
1018          IF ( nzb_u_outer(j,i) > nzb_u_outer(j+1,i) )  THEN
[1353]1019             wall_u(j,i) = 1.0_wp   ! north wall (location of adjacent fluid)
1020             fym(j,i)    = 0.0_wp
1021             fyp(j,i)    = 1.0_wp
[1]1022          ELSEIF ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) )  THEN
[1353]1023             wall_u(j,i) = 1.0_wp   ! south wall (location of adjacent fluid)
1024             fym(j,i)    = 1.0_wp
1025             fyp(j,i)    = 0.0_wp
[1]1026          ENDIF
1027!
1028!--       v-component
1029          IF ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) )  THEN
[1353]1030             wall_v(j,i) = 1.0_wp   ! rigth wall (location of adjacent fluid)
1031             fxm(j,i)    = 0.0_wp
1032             fxp(j,i)    = 1.0_wp
[1]1033          ELSEIF ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) )  THEN
[1353]1034             wall_v(j,i) = 1.0_wp   ! left wall (location of adjacent fluid)
1035             fxm(j,i)    = 1.0_wp
1036             fxp(j,i)    = 0.0_wp
[1]1037          ENDIF
1038!
1039!--       w-component, also used for scalars, separate arrays for shear
1040!--       production of tke
1041          IF ( nzb_w_outer(j,i) > nzb_w_outer(j+1,i) )  THEN
[1353]1042             wall_e_y(j,i) =  1.0_wp   ! north wall (location of adjacent fluid)
1043             wall_w_y(j,i) =  1.0_wp
1044             fwym(j,i)     =  0.0_wp
1045             fwyp(j,i)     =  1.0_wp
[1]1046          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) )  THEN
[1353]1047             wall_e_y(j,i) = -1.0_wp   ! south wall (location of adjacent fluid)
1048             wall_w_y(j,i) =  1.0_wp
1049             fwym(j,i)     =  1.0_wp
1050             fwyp(j,i)     =  0.0_wp
[1]1051          ENDIF
1052          IF ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) )  THEN
[1353]1053             wall_e_x(j,i) =  1.0_wp   ! right wall (location of adjacent fluid)
1054             wall_w_x(j,i) =  1.0_wp
1055             fwxm(j,i)     =  0.0_wp
1056             fwxp(j,i)     =  1.0_wp
[1]1057          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) )  THEN
[1353]1058             wall_e_x(j,i) = -1.0_wp   ! left wall (location of adjacent fluid)
1059             wall_w_x(j,i) =  1.0_wp
1060             fwxm(j,i)     =  1.0_wp
1061             fwxp(j,i)     =  0.0_wp
[1]1062          ENDIF
1063!
1064!--       Wall and corner locations inside buildings for limitation of
1065!--       near-wall mixing length l_wall
1066          IF ( nzb_s_inner(j,i) > nzb_s_inner(j+1,i) )  THEN
1067
1068             wall_n(j,i) = nzb_s_inner(j+1,i) + 1            ! North wall
1069
1070             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
1071                corner_nl(j,i) = MAX( nzb_s_inner(j+1,i),  & ! Northleft corner
1072                                      nzb_s_inner(j,i-1) ) + 1
1073             ENDIF
1074
1075             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
1076                corner_nr(j,i) = MAX( nzb_s_inner(j+1,i),  & ! Northright corner
1077                                      nzb_s_inner(j,i+1) ) + 1
1078             ENDIF
1079
1080          ENDIF
1081
1082          IF ( nzb_s_inner(j,i) > nzb_s_inner(j-1,i) )  THEN
1083
1084             wall_s(j,i) = nzb_s_inner(j-1,i) + 1            ! South wall
1085             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
1086                corner_sl(j,i) = MAX( nzb_s_inner(j-1,i),  & ! Southleft corner
1087                                      nzb_s_inner(j,i-1) ) + 1
1088             ENDIF
1089
1090             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
1091                corner_sr(j,i) = MAX( nzb_s_inner(j-1,i),  & ! Southright corner
1092                                      nzb_s_inner(j,i+1) ) + 1
1093             ENDIF
1094
1095          ENDIF
1096
1097          IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
1098             wall_l(j,i) = nzb_s_inner(j,i-1) + 1            ! Left wall
1099          ENDIF
1100
1101          IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
1102             wall_r(j,i) = nzb_s_inner(j,i+1) + 1            ! Right wall
1103          ENDIF
1104
1105       ENDDO
1106    ENDDO
1107
1108!
[114]1109!-- Calculate wall flag arrays for the multigrid method
[1575]1110    IF ( psolver(1:9) == 'multigrid' )  THEN
[114]1111!
1112!--    Gridpoint increment of the current level
1113       inc = 1
1114
1115       DO  l = maximum_grid_level, 1 , -1
1116
1117          nxl_l = nxl_mg(l)
1118          nxr_l = nxr_mg(l)
1119          nys_l = nys_mg(l)
1120          nyn_l = nyn_mg(l)
1121          nzt_l = nzt_mg(l)
1122
1123!
1124!--       Assign the flag level to be calculated
1125          SELECT CASE ( l )
1126             CASE ( 1 )
1127                flags => wall_flags_1
1128             CASE ( 2 )
1129                flags => wall_flags_2
1130             CASE ( 3 )
1131                flags => wall_flags_3
1132             CASE ( 4 )
1133                flags => wall_flags_4
1134             CASE ( 5 )
1135                flags => wall_flags_5
1136             CASE ( 6 )
1137                flags => wall_flags_6
1138             CASE ( 7 )
1139                flags => wall_flags_7
1140             CASE ( 8 )
1141                flags => wall_flags_8
1142             CASE ( 9 )
1143                flags => wall_flags_9
1144             CASE ( 10 )
1145                flags => wall_flags_10
1146          END SELECT
1147
1148!
1149!--       Depending on the grid level, set the respective bits in case of
1150!--       neighbouring walls
1151!--       Bit 0:  wall to the bottom
1152!--       Bit 1:  wall to the top (not realized in remaining PALM code so far)
1153!--       Bit 2:  wall to the south
1154!--       Bit 3:  wall to the north
1155!--       Bit 4:  wall to the left
1156!--       Bit 5:  wall to the right
[116]1157!--       Bit 6:  inside building
[114]1158
1159          flags = 0
1160
[927]1161!
1162!--       In case of masking method, flags are not set and multigrid method
1163!--       works like FFT-solver
1164          IF ( .NOT. masking_method )  THEN
1165
1166             DO  i = nxl_l-1, nxr_l+1
1167                DO  j = nys_l-1, nyn_l+1
1168                   DO  k = nzb, nzt_l+1
[114]1169                         
1170!
[927]1171!--                   Inside/outside building (inside building does not need
1172!--                   further tests for walls)
1173                      IF ( k*inc <= nzb_local(j*inc,i*inc) )  THEN
[114]1174
[927]1175                         flags(k,j,i) = IBSET( flags(k,j,i), 6 )
[114]1176
[927]1177                      ELSE
[114]1178!
[927]1179!--                      Bottom wall
1180                         IF ( (k-1)*inc <= nzb_local(j*inc,i*inc) )  THEN
1181                            flags(k,j,i) = IBSET( flags(k,j,i), 0 )
1182                         ENDIF
[114]1183!
[927]1184!--                      South wall
1185                         IF ( k*inc <= nzb_local((j-1)*inc,i*inc) )  THEN
1186                            flags(k,j,i) = IBSET( flags(k,j,i), 2 )
1187                         ENDIF
[114]1188!
[927]1189!--                      North wall
1190                         IF ( k*inc <= nzb_local((j+1)*inc,i*inc) )  THEN
1191                            flags(k,j,i) = IBSET( flags(k,j,i), 3 )
1192                         ENDIF
[114]1193!
[927]1194!--                      Left wall
1195                         IF ( k*inc <= nzb_local(j*inc,(i-1)*inc) )  THEN
1196                            flags(k,j,i) = IBSET( flags(k,j,i), 4 )
1197                         ENDIF
[114]1198!
[927]1199!--                      Right wall
1200                         IF ( k*inc <= nzb_local(j*inc,(i+1)*inc) )  THEN
1201                            flags(k,j,i) = IBSET( flags(k,j,i), 5 )
1202                         ENDIF
1203
[114]1204                      ENDIF
1205                           
[927]1206                   ENDDO
[114]1207                ENDDO
1208             ENDDO
1209
[927]1210          ENDIF
1211
[114]1212!
1213!--       Test output of flag arrays
[145]1214!          i = nxl_l
1215!          WRITE (9,*)  ' '
1216!          WRITE (9,*)  '*** mg level ', l, ' ***', mg_switch_to_pe0_level
1217!          WRITE (9,*)  '    inc=', inc, '  i =', nxl_l
1218!          WRITE (9,*)  '    nxl_l',nxl_l,' nxr_l=',nxr_l,' nys_l=',nys_l,' nyn_l=',nyn_l
1219!          DO  k = nzt_l+1, nzb, -1
1220!             WRITE (9,'(194(1X,I2))')  ( flags(k,j,i), j = nys_l-1, nyn_l+1 )
1221!          ENDDO
[114]1222
1223          inc = inc * 2
1224
1225       ENDDO
1226
1227    ENDIF
[861]1228!
1229!-- Allocate flags needed for masking walls.
[1677]1230    ALLOCATE( wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
1231              wall_flags_00(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
[1221]1232    wall_flags_0  = 0
1233    wall_flags_00 = 0
[114]1234
[1886]1235    IF ( scalar_advec == 'ws-scheme' .OR.                                      &
[1557]1236         scalar_advec == 'ws-scheme-mono' )  THEN
[114]1237!
[861]1238!--    Set flags to steer the degradation of the advection scheme in advec_ws
1239!--    near topography, inflow- and outflow boundaries as well as bottom and
1240!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
1241!--    grid points.
1242       DO  i = nxl, nxr
1243          DO  j = nys, nyn
1244             DO  k = nzb_s_inner(j,i)+1, nzt
1245!
1246!--             scalar - x-direction
1247!--             WS1 (0), WS3 (1), WS5 (2)
[1886]1248                IF ( ( k <= nzb_s_inner(j,i+1) .OR. k <= nzb_s_inner(j,i+2)    &   
1249                  .OR. k <= nzb_s_inner(j,i-1) )                               &
1250                    .OR. ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
[1762]1251                           .AND. i == nxl   )    .OR.                          &
1252                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1253                           .AND. i == nxr   ) )                                &
1254                THEN
[861]1255                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
[1886]1256                ELSEIF ( ( k <= nzb_s_inner(j,i+3) .AND. k > nzb_s_inner(j,i+1)&
1257                                                   .AND. k > nzb_s_inner(j,i+2)&
1258                                                   .AND. k > nzb_s_inner(j,i-1)&
1259                         )                       .OR.                          &
1260                         ( k <= nzb_s_inner(j,i-2) .AND. k > nzb_s_inner(j,i+1)&
1261                                                   .AND. k > nzb_s_inner(j,i+2)&
1262                                                   .AND. k > nzb_s_inner(j,i-1)&
1263                         )                                                     &
[1762]1264                                                 .OR.                          &
1265                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1266                           .AND. i == nxr-1 )    .OR.                          &
1267                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1268                           .AND. i == nxlu  ) )                                &
1269                THEN
[861]1270                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
[1886]1271                ELSEIF ( k > nzb_s_inner(j,i+1) .AND. k > nzb_s_inner(j,i+2)   &
1272                   .AND. k > nzb_s_inner(j,i+3) .AND. k > nzb_s_inner(j,i-1)   &
1273                   .AND. k > nzb_s_inner(j,i-2) )                              &
1274                THEN
[861]1275                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
1276                ENDIF
1277!
1278!--             scalar - y-direction
1279!--             WS1 (3), WS3 (4), WS5 (5)
[1886]1280                IF ( ( k <= nzb_s_inner(j+1,i)  .OR. k <= nzb_s_inner(j+2,i)   &   
1281                                                .OR. k <= nzb_s_inner(j-1,i) ) &
1282                                                 .OR.                          &
[1762]1283                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1284                           .AND. j == nys   )    .OR.                          &
1285                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1286                           .AND. j == nyn   ) )                                &
1287                THEN
[861]1288                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
[1762]1289!
[861]1290!--             WS3
[1886]1291                ELSEIF ( ( k <= nzb_s_inner(j+3,i) .AND. k > nzb_s_inner(j+1,i)&
1292                                                   .AND. k > nzb_s_inner(j+2,i)&
1293                                                   .AND. k > nzb_s_inner(j-1,i)&
1294                         )                           .OR.                      &
1295                         ( k <= nzb_s_inner(j-2,i) .AND. k > nzb_s_inner(j+1,i)&
1296                                                   .AND. k > nzb_s_inner(j+2,i)&
1297                                                   .AND. k > nzb_s_inner(j-1,i)&
1298                         )                                                     &
1299                                                     .OR.                      &
[1762]1300                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1301                           .AND. j == nysv  )    .OR.                          &
1302                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1303                           .AND. j == nyn-1 ) )                                &
1304                THEN
[861]1305                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 4 )
[1762]1306!
[861]1307!--             WS5
[1886]1308                ELSEIF ( k > nzb_s_inner(j+1,i) .AND. k > nzb_s_inner(j+2,i)   &
1309                   .AND. k > nzb_s_inner(j+3,i) .AND. k > nzb_s_inner(j-1,i)   &
1310                   .AND. k > nzb_s_inner(j-2,i) )                              &
1311                THEN
[861]1312                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 5 )
1313                ENDIF
1314!
1315!--             scalar - z-direction
1316!--             WS1 (6), WS3 (7), WS5 (8)
1317                flag_set = .FALSE.
1318                IF ( k == nzb_s_inner(j,i) + 1 .OR. k == nzt )  THEN
1319                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 6 )
1320                   flag_set = .TRUE.
1321                ELSEIF ( k == nzb_s_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
1322                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 7 )
1323                   flag_set = .TRUE.
1324                ELSEIF ( k > nzb_s_inner(j,i) .AND. .NOT. flag_set )  THEN
1325                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
1326                ENDIF
[1886]1327
[861]1328             ENDDO
1329          ENDDO
1330       ENDDO
1331    ENDIF
1332
1333    IF ( momentum_advec == 'ws-scheme' )  THEN
1334!
1335!--    Set wall_flags_0 to steer the degradation of the advection scheme in advec_ws
1336!--    near topography, inflow- and outflow boundaries as well as bottom and
1337!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
1338!--    grid points.
1339       DO  i = nxl, nxr
1340          DO  j = nys, nyn
[1580]1341             DO  k = nzb+1, nzt
[861]1342!
[1580]1343!--             At first, set flags to WS1.
1344!--             Since fluxes are swapped in advec_ws.f90, this is necessary to
1345!--             in order to handle the left/south flux.
1346!--             near vertical walls.
1347                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 9 )
1348                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
1349!
[861]1350!--             u component - x-direction
1351!--             WS1 (9), WS3 (10), WS5 (11)
[1886]1352                IF ( k <= nzb_u_inner(j,i+1)     .OR.                          &
[1762]1353                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1354                           .AND. i <= nxlu  )    .OR.                          &
1355                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1356                           .AND. i == nxr   ) )                                &
1357                THEN
[1580]1358                    wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 9 )
[1886]1359                ELSEIF ( ( k <= nzb_u_inner(j,i+2) .AND.                       &
1360                           k >  nzb_u_inner(j,i+1) ) .OR.                      &
1361                           k <= nzb_u_inner(j,i-1)                             &
1362                                                     .OR.                      &
[1762]1363                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1364                           .AND. i == nxr-1 )    .OR.                          &
1365                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1366                           .AND. i == nxlu+1) )                                &
1367                THEN
[861]1368                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 10 )
[1580]1369!
1370!--                Clear flag for WS1
1371                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 9 )
[1886]1372                ELSEIF ( k > nzb_u_inner(j,i+1) .AND. k > nzb_u_inner(j,i+2)   &
1373                                                .AND. k > nzb_u_inner(j,i-1) ) &
1374                THEN   
[861]1375                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 11 )
[1580]1376!
1377!--                Clear flag for WS1
1378                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 9 )
[861]1379                ENDIF
1380!
1381!--             u component - y-direction
1382!--             WS1 (12), WS3 (13), WS5 (14)
[1762]1383                IF ( k <= nzb_u_inner(j+1,i) .OR.                              &
1384                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1385                           .AND. j == nys   )    .OR.                          &
1386                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1387                           .AND. j == nyn   ) )                                &
1388                THEN
1389                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
[1886]1390                ELSEIF ( ( k <= nzb_u_inner(j+2,i) .AND.                       &
1391                           k >  nzb_u_inner(j+1,i) ) .OR.                      &
1392                           k <= nzb_u_inner(j-1,i)                             &
1393                                                     .OR.                      &
[1762]1394                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1395                           .AND. j == nysv  )    .OR.                          &
1396                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1397                           .AND. j == nyn-1 ) )                                &
1398                THEN
[861]1399                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 )
[1580]1400!
1401!--                Clear flag for WS1
1402                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 12 )
[1886]1403                ELSEIF ( k > nzb_u_inner(j+1,i) .AND. k > nzb_u_inner(j+2,i)   &
1404                                                .AND. k > nzb_u_inner(j-1,i) ) &
1405                THEN
[861]1406                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
[1580]1407!
1408!--                Clear flag for WS1
1409                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 12 )
[861]1410                ENDIF
1411!
1412!--             u component - z-direction
1413!--             WS1 (15), WS3 (16), WS5 (17)
1414                flag_set = .FALSE.
1415                IF ( k == nzb_u_inner(j,i) + 1 .OR. k == nzt )  THEN
1416                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
1417                   flag_set = .TRUE.
1418                ELSEIF ( k == nzb_u_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
1419                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
1420                   flag_set = .TRUE.
1421                ELSEIF ( k > nzb_u_inner(j,i) .AND. .NOT. flag_set )  THEN
1422                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
1423                ENDIF
1424
1425             ENDDO
1426          ENDDO
1427       ENDDO
1428
1429       DO  i = nxl, nxr
1430          DO  j = nys, nyn
[1580]1431             DO  k = nzb+1, nzt
[861]1432!
[1580]1433!--             At first, set flags to WS1.
1434!--             Since fluxes are swapped in advec_ws.f90, this is necessary to
1435!--             in order to handle the left/south flux.
1436                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
1437                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
1438!
[861]1439!--             v component - x-direction
1440!--             WS1 (18), WS3 (19), WS5 (20)
[1762]1441                IF ( k <= nzb_v_inner(j,i+1) .OR.                              &
1442                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1443                           .AND. i == nxl   )    .OR.                          &
1444                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1445                           .AND. i == nxr   ) )                                &
1446                THEN
1447                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
1448!
[861]1449!--             WS3
[1886]1450                ELSEIF ( ( k <= nzb_v_inner(j,i+2) .AND.                       &
1451                           k >  nzb_v_inner(j,i+1) ) .OR.                      &
1452                           k <= nzb_v_inner(j,i-1)                             &
[1762]1453                                                 .OR.                          &
1454                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1455                           .AND. i == nxr-1 )    .OR.                          &
1456                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1457                           .AND. i == nxlu  ) )                                &
1458                THEN
[861]1459                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
[1580]1460!
1461!--                Clear flag for WS1
1462                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 18 )
[1886]1463                ELSEIF ( k > nzb_v_inner(j,i+1) .AND. k > nzb_v_inner(j,i+2)   &
1464                                                .AND. k > nzb_v_inner(j,i-1) ) &
1465                THEN
[861]1466                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
[1580]1467!
1468!--                Clear flag for WS1
1469                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 18 )
[861]1470                ENDIF
1471!
1472!--             v component - y-direction
1473!--             WS1 (21), WS3 (22), WS5 (23)
[1762]1474                IF ( k <= nzb_v_inner(j+1,i) .OR.                              &
1475                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1476                           .AND. j <= nysv  )    .OR.                          &
1477                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1478                           .AND. j == nyn   ) )                                &
1479                THEN
1480                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
[1886]1481                ELSEIF ( ( k <= nzb_v_inner(j+2,i) .AND.                       &
1482                           k >  nzb_v_inner(j+1,i) ) .OR.                      &
1483                           k <= nzb_v_inner(j-1,i)                             &
1484                                                     .OR.                      &
[1762]1485                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1486                           .AND. j == nysv+1)    .OR.                          &
1487                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1488                           .AND. j == nyn-1 ) )                                &
1489                THEN
[861]1490                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
[1580]1491!
1492!--                Clear flag for WS1
1493                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 21 )
[1886]1494                ELSEIF ( k > nzb_v_inner(j+1,i) .AND. k > nzb_v_inner(j+2,i)   &
1495                                                .AND. k > nzb_v_inner(j-1,i) ) &
1496                THEN
[861]1497                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
[1580]1498!
1499!--                Clear flag for WS1
1500                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 21 )
[861]1501                ENDIF
1502!
1503!--             v component - z-direction
1504!--             WS1 (24), WS3 (25), WS5 (26)
1505                flag_set = .FALSE.
1506                IF ( k == nzb_v_inner(j,i) + 1 .OR. k == nzt )  THEN
1507                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
1508                   flag_set = .TRUE.
1509                ELSEIF ( k == nzb_v_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
1510                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
1511                   flag_set = .TRUE.
1512                ELSEIF ( k > nzb_v_inner(j,i) .AND. .NOT. flag_set )  THEN
1513                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
1514                ENDIF
1515
1516             ENDDO
1517          ENDDO
1518       ENDDO
1519       DO  i = nxl, nxr
1520          DO  j = nys, nyn
[1580]1521             DO  k = nzb+1, nzt
[861]1522!
[1580]1523!--             At first, set flags to WS1.
1524!--             Since fluxes are swapped in advec_ws.f90, this is necessary to
1525!--             in order to handle the left/south flux.
1526                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
1527                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
1528!
[861]1529!--             w component - x-direction
1530!--             WS1 (27), WS3 (28), WS5 (29)
[1762]1531                IF ( k <= nzb_w_inner(j,i+1) .OR.                              &
1532                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1533                           .AND. i == nxl   )    .OR.                          &
1534                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1535                           .AND. i == nxr   ) )                                &
1536                THEN
1537                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
[1886]1538                ELSEIF ( ( k <= nzb_w_inner(j,i+2) .AND.                       &
1539                           k >  nzb_w_inner(j,i+1) ) .OR.                      &
1540                           k <= nzb_w_inner(j,i-1)                             &
1541                                                     .OR.                      &
[1762]1542                         ( ( inflow_r .OR. outflow_r .OR. nest_bound_r )       &
1543                           .AND. i == nxr-1 )    .OR.                          &
1544                         ( ( inflow_l .OR. outflow_l .OR. nest_bound_l )       &
1545                           .AND. i == nxlu  ) )                                &
1546                THEN
[861]1547                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
[1580]1548!
1549!--                Clear flag for WS1
1550                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 27 )
[1886]1551                ELSEIF ( k > nzb_w_inner(j,i+1) .AND. k > nzb_w_inner(j,i+2)   &
1552                                                .AND. k > nzb_w_inner(j,i-1) ) &
1553                THEN
[861]1554                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i),29 )
[1580]1555!
1556!--                Clear flag for WS1
1557                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 27 )
[861]1558                ENDIF
1559!
1560!--             w component - y-direction
1561!--             WS1 (30), WS3 (31), WS5 (32)
[1762]1562                IF ( k <= nzb_w_inner(j+1,i) .OR.                              &
1563                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1564                           .AND. j == nys   )    .OR.                          &
1565                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1566                           .AND. j == nyn   ) )                                &
1567                THEN
1568                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
[1886]1569                ELSEIF ( ( k <= nzb_w_inner(j+2,i) .AND.                       &
1570                           k >  nzb_w_inner(j+1,i) ) .OR.                      &
1571                           k <= nzb_w_inner(j-1,i)                             &
1572                                                     .OR.                      &
[1762]1573                         ( ( inflow_s .OR. outflow_s .OR. nest_bound_s )       &
1574                           .AND. j == nysv  )    .OR.                          &
1575                         ( ( inflow_n .OR. outflow_n .OR. nest_bound_n )       &
1576                           .AND. j == nyn-1 ) )                                &
1577                THEN
[861]1578                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 31 )
[1580]1579!
1580!--                Clear flag for WS1
1581                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 30 )
[1886]1582                ELSEIF ( k > nzb_w_inner(j+1,i) .AND. k > nzb_w_inner(j+2,i)   &
1583                                                .AND. k > nzb_w_inner(j-1,i) ) &
1584                THEN
[1221]1585                   wall_flags_00(k,j,i) = IBSET( wall_flags_00(k,j,i), 0 )
[1580]1586!
1587!--                Clear flag for WS1
1588                   wall_flags_0(k,j,i) = IBCLR( wall_flags_0(k,j,i), 30 )
[861]1589                ENDIF
1590!
1591!--             w component - z-direction
[1679]1592!--             WS1 (33), WS3 (34), WS5 (35)
[861]1593                flag_set = .FALSE.
1594                IF ( k == nzb_w_inner(j,i) .OR. k == nzb_w_inner(j,i) + 1      &
1595                                           .OR. k == nzt )  THEN
1596!
1597!--                Please note, at k == nzb_w_inner(j,i) a flag is explictely
1598!--                set, although this is not a prognostic level. However,
1599!--                contrary to the advection of u,v and s this is necessary
1600!--                because flux_t(nzb_w_inner(j,i)) is used for the tendency
1601!--                at k == nzb_w_inner(j,i)+1.
[1221]1602                   wall_flags_00(k,j,i) = IBSET( wall_flags_00(k,j,i), 1 )
[861]1603                   flag_set = .TRUE.
1604                ELSEIF ( k == nzb_w_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
[1221]1605                   wall_flags_00(k,j,i) = IBSET( wall_flags_00(k,j,i), 2 )
[861]1606                   flag_set = .TRUE.
1607                ELSEIF ( k > nzb_w_inner(j,i) .AND. .NOT. flag_set )  THEN
[1221]1608                   wall_flags_00(k,j,i) = IBSET( wall_flags_00(k,j,i), 3 )
[861]1609                ENDIF
1610
1611             ENDDO
1612          ENDDO
1613       ENDDO
1614
1615    ENDIF
1616
1617!
[1677]1618!-- Exchange 3D integer wall_flags.
1619    IF ( momentum_advec == 'ws-scheme' .OR. scalar_advec == 'ws-scheme'     &
1620    .OR. scalar_advec == 'ws-scheme-mono' )  THEN 
1621!
1622!--    Exchange ghost points for advection flags
1623       CALL exchange_horiz_int( wall_flags_0,  nbgp )
1624       CALL exchange_horiz_int( wall_flags_00, nbgp )
1625!
1626!--    Set boundary flags at inflow and outflow boundary in case of
1627!--    non-cyclic boundary conditions.
[1762]1628       IF ( inflow_l .OR. outflow_l .OR. nest_bound_l )  THEN
[1677]1629          wall_flags_0(:,:,nxl-1)  = wall_flags_0(:,:,nxl)
1630          wall_flags_00(:,:,nxl-1) = wall_flags_00(:,:,nxl)
1631       ENDIF
1632
[1762]1633       IF ( inflow_r .OR. outflow_r .OR. nest_bound_r )  THEN
[1677]1634          wall_flags_0(:,:,nxr+1)  = wall_flags_0(:,:,nxr)
1635          wall_flags_00(:,:,nxr+1) = wall_flags_00(:,:,nxr)
1636       ENDIF
1637
[1762]1638       IF ( inflow_n .OR. outflow_n .OR. nest_bound_n )  THEN
[1677]1639          wall_flags_0(:,nyn+1,:)  = wall_flags_0(:,nyn,:)
1640          wall_flags_00(:,nyn+1,:) = wall_flags_00(:,nyn,:)
1641       ENDIF
1642
[1762]1643       IF ( inflow_s .OR. outflow_s  .OR. nest_bound_s )  THEN
[1677]1644          wall_flags_0(:,nys-1,:)  = wall_flags_0(:,nys,:)
1645          wall_flags_00(:,nys-1,:) = wall_flags_00(:,nys,:)
1646       ENDIF
1647
1648    ENDIF
1649
1650!
[1]1651!-- In case of topography: limit near-wall mixing length l_wall further:
1652!-- Go through all points of the subdomain one by one and look for the closest
1653!-- surface
1654    IF ( TRIM(topography) /= 'flat' )  THEN
1655       DO  i = nxl, nxr
1656          DO  j = nys, nyn
1657
1658             nzb_si = nzb_s_inner(j,i)
1659             vi     = vertical_influence(nzb_si)
1660
1661             IF ( wall_n(j,i) > 0 )  THEN
1662!
1663!--             North wall (y distance)
1664                DO  k = wall_n(j,i), nzb_si
[1353]1665                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i), 0.5_wp * dy )
[1]1666                ENDDO
1667!
1668!--             Above North wall (yz distance)
1669                DO  k = nzb_si + 1, nzb_si + vi
[1353]1670                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i),                     &
1671                                          SQRT( 0.25_wp * dy**2 +              &
[1]1672                                          ( zu(k) - zw(nzb_si) )**2 ) )
1673                ENDDO
1674!
1675!--             Northleft corner (xy distance)
1676                IF ( corner_nl(j,i) > 0 )  THEN
1677                   DO  k = corner_nl(j,i), nzb_si
1678                      l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1), &
[1353]1679                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
[1]1680                   ENDDO
1681!
1682!--                Above Northleft corner (xyz distance)
1683                   DO  k = nzb_si + 1, nzb_si + vi
[1353]1684                      l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1),              &
1685                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
1686                                            ( zu(k) - zw(nzb_si) )**2 ) )
[1]1687                   ENDDO
1688                ENDIF
1689!
1690!--             Northright corner (xy distance)
1691                IF ( corner_nr(j,i) > 0 )  THEN
1692                   DO  k = corner_nr(j,i), nzb_si
[1353]1693                       l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1),             &
1694                                                0.5_wp * SQRT( dx**2 + dy**2 ) )
[1]1695                   ENDDO
1696!
1697!--                Above northright corner (xyz distance)
1698                   DO  k = nzb_si + 1, nzb_si + vi
[1353]1699                      l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1),              &
1700                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
1701                                            ( zu(k) - zw(nzb_si) )**2 ) )
[1]1702                   ENDDO
1703                ENDIF
1704             ENDIF
1705
1706             IF ( wall_s(j,i) > 0 )  THEN
1707!
1708!--             South wall (y distance)
1709                DO  k = wall_s(j,i), nzb_si
[1353]1710                   l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i), 0.5_wp * dy )
[1]1711                ENDDO
1712!
1713!--             Above south wall (yz distance)
[1353]1714                DO  k = nzb_si + 1, nzb_si + vi
1715                   l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i),                     &
1716                                          SQRT( 0.25_wp * dy**2 +              &
[1]1717                                          ( zu(k) - zw(nzb_si) )**2 ) )
1718                ENDDO
1719!
1720!--             Southleft corner (xy distance)
1721                IF ( corner_sl(j,i) > 0 )  THEN
1722                   DO  k = corner_sl(j,i), nzb_si
[1353]1723                      l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1),              &
1724                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
[1]1725                   ENDDO
1726!
1727!--                Above southleft corner (xyz distance)
1728                   DO  k = nzb_si + 1, nzb_si + vi
[1353]1729                      l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1),              &
1730                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
1731                                            ( zu(k) - zw(nzb_si) )**2 ) )
[1]1732                   ENDDO
1733                ENDIF
1734!
1735!--             Southright corner (xy distance)
1736                IF ( corner_sr(j,i) > 0 )  THEN
1737                   DO  k = corner_sr(j,i), nzb_si
[1353]1738                      l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1),              &
1739                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
[1]1740                   ENDDO
1741!
1742!--                Above southright corner (xyz distance)
1743                   DO  k = nzb_si + 1, nzb_si + vi
[1353]1744                      l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1),              &
1745                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
1746                                            ( zu(k) - zw(nzb_si) )**2 ) )
[1]1747                   ENDDO
1748                ENDIF
1749
1750             ENDIF
1751
1752             IF ( wall_l(j,i) > 0 )  THEN
1753!
1754!--             Left wall (x distance)
1755                DO  k = wall_l(j,i), nzb_si
[1353]1756                   l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1), 0.5_wp * dx )
[1]1757                ENDDO
1758!
1759!--             Above left wall (xz distance)
1760                DO  k = nzb_si + 1, nzb_si + vi
[1353]1761                   l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1),                     &
1762                                       SQRT( 0.25_wp * dx**2 +                 &
1763                                       ( zu(k) - zw(nzb_si) )**2 ) )
[1]1764                ENDDO
1765             ENDIF
1766
1767             IF ( wall_r(j,i) > 0 )  THEN
1768!
1769!--             Right wall (x distance)
1770                DO  k = wall_r(j,i), nzb_si
[1353]1771                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1), 0.5_wp * dx )
[1]1772                ENDDO
1773!
1774!--             Above right wall (xz distance)
1775                DO  k = nzb_si + 1, nzb_si + vi
[1353]1776                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1),                     &
1777                                          SQRT( 0.25_wp * dx**2 +              &
[1]1778                                          ( zu(k) - zw(nzb_si) )**2 ) )
1779                ENDDO
1780
1781             ENDIF
1782
1783          ENDDO
1784       ENDDO
1785
1786    ENDIF
1787
1788!
1789!-- Multiplication with wall_adjustment_factor
1790    l_wall = wall_adjustment_factor * l_wall
1791
1792!
[709]1793!-- Set lateral boundary conditions for l_wall
[667]1794    CALL exchange_horiz( l_wall, nbgp )
1795
[1]1796    DEALLOCATE( corner_nl, corner_nr, corner_sl, corner_sr, nzb_local, &
1797                nzb_tmp, vertical_influence, wall_l, wall_n, wall_r, wall_s )
1798
1799
1800 END SUBROUTINE init_grid
Note: See TracBrowser for help on using the repository browser.