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

Last change on this file since 1409 was 1409, checked in by suehring, 10 years ago

Bugfix: set inflow boundary conditions also if no humidity or passive_scalar is used

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