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

Last change on this file since 978 was 978, checked in by fricke, 12 years ago

merge fricke branch back into trunk

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