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

Last change on this file since 2000 was 2000, checked in by knoop, 8 years ago

Forced header and separation lines into 80 columns

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