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

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

last commit documented

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