source: palm/trunk/SOURCE/check_for_restart.f90 @ 1509

Last change on this file since 1509 was 1509, checked in by heinze, 9 years ago

bugfix: prevent infinite loop in case of automatic restarts

  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1 SUBROUTINE check_for_restart
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-2014 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22! bugfix: prevent infinite loop in case of automatic restarts
23!
24! Former revisions:
25! -----------------
26! $Id: check_for_restart.f90 1509 2014-12-16 08:56:46Z heinze $
27!
28! 1468 2014-09-24 14:06:57Z maronga
29! Added support for unscheduled job termination using the flag files
30! DO_STOP_NOW and DO_RESTART_NOW
31!
32! 1353 2014-04-08 15:21:23Z heinze
33! REAL constants provided with KIND-attribute
34!
35! 1320 2014-03-20 08:40:49Z raasch
36! ONLY-attribute added to USE-statements,
37! kind-parameters added to all INTEGER and REAL declaration statements,
38! kinds are defined in new module kinds,
39! revision history before 2012 removed,
40! comment fields (!:) to be used for variable explanations added to
41! all variable declaration statements
42!
43! 1036 2012-10-22 13:43:42Z raasch
44! code put under GPL (PALM 3.9)
45!
46! 1032 2012-10-21 13:03:21Z letzel
47! minor reformatting
48!
49! Revision 1.1  1998/03/18 20:06:51  raasch
50! Initial revision
51!
52!
53! Description:
54! ------------
55! Set stop flag, if restart is neccessary because of expiring cpu-time or
56! if it is forced by user
57!------------------------------------------------------------------------------!
58
59    USE control_parameters,                                                    &
60        ONLY:  coupling_mode, dt_restart, end_time, message_string,            &
61               run_description_header, simulated_time, terminate_coupled,      &
62               terminate_coupled_remote, terminate_run,                        &
63               termination_time_needed, time_restart,                          &
64               time_since_reference_point, write_binary
65    USE kinds
66    USE pegrid
67
68    IMPLICIT NONE
69
70
71    LOGICAL :: terminate_run_l           !:
72    LOGICAL :: do_stop_now = .FALSE.     !:
73    LOGICAL :: do_restart_now = .FALSE.  !:
74
75    REAL(wp) ::  remaining_time !:
76
77
78!
79!-- Check remaining CPU-time
80    CALL local_tremain( remaining_time )
81
82!
83!-- If necessary set a flag to stop the model run
84    terminate_run_l = .FALSE.
85    IF ( remaining_time <= termination_time_needed  .AND.                      &
86         write_binary(1:4) == 'true' )  THEN
87
88       terminate_run_l = .TRUE.
89    ENDIF
90
91#if defined( __parallel )
92!
93!-- Make a logical OR for all processes. Stop the model run if at least
94!-- one processor has reached the time limit.
95    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
96    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL,        &
97                        MPI_LOR, comm2d, ierr )
98#else
99    terminate_run = terminate_run_l
100#endif
101
102!
103!-- Output that job will be terminated
104    IF ( terminate_run  .AND.  myid == 0 )  THEN
105       WRITE( message_string, * ) 'run will be terminated because it is ',     &
106                       'running out of job cpu limit & ',                      &
107                       'remaining time:         ', remaining_time, ' s',       &
108                       'termination time needed:', termination_time_needed, ' s'
109       CALL message( 'check_for_restart', 'PA0163', 0, 1, 0, 6, 0 )
110    ENDIF
111
112!
113!-- In case of coupled runs inform the remote model of the termination
114!-- and its reason, provided the remote model has not already been
115!-- informed of another termination reason (terminate_coupled > 0) before,
116!-- or vice versa (terminate_coupled_remote > 0).
117    IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled'  .AND.       &
118         terminate_coupled == 0  .AND.  terminate_coupled_remote == 0 )  THEN
119
120       terminate_coupled = 3
121
122#if defined( __parallel )
123       IF ( myid == 0 ) THEN
124          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,         &
125                             target_id, 0,                                     &
126                             terminate_coupled_remote, 1, MPI_INTEGER,         &
127                             target_id, 0,                                     &
128                             comm_inter, status, ierr )
129       ENDIF
130       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d,    &
131                       ierr )
132#endif
133    ENDIF
134
135
136!
137!-- Check if a flag file exists that forces a termination of the model
138    IF ( myid == 0 )  THEN
139       INQUIRE(FILE="DO_STOP_NOW", EXIST=do_stop_now)
140       INQUIRE(FILE="DO_RESTART_NOW", EXIST=do_restart_now)
141
142       IF ( do_stop_now .OR. do_restart_now )  THEN
143
144          terminate_run_l = .TRUE.
145
146          WRITE( message_string, * ) 'run will be terminated because user ',   &
147                                  'forced a job finialization using a flag',   &
148                                  'file:',                                     &
149                                  '&DO_STOP_NOW: ', do_stop_now,               &
150                                  '&DO_RESTART_NOW: ', do_restart_now 
151          CALL message( 'check_for_restart', 'PA0398', 0, 0, 0, 6, 0 )
152
153       ENDIF
154    ENDIF
155
156
157#if defined( __parallel )
158!
159!-- Make a logical OR for all processes. Stop the model run if at least
160!-- one processor has reached the time limit.
161    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
162    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL,        &
163                        MPI_LOR, comm2d, ierr )
164#else
165    terminate_run = terminate_run_l
166#endif
167
168!
169!-- In case of coupled runs inform the remote model of the termination
170!-- and its reason, provided the remote model has not already been
171!-- informed of another termination reason (terminate_coupled > 0) before,
172!-- or vice versa (terminate_coupled_remote > 0).
173    IF ( terminate_run .AND. coupling_mode /= 'uncoupled' .AND.                &
174         terminate_coupled == 0 .AND.  terminate_coupled_remote == 0 )  THEN
175
176       terminate_coupled = 6
177
178#if defined( __parallel )
179       IF ( myid == 0 ) THEN
180          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,      &
181                             target_id,  0,                                 &
182                             terminate_coupled_remote, 1, MPI_INTEGER,      &
183                             target_id,  0,                                 &
184                             comm_inter, status, ierr )   
185       ENDIF
186       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0,         &
187                       comm2d, ierr ) 
188#endif
189
190    ENDIF
191
192!
193!-- Set the stop flag also, if restart is forced by user settings
194    IF ( time_restart /= 9999999.9_wp  .AND.                                   &
195         time_restart < time_since_reference_point )  THEN
196
197!
198!--    Restart is not neccessary, if the end time of the run (given by
199!--    the user) has been reached
200       IF ( simulated_time < end_time )  THEN
201          terminate_run = .TRUE.
202!
203!--       Increment restart time, if forced by user, otherwise set restart
204!--       time to default (no user restart)
205          IF ( dt_restart /= 9999999.9_wp )  THEN
206             time_restart = time_restart + dt_restart
207          ELSE
208             time_restart = 9999999.9_wp
209          ENDIF
210
211          WRITE( message_string, * ) 'run will be terminated due to user ',    &
212                                  'settings of',                               &
213                                  '&restart_time / dt_restart',                &
214                                  '&new restart time is: ', time_restart, ' s' 
215          CALL message( 'check_for_restart', 'PA0164', 0, 0, 0, 6, 0 )
216 
217!
218!--       In case of coupled runs inform the remote model of the termination
219!--       and its reason, provided the remote model has not already been
220!--       informed of another termination reason (terminate_coupled > 0) before,
221!--       or vice versa (terminate_coupled_remote > 0).
222          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0       &
223               .AND.  terminate_coupled_remote == 0 )  THEN
224
225             IF ( dt_restart /= 9999999.9_wp )  THEN
226                terminate_coupled = 4
227             ELSE
228                terminate_coupled = 5
229             ENDIF
230#if defined( __parallel )
231             IF ( myid == 0 ) THEN
232                CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,   &
233                                   target_id,  0,                              &
234                                   terminate_coupled_remote, 1, MPI_INTEGER,   &
235                                   target_id,  0,                              &
236                                   comm_inter, status, ierr )   
237             ENDIF
238             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0,      &
239                             comm2d, ierr ) 
240#endif
241          ENDIF
242       ELSE
243          time_restart = 9999999.9_wp
244       ENDIF
245    ENDIF
246
247!
248!-- If the run is stopped, set a flag file which is necessary to initiate
249!-- the start of a continuation run, except if the user forced to stop the
250!-- run without restart
251    IF ( terminate_run  .AND.  myid == 0 .AND. .NOT. do_stop_now)  THEN
252
253       OPEN ( 90, FILE='CONTINUE_RUN', FORM='FORMATTED' )
254       WRITE ( 90, '(A)' )  TRIM( run_description_header )
255       CLOSE ( 90 )
256
257    ENDIF
258
259
260 END SUBROUTINE check_for_restart
Note: See TracBrowser for help on using the repository browser.