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

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

Revise mapping of 3D-buildings onto orography; further bugfix concering call of user_init (see commit -r 2865)

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