source: palm/trunk/SOURCE/init_3d_model.f90 @ 2328

Last change on this file since 2328 was 2327, checked in by maronga, 7 years ago

temporary bugfix for calculation of air density

  • Property svn:keywords set to Id
  • Property svn:mergeinfo set to False
    /palm/branches/forwind/SOURCE/init_3d_model.f901564-1913
File size: 84.0 KB
Line 
1!> @file init_3d_model.f90
2!------------------------------------------------------------------------------!
3! This file is part of PALM.
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-2017 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: init_3d_model.f90 2327 2017-08-02 07:40:57Z maronga $
27! Temporary bugfix
28!
29! 2320 2017-07-21 12:47:43Z suehring
30! Modularize large-scale forcing and nudging
31!
32! 2292 2017-06-20 09:51:42Z schwenkel
33! Implementation of new microphysic scheme: cloud_scheme = 'morrison'
34! includes two more prognostic equations for cloud drop concentration (nc) 
35! and cloud water content (qc).
36!
37! 2277 2017-06-12 10:47:51Z kanani
38! Removed unused variable sums_up_fraction_l
39!
40! 2270 2017-06-09 12:18:47Z maronga
41! dots_num must be increased when LSM and/or radiation is used
42!
43! 2259 2017-06-08 09:09:11Z gronemeier
44! Implemented synthetic turbulence generator
45!
46! 2252 2017-06-07 09:35:37Z knoop
47! rho_air now depending on surface_pressure even in Boussinesq mode
48!
49! 2233 2017-05-30 18:08:54Z suehring
50!
51! 2232 2017-05-30 17:47:52Z suehring
52! Adjustments to new topography and surface concept:
53!   - Modify passed parameters for disturb_field
54!   - Topography representation via flags
55!   - Remove unused arrays.
56!   - Move initialization of surface-related quantities to surface_mod
57!
58! 2172 2017-03-08 15:55:25Z knoop
59! Bugfix: moved parallel random generator initialization into its module
60!
61! 2118 2017-01-17 16:38:49Z raasch
62! OpenACC directives removed
63!
64! 2037 2016-10-26 11:15:40Z knoop
65! Anelastic approximation implemented
66!
67! 2031 2016-10-21 15:11:58Z knoop
68! renamed variable rho to rho_ocean
69!
70! 2011 2016-09-19 17:29:57Z kanani
71! Flag urban_surface is now defined in module control_parameters.
72!
73! 2007 2016-08-24 15:47:17Z kanani
74! Added support for urban surface model,
75! adjusted location_message in case of plant_canopy
76!
77! 2000 2016-08-20 18:09:15Z knoop
78! Forced header and separation lines into 80 columns
79!
80! 1992 2016-08-12 15:14:59Z suehring
81! Initializaton of scalarflux at model top
82! Bugfixes in initialization of surface and top salinity flux, top scalar and
83! humidity fluxes
84!
85! 1960 2016-07-12 16:34:24Z suehring
86! Separate humidity and passive scalar
87! Increase dimension for mean_inflow_profiles
88! Remove inadvertent write-statement
89! Bugfix, large-scale forcing is still not implemented for passive scalars
90!
91! 1957 2016-07-07 10:43:48Z suehring
92! flight module added
93!
94! 1920 2016-05-30 10:50:15Z suehring
95! Initialize us with very small number to avoid segmentation fault during
96! calculation of Obukhov length
97!
98! 1918 2016-05-27 14:35:57Z raasch
99! intermediate_timestep_count is set 0 instead 1 for first call of pres,
100! bugfix: initialization of local sum arrays are moved to the beginning of the
101!         routine because otherwise results from pres are overwritten
102!
103! 1914 2016-05-26 14:44:07Z witha
104! Added initialization of the wind turbine model
105!
106! 1878 2016-04-19 12:30:36Z hellstea
107! The zeroth element of weight_pres removed as unnecessary
108!
109! 1849 2016-04-08 11:33:18Z hoffmann
110! Adapted for modularization of microphysics.
111! precipitation_amount, precipitation_rate, prr moved to arrays_3d.
112! Initialization of nc_1d, nr_1d, pt_1d, qc_1d, qr_1d, q_1d moved to
113! microphysics_init.
114!
115! 1845 2016-04-08 08:29:13Z raasch
116! nzb_2d replaced by nzb_u|v_inner
117!
118! 1833 2016-04-07 14:23:03Z raasch
119! initialization of spectra quantities moved to spectra_mod
120!
121! 1831 2016-04-07 13:15:51Z hoffmann
122! turbulence renamed collision_turbulence
123!
124! 1826 2016-04-07 12:01:39Z maronga
125! Renamed radiation calls.
126! Renamed canopy model calls.
127!
128! 1822 2016-04-07 07:49:42Z hoffmann
129! icloud_scheme replaced by microphysics_*
130!
131! 1817 2016-04-06 15:44:20Z maronga
132! Renamed lsm calls.
133!
134! 1815 2016-04-06 13:49:59Z raasch
135! zero-settings for velocities inside topography re-activated (was deactivated
136! in r1762)
137!
138! 1788 2016-03-10 11:01:04Z maronga
139! Added z0q.
140! Syntax layout improved.
141!
142! 1783 2016-03-06 18:36:17Z raasch
143! netcdf module name changed + related changes
144!
145! 1764 2016-02-28 12:45:19Z raasch
146! bugfix: increase size of volume_flow_area_l and volume_flow_initial_l by 1
147!
148! 1762 2016-02-25 12:31:13Z hellstea
149! Introduction of nested domain feature
150!
151! 1738 2015-12-18 13:56:05Z raasch
152! calculate mean surface level height for each statistic region
153!
154! 1734 2015-12-02 12:17:12Z raasch
155! no initial disturbances in case that the disturbance energy limit has been
156! set zero
157!
158! 1707 2015-11-02 15:24:52Z maronga
159! Bugfix: transfer of Richardson number from 1D model to Obukhov length caused
160! devision by zero in neutral stratification
161!
162! 1691 2015-10-26 16:17:44Z maronga
163! Call to init_surface_layer added. rif is replaced by ol and zeta.
164!
165! 1682 2015-10-07 23:56:08Z knoop
166! Code annotations made doxygen readable
167!
168! 1615 2015-07-08 18:49:19Z suehring
169! Enable turbulent inflow for passive_scalar and humidity
170!
171! 1585 2015-04-30 07:05:52Z maronga
172! Initialization of radiation code is now done after LSM initializtion
173!
174! 1575 2015-03-27 09:56:27Z raasch
175! adjustments for psolver-queries
176!
177! 1551 2015-03-03 14:18:16Z maronga
178! Allocation of land surface arrays is now done in the subroutine lsm_init_arrays,
179! which is part of land_surface_model.
180!
181! 1507 2014-12-10 12:14:18Z suehring
182! Bugfix: set horizontal velocity components to zero inside topography
183!
184! 1496 2014-12-02 17:25:50Z maronga
185! Added initialization of the land surface and radiation schemes
186!
187! 1484 2014-10-21 10:53:05Z kanani
188! Changes due to new module structure of the plant canopy model:
189! canopy-related initialization (e.g. lad and canopy_heat_flux) moved to new
190! subroutine init_plant_canopy within the module plant_canopy_model_mod,
191! call of subroutine init_plant_canopy added.
192!
193! 1431 2014-07-15 14:47:17Z suehring
194! var_d added, in order to normalize spectra.
195!
196! 1429 2014-07-15 12:53:45Z knoop
197! Ensemble run capability added to parallel random number generator
198!
199! 1411 2014-05-16 18:01:51Z suehring
200! Initial horizontal velocity profiles were not set to zero at the first vertical
201! grid level in case of non-cyclic lateral boundary conditions.
202!
203! 1406 2014-05-16 13:47:01Z raasch
204! bugfix: setting of initial velocities at k=1 to zero not in case of a
205! no-slip boundary condition for uv
206!
207! 1402 2014-05-09 14:25:13Z raasch
208! location messages modified
209!
210! 1400 2014-05-09 14:03:54Z knoop
211! Parallel random number generator added
212!
213! 1384 2014-05-02 14:31:06Z raasch
214! location messages added
215!
216! 1361 2014-04-16 15:17:48Z hoffmann
217! tend_* removed
218! Bugfix: w_subs is not allocated anymore if it is already allocated
219!
220! 1359 2014-04-11 17:15:14Z hoffmann
221! module lpm_init_mod added to use statements, because lpm_init has become a
222! module
223!
224! 1353 2014-04-08 15:21:23Z heinze
225! REAL constants provided with KIND-attribute
226!
227! 1340 2014-03-25 19:45:13Z kanani
228! REAL constants defined as wp-kind
229!
230! 1322 2014-03-20 16:38:49Z raasch
231! REAL constants defined as wp-kind
232! module interfaces removed
233!
234! 1320 2014-03-20 08:40:49Z raasch
235! ONLY-attribute added to USE-statements,
236! kind-parameters added to all INTEGER and REAL declaration statements,
237! kinds are defined in new module kinds,
238! revision history before 2012 removed,
239! comment fields (!:) to be used for variable explanations added to
240! all variable declaration statements
241!
242! 1316 2014-03-17 07:44:59Z heinze
243! Bugfix: allocation of w_subs
244!
245! 1299 2014-03-06 13:15:21Z heinze
246! Allocate w_subs due to extension of large scale subsidence in combination
247! with large scale forcing data (LSF_DATA)
248!
249! 1241 2013-10-30 11:36:58Z heinze
250! Overwrite initial profiles in case of nudging
251! Inititialize shf and qsws in case of large_scale_forcing
252!
253! 1221 2013-09-10 08:59:13Z raasch
254! +rflags_s_inner in copyin statement, use copyin for most arrays instead of
255! copy
256!
257! 1212 2013-08-15 08:46:27Z raasch
258! array tri is allocated and included in data copy statement
259!
260! 1195 2013-07-01 12:27:57Z heinze
261! Bugfix: move allocation of ref_state to parin.f90 and read_var_list.f90
262!
263! 1179 2013-06-14 05:57:58Z raasch
264! allocate and set ref_state to be used in buoyancy terms
265!
266! 1171 2013-05-30 11:27:45Z raasch
267! diss array is allocated with full size if accelerator boards are used
268!
269! 1159 2013-05-21 11:58:22Z fricke
270! -bc_lr_dirneu, bc_lr_neudir, bc_ns_dirneu, bc_ns_neudir
271!
272! 1153 2013-05-10 14:33:08Z raasch
273! diss array is allocated with dummy elements even if it is not needed
274! (required by PGI 13.4 / CUDA 5.0)
275!
276! 1115 2013-03-26 18:16:16Z hoffmann
277! unused variables removed
278!
279! 1113 2013-03-10 02:48:14Z raasch
280! openACC directive modified
281!
282! 1111 2013-03-08 23:54:10Z raasch
283! openACC directives added for pres
284! array diss allocated only if required
285!
286! 1092 2013-02-02 11:24:22Z raasch
287! unused variables removed
288!
289! 1065 2012-11-22 17:42:36Z hoffmann
290! allocation of diss (dissipation rate) in case of turbulence = .TRUE. added
291!
292! 1053 2012-11-13 17:11:03Z hoffmann
293! allocation and initialisation of necessary data arrays for the two-moment
294! cloud physics scheme the two new prognostic equations (nr, qr):
295! +dr, lambda_r, mu_r, sed_*, xr, *s, *sws, *swst, *, *_p, t*_m, *_1, *_2, *_3,
296! +tend_*, prr
297!
298! 1036 2012-10-22 13:43:42Z raasch
299! code put under GPL (PALM 3.9)
300!
301! 1032 2012-10-21 13:03:21Z letzel
302! save memory by not allocating pt_2 in case of neutral = .T.
303!
304! 1025 2012-10-07 16:04:41Z letzel
305! bugfix: swap indices of mask for ghost boundaries
306!
307! 1015 2012-09-27 09:23:24Z raasch
308! mask is set to zero for ghost boundaries
309!
310! 1010 2012-09-20 07:59:54Z raasch
311! cpp switch __nopointer added for pointer free version
312!
313! 1003 2012-09-14 14:35:53Z raasch
314! nxra,nyna, nzta replaced ny nxr, nyn, nzt
315!
316! 1001 2012-09-13 14:08:46Z raasch
317! all actions concerning leapfrog scheme removed
318!
319! 996 2012-09-07 10:41:47Z raasch
320! little reformatting
321!
322! 978 2012-08-09 08:28:32Z fricke
323! outflow damping layer removed
324! roughness length for scalar quantites z0h added
325! damping zone for the potential temperatur in case of non-cyclic lateral
326! boundaries added
327! initialization of ptdf_x, ptdf_y
328! initialization of c_u_m, c_u_m_l, c_v_m, c_v_m_l, c_w_m, c_w_m_l
329!
330! 849 2012-03-15 10:35:09Z raasch
331! init_particles renamed lpm_init
332!
333! 825 2012-02-19 03:03:44Z raasch
334! wang_collision_kernel renamed wang_kernel
335!
336! Revision 1.1  1998/03/09 16:22:22  raasch
337! Initial revision
338!
339!
340! Description:
341! ------------
342!> Allocation of arrays and initialization of the 3D model via
343!> a) pre-run the 1D model
344!> or
345!> b) pre-set constant linear profiles
346!> or
347!> c) read values of a previous run
348!> @todo fix crashes for runs with topography and density /= 1
349!------------------------------------------------------------------------------!
350 SUBROUTINE init_3d_model
351 
352
353    USE advec_ws
354
355    USE arrays_3d
356
357    USE cloud_parameters,                                                      &
358        ONLY:  cp, l_v, r_d
359
360    USE constants,                                                             &
361        ONLY:  pi
362   
363    USE control_parameters
364   
365    USE flight_mod,                                                            &
366        ONLY:  flight_init
367   
368    USE grid_variables,                                                        &
369        ONLY:  dx, dy, ddx2_mg, ddy2_mg
370   
371    USE indices
372
373    USE lpm_init_mod,                                                          &
374        ONLY:  lpm_init
375   
376    USE kinds
377
378    USE land_surface_model_mod,                                                &
379        ONLY:  lsm_init, lsm_init_arrays
380 
381    USE lsf_nudging_mod,                                                       &
382        ONLY:  lsf_init, ls_forcing_surf, nudge_init 
383
384    USE microphysics_mod,                                                      &
385        ONLY:  collision_turbulence, microphysics_init
386
387    USE model_1d,                                                              &
388        ONLY:  e1d, kh1d, km1d, l1d, rif1d, u1d, us1d, usws1d, v1d, vsws1d 
389   
390    USE netcdf_interface,                                                      &
391        ONLY:  dots_max, dots_num
392   
393    USE particle_attributes,                                                   &
394        ONLY:  particle_advection, use_sgs_for_particles, wang_kernel
395   
396    USE pegrid
397   
398    USE plant_canopy_model_mod,                                                &
399        ONLY:  pcm_init, plant_canopy
400
401    USE radiation_model_mod,                                                   &
402        ONLY:  radiation_init, radiation, radiation_scheme
403   
404    USE random_function_mod 
405   
406    USE random_generator_parallel,                                             &
407        ONLY:  init_parallel_random_generator
408   
409    USE statistics,                                                            &
410        ONLY:  hom, hom_sum, mean_surface_level_height, pr_palm, rmask,        &
411               statistic_regions, sums, sums_divnew_l, sums_divold_l, sums_l,  &
412               sums_l_l, sums_wsts_bc_l, ts_value,                             &
413               weight_pres, weight_substep
414
415    USE synthetic_turbulence_generator_mod,                                    &
416        ONLY:  stg_init, use_synthetic_turbulence_generator
417
418    USE surface_layer_fluxes_mod,                                              &
419        ONLY:  init_surface_layer_fluxes
420
421    USE surface_mod,                                                           &
422        ONLY :  init_surface_arrays, init_surfaces, surf_def_h, surf_lsm_h,    &
423                surf_usm_h
424   
425    USE transpose_indices
426
427    USE urban_surface_mod,                                                     &
428        ONLY:  usm_init_urban_surface
429
430    USE wind_turbine_model_mod,                                                &
431        ONLY:  wtm_init, wtm_init_arrays, wind_turbine
432
433    IMPLICIT NONE
434
435    INTEGER(iwp) ::  i             !<
436    INTEGER(iwp) ::  ind_array(1)  !<
437    INTEGER(iwp) ::  j             !<
438    INTEGER(iwp) ::  k             !<
439    INTEGER(iwp) ::  k_surf        !< surface level index
440    INTEGER(iwp) ::  m             !< index of surface element in surface data type
441    INTEGER(iwp) ::  sr            !< index of statistic region
442
443    INTEGER(iwp), DIMENSION(:), ALLOCATABLE   ::  ngp_2dh_l  !<
444
445    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_outer_l    !<
446    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  ngp_2dh_s_inner_l  !<
447
448    REAL(wp)     ::  t_surface !< air temperature at the surface
449
450    REAL(wp), DIMENSION(:), ALLOCATABLE ::  p_hydrostatic !< hydrostatic pressure
451
452    INTEGER(iwp) ::  l       !< loop variable
453    INTEGER(iwp) ::  nzt_l   !< index of top PE boundary for multigrid level
454    REAL(wp) ::  dx_l !< grid spacing along x on different multigrid level
455    REAL(wp) ::  dy_l !< grid spacing along y on different multigrid level
456
457    REAL(wp), DIMENSION(1:3) ::  volume_flow_area_l     !<
458    REAL(wp), DIMENSION(1:3) ::  volume_flow_initial_l  !<
459
460    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mean_surface_level_height_l    !<
461    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner_l    !<
462    REAL(wp), DIMENSION(:), ALLOCATABLE ::  ngp_3d_inner_tmp  !<
463
464
465    CALL location_message( 'allocating arrays', .FALSE. )
466!
467!-- Allocate arrays
468    ALLOCATE( mean_surface_level_height(0:statistic_regions),                  &
469              mean_surface_level_height_l(0:statistic_regions),                &
470              ngp_2dh(0:statistic_regions), ngp_2dh_l(0:statistic_regions),    &
471              ngp_3d(0:statistic_regions),                                     &
472              ngp_3d_inner(0:statistic_regions),                               &
473              ngp_3d_inner_l(0:statistic_regions),                             &
474              ngp_3d_inner_tmp(0:statistic_regions),                           &
475              sums_divnew_l(0:statistic_regions),                              &
476              sums_divold_l(0:statistic_regions) )
477    ALLOCATE( dp_smooth_factor(nzb:nzt), rdf(nzb+1:nzt), rdf_sc(nzb+1:nzt) )
478    ALLOCATE( ngp_2dh_outer(nzb:nzt+1,0:statistic_regions),                    &
479              ngp_2dh_outer_l(nzb:nzt+1,0:statistic_regions),                  &
480              ngp_2dh_s_inner(nzb:nzt+1,0:statistic_regions),                  &
481              ngp_2dh_s_inner_l(nzb:nzt+1,0:statistic_regions),                &
482              rmask(nysg:nyng,nxlg:nxrg,0:statistic_regions),                  &
483              sums(nzb:nzt+1,pr_palm+max_pr_user),                             &
484              sums_l(nzb:nzt+1,pr_palm+max_pr_user,0:threads_per_task-1),      &
485              sums_l_l(nzb:nzt+1,0:statistic_regions,0:threads_per_task-1),    &
486              sums_wsts_bc_l(nzb:nzt+1,0:statistic_regions),                   &
487              ts_value(dots_max,0:statistic_regions) )
488    ALLOCATE( ptdf_x(nxlg:nxrg), ptdf_y(nysg:nyng) )
489
490    ALLOCATE( d(nzb+1:nzt,nys:nyn,nxl:nxr),                                    &
491              kh(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                               &
492              km(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                               &
493              p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                &
494              tend(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
495
496#if defined( __nopointer )
497    ALLOCATE( e(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                &
498              e_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
499              pt(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                               &
500              pt_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
501              u(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                &
502              u_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
503              v(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                &
504              v_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
505              w(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                                &
506              w_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
507              te_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
508              tpt_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                            &
509              tu_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
510              tv_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
511              tw_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
512#else
513    ALLOCATE( e_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
514              e_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
515              e_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
516              pt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
517              pt_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
518              u_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
519              u_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
520              u_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
521              v_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
522              v_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
523              v_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
524              w_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
525              w_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                              &
526              w_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
527    IF (  .NOT.  neutral )  THEN
528       ALLOCATE( pt_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
529    ENDIF
530#endif
531
532!
533!-- Following array is required for perturbation pressure within the iterative
534!-- pressure solvers. For the multistep schemes (Runge-Kutta), array p holds
535!-- the weighted average of the substeps and cannot be used in the Poisson
536!-- solver.
537    IF ( psolver == 'sor' )  THEN
538       ALLOCATE( p_loc(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
539    ELSEIF ( psolver(1:9) == 'multigrid' )  THEN
540!
541!--    For performance reasons, multigrid is using one ghost layer only
542       ALLOCATE( p_loc(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) )
543    ENDIF
544
545!
546!-- Array for storing constant coeffficients of the tridiagonal solver
547    IF ( psolver == 'poisfft' )  THEN
548       ALLOCATE( tri(nxl_z:nxr_z,nys_z:nyn_z,0:nz-1,2) )
549       ALLOCATE( tric(nxl_z:nxr_z,nys_z:nyn_z,0:nz-1) )
550    ENDIF
551
552    IF ( humidity )  THEN
553!
554!--    3D-humidity
555#if defined( __nopointer )
556       ALLOCATE( q(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
557                 q_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
558                 tq_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
559#else
560       ALLOCATE( q_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
561                 q_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
562                 q_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
563#endif
564
565!
566!--    3D-arrays needed for humidity
567       IF ( humidity )  THEN
568#if defined( __nopointer )
569          ALLOCATE( vpt(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
570#else
571          ALLOCATE( vpt_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
572#endif
573
574          IF ( cloud_physics )  THEN
575!
576!--          Liquid water content
577#if defined( __nopointer )
578             ALLOCATE ( ql(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
579#else
580             ALLOCATE ( ql_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
581#endif
582
583!
584!--          3D-cloud water content
585             IF ( .NOT. microphysics_morrison )  THEN
586#if defined( __nopointer )
587                ALLOCATE( qc(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
588#else
589                ALLOCATE( qc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
590#endif
591             ENDIF
592!
593!--          Precipitation amount and rate (only needed if output is switched)
594             ALLOCATE( precipitation_amount(nysg:nyng,nxlg:nxrg),              &
595                       precipitation_rate(nysg:nyng,nxlg:nxrg) )
596
597!
598!--          3d-precipitation rate
599             ALLOCATE( prr(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
600
601             IF ( microphysics_morrison )  THEN
602!
603!--             3D-cloud drop water content, cloud drop concentration arrays
604#if defined( __nopointer )
605                ALLOCATE( nc(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                &
606                          nc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
607                          qc(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                &
608                          qc_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
609                          tnc_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) 
610                          tqc_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
611#else
612                ALLOCATE( nc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
613                          nc_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
614                          nc_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
615                          qc_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
616                          qc_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
617                          qc_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
618#endif
619             ENDIF
620
621             IF ( microphysics_seifert )  THEN
622!
623!--             3D-rain water content, rain drop concentration arrays
624#if defined( __nopointer )
625                ALLOCATE( nr(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                &
626                          nr_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
627                          qr(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                &
628                          qr_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
629                          tnr_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg),             &
630                          tqr_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
631#else
632                ALLOCATE( nr_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
633                          nr_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
634                          nr_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
635                          qr_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
636                          qr_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),              &
637                          qr_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
638#endif
639             ENDIF
640
641          ENDIF
642
643          IF ( cloud_droplets )  THEN
644!
645!--          Liquid water content, change in liquid water content
646#if defined( __nopointer )
647             ALLOCATE ( ql(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
648                        ql_c(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
649#else
650             ALLOCATE ( ql_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                   &
651                        ql_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
652#endif
653!
654!--          Real volume of particles (with weighting), volume of particles
655             ALLOCATE ( ql_v(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                   &
656                        ql_vp(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
657          ENDIF
658
659       ENDIF
660
661    ENDIF
662   
663   
664    IF ( passive_scalar )  THEN
665
666!
667!--    3D scalar arrays
668#if defined( __nopointer )
669       ALLOCATE( s(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                             &
670                 s_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
671                 ts_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
672#else
673       ALLOCATE( s_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
674                 s_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
675                 s_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
676#endif
677    ENDIF
678
679    IF ( ocean )  THEN
680#if defined( __nopointer )
681       ALLOCATE( prho(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                          &
682                 rho_ocean(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                           &
683                 sa(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                            &
684                 sa_p(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                          &
685                 tsa_m(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
686#else
687       ALLOCATE( prho_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                        &
688                 rho_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                         &
689                 sa_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                          &
690                 sa_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                          &
691                 sa_3(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
692       prho => prho_1
693       rho_ocean  => rho_1  ! routines calc_mean_profile and diffusion_e require
694                      ! density to be apointer
695#endif
696    ENDIF
697
698!
699!-- Allocation of anelastic and Boussinesq approximation specific arrays
700    ALLOCATE( p_hydrostatic(nzb:nzt+1) )
701    ALLOCATE( rho_air(nzb:nzt+1) )
702    ALLOCATE( rho_air_zw(nzb:nzt+1) )
703    ALLOCATE( drho_air(nzb:nzt+1) )
704    ALLOCATE( drho_air_zw(nzb:nzt+1) )
705
706!
707!-- Density profile calculation for anelastic approximation
708    t_surface = pt_surface * ( surface_pressure / 1000.0_wp )**( r_d / cp )
709    IF ( TRIM( approximation ) == 'anelastic' ) THEN
710       DO  k = nzb, nzt+1
711          p_hydrostatic(k)    = surface_pressure * 100.0_wp *                  &
712                                ( 1 - ( g * zu(k) ) / ( cp * t_surface )       &
713                                )**( cp / r_d )
714          rho_air(k)          = ( p_hydrostatic(k) *                           &
715                                  ( 100000.0_wp / p_hydrostatic(k)             &
716                                  )**( r_d / cp )                              &
717                                ) / ( r_d * pt_init(k) )
718       ENDDO
719       DO  k = nzb, nzt
720          rho_air_zw(k) = 0.5_wp * ( rho_air(k) + rho_air(k+1) )
721       ENDDO
722       rho_air_zw(nzt+1)  = rho_air_zw(nzt)                                    &
723                            + 2.0_wp * ( rho_air(nzt+1) - rho_air_zw(nzt)  )
724    ELSE
725       DO  k = nzb, nzt+1
726          p_hydrostatic(k)    = surface_pressure * 100.0_wp *                  &
727                                ( 1 - ( g * zu(nzb) ) / ( cp * t_surface )       &
728                                )**( cp / r_d )
729          rho_air(k)          = ( p_hydrostatic(k) *                           &
730                                  ( 100000.0_wp / p_hydrostatic(k)             &
731                                  )**( r_d / cp )                              &
732                                ) / ( r_d * pt_init(nzb) )
733       ENDDO
734       DO  k = nzb, nzt
735          rho_air_zw(k) = 0.5_wp * ( rho_air(k) + rho_air(k+1) )
736       ENDDO
737       rho_air_zw(nzt+1)  = rho_air_zw(nzt)                                    &
738                            + 2.0_wp * ( rho_air(nzt+1) - rho_air_zw(nzt)  )
739
740!
741!--    Temporary workaround as runs with topography do not work with density /= 1
742       rho_air    = 1.0_wp
743       rho_air_zw = 1.0_wp
744
745    ENDIF
746
747
748
749
750!-- compute the inverse density array in order to avoid expencive divisions
751    drho_air    = 1.0_wp / rho_air
752    drho_air_zw = 1.0_wp / rho_air_zw
753
754!
755!-- Allocation of flux conversion arrays
756    ALLOCATE( heatflux_input_conversion(nzb:nzt+1) )
757    ALLOCATE( waterflux_input_conversion(nzb:nzt+1) )
758    ALLOCATE( momentumflux_input_conversion(nzb:nzt+1) )
759    ALLOCATE( heatflux_output_conversion(nzb:nzt+1) )
760    ALLOCATE( waterflux_output_conversion(nzb:nzt+1) )
761    ALLOCATE( momentumflux_output_conversion(nzb:nzt+1) )
762
763!
764!-- calculate flux conversion factors according to approximation and in-/output mode
765    DO  k = nzb, nzt+1
766
767        IF ( TRIM( flux_input_mode ) == 'kinematic' )  THEN
768            heatflux_input_conversion(k)      = rho_air_zw(k)
769            waterflux_input_conversion(k)     = rho_air_zw(k)
770            momentumflux_input_conversion(k)  = rho_air_zw(k)
771        ELSEIF ( TRIM( flux_input_mode ) == 'dynamic' ) THEN
772            heatflux_input_conversion(k)      = 1.0_wp / cp
773            waterflux_input_conversion(k)     = 1.0_wp / l_v
774            momentumflux_input_conversion(k)  = 1.0_wp
775        ENDIF
776
777        IF ( TRIM( flux_output_mode ) == 'kinematic' )  THEN
778            heatflux_output_conversion(k)     = drho_air_zw(k)
779            waterflux_output_conversion(k)    = drho_air_zw(k)
780            momentumflux_output_conversion(k) = drho_air_zw(k)
781        ELSEIF ( TRIM( flux_output_mode ) == 'dynamic' ) THEN
782            heatflux_output_conversion(k)     = cp
783            waterflux_output_conversion(k)    = l_v
784            momentumflux_output_conversion(k) = 1.0_wp
785        ENDIF
786
787        IF ( .NOT. humidity ) THEN
788            waterflux_input_conversion(k)  = 1.0_wp
789            waterflux_output_conversion(k) = 1.0_wp
790        ENDIF
791
792    ENDDO
793
794!
795!-- In case of multigrid method, compute grid lengths and grid factors for the
796!-- grid levels with respective density on each grid
797    IF ( psolver(1:9) == 'multigrid' )  THEN
798
799       ALLOCATE( ddx2_mg(maximum_grid_level) )
800       ALLOCATE( ddy2_mg(maximum_grid_level) )
801       ALLOCATE( dzu_mg(nzb+1:nzt+1,maximum_grid_level) )
802       ALLOCATE( dzw_mg(nzb+1:nzt+1,maximum_grid_level) )
803       ALLOCATE( f1_mg(nzb+1:nzt,maximum_grid_level) )
804       ALLOCATE( f2_mg(nzb+1:nzt,maximum_grid_level) )
805       ALLOCATE( f3_mg(nzb+1:nzt,maximum_grid_level) )
806       ALLOCATE( rho_air_mg(nzb:nzt+1,maximum_grid_level) )
807       ALLOCATE( rho_air_zw_mg(nzb:nzt+1,maximum_grid_level) )
808
809       dzu_mg(:,maximum_grid_level) = dzu
810       rho_air_mg(:,maximum_grid_level) = rho_air
811!       
812!--    Next line to ensure an equally spaced grid.
813       dzu_mg(1,maximum_grid_level) = dzu(2)
814       rho_air_mg(nzb,maximum_grid_level) = rho_air(nzb) +                     &
815                                             (rho_air(nzb) - rho_air(nzb+1))
816
817       dzw_mg(:,maximum_grid_level) = dzw
818       rho_air_zw_mg(:,maximum_grid_level) = rho_air_zw
819       nzt_l = nzt
820       DO  l = maximum_grid_level-1, 1, -1
821           dzu_mg(nzb+1,l) = 2.0_wp * dzu_mg(nzb+1,l+1)
822           dzw_mg(nzb+1,l) = 2.0_wp * dzw_mg(nzb+1,l+1)
823           rho_air_mg(nzb,l)    = rho_air_mg(nzb,l+1) + (rho_air_mg(nzb,l+1) - rho_air_mg(nzb+1,l+1))
824           rho_air_zw_mg(nzb,l) = rho_air_zw_mg(nzb,l+1) + (rho_air_zw_mg(nzb,l+1) - rho_air_zw_mg(nzb+1,l+1))
825           rho_air_mg(nzb+1,l)    = rho_air_mg(nzb+1,l+1)
826           rho_air_zw_mg(nzb+1,l) = rho_air_zw_mg(nzb+1,l+1)
827           nzt_l = nzt_l / 2
828           DO  k = 2, nzt_l+1
829              dzu_mg(k,l) = dzu_mg(2*k-2,l+1) + dzu_mg(2*k-1,l+1)
830              dzw_mg(k,l) = dzw_mg(2*k-2,l+1) + dzw_mg(2*k-1,l+1)
831              rho_air_mg(k,l)    = rho_air_mg(2*k-1,l+1)
832              rho_air_zw_mg(k,l) = rho_air_zw_mg(2*k-1,l+1)
833           ENDDO
834       ENDDO
835
836       nzt_l = nzt
837       dx_l  = dx
838       dy_l  = dy
839       DO  l = maximum_grid_level, 1, -1
840          ddx2_mg(l) = 1.0_wp / dx_l**2
841          ddy2_mg(l) = 1.0_wp / dy_l**2
842          DO  k = nzb+1, nzt_l
843             f2_mg(k,l) = rho_air_zw_mg(k,l) / ( dzu_mg(k+1,l) * dzw_mg(k,l) )
844             f3_mg(k,l) = rho_air_zw_mg(k-1,l) / ( dzu_mg(k,l)   * dzw_mg(k,l) )
845             f1_mg(k,l) = 2.0_wp * ( ddx2_mg(l) + ddy2_mg(l) ) &
846                          * rho_air_mg(k,l) + f2_mg(k,l) + f3_mg(k,l)
847          ENDDO
848          nzt_l = nzt_l / 2
849          dx_l  = dx_l * 2.0_wp
850          dy_l  = dy_l * 2.0_wp
851       ENDDO
852
853    ENDIF
854
855!
856!-- 3D-array for storing the dissipation, needed for calculating the sgs
857!-- particle velocities
858    IF ( use_sgs_for_particles  .OR.  wang_kernel  .OR.  collision_turbulence )&
859    THEN
860       ALLOCATE( diss(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
861    ENDIF
862
863!
864!-- 1D-array for large scale subsidence velocity
865    IF ( .NOT. ALLOCATED( w_subs ) )  THEN
866       ALLOCATE ( w_subs(nzb:nzt+1) )
867       w_subs = 0.0_wp
868    ENDIF
869
870!
871!-- Arrays to store velocity data from t-dt and the phase speeds which
872!-- are needed for radiation boundary conditions
873    IF ( outflow_l )  THEN
874       ALLOCATE( u_m_l(nzb:nzt+1,nysg:nyng,1:2),                               &
875                 v_m_l(nzb:nzt+1,nysg:nyng,0:1),                               &
876                 w_m_l(nzb:nzt+1,nysg:nyng,0:1) )
877    ENDIF
878    IF ( outflow_r )  THEN
879       ALLOCATE( u_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx),                           &
880                 v_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx),                           &
881                 w_m_r(nzb:nzt+1,nysg:nyng,nx-1:nx) )
882    ENDIF
883    IF ( outflow_l  .OR.  outflow_r )  THEN
884       ALLOCATE( c_u(nzb:nzt+1,nysg:nyng), c_v(nzb:nzt+1,nysg:nyng),           &
885                 c_w(nzb:nzt+1,nysg:nyng) )
886    ENDIF
887    IF ( outflow_s )  THEN
888       ALLOCATE( u_m_s(nzb:nzt+1,0:1,nxlg:nxrg),                               &
889                 v_m_s(nzb:nzt+1,1:2,nxlg:nxrg),                               &
890                 w_m_s(nzb:nzt+1,0:1,nxlg:nxrg) )
891    ENDIF
892    IF ( outflow_n )  THEN
893       ALLOCATE( u_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg),                           &
894                 v_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg),                           &
895                 w_m_n(nzb:nzt+1,ny-1:ny,nxlg:nxrg) )
896    ENDIF
897    IF ( outflow_s  .OR.  outflow_n )  THEN
898       ALLOCATE( c_u(nzb:nzt+1,nxlg:nxrg), c_v(nzb:nzt+1,nxlg:nxrg),           &
899                 c_w(nzb:nzt+1,nxlg:nxrg) )
900    ENDIF
901    IF ( outflow_l  .OR.  outflow_r  .OR.  outflow_s  .OR.  outflow_n )  THEN
902       ALLOCATE( c_u_m_l(nzb:nzt+1), c_v_m_l(nzb:nzt+1), c_w_m_l(nzb:nzt+1) )                   
903       ALLOCATE( c_u_m(nzb:nzt+1), c_v_m(nzb:nzt+1), c_w_m(nzb:nzt+1) )
904    ENDIF
905
906
907#if ! defined( __nopointer )
908!
909!-- Initial assignment of the pointers
910    e  => e_1;   e_p  => e_2;   te_m  => e_3
911    IF ( .NOT. neutral )  THEN
912       pt => pt_1;  pt_p => pt_2;  tpt_m => pt_3
913    ELSE
914       pt => pt_1;  pt_p => pt_1;  tpt_m => pt_3
915    ENDIF
916    u  => u_1;   u_p  => u_2;   tu_m  => u_3
917    v  => v_1;   v_p  => v_2;   tv_m  => v_3
918    w  => w_1;   w_p  => w_2;   tw_m  => w_3
919
920    IF ( humidity )  THEN
921       q => q_1;  q_p => q_2;  tq_m => q_3
922       IF ( humidity )  THEN
923          vpt  => vpt_1   
924          IF ( cloud_physics )  THEN
925             ql => ql_1
926             IF ( .NOT. microphysics_morrison )  THEN
927                qc => qc_1
928             ENDIF
929             IF ( microphysics_morrison )  THEN
930                qc => qc_1;  qc_p  => qc_2;  tqc_m  => qc_3
931                nc => nc_1;  nc_p  => nc_2;  tnc_m  => nc_3
932             ENDIF
933             IF ( microphysics_seifert )  THEN
934                qr => qr_1;  qr_p  => qr_2;  tqr_m  => qr_3
935                nr => nr_1;  nr_p  => nr_2;  tnr_m  => nr_3
936             ENDIF
937          ENDIF
938       ENDIF
939       IF ( cloud_droplets )  THEN
940          ql   => ql_1
941          ql_c => ql_2
942       ENDIF
943    ENDIF
944   
945    IF ( passive_scalar )  THEN
946       s => s_1;  s_p => s_2;  ts_m => s_3
947    ENDIF   
948
949    IF ( ocean )  THEN
950       sa => sa_1;  sa_p => sa_2;  tsa_m => sa_3
951    ENDIF
952#endif
953!
954!-- Initialize wall arrays
955    CALL init_surface_arrays
956!
957!-- Allocate land surface model arrays
958    IF ( land_surface )  THEN
959       CALL lsm_init_arrays
960    ENDIF
961
962!
963!-- Allocate wind turbine model arrays
964    IF ( wind_turbine )  THEN
965       CALL wtm_init_arrays
966    ENDIF
967   
968!
969!-- Initialize virtual flight measurements
970    IF ( virtual_flight )  THEN
971       CALL flight_init
972    ENDIF
973
974!
975!-- Initialize nudging if required
976    IF ( nudging )  THEN
977       CALL nudge_init
978    ENDIF
979
980!
981!-- Initialize reading of large scale forcing from external file - if required
982    IF ( large_scale_forcing )  THEN
983       CALL lsf_init
984    ENDIF
985
986!
987!-- Allocate arrays containing the RK coefficient for calculation of
988!-- perturbation pressure and turbulent fluxes. At this point values are
989!-- set for pressure calculation during initialization (where no timestep
990!-- is done). Further below the values needed within the timestep scheme
991!-- will be set.
992    ALLOCATE( weight_substep(1:intermediate_timestep_count_max),               &
993              weight_pres(1:intermediate_timestep_count_max) )
994    weight_substep = 1.0_wp
995    weight_pres    = 1.0_wp
996    intermediate_timestep_count = 0  ! needed when simulated_time = 0.0
997       
998    CALL location_message( 'finished', .TRUE. )
999
1000!
1001!-- Initialize local summation arrays for routine flow_statistics.
1002!-- This is necessary because they may not yet have been initialized when they
1003!-- are called from flow_statistics (or - depending on the chosen model run -
1004!-- are never initialized)
1005    sums_divnew_l      = 0.0_wp
1006    sums_divold_l      = 0.0_wp
1007    sums_l_l           = 0.0_wp
1008    sums_wsts_bc_l     = 0.0_wp
1009
1010!
1011!-- Initialize model variables
1012    IF ( TRIM( initializing_actions ) /= 'read_restart_data'  .AND.            &
1013         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
1014!
1015!--    First model run of a possible job queue.
1016!--    Initial profiles of the variables must be computes.
1017       IF ( INDEX( initializing_actions, 'set_1d-model_profiles' ) /= 0 )  THEN
1018
1019          CALL location_message( 'initializing with 1D model profiles', .FALSE. )
1020!
1021!--       Use solutions of the 1D model as initial profiles,
1022!--       start 1D model
1023          CALL init_1d_model
1024!
1025!--       Transfer initial profiles to the arrays of the 3D model
1026          DO  i = nxlg, nxrg
1027             DO  j = nysg, nyng
1028                e(:,j,i)  = e1d
1029                kh(:,j,i) = kh1d
1030                km(:,j,i) = km1d
1031                pt(:,j,i) = pt_init
1032                u(:,j,i)  = u1d
1033                v(:,j,i)  = v1d
1034             ENDDO
1035          ENDDO
1036
1037          IF ( humidity )  THEN
1038             DO  i = nxlg, nxrg
1039                DO  j = nysg, nyng
1040                   q(:,j,i) = q_init
1041                ENDDO
1042             ENDDO
1043             IF ( cloud_physics  .AND.  microphysics_morrison )  THEN
1044                DO  i = nxlg, nxrg
1045                   DO  j = nysg, nyng
1046                      qc(:,j,i) = 0.0_wp
1047                      nc(:,j,i) = 0.0_wp
1048                   ENDDO
1049                ENDDO
1050             ENDIF
1051             IF ( cloud_physics  .AND.  microphysics_seifert )  THEN
1052                DO  i = nxlg, nxrg
1053                   DO  j = nysg, nyng
1054                      qr(:,j,i) = 0.0_wp
1055                      nr(:,j,i) = 0.0_wp
1056                   ENDDO
1057                ENDDO
1058             ENDIF
1059          ENDIF
1060
1061          IF ( passive_scalar )  THEN
1062             DO  i = nxlg, nxrg
1063                DO  j = nysg, nyng
1064                   s(:,j,i) = s_init
1065                ENDDO
1066             ENDDO   
1067          ENDIF
1068
1069          IF ( .NOT. constant_diffusion )  THEN
1070             DO  i = nxlg, nxrg
1071                DO  j = nysg, nyng
1072                   e(:,j,i)  = e1d
1073                ENDDO
1074             ENDDO
1075!
1076!--          Store initial profiles for output purposes etc.
1077             hom(:,1,25,:) = SPREAD( l1d, 2, statistic_regions+1 )
1078
1079          ELSE
1080             e    = 0.0_wp  ! must be set, because used in
1081          ENDIF
1082!
1083!--       Inside buildings set velocities back to zero
1084          IF ( topography /= 'flat' )  THEN
1085             DO  i = nxlg, nxrg
1086                DO  j = nysg, nyng
1087                   DO  k = nzb, nzt
1088                      u(k,j,i) = MERGE( u(k,j,i), 0.0_wp,                      &
1089                                        BTEST( wall_flags_0(k,j,i), 1 ) )
1090                      v(k,j,i) = MERGE( v(k,j,i), 0.0_wp,                      &
1091                                        BTEST( wall_flags_0(k,j,i), 2 ) )
1092                   ENDDO
1093                ENDDO
1094             ENDDO
1095             
1096!
1097!--          WARNING: The extra boundary conditions set after running the
1098!--          -------  1D model impose an error on the divergence one layer
1099!--                   below the topography; need to correct later
1100!--          ATTENTION: Provisional correction for Piacsek & Williams
1101!--          ---------  advection scheme: keep u and v zero one layer below
1102!--                     the topography.
1103             IF ( ibc_uv_b == 1 )  THEN
1104!
1105!--             Neumann condition
1106                DO  i = nxl-1, nxr+1
1107                   DO  j = nys-1, nyn+1
1108                      u(nzb,j,i) = u(nzb+1,j,i)
1109                      v(nzb,j,i) = v(nzb+1,j,i)
1110                   ENDDO
1111                ENDDO
1112
1113             ENDIF
1114
1115          ENDIF
1116
1117          CALL location_message( 'finished', .TRUE. )
1118
1119       ELSEIF ( INDEX(initializing_actions, 'set_constant_profiles') /= 0 )    &
1120       THEN
1121
1122          CALL location_message( 'initializing with constant profiles', .FALSE. )
1123!
1124!--       Overwrite initial profiles in case of synthetic turbulence generator
1125          IF( use_synthetic_turbulence_generator ) THEN
1126             CALL stg_init
1127          ENDIF
1128
1129!
1130!--       Use constructed initial profiles (velocity constant with height,
1131!--       temperature profile with constant gradient)
1132          DO  i = nxlg, nxrg
1133             DO  j = nysg, nyng
1134                pt(:,j,i) = pt_init
1135                u(:,j,i)  = u_init
1136                v(:,j,i)  = v_init
1137             ENDDO
1138          ENDDO
1139
1140!
1141!--       Set initial horizontal velocities at the lowest computational grid
1142!--       levels to zero in order to avoid too small time steps caused by the
1143!--       diffusion limit in the initial phase of a run (at k=1, dz/2 occurs
1144!--       in the limiting formula!).
1145          IF ( ibc_uv_b /= 1 )  THEN
1146             DO  i = nxlg, nxrg
1147                DO  j = nysg, nyng
1148                   DO  k = nzb, nzt
1149                      u(k,j,i) = MERGE( u(k,j,i), 0.0_wp,                      &
1150                                        BTEST( wall_flags_0(k,j,i), 20 ) )
1151                      v(k,j,i) = MERGE( v(k,j,i), 0.0_wp,                      &
1152                                        BTEST( wall_flags_0(k,j,i), 21 ) )
1153                   ENDDO
1154                ENDDO
1155             ENDDO
1156          ENDIF
1157
1158          IF ( humidity )  THEN
1159             DO  i = nxlg, nxrg
1160                DO  j = nysg, nyng
1161                   q(:,j,i) = q_init
1162                ENDDO
1163             ENDDO
1164             IF ( cloud_physics  .AND.  microphysics_morrison )  THEN
1165                DO  i = nxlg, nxrg
1166                   DO  j = nysg, nyng
1167                      qc(:,j,i) = 0.0_wp
1168                      nc(:,j,i) = 0.0_wp
1169                   ENDDO
1170                ENDDO
1171             ENDIF
1172
1173             IF ( cloud_physics  .AND.  microphysics_seifert )  THEN
1174                DO  i = nxlg, nxrg
1175                   DO  j = nysg, nyng
1176                      qr(:,j,i) = 0.0_wp
1177                      nr(:,j,i) = 0.0_wp
1178                   ENDDO
1179                ENDDO
1180             ENDIF
1181
1182          ENDIF
1183         
1184          IF ( passive_scalar )  THEN
1185             DO  i = nxlg, nxrg
1186                DO  j = nysg, nyng
1187                   s(:,j,i) = s_init
1188                ENDDO
1189             ENDDO
1190          ENDIF
1191
1192          IF ( ocean )  THEN
1193             DO  i = nxlg, nxrg
1194                DO  j = nysg, nyng
1195                   sa(:,j,i) = sa_init
1196                ENDDO
1197             ENDDO
1198          ENDIF
1199         
1200          IF ( constant_diffusion )  THEN
1201             km   = km_constant
1202             kh   = km / prandtl_number
1203             e    = 0.0_wp
1204          ELSEIF ( e_init > 0.0_wp )  THEN
1205             DO  k = nzb+1, nzt
1206                km(k,:,:) = 0.1_wp * l_grid(k) * SQRT( e_init )
1207             ENDDO
1208             km(nzb,:,:)   = km(nzb+1,:,:)
1209             km(nzt+1,:,:) = km(nzt,:,:)
1210             kh   = km / prandtl_number
1211             e    = e_init
1212          ELSE
1213             IF ( .NOT. ocean )  THEN
1214                kh   = 0.01_wp   ! there must exist an initial diffusion, because
1215                km   = 0.01_wp   ! otherwise no TKE would be produced by the
1216                              ! production terms, as long as not yet
1217                              ! e = (u*/cm)**2 at k=nzb+1
1218             ELSE
1219                kh   = 0.00001_wp
1220                km   = 0.00001_wp
1221             ENDIF
1222             e    = 0.0_wp
1223          ENDIF
1224!
1225!--       Compute initial temperature field and other constants used in case
1226!--       of a sloping surface
1227          IF ( sloping_surface )  CALL init_slope
1228
1229          CALL location_message( 'finished', .TRUE. )
1230
1231       ELSEIF ( INDEX(initializing_actions, 'by_user') /= 0 )                  &
1232       THEN
1233
1234          CALL location_message( 'initializing by user', .FALSE. )
1235!
1236!--       Initialization will completely be done by the user
1237          CALL user_init_3d_model
1238
1239          CALL location_message( 'finished', .TRUE. )
1240
1241       ENDIF
1242
1243       CALL location_message( 'initializing statistics, boundary conditions, etc.', &
1244                              .FALSE. )
1245
1246!
1247!--    Bottom boundary
1248       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2  )  THEN
1249          u(nzb,:,:) = 0.0_wp
1250          v(nzb,:,:) = 0.0_wp
1251       ENDIF
1252
1253!
1254!--    Apply channel flow boundary condition
1255       IF ( TRIM( bc_uv_t ) == 'dirichlet_0' )  THEN
1256          u(nzt+1,:,:) = 0.0_wp
1257          v(nzt+1,:,:) = 0.0_wp
1258       ENDIF
1259
1260!
1261!--    Calculate virtual potential temperature
1262       IF ( humidity )  vpt = pt * ( 1.0_wp + 0.61_wp * q )
1263
1264!
1265!--    Store initial profiles for output purposes etc.
1266       hom(:,1,5,:) = SPREAD( u(:,nys,nxl), 2, statistic_regions+1 )
1267       hom(:,1,6,:) = SPREAD( v(:,nys,nxl), 2, statistic_regions+1 )
1268       IF ( ibc_uv_b == 0 .OR. ibc_uv_b == 2)  THEN
1269          hom(nzb,1,5,:) = 0.0_wp
1270          hom(nzb,1,6,:) = 0.0_wp
1271       ENDIF
1272       hom(:,1,7,:)  = SPREAD( pt(:,nys,nxl), 2, statistic_regions+1 )
1273       hom(:,1,23,:) = SPREAD( km(:,nys,nxl), 2, statistic_regions+1 )
1274       hom(:,1,24,:) = SPREAD( kh(:,nys,nxl), 2, statistic_regions+1 )
1275
1276       IF ( ocean )  THEN
1277!
1278!--       Store initial salinity profile
1279          hom(:,1,26,:)  = SPREAD( sa(:,nys,nxl), 2, statistic_regions+1 )
1280       ENDIF
1281
1282       IF ( humidity )  THEN
1283!
1284!--       Store initial profile of total water content, virtual potential
1285!--       temperature
1286          hom(:,1,26,:) = SPREAD(   q(:,nys,nxl), 2, statistic_regions+1 )
1287          hom(:,1,29,:) = SPREAD( vpt(:,nys,nxl), 2, statistic_regions+1 )
1288          IF ( cloud_physics  .OR.  cloud_droplets ) THEN
1289!
1290!--          Store initial profile of specific humidity and potential
1291!--          temperature
1292             hom(:,1,27,:) = SPREAD(  q(:,nys,nxl), 2, statistic_regions+1 )
1293             hom(:,1,28,:) = SPREAD( pt(:,nys,nxl), 2, statistic_regions+1 )
1294          ENDIF
1295       ENDIF
1296
1297       IF ( passive_scalar )  THEN
1298!
1299!--       Store initial scalar profile
1300          hom(:,1,115,:) = SPREAD(  s(:,nys,nxl), 2, statistic_regions+1 )
1301       ENDIF
1302
1303!
1304!--    Initialize the random number generators (from numerical recipes)
1305       CALL random_function_ini
1306       
1307       IF ( random_generator == 'random-parallel' )  THEN
1308          CALL init_parallel_random_generator(nx, ny, nys, nyn, nxl, nxr)
1309       ENDIF
1310!
1311!--    Set the reference state to be used in the buoyancy terms (for ocean runs
1312!--    the reference state will be set (overwritten) in init_ocean)
1313       IF ( use_single_reference_value )  THEN
1314          IF (  .NOT.  humidity )  THEN
1315             ref_state(:) = pt_reference
1316          ELSE
1317             ref_state(:) = vpt_reference
1318          ENDIF
1319       ELSE
1320          IF (  .NOT.  humidity )  THEN
1321             ref_state(:) = pt_init(:)
1322          ELSE
1323             ref_state(:) = vpt(:,nys,nxl)
1324          ENDIF
1325       ENDIF
1326
1327!
1328!--    For the moment, vertical velocity is zero
1329       w = 0.0_wp
1330
1331!
1332!--    Initialize array sums (must be defined in first call of pres)
1333       sums = 0.0_wp
1334
1335!
1336!--    In case of iterative solvers, p must get an initial value
1337       IF ( psolver(1:9) == 'multigrid'  .OR.  psolver == 'sor' )  p = 0.0_wp
1338
1339!
1340!--    Treating cloud physics, liquid water content and precipitation amount
1341!--    are zero at beginning of the simulation
1342       IF ( cloud_physics )  THEN
1343          ql = 0.0_wp
1344          qc = 0.0_wp
1345
1346          precipitation_amount = 0.0_wp
1347       ENDIF
1348!
1349!--    Impose vortex with vertical axis on the initial velocity profile
1350       IF ( INDEX( initializing_actions, 'initialize_vortex' ) /= 0 )  THEN
1351          CALL init_rankine
1352       ENDIF
1353
1354!
1355!--    Impose temperature anomaly (advection test only)
1356       IF ( INDEX( initializing_actions, 'initialize_ptanom' ) /= 0 )  THEN
1357          CALL init_pt_anomaly
1358       ENDIF
1359
1360!
1361!--    If required, change the surface temperature at the start of the 3D run
1362       IF ( pt_surface_initial_change /= 0.0_wp )  THEN
1363          pt(nzb,:,:) = pt(nzb,:,:) + pt_surface_initial_change
1364       ENDIF
1365
1366!
1367!--    If required, change the surface humidity/scalar at the start of the 3D
1368!--    run
1369       IF ( humidity  .AND.  q_surface_initial_change /= 0.0_wp )              &
1370          q(nzb,:,:) = q(nzb,:,:) + q_surface_initial_change
1371         
1372       IF ( passive_scalar .AND.  s_surface_initial_change /= 0.0_wp )         &
1373          s(nzb,:,:) = s(nzb,:,:) + s_surface_initial_change
1374       
1375
1376!
1377!--    Initialize old and new time levels.
1378       te_m = 0.0_wp; tpt_m = 0.0_wp; tu_m = 0.0_wp; tv_m = 0.0_wp; tw_m = 0.0_wp
1379       e_p = e; pt_p = pt; u_p = u; v_p = v; w_p = w
1380
1381       IF ( humidity  )  THEN
1382          tq_m = 0.0_wp
1383          q_p = q
1384          IF ( cloud_physics  .AND.  microphysics_morrison )  THEN
1385             tqc_m = 0.0_wp
1386             qc_p  = qc
1387             tnc_m = 0.0_wp
1388             nc_p  = nc
1389          ENDIF
1390          IF ( cloud_physics  .AND.  microphysics_seifert )  THEN
1391             tqr_m = 0.0_wp
1392             qr_p  = qr
1393             tnr_m = 0.0_wp
1394             nr_p  = nr
1395          ENDIF
1396       ENDIF
1397       
1398       IF ( passive_scalar )  THEN
1399          ts_m = 0.0_wp
1400          s_p  = s
1401       ENDIF       
1402
1403       IF ( ocean )  THEN
1404          tsa_m = 0.0_wp
1405          sa_p  = sa
1406       ENDIF
1407       
1408       CALL location_message( 'finished', .TRUE. )
1409
1410    ELSEIF ( TRIM( initializing_actions ) == 'read_restart_data'  .OR.         &
1411             TRIM( initializing_actions ) == 'cyclic_fill' )                   &
1412    THEN
1413
1414       CALL location_message( 'initializing in case of restart / cyclic_fill', &
1415                              .FALSE. )
1416!
1417!--    Initialize surface elements and its attributes, e.g. heat- and
1418!--    momentumfluxes, roughness, scaling parameters. As number of surface
1419!--    elements might be different between runs, e.g. in case of cyclic fill,
1420!--    and not all surface elements are read, surface elements need to be
1421!--    initialized before.     
1422       CALL init_surfaces
1423!
1424!--    When reading data for cyclic fill of 3D prerun data files, read
1425!--    some of the global variables from the restart file which are required
1426!--    for initializing the inflow
1427       IF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
1428
1429          DO  i = 0, io_blocks-1
1430             IF ( i == io_group )  THEN
1431                CALL read_parts_of_var_list
1432                CALL close_file( 13 )
1433             ENDIF
1434#if defined( __parallel )
1435             CALL MPI_BARRIER( comm2d, ierr )
1436#endif
1437          ENDDO
1438
1439       ENDIF
1440
1441!
1442!--    Read binary data from restart file
1443       DO  i = 0, io_blocks-1
1444          IF ( i == io_group )  THEN
1445             CALL read_3d_binary
1446          ENDIF
1447#if defined( __parallel )
1448          CALL MPI_BARRIER( comm2d, ierr )
1449#endif
1450       ENDDO
1451
1452!
1453!--    Initialization of the turbulence recycling method
1454       IF ( TRIM( initializing_actions ) == 'cyclic_fill'  .AND.               &
1455            turbulent_inflow )  THEN
1456!
1457!--       First store the profiles to be used at the inflow.
1458!--       These profiles are the (temporally) and horizontally averaged vertical
1459!--       profiles from the prerun. Alternatively, prescribed profiles
1460!--       for u,v-components can be used.
1461          ALLOCATE( mean_inflow_profiles(nzb:nzt+1,7) )
1462
1463          IF ( use_prescribed_profile_data )  THEN
1464             mean_inflow_profiles(:,1) = u_init            ! u
1465             mean_inflow_profiles(:,2) = v_init            ! v
1466          ELSE
1467             mean_inflow_profiles(:,1) = hom_sum(:,1,0)    ! u
1468             mean_inflow_profiles(:,2) = hom_sum(:,2,0)    ! v
1469          ENDIF
1470          mean_inflow_profiles(:,4) = hom_sum(:,4,0)       ! pt
1471          mean_inflow_profiles(:,5) = hom_sum(:,8,0)       ! e
1472          IF ( humidity )                                                      &
1473             mean_inflow_profiles(:,6) = hom_sum(:,41,0)   ! q
1474          IF ( passive_scalar )                                                &
1475             mean_inflow_profiles(:,7) = hom_sum(:,115,0)   ! s
1476
1477!
1478!--       If necessary, adjust the horizontal flow field to the prescribed
1479!--       profiles
1480          IF ( use_prescribed_profile_data )  THEN
1481             DO  i = nxlg, nxrg
1482                DO  j = nysg, nyng
1483                   DO  k = nzb, nzt+1
1484                      u(k,j,i) = u(k,j,i) - hom_sum(k,1,0) + u_init(k)
1485                      v(k,j,i) = v(k,j,i) - hom_sum(k,2,0) + v_init(k)
1486                   ENDDO
1487                ENDDO
1488             ENDDO
1489          ENDIF
1490
1491!
1492!--       Use these mean profiles at the inflow (provided that Dirichlet
1493!--       conditions are used)
1494          IF ( inflow_l )  THEN
1495             DO  j = nysg, nyng
1496                DO  k = nzb, nzt+1
1497                   u(k,j,nxlg:-1)  = mean_inflow_profiles(k,1)
1498                   v(k,j,nxlg:-1)  = mean_inflow_profiles(k,2)
1499                   w(k,j,nxlg:-1)  = 0.0_wp
1500                   pt(k,j,nxlg:-1) = mean_inflow_profiles(k,4)
1501                   e(k,j,nxlg:-1)  = mean_inflow_profiles(k,5)
1502                   IF ( humidity )                                             &
1503                      q(k,j,nxlg:-1)  = mean_inflow_profiles(k,6)
1504                   IF ( passive_scalar )                                       &
1505                      s(k,j,nxlg:-1)  = mean_inflow_profiles(k,7)                     
1506                ENDDO
1507             ENDDO
1508          ENDIF
1509
1510!
1511!--       Calculate the damping factors to be used at the inflow. For a
1512!--       turbulent inflow the turbulent fluctuations have to be limited
1513!--       vertically because otherwise the turbulent inflow layer will grow
1514!--       in time.
1515          IF ( inflow_damping_height == 9999999.9_wp )  THEN
1516!
1517!--          Default: use the inversion height calculated by the prerun; if
1518!--          this is zero, inflow_damping_height must be explicitly
1519!--          specified.
1520             IF ( hom_sum(nzb+6,pr_palm,0) /= 0.0_wp )  THEN
1521                inflow_damping_height = hom_sum(nzb+6,pr_palm,0)
1522             ELSE
1523                WRITE( message_string, * ) 'inflow_damping_height must be ',   &
1524                     'explicitly specified because&the inversion height ',     &
1525                     'calculated by the prerun is zero.'
1526                CALL message( 'init_3d_model', 'PA0318', 1, 2, 0, 6, 0 )
1527             ENDIF
1528
1529          ENDIF
1530
1531          IF ( inflow_damping_width == 9999999.9_wp )  THEN
1532!
1533!--          Default for the transition range: one tenth of the undamped
1534!--          layer
1535             inflow_damping_width = 0.1_wp * inflow_damping_height
1536
1537          ENDIF
1538
1539          ALLOCATE( inflow_damping_factor(nzb:nzt+1) )
1540
1541          DO  k = nzb, nzt+1
1542
1543             IF ( zu(k) <= inflow_damping_height )  THEN
1544                inflow_damping_factor(k) = 1.0_wp
1545             ELSEIF ( zu(k) <= ( inflow_damping_height + inflow_damping_width ) )  THEN
1546                inflow_damping_factor(k) = 1.0_wp -                            &
1547                                           ( zu(k) - inflow_damping_height ) / &
1548                                           inflow_damping_width
1549             ELSE
1550                inflow_damping_factor(k) = 0.0_wp
1551             ENDIF
1552
1553          ENDDO
1554
1555       ENDIF
1556
1557!
1558!--    Inside buildings set velocities and TKE back to zero
1559       IF ( TRIM( initializing_actions ) == 'cyclic_fill' .AND.                &
1560            topography /= 'flat' )  THEN
1561!
1562!--       Inside buildings set velocities and TKE back to zero.
1563!--       Other scalars (pt, q, s, km, kh, p, sa, ...) are ignored at present,
1564!--       maybe revise later.
1565          DO  i = nxlg, nxrg
1566             DO  j = nysg, nyng
1567                DO  k = nzb, nzt
1568                   u(k,j,i)     = MERGE( u(k,j,i), 0.0_wp,                     &
1569                                         BTEST( wall_flags_0(k,j,i), 1 ) )
1570                   v(k,j,i)     = MERGE( v(k,j,i), 0.0_wp,                     &
1571                                         BTEST( wall_flags_0(k,j,i), 2 ) )
1572                   w(k,j,i)     = MERGE( w(k,j,i), 0.0_wp,                     &
1573                                         BTEST( wall_flags_0(k,j,i), 3 ) )
1574                   e(k,j,i)     = MERGE( e(k,j,i), 0.0_wp,                     &
1575                                         BTEST( wall_flags_0(k,j,i), 0 ) )
1576                   tu_m(k,j,i)  = MERGE( tu_m(k,j,i), 0.0_wp,                  &
1577                                         BTEST( wall_flags_0(k,j,i), 1 ) )
1578                   tv_m(k,j,i)  = MERGE( tv_m(k,j,i), 0.0_wp,                  &
1579                                         BTEST( wall_flags_0(k,j,i), 2 ) )
1580                   tw_m(k,j,i)  = MERGE( tw_m(k,j,i), 0.0_wp,                  &
1581                                         BTEST( wall_flags_0(k,j,i), 3 ) )
1582                   te_m(k,j,i)  = MERGE( te_m(k,j,i), 0.0_wp,                  &
1583                                         BTEST( wall_flags_0(k,j,i), 0 ) )
1584                   tpt_m(k,j,i) = MERGE( tpt_m(k,j,i), 0.0_wp,                 &
1585                                         BTEST( wall_flags_0(k,j,i), 0 ) )
1586                ENDDO
1587             ENDDO
1588          ENDDO
1589
1590       ENDIF
1591
1592!
1593!--    Calculate initial temperature field and other constants used in case
1594!--    of a sloping surface
1595       IF ( sloping_surface )  CALL init_slope
1596
1597!
1598!--    Initialize new time levels (only done in order to set boundary values
1599!--    including ghost points)
1600       e_p = e; pt_p = pt; u_p = u; v_p = v; w_p = w
1601       IF ( humidity )  THEN
1602          q_p = q
1603          IF ( cloud_physics  .AND.  microphysics_morrison )  THEN
1604             qc_p = qc
1605             nc_p = nc
1606          ENDIF
1607          IF ( cloud_physics  .AND.  microphysics_seifert )  THEN
1608             qr_p = qr
1609             nr_p = nr
1610          ENDIF
1611       ENDIF
1612       IF ( passive_scalar )  s_p  = s
1613       IF ( ocean          )  sa_p = sa
1614
1615!
1616!--    Allthough tendency arrays are set in prognostic_equations, they have
1617!--    have to be predefined here because they are used (but multiplied with 0)
1618!--    there before they are set.
1619       te_m = 0.0_wp; tpt_m = 0.0_wp; tu_m = 0.0_wp; tv_m = 0.0_wp; tw_m = 0.0_wp
1620       IF ( humidity )  THEN
1621          tq_m = 0.0_wp
1622          IF ( cloud_physics  .AND.  microphysics_morrison )  THEN
1623             tqc_m = 0.0_wp
1624             tnc_m = 0.0_wp
1625          ENDIF
1626          IF ( cloud_physics  .AND.  microphysics_seifert )  THEN
1627             tqr_m = 0.0_wp
1628             tnr_m = 0.0_wp
1629          ENDIF
1630       ENDIF
1631       IF ( passive_scalar )  ts_m  = 0.0_wp
1632       IF ( ocean          )  tsa_m = 0.0_wp
1633!
1634!--    Initialize synthetic turbulence generator in case of restart.
1635       IF ( TRIM( initializing_actions ) == 'read_restart_data'  .AND.         &
1636            use_synthetic_turbulence_generator )  CALL stg_init
1637
1638       CALL location_message( 'finished', .TRUE. )
1639
1640    ELSE
1641!
1642!--    Actually this part of the programm should not be reached
1643       message_string = 'unknown initializing problem'
1644       CALL message( 'init_3d_model', 'PA0193', 1, 2, 0, 6, 0 )
1645    ENDIF
1646
1647
1648    IF (  TRIM( initializing_actions ) /= 'read_restart_data' )  THEN
1649!
1650!--    Initialize old timelevels needed for radiation boundary conditions
1651       IF ( outflow_l )  THEN
1652          u_m_l(:,:,:) = u(:,:,1:2)
1653          v_m_l(:,:,:) = v(:,:,0:1)
1654          w_m_l(:,:,:) = w(:,:,0:1)
1655       ENDIF
1656       IF ( outflow_r )  THEN
1657          u_m_r(:,:,:) = u(:,:,nx-1:nx)
1658          v_m_r(:,:,:) = v(:,:,nx-1:nx)
1659          w_m_r(:,:,:) = w(:,:,nx-1:nx)
1660       ENDIF
1661       IF ( outflow_s )  THEN
1662          u_m_s(:,:,:) = u(:,0:1,:)
1663          v_m_s(:,:,:) = v(:,1:2,:)
1664          w_m_s(:,:,:) = w(:,0:1,:)
1665       ENDIF
1666       IF ( outflow_n )  THEN
1667          u_m_n(:,:,:) = u(:,ny-1:ny,:)
1668          v_m_n(:,:,:) = v(:,ny-1:ny,:)
1669          w_m_n(:,:,:) = w(:,ny-1:ny,:)
1670       ENDIF
1671       
1672    ENDIF
1673
1674!
1675!-- Calculate the initial volume flow at the right and north boundary
1676    IF ( conserve_volume_flow )  THEN
1677
1678       IF ( use_prescribed_profile_data )  THEN
1679
1680          volume_flow_initial_l = 0.0_wp
1681          volume_flow_area_l    = 0.0_wp
1682
1683          IF ( nxr == nx )  THEN
1684             DO  j = nys, nyn
1685                DO  k = nzb+1, nzt
1686                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +       &
1687                                              u_init(k) * dzw(k)               &
1688                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1689                                              BTEST( wall_flags_0(k,j,nxr), 1 )&
1690                                            )
1691
1692                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)   &
1693                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1694                                              BTEST( wall_flags_0(k,j,nxr), 1 )&
1695                                            )
1696                ENDDO
1697             ENDDO
1698          ENDIF
1699         
1700          IF ( nyn == ny )  THEN
1701             DO  i = nxl, nxr
1702                DO  k = nzb+1, nzt
1703                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +       &
1704                                              v_init(k) * dzw(k)               &       
1705                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1706                                              BTEST( wall_flags_0(k,nyn,i), 2 )&
1707                                            )
1708                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)   &       
1709                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1710                                              BTEST( wall_flags_0(k,nyn,i), 2 )&
1711                                            )
1712                ENDDO
1713             ENDDO
1714          ENDIF
1715
1716#if defined( __parallel )
1717          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
1718                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1719          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
1720                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1721
1722#else
1723          volume_flow_initial = volume_flow_initial_l
1724          volume_flow_area    = volume_flow_area_l
1725#endif 
1726
1727       ELSEIF ( TRIM( initializing_actions ) == 'cyclic_fill' )  THEN
1728
1729          volume_flow_initial_l = 0.0_wp
1730          volume_flow_area_l    = 0.0_wp
1731
1732          IF ( nxr == nx )  THEN
1733             DO  j = nys, nyn
1734                DO  k = nzb+1, nzt
1735                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +       &
1736                                              hom_sum(k,1,0) * dzw(k)          &
1737                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1738                                              BTEST( wall_flags_0(k,j,nx), 1 ) &
1739                                            )
1740                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)   &
1741                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1742                                              BTEST( wall_flags_0(k,j,nx), 1 ) &
1743                                            )
1744                ENDDO
1745             ENDDO
1746          ENDIF
1747         
1748          IF ( nyn == ny )  THEN
1749             DO  i = nxl, nxr
1750                DO  k = nzb+1, nzt
1751                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +       &
1752                                              hom_sum(k,2,0) * dzw(k)          &       
1753                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1754                                              BTEST( wall_flags_0(k,ny,i), 2 ) &
1755                                            )
1756                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)   &       
1757                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1758                                              BTEST( wall_flags_0(k,ny,i), 2 ) &
1759                                            )
1760                ENDDO
1761             ENDDO
1762          ENDIF
1763
1764#if defined( __parallel )
1765          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
1766                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1767          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
1768                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1769
1770#else
1771          volume_flow_initial = volume_flow_initial_l
1772          volume_flow_area    = volume_flow_area_l
1773#endif 
1774
1775       ELSEIF ( TRIM( initializing_actions ) /= 'read_restart_data' )  THEN
1776
1777          volume_flow_initial_l = 0.0_wp
1778          volume_flow_area_l    = 0.0_wp
1779
1780          IF ( nxr == nx )  THEN
1781             DO  j = nys, nyn
1782                DO  k = nzb+1, nzt
1783                   volume_flow_initial_l(1) = volume_flow_initial_l(1) +       &
1784                                              u(k,j,nx) * dzw(k)               &
1785                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1786                                              BTEST( wall_flags_0(k,j,nx), 1 ) &
1787                                            )
1788                   volume_flow_area_l(1)    = volume_flow_area_l(1) + dzw(k)   &
1789                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1790                                              BTEST( wall_flags_0(k,j,nx), 1 ) &
1791                                            )
1792                ENDDO
1793             ENDDO
1794          ENDIF
1795         
1796          IF ( nyn == ny )  THEN
1797             DO  i = nxl, nxr
1798                DO  k = nzb+1, nzt
1799                   volume_flow_initial_l(2) = volume_flow_initial_l(2) +       &
1800                                              v(k,ny,i) * dzw(k)               &       
1801                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1802                                              BTEST( wall_flags_0(k,ny,i), 2 ) &
1803                                            )
1804                   volume_flow_area_l(2)    = volume_flow_area_l(2) + dzw(k)   &       
1805                                     * MERGE( 1.0_wp, 0.0_wp,                  &
1806                                              BTEST( wall_flags_0(k,ny,i), 2 ) &
1807                                            )
1808                ENDDO
1809             ENDDO
1810          ENDIF
1811
1812#if defined( __parallel )
1813          CALL MPI_ALLREDUCE( volume_flow_initial_l(1), volume_flow_initial(1),&
1814                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1815          CALL MPI_ALLREDUCE( volume_flow_area_l(1), volume_flow_area(1),      &
1816                              2, MPI_REAL, MPI_SUM, comm2d, ierr )
1817
1818#else
1819          volume_flow_initial = volume_flow_initial_l
1820          volume_flow_area    = volume_flow_area_l
1821#endif 
1822
1823       ENDIF
1824
1825!
1826!--    In case of 'bulk_velocity' mode, volume_flow_initial is calculated
1827!--    from u|v_bulk instead
1828       IF ( TRIM( conserve_volume_flow_mode ) == 'bulk_velocity' )  THEN
1829          volume_flow_initial(1) = u_bulk * volume_flow_area(1)
1830          volume_flow_initial(2) = v_bulk * volume_flow_area(2)
1831       ENDIF
1832
1833    ENDIF
1834!
1835!-- Initialize surface elements and its attributes, e.g. heat- and
1836!-- momentumfluxes, roughness, scaling parameters.
1837!-- This is already done in case of restart data. 
1838    IF ( TRIM( initializing_actions ) /= 'read_restart_data'  .AND.            &
1839         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
1840       CALL init_surfaces
1841!
1842!--    Finally, if random_heatflux is set, disturb shf at horizontal
1843!--    surfaces. Actually, this should be done in surface_mod, where all other
1844!--    initializations of surface quantities are done. However, this
1845!--    would create a ring dependency, hence, it is done here. Maybe delete
1846!--    disturb_heatflux and tranfer the respective code directly into the
1847!--    initialization in surface_mod.         
1848       IF ( use_surface_fluxes  .AND.  constant_heatflux  .AND.                &
1849            random_heatflux )  THEN
1850          IF ( surf_def_h(0)%ns >= 1 )  CALL disturb_heatflux( surf_def_h(0) )
1851          IF ( surf_lsm_h%ns    >= 1 )  CALL disturb_heatflux( surf_lsm_h    )
1852          IF ( surf_usm_h%ns    >= 1 )  CALL disturb_heatflux( surf_usm_h    )
1853       ENDIF
1854    ENDIF
1855
1856!
1857!-- Initialize surface forcing corresponding to large-scale forcing. Therein,
1858!-- initialize heat-fluxes, etc. via datatype. Revise it later!
1859    IF ( large_scale_forcing .AND. lsf_surf )  THEN
1860       IF ( use_surface_fluxes  .AND.  constant_heatflux )  THEN
1861          CALL ls_forcing_surf ( simulated_time )
1862       ENDIF
1863    ENDIF
1864!
1865!-- Initialize quantities for special advections schemes
1866    CALL init_advec
1867
1868!
1869!-- Impose random perturbation on the horizontal velocity field and then
1870!-- remove the divergences from the velocity field at the initial stage
1871    IF ( create_disturbances  .AND.  disturbance_energy_limit /= 0.0_wp  .AND. &
1872         TRIM( initializing_actions ) /= 'read_restart_data'  .AND.            &
1873         TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
1874
1875       CALL location_message( 'creating initial disturbances', .FALSE. )
1876       CALL disturb_field( 'u', tend, u )
1877       CALL disturb_field( 'v', tend, v )
1878       CALL location_message( 'finished', .TRUE. )
1879
1880       CALL location_message( 'calling pressure solver', .FALSE. )
1881       n_sor = nsor_ini
1882       CALL pres
1883       n_sor = nsor
1884       CALL location_message( 'finished', .TRUE. )
1885
1886    ENDIF
1887
1888!
1889!-- If required, initialize quantities needed for the plant canopy model
1890    IF ( plant_canopy )  THEN
1891       CALL location_message( 'initializing plant canopy model', .FALSE. )   
1892       CALL pcm_init
1893       CALL location_message( 'finished', .TRUE. )
1894    ENDIF
1895
1896!
1897!-- If required, initialize dvrp-software
1898    IF ( dt_dvrp /= 9999999.9_wp )  CALL init_dvrp
1899
1900    IF ( ocean )  THEN
1901!
1902!--    Initialize quantities needed for the ocean model
1903       CALL init_ocean
1904
1905    ELSE
1906!
1907!--    Initialize quantities for handling cloud physics
1908!--    This routine must be called before lpm_init, because
1909!--    otherwise, array pt_d_t, needed in data_output_dvrp (called by
1910!--    lpm_init) is not defined.
1911       CALL init_cloud_physics
1912!
1913!--    Initialize bulk cloud microphysics
1914       CALL microphysics_init
1915    ENDIF
1916
1917!
1918!-- If required, initialize particles
1919    IF ( particle_advection )  CALL lpm_init
1920
1921!
1922!-- If required, initialize quantities needed for the LSM
1923    IF ( land_surface )  THEN
1924       CALL location_message( 'initializing land surface model', .FALSE. )
1925       CALL lsm_init
1926       CALL location_message( 'finished', .TRUE. )
1927    ENDIF
1928
1929!
1930!-- Initialize surface layer (done after LSM as roughness length are required
1931!-- for initialization
1932    IF ( constant_flux_layer )  THEN
1933       CALL location_message( 'initializing surface layer', .FALSE. )
1934       CALL init_surface_layer_fluxes
1935       CALL location_message( 'finished', .TRUE. )
1936    ENDIF
1937
1938!
1939!-- If required, initialize radiation model
1940    IF ( radiation )  THEN
1941       CALL location_message( 'initializing radiation model', .FALSE. )
1942       CALL radiation_init
1943       CALL location_message( 'finished', .TRUE. )
1944    ENDIF
1945
1946   
1947!
1948!-- Temporary solution to add LSM and radiation time series to the default
1949!-- output
1950    IF ( land_surface  .OR.  radiation )  THEN
1951       IF ( TRIM( radiation_scheme ) == 'rrtmg' )  THEN
1952          dots_num = dots_num + 15
1953       ELSE
1954          dots_num = dots_num + 11
1955       ENDIF
1956    ENDIF
1957   
1958!
1959!-- If required, initialize urban surface model
1960    IF ( urban_surface )  THEN
1961       CALL location_message( 'initializing urban surface model', .FALSE. )
1962       CALL usm_init_urban_surface
1963       CALL location_message( 'finished', .TRUE. )
1964    ENDIF
1965
1966!
1967!-- If required, initialize quantities needed for the wind turbine model
1968    IF ( wind_turbine )  THEN
1969       CALL location_message( 'initializing wind turbine model', .FALSE. )
1970       CALL wtm_init
1971       CALL location_message( 'finished', .TRUE. )
1972    ENDIF
1973
1974
1975!
1976!-- Initialize the ws-scheme.   
1977    IF ( ws_scheme_sca .OR. ws_scheme_mom )  CALL ws_init       
1978
1979!
1980!-- Setting weighting factors for calculation of perturbation pressure
1981!-- and turbulent quantities from the RK substeps
1982    IF ( TRIM(timestep_scheme) == 'runge-kutta-3' )  THEN      ! for RK3-method
1983
1984       weight_substep(1) = 1._wp/6._wp
1985       weight_substep(2) = 3._wp/10._wp
1986       weight_substep(3) = 8._wp/15._wp
1987
1988       weight_pres(1)    = 1._wp/3._wp
1989       weight_pres(2)    = 5._wp/12._wp
1990       weight_pres(3)    = 1._wp/4._wp
1991
1992    ELSEIF ( TRIM(timestep_scheme) == 'runge-kutta-2' )  THEN  ! for RK2-method
1993
1994       weight_substep(1) = 1._wp/2._wp
1995       weight_substep(2) = 1._wp/2._wp
1996         
1997       weight_pres(1)    = 1._wp/2._wp
1998       weight_pres(2)    = 1._wp/2._wp       
1999
2000    ELSE                                     ! for Euler-method
2001
2002       weight_substep(1) = 1.0_wp     
2003       weight_pres(1)    = 1.0_wp                   
2004
2005    ENDIF
2006
2007!
2008!-- Initialize Rayleigh damping factors
2009    rdf    = 0.0_wp
2010    rdf_sc = 0.0_wp
2011    IF ( rayleigh_damping_factor /= 0.0_wp )  THEN
2012       IF (  .NOT.  ocean )  THEN
2013          DO  k = nzb+1, nzt
2014             IF ( zu(k) >= rayleigh_damping_height )  THEN
2015                rdf(k) = rayleigh_damping_factor *                             &
2016                      ( SIN( pi * 0.5_wp * ( zu(k) - rayleigh_damping_height ) &
2017                             / ( zu(nzt) - rayleigh_damping_height ) )         &
2018                      )**2
2019             ENDIF
2020          ENDDO
2021       ELSE
2022          DO  k = nzt, nzb+1, -1
2023             IF ( zu(k) <= rayleigh_damping_height )  THEN
2024                rdf(k) = rayleigh_damping_factor *                             &
2025                      ( SIN( pi * 0.5_wp * ( rayleigh_damping_height - zu(k) ) &
2026                             / ( rayleigh_damping_height - zu(nzb+1) ) )       &
2027                      )**2
2028             ENDIF
2029          ENDDO
2030       ENDIF
2031    ENDIF
2032    IF ( scalar_rayleigh_damping )  rdf_sc = rdf
2033
2034!
2035!-- Initialize the starting level and the vertical smoothing factor used for
2036!-- the external pressure gradient
2037    dp_smooth_factor = 1.0_wp
2038    IF ( dp_external )  THEN
2039!
2040!--    Set the starting level dp_level_ind_b only if it has not been set before
2041!--    (e.g. in init_grid).
2042       IF ( dp_level_ind_b == 0 )  THEN
2043          ind_array = MINLOC( ABS( dp_level_b - zu ) )
2044          dp_level_ind_b = ind_array(1) - 1 + nzb 
2045                                        ! MINLOC uses lower array bound 1
2046       ENDIF
2047       IF ( dp_smooth )  THEN
2048          dp_smooth_factor(:dp_level_ind_b) = 0.0_wp
2049          DO  k = dp_level_ind_b+1, nzt
2050             dp_smooth_factor(k) = 0.5_wp * ( 1.0_wp + SIN( pi *               &
2051                        ( REAL( k - dp_level_ind_b, KIND=wp ) /                &
2052                          REAL( nzt - dp_level_ind_b, KIND=wp ) - 0.5_wp ) ) )
2053          ENDDO
2054       ENDIF
2055    ENDIF
2056
2057!
2058!-- Initialize damping zone for the potential temperature in case of
2059!-- non-cyclic lateral boundaries. The damping zone has the maximum value
2060!-- at the inflow boundary and decreases to zero at pt_damping_width.
2061    ptdf_x = 0.0_wp
2062    ptdf_y = 0.0_wp
2063    IF ( bc_lr_dirrad )  THEN
2064       DO  i = nxl, nxr
2065          IF ( ( i * dx ) < pt_damping_width )  THEN
2066             ptdf_x(i) = pt_damping_factor * ( SIN( pi * 0.5_wp *              &
2067                            REAL( pt_damping_width - i * dx, KIND=wp ) / (     &
2068                            REAL( pt_damping_width, KIND=wp ) ) ) )**2 
2069          ENDIF
2070       ENDDO
2071    ELSEIF ( bc_lr_raddir )  THEN
2072       DO  i = nxl, nxr
2073          IF ( ( i * dx ) > ( nx * dx - pt_damping_width ) )  THEN
2074             ptdf_x(i) = pt_damping_factor *                                   &
2075                         SIN( pi * 0.5_wp *                                    &
2076                                 ( ( i - nx ) * dx + pt_damping_width ) /      &
2077                                 REAL( pt_damping_width, KIND=wp ) )**2
2078          ENDIF
2079       ENDDO 
2080    ELSEIF ( bc_ns_dirrad )  THEN
2081       DO  j = nys, nyn
2082          IF ( ( j * dy ) > ( ny * dy - pt_damping_width ) )  THEN
2083             ptdf_y(j) = pt_damping_factor *                                   &
2084                         SIN( pi * 0.5_wp *                                    &
2085                                 ( ( j - ny ) * dy + pt_damping_width ) /      &
2086                                 REAL( pt_damping_width, KIND=wp ) )**2
2087          ENDIF
2088       ENDDO 
2089    ELSEIF ( bc_ns_raddir )  THEN
2090       DO  j = nys, nyn
2091          IF ( ( j * dy ) < pt_damping_width )  THEN
2092             ptdf_y(j) = pt_damping_factor *                                   &
2093                         SIN( pi * 0.5_wp *                                    &
2094                                ( pt_damping_width - j * dy ) /                &
2095                                REAL( pt_damping_width, KIND=wp ) )**2
2096          ENDIF
2097       ENDDO
2098    ENDIF
2099
2100!
2101!-- Pre-set masks for regional statistics. Default is the total model domain.
2102!-- Ghost points are excluded because counting values at the ghost boundaries
2103!-- would bias the statistics
2104    rmask = 1.0_wp
2105    rmask(:,nxlg:nxl-1,:) = 0.0_wp;  rmask(:,nxr+1:nxrg,:) = 0.0_wp
2106    rmask(nysg:nys-1,:,:) = 0.0_wp;  rmask(nyn+1:nyng,:,:) = 0.0_wp
2107
2108!
2109!-- User-defined initializing actions. Check afterwards, if maximum number
2110!-- of allowed timeseries is exceeded
2111    CALL user_init
2112
2113    IF ( dots_num > dots_max )  THEN
2114       WRITE( message_string, * ) 'number of time series quantities exceeds',  &
2115                                  ' its maximum of dots_max = ', dots_max,     &
2116                                  ' &Please increase dots_max in modules.f90.'
2117       CALL message( 'init_3d_model', 'PA0194', 1, 2, 0, 6, 0 )   
2118    ENDIF
2119
2120!
2121!-- Input binary data file is not needed anymore. This line must be placed
2122!-- after call of user_init!
2123    CALL close_file( 13 )
2124
2125!
2126!-- Compute total sum of active mask grid points
2127!-- and the mean surface level height for each statistic region
2128!-- ngp_2dh: number of grid points of a horizontal cross section through the
2129!--          total domain
2130!-- ngp_3d:  number of grid points of the total domain
2131    ngp_2dh_outer_l   = 0
2132    ngp_2dh_outer     = 0
2133    ngp_2dh_s_inner_l = 0
2134    ngp_2dh_s_inner   = 0
2135    ngp_2dh_l         = 0
2136    ngp_2dh           = 0
2137    ngp_3d_inner_l    = 0.0_wp
2138    ngp_3d_inner      = 0
2139    ngp_3d            = 0
2140    ngp_sums          = ( nz + 2 ) * ( pr_palm + max_pr_user )
2141
2142    mean_surface_level_height   = 0.0_wp
2143    mean_surface_level_height_l = 0.0_wp
2144
2145!
2146!-- To do: New concept for these non-topography grid points!
2147    DO  sr = 0, statistic_regions
2148       DO  i = nxl, nxr
2149          DO  j = nys, nyn
2150             IF ( rmask(j,i,sr) == 1.0_wp )  THEN
2151!
2152!--             All xy-grid points
2153                ngp_2dh_l(sr) = ngp_2dh_l(sr) + 1
2154!
2155!--             Determine mean surface-level height. In case of downward-
2156!--             facing walls are present, more than one surface level exist.
2157!--             In this case, use the lowest surface-level height.
2158                IF ( surf_def_h(0)%start_index(j,i) <=                         &
2159                     surf_def_h(0)%end_index(j,i) )  THEN
2160                   m = surf_def_h(0)%start_index(j,i)
2161                   k = surf_def_h(0)%k(m)
2162                   mean_surface_level_height_l(sr) =                           &
2163                                       mean_surface_level_height_l(sr) + zw(k-1)
2164                ENDIF
2165                IF ( surf_lsm_h%start_index(j,i) <=                            &
2166                     surf_lsm_h%end_index(j,i) )  THEN
2167                   m = surf_lsm_h%start_index(j,i)
2168                   k = surf_lsm_h%k(m)
2169                   mean_surface_level_height_l(sr) =                           &
2170                                       mean_surface_level_height_l(sr) + zw(k-1)
2171                ENDIF
2172                IF ( surf_usm_h%start_index(j,i) <=                            &
2173                     surf_usm_h%end_index(j,i) )  THEN
2174                   m = surf_usm_h%start_index(j,i)
2175                   k = surf_usm_h%k(m)
2176                   mean_surface_level_height_l(sr) =                           &
2177                                       mean_surface_level_height_l(sr) + zw(k-1)
2178                ENDIF
2179
2180                k_surf = k - 1
2181
2182                DO  k = nzb, nzt+1
2183!
2184!--                xy-grid points above topography
2185                   ngp_2dh_outer_l(k,sr) = ngp_2dh_outer_l(k,sr)     +         &
2186                                  MERGE( 1, 0, BTEST( wall_flags_0(k,j,i), 24 ) )
2187
2188                   ngp_2dh_s_inner_l(k,sr) = ngp_2dh_s_inner_l(k,sr) +         &
2189                                  MERGE( 1, 0, BTEST( wall_flags_0(k,j,i), 22 ) )
2190
2191                ENDDO
2192!
2193!--             All grid points of the total domain above topography
2194                ngp_3d_inner_l(sr) = ngp_3d_inner_l(sr) + ( nz - k_surf + 2 )
2195
2196
2197
2198             ENDIF
2199          ENDDO
2200       ENDDO
2201    ENDDO
2202
2203    sr = statistic_regions + 1
2204#if defined( __parallel )
2205    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2206    CALL MPI_ALLREDUCE( ngp_2dh_l(0), ngp_2dh(0), sr, MPI_INTEGER, MPI_SUM,    &
2207                        comm2d, ierr )
2208    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2209    CALL MPI_ALLREDUCE( ngp_2dh_outer_l(0,0), ngp_2dh_outer(0,0), (nz+2)*sr,   &
2210                        MPI_INTEGER, MPI_SUM, comm2d, ierr )
2211    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2212    CALL MPI_ALLREDUCE( ngp_2dh_s_inner_l(0,0), ngp_2dh_s_inner(0,0),          &
2213                        (nz+2)*sr, MPI_INTEGER, MPI_SUM, comm2d, ierr )
2214    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2215    CALL MPI_ALLREDUCE( ngp_3d_inner_l(0), ngp_3d_inner_tmp(0), sr, MPI_REAL,  &
2216                        MPI_SUM, comm2d, ierr )
2217    ngp_3d_inner = INT( ngp_3d_inner_tmp, KIND = SELECTED_INT_KIND( 18 ) )
2218    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2219    CALL MPI_ALLREDUCE( mean_surface_level_height_l(0),                        &
2220                        mean_surface_level_height(0), sr, MPI_REAL,            &
2221                        MPI_SUM, comm2d, ierr )
2222    mean_surface_level_height = mean_surface_level_height / REAL( ngp_2dh )
2223#else
2224    ngp_2dh         = ngp_2dh_l
2225    ngp_2dh_outer   = ngp_2dh_outer_l
2226    ngp_2dh_s_inner = ngp_2dh_s_inner_l
2227    ngp_3d_inner    = INT( ngp_3d_inner_l, KIND = SELECTED_INT_KIND( 18 ) )
2228    mean_surface_level_height = mean_surface_level_height_l / REAL( ngp_2dh_l )
2229#endif
2230
2231    ngp_3d = INT ( ngp_2dh, KIND = SELECTED_INT_KIND( 18 ) ) * &
2232             INT ( (nz + 2 ), KIND = SELECTED_INT_KIND( 18 ) )
2233
2234!
2235!-- Set a lower limit of 1 in order to avoid zero divisions in flow_statistics,
2236!-- buoyancy, etc. A zero value will occur for cases where all grid points of
2237!-- the respective subdomain lie below the surface topography
2238    ngp_2dh_outer   = MAX( 1, ngp_2dh_outer(:,:)   ) 
2239    ngp_3d_inner    = MAX( INT(1, KIND = SELECTED_INT_KIND( 18 )),             &
2240                           ngp_3d_inner(:) )
2241    ngp_2dh_s_inner = MAX( 1, ngp_2dh_s_inner(:,:) ) 
2242
2243    DEALLOCATE( mean_surface_level_height_l, ngp_2dh_l, ngp_2dh_outer_l,       &
2244                ngp_3d_inner_l, ngp_3d_inner_tmp )
2245
2246    CALL location_message( 'leaving init_3d_model', .TRUE. )
2247
2248 END SUBROUTINE init_3d_model
Note: See TracBrowser for help on using the repository browser.