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

Last change on this file since 1746 was 1744, checked in by raasch, 8 years ago

two last commits documented

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