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

Last change on this file since 2955 was 2955, checked in by suehring, 6 years ago

Revise topography filter; extend checks for consistent setting of building ID and type; add cpu measures in netcdf-data input

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