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

Last change on this file since 1429 was 1429, checked in by knoop, 10 years ago

Ensemble run capability added to parallel random number generator.

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