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