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

Last change on this file since 389 was 388, checked in by raasch, 15 years ago

in-situ AND potential density are calculated and used in the ocean version

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