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

Last change on this file since 418 was 407, checked in by maronga, 14 years ago

humidity for non-flat topography implemented, re-adjustments for lcxt4

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