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

Last change on this file since 2186 was 2101, checked in by suehring, 8 years ago

last commit documented

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