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

Last change on this file since 1580 was 1580, checked in by suehring, 9 years ago

Bugfix: setting flags for 5th-order advection scheme, and stastical evaluation of scalar fluxes in 5th-order scheme in case of monotonic limiter.

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