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

Last change on this file since 2088 was 2088, checked in by suehring, 7 years ago

Bugfixes in initial salinity profile and generic topography definition in case of ocean simulations

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