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

Last change on this file since 1675 was 1675, checked in by gronemeier, 8 years ago

Bugfix: Definition of topography grid levels

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