source: palm/tags/release-4.0/SOURCE/check_for_restart.f90 @ 3049

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

last commit documented

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