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