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

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

Bugfix: Definition of topography height in case of grid stretching

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