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

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

last commit documented

  • Property svn:keywords set to Id
File size: 9.9 KB
RevLine 
[1682]1!> @file check_for_restart.f90
[1036]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!
[1310]16! Copyright 1997-2014 Leibniz Universitaet Hannover
[1036]17!--------------------------------------------------------------------------------!
18!
[247]19! Current revisions:
[1]20! -----------------
[1354]21!
[1683]22!
[1321]23! Former revisions:
24! -----------------
25! $Id: check_for_restart.f90 1683 2015-10-07 23:57:51Z maronga $
26!
[1683]27! 1682 2015-10-07 23:56:08Z knoop
28! Code annotations made doxygen readable
29!
[1510]30! 1509 2014-12-16 08:56:46Z heinze
31! bugfix: prevent infinite loop in case of automatic restarts
32!
[1469]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!
[1354]37! 1353 2014-04-08 15:21:23Z heinze
38! REAL constants provided with KIND-attribute
39!
[1321]40! 1320 2014-03-20 08:40:49Z raasch
[1320]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
[1]47!
[1037]48! 1036 2012-10-22 13:43:42Z raasch
49! code put under GPL (PALM 3.9)
50!
[1033]51! 1032 2012-10-21 13:03:21Z letzel
52! minor reformatting
53!
[1]54! Revision 1.1  1998/03/18 20:06:51  raasch
55! Initial revision
56!
57!
58! Description:
59! ------------
[1682]60!> Set stop flag, if restart is neccessary because of expiring cpu-time or
61!> if it is forced by user
[1]62!------------------------------------------------------------------------------!
[1682]63 SUBROUTINE check_for_restart
64 
[1]65
[1320]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
[1]73    USE pegrid
74
75    IMPLICIT NONE
76
77
[1682]78    LOGICAL :: terminate_run_l           !<
79    LOGICAL :: do_stop_now = .FALSE.     !<
80    LOGICAL :: do_restart_now = .FALSE.  !<
[1]81
[1682]82    REAL(wp) ::  remaining_time !<
[1]83
[1320]84
[1]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.
[1320]92    IF ( remaining_time <= termination_time_needed  .AND.                      &
[1]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.
[622]102    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[1320]103    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL,        &
[1]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
[1320]112       WRITE( message_string, * ) 'run will be terminated because it is ',     &
113                       'running out of job cpu limit & ',                      &
114                       'remaining time:         ', remaining_time, ' s',       &
[274]115                       'termination time needed:', termination_time_needed, ' s'
[247]116       CALL message( 'check_for_restart', 'PA0163', 0, 1, 0, 6, 0 )
[1]117    ENDIF
118
119!
[108]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).
[1320]124    IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled'  .AND.       &
[110]125         terminate_coupled == 0  .AND.  terminate_coupled_remote == 0 )  THEN
126
[108]127       terminate_coupled = 3
[1468]128
[222]129#if defined( __parallel )
[667]130       IF ( myid == 0 ) THEN
[1032]131          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,         &
132                             target_id, 0,                                     &
133                             terminate_coupled_remote, 1, MPI_INTEGER,         &
134                             target_id, 0,                                     &
[667]135                             comm_inter, status, ierr )
136       ENDIF
[1320]137       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d,    &
[1032]138                       ierr )
[222]139#endif
[108]140    ENDIF
141
[1468]142
[108]143!
[1468]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.                                   &
[291]202         time_restart < time_since_reference_point )  THEN
203
[1]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)
[1353]212          IF ( dt_restart /= 9999999.9_wp )  THEN
[1]213             time_restart = time_restart + dt_restart
214          ELSE
[1353]215             time_restart = 9999999.9_wp
[1]216          ENDIF
217
[1320]218          WRITE( message_string, * ) 'run will be terminated due to user ',    &
219                                  'settings of',                               &
220                                  '&restart_time / dt_restart',                &
[274]221                                  '&new restart time is: ', time_restart, ' s' 
222          CALL message( 'check_for_restart', 'PA0164', 0, 0, 0, 6, 0 )
[247]223 
[108]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).
[1320]229          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0       &
[206]230               .AND.  terminate_coupled_remote == 0 )  THEN
[110]231
[1353]232             IF ( dt_restart /= 9999999.9_wp )  THEN
[108]233                terminate_coupled = 4
234             ELSE
235                terminate_coupled = 5
236             ENDIF
[222]237#if defined( __parallel )
[667]238             IF ( myid == 0 ) THEN
[1032]239                CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,   &
240                                   target_id,  0,                              &
241                                   terminate_coupled_remote, 1, MPI_INTEGER,   &
242                                   target_id,  0,                              &
[667]243                                   comm_inter, status, ierr )   
244             ENDIF
[1320]245             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0,      &
[1032]246                             comm2d, ierr ) 
[222]247#endif
[108]248          ENDIF
249       ELSE
[1353]250          time_restart = 9999999.9_wp
[1]251       ENDIF
252    ENDIF
253
254!
255!-- If the run is stopped, set a flag file which is necessary to initiate
[1468]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
[1]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.