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

Last change on this file since 3731 was 3711, checked in by knoop, 5 years ago

Introduced module_interface_init_checks for post-init checks

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