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

Last change on this file since 3873 was 3857, checked in by knoop, 5 years ago

Introduced 1D arrays for horizontal grid dimensions

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