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

Last change on this file since 4015 was 3987, checked in by kanani, 5 years ago

clean up location, debug and error messages

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