MONC
terminationcheck.F90
Go to the documentation of this file.
1 
11  use mpi, only : mpi_int, mpi_logical, mpi_in_place, mpi_lor, mpi_wtime
12  implicit none
13 
14 #ifndef TEST_MODE
15  private
16 #endif
17 
18  integer, parameter :: file_line_len=100, file_unit=10
21  character(len=STRING_LENGTH) :: messages_file_name
22  logical :: check_for_walltime
23 
25 
26  contains
27 
31  terminationcheck_get_descriptor%name="termination_check"
36 
40  subroutine init_callback(current_state)
41  type(model_state_type), target, intent(inout) :: current_state
42  integer :: i, idx, pidx, walltime_secs, walltime_mins, walltime_hours
43  character(len=STRING_LENGTH) :: walltime_string
44  logical :: mangled
45 
46  max_timesteps=options_get_integer(current_state%options_database, "nn_timesteps")
47  termination_time=options_get_real(current_state%options_database, "termination_time")
48  check_messages_file_frequency=options_get_integer(current_state%options_database, "check_msg_frequency")
49  messages_file_name=options_get_string(current_state%options_database, "msg_filename")
50  check_walltime_frequency=options_get_integer(current_state%options_database, "check_walltime_frequency")
51  walltime_string=options_get_string(current_state%options_database, "walltime_limit")
52  check_for_walltime=trim(walltime_string) /= "none"
53  if (check_for_walltime) then
54  pidx=1
55  mangled=.false.
56  do i=1, 2
57  idx=index(walltime_string(pidx:), ":")
58  if (idx .gt. 0) then
59  if (i==1) walltime_hours=conv_to_integer(walltime_string(pidx:pidx+idx-2))
60  if (i==2) walltime_mins=conv_to_integer(walltime_string(pidx:pidx+idx-2))
61  pidx=pidx+idx
62  else
63  call log_master_log(log_warn, "Walltime limit of `"//trim(walltime_string)//&
64  "` does not contains hh:mm:ss, defaulting to no limit")
65  check_for_walltime=.false.
66  exit
67  end if
68  end do
69  if (check_for_walltime) then
70  walltime_secs=conv_to_integer(walltime_string(pidx:))
71  if (walltime_mins .lt. 0 .or. walltime_mins .gt. 59) then
72  walltime_mins=0
73  mangled=.true.
74  end if
75  if (walltime_secs .lt. 0 .or. walltime_secs .gt. 59) then
76  walltime_secs=0
77  mangled=.true.
78  end if
79  if (mangled) then
80  call log_master_log(log_warn, "Walltime limit of `"//trim(walltime_string)//"` mangled, defaulting to "//&
81  trim(conv_to_string(walltime_hours))//":"//trim(conv_to_string(walltime_mins))//":"//&
82  trim(conv_to_string(walltime_secs)))
83  end if
84  max_walltime_secs=(walltime_hours*60*60)+(walltime_mins*60)+walltime_secs
85  end if
86  end if
87  end subroutine init_callback
88 
91  subroutine timestep_callback(current_state)
92  type(model_state_type), target, intent(inout) :: current_state
93 
94  integer :: ierr, file_message_status
95 
96  if (max_timesteps .gt. 0) then
97  current_state%continue_timestep=mod(current_state%timestep, max_timesteps) /= 0
98  if (.not. current_state%continue_timestep) current_state%termination_reason=timestep_termination_reason
99  else
100  current_state%continue_timestep=.true.
101  end if
102  if (current_state%continue_timestep) then
103  current_state%continue_timestep = current_state%time .lt. termination_time
104  if (.not. current_state%continue_timestep) current_state%termination_reason=time_termination_reason
105  end if
106  if (current_state%continue_timestep .and. check_for_walltime .and. &
107  mod(current_state%timestep, check_walltime_frequency) == 0) then
108  current_state%continue_timestep=int(mpi_wtime() - current_state%model_start_wtime) .lt. max_walltime_secs
109  call mpi_allreduce(mpi_in_place, current_state%continue_timestep, 1, mpi_logical, mpi_lor, &
110  current_state%parallel%monc_communicator, ierr)
111  if (.not. current_state%continue_timestep) current_state%termination_reason=walltime_termination_reason
112  end if
113 
114  if (current_state%continue_timestep .and. mod(current_state%timestep, check_messages_file_frequency) == 0) then
115  if (current_state%parallel%my_rank == 0) then
116  file_message_status=check_messages_file(current_state)
117  call mpi_bcast(file_message_status, 1, mpi_int, 0, current_state%parallel%monc_communicator, ierr)
118  else
119  call mpi_bcast(file_message_status, 1, mpi_int, 0, current_state%parallel%monc_communicator, ierr)
120  if (file_message_status == 1) current_state%continue_timestep=.false.
121  end if
122  if (.not. current_state%continue_timestep) current_state%termination_reason=message_termination_reason
123  end if
124  end subroutine timestep_callback
125 
128  integer function check_messages_file(current_state)
129  type(model_state_type), target, intent(inout) :: current_state
130 
131  character(len=FILE_LINE_LEN) :: msg_line
132  integer :: ierr
133 
135  open (unit=file_unit, file=messages_file_name, status='OLD', iostat=ierr)
136  if (ierr == 0) then
137  read(file_unit,"(A)",iostat=ierr) msg_line
138  if (ierr == 0) then
139  if (trim(msg_line) .eq. "terminate") then
141  current_state%continue_timestep=.false.
142  end if
143  end if
144  end if
145  close(file_unit)
146  end function check_messages_file
147 end module terminationcheck_mod
conversions_mod
Conversion between common inbuilt FORTRAN data types.
Definition: conversions.F90:5
terminationcheck_mod::init_callback
subroutine init_callback(current_state)
Called upon model initialisation. Will basically read from the options database and set options in th...
Definition: terminationcheck.F90:41
logging_mod::log_warn
integer, parameter, public log_warn
Log WARNING and ERROR messages.
Definition: logging.F90:12
terminationcheck_mod::file_unit
integer, parameter file_unit
Definition: terminationcheck.F90:18
conversions_mod::conv_to_integer
Converts data types to integers.
Definition: conversions.F90:49
terminationcheck_mod::max_timesteps
integer max_timesteps
Definition: terminationcheck.F90:19
state_mod::timestep_termination_reason
integer, parameter, public timestep_termination_reason
Definition: state.F90:17
terminationcheck_mod::check_for_walltime
logical check_for_walltime
Definition: terminationcheck.F90:22
optionsdatabase_mod::options_get_integer
integer function, public options_get_integer(options_database, key, index)
Retrieves an integer value from the database that matches the provided key.
Definition: optionsdatabase.F90:217
terminationcheck_mod::max_walltime_secs
integer max_walltime_secs
Definition: terminationcheck.F90:19
terminationcheck_mod::messages_file_name
character(len=string_length) messages_file_name
Definition: terminationcheck.F90:21
terminationcheck_mod::check_messages_file
integer function check_messages_file(current_state)
Checks the messages file for commands which determine user control of the model.
Definition: terminationcheck.F90:129
monc_component_mod
Interfaces and types that MONC components must specify.
Definition: monc_component.F90:6
optionsdatabase_mod::options_add
Generic add interface for adding different types of data to the databases.
Definition: optionsdatabase.F90:28
optionsdatabase_mod::options_get_string
character(len=string_length) function, public options_get_string(options_database, key, index)
Retrieves a string value from the database that matches the provided key.
Definition: optionsdatabase.F90:280
terminationcheck_mod::terminationcheck_get_descriptor
type(component_descriptor_type) function, public terminationcheck_get_descriptor()
Provides the descriptor back to the caller and is used in component registration.
Definition: terminationcheck.F90:31
optionsdatabase_mod::options_has_key
logical function, public options_has_key(options_database, key)
Determines whether a specific key is in the database.
Definition: optionsdatabase.F90:76
conversions_mod::conv_to_string
Converts data types to strings.
Definition: conversions.F90:38
terminationcheck_mod
This component will check for termination conditions at stages of the model run and terminate that sp...
Definition: terminationcheck.F90:3
state_mod::model_state_type
The ModelState which represents the current state of a run.
Definition: state.F90:39
state_mod::time_termination_reason
integer, parameter, public time_termination_reason
The constants defining the reason why the model has terminated.
Definition: state.F90:17
terminationcheck_mod::file_line_len
integer, parameter file_line_len
Definition: terminationcheck.F90:18
logging_mod
Logging utility.
Definition: logging.F90:2
state_mod::walltime_termination_reason
integer, parameter, public walltime_termination_reason
Definition: state.F90:17
datadefn_mod
Contains common definitions for the data and datatypes used by MONC.
Definition: datadefn.F90:2
datadefn_mod::string_length
integer, parameter, public string_length
Default length of strings.
Definition: datadefn.F90:10
logging_mod::log_master_log
subroutine, public log_master_log(level, message)
Will log just from the master process.
Definition: logging.F90:47
terminationcheck_mod::check_messages_file_frequency
integer check_messages_file_frequency
Definition: terminationcheck.F90:19
terminationcheck_mod::termination_time
real(kind=default_precision) termination_time
Definition: terminationcheck.F90:20
terminationcheck_mod::check_walltime_frequency
integer check_walltime_frequency
Definition: terminationcheck.F90:19
state_mod::message_termination_reason
integer, parameter, public message_termination_reason
Definition: state.F90:17
optionsdatabase_mod
Manages the options database. Contains administration functions and deduce runtime options from the c...
Definition: optionsdatabase.F90:7
monc_component_mod::component_descriptor_type
Description of a component.
Definition: monc_component.F90:42
optionsdatabase_mod::options_get_real
real(kind=default_precision) function, public options_get_real(options_database, key, index)
Retrieves a real value from the database that matches the provided key.
Definition: optionsdatabase.F90:91
terminationcheck_mod::timestep_callback
subroutine timestep_callback(current_state)
Timestep hook which is called at each timestep to determine whether or not to terminate timestep iter...
Definition: terminationcheck.F90:92
datadefn_mod::default_precision
integer, parameter, public default_precision
MPI communication type which we use for the prognostic and calculation data.
Definition: datadefn.F90:17
conversions_mod::conv_single_real_to_double
real(kind=double_precision) function, public conv_single_real_to_double(input_real)
Converts from a single to double precision real. This applies some rounding to a certain number of de...
Definition: conversions.F90:114
state_mod
The model state which represents the current state of a run.
Definition: state.F90:2