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

Last change on this file since 485 was 485, checked in by raasch, 14 years ago

ngp_p3d changed to 64 bit integer

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