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

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

New Inifor features: grid stretching, improved command-interface, support start dates in different formats in both YYYYMMDD and YYYYMMDDHH, Ability to manually control input file prefixes (--radiation-prefix, --soil-preifx, --flow-prefix, --soilmoisture-prefix) for compatiblity with DWD forcast naming scheme; GNU-style short and long option; Prepared output of large-scale forcing profiles (no computation yet); Added preprocessor flag netcdf4 to switch output format between netCDF 3 and 4; Updated netCDF variable names and attributes to comply with PIDS v1.9; Inifor bugfixes: Improved compatibility with older Intel Intel compilers by avoiding implicit array allocation; Added origin_lon/_lat values and correct reference time in dynamic driver global attributes; corresponding PALM changes: adjustments to revised Inifor; variables names in dynamic driver adjusted; enable geostrophic forcing also in offline nested mode; variable names in LES-LES and COSMO offline nesting changed; lateral boundary flags for nesting, in- and outflow conditions renamed

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