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

Last change on this file since 4598 was 4542, checked in by raasch, 11 months ago

files re-formatted to follow the PALM coding standard, redundant if statement removed

  • Property svn:keywords set to Id
File size: 10.0 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 terms of the GNU General
6! Public License as published by the Free Software Foundation, either version 3 of the License, or
7! (at your option) any later version.
8!
9! PALM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10! implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11! Public License for more details.
12!
13! You should have received a copy of the GNU General Public License along with PALM. If not, see
14! <http://www.gnu.org/licenses/>.
15!
16! Copyright 1997-2020 Leibniz Universitaet Hannover
17!--------------------------------------------------------------------------------------------------!
18!
19! Current revisions:
20! -----------------
21!
22!
23! Former revisions:
24! -----------------
25! $Id: check_for_restart.f90 4542 2020-05-19 15:45:12Z suehring $
26! file re-formatted to follow the PALM coding standard
27!
28! 4360 2020-01-07 11:25:50Z suehring
29! Corrected "Former revisions" section
30!
31! 3655 2019-01-07 16:51:22Z knoop
32! Error messages revised
33!
34! Revision 1.1  1998/03/18 20:06:51  raasch
35! Initial revision
36!
37!
38! Description:
39! ------------
40!> Set stop flag, if restart is neccessary because of expiring cpu-time or if it is forced by user.
41!--------------------------------------------------------------------------------------------------!
42 SUBROUTINE check_for_restart
43
44
45    USE control_parameters,                                                                        &
46        ONLY:  coupling_mode, dt_restart, end_time, message_string, run_description_header,        &
47               simulated_time, terminate_coupled, terminate_coupled_remote, terminate_run,         &
48               termination_time_needed, time_restart, time_since_reference_point, write_binary
49
50    USE kinds
51
52    USE pegrid
53
54    USE pmc_interface,                                                                             &
55        ONLY:  comm_world_nesting, cpl_id, nested_run
56
57    IMPLICIT NONE
58
59    INTEGER ::  global_communicator       !< global communicator to be used here
60
61    LOGICAL ::  terminate_run_l           !<
62    LOGICAL ::  do_stop_now = .FALSE.     !<
63    LOGICAL ::  do_restart_now = .FALSE.  !<
64
65    REAL(wp) ::  remaining_time !<
66
67
68!
69!-- Check remaining CPU-time
70    CALL local_tremain( remaining_time )
71
72!
73!-- If necessary set a flag to stop the model run
74    terminate_run_l = .FALSE.
75    IF ( remaining_time <= termination_time_needed  .AND.  write_binary )  THEN
76
77       terminate_run_l = .TRUE.
78    ENDIF
79
80!
81!-- Set the global communicator to be used (depends on the mode in which PALM is running)
82    IF ( nested_run )  THEN
83       global_communicator = comm_world_nesting
84    ELSE
85       global_communicator = comm2d
86    ENDIF
87
88#if defined( __parallel )
89!
90!-- Make a logical OR for all processes. Stop the model run if at least one process has reached the
91!-- time limit.
92    IF ( collective_wait )  CALL MPI_BARRIER( global_communicator, ierr )
93    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL, MPI_LOR,                   &
94                        global_communicator, ierr )
95#else
96    terminate_run = terminate_run_l
97#endif
98
99!
100!-- Output that job will be terminated
101    IF ( terminate_run  .AND.  myid == 0 )  THEN
102       WRITE( message_string, * ) 'run will be terminated because it is ',                         &
103                                  'running out of job cpu limit & ',                               &
104                                  'remaining time:         ', remaining_time, ' s &',              &
105                                  'termination time needed:', termination_time_needed, ' s'
106       CALL message( 'check_for_restart', 'PA0163', 0, 1, 0, 6, 0 )
107    ENDIF
108
109!
110!-- In case of coupled runs inform the remote model of the termination and its reason, provided the
111!-- remote model has not already been informed of another termination reason (terminate_coupled > 0)
112!-- before, or vice versa (terminate_coupled_remote > 0).
113    IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled'  .AND.                           &
114         terminate_coupled == 0  .AND.  terminate_coupled_remote == 0 )  THEN
115
116       terminate_coupled = 3
117
118#if defined( __parallel )
119       IF ( myid == 0 ) THEN
120          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,                             &
121                             target_id, 0,                                                         &
122                             terminate_coupled_remote, 1, MPI_INTEGER,                             &
123                             target_id, 0,                                                         &
124                             comm_inter, status, ierr )
125       ENDIF
126       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr )
127#endif
128    ENDIF
129
130
131!
132!-- Check if a flag file exists that forces a termination of the model.
133    IF ( myid == 0 )  THEN
134       INQUIRE(FILE="DO_STOP_NOW", EXIST=do_stop_now)
135       INQUIRE(FILE="DO_RESTART_NOW", EXIST=do_restart_now)
136
137       IF ( do_stop_now .OR. do_restart_now )  THEN
138
139          terminate_run_l = .TRUE.
140
141          WRITE( message_string, * ) 'run will be terminated because user ',                       &
142                                     'forced a job finalization using a flag',                     &
143                                     'file:',                                                      &
144                                     '&DO_STOP_NOW: ', do_stop_now,                                &
145                                     '&DO_RESTART_NOW: ', do_restart_now
146          CALL message( 'check_for_restart', 'PA0398', 0, 0, 0, 6, 0 )
147
148       ENDIF
149    ENDIF
150
151
152#if defined( __parallel )
153!
154!-- Make a logical OR for all processes. Stop the model run if a flag file has been detected above.
155    IF ( collective_wait )  CALL MPI_BARRIER( global_communicator, ierr )
156    CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL, MPI_LOR,                   &
157                        global_communicator, ierr )
158#else
159    terminate_run = terminate_run_l
160#endif
161
162!
163!-- In case of coupled runs inform the remote model of the termination and its reason, provided the
164!-- remote model has not already been informed of another termination reason (terminate_coupled > 0)
165!-- before, or vice versa (terminate_coupled_remote > 0).
166    IF ( terminate_run .AND. coupling_mode /= 'uncoupled' .AND.                                    &
167         terminate_coupled == 0 .AND.  terminate_coupled_remote == 0 )  THEN
168
169       terminate_coupled = 6
170
171#if defined( __parallel )
172       IF ( myid == 0 ) THEN
173          CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,                             &
174                             target_id,  0,                                                        &
175                             terminate_coupled_remote, 1, MPI_INTEGER,                             &
176                             target_id,  0,                                                        &
177                             comm_inter, status, ierr )
178       ENDIF
179       CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr )
180#endif
181
182    ENDIF
183
184!
185!-- Set the stop flag also, if restart is forced by user settings
186    IF ( time_restart /= 9999999.9_wp  .AND.  time_restart < time_since_reference_point )  THEN
187
188!
189!--    Restart is not neccessary, if the end time of the run (given by the user) has been reached.
190       IF ( simulated_time < end_time )  THEN
191          terminate_run = .TRUE.
192!
193!--       Increment restart time, if forced by user, otherwise set restart time to default (no user
194!--       restart).
195          IF ( dt_restart /= 9999999.9_wp )  THEN
196             time_restart = time_restart + dt_restart
197          ELSE
198             time_restart = 9999999.9_wp
199          ENDIF
200
201          WRITE( message_string, * ) 'run will be terminated due to user ',                        &
202                                     'settings of ',                                               &
203                                     'restart_time / dt_restart, ',                                &
204                                     'new restart time is: ', time_restart, ' s'
205          CALL message( 'check_for_restart', 'PA0164', 0, 0, 0, 6, 0 )
206
207!
208!--       In case of coupled runs inform the remote model of the termination and its reason,
209!--       provided the remote model has not already been informed of another termination reason
210!--       (terminate_coupled > 0) before, or vice versa (terminate_coupled_remote > 0).
211          IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0                           &
212               .AND.  terminate_coupled_remote == 0 )  THEN
213
214             IF ( dt_restart /= 9999999.9_wp )  THEN
215                terminate_coupled = 4
216             ELSE
217                terminate_coupled = 5
218             ENDIF
219#if defined( __parallel )
220             IF ( myid == 0 ) THEN
221                CALL MPI_SENDRECV( terminate_coupled,        1, MPI_INTEGER,                       &
222                                   target_id,  0,                                                  &
223                                   terminate_coupled_remote, 1, MPI_INTEGER,                       &
224                                   target_id,  0,                                                  &
225                                   comm_inter, status, ierr )
226             ENDIF
227             CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, ierr )
228#endif
229          ENDIF
230       ELSE
231          time_restart = 9999999.9_wp
232       ENDIF
233    ENDIF
234
235!
236!-- If the run is stopped, set a flag file which is necessary to initiate the start of a
237!-- continuation run, except if the user forced to stop the run without restart.
238    IF ( terminate_run  .AND.  myid == 0  .AND.  cpl_id == 1  .AND.  .NOT. do_stop_now)  THEN
239
240       OPEN ( 90, FILE='CONTINUE_RUN', FORM='FORMATTED' )
241       WRITE ( 90, '(A)' )  TRIM( run_description_header )
242       CLOSE ( 90 )
243
244    ENDIF
245
246
247 END SUBROUTINE check_for_restart
Note: See TracBrowser for help on using the repository browser.