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

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