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

Last change on this file since 1691 was 1691, checked in by maronga, 8 years ago

various bugfixes and modifications of the atmosphere-land-surface-radiation interaction. Completely re-written routine to calculate surface fluxes (surface_layer_fluxes.f90) that replaces prandtl_fluxes. Minor formatting corrections and renamings

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