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

Last change on this file since 1960 was 1960, checked in by suehring, 8 years ago

Separate balance equations for humidity and passive_scalar

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