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

Last change on this file since 1745 was 1683, checked in by knoop, 9 years ago

last commit documented

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