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

Last change on this file since 1575 was 1575, checked in by raasch, 9 years ago

optimized multigrid method installed, new parameter seed_follows_topography for particle release, small adjustment in subjob for HLRN

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