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

Last change on this file since 2980 was 2977, checked in by kanani, 6 years ago

Fixes for radiative transfer model

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