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

Last change on this file since 1623 was 1510, checked in by heinze, 10 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 9.8 KB
RevLine 
[1]1 SUBROUTINE check_for_restart
2
[1036]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!
[1310]17! Copyright 1997-2014 Leibniz Universitaet Hannover
[1036]18!--------------------------------------------------------------------------------!
19!
[247]20! Current revisions:
[1]21! -----------------
[1354]22!
[1510]23!
[1321]24! Former revisions:
25! -----------------
26! $Id: check_for_restart.f90 1510 2014-12-16 08:57:29Z heinze $
27!
[1510]28! 1509 2014-12-16 08:56:46Z heinze
29! bugfix: prevent infinite loop in case of automatic restarts
30!
[1469]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!
[1354]35! 1353 2014-04-08 15:21:23Z heinze
36! REAL constants provided with KIND-attribute
37!
[1321]38! 1320 2014-03-20 08:40:49Z raasch
[1320]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
[1]45!
[1037]46! 1036 2012-10-22 13:43:42Z raasch
47! code put under GPL (PALM 3.9)
48!
[1033]49! 1032 2012-10-21 13:03:21Z letzel
50! minor reformatting
51!
[1]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
[1320]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
[1]69    USE pegrid
70
71    IMPLICIT NONE
72
73
[1468]74    LOGICAL :: terminate_run_l           !:
75    LOGICAL :: do_stop_now = .FALSE.     !:
76    LOGICAL :: do_restart_now = .FALSE.  !:
[1]77
[1320]78    REAL(wp) ::  remaining_time !:
[1]79
[1320]80
[1]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.
[1320]88    IF ( remaining_time <= termination_time_needed  .AND.                      &
[1]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.
[622]98    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
[1320]99    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL,        &
[1]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
[1320]108       WRITE( message_string, * ) 'run will be terminated because it is ',     &
109                       'running out of job cpu limit & ',                      &
110                       'remaining time:         ', remaining_time, ' s',       &
[274]111                       'termination time needed:', termination_time_needed, ' s'
[247]112       CALL message( 'check_for_restart', 'PA0163', 0, 1, 0, 6, 0 )
[1]113    ENDIF
114
115!
[108]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).
[1320]120    IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled'  .AND.       &
[110]121         terminate_coupled == 0  .AND.  terminate_coupled_remote == 0 )  THEN
122
[108]123       terminate_coupled = 3
[1468]124
[222]125#if defined( __parallel )
[667]126       IF ( myid == 0 ) THEN
[1032]127          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,         &
128                             target_id, 0,                                     &
129                             terminate_coupled_remote, 1, MPI_INTEGER,         &
130                             target_id, 0,                                     &
[667]131                             comm_inter, status, ierr )
132       ENDIF
[1320]133       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d,    &
[1032]134                       ierr )
[222]135#endif
[108]136    ENDIF
137
[1468]138
[108]139!
[1468]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.                                   &
[291]198         time_restart < time_since_reference_point )  THEN
199
[1]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)
[1353]208          IF ( dt_restart /= 9999999.9_wp )  THEN
[1]209             time_restart = time_restart + dt_restart
210          ELSE
[1353]211             time_restart = 9999999.9_wp
[1]212          ENDIF
213
[1320]214          WRITE( message_string, * ) 'run will be terminated due to user ',    &
215                                  'settings of',                               &
216                                  '&restart_time / dt_restart',                &
[274]217                                  '&new restart time is: ', time_restart, ' s' 
218          CALL message( 'check_for_restart', 'PA0164', 0, 0, 0, 6, 0 )
[247]219 
[108]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).
[1320]225          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0       &
[206]226               .AND.  terminate_coupled_remote == 0 )  THEN
[110]227
[1353]228             IF ( dt_restart /= 9999999.9_wp )  THEN
[108]229                terminate_coupled = 4
230             ELSE
231                terminate_coupled = 5
232             ENDIF
[222]233#if defined( __parallel )
[667]234             IF ( myid == 0 ) THEN
[1032]235                CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,   &
236                                   target_id,  0,                              &
237                                   terminate_coupled_remote, 1, MPI_INTEGER,   &
238                                   target_id,  0,                              &
[667]239                                   comm_inter, status, ierr )   
240             ENDIF
[1320]241             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0,      &
[1032]242                             comm2d, ierr ) 
[222]243#endif
[108]244          ENDIF
245       ELSE
[1353]246          time_restart = 9999999.9_wp
[1]247       ENDIF
248    ENDIF
249
250!
251!-- If the run is stopped, set a flag file which is necessary to initiate
[1468]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
[1]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.