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

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

all OpenACC directives and related parts removed from the code

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