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