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

Last change on this file since 3289 was 3289, checked in by suehring, 6 years ago

last commit documented

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