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

Last change on this file since 1036 was 1036, checked in by raasch, 11 years ago

code has been put under the GNU General Public License (v3)

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