source: palm/trunk/SOURCE/timestep.f90 @ 866

Last change on this file since 866 was 866, checked in by raasch, 12 years ago

bugfix for timestep calculation in case of Galilei transformation

  • Property svn:keywords set to Id
File size: 15.9 KB
Line 
1 SUBROUTINE timestep
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! ------------------
6! bugfix for timestep calculation in case of Galilei transformation,
7! special treatment in case of mirror velocity boundary condition removed
8!
9! Former revisions:
10! -----------------
11! $Id: timestep.f90 866 2012-03-28 06:44:41Z raasch $
12!
13! 707 2011-03-29 11:39:40Z raasch
14! bc_lr/ns replaced by bc_lr/ns_cyc
15!
16! 667 2010-12-23 12:06:00Z suehring/gryschka
17! Exchange of terminate_coupled between ocean and atmosphere via PE0
18! Minimum grid spacing dxyz2_min(k) is now calculated using dzw instead of dzu
19!
20! 622 2010-12-10 08:08:13Z raasch
21! optional barriers included in order to speed up collective operations
22!
23! 343 2009-06-24 12:59:09Z maronga
24! Additional timestep criterion in case of simulations with plant canopy
25! Output of messages replaced by message handling routine.
26!
27! 222 2009-01-12 16:04:16Z letzel
28! Implementation of a MPI-1 Coupling: replaced myid with target_id
29! Bugfix for nonparallel execution
30!
31! 108 2007-08-24 15:10:38Z letzel
32! modifications to terminate coupled runs
33!
34! RCS Log replace by Id keyword, revision history cleaned up
35!
36! Revision 1.21  2006/02/23 12:59:44  raasch
37! nt_anz renamed current_timestep_number
38!
39! Revision 1.1  1997/08/11 06:26:19  raasch
40! Initial revision
41!
42!
43! Description:
44! ------------
45! Compute the time step under consideration of the FCL and diffusion criterion.
46!------------------------------------------------------------------------------!
47
48    USE arrays_3d
49    USE control_parameters
50    USE cpulog
51    USE grid_variables
52    USE indices
53    USE interfaces
54    USE pegrid
55    USE statistics
56
57    IMPLICIT NONE
58
59    INTEGER ::  i, j, k, u_max_cfl_ijk(3), v_max_cfl_ijk(3)
60
61    REAL ::  div, dt_diff, dt_diff_l, dt_plant_canopy,                 &
62             dt_plant_canopy_l,                                        &
63             dt_plant_canopy_u, dt_plant_canopy_v, dt_plant_canopy_w,  & 
64             dt_u, dt_v, dt_w, lad_max, percent_change,                &
65             u_gtrans_l, u_max_cfl, vabs_max, value, v_gtrans_l, v_max_cfl
66
67    REAL, DIMENSION(2)         ::  uv_gtrans, uv_gtrans_l
68    REAL, DIMENSION(nzb+1:nzt) ::  dxyz2_min
69
70
71
72    CALL cpu_log( log_point(12), 'calculate_timestep', 'start' )
73
74!
75!-- In case of Galilei-transform not using the geostrophic wind as translation
76!-- velocity, compute the volume-averaged horizontal velocity components, which
77!-- will then be subtracted from the horizontal wind for the time step and
78!-- horizontal advection routines.
79    IF ( galilei_transformation  .AND. .NOT.  use_ug_for_galilei_tr )  THEN
80       IF ( flow_statistics_called )  THEN
81!
82!--       Horizontal averages already existent, just need to average them
83!--       vertically.
84          u_gtrans = 0.0
85          v_gtrans = 0.0
86          DO  k = nzb+1, nzt
87             u_gtrans = u_gtrans + hom(k,1,1,0)
88             v_gtrans = v_gtrans + hom(k,1,2,0)
89          ENDDO
90          u_gtrans = u_gtrans / REAL( nzt - nzb )
91          v_gtrans = v_gtrans / REAL( nzt - nzb )
92       ELSE
93!
94!--       Averaging over the entire model domain.
95          uv_gtrans_l = 0.0
96          DO  i = nxl, nxr
97             DO  j = nys, nyn
98                DO  k = nzb+1, nzt
99                   uv_gtrans_l(1) = uv_gtrans_l(1) + u(k,j,i)
100                   uv_gtrans_l(2) = uv_gtrans_l(2) + v(k,j,i)
101                ENDDO
102             ENDDO
103          ENDDO
104          uv_gtrans_l = uv_gtrans_l / REAL( (nxr-nxl+1)*(nyn-nys+1)*(nzt-nzb) )
105#if defined( __parallel )
106          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
107          CALL MPI_ALLREDUCE( uv_gtrans_l, uv_gtrans, 2, MPI_REAL, MPI_SUM, &
108                              comm2d, ierr )
109          u_gtrans = uv_gtrans(1) / REAL( numprocs )
110          v_gtrans = uv_gtrans(2) / REAL( numprocs )
111#else
112          u_gtrans = uv_gtrans_l(1)
113          v_gtrans = uv_gtrans_l(2)
114#endif
115       ENDIF
116    ENDIF
117
118!
119!-- Determine the maxima of the velocity components.
120    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, u, 'abs', 0.0, &
121                         u_max, u_max_ijk )
122    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, v, 'abs', 0.0, &
123                         v_max, v_max_ijk )
124    CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, w, 'abs', 0.0, &
125                         w_max, w_max_ijk )
126
127!
128!-- In case of Galilei transformation, the horizontal velocity maxima have
129!-- to be calculated from the transformed horizontal velocities
130    IF ( galilei_transformation )  THEN
131       CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, u, 'absoff', &
132                            u_gtrans, u_max_cfl, u_max_cfl_ijk )
133       CALL global_min_max( nzb, nzt+1, nysg, nyng, nxlg, nxrg, v, 'absoff', &
134                            v_gtrans, v_max_cfl, v_max_cfl_ijk )
135    ELSE
136       u_max_cfl = u_max
137       v_max_cfl = v_max
138       u_max_cfl_ijk = u_max_ijk
139       v_max_cfl_ijk = v_max_ijk
140    ENDIF
141
142
143    IF ( .NOT. dt_fixed )  THEN
144!
145!--    Variable time step:
146!
147!--    For each component, compute the maximum time step according to the
148!--    CFL-criterion.
149       dt_u = dx / ( ABS( u_max_cfl ) + 1.0E-10 )
150       dt_v = dy / ( ABS( v_max_cfl ) + 1.0E-10 )
151       dt_w = dzu(MAX( 1, w_max_ijk(1) )) / ( ABS( w_max ) + 1.0E-10 )
152
153!
154!--    Compute time step according to the diffusion criterion.
155!--    First calculate minimum grid spacing which only depends on index k
156!--    Note: also at k=nzb+1 a full grid length is being assumed, although
157!--          in the Prandtl-layer friction term only dz/2 is used.
158!--          Experience from the old model seems to justify this.
159       dt_diff_l = 999999.0
160
161       DO  k = nzb+1, nzt
162           dxyz2_min(k) = MIN( dx2, dy2, dzw(k)*dzw(k) ) * 0.125
163       ENDDO
164
165!$OMP PARALLEL private(i,j,k,value) reduction(MIN: dt_diff_l)
166!$OMP DO
167       DO  i = nxl, nxr
168          DO  j = nys, nyn
169             DO  k = nzb+1, nzt
170                value = dxyz2_min(k) / ( MAX( kh(k,j,i), km(k,j,i) ) + 1E-20 )
171
172                dt_diff_l = MIN( value, dt_diff_l )
173             ENDDO
174          ENDDO
175       ENDDO
176!$OMP END PARALLEL
177#if defined( __parallel )
178       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
179       CALL MPI_ALLREDUCE( dt_diff_l, dt_diff, 1, MPI_REAL, MPI_MIN, comm2d, &
180                           ierr )
181#else
182       dt_diff = dt_diff_l
183#endif
184
185!
186!--    In case of non-cyclic lateral boundaries, the diffusion time step
187!--    may be further restricted by the lateral damping layer (damping only
188!--    along x and y)
189       IF ( .NOT. bc_lr_cyc )  THEN
190          dt_diff = MIN( dt_diff, 0.125 * dx2 / ( km_damp_max + 1E-20 ) )
191       ELSEIF ( .NOT. bc_ns_cyc )  THEN
192          dt_diff = MIN( dt_diff, 0.125 * dy2 / ( km_damp_max + 1E-20 ) )
193       ENDIF
194
195!
196!--    Additional timestep criterion with plant canopies:
197!--    it is not allowed to extract more than the available momentum
198       IF ( plant_canopy ) THEN
199
200          dt_plant_canopy_l = 0.0
201          DO  i = nxl, nxr
202             DO  j = nys, nyn
203                DO k = nzb+1, nzt
204                   dt_plant_canopy_u = cdc(k,j,i) * lad_u(k,j,i) *  &
205                                       SQRT(     u(k,j,i)**2     +  &
206                                             ( ( v(k,j,i-1)      +  &
207                                                 v(k,j,i)        +  &
208                                                 v(k,j+1,i)      +  &
209                                                 v(k,j+1,i-1) )     &
210                                               / 4.0 )**2        +  &
211                                             ( ( w(k-1,j,i-1)    +  &
212                                                 w(k-1,j,i)      +  &
213                                                 w(k,j,i-1)      +  &
214                                                 w(k,j,i) )         &
215                                                 / 4.0 )**2 ) 
216                   IF ( dt_plant_canopy_u > dt_plant_canopy_l ) THEN
217                      dt_plant_canopy_l = dt_plant_canopy_u 
218                   ENDIF
219                   dt_plant_canopy_v = cdc(k,j,i) * lad_v(k,j,i) *  &
220                                       SQRT( ( ( u(k,j-1,i)      +  &
221                                                 u(k,j-1,i+1)    +  &
222                                                 u(k,j,i)        +  &
223                                                 u(k,j,i+1) )       &
224                                               / 4.0 )**2        +  &
225                                                 v(k,j,i)**2     +  &
226                                             ( ( w(k-1,j-1,i)    +  &
227                                                 w(k-1,j,i)      +  &
228                                                 w(k,j-1,i)      +  &
229                                                 w(k,j,i) )         &
230                                                 / 4.0 )**2 ) 
231                   IF ( dt_plant_canopy_v > dt_plant_canopy_l ) THEN
232                      dt_plant_canopy_l = dt_plant_canopy_v
233                   ENDIF                   
234                   dt_plant_canopy_w = cdc(k,j,i) * lad_w(k,j,i) *  &
235                                       SQRT( ( ( u(k,j,i)        +  &
236                                                 u(k,j,i+1)      +  &
237                                                 u(k+1,j,i)      +  &
238                                                 u(k+1,j,i+1) )     &
239                                               / 4.0 )**2        +  &
240                                             ( ( v(k,j,i)        +  &
241                                                 v(k,j+1,i)      +  &
242                                                 v(k+1,j,i)      +  &
243                                                 v(k+1,j+1,i) )     &
244                                               / 4.0 )**2        +  &
245                                                 w(k,j,i)**2 )     
246                   IF ( dt_plant_canopy_w > dt_plant_canopy_l ) THEN
247                      dt_plant_canopy_l = dt_plant_canopy_w
248                   ENDIF
249                ENDDO
250             ENDDO
251          ENDDO 
252
253          IF ( dt_plant_canopy_l > 0.0 ) THEN
254!
255!--          Invert dt_plant_canopy_l and apply a security timestep factor 0.1
256             dt_plant_canopy_l = 0.1 / dt_plant_canopy_l
257          ELSE
258!
259!--          In case of inhomogeneous plant canopy, some processors may have no
260!--          canopy at all. Then use dt_max as dummy instead.
261             dt_plant_canopy_l = dt_max
262          ENDIF
263
264!
265!--       Determine the global minumum
266#if defined( __parallel )
267          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
268          CALL MPI_ALLREDUCE( dt_plant_canopy_l, dt_plant_canopy, 1, MPI_REAL, &
269                              MPI_MIN, comm2d, ierr )
270#else
271          dt_plant_canopy = dt_plant_canopy_l
272#endif
273
274       ELSE
275!
276!--       Use dt_diff as dummy value to avoid extra IF branches further below
277          dt_plant_canopy = dt_diff
278
279       ENDIF
280
281!
282!--    The time step is the minimum of the 3-4 components and the diffusion time
283!--    step minus a reduction to be on the safe side. Factor 0.5 is necessary
284!--    since the leap-frog scheme always progresses by 2 * delta t.
285!--    The user has to set the cfl_factor small enough to ensure that the
286!--    divergences do not become too large.
287!--    The time step must not exceed the maximum allowed value.
288       IF ( timestep_scheme(1:5) == 'runge' )  THEN
289          dt_3d = cfl_factor * MIN( dt_diff, dt_plant_canopy, dt_u, dt_v, dt_w )
290       ELSE
291          dt_3d = cfl_factor * 0.5 *  &
292                  MIN( dt_diff, dt_plant_canopy, dt_u, dt_v, dt_w )
293       ENDIF
294       dt_3d = MIN( dt_3d, dt_max )
295
296!
297!--    Remember the restricting time step criterion for later output.
298       IF ( MIN( dt_u, dt_v, dt_w ) < MIN( dt_diff, dt_plant_canopy ) )  THEN
299          timestep_reason = 'A'
300       ELSEIF ( dt_plant_canopy < dt_diff )  THEN
301          timestep_reason = 'C'
302       ELSE
303          timestep_reason = 'D'
304       ENDIF
305
306!
307!--    Set flag if the time step becomes too small.
308       IF ( dt_3d < ( 0.00001 * dt_max ) )  THEN
309          stop_dt = .TRUE.
310
311          WRITE( message_string, * ) 'Time step has reached minimum limit.',  &
312               '&dt              = ', dt_3d, ' s  Simulation is terminated.', &
313               '&old_dt          = ', old_dt, ' s',                           &
314               '&dt_u            = ', dt_u, ' s',                             &
315               '&dt_v            = ', dt_v, ' s',                             &
316               '&dt_w            = ', dt_w, ' s',                             &
317               '&dt_diff         = ', dt_diff, ' s',                          &
318               '&dt_plant_canopy = ', dt_plant_canopy, ' s',                  &
319               '&u_max_cfl   = ', u_max_cfl, ' m/s   k=', u_max_cfl_ijk(1),   &
320               '  j=', u_max_ijk(2), '  i=', u_max_ijk(3),                    &
321               '&v_max_cfl   = ', v_max_cfl, ' m/s   k=', v_max_cfl_ijk(1),   &
322               '  j=', v_max_ijk(2), '  i=', v_max_ijk(3),                    &
323               '&w_max       = ', w_max, ' m/s   k=', w_max_ijk(1),           &
324               '  j=', w_max_ijk(2), '  i=', w_max_ijk(3)
325          CALL message( 'timestep', 'PA0312', 0, 1, 0, 6, 0 )
326!
327!--       In case of coupled runs inform the remote model of the termination
328!--       and its reason, provided the remote model has not already been
329!--       informed of another termination reason (terminate_coupled > 0) before.
330#if defined( __parallel )
331          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0 )  THEN
332             terminate_coupled = 2
333             IF ( myid == 0 ) THEN
334                CALL MPI_SENDRECV( &
335                     terminate_coupled,        1, MPI_INTEGER, target_id,  0, &
336                     terminate_coupled_remote, 1, MPI_INTEGER, target_id,  0, &
337                     comm_inter, status, ierr )
338             ENDIF
339             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr)
340          ENDIF
341#endif
342       ENDIF
343
344!
345!--    Ensure a smooth value (two significant digits) of the timestep. For
346!--    other schemes than Runge-Kutta, the following restrictions appear:
347!--    The current timestep is only then changed, if the change relative to
348!--    its previous value exceeds +5 % or -2 %. In case of a timestep
349!--    reduction, at least 30 iterations have to be performed before a timestep
350!--    enlargement is permitted again.
351       percent_change = dt_3d / old_dt - 1.0
352       IF ( percent_change > 0.05  .OR.  percent_change < -0.02  .OR. &
353            timestep_scheme(1:5) == 'runge' )  THEN
354
355!
356!--       Time step enlargement by no more than 2 %.
357          IF ( percent_change > 0.0  .AND.  simulated_time /= 0.0  .AND. &
358               timestep_scheme(1:5) /= 'runge' )  THEN
359             dt_3d = 1.02 * old_dt
360          ENDIF
361
362!
363!--       A relatively smooth value of the time step is ensured by taking
364!--       only the first two significant digits.
365          div = 1000.0
366          DO  WHILE ( dt_3d < div )
367             div = div / 10.0
368          ENDDO
369          dt_3d = NINT( dt_3d * 100.0 / div ) * div / 100.0
370
371!
372!--       Now the time step can be adjusted.
373          IF ( percent_change < 0.0  .OR.  timestep_scheme(1:5) == 'runge' ) &
374          THEN
375!
376!--          Time step reduction.
377             old_dt = dt_3d
378             dt_changed = .TRUE.
379          ELSE
380!
381!--          For other timestep schemes , the time step is only enlarged
382!--          after at least 30 iterations since the previous time step
383!--          change or, of course, after model initialization.
384             IF ( current_timestep_number >= last_dt_change + 30  .OR. &
385                  simulated_time == 0.0 )  THEN
386                old_dt = dt_3d
387                dt_changed = .TRUE.
388             ELSE
389                dt_3d = old_dt
390                dt_changed = .FALSE.
391             ENDIF
392
393          ENDIF
394       ELSE
395!
396!--       No time step change since the difference is too small.
397          dt_3d = old_dt
398          dt_changed = .FALSE.
399       ENDIF
400
401       IF ( dt_changed )  last_dt_change = current_timestep_number
402
403    ENDIF
404    CALL cpu_log( log_point(12), 'calculate_timestep', 'stop' )
405
406 END SUBROUTINE timestep
Note: See TracBrowser for help on using the repository browser.