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

Last change on this file since 1841 was 1818, checked in by maronga, 8 years ago

last commit documented / copyright update

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