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

Last change on this file since 1762 was 1762, checked in by hellstea, 8 years ago

Introduction of nested domain system

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