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
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!
[1818]17! Copyright 1997-2016 Leibniz Universitaet Hannover
[2000]18!------------------------------------------------------------------------------!
[1036]19!
[254]20! Current revisions:
[1]21! -----------------
[2088]22! Bugfix in generic topography in case of ocean simulations
[1969]23!
24! Former revisions:
25! -----------------
26! $Id: init_grid.f90 2088 2016-12-19 16:30:25Z suehring $
27!
[2038]28! 2037 2016-10-26 11:15:40Z knoop
29! Anelastic approximation implemented
30!
[2022]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!
[2001]35! 2000 2016-08-20 18:09:15Z knoop
36! Forced header and separation lines into 80 columns
37!
[1995]38! 1994 2016-08-15 09:52:21Z suehring
39! Bugfix in definition of generic topography
40!
[1983]41! 1982 2016-08-01 11:04:48Z suehring
42! Bugfix concering consistency check for topography
43!
[1969]44! 1968 2016-07-18 12:01:49Z suehring
[1968]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.
[1932]53!
[1943]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!
[1932]58! 1931 2016-06-10 12:06:59Z suehring
59! Rename multigrid into multigrid_noopt and multigrid_fast into multigrid
60!
[1911]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!
[1903]66! 1902 2016-05-09 11:18:56Z suehring
[1910]67! Set topography flags for multigrid solver only (not for multigrid_fast)
[1903]68!
[1887]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!
[1846]74! 1845 2016-04-08 08:29:13Z raasch
75! nzb_2d removed
76!
[1805]77! 1804 2016-04-05 16:30:18Z maronga
78! Removed code for parameter file check (__check)
79!
[1780]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!
[1763]84! 1762 2016-02-25 12:31:13Z hellstea
85! Introduction of nested domain feature
86!
[1744]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!
[1692]91! 1691 2015-10-26 16:17:44Z maronga
92! Renamed prandtl_layer to constant_flux_layer.
93!
[1683]94! 1682 2015-10-07 23:56:08Z knoop
95! Code annotations made doxygen readable
96!
[1678]97! 1677 2015-10-02 13:25:23Z boeske
98! Bugfix: Ghost points are included in wall_flags_0 and wall_flags_00
99!
[1676]100! 1675 2015-10-02 08:28:59Z gronemeier
101! Bugfix: Definition of topography grid levels
102!
[1662]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!
[1581]107! 1580 2015-04-10 13:43:49Z suehring
108! Bugfix: setting flags for 5th order scheme near buildings
109!
[1576]110! 1575 2015-03-27 09:56:27Z raasch
111! adjustments for psolver-queries
112!
[1558]113! 1557 2015-03-05 16:43:04Z suehring
114! Adjustment for monotoinic limiter
115!
[1419]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!
[1410]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!
[1354]125! 1353 2014-04-08 15:21:23Z heinze
126! REAL constants provided with KIND-attribute
127!
[1323]128! 1322 2014-03-20 16:38:49Z raasch
129! REAL constants defined as wp-kind
130!
[1321]131! 1320 2014-03-20 08:40:49Z raasch
[1320]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
[1321]138!
[1222]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!
[1093]144! 1092 2013-02-02 11:24:22Z raasch
145! unused variables removed
146!
[1070]147! 1069 2012-11-28 16:18:43Z maronga
[1779]148! bugfix: added coupling_char to TOPOGRAPHY_DATA to allow topography in the
149!         ocean model in case of coupled runs
[1070]150!
[1037]151! 1036 2012-10-22 13:43:42Z raasch
152! code put under GPL (PALM 3.9)
153!
[1017]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!
[997]158! 996 2012-09-07 10:41:47Z raasch
159! little reformatting
160!
[979]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!
[928]165! 927 2012-06-06 19:15:04Z raasch
166! Wall flags are not set for multigrid method in case of masking method
167!
[865]168! 864 2012-03-27 15:10:33Z gryschka
[927]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.
[865]171!
[863]172! 861 2012-03-26 14:18:34Z suehring
[861]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
[863]175! model domain.!
[861]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!
[844]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!
[819]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!
[810]188! 809 2012-01-30 13:32:58Z maronga
189! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
190!
[808]191! 807 2012-01-25 11:53:51Z maronga
192! New cpp directive "__check" implemented which is used by check_namelist_files
193!
[1]194! Revision 1.1  1997/08/11 06:17:45  raasch
195! Initial revision (Testversion)
196!
197!
198! Description:
199! ------------
[1682]200!> Creating grid depending constants
[1]201!------------------------------------------------------------------------------!
[1682]202 SUBROUTINE init_grid
203 
[1942]204    USE advec_ws,                                                              &
205        ONLY:  ws_init_flags
[1]206
[1320]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       
[1353]211    USE control_parameters,                                                    &
[1910]212        ONLY:  bc_lr_cyc, bc_ns_cyc, building_height, building_length_x,       &
[1320]213               building_length_y, building_wall_left, building_wall_south,     &
214               canyon_height, canyon_wall_left, canyon_wall_south,             &
[1691]215               canyon_width_x, canyon_width_y, constant_flux_layer,            &
216               coupling_char, dp_level_ind_b, dz, dz_max, dz_stretch_factor,   &
[1968]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,    &
[1691]219               masking_method, maximum_grid_level, message_string,             &
[2021]220               momentum_advec, nest_domain, nest_bound_l, nest_bound_n,        &
221               nest_bound_r, nest_bound_s, ocean, outflow_l, outflow_n,        &
[1762]222               outflow_r, outflow_s, psolver, scalar_advec, topography,        &
[1691]223               topography_grid_convention, use_surface_fluxes, use_top_fluxes, &
[2021]224               wall_adjustment_factor
225         
[1320]226    USE grid_variables,                                                        &
[2037]227        ONLY:  ddx, ddx2, ddy, ddy2, dx, dx2, dy, dy2, fwxm,                   &
[1320]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,                                                               &
[1942]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,              &
[1845]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,              &
[1320]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   
[1]245    USE pegrid
246
247    IMPLICIT NONE
248
[1968]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
[1]282
[1886]283    INTEGER(iwp), DIMENSION(:), ALLOCATABLE   ::                               &
[1931]284                     vertical_influence  !< number of vertical grid points above obstacle where adjustment of near-wall mixing length is required
[1886]285                                         
[1968]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
[1]296
[1968]297    REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file   
[1886]298    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
[861]299
[1968]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
[1]303
[667]304   
[1]305!
[709]306!-- Calculation of horizontal array bounds including ghost layers
[667]307    nxlg = nxl - nbgp
308    nxrg = nxr + nbgp
309    nysg = nys - nbgp
310    nyng = nyn + nbgp
[709]311
[667]312!
[1]313!-- Allocate grid arrays
[1353]314    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
[667]315              dzw(1:nzt+1), l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1) )
[1]316
317!
318!-- Compute height of u-levels from constant grid length and dz stretch factors
[1353]319    IF ( dz == -1.0_wp )  THEN
[254]320       message_string = 'missing dz'
321       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
[1353]322    ELSEIF ( dz <= 0.0_wp )  THEN
[254]323       WRITE( message_string, * ) 'dz=',dz,' <= 0.0'
324       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
[1]325    ENDIF
[94]326
[1]327!
[94]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).
[843]332!--    The second u-level (k=1) corresponds to the top of the
[94]333!--    Prandtl-layer.
[667]334
335       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
[1353]336          zu(0) = 0.0_wp
337      !    zu(0) = - dz * 0.5_wp
[667]338       ELSE
[1353]339          zu(0) = - dz * 0.5_wp
[667]340       ENDIF
[1353]341       zu(1) =   dz * 0.5_wp
[1]342
[94]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
[1]353
354!
[94]355!--    Compute the w-levels. They are always staggered half-way between the
[843]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.
[1353]359       zw(0) = 0.0_wp
[94]360       DO  k = 1, nzt
[1353]361          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]362       ENDDO
[1353]363       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
[1]364
[94]365    ELSE
[1]366!
[843]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.
[1353]372       zu(nzt+1) =   dz * 0.5_wp
373       zu(nzt)   = - dz * 0.5_wp
[94]374
375       dz_stretch_level_index = 0
376       dz_stretched = dz
377       DO  k = nzt-1, 0, -1
[1418]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
[94]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
[843]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.
[94]396       zw(nzt+1) = dz
[1353]397       zw(nzt)   = 0.0_wp
[94]398       DO  k = 0, nzt
[1353]399          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
[94]400       ENDDO
401
[843]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
[94]409    ENDIF
410
411!
[1]412!-- Compute grid lengths.
413    DO  k = 1, nzt+1
414       dzu(k)  = zu(k) - zu(k-1)
[1353]415       ddzu(k) = 1.0_wp / dzu(k)
[1]416       dzw(k)  = zw(k) - zw(k-1)
[1353]417       ddzw(k) = 1.0_wp / dzw(k)
[1]418    ENDDO
419
420    DO  k = 1, nzt
[1353]421       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
[1]422    ENDDO
[667]423   
424!   
[709]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.
[1575]430    IF ( psolver(1:9) /= 'multigrid' )  THEN
[667]431       ALLOCATE( ddzu_pres(1:nzt+1) )
432       ddzu_pres = ddzu
[864]433       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
[1]434    ENDIF
435
436!
437!-- Compute the reciprocal values of the horizontal grid lengths.
[1353]438    ddx = 1.0_wp / dx
439    ddy = 1.0_wp / dy
[1]440    dx2 = dx * dx
441    dy2 = dy * dy
[1353]442    ddx2 = 1.0_wp / dx2
443    ddy2 = 1.0_wp / dy2
[1]444
445!
446!-- Compute the grid-dependent mixing length.
447    DO  k = 1, nzt
[1322]448       l_grid(k)  = ( dx * dy * dzw(k) )**0.33333333333333_wp
[1]449    ENDDO
450
451!
452!-- Allocate outer and inner index arrays for topography and set
[114]453!-- defaults.
[667]454
[114]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),             &
[1968]458              wall_r(nys:nyn,nxl:nxr), wall_s(nys:nyn,nxl:nxr) )                     
459     
[667]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),                              &
[1968]476              nzb_local(nysg:nyng,nxlg:nxrg),                               &
477              nzb_tmp(nysg:nyng,nxlg:nxrg),                                 &
[1221]478              rflags_s_inner(nzb:nzt+2,nysg:nyng,nxlg:nxrg),                &
479              rflags_invers(nysg:nyng,nxlg:nxrg,nzb:nzt+2),                 &
[667]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) )
[1]486
487
[667]488
489    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
490
[818]491
[1]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
[1353]497    rflags_s_inner = 1.0_wp
498    rflags_invers  = 1.0_wp
[1221]499
[1]500!
[19]501!-- Define vertical gridpoint from (or to) which on the usual finite difference
[1]502!-- form (which does not use surface fluxes) is applied
[1691]503    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
[1]504       nzb_diff = nzb + 2
505    ELSE
506       nzb_diff = nzb + 1
507    ENDIF
[19]508    IF ( use_top_fluxes )  THEN
509       nzt_diff = nzt - 1
510    ELSE
511       nzt_diff = nzt
512    ENDIF
[1]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
[1353]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
[1]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) / &
[1353]535                     ( wall_adjustment_factor * dzw(k) ) + 0.5_wp ), nzt - k )
[1]536    ENDDO
537
[1968]538    DO  k = 1, nzt
[1353]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
[254]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 )
[1]547          EXIT
548       ENDIF
549    ENDDO
550    vertical_influence(0) = vertical_influence(1)
551
[1968]552    DO  k = nzb + 1, nzb + vertical_influence(nzb)
553       l_wall(k,:,:) = zu(k) - zw(nzb)
[1]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!
[555]567!--       nzb_local is required for the multigrid solver
568          nzb_local = 0
[1]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 )
[2088]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
[1675]582          IF ( ABS( zw(bh  ) - building_height ) == &
583               ABS( zw(bh+1) - building_height )    )  bh = bh + 1
[1]584
[1322]585          IF ( building_wall_left == 9999999.9_wp )  THEN
[1]586             building_wall_left = ( nx + 1 - blx ) / 2 * dx
587          ENDIF
588          bxl = NINT( building_wall_left / dx )
589          bxr = bxl + blx
590
[1322]591          IF ( building_wall_south == 9999999.9_wp )  THEN
[1]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
[274]601             WRITE( message_string, * ) 'inconsistent building parameters:',   &
602                                      '& bxl=', bxl, 'bxr=', bxr, 'bys=', bys, &
603                                      'byn=', byn, 'nx=', nx, 'ny=', ny
[254]604             CALL message( 'init_grid', 'PA0203', 1, 2, 0, 6, 0 )
[1]605          ENDIF
606
607!
[1968]608!--       Define the building.
[1]609          nzb_local = 0
[1968]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
[1]613
[1994]614          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
615
[240]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.
[1322]620          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[240]621!
622!--          Street canyon in y direction
623             cwx = NINT( canyon_width_x / dx )
[1322]624             IF ( canyon_wall_left == 9999999.9_wp )  THEN
[240]625                canyon_wall_left = ( nx + 1 - cwx ) / 2 * dx
626             ENDIF
627             cxl = NINT( canyon_wall_left / dx )
628             cxr = cxl + cwx
629
[1322]630          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[240]631!
632!--          Street canyon in x direction
633             cwy = NINT( canyon_width_y / dy )
[1322]634             IF ( canyon_wall_south == 9999999.9_wp )  THEN
[240]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
[254]641             
642             message_string = 'no street canyon width given'
643             CALL message( 'init_grid', 'PA0204', 1, 2, 0, 6, 0 )
644 
[240]645          ENDIF
646
[2088]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
[1675]653          IF ( ABS( zw(ch  ) - canyon_height ) == &
654               ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
655
[240]656          dp_level_ind_b = ch
657!
658!--       Street canyon size has to meet some requirements
[1322]659          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[1353]660             IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR.        &
[240]661               ( ch < 3 ) )  THEN
[1353]662                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
663                                           '&cxl=', cxl, 'cxr=', cxr,          &
664                                           'cwx=', cwx,                        &
[254]665                                           'ch=', ch, 'nx=', nx, 'ny=', ny
666                CALL message( 'init_grid', 'PA0205', 1, 2, 0, 6, 0 ) 
[240]667             ENDIF
[1322]668          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[1353]669             IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR.        &
[240]670               ( ch < 3 ) )  THEN
[1353]671                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
672                                           '&cys=', cys, 'cyn=', cyn,          &
673                                           'cwy=', cwy,                        &
[254]674                                           'ch=', ch, 'nx=', nx, 'ny=', ny
675                CALL message( 'init_grid', 'PA0206', 1, 2, 0, 6, 0 ) 
[240]676             ENDIF
677          ENDIF
[1353]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' //         &
[254]682                              '&either in x- or in y-direction'
683             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
[240]684          ENDIF
685
686          nzb_local = ch
[1322]687          IF ( canyon_width_x /= 9999999.9_wp )  THEN
[1968]688             IF ( cxl <= nxr  .AND.  cxr >= nxl )                              &
689                nzb_local(:,MAX(nxl,cxl+1):MIN(nxr,cxr-1)) = 0
[1322]690          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
[1968]691             IF ( cys <= nyn  .AND.  cyn >= nys )                              &         
692                nzb_local(MAX(nys,cys+1):MIN(nyn,cyn-1),:) = 0
[240]693          ENDIF
694
[1994]695          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
696
[1]697       CASE ( 'read_from_file' )
[759]698
[1968]699          ALLOCATE ( topo_height(nys:nyn,nxl:nxr) )
[818]700
[759]701          DO  ii = 0, io_blocks-1
702             IF ( ii == io_group )  THEN
703
[1]704!
[759]705!--             Arbitrary irregular topography data in PALM format (exactly
706!--             matching the grid size and total domain size)
[1779]707                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
708                          STATUS='OLD', FORM='FORMATTED', ERR=10 )
[1968]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
[759]716                ENDDO
[1968]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
[759]725
726                GOTO 12
727         
[1779]728 10             message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )//    &
729                                 ' does not exist'
[759]730                CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
731
[1779]732 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
733                                 TRIM( coupling_char )
[759]734                CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
735
736 12             CLOSE( 90 )
737
738             ENDIF
[1804]739#if defined( __parallel )
[759]740             CALL MPI_BARRIER( comm2d, ierr )
741#endif
[559]742          ENDDO
[759]743
[1]744!
745!--       Calculate the index height of the topography
[1968]746          nzb_local = 0
747          DO  i = nxl, nxr
748             DO  j = nys, nyn
[1675]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
[1]753             ENDDO
754          ENDDO
[818]755
756          DEALLOCATE ( topo_height )
[1942]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!
[1968]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,:)
[1942]770          ENDIF
[1910]771
[1968]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)         
[1942]775          ENDIF
776
[1968]777          num_hole_l = 0
778          DO i = nxl, nxr
779             DO j = nys, nyn
[1942]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) )
[1968]795                   num_hole_l     = num_hole_l + 1
[1942]796                ENDIF
797             ENDDO
798          ENDDO
[114]799!
[1968]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!
[1942]808!--       Create an informative message if any hole was removed.
[1968]809          IF ( num_hole > 0 )  THEN
[1942]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!
[1968]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,:)
[1910]822          ENDIF
[667]823
[1968]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)         
[1910]827          ENDIF
828
[1]829       CASE DEFAULT
830!
831!--       The DEFAULT case is reached either if the parameter topography
[217]832!--       contains a wrong character string or if the user has defined a special
[1]833!--       case in the user interface. There, the subroutine user_init_grid
834!--       checks which of these two conditions applies.
[1968]835          CALL user_init_grid( nzb_local )
[1]836
837    END SELECT
838!
[861]839!-- Determine the maximum level of topography. Furthermore it is used for
840!-- steering the degradation of order of the applied advection scheme.
[978]841!-- In case of non-cyclic lateral boundaries, the order of the advection
[1968]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
[1677]847    nzb_max = MAXVAL( nzb_local ) + 1
[1968]848#endif
[1353]849    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
[1762]850         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
851         nest_domain )                                                         &
852    THEN
853       nzb_max = nzt
[978]854    ENDIF
855
[861]856!
[1]857!-- Consistency checks and index array initialization are only required for
[217]858!-- non-flat topography, also the initialization of topography height arrays
[49]859!-- zu_s_inner and zw_w_inner
[1]860    IF ( TRIM( topography ) /= 'flat' )  THEN
[1968]861#if defined( __parallel )
862       CALL MPI_ALLREDUCE( MAXVAL( nzb_local ), nzb_local_max, 1, MPI_INTEGER, &
863                           MPI_MAX, comm2d, ierr )
[1982]864       CALL MPI_ALLREDUCE( MINVAL( nzb_local ), nzb_local_min, 1, MPI_INTEGER, &
[1968]865                           MPI_MIN, comm2d, ierr )                           
866#else
867       nzb_local_max = MAXVAL( nzb_local )
868       nzb_local_min = MINVAL( nzb_local )
869#endif
[1982]870
[1]871!
872!--    Consistency checks
[1968]873       IF ( nzb_local_min < 0  .OR.  nzb_local_max  > nz + 1 )  THEN
[1353]874          WRITE( message_string, * ) 'nzb_local values are outside the',       &
875                                'model domain',                                &
[1968]876                                '&MINVAL( nzb_local ) = ', nzb_local_min,      &
877                                '&MAXVAL( nzb_local ) = ', nzb_local_max
[254]878          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
[1]879       ENDIF
880
[217]881       IF ( topography_grid_convention == 'cell_edge' )  THEN
[134]882!
[217]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
[1968]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
[217]895                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j,i+1) )
896             ENDDO
[134]897          ENDDO
[1968]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
[217]904                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) )
905             ENDDO
[134]906          ENDDO
[1968]907!
908!--       Exchange ghost points         
909          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
[217]910       ENDIF
[1]911!
912!--    Initialize index arrays nzb_s_inner and nzb_w_inner
[1968]913       nzb_s_inner = nzb_local
914       nzb_w_inner = nzb_local
[1]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
[1968]933       nzb_tmp = nzb_local
934       DO  j = nys, nyn
935          DO  i = nxl, nxr
[1353]936             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
[1]937                                 nzb_local(j,i+1) )
938          ENDDO
939       ENDDO
[1968]940       
941       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
942       
[1]943       DO  i = nxl, nxr
944          DO  j = nys, nyn
[1353]945             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
[1]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
[1743]955          IF ( nyn == ny )  THEN
[1]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
[1968]968       nzb_tmp = nzb_local
969       DO  j = nys, nyn
970          DO  i = nxl, nxr
[1]971             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
972          ENDDO
973       ENDDO
[1968]974       
975       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
976       
977       nzb_u_inner = nzb_tmp
[1]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
[1353]983             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
[1]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
[1743]993          IF ( nyn == ny )  THEN
[1]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
[1968]1002       nzb_tmp = nzb_local
1003       DO  i = nxl, nxr
1004          DO  j = nys, nyn
[1]1005             nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
1006          ENDDO
1007       ENDDO
[1968]1008       
1009       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )     
1010       nzb_v_inner = nzb_tmp
[1]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
[1353]1017             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),             &
[1]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
[1804]1032
[1]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.
[1968]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 )
[1]1044
[49]1045!
1046!--    Allocate and set the arrays containing the topography height
[1968]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
[49]1071
[1968]1072      DEALLOCATE( zu_s_inner_l, zw_w_inner_l )
1073      IF ( myid /= 0 )  DEALLOCATE( zu_s_inner, zw_w_inner )
[1221]1074!
[1968]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!
[1221]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
[1353]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
[1221]1099             ENDDO
1100          ENDDO
1101       ENDDO
[1804]1102
[1]1103    ENDIF
[1968]1104!
1105!-- Deallocate temporary array, as it might be reused for different
1106!-- grid-levels further below.
1107    DEALLOCATE( nzb_tmp )
[1]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
[1691]1113    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
[1]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
[1353]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
[1]1145          ELSEIF ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) )  THEN
[1353]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
[1]1149          ENDIF
1150!
1151!--       v-component
1152          IF ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) )  THEN
[1353]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
[1]1156          ELSEIF ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) )  THEN
[1353]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
[1]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
[1353]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
[1]1169          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) )  THEN
[1353]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
[1]1174          ENDIF
1175          IF ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) )  THEN
[1353]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
[1]1180          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) )  THEN
[1353]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
[1]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!
[1931]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
[1968]1235
[114]1236!
[1968]1237!--    Gridpoint increment of the current level.
[114]1238       inc = 1
1239       DO  l = maximum_grid_level, 1 , -1
[1968]1240!
1241!--       Set grid_level as it is required for exchange_horiz_2d_int
1242          grid_level = l
[114]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
[116]1283!--       Bit 6:  inside building
[114]1284
1285          flags = 0
1286
[927]1287!
1288!--       In case of masking method, flags are not set and multigrid method
1289!--       works like FFT-solver
[1931]1290          IF ( .NOT. masking_method )  THEN
[927]1291
[1968]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
[2021]1313                IF ( inflow_s .OR. outflow_s .OR. nest_bound_s  )  THEN
[1968]1314                   nzb_tmp(-2,:) = nzb_tmp(0,:)
1315                   nzb_tmp(-1,:) = nzb_tmp(0,:)
1316                ENDIF
[2021]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,:)
[1968]1320                ENDIF
1321             ENDIF
1322             IF ( .NOT. bc_lr_cyc )  THEN
[2021]1323                IF ( inflow_l .OR. outflow_l .OR. nest_bound_l  )  THEN
[1968]1324                   nzb_tmp(:,-2) = nzb_tmp(:,0)
1325                   nzb_tmp(:,-1) = nzb_tmp(:,0)
1326                ENDIF
[2021]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)     
[1968]1330                ENDIF       
1331             ENDIF
1332                       
[927]1333             DO  i = nxl_l-1, nxr_l+1
1334                DO  j = nys_l-1, nyn_l+1
[1968]1335                   DO  k = nzb, nzt_l+1     
[114]1336!
[927]1337!--                   Inside/outside building (inside building does not need
1338!--                   further tests for walls)
[1968]1339                      IF ( k*inc <= nzb_tmp(j,i) )  THEN
[114]1340
[927]1341                         flags(k,j,i) = IBSET( flags(k,j,i), 6 )
[114]1342
[927]1343                      ELSE
[114]1344!
[927]1345!--                      Bottom wall
[1968]1346                         IF ( (k-1)*inc <= nzb_tmp(j,i) )  THEN
[927]1347                            flags(k,j,i) = IBSET( flags(k,j,i), 0 )
1348                         ENDIF
[114]1349!
[927]1350!--                      South wall
[1968]1351                         IF ( k*inc <= nzb_tmp(j-1,i) )  THEN
[927]1352                            flags(k,j,i) = IBSET( flags(k,j,i), 2 )
1353                         ENDIF
[114]1354!
[927]1355!--                      North wall
[1968]1356                         IF ( k*inc <= nzb_tmp(j+1,i) )  THEN
[927]1357                            flags(k,j,i) = IBSET( flags(k,j,i), 3 )
1358                         ENDIF
[114]1359!
[927]1360!--                      Left wall
[1968]1361                         IF ( k*inc <= nzb_tmp(j,i-1) )  THEN
[927]1362                            flags(k,j,i) = IBSET( flags(k,j,i), 4 )
1363                         ENDIF
[114]1364!
[927]1365!--                      Right wall
[1968]1366                         IF ( k*inc <= nzb_tmp(j,i+1) )  THEN
[927]1367                            flags(k,j,i) = IBSET( flags(k,j,i), 5 )
1368                         ENDIF
1369
[114]1370                      ENDIF
1371                           
[927]1372                   ENDDO
[114]1373                ENDDO
1374             ENDDO
1375
[1968]1376             DEALLOCATE( nzb_tmp )
1377
[927]1378          ENDIF
1379
[114]1380          inc = inc * 2
1381
1382       ENDDO
[1968]1383!
1384!--    Reset grid_level to "normal" grid
1385       grid_level = 0
1386       
[114]1387    ENDIF
[861]1388!
[1942]1389!-- Allocate flags needed for masking walls. Even though these flags are only
[2021]1390!-- required in the ws-scheme, the arrays need to be allocated here as they are
[1942]1391!-- used in OpenACC directives.
[1677]1392    ALLOCATE( wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
1393              wall_flags_00(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
[1221]1394    wall_flags_0  = 0
1395    wall_flags_00 = 0
[114]1396!
[2021]1397!-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e.
1398!-- to decrease the numerical stencil appropriately.
[1942]1399    IF ( momentum_advec == 'ws-scheme'  .OR.  scalar_advec == 'ws-scheme'  .OR.&
1400         scalar_advec   == 'ws-scheme-mono' )  THEN
1401       CALL ws_init_flags
[861]1402    ENDIF
1403
1404!
[1]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
[1353]1419                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i), 0.5_wp * dy )
[1]1420                ENDDO
1421!
1422!--             Above North wall (yz distance)
1423                DO  k = nzb_si + 1, nzb_si + vi
[1353]1424                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i),                     &
1425                                          SQRT( 0.25_wp * dy**2 +              &
[1]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), &
[1353]1433                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
[1]1434                   ENDDO
1435!
1436!--                Above Northleft corner (xyz distance)
1437                   DO  k = nzb_si + 1, nzb_si + vi
[1353]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 ) )
[1]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
[1353]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 ) )
[1]1449                   ENDDO
1450!
1451!--                Above northright corner (xyz distance)
1452                   DO  k = nzb_si + 1, nzb_si + vi
[1353]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 ) )
[1]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
[1353]1464                   l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i), 0.5_wp * dy )
[1]1465                ENDDO
1466!
1467!--             Above south wall (yz distance)
[1353]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 +              &
[1]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
[1353]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 ) )
[1]1479                   ENDDO
1480!
1481!--                Above southleft corner (xyz distance)
1482                   DO  k = nzb_si + 1, nzb_si + vi
[1353]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 ) )
[1]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
[1353]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 ) )
[1]1494                   ENDDO
1495!
1496!--                Above southright corner (xyz distance)
1497                   DO  k = nzb_si + 1, nzb_si + vi
[1353]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 ) )
[1]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
[1353]1510                   l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1), 0.5_wp * dx )
[1]1511                ENDDO
1512!
1513!--             Above left wall (xz distance)
1514                DO  k = nzb_si + 1, nzb_si + vi
[1353]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 ) )
[1]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
[1353]1525                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1), 0.5_wp * dx )
[1]1526                ENDDO
1527!
1528!--             Above right wall (xz distance)
1529                DO  k = nzb_si + 1, nzb_si + vi
[1353]1530                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1),                     &
1531                                          SQRT( 0.25_wp * dx**2 +              &
[1]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!
[709]1547!-- Set lateral boundary conditions for l_wall
[667]1548    CALL exchange_horiz( l_wall, nbgp )
1549
[1]1550    DEALLOCATE( corner_nl, corner_nr, corner_sl, corner_sr, nzb_local, &
[1968]1551                vertical_influence, wall_l, wall_n, wall_r, wall_s )
[1]1552
1553
1554 END SUBROUTINE init_grid
Note: See TracBrowser for help on using the repository browser.