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

Last change on this file since 2037 was 2037, checked in by knoop, 8 years ago

Anelastic approximation implemented

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