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

Last change on this file since 3058 was 3049, checked in by Giersch, 7 years ago

Revision history corrected

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