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

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

last commit documented

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