-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_cp.R
57 lines (50 loc) · 2.04 KB
/
check_cp.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' @title Checks for Checkpointed Rdata Files
#'
#' @description Checks whether there are checkpointed data files in a specific
#' format given a simulation number.
#'
#' @param simno Simulation number for current model simulation, typically stored
#' in \code{control$simno}.
#'
#' @details
#' This function checks whether checkpointed data files are available for loading.
#' Checkpointed data files are incrementally saved during the simulation and
#' loaded when a simulation job has been cancelled and restarted. This is done
#' automatically within the \code{\link{netsim_hpc}} function.
#'
#' Checkpointed data files are searched for in a specific subdirectory relative
#' to the current working directory: \code{data/sim<x>}, where \code{<x>} is the
#' \code{simno} value. Within that directory \code{check_cp} looks for files
#' ending \code{.cp.rda}, which is the standard checkpoint data file name. Note
#' that these standards for file directory and name are consistent with the
#' \code{\link{save_cpdata}} module function. If running simulations using the
#' \code{\link{netsim_hpc}} function, this data saving module will automatically
#' be inserted in the workflow of a simulation.
#'
#' The files are tested to see that they are of similar size, meaning that no
#' file is less than 50% of the average file size of the others. Smaller size
#' files usually indicates that the interim file saving was interrupted. If the
#' files exist and are of correct size, a full directory name is returned, else
#' \code{NULL} is returned.
#'
#' @export
#'
check_cp <- function(simno) {
badFile <- FALSE
dirname <- paste0("data/sim", simno)
if (file.exists(dirname) == FALSE) {
return(NULL)
}
fn <- list.files(path = dirname, pattern = "*.cp.rda", full.names = TRUE)
if (length(fn) < 16) {
badFile <- TRUE
} else {
badFile <- any(sapply(1:length(fn),
function(x) inherits(try(load(fn[x]), silent = TRUE), "try-error")))
}
if (badFile == TRUE) {
return(NULL)
} else {
return(dirname)
}
}