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

Last change on this file since 3607 was 3589, checked in by suehring, 5 years ago

Remove erroneous UTF encoding; last commit documented

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