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