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

Last change on this file since 3159 was 3159, checked in by sward, 6 years ago

Added multi agent system

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