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-2019 Leibniz Universitaet Hannover |
---|
18 | !------------------------------------------------------------------------------! |
---|
19 | ! |
---|
20 | ! Current revisions: |
---|
21 | ! ----------------- |
---|
22 | ! |
---|
23 | ! |
---|
24 | ! Former revisions: |
---|
25 | ! ----------------- |
---|
26 | ! $Id: check_for_restart.f90 4180 2019-08-21 14:37:54Z raasch $ |
---|
27 | ! Error messages revised |
---|
28 | ! |
---|
29 | ! |
---|
30 | ! Description: |
---|
31 | ! ------------ |
---|
32 | !> Set stop flag, if restart is neccessary because of expiring cpu-time or |
---|
33 | !> if it is forced by user |
---|
34 | !------------------------------------------------------------------------------! |
---|
35 | SUBROUTINE check_for_restart |
---|
36 | |
---|
37 | |
---|
38 | USE control_parameters, & |
---|
39 | ONLY: coupling_mode, dt_restart, end_time, message_string, & |
---|
40 | run_description_header, simulated_time, terminate_coupled, & |
---|
41 | terminate_coupled_remote, terminate_run, & |
---|
42 | termination_time_needed, time_restart, & |
---|
43 | time_since_reference_point, write_binary |
---|
44 | |
---|
45 | USE kinds |
---|
46 | |
---|
47 | USE pegrid |
---|
48 | |
---|
49 | USE pmc_interface, & |
---|
50 | ONLY: comm_world_nesting, cpl_id, nested_run |
---|
51 | |
---|
52 | IMPLICIT NONE |
---|
53 | |
---|
54 | INTEGER :: global_communicator !< global communicator to be used here |
---|
55 | |
---|
56 | LOGICAL :: terminate_run_l !< |
---|
57 | LOGICAL :: do_stop_now = .FALSE. !< |
---|
58 | LOGICAL :: do_restart_now = .FALSE. !< |
---|
59 | |
---|
60 | REAL(wp) :: remaining_time !< |
---|
61 | |
---|
62 | |
---|
63 | ! |
---|
64 | !-- Check remaining CPU-time |
---|
65 | CALL local_tremain( remaining_time ) |
---|
66 | |
---|
67 | ! |
---|
68 | !-- If necessary set a flag to stop the model run |
---|
69 | terminate_run_l = .FALSE. |
---|
70 | IF ( remaining_time <= termination_time_needed .AND. write_binary ) THEN |
---|
71 | |
---|
72 | terminate_run_l = .TRUE. |
---|
73 | ENDIF |
---|
74 | |
---|
75 | ! |
---|
76 | !-- Set the global communicator to be used (depends on the mode in which PALM is |
---|
77 | !-- running) |
---|
78 | IF ( nested_run ) THEN |
---|
79 | global_communicator = comm_world_nesting |
---|
80 | ELSE |
---|
81 | global_communicator = comm2d |
---|
82 | ENDIF |
---|
83 | |
---|
84 | #if defined( __parallel ) |
---|
85 | ! |
---|
86 | !-- Make a logical OR for all processes. Stop the model run if at least |
---|
87 | !-- one process has reached the time limit. |
---|
88 | IF ( collective_wait ) CALL MPI_BARRIER( global_communicator, ierr ) |
---|
89 | CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL, & |
---|
90 | MPI_LOR, global_communicator, ierr ) |
---|
91 | #else |
---|
92 | terminate_run = terminate_run_l |
---|
93 | #endif |
---|
94 | |
---|
95 | ! |
---|
96 | !-- Output that job will be terminated |
---|
97 | IF ( terminate_run .AND. myid == 0 ) THEN |
---|
98 | WRITE( message_string, * ) 'run will be terminated because it is ', & |
---|
99 | 'running out of job cpu limit & ', & |
---|
100 | 'remaining time: ', remaining_time, ' s &', & |
---|
101 | 'termination time needed:', termination_time_needed, ' s' |
---|
102 | CALL message( 'check_for_restart', 'PA0163', 0, 1, 0, 6, 0 ) |
---|
103 | ENDIF |
---|
104 | |
---|
105 | ! |
---|
106 | !-- In case of coupled runs inform the remote model of the termination |
---|
107 | !-- and its reason, provided the remote model has not already been |
---|
108 | !-- informed of another termination reason (terminate_coupled > 0) before, |
---|
109 | !-- or vice versa (terminate_coupled_remote > 0). |
---|
110 | IF ( terminate_run .AND. TRIM( coupling_mode ) /= 'uncoupled' .AND. & |
---|
111 | terminate_coupled == 0 .AND. terminate_coupled_remote == 0 ) THEN |
---|
112 | |
---|
113 | terminate_coupled = 3 |
---|
114 | |
---|
115 | #if defined( __parallel ) |
---|
116 | IF ( myid == 0 ) THEN |
---|
117 | CALL MPI_SENDRECV( terminate_coupled, 1, MPI_INTEGER, & |
---|
118 | target_id, 0, & |
---|
119 | terminate_coupled_remote, 1, MPI_INTEGER, & |
---|
120 | target_id, 0, & |
---|
121 | comm_inter, status, ierr ) |
---|
122 | ENDIF |
---|
123 | CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, comm2d, & |
---|
124 | ierr ) |
---|
125 | #endif |
---|
126 | ENDIF |
---|
127 | |
---|
128 | |
---|
129 | ! |
---|
130 | !-- Check if a flag file exists that forces a termination of the model |
---|
131 | IF ( myid == 0 ) THEN |
---|
132 | INQUIRE(FILE="DO_STOP_NOW", EXIST=do_stop_now) |
---|
133 | INQUIRE(FILE="DO_RESTART_NOW", EXIST=do_restart_now) |
---|
134 | |
---|
135 | IF ( do_stop_now .OR. do_restart_now ) THEN |
---|
136 | |
---|
137 | terminate_run_l = .TRUE. |
---|
138 | |
---|
139 | WRITE( message_string, * ) 'run will be terminated because user ', & |
---|
140 | 'forced a job finalization using a flag', & |
---|
141 | 'file:', & |
---|
142 | '&DO_STOP_NOW: ', do_stop_now, & |
---|
143 | '&DO_RESTART_NOW: ', do_restart_now |
---|
144 | CALL message( 'check_for_restart', 'PA0398', 0, 0, 0, 6, 0 ) |
---|
145 | |
---|
146 | ENDIF |
---|
147 | ENDIF |
---|
148 | |
---|
149 | |
---|
150 | #if defined( __parallel ) |
---|
151 | ! |
---|
152 | !-- Make a logical OR for all processes. Stop the model run if a flag file has |
---|
153 | !-- been detected above. |
---|
154 | IF ( collective_wait ) CALL MPI_BARRIER( global_communicator, ierr ) |
---|
155 | CALL MPI_ALLREDUCE( terminate_run_l, terminate_run, 1, MPI_LOGICAL, & |
---|
156 | MPI_LOR, global_communicator, ierr ) |
---|
157 | #else |
---|
158 | terminate_run = terminate_run_l |
---|
159 | #endif |
---|
160 | |
---|
161 | ! |
---|
162 | !-- In case of coupled runs inform the remote model of the termination |
---|
163 | !-- and its reason, provided the remote model has not already been |
---|
164 | !-- informed of another termination reason (terminate_coupled > 0) before, |
---|
165 | !-- 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, & |
---|
180 | comm2d, ierr ) |
---|
181 | #endif |
---|
182 | |
---|
183 | ENDIF |
---|
184 | |
---|
185 | ! |
---|
186 | !-- Set the stop flag also, if restart is forced by user settings |
---|
187 | IF ( time_restart /= 9999999.9_wp .AND. & |
---|
188 | time_restart < time_since_reference_point ) THEN |
---|
189 | |
---|
190 | ! |
---|
191 | !-- Restart is not neccessary, if the end time of the run (given by |
---|
192 | !-- the user) has been reached |
---|
193 | IF ( simulated_time < end_time ) THEN |
---|
194 | terminate_run = .TRUE. |
---|
195 | ! |
---|
196 | !-- Increment restart time, if forced by user, otherwise set restart |
---|
197 | !-- time to default (no user restart) |
---|
198 | IF ( dt_restart /= 9999999.9_wp ) THEN |
---|
199 | time_restart = time_restart + dt_restart |
---|
200 | ELSE |
---|
201 | time_restart = 9999999.9_wp |
---|
202 | ENDIF |
---|
203 | |
---|
204 | WRITE( message_string, * ) 'run will be terminated due to user ', & |
---|
205 | 'settings of ', & |
---|
206 | 'restart_time / dt_restart, ', & |
---|
207 | 'new restart time is: ', time_restart, ' s' |
---|
208 | CALL message( 'check_for_restart', 'PA0164', 0, 0, 0, 6, 0 ) |
---|
209 | |
---|
210 | ! |
---|
211 | !-- In case of coupled runs inform the remote model of the termination |
---|
212 | !-- and its reason, provided the remote model has not already been |
---|
213 | !-- informed of another termination reason (terminate_coupled > 0) before, |
---|
214 | !-- or vice versa (terminate_coupled_remote > 0). |
---|
215 | IF ( coupling_mode /= 'uncoupled' .AND. terminate_coupled == 0 & |
---|
216 | .AND. terminate_coupled_remote == 0 ) THEN |
---|
217 | |
---|
218 | IF ( dt_restart /= 9999999.9_wp ) THEN |
---|
219 | terminate_coupled = 4 |
---|
220 | ELSE |
---|
221 | terminate_coupled = 5 |
---|
222 | ENDIF |
---|
223 | #if defined( __parallel ) |
---|
224 | IF ( myid == 0 ) THEN |
---|
225 | CALL MPI_SENDRECV( terminate_coupled, 1, MPI_INTEGER, & |
---|
226 | target_id, 0, & |
---|
227 | terminate_coupled_remote, 1, MPI_INTEGER, & |
---|
228 | target_id, 0, & |
---|
229 | comm_inter, status, ierr ) |
---|
230 | ENDIF |
---|
231 | CALL MPI_BCAST( terminate_coupled_remote, 1, MPI_INTEGER, 0, & |
---|
232 | comm2d, ierr ) |
---|
233 | #endif |
---|
234 | ENDIF |
---|
235 | ELSE |
---|
236 | time_restart = 9999999.9_wp |
---|
237 | ENDIF |
---|
238 | ENDIF |
---|
239 | |
---|
240 | ! |
---|
241 | !-- If the run is stopped, set a flag file which is necessary to initiate |
---|
242 | !-- the start of a continuation run, except if the user forced to stop the |
---|
243 | !-- run without restart |
---|
244 | IF ( terminate_run .AND. myid == 0 .AND. cpl_id == 1 .AND. & |
---|
245 | .NOT. do_stop_now) THEN |
---|
246 | |
---|
247 | OPEN ( 90, FILE='CONTINUE_RUN', FORM='FORMATTED' ) |
---|
248 | WRITE ( 90, '(A)' ) TRIM( run_description_header ) |
---|
249 | CLOSE ( 90 ) |
---|
250 | |
---|
251 | ENDIF |
---|
252 | |
---|
253 | |
---|
254 | END SUBROUTINE check_for_restart |
---|