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