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

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

Bugfix in topography in case of ocean simulations

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