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

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

Reading of 3D topography using NetCDF data type NC_BYTE; bugfixes in reading 3D topography from file

  • Property svn:keywords set to Id
File size: 85.5 KB
Line 
1!> @file init_grid.f90
2!------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2017 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: init_grid.f90 2302 2017-07-03 14:07:20Z maronga $
27! Bugfixes in reading 3D topography from file
28!
29! 2274 2017-06-09 13:27:48Z Giersch
30! Changed error messages
31!
32! 2233 2017-05-30 18:08:54Z suehring
33!
34! 2232 2017-05-30 17:47:52Z suehring
35! - Adjustments according to new topography representation
36! - Bugfix: Move determination of nzb_max behind topography modification in
37!   cell-edge case
38! - Get rid off global arrays required for topography output
39! - Enable topography input via netcdf
40! - Generic tunnel set-up added
41!
42! 2200 2017-04-11 11:37:51Z suehring
43! monotonic_adjustment removed
44!
45! 2169 2017-03-06 18:16:35Z suehring
46! Bugfix, move setting for topography grid convention to init_grid, else, if no
47! value is set, the simulation may abort in case of restarts
48!
49! 2128 2017-01-23 15:00:03Z suehring
50! Bugfix in setting topography from file in case of ocean simulations
51!
52! 2088 2016-12-19 16:30:25Z suehring
53! Bugfix in generic topography in case of ocean simulations
54!
55! 2037 2016-10-26 11:15:40Z knoop
56! Anelastic approximation implemented
57!
58! 2021 2016-10-07 14:08:57Z suehring
59! Bugfix: setting Neumann boundary conditions for topography required for
60! topography flags in multigrid_noopt solver
61!
62! 2000 2016-08-20 18:09:15Z knoop
63! Forced header and separation lines into 80 columns
64!
65! 1994 2016-08-15 09:52:21Z suehring
66! Bugfix in definition of generic topography
67!
68! 1982 2016-08-01 11:04:48Z suehring
69! Bugfix concering consistency check for topography
70!
71! 1968 2016-07-18 12:01:49Z suehring
72! Changed: PE-wise reading of topography file in order to avoid global definition
73! of arrays nzb_local and nzb_tmp. Thereby, topography definition for single
74! buildings and street canyons has changed, as well as flag setting for
75! multigrid scheme.
76!
77! Bugfix in checking l_grid anisotropy.
78! Simplify initial computation of lwall and vertical_influence, i.e. remove
79! nzb_s_inner as it is still zero at this point.
80!
81! 1942 2016-06-14 12:18:18Z suehring
82! Topography filter implemented to fill holes resolved by only one grid point.
83! Initialization of flags for ws-scheme moved to advec_ws. 
84!
85! 1931 2016-06-10 12:06:59Z suehring
86! Rename multigrid into multigrid_noopt and multigrid_fast into multigrid
87!
88! 1910 2016-05-26 06:49:46Z raasch
89! Bugfix: if topography is read from file, Neumann conditions are used for the
90! nzb_local array (instead of cyclic conditions) in case that non-cyclic
91! boundary conditions are switched on for the run
92!
93! 1902 2016-05-09 11:18:56Z suehring
94! Set topography flags for multigrid solver only (not for multigrid_fast)
95!
96! 1886 2016-04-21 11:20:47Z suehring
97! Bugfix: setting advection flags near walls
98! reformulated index values for nzb_v_inner
99! variable discriptions added in declaration block
100!
101! 1845 2016-04-08 08:29:13Z raasch
102! nzb_2d removed
103!
104! 1804 2016-04-05 16:30:18Z maronga
105! Removed code for parameter file check (__check)
106!
107! 1779 2016-03-03 08:01:28Z raasch
108! coupling_char is trimmed at every place it occurs, because it can have
109! different length now
110!
111! 1762 2016-02-25 12:31:13Z hellstea
112! Introduction of nested domain feature
113!
114! 1743 2016-01-13 10:23:51Z raasch
115! Bugfix for calculation of nzb_s_outer and nzb_u_outer at north boundary of
116! total domain
117!
118! 1691 2015-10-26 16:17:44Z maronga
119! Renamed prandtl_layer to constant_flux_layer.
120!
121! 1682 2015-10-07 23:56:08Z knoop
122! Code annotations made doxygen readable
123!
124! 1677 2015-10-02 13:25:23Z boeske
125! Bugfix: Ghost points are included in wall_flags_0 and wall_flags_00
126!
127! 1675 2015-10-02 08:28:59Z gronemeier
128! Bugfix: Definition of topography grid levels
129!
130! 1660 2015-09-21 08:15:16Z gronemeier
131! Bugfix: Definition of topography grid levels if vertical grid stretching
132!         starts below the maximum topography height.
133!
134! 1580 2015-04-10 13:43:49Z suehring
135! Bugfix: setting flags for 5th order scheme near buildings
136!
137! 1575 2015-03-27 09:56:27Z raasch
138! adjustments for psolver-queries
139!
140! 1557 2015-03-05 16:43:04Z suehring
141! Adjustment for monotoinic limiter
142!
143! 1418 2014-06-06 13:05:08Z fricke
144! Bugfix: Change if-condition for stretched grid in the ocean, with the old
145!          condition and a negative value for dz_stretch_level the condition
146!          was always true for the whole model domain
147!
148! 1409 2014-05-23 12:11:32Z suehring
149! Bugfix: set wall_flags_0 at inflow and outflow boundary also for i <= nxlu
150! j <= nysv
151!
152! 1353 2014-04-08 15:21:23Z heinze
153! REAL constants provided with KIND-attribute
154!
155! 1322 2014-03-20 16:38:49Z raasch
156! REAL constants defined as wp-kind
157!
158! 1320 2014-03-20 08:40:49Z raasch
159! ONLY-attribute added to USE-statements,
160! kind-parameters added to all INTEGER and REAL declaration statements,
161! kinds are defined in new module kinds,
162! revision history before 2012 removed,
163! comment fields (!:) to be used for variable explanations added to
164! all variable declaration statements
165!
166! 1221 2013-09-10 08:59:13Z raasch
167! wall_flags_00 introduced to hold bits 32-63,
168! additional 3D-flag arrays for replacing the 2D-index array nzb_s_inner in
169! loops optimized for openACC (pres + flow_statistics)
170!
171! 1092 2013-02-02 11:24:22Z raasch
172! unused variables removed
173!
174! 1069 2012-11-28 16:18:43Z maronga
175! bugfix: added coupling_char to TOPOGRAPHY_DATA to allow topography in the
176!         ocean model in case of coupled runs
177!
178! 1036 2012-10-22 13:43:42Z raasch
179! code put under GPL (PALM 3.9)
180!
181! 1015 2012-09-27 09:23:24Z raasch
182! lower index for calculating wall_flags_0 set to nzb_w_inner instead of
183! nzb_w_inner+1
184!
185! 996 2012-09-07 10:41:47Z raasch
186! little reformatting
187!
188! 978 2012-08-09 08:28:32Z fricke
189! Bugfix: nzb_max is set to nzt at non-cyclic lateral boundaries
190! Bugfix: Set wall_flags_0 for inflow boundary
191!
192! 927 2012-06-06 19:15:04Z raasch
193! Wall flags are not set for multigrid method in case of masking method
194!
195! 864 2012-03-27 15:10:33Z gryschka
196! In case of ocean and Dirichlet bottom bc for u and v dzu_mg and ddzu_pres
197! were not correctly defined for k=1.
198!
199! 861 2012-03-26 14:18:34Z suehring
200! Set wall_flags_0. The array is needed for degradation in ws-scheme near walls,
201! inflow and outflow boundaries as well as near the bottom and the top of the
202! model domain.!
203! Initialization of nzb_s_inner and nzb_w_inner.
204! gls has to be at least nbgp to do not exceed the array bounds of nzb_local
205! while setting wall_flags_0
206!
207! 843 2012-02-29 15:16:21Z gryschka
208! In case of ocean and dirichlet bc for u and v at the bottom
209! the first u-level ist defined at same height as the first w-level
210!
211! 818 2012-02-08 16:11:23Z maronga
212! Bugfix: topo_height is only required if topography is used. It is thus now
213! allocated in the topography branch
214!
215! 809 2012-01-30 13:32:58Z maronga
216! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
217!
218! 807 2012-01-25 11:53:51Z maronga
219! New cpp directive "__check" implemented which is used by check_namelist_files
220!
221! Revision 1.1  1997/08/11 06:17:45  raasch
222! Initial revision (Testversion)
223!
224!
225! Description:
226! ------------
227!> Creating grid depending constants
228!> To Do: Setting topo flags only based on topo_3d array - flags for former
229!> nzb_outer arrays are still not set properly.
230!> To Do: Rearrange topo flag list
231!------------------------------------------------------------------------------!
232 SUBROUTINE init_grid
233 
234    USE advec_ws,                                                              &
235        ONLY:  ws_init_flags
236
237    USE arrays_3d,                                                             &
238        ONLY:  dd2zu, ddzu, ddzu_pres, ddzw, dzu, dzu_mg, dzw, dzw_mg, f1_mg,  &
239               f2_mg, f3_mg, l_grid, l_wall, zu, zw
240       
241    USE control_parameters,                                                    &
242        ONLY:  bc_lr_cyc, bc_ns_cyc, building_height, building_length_x,       &
243               building_length_y, building_wall_left, building_wall_south,     &
244               canyon_height, canyon_wall_left, canyon_wall_south,             &
245               canyon_width_x, canyon_width_y, constant_flux_layer,            &
246               coupling_char, dp_level_ind_b, dz, dz_max, dz_stretch_factor,   &
247               dz_stretch_level, dz_stretch_level_index, grid_level, ibc_uv_b, &
248               io_blocks, io_group, inflow_l, inflow_n, inflow_r, inflow_s,    &
249               lod, masking_method, maximum_grid_level, message_string,        &
250               momentum_advec, nest_domain, nest_bound_l, nest_bound_n,        &
251               nest_bound_r, nest_bound_s, ocean, outflow_l, outflow_n,        &
252               outflow_r, outflow_s, psolver, scalar_advec, topography,        &
253               topography_grid_convention, tunnel_height, tunnel_length,       &
254               tunnel_width_x, tunnel_width_y, tunnel_wall_depth,              &
255               use_surface_fluxes, use_top_fluxes, wall_adjustment_factor
256         
257    USE grid_variables,                                                        &
258        ONLY:  ddx, ddx2, ddy, ddy2, dx, dx2, dy, dy2, zu_s_inner, zw_w_inner
259       
260    USE indices,                                                               &
261        ONLY:  advc_flags_1, advc_flags_2, flags, nbgp, nx, nxl, nxlg, nxl_mg, &
262               nxr, nxrg, nxr_mg, ny, nyn, nyng, nyn_mg, nys, nys_mg, nysg, nz,&
263               nzb, nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer,              &
264               nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,                 &
265               nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner,             &
266               nzb_w_outer, nzt, nzt_mg, wall_flags_0, wall_flags_1,           &
267               wall_flags_10, wall_flags_2, wall_flags_3,  wall_flags_4,       &
268               wall_flags_5, wall_flags_6, wall_flags_7, wall_flags_8,         &
269               wall_flags_9
270   
271    USE kinds
272#if defined ( __netcdf )
273    USE netcdf_interface,                                                      &
274        ONLY:  netcdf_close_file, netcdf_open_read_file, netcdf_get_attribute, &
275               netcdf_get_variable
276#endif
277    USE pegrid
278
279    USE surface_mod,                                                           &
280        ONLY:  init_bc
281
282    IMPLICIT NONE
283
284    INTEGER(iwp) ::  bh            !< temporary vertical index of building height
285    INTEGER(iwp) ::  blx           !< grid point number of building size along x
286    INTEGER(iwp) ::  bly           !< grid point number of building size along y
287    INTEGER(iwp) ::  bxl           !< index for left building wall
288    INTEGER(iwp) ::  bxr           !< index for right building wall
289    INTEGER(iwp) ::  byn           !< index for north building wall
290    INTEGER(iwp) ::  bys           !< index for south building wall
291    INTEGER(iwp) ::  ch            !< temporary vertical index for canyon height
292    INTEGER(iwp) ::  cwx           !< grid point number of canyon size along x
293    INTEGER(iwp) ::  cwy           !< grid point number of canyon size along y
294    INTEGER(iwp) ::  cxl           !< index for left canyon wall
295    INTEGER(iwp) ::  cxr           !< index for right canyon wall
296    INTEGER(iwp) ::  cyn           !< index for north canyon wall
297    INTEGER(iwp) ::  cys           !< index for south canyon wall
298    INTEGER(iwp) ::  i             !< index variable along x
299    INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
300    INTEGER(iwp) ::  ii            !< loop variable for reading topography file
301    INTEGER(iwp) ::  inc           !< incremental parameter for coarsening grid level
302    INTEGER(iwp) ::  j             !< index variable along y
303    INTEGER(iwp) ::  k             !< index variable along z
304    INTEGER(iwp) ::  k_top         !< topography top index on local PE
305    INTEGER(iwp) ::  l             !< loop variable
306    INTEGER(iwp) ::  nxl_l         !< index of left PE boundary for multigrid level
307    INTEGER(iwp) ::  nxr_l         !< index of right PE boundary for multigrid level
308    INTEGER(iwp) ::  nyn_l         !< index of north PE boundary for multigrid level
309    INTEGER(iwp) ::  nys_l         !< index of south PE boundary for multigrid level
310    INTEGER(iwp) ::  nzb_local_max !< vertical grid index of maximum topography height
311    INTEGER(iwp) ::  nzb_local_min !< vertical grid index of minimum topography height
312    INTEGER(iwp) ::  nzt_l         !< index of top PE boundary for multigrid level
313    INTEGER(iwp) ::  num_hole      !< number of holes (in topography) resolved by only one grid point
314    INTEGER(iwp) ::  num_hole_l    !< number of holes (in topography) resolved by only one grid point on local PE     
315    INTEGER(iwp) ::  num_wall      !< number of surrounding vertical walls for a single grid point
316    INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file   
317    INTEGER(iwp) ::  hv_in         !< heavyside function to model inner tunnel surface
318    INTEGER(iwp) ::  hv_out        !< heavyside function to model outer tunnel surface
319    INTEGER(iwp) ::  txe_out       !< end position of outer tunnel wall in x
320    INTEGER(iwp) ::  txs_out       !< start position of outer tunnel wall in x
321    INTEGER(iwp) ::  tye_out       !< end position of outer tunnel wall in y
322    INTEGER(iwp) ::  tys_out       !< start position of outer tunnel wall in y
323    INTEGER(iwp) ::  txe_in        !< end position of inner tunnel wall in x
324    INTEGER(iwp) ::  txs_in        !< start position of inner tunnel wall in x
325    INTEGER(iwp) ::  tye_in        !< end position of inner tunnel wall in y
326    INTEGER(iwp) ::  tys_in        !< start position of inner tunnel wall in y
327    INTEGER(iwp) ::  td            !< tunnel wall depth
328    INTEGER(iwp) ::  th            !< height of outer tunnel wall
329                                     
330    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local      !< index for topography top at cell-center
331    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp        !< dummy to calculate topography indices on u- and v-grid
332
333    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo_3d !< input array for 3D topography and dummy array for setting "outer"-flags
334
335    LOGICAL  ::  netcdf_extend = .FALSE. !< Flag indicating wether netcdf topography input file or not
336
337    REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file   
338    REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
339
340    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  oro_height    !< input variable for terrain height
341    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  topo_height   !< input variable for topography height
342
343    INTEGER( KIND=1 ), DIMENSION(:,:,:), ALLOCATABLE ::  topo_3d_read !< input variable for 3D topography
344
345!
346!-- Calculation of horizontal array bounds including ghost layers
347    nxlg = nxl - nbgp
348    nxrg = nxr + nbgp
349    nysg = nys - nbgp
350    nyng = nyn + nbgp
351
352!
353!-- Allocate grid arrays
354    ALLOCATE( ddzu(1:nzt+1), ddzw(1:nzt+1), dd2zu(1:nzt), dzu(1:nzt+1),        &
355              dzw(1:nzt+1), l_grid(1:nzt), zu(nzb:nzt+1), zw(nzb:nzt+1) )
356
357!
358!-- Compute height of u-levels from constant grid length and dz stretch factors
359    IF ( dz == -1.0_wp )  THEN
360       message_string = 'missing dz'
361       CALL message( 'init_grid', 'PA0200', 1, 2, 0, 6, 0 ) 
362    ELSEIF ( dz <= 0.0_wp )  THEN
363       WRITE( message_string, * ) 'dz=',dz,' <= 0.0'
364       CALL message( 'init_grid', 'PA0201', 1, 2, 0, 6, 0 )
365    ENDIF
366
367!
368!-- Define the vertical grid levels
369    IF ( .NOT. ocean )  THEN
370!
371!--    Grid for atmosphere with surface at z=0 (k=0, w-grid).
372!--    The second u-level (k=1) corresponds to the top of the
373!--    Prandtl-layer.
374
375       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2 ) THEN
376          zu(0) = 0.0_wp
377      !    zu(0) = - dz * 0.5_wp
378       ELSE
379          zu(0) = - dz * 0.5_wp
380       ENDIF
381       zu(1) =   dz * 0.5_wp
382
383       dz_stretch_level_index = nzt+1
384       dz_stretched = dz
385       DO  k = 2, nzt+1
386          IF ( dz_stretch_level <= zu(k-1)  .AND.  dz_stretched < dz_max )  THEN
387             dz_stretched = dz_stretched * dz_stretch_factor
388             dz_stretched = MIN( dz_stretched, dz_max )
389             IF ( dz_stretch_level_index == nzt+1 ) dz_stretch_level_index = k-1
390          ENDIF
391          zu(k) = zu(k-1) + dz_stretched
392       ENDDO
393
394!
395!--    Compute the w-levels. They are always staggered half-way between the
396!--    corresponding u-levels. In case of dirichlet bc for u and v at the
397!--    ground the first u- and w-level (k=0) are defined at same height (z=0).
398!--    The top w-level is extrapolated linearly.
399       zw(0) = 0.0_wp
400       DO  k = 1, nzt
401          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
402       ENDDO
403       zw(nzt+1) = zw(nzt) + 2.0_wp * ( zu(nzt+1) - zw(nzt) )
404
405    ELSE
406!
407!--    Grid for ocean with free water surface is at k=nzt (w-grid).
408!--    In case of neumann bc at the ground the first first u-level (k=0) lies
409!--    below the first w-level (k=0). In case of dirichlet bc the first u- and
410!--    w-level are defined at same height, but staggered from the second level.
411!--    The second u-level (k=1) corresponds to the top of the Prandtl-layer.
412       zu(nzt+1) =   dz * 0.5_wp
413       zu(nzt)   = - dz * 0.5_wp
414
415       dz_stretch_level_index = 0
416       dz_stretched = dz
417       DO  k = nzt-1, 0, -1
418!
419!--       The default value of dz_stretch_level is positive, thus the first
420!--       condition is always true. Hence, the second condition is necessary.
421          IF ( dz_stretch_level >= zu(k+1)  .AND.  dz_stretch_level <= 0.0  &
422               .AND.  dz_stretched < dz_max )  THEN
423             dz_stretched = dz_stretched * dz_stretch_factor
424             dz_stretched = MIN( dz_stretched, dz_max )
425             IF ( dz_stretch_level_index == 0 ) dz_stretch_level_index = k+1
426          ENDIF
427          zu(k) = zu(k+1) - dz_stretched
428       ENDDO
429
430!
431!--    Compute the w-levels. They are always staggered half-way between the
432!--    corresponding u-levels, except in case of dirichlet bc for u and v
433!--    at the ground. In this case the first u- and w-level are defined at
434!--    same height. The top w-level (nzt+1) is not used but set for
435!--    consistency, since w and all scalar variables are defined up tp nzt+1.
436       zw(nzt+1) = dz
437       zw(nzt)   = 0.0_wp
438       DO  k = 0, nzt
439          zw(k) = ( zu(k) + zu(k+1) ) * 0.5_wp
440       ENDDO
441
442!
443!--    In case of dirichlet bc for u and v the first u- and w-level are defined
444!--    at same height.
445       IF ( ibc_uv_b == 0 ) THEN
446          zu(0) = zw(0)
447       ENDIF
448
449    ENDIF
450
451!
452!-- Compute grid lengths.
453    DO  k = 1, nzt+1
454       dzu(k)  = zu(k) - zu(k-1)
455       ddzu(k) = 1.0_wp / dzu(k)
456       dzw(k)  = zw(k) - zw(k-1)
457       ddzw(k) = 1.0_wp / dzw(k)
458    ENDDO
459
460    DO  k = 1, nzt
461       dd2zu(k) = 1.0_wp / ( dzu(k) + dzu(k+1) )
462    ENDDO
463   
464!   
465!-- The FFT- SOR-pressure solvers assume grid spacings of a staggered grid
466!-- everywhere. For the actual grid, the grid spacing at the lowest level
467!-- is only dz/2, but should be dz. Therefore, an additional array
468!-- containing with appropriate grid information is created for these
469!-- solvers.
470    IF ( psolver(1:9) /= 'multigrid' )  THEN
471       ALLOCATE( ddzu_pres(1:nzt+1) )
472       ddzu_pres = ddzu
473       ddzu_pres(1) = ddzu_pres(2)  ! change for lowest level
474    ENDIF
475
476!
477!-- Compute the reciprocal values of the horizontal grid lengths.
478    ddx = 1.0_wp / dx
479    ddy = 1.0_wp / dy
480    dx2 = dx * dx
481    dy2 = dy * dy
482    ddx2 = 1.0_wp / dx2
483    ddy2 = 1.0_wp / dy2
484
485!
486!-- Compute the grid-dependent mixing length.
487    DO  k = 1, nzt
488       l_grid(k)  = ( dx * dy * dzw(k) )**0.33333333333333_wp
489    ENDDO
490
491!
492!-- Allocate outer and inner index arrays for topography and set
493!-- defaults.                   
494    ALLOCATE( nzb_s_inner(nysg:nyng,nxlg:nxrg),                             &
495              nzb_s_outer(nysg:nyng,nxlg:nxrg),                             &
496              nzb_u_inner(nysg:nyng,nxlg:nxrg),                             &
497              nzb_u_outer(nysg:nyng,nxlg:nxrg),                             &
498              nzb_v_inner(nysg:nyng,nxlg:nxrg),                             &
499              nzb_v_outer(nysg:nyng,nxlg:nxrg),                             &
500              nzb_w_inner(nysg:nyng,nxlg:nxrg),                             &
501              nzb_w_outer(nysg:nyng,nxlg:nxrg),                             &
502              nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                        &
503              nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                        &
504              nzb_local(nysg:nyng,nxlg:nxrg),                               &
505              nzb_tmp(nysg:nyng,nxlg:nxrg),                                 &
506              wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
507
508    ALLOCATE( topo_3d(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
509    topo_3d    = 0
510
511    ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
512
513    nzb_s_inner = nzb;  nzb_s_outer = nzb
514    nzb_u_inner = nzb;  nzb_u_outer = nzb
515    nzb_v_inner = nzb;  nzb_v_outer = nzb
516    nzb_w_inner = nzb;  nzb_w_outer = nzb
517
518!
519!-- Define vertical gridpoint from (or to) which on the usual finite difference
520!-- form (which does not use surface fluxes) is applied
521    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
522       nzb_diff = nzb + 2
523    ELSE
524       nzb_diff = nzb + 1
525    ENDIF
526
527    nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
528
529!
530!-- Initialize near-wall mixing length l_wall only in the vertical direction
531!-- for the moment,
532!-- multiplication with wall_adjustment_factor near the end of this routine
533    l_wall(nzb,:,:)   = l_grid(1)
534    DO  k = nzb+1, nzt
535       l_wall(k,:,:)  = l_grid(k)
536    ENDDO
537    l_wall(nzt+1,:,:) = l_grid(nzt)
538
539    DO  k = 1, nzt
540       IF ( l_grid(k) > 1.5_wp * dx * wall_adjustment_factor .OR.  &
541            l_grid(k) > 1.5_wp * dy * wall_adjustment_factor )  THEN
542          WRITE( message_string, * ) 'grid anisotropy exceeds ', &
543                                     'threshold given by only local', &
544                                     ' &horizontal reduction of near_wall ', &
545                                     'mixing length l_wall', &
546                                     ' &starting from height level k = ', k, '.'
547          CALL message( 'init_grid', 'PA0202', 0, 1, 0, 6, 0 )
548          EXIT
549       ENDIF
550    ENDDO
551!
552!-- Set outer and inner index arrays for non-flat topography.
553!-- Here consistency checks concerning domain size and periodicity are
554!-- necessary.
555!-- Within this SELECT CASE structure only nzb_local is initialized
556!-- individually depending on the chosen topography type, all other index
557!-- arrays are initialized further below.
558    SELECT CASE ( TRIM( topography ) )
559
560       CASE ( 'flat' )
561!
562!--       nzb_local is required for the multigrid solver
563          nzb_local = 0
564!
565!--       Initialilize 3D topography array, used later for initializing flags
566          topo_3d(nzb+1:nzt+1,:,:) = IBSET( topo_3d(nzb+1:nzt+1,:,:), 0 ) 
567!
568!--       level of detail is required for output routines
569          lod       = 1
570
571       CASE ( 'single_building' )
572!
573!--       Single rectangular building, by default centered in the middle of the
574!--       total domain
575          blx = NINT( building_length_x / dx )
576          bly = NINT( building_length_y / dy )
577          bh  = MINLOC( ABS( zw - building_height ), 1 ) - 1
578          IF ( ABS( zw(bh)   - building_height ) == &
579               ABS( zw(bh+1) - building_height )    )  bh = bh + 1
580
581          IF ( building_wall_left == 9999999.9_wp )  THEN
582             building_wall_left = ( nx + 1 - blx ) / 2 * dx
583          ENDIF
584          bxl = NINT( building_wall_left / dx )
585          bxr = bxl + blx
586
587          IF ( building_wall_south == 9999999.9_wp )  THEN
588             building_wall_south = ( ny + 1 - bly ) / 2 * dy
589          ENDIF
590          bys = NINT( building_wall_south / dy )
591          byn = bys + bly
592
593!
594!--       Building size has to meet some requirements
595          IF ( ( bxl < 1 ) .OR. ( bxr > nx-1 ) .OR. ( bxr < bxl+3 ) .OR.  &
596               ( bys < 1 ) .OR. ( byn > ny-1 ) .OR. ( byn < bys+3 ) )  THEN
597             WRITE( message_string, * ) 'inconsistent building parameters:',   &
598                                      '& bxl=', bxl, 'bxr=', bxr, 'bys=', bys, &
599                                      'byn=', byn, 'nx=', nx, 'ny=', ny
600             CALL message( 'init_grid', 'PA0203', 1, 2, 0, 6, 0 )
601          ENDIF
602
603!
604!--       Define the building.
605          nzb_local = 0
606          IF ( bxl <= nxr  .AND.  bxr >= nxl  .AND.                            &
607               bys <= nyn  .AND.  byn >= nys )                                 &       
608             nzb_local(MAX(nys,bys):MIN(nyn,byn),MAX(nxl,bxl):MIN(nxr,bxr)) = bh
609
610          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
611!
612!--       Set bit array to mask topography
613          DO  i = nxlg, nxrg
614             DO  j = nysg, nyng
615
616                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
617                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
618             ENDDO
619          ENDDO
620!
621!--       level of detail is required for output routines. Here, 2D topography.
622          lod = 1
623
624          CALL exchange_horiz_int( topo_3d, nbgp )
625
626       CASE ( 'single_street_canyon' )
627!
628!--       Single quasi-2D street canyon of infinite length in x or y direction.
629!--       The canyon is centered in the other direction by default.
630          IF ( canyon_width_x /= 9999999.9_wp )  THEN
631!
632!--          Street canyon in y direction
633             cwx = NINT( canyon_width_x / dx )
634             IF ( canyon_wall_left == 9999999.9_wp )  THEN
635                canyon_wall_left = ( nx + 1 - cwx ) / 2 * dx
636             ENDIF
637             cxl = NINT( canyon_wall_left / dx )
638             cxr = cxl + cwx
639
640          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
641!
642!--          Street canyon in x direction
643             cwy = NINT( canyon_width_y / dy )
644             IF ( canyon_wall_south == 9999999.9_wp )  THEN
645                canyon_wall_south = ( ny + 1 - cwy ) / 2 * dy
646             ENDIF
647             cys = NINT( canyon_wall_south / dy )
648             cyn = cys + cwy
649
650          ELSE
651             
652             message_string = 'no street canyon width given'
653             CALL message( 'init_grid', 'PA0204', 1, 2, 0, 6, 0 )
654 
655          ENDIF
656
657          ch  = MINLOC( ABS( zw - canyon_height ), 1 ) - 1
658          IF ( ABS( zw(ch)   - canyon_height ) == &
659               ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
660
661          dp_level_ind_b = ch
662!
663!--       Street canyon size has to meet some requirements
664          IF ( canyon_width_x /= 9999999.9_wp )  THEN
665             IF ( ( cxl < 1 ) .OR. ( cxr > nx-1 ) .OR. ( cwx < 3 ) .OR.        &
666               ( ch < 3 ) )  THEN
667                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
668                                           '&cxl=', cxl, 'cxr=', cxr,          &
669                                           'cwx=', cwx,                        &
670                                           'ch=', ch, 'nx=', nx, 'ny=', ny
671                CALL message( 'init_grid', 'PA0205', 1, 2, 0, 6, 0 ) 
672             ENDIF
673          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
674             IF ( ( cys < 1 ) .OR. ( cyn > ny-1 ) .OR. ( cwy < 3 ) .OR.        &
675               ( ch < 3 ) )  THEN
676                WRITE( message_string, * ) 'inconsistent canyon parameters:',  &
677                                           '&cys=', cys, 'cyn=', cyn,          &
678                                           'cwy=', cwy,                        &
679                                           'ch=', ch, 'nx=', nx, 'ny=', ny
680                CALL message( 'init_grid', 'PA0206', 1, 2, 0, 6, 0 ) 
681             ENDIF
682          ENDIF
683          IF ( canyon_width_x /= 9999999.9_wp .AND.                            &                 
684               canyon_width_y /= 9999999.9_wp )  THEN
685             message_string = 'inconsistent canyon parameters:' //             &   
686                              '&street canyon can only be oriented' //         &
687                              '&either in x- or in y-direction'
688             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
689          ENDIF
690
691          nzb_local = ch
692          IF ( canyon_width_x /= 9999999.9_wp )  THEN
693             IF ( cxl <= nxr  .AND.  cxr >= nxl )                              &
694                nzb_local(:,MAX(nxl,cxl+1):MIN(nxr,cxr-1)) = 0
695          ELSEIF ( canyon_width_y /= 9999999.9_wp )  THEN
696             IF ( cys <= nyn  .AND.  cyn >= nys )                              &         
697                nzb_local(MAX(nys,cys+1):MIN(nyn,cyn-1),:) = 0
698          ENDIF
699
700          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
701!
702!--       Set bit array to mask topography
703          DO  i = nxlg, nxrg
704             DO  j = nysg, nyng
705                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
706                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
707             ENDDO
708          ENDDO
709!
710!--       level of detail is required for output routines. Here, 2D topography.
711          lod = 1
712
713          CALL exchange_horiz_int( topo_3d, nbgp )
714
715       CASE ( 'tunnel' )
716
717!
718!--       Tunnel height
719          IF ( tunnel_height == 9999999.9_wp )  THEN
720             th = zw( INT( 0.2 * nz) )
721          ELSE
722             th = tunnel_height
723          ENDIF
724!
725!--       Tunnel-wall depth
726          IF ( tunnel_wall_depth == 9999999.9_wp )  THEN
727             td = MAX ( dx, dy, dz )
728          ELSE
729             td = tunnel_wall_depth
730          ENDIF
731!
732!--       Check for tunnel width
733          IF ( tunnel_width_x == 9999999.9_wp  .AND.                           &
734               tunnel_width_y == 9999999.9_wp  )  THEN
735             message_string = 'No tunnel width is given. '
736             CALL message( 'init_grid', 'PA0280', 1, 2, 0, 6, 0 )
737          ENDIF
738          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
739               tunnel_width_y /= 9999999.9_wp  )  THEN
740             message_string = 'Inconsistent tunnel parameters:' //             &   
741                              'tunnel can only be oriented' //                 &
742                              'either in x- or in y-direction.'
743             CALL message( 'init_grid', 'PA0281', 1, 2, 0, 6, 0 )
744          ENDIF
745!
746!--       Tunnel axis along y
747          IF ( tunnel_width_x /= 9999999.9_wp )  THEN
748             IF ( tunnel_width_x > ( nx + 1 ) * dx )  THEN
749                message_string = 'Tunnel width too large'
750                CALL message( 'init_grid', 'PA0282', 1, 2, 0, 6, 0 )
751             ENDIF
752
753             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_width_x * 0.5_wp )
754             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_width_x * 0.5_wp )
755             txs_in  = INT( ( nx + 1 ) * 0.5_wp * dx -                         &
756                                      ( tunnel_width_x * 0.5_wp - td ) )
757             txe_in  = INT( ( nx + 1 ) * 0.5_wp * dx +                         &
758                                      ( tunnel_width_x * 0.5_wp - td ) )
759
760             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_length * 0.5_wp )
761             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_length * 0.5_wp )
762             tys_in  = tys_out
763             tye_in  = tye_out
764          ENDIF
765          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
766               tunnel_width_x - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dx )  THEN
767             message_string = 'Tunnel width too small'
768             CALL message( 'init_grid', 'PA0175', 1, 2, 0, 6, 0 )
769          ENDIF
770          IF ( tunnel_width_y /= 9999999.9_wp  .AND.                           &
771               tunnel_width_y - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dy )  THEN
772             message_string = 'Tunnel width too small'
773             CALL message( 'init_grid', 'PA0455', 1, 2, 0, 6, 0 )
774          ENDIF
775!
776!--       Tunnel axis along x
777          IF ( tunnel_width_y /= 9999999.9_wp )  THEN
778             IF ( tunnel_width_y > ( ny + 1 ) * dy )  THEN
779                message_string = 'Tunnel width too large'
780                CALL message( 'init_grid', 'PA0456', 1, 2, 0, 6, 0 )
781             ENDIF
782
783             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_length * 0.5_wp )
784             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_length * 0.5_wp )
785             txs_in  = txs_out
786             txe_in  = txe_out
787
788             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_width_y * 0.5_wp )
789             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_width_y * 0.5_wp )
790             tys_in  = INT( ( ny + 1 ) * 0.5_wp * dy -                         &
791                                     ( tunnel_width_y * 0.5_wp - td ) )
792             tye_in  = INT( ( ny + 1 ) * 0.5_wp * dy +                         &
793                                     ( tunnel_width_y * 0.5_wp - td ) )
794          ENDIF
795
796          topo_3d = 0
797          DO  i = nxl, nxr
798             DO  j = nys, nyn
799!
800!--             Use heaviside function to model outer tunnel surface
801                hv_out = th * 0.5_wp *                                         &
802                              ( ( SIGN( 1.0_wp, i * dx - txs_out ) + 1.0_wp )  &
803                              - ( SIGN( 1.0_wp, i * dx - txe_out ) + 1.0_wp ) )
804
805                hv_out = hv_out * 0.5_wp *                                     &
806                            ( ( SIGN( 1.0_wp, j * dy - tys_out ) + 1.0_wp )    &
807                            - ( SIGN( 1.0_wp, j * dy - tye_out ) + 1.0_wp ) )
808!
809!--             Use heaviside function to model inner tunnel surface
810                hv_in  = ( th - td ) * 0.5_wp *                                &
811                                ( ( SIGN( 1.0_wp, i * dx - txs_in ) + 1.0_wp ) &
812                                - ( SIGN( 1.0_wp, i * dx - txe_in ) + 1.0_wp ) )
813
814                hv_in = hv_in * 0.5_wp *                                       &
815                                ( ( SIGN( 1.0_wp, j * dy - tys_in ) + 1.0_wp ) &
816                                - ( SIGN( 1.0_wp, j * dy - tye_in ) + 1.0_wp ) )
817!
818!--             Set flags at x-y-positions without any tunnel surface
819                IF ( hv_out - hv_in == 0.0_wp )  THEN
820                   topo_3d(nzb+1:nzt+1,j,i) = IBSET( topo_3d(nzb+1:nzt+1,j,i), 0 )
821!
822!--             Set flags at x-y-positions with tunnel surfaces
823                ELSE
824                   DO  k = nzb + 1, nzt + 1
825!
826!--                   Inner tunnel
827                      IF ( hv_out - hv_in == th )  THEN
828                         IF ( zw(k) <= hv_out )  THEN
829                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
830                         ELSE
831                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
832                         ENDIF
833                      ENDIF
834!
835!--                   Lateral tunnel walls
836                      IF ( hv_out - hv_in == td )  THEN
837                         IF ( zw(k) <= hv_in )  THEN
838                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
839                         ELSEIF ( zw(k) > hv_in  .AND.  zw(k) <= hv_out )  THEN
840                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
841                         ELSEIF ( zw(k) > hv_out )  THEN
842                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
843                         ENDIF
844                      ENDIF
845                   ENDDO
846                ENDIF
847             ENDDO
848          ENDDO
849
850          nzb_local = 0
851!
852!--       level of detail is required for output routines. Here, 3D topography.
853          lod = 2
854
855          CALL exchange_horiz_int( topo_3d, nbgp )
856
857       CASE ( 'read_from_file' )
858
859          ALLOCATE ( oro_height(nys:nyn,nxl:nxr)  )
860          ALLOCATE ( topo_height(nys:nyn,nxl:nxr) )
861          oro_height  = 0.0_wp
862          topo_height = 0.0_wp
863
864          DO  ii = 0, io_blocks-1
865             IF ( ii == io_group )  THEN
866
867!
868!--             Arbitrary irregular topography data in PALM format (exactly
869!--             matching the grid size and total domain size).
870!--             First, check if NetCDF file for topography exist or not.
871!--             This case, read topography from NetCDF, else read it from
872!--             ASCII file.
873#if defined ( __netcdf )
874                INQUIRE( FILE='TOPOGRAPHY_DATA_NC'//TRIM( coupling_char ),     &
875                         EXIST=netcdf_extend )
876!
877!--             NetCDF branch   
878                IF ( netcdf_extend )  THEN
879!
880!--                Open file in read-only mode
881                   CALL netcdf_open_read_file( 'TOPOGRAPHY_DATA_NC',           &
882                                               id_topo, 20 )  !Error number still need to be set properly
883
884!
885!--                Read terrain height. Reading is done PE-wise, i.e. each
886!--                processor reads its own domain. Reading is realized
887!--                via looping over x-dimension, i.e. calling
888!--                netcdf_get_variable reads topography along y for given x.
889!--                Orography is 2D.
890                   DO  i = nxl, nxr
891                      CALL netcdf_get_variable( id_topo, 'orography_0',        &
892                                                i, oro_height(:,i), 20 )  !Error number still need to be set properly
893                   ENDDO
894!
895!--                Read attribute lod (level of detail), required for variable
896!--                buildings_0
897                   CALL netcdf_get_attribute( id_topo, "lod", lod, .FALSE.,    &
898                                              20, 'buildings_0' ) !Error number still need to be set properly
899!
900!--                Read building height
901!--                2D for lod = 1, 3D for lod = 2
902                   IF ( lod == 1 )  THEN
903                      DO  i = nxl, nxr
904                         CALL netcdf_get_variable( id_topo, 'buildings_0',     &
905                                                   i, topo_height(:,i), 20 )  !Error number still need to be set properly
906                      ENDDO
907
908                   ELSEIF ( lod == 2 )  THEN
909!
910!--                   Allocate 1-byte integer dummy array to read 3D topography
911                      ALLOCATE( topo_3d_read(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
912!
913!--                   Read data PE-wise. Read yz-slices.
914                      DO  i = nxl, nxr
915                         DO  j = nys, nyn
916                            CALL netcdf_get_variable( id_topo, 'buildings_0',  &
917                                                      i, j, topo_3d_read(:,j,i), 20 )  !Error number still need to be set properly
918                         ENDDO
919                      ENDDO
920                   ELSE
921                      message_string = 'NetCDF attribute lod ' //              &
922                                       '(level of detail) is not set properly.'
923                      CALL message( 'init_grid', 'PA0457', 1, 2, 0, 6, 0 )
924                   ENDIF
925!
926!--                On file, 3D topography grid points is classified with 1,
927!--                atmosphere is classified with 0, contrary to the internal
928!--                treatment. Hence, conversion is required. Moreover, set
929!--                topography array to zero at lowest grid level.
930                   topo_3d = MERGE( 0, 1, topo_3d_read == 1 )
931                   topo_3d(nzb,:,:) = 0 
932!
933!--                Deallocate dummy array
934                   DEALLOCATE( topo_3d_read )
935!
936!--                Close topography input file
937                   CALL netcdf_close_file( id_topo, 20 )
938#endif
939
940!
941!--             ASCII branch. Please note, reading of 3D topography is not
942!--             supported in ASCII format. Further, no distinction is made
943!--             between orography and buildings
944                ELSE
945
946                   OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),    &
947                         STATUS='OLD', FORM='FORMATTED', ERR=10 )
948!
949!--                Read topography PE-wise. Rows are read from nyn to nys, columns
950!--                are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
951                   skip_n_rows = 0
952                   DO WHILE ( skip_n_rows < ny - nyn )
953                      READ( 90, * ) 
954                      skip_n_rows = skip_n_rows + 1
955                   ENDDO
956!
957!--                Read data from nyn to nys and nxl to nxr. Therefore, skip
958!--                column until nxl-1 is reached
959                   DO  j = nyn, nys, -1
960                      READ( 90, *, ERR=11, END=11 )                            &
961                                              ( dum, i = 0, nxl-1 ),           &
962                                              ( topo_height(j,i), i = nxl, nxr )
963                   ENDDO
964
965                   GOTO 12
966         
967 10                message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )// &
968                                    ' does not exist'
969                   CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
970
971 11                message_string = 'errors in file TOPOGRAPHY_DATA'//         &
972                                    TRIM( coupling_char )
973                   CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
974
975 12                CLOSE( 90 )
976
977                ENDIF
978
979             ENDIF
980#if defined( __parallel )
981             CALL MPI_BARRIER( comm2d, ierr )
982#endif
983          ENDDO
984
985!
986!--       Calculate the index height of the topography
987          nzb_local = 0
988          DO  i = nxl, nxr
989             DO  j = nys, nyn
990                IF ( .NOT. ocean )  THEN
991                   nzb_local(j,i) = MINLOC( ABS( zw - topo_height(j,i)         &
992                                                    - oro_height(j,i) ), 1 ) - 1
993                   IF ( ABS( zw(nzb_local(j,i)  ) - topo_height(j,i)           &
994                                                  - oro_height(j,i)  ) ==      &
995                        ABS( zw(nzb_local(j,i)+1) - topo_height(j,i)           &
996                                                  - oro_height(j,i)  )    )    &
997                      nzb_local(j,i) = nzb_local(j,i) + 1
998                ELSE
999                   nzb_local(j,i) = MINLOC( ABS( zw - zw(0)                    &
1000                                                    - topo_height(j,i)         &
1001                                                    - oro_height(j,i) ), 1 ) - 1
1002                   IF ( ABS( zw(nzb_local(j,i)  ) - zw(0)                      &
1003                                                  - topo_height(j,i)           &
1004                                                  - oro_height(j,i)  )  ==     &
1005                        ABS( zw(nzb_local(j,i)+1) - zw(0)                      &
1006                                                  - topo_height(j,i)           &
1007                                                  - oro_height(j,i)  )    )    &
1008                      nzb_local(j,i) = nzb_local(j,i) + 1
1009                ENDIF
1010
1011             ENDDO
1012          ENDDO
1013
1014          DEALLOCATE ( oro_height  )
1015          DEALLOCATE ( topo_height )
1016!
1017!--       Filter topography, i.e. fill holes resolved by only one grid point. 
1018!--       Such holes are suspected to lead to velocity blow-ups as continuity
1019!--       equation on discrete grid cannot be fulfilled in such case.
1020!--       For now, check only for holes and fill them to the lowest height level
1021!--       of the directly adjoining grid points along x- and y- direction.
1022!--       Before checking for holes, set lateral boundary conditions for
1023!--       topography. After hole-filling, boundary conditions must be set again!
1024          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
1025         
1026          IF ( .NOT. bc_ns_cyc )  THEN
1027             IF ( nys == 0  )  nzb_local(-1,:)   = nzb_local(0,:)
1028             IF ( nyn == ny )  nzb_local(ny+1,:) = nzb_local(ny,:)
1029          ENDIF
1030
1031          IF ( .NOT. bc_lr_cyc )  THEN
1032             IF ( nxl == 0  )  nzb_local(:,-1)   = nzb_local(:,0)
1033             IF ( nxr == nx )  nzb_local(:,nx+1) = nzb_local(:,nx)         
1034          ENDIF
1035
1036          num_hole_l = 0
1037          DO i = nxl, nxr
1038             DO j = nys, nyn
1039
1040                num_wall = 0
1041
1042                IF ( nzb_local(j-1,i) > nzb_local(j,i) )                       &
1043                   num_wall = num_wall + 1
1044                IF ( nzb_local(j+1,i) > nzb_local(j,i) )                       &
1045                   num_wall = num_wall + 1
1046                IF ( nzb_local(j,i-1) > nzb_local(j,i) )                       &
1047                   num_wall = num_wall + 1
1048                IF ( nzb_local(j,i+1) > nzb_local(j,i) )                       &
1049                   num_wall = num_wall + 1
1050
1051                IF ( num_wall == 4 )  THEN
1052                   nzb_local(j,i) = MIN( nzb_local(j-1,i), nzb_local(j+1,i),   &
1053                                         nzb_local(j,i-1), nzb_local(j,i+1) )
1054                   num_hole_l     = num_hole_l + 1
1055                ENDIF
1056             ENDDO
1057          ENDDO
1058!
1059!--       Count the total number of holes, required for informative message.
1060#if defined( __parallel )
1061          CALL MPI_ALLREDUCE( num_hole_l, num_hole, 1, MPI_INTEGER, MPI_SUM,   &
1062                              comm2d, ierr )
1063#else
1064          num_hole = num_hole_l
1065#endif   
1066!
1067!--       Create an informative message if any hole was removed.
1068          IF ( num_hole > 0 )  THEN
1069             WRITE( message_string, * ) num_hole, 'hole(s) resolved by only '//&
1070                                                  'one grid point were filled'
1071             CALL message( 'init_grid', 'PA0430', 0, 0, 0, 6, 0 )
1072          ENDIF
1073
1074!
1075!--       Set bit array to mask topography. Only required for lod = 1
1076          IF ( lod == 1 )  THEN
1077             DO  i = nxlg, nxrg
1078                DO  j = nysg, nyng
1079                   topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
1080                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
1081                ENDDO
1082             ENDDO
1083          ENDIF
1084!
1085!--       Exchange ghost-points, as well as add cyclic or Neumann boundary
1086!--       conditions.
1087          CALL exchange_horiz_int( topo_3d, nbgp )
1088          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
1089         
1090          IF ( .NOT. bc_ns_cyc )  THEN
1091             IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
1092             IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
1093
1094             IF ( nys == 0  )  nzb_local(-1,:)   = nzb_local(0,:)
1095             IF ( nyn == ny )  nzb_local(ny+1,:) = nzb_local(ny,:)
1096          ENDIF
1097
1098          IF ( .NOT. bc_lr_cyc )  THEN
1099             IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
1100             IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)     
1101
1102             IF ( nxl == 0  )  nzb_local(:,-1)   = nzb_local(:,0)
1103             IF ( nxr == nx )  nzb_local(:,nx+1) = nzb_local(:,nx)         
1104          ENDIF
1105
1106
1107       CASE DEFAULT
1108!
1109!--       The DEFAULT case is reached either if the parameter topography
1110!--       contains a wrong character string or if the user has defined a special
1111!--       case in the user interface. There, the subroutine user_init_grid
1112!--       checks which of these two conditions applies.
1113          CALL user_init_grid( topo_3d )
1114
1115    END SELECT
1116!
1117!-- Consistency checks and index array initialization are only required for
1118!-- non-flat topography, also the initialization of topography height arrays
1119!-- zu_s_inner and zw_w_inner
1120    IF ( TRIM( topography ) /= 'flat' )  THEN
1121#if defined( __parallel )
1122       CALL MPI_ALLREDUCE( MAXVAL( nzb_local ), nzb_local_max, 1, MPI_INTEGER, &
1123                           MPI_MAX, comm2d, ierr )
1124       CALL MPI_ALLREDUCE( MINVAL( nzb_local ), nzb_local_min, 1, MPI_INTEGER, &
1125                           MPI_MIN, comm2d, ierr )                           
1126#else
1127       nzb_local_max = MAXVAL( nzb_local )
1128       nzb_local_min = MINVAL( nzb_local )
1129#endif
1130
1131!
1132!--    Consistency checks
1133       IF ( nzb_local_min < 0  .OR.  nzb_local_max  > nz + 1 )  THEN
1134          WRITE( message_string, * ) 'nzb_local values are outside the',       &
1135                                'model domain',                                &
1136                                '&MINVAL( nzb_local ) = ', nzb_local_min,      &
1137                                '&MAXVAL( nzb_local ) = ', nzb_local_max
1138          CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
1139       ENDIF
1140!
1141!--    In case of non-flat topography, check whether the convention how to
1142!--    define the topography grid has been set correctly, or whether the default
1143!--    is applicable. If this is not possible, abort.
1144       IF ( TRIM( topography_grid_convention ) == ' ' )  THEN
1145          IF ( TRIM( topography ) /= 'single_building' .AND.                   &
1146               TRIM( topography ) /= 'single_street_canyon' .AND.              &
1147               TRIM( topography ) /= 'tunnel'  .AND.                           &
1148               TRIM( topography ) /= 'read_from_file')  THEN
1149!--          The default value is not applicable here, because it is only valid
1150!--          for the two standard cases 'single_building' and 'read_from_file'
1151!--          defined in init_grid.
1152             WRITE( message_string, * )                                        &
1153                  'The value for "topography_grid_convention" ',               &
1154                  'is not set. Its default value is & only valid for ',        &
1155                  '"topography" = ''single_building'', ',                      &
1156                  '''single_street_canyon'' & or ''read_from_file''.',         &
1157                  ' & Choose ''cell_edge'' or ''cell_center''.'
1158             CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
1159          ELSE
1160!--          The default value is applicable here.
1161!--          Set convention according to topography.
1162             IF ( TRIM( topography ) == 'single_building' .OR.                 &
1163                  TRIM( topography ) == 'single_street_canyon' )  THEN
1164                topography_grid_convention = 'cell_edge'
1165             ELSEIF ( TRIM( topography ) == 'read_from_file'  .OR.             &
1166                      TRIM( topography ) == 'tunnel')  THEN
1167                topography_grid_convention = 'cell_center'
1168             ENDIF
1169          ENDIF
1170       ELSEIF ( TRIM( topography_grid_convention ) /= 'cell_edge' .AND.        &
1171                TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
1172          WRITE( message_string, * )                                           &
1173               'The value for "topography_grid_convention" is ',               &
1174               'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
1175          CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
1176       ENDIF
1177
1178!
1179!--    In case of non-flat topography, check whether the convention how to
1180!--    define the topography grid has been set correctly, or whether the default
1181!--    is applicable. If this is not possible, abort.
1182       IF ( TRIM( topography_grid_convention ) == ' ' )  THEN
1183          IF ( TRIM( topography ) /= 'single_building' .AND.                   &
1184               TRIM( topography ) /= 'single_street_canyon' .AND.              &
1185               TRIM( topography ) /= 'read_from_file' )  THEN
1186!--          The default value is not applicable here, because it is only valid
1187!--          for the two standard cases 'single_building' and 'read_from_file'
1188!--          defined in init_grid.
1189             WRITE( message_string, * )                                        &
1190                  'The value for "topography_grid_convention" ',               &
1191                  'is not set. Its default value is & only valid for ',        &
1192                  '"topography" = ''single_building'', ',                      &
1193                  '''single_street_canyon'' & or ''read_from_file''.',         &
1194                  ' & Choose ''cell_edge'' or ''cell_center''.'
1195             CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
1196          ELSE
1197!--          The default value is applicable here.
1198!--          Set convention according to topography.
1199             IF ( TRIM( topography ) == 'single_building' .OR.                 &
1200                  TRIM( topography ) == 'single_street_canyon' )  THEN
1201                topography_grid_convention = 'cell_edge'
1202             ELSEIF ( TRIM( topography ) == 'read_from_file' )  THEN
1203                topography_grid_convention = 'cell_center'
1204             ENDIF
1205          ENDIF
1206       ELSEIF ( TRIM( topography_grid_convention ) /= 'cell_edge' .AND.        &
1207                TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
1208          WRITE( message_string, * )                                           &
1209               'The value for "topography_grid_convention" is ',               &
1210               'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
1211          CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
1212       ENDIF
1213
1214       IF ( topography_grid_convention == 'cell_edge' )  THEN
1215!
1216!--       The array nzb_local as defined using the 'cell_edge' convention
1217!--       describes the actual total size of topography which is defined at the
1218!--       cell edges where u=0 on the topography walls in x-direction and v=0
1219!--       on the topography walls in y-direction. However, PALM uses individual
1220!--       arrays nzb_u|v|w|s_inner|outer that are based on nzb_s_inner.
1221!--       Therefore, the extent of topography in nzb_local is now reduced by
1222!--       1dx at the E topography walls and by 1dy at the N topography walls
1223!--       to form the basis for nzb_s_inner.
1224!--       Note, the reverse memory access (i-j instead of j-i) is absolutely
1225!--       required at this point.
1226          DO  j = nys+1, nyn+1
1227             DO  i = nxl-1, nxr
1228                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j,i+1) )
1229             ENDDO
1230          ENDDO
1231!
1232!--       Exchange ghost points
1233          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
1234
1235          DO  i = nxl, nxr+1
1236             DO  j = nys-1, nyn
1237                nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) )
1238             ENDDO
1239          ENDDO 
1240!
1241!--       Exchange ghost points         
1242          CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
1243!
1244!--       Apply cell-edge convention also for 3D topo array. The former setting
1245!--       of nzb_local will be removed later.
1246          DO  j = nys+1, nyn+1
1247             DO  i = nxl-1, nxr
1248                DO  k = nzb, nzt+1
1249                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
1250                        BTEST( topo_3d(k,j,i+1), 0 ) )                         &
1251                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
1252                ENDDO
1253             ENDDO
1254          ENDDO     
1255          CALL exchange_horiz_int( topo_3d, nbgp )   
1256
1257          DO  i = nxl, nxr+1
1258             DO  j = nys-1, nyn
1259                DO  k = nzb, nzt+1
1260                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
1261                        BTEST( topo_3d(k,j+1,i), 0 ) )                         &
1262                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
1263                ENDDO
1264             ENDDO
1265          ENDDO 
1266          CALL exchange_horiz_int( topo_3d, nbgp )
1267   
1268       ENDIF
1269     
1270!
1271!--    Initialize index arrays nzb_s_inner and nzb_w_inner
1272
1273       nzb_s_inner = nzb_local
1274       nzb_w_inner = nzb_local
1275
1276!
1277!--    Initialize remaining index arrays:
1278!--    first pre-initialize them with nzb_s_inner...
1279       nzb_u_inner = nzb_s_inner
1280       nzb_u_outer = nzb_s_inner
1281       nzb_v_inner = nzb_s_inner
1282       nzb_v_outer = nzb_s_inner
1283       nzb_w_outer = nzb_s_inner
1284       nzb_s_outer = nzb_s_inner
1285
1286!
1287!--    ...then extend pre-initialized arrays in their according directions
1288!--    based on nzb_local using nzb_tmp as a temporary global index array
1289
1290!
1291!--    nzb_s_outer:
1292!--    extend nzb_local east-/westwards first, then north-/southwards
1293       nzb_tmp = nzb_local
1294       DO  j = nys, nyn
1295          DO  i = nxl, nxr
1296             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i),             &
1297                                 nzb_local(j,i+1) )
1298          ENDDO
1299       ENDDO
1300       
1301       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
1302       
1303       DO  i = nxl, nxr
1304          DO  j = nys, nyn
1305             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
1306                                     nzb_tmp(j+1,i) )
1307          ENDDO
1308!
1309!--       non-cyclic boundary conditions (overwritten by call of
1310!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
1311          IF ( nys == 0 )  THEN
1312             j = -1
1313             nzb_s_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
1314          ENDIF
1315          IF ( nyn == ny )  THEN
1316             j = ny + 1
1317             nzb_s_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
1318          ENDIF
1319       ENDDO
1320!
1321!--    nzb_w_outer:
1322!--    identical to nzb_s_outer
1323       nzb_w_outer = nzb_s_outer
1324
1325!
1326!--    nzb_u_inner:
1327!--    extend nzb_local rightwards only
1328       nzb_tmp = nzb_local
1329       DO  j = nys, nyn
1330          DO  i = nxl, nxr
1331             nzb_tmp(j,i) = MAX( nzb_local(j,i-1), nzb_local(j,i) )
1332          ENDDO
1333       ENDDO
1334       
1335       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )
1336       
1337       nzb_u_inner = nzb_tmp
1338!
1339!--    nzb_u_outer:
1340!--    extend current nzb_tmp (nzb_u_inner) north-/southwards
1341       DO  i = nxl, nxr
1342          DO  j = nys, nyn
1343             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i),             &
1344                                     nzb_tmp(j+1,i) )
1345          ENDDO
1346!
1347!--       non-cyclic boundary conditions (overwritten by call of
1348!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
1349          IF ( nys == 0 )  THEN
1350             j = -1
1351             nzb_u_outer(j,i) = MAX( nzb_tmp(j+1,i), nzb_tmp(j,i) )
1352          ENDIF
1353          IF ( nyn == ny )  THEN
1354             j = ny + 1
1355             nzb_u_outer(j,i) = MAX( nzb_tmp(j-1,i), nzb_tmp(j,i) )
1356          ENDIF
1357       ENDDO
1358
1359!
1360!--    nzb_v_inner:
1361!--    extend nzb_local northwards only
1362       nzb_tmp = nzb_local
1363       DO  i = nxl, nxr
1364          DO  j = nys, nyn
1365             nzb_tmp(j,i) = MAX( nzb_local(j-1,i), nzb_local(j,i) )
1366          ENDDO
1367       ENDDO
1368       
1369       CALL exchange_horiz_2d_int( nzb_tmp, nys, nyn, nxl, nxr, nbgp )     
1370       nzb_v_inner = nzb_tmp
1371
1372!
1373!--    nzb_v_outer:
1374!--    extend current nzb_tmp (nzb_v_inner) right-/leftwards
1375       DO  j = nys, nyn
1376          DO  i = nxl, nxr
1377             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i),             &
1378                                     nzb_tmp(j,i+1) )
1379          ENDDO
1380!
1381!--       non-cyclic boundary conditions (overwritten by call of
1382!--       exchange_horiz_2d_int below in case of cyclic boundary conditions)
1383          IF ( nxl == 0 )  THEN
1384             i = -1
1385             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i+1), nzb_tmp(j,i) )
1386          ENDIF
1387          IF ( nxr == nx )  THEN
1388             i = nx + 1
1389             nzb_v_outer(j,i) = MAX( nzb_tmp(j,i-1), nzb_tmp(j,i) )
1390          ENDIF
1391       ENDDO
1392
1393!
1394!--    Exchange of lateral boundary values (parallel computers) and cyclic
1395!--    boundary conditions, if applicable.
1396!--    Since nzb_s_inner and nzb_w_inner are derived directly from nzb_local
1397!--    they do not require exchange and are not included here.
1398       CALL exchange_horiz_2d_int( nzb_u_inner, nys, nyn, nxl, nxr, nbgp )
1399       CALL exchange_horiz_2d_int( nzb_u_outer, nys, nyn, nxl, nxr, nbgp )
1400       CALL exchange_horiz_2d_int( nzb_v_inner, nys, nyn, nxl, nxr, nbgp )
1401       CALL exchange_horiz_2d_int( nzb_v_outer, nys, nyn, nxl, nxr, nbgp )
1402       CALL exchange_horiz_2d_int( nzb_w_outer, nys, nyn, nxl, nxr, nbgp )
1403       CALL exchange_horiz_2d_int( nzb_s_outer, nys, nyn, nxl, nxr, nbgp )
1404
1405    ENDIF
1406!
1407!-- Deallocate temporary array, as it might be reused for different
1408!-- grid-levels further below.
1409    DEALLOCATE( nzb_tmp )
1410
1411!
1412!-- Determine the maximum level of topography. It is used for
1413!-- steering the degradation of order of the applied advection scheme.
1414!-- In case of non-cyclic lateral boundaries, the order of the advection
1415!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
1416    k_top = 0
1417    DO  i = nxl, nxr
1418       DO  j = nys, nyn
1419          DO  k = nzb, nzt + 1
1420             k_top = MAX( k_top, MERGE( k, 0,                                  &
1421                                        .NOT. BTEST( topo_3d(k,j,i), 0 ) ) )
1422          ENDDO
1423       ENDDO
1424    ENDDO
1425#if defined( __parallel )
1426    CALL MPI_ALLREDUCE( k_top + 1, nzb_max, 1, MPI_INTEGER,                    & !is +1 really necessary here?
1427                        MPI_MAX, comm2d, ierr )
1428#else
1429    nzb_max = k_top + 1
1430#endif
1431    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
1432         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
1433         nest_domain )                                                         &
1434    THEN
1435       nzb_max = nzt
1436    ENDIF
1437!
1438!-- Finally, if topography extents up to the model top, limit nzb_max to nzt.
1439    nzb_max = MIN( nzb_max, nzt ) 
1440
1441!
1442!-- Set the individual index arrays which define the k index from which on
1443!-- the usual finite difference form (which does not use surface fluxes) is
1444!-- applied
1445    IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1446       nzb_diff_s_inner   = nzb_s_inner + 2
1447       nzb_diff_s_outer   = nzb_s_outer + 2
1448    ELSE
1449       nzb_diff_s_inner   = nzb_s_inner + 1
1450       nzb_diff_s_outer   = nzb_s_outer + 1
1451    ENDIF
1452
1453!
1454!-- Set-up topography flags. First, set flags only for s, u, v and w-grid.
1455!-- Further special flags will be set in following loops.
1456    wall_flags_0 = 0
1457    DO  j = nys, nyn
1458       DO  i = nxl, nxr
1459          DO  k = nzb, nzt+1
1460!
1461!--          scalar grid
1462             IF ( BTEST( topo_3d(k,j,i), 0 ) )                                 &
1463                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
1464!
1465!--          v grid
1466             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
1467                  BTEST( topo_3d(k,j-1,i), 0 ) )                               &
1468                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
1469!
1470!--     To do: set outer arrays on basis of topo_3d array, adjust for downward-facing walls
1471!--          s grid outer array
1472             IF ( k >= nzb_s_outer(j,i) )                                      &
1473                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
1474!
1475!--          s grid outer array
1476             IF ( k >= nzb_u_outer(j,i) )                                      &
1477                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
1478!
1479!--          s grid outer array
1480             IF ( k >= nzb_v_outer(j,i) )                                      &
1481                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
1482!
1483!--          w grid outer array
1484             IF ( k >= nzb_w_outer(j,i) )                                      &
1485                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
1486          ENDDO
1487
1488          DO k = nzb, nzt
1489!
1490!--          w grid
1491             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
1492                  BTEST( topo_3d(k+1,j,i), 0 ) )                               &
1493                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
1494          ENDDO
1495          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 3 )
1496
1497       ENDDO
1498    ENDDO
1499!
1500!-- u grid. Note, reverse
1501!-- memory access is required for setting flag on u-grid
1502    DO  j = nys, nyn
1503       DO  i = nxl, nxr
1504          DO k = nzb, nzt+1
1505             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
1506                  BTEST( topo_3d(k,j,i-1), 0 ) )                               &
1507                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
1508          ENDDO
1509       ENDDO
1510    ENDDO
1511!
1512!-- Set further special flags
1513    DO i = nxl, nxr
1514       DO j = nys, nyn
1515          DO k = nzb, nzt+1
1516!
1517!--          scalar grid, former nzb_diff_s_inner.
1518!--          Note, use this flag also to mask topography in diffusion_u and
1519!--          diffusion_v along the vertical direction. In case of
1520!--          use_surface_fluxes, fluxes are calculated via MOST, else, simple
1521!--          gradient approach is applied. Please note, in case of u- and v-
1522!--          diffuison, a small error is made at edges (on the east side for u,
1523!--          at the north side for v), since topography on scalar grid point
1524!--          is used instead of topography on u/v-grid. As number of topography grid
1525!--          points on uv-grid is different than s-grid, different number of
1526!--          surface elements would be required. In order to avoid this,
1527!--          treat edges (u(k,j,i+1)) simply by a gradient approach, i.e. these
1528!--          points are not masked within diffusion_u. Tests had shown that the
1529!--          effect on the flow is negligible.
1530             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1531                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
1532                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
1533             ELSE
1534                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
1535             ENDIF
1536
1537          ENDDO
1538!
1539!--       Special flag to control vertical diffusion at model top - former
1540!--       nzt_diff
1541          wall_flags_0(:,j,i) = IBSET( wall_flags_0(:,j,i), 9 )
1542          IF ( use_top_fluxes )                                                &
1543             wall_flags_0(nzt:nzt+1,j,i) = IBCLR( wall_flags_0(nzt:nzt+1,j,i), 9 )
1544
1545          DO k = nzb+1, nzt
1546!
1547!--          Special flag on u grid, former nzb_u_inner + 1, required   
1548!--          for disturb_field and initialization. Do not disturb directly at
1549!--          topography, as well as initialize u with zero one grid point outside
1550!--          of topography.
1551             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
1552                  BTEST( wall_flags_0(k,j,i),   1 )  .AND.                     &
1553                  BTEST( wall_flags_0(k+1,j,i), 1 ) )                          &
1554                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
1555!
1556!--          Special flag on v grid, former nzb_v_inner + 1, required   
1557!--          for disturb_field and initialization. Do not disturb directly at
1558!--          topography, as well as initialize v with zero one grid point outside
1559!--          of topography.
1560             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
1561                  BTEST( wall_flags_0(k,j,i),   2 )  .AND.                     &
1562                  BTEST( wall_flags_0(k+1,j,i), 2 ) )                          &
1563                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
1564!
1565!--          Special flag on scalar grid, former nzb_s_inner+1. Used for
1566!--          lpm_sgs_tke
1567             IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                     &
1568                  BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
1569                  BTEST( wall_flags_0(k+1,j,i), 0 ) )                          &
1570                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
1571!
1572!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in
1573!--          in production_e
1574             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1575                IF ( BTEST( wall_flags_0(k,j,i),   24 )  .AND.                 &
1576                     BTEST( wall_flags_0(k-1,j,i), 24 )  .AND.                 &
1577                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
1578                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
1579             ELSE
1580                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
1581                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
1582             ENDIF
1583!
1584!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in
1585!--          in production_e
1586             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1587                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
1588                     BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                  &
1589                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
1590                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
1591             ELSE
1592                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
1593                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
1594             ENDIF
1595          ENDDO
1596!
1597!--       Flags indicating downward facing walls
1598          DO k = nzb+1, nzt
1599!
1600!--          Scalar grid
1601             IF ( BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
1602            .NOT. BTEST( wall_flags_0(k,j,i), 0   ) )                          & 
1603                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 ) 
1604!
1605!--          Downward facing wall on u grid
1606             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
1607            .NOT. BTEST( wall_flags_0(k,j,i), 1   ) )                          & 
1608                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
1609!
1610!--          Downward facing wall on v grid
1611             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
1612            .NOT. BTEST( wall_flags_0(k,j,i), 2   ) )                          & 
1613                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
1614!
1615!--          Downward facing wall on w grid
1616             IF ( BTEST( wall_flags_0(k-1,j,i), 3 )  .AND.                     &
1617            .NOT. BTEST( wall_flags_0(k,j,i), 3 ) )                            & 
1618                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
1619          ENDDO
1620!
1621!--       Flags indicating upward facing walls
1622          DO k = nzb, nzt
1623!
1624!--          Upward facing wall on scalar grid
1625             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   0 )  .AND.               &
1626                        BTEST( wall_flags_0(k+1,j,i), 0 ) )                    & 
1627                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
1628!
1629!--          Upward facing wall on u grid
1630             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   1 )  .AND.               &
1631                        BTEST( wall_flags_0(k+1,j,i), 1 ) )                    & 
1632                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
1633
1634!
1635!--          Upward facing wall on v grid
1636             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   2 )  .AND.               &
1637                        BTEST( wall_flags_0(k+1,j,i), 2 ) )                    & 
1638                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
1639
1640!
1641!--          Upward facing wall on w grid
1642             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   3 )  .AND.               &
1643                        BTEST( wall_flags_0(k+1,j,i), 3 ) )                    & 
1644                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
1645!
1646!--          Special flag on scalar grid, former nzb_s_inner
1647             IF ( BTEST( wall_flags_0(k,j,i), 0 )  .OR.                        &
1648                  BTEST( wall_flags_0(k,j,i), 12 ) .OR.                        &
1649                  BTEST( wall_flags_0(k,j,i), 13 ) )                           &
1650                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
1651!
1652!--          Special flag on scalar grid, nzb_diff_s_inner - 1, required for
1653!--          flow_statistics
1654             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
1655                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
1656                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
1657                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
1658             ELSE
1659                IF ( BTEST( wall_flags_0(k,j,i), 22 ) )                        &
1660                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
1661             ENDIF
1662
1663
1664          ENDDO
1665          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 22 )
1666          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 23 )
1667       ENDDO
1668    ENDDO
1669!
1670!-- Exchange ghost points for wall flags
1671    CALL exchange_horiz_int( wall_flags_0, nbgp )
1672!
1673!-- Set boundary conditions also for flags. Can be interpreted as Neumann
1674!-- boundary conditions for topography.
1675    IF ( .NOT. bc_ns_cyc )  THEN
1676       IF ( nys == 0  )  wall_flags_0(:,-1,:)   = wall_flags_0(:,0,:)
1677       IF ( nyn == ny )  wall_flags_0(:,ny+1,:) = wall_flags_0(:,ny,:)
1678    ENDIF
1679    IF ( .NOT. bc_lr_cyc )  THEN
1680       IF ( nxl == 0  )  wall_flags_0(:,:,-1)   = wall_flags_0(:,:,0)
1681       IF ( nxr == nx )  wall_flags_0(:,:,nx+1) = wall_flags_0(:,:,nx)           
1682    ENDIF
1683
1684!
1685!-- Initialize boundary conditions via surface type
1686    CALL init_bc
1687!
1688!-- Allocate and set topography height arrays required for data output
1689    IF ( TRIM( topography ) /= 'flat' )  THEN
1690!
1691!--    Allocate and set the arrays containing the topography height
1692
1693       IF ( nxr == nx  .AND.  nyn /= ny )  THEN
1694          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn),                             &
1695                    zw_w_inner(nxl:nxr+1,nys:nyn) )
1696       ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
1697          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn+1),                             &
1698                    zw_w_inner(nxl:nxr,nys:nyn+1) )
1699       ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
1700          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn+1),                           &
1701                    zw_w_inner(nxl:nxr+1,nys:nyn+1) )
1702       ELSE
1703          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn),                               &
1704                    zw_w_inner(nxl:nxr,nys:nyn) )
1705       ENDIF
1706
1707       zu_s_inner   = 0.0_wp
1708       zw_w_inner   = 0.0_wp
1709!
1710!--    Determine local topography height on scalar and w-grid. Note, setting
1711!--    lateral boundary values is not necessary, realized via wall_flags_0
1712!--    array. Further, please note that loop bounds are different from
1713!--    nxl to nxr and nys to nyn on south and right model boundary, hence,
1714!--    use intrinsic lbound and ubound functions to infer array bounds.
1715       DO  i = lbound(zu_s_inner, 1), ubound(zu_s_inner, 1)
1716          DO  j = lbound(zu_s_inner, 2), ubound(zu_s_inner, 2)
1717!
1718!--          Topography height on scalar grid. Therefore, determine index of
1719!--          upward-facing surface element on scalar grid (bit 12).
1720             zu_s_inner(i,j) = zu( MAXLOC( MERGE(                              &
1721                                         1, 0, BTEST( wall_flags_0(:,j,i), 12 )&
1722                                                ), DIM = 1                     &
1723                                         ) - 1                                 &
1724                                 )
1725!
1726!--          Topography height on w grid. Therefore, determine index of
1727!--          upward-facing surface element on w grid (bit 18).
1728             zw_w_inner(i,j) = zw( MAXLOC( MERGE(                              &
1729                                         1, 0, BTEST( wall_flags_0(:,j,i), 18 )&
1730                                                ), DIM = 1                     &
1731                                         ) - 1                                 &
1732                                 )
1733          ENDDO
1734       ENDDO
1735
1736
1737    ENDIF
1738
1739!
1740!-- Calculate wall flag arrays for the multigrid method.
1741!-- Please note, wall flags are only applied in the not cache-optimized
1742!-- version.
1743    IF ( psolver == 'multigrid_noopt' )  THEN
1744
1745!
1746!--    Gridpoint increment of the current level.
1747       inc = 1
1748       DO  l = maximum_grid_level, 1 , -1
1749!
1750!--       Set grid_level as it is required for exchange_horiz_2d_int
1751          grid_level = l
1752
1753          nxl_l = nxl_mg(l)
1754          nxr_l = nxr_mg(l)
1755          nys_l = nys_mg(l)
1756          nyn_l = nyn_mg(l)
1757          nzt_l = nzt_mg(l)
1758!
1759!--       Assign the flag level to be calculated
1760          SELECT CASE ( l )
1761             CASE ( 1 )
1762                flags => wall_flags_1
1763             CASE ( 2 )
1764                flags => wall_flags_2
1765             CASE ( 3 )
1766                flags => wall_flags_3
1767             CASE ( 4 )
1768                flags => wall_flags_4
1769             CASE ( 5 )
1770                flags => wall_flags_5
1771             CASE ( 6 )
1772                flags => wall_flags_6
1773             CASE ( 7 )
1774                flags => wall_flags_7
1775             CASE ( 8 )
1776                flags => wall_flags_8
1777             CASE ( 9 )
1778                flags => wall_flags_9
1779             CASE ( 10 )
1780                flags => wall_flags_10
1781          END SELECT
1782
1783!
1784!--       Depending on the grid level, set the respective bits in case of
1785!--       neighbouring walls
1786!--       Bit 0:  wall to the bottom
1787!--       Bit 1:  wall to the top (not realized in remaining PALM code so far)
1788!--       Bit 2:  wall to the south
1789!--       Bit 3:  wall to the north
1790!--       Bit 4:  wall to the left
1791!--       Bit 5:  wall to the right
1792!--       Bit 6:  inside building
1793
1794          flags = 0
1795
1796!
1797!--       In case of masking method, flags are not set and multigrid method
1798!--       works like FFT-solver
1799          IF ( .NOT. masking_method )  THEN
1800
1801!
1802!--          Allocate temporary array for topography heights on coarser grid
1803!--          level. Please note, 2 ghoist points are required, in order to
1804!--          calculate flags() on the interior ghost point.
1805             ALLOCATE( nzb_tmp(nys_l-2:nyn_l+2,nxl_l-2:nxr_l+2) )
1806             nzb_tmp = 0
1807             
1808             DO  i = nxl_l, nxr_l
1809                DO  j = nys_l, nyn_l
1810                   nzb_tmp(j,i) = nzb_local(j*inc,i*inc)
1811                ENDDO
1812             ENDDO
1813!
1814!--          Exchange ghost points on respective multigrid level. 2 ghost points
1815!--          are required, in order to calculate flags on
1816!--          nys_l-1 / nyn_l+1 / nxl_l-1 / nxr_l+1. The alternative would be to
1817!--          exchange 3D-INTEGER array flags on the respective multigrid level.
1818             CALL exchange_horiz_2d_int( nzb_tmp, nys_l, nyn_l, nxl_l, nxr_l, 2 )
1819!
1820!--          Set non-cyclic boundary conditions on respective multigrid level
1821             IF ( .NOT. bc_ns_cyc )  THEN
1822                IF ( inflow_s .OR. outflow_s .OR. nest_bound_s  )  THEN
1823                   nzb_tmp(-2,:) = nzb_tmp(0,:)
1824                   nzb_tmp(-1,:) = nzb_tmp(0,:)
1825                ENDIF
1826                IF ( inflow_n .OR. outflow_n .OR. nest_bound_n )  THEN
1827                   nzb_tmp(nyn_l+2,:) = nzb_tmp(nyn_l,:)
1828                   nzb_tmp(nyn_l+1,:) = nzb_tmp(nyn_l,:)
1829                ENDIF
1830             ENDIF
1831             IF ( .NOT. bc_lr_cyc )  THEN
1832                IF ( inflow_l .OR. outflow_l .OR. nest_bound_l  )  THEN
1833                   nzb_tmp(:,-2) = nzb_tmp(:,0)
1834                   nzb_tmp(:,-1) = nzb_tmp(:,0)
1835                ENDIF
1836                IF ( inflow_r .OR. outflow_r .OR. nest_bound_r )  THEN
1837                   nzb_tmp(:,nxr_l+1) = nzb_tmp(:,nxr_l)   
1838                   nzb_tmp(:,nxr_l+2) = nzb_tmp(:,nxr_l)     
1839                ENDIF       
1840             ENDIF
1841                       
1842             DO  i = nxl_l-1, nxr_l+1
1843                DO  j = nys_l-1, nyn_l+1
1844                   DO  k = nzb, nzt_l+1     
1845!
1846!--                   Inside/outside building (inside building does not need
1847!--                   further tests for walls)
1848                      IF ( k*inc <= nzb_tmp(j,i) )  THEN
1849
1850                         flags(k,j,i) = IBSET( flags(k,j,i), 6 )
1851
1852                      ELSE
1853!
1854!--                      Bottom wall
1855                         IF ( (k-1)*inc <= nzb_tmp(j,i) )  THEN
1856                            flags(k,j,i) = IBSET( flags(k,j,i), 0 )
1857                         ENDIF
1858!
1859!--                      South wall
1860                         IF ( k*inc <= nzb_tmp(j-1,i) )  THEN
1861                            flags(k,j,i) = IBSET( flags(k,j,i), 2 )
1862                         ENDIF
1863!
1864!--                      North wall
1865                         IF ( k*inc <= nzb_tmp(j+1,i) )  THEN
1866                            flags(k,j,i) = IBSET( flags(k,j,i), 3 )
1867                         ENDIF
1868!
1869!--                      Left wall
1870                         IF ( k*inc <= nzb_tmp(j,i-1) )  THEN
1871                            flags(k,j,i) = IBSET( flags(k,j,i), 4 )
1872                         ENDIF
1873!
1874!--                      Right wall
1875                         IF ( k*inc <= nzb_tmp(j,i+1) )  THEN
1876                            flags(k,j,i) = IBSET( flags(k,j,i), 5 )
1877                         ENDIF
1878
1879                      ENDIF
1880                           
1881                   ENDDO
1882                ENDDO
1883             ENDDO
1884
1885             DEALLOCATE( nzb_tmp )
1886
1887          ENDIF
1888
1889          inc = inc * 2
1890
1891       ENDDO
1892!
1893!--    Reset grid_level to "normal" grid
1894       grid_level = 0
1895       
1896    ENDIF
1897!
1898!-- Allocate flags needed for masking walls. Even though these flags are only
1899!-- required in the ws-scheme, the arrays need to be allocated here as they are
1900!-- used in OpenACC directives.
1901    ALLOCATE( advc_flags_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
1902              advc_flags_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
1903    advc_flags_1 = 0
1904    advc_flags_2 = 0
1905!
1906!-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e.
1907!-- to decrease the numerical stencil appropriately.
1908    IF ( momentum_advec == 'ws-scheme'  .OR.  scalar_advec == 'ws-scheme'      &
1909       )  THEN
1910       CALL ws_init_flags
1911    ENDIF
1912
1913!
1914!-- In case of topography: limit near-wall mixing length l_wall further:
1915!-- Go through all points of the subdomain one by one and look for the closest
1916!-- surface
1917    DO  i = nxl, nxr
1918       DO  j = nys, nyn
1919          DO  k = nzb+1, nzt
1920!
1921!--          Check if current gridpoint belongs to the atmosphere
1922             IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
1923!
1924!--             Check for neighbouring grid-points.
1925!--             Vertical distance, down
1926                IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )                 &
1927                   l_wall(k,j,i) = MIN( l_grid(k), zu(k) - zw(k-1) )
1928!
1929!--             Vertical distance, up
1930                IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )                 &
1931                   l_wall(k,j,i) = MIN( l_grid(k), zw(k) - zu(k) )
1932!
1933!--             y-distance
1934                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .OR.             &
1935                     .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                 &
1936                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dy )
1937!
1938!--             x-distance
1939                IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .OR.             &
1940                     .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                 &
1941                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dx )
1942!
1943!--              yz-distance (vertical edges, down)
1944                 IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i), 0 )  .OR.          &
1945                      .NOT. BTEST( wall_flags_0(k-1,j+1,i), 0 )  )             &
1946                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1947                                        SQRT( 0.25_wp * dy**2 +                &
1948                                       ( zu(k) - zw(k-1) )**2 ) )
1949!
1950!--              yz-distance (vertical edges, up)
1951                 IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i), 0 )  .OR.          &
1952                      .NOT. BTEST( wall_flags_0(k+1,j+1,i), 0 )  )             &
1953                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1954                                        SQRT( 0.25_wp * dy**2 +                &
1955                                       ( zw(k) - zu(k) )**2 ) )
1956!
1957!--              xz-distance (vertical edges, down)
1958                 IF ( .NOT. BTEST( wall_flags_0(k-1,j,i-1), 0 )  .OR.          &
1959                      .NOT. BTEST( wall_flags_0(k-1,j,i+1), 0 )  )             &
1960                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1961                                        SQRT( 0.25_wp * dx**2 +                &
1962                                       ( zu(k) - zw(k-1) )**2 ) )
1963!
1964!--              xz-distance (vertical edges, up)
1965                 IF ( .NOT. BTEST( wall_flags_0(k+1,j,i-1), 0 )  .OR.          &
1966                      .NOT. BTEST( wall_flags_0(k+1,j,i+1), 0 )  )             &
1967                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1968                                        SQRT( 0.25_wp * dx**2 +                &
1969                                       ( zw(k) - zu(k) )**2 ) )
1970!
1971!--             xy-distance (horizontal edges)
1972                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i-1), 0 )  .OR.           &
1973                     .NOT. BTEST( wall_flags_0(k,j+1,i-1), 0 )  .OR.           &
1974                     .NOT. BTEST( wall_flags_0(k,j-1,i+1), 0 )  .OR.           &
1975                     .NOT. BTEST( wall_flags_0(k,j+1,i+1), 0 ) )               &
1976                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1977                                        SQRT( 0.25_wp * ( dx**2 + dy**2 ) ) )
1978!
1979!--             xyz distance (vertical and horizontal edges, down)
1980                IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i-1), 0 )  .OR.         &
1981                     .NOT. BTEST( wall_flags_0(k-1,j+1,i-1), 0 )  .OR.         &
1982                     .NOT. BTEST( wall_flags_0(k-1,j-1,i+1), 0 )  .OR.         &
1983                     .NOT. BTEST( wall_flags_0(k-1,j+1,i+1), 0 ) )             &
1984                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1985                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
1986                                              +  ( zu(k) - zw(k-1) )**2  ) )
1987!
1988!--             xyz distance (vertical and horizontal edges, up)
1989                IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i-1), 0 )  .OR.         &
1990                     .NOT. BTEST( wall_flags_0(k+1,j+1,i-1), 0 )  .OR.         &
1991                     .NOT. BTEST( wall_flags_0(k+1,j-1,i+1), 0 )  .OR.         &
1992                     .NOT. BTEST( wall_flags_0(k+1,j+1,i+1), 0 ) )             &
1993                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
1994                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
1995                                              +  ( zw(k) - zu(k) )**2  ) )
1996                 
1997             ENDIF
1998          ENDDO
1999       ENDDO
2000    ENDDO
2001!
2002!-- Set lateral boundary conditions for l_wall
2003    CALL exchange_horiz( l_wall, nbgp )     
2004
2005
2006 END SUBROUTINE init_grid
Note: See TracBrowser for help on using the repository browser.