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

Last change on this file since 1845 was 1845, checked in by raasch, 8 years ago

nzb_2d replaced by nzb_..._inner, Kessler precipitation stored on surface grid point

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