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

Last change on this file since 3046 was 3046, checked in by Giersch, 3 years ago

Remaining error messages revised, comments extended

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