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

Last change on this file since 2338 was 2338, checked in by gronemeier, 7 years ago

modularized 1d model

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