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

Last change on this file since 2488 was 2298, checked in by raasch, 7 years ago

write_binary is of type LOGICAL now, MPI2-related code removed, obsolete variables removed, sendrecv_in_background related parts removed, missing variable descriptions added

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