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

Last change on this file since 3139 was 3139, checked in by Giersch, 6 years ago

Bugfix in calculation of alpha_attack and Bugfix for restarts in combination with grid stretching

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