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

Last change on this file since 2123 was 2119, checked in by raasch, 7 years ago

last commit documented

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