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

Last change on this file since 3274 was 3274, checked in by knoop, 6 years ago

Modularization of all bulk cloud physics code components

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