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

Last change on this file since 2930 was 2920, checked in by kanani, 6 years ago

Optimize SVF calculation, clean-up, bugfixes

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