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

Last change on this file since 1322 was 1322, checked in by raasch, 10 years ago

REAL functions and a lot of REAL constants provided with KIND-attribute,
some small bugfixes

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