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

Last change on this file since 1927 was 1818, checked in by maronga, 9 years ago

last commit documented / copyright update

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