-
Notifications
You must be signed in to change notification settings - Fork 60
/
net.helpers.R
107 lines (99 loc) · 3.21 KB
/
net.helpers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#' @title Create a Minimal netsim_dat Main List Object for a Network Model
#'
#' @description This helper function populates a \code{netsim_dat} main list
#' object with the minimal required elements. All parameters are
#' optional. When none are given the resulting object is only a
#' shell list of class \code{netsim_dat} with the different named
#' elements defined as empty lists.
#'
#' @param param An \code{EpiModel} object of class \code{\link{param.net}}.
#' @param init An \code{EpiModel} object of class \code{\link{init.net}}.
#' @param control An \code{EpiModel} object of class \code{\link{control.net}}.
#' @param run A \code{list} that will contains the objects created by
#' \code{\link{netsim}} that are required for between step communication. This
#' list must be preserved for restarting models.
#'
#' @return A \code{netsim_dat} main list object.
#' @export
create_dat_object <- function(param = list(), init = list(), control = list(),
run = list()) {
dat <- list(
"run" = validate_run(run),
"param" = param,
"control" = control,
"epi" = list(),
"temp" = list(),
"stats" = list(),
"init" = init
)
class(dat) <- c("netsim_dat", class(dat))
return(dat)
}
#' Ensures that the `run` sublist contains all the mandatory elements
#'
#' @param run A `run` sublist to validate
#' @return A valid `run` sublist
#' @noRd
validate_run <- function(run) {
defaults <- list(
attr = list(),
current_timestep = 1L,
last_unique_id = 0L
)
for (elt_name in names(defaults)) {
if (is.null(run[[elt_name]])) {
run[[elt_name]] <- defaults[[elt_name]]
}
}
return(run)
}
#' @title Return the Current Timestep
#'
#' @inheritParams recovery.net
#'
#' @return The current timestep.
#' @export
get_current_timestep <- function(dat) {
return(dat$run$current_timestep)
}
#' @title Set the Current Timestep
#'
#' @description Changes the current timestep in the \code{netsim_dat} object.
#' Use with caution. This function exists to work around unforeseen
#' corner cases. In most situation, \code{increment_timestep} is
#' preferred.
#'
#' @inheritParams recovery.net
#' @param timestep The new value for the timestep.
#'
#' @inherit recovery.net return
#'
#' @section Mutability:
#' This DOES NOT modify the \code{netsim_dat} object in place. The result must
#' be assigned back to \code{dat} in order to be registered:
#' \code{dat <- increment_timestep(dat)}.
#'
#' @export
set_current_timestep <- function(dat, timestep) {
dat$run$current_timestep <- timestep
return(dat)
}
#' @title Increment the Current Timestep
#'
#' @description This function adds 1 to the timestep counter stored in the
#' \code{netsim_dat} main list object.
#'
#' @inheritParams recovery.net
#'
#' @inherit recovery.net return
#'
#' @section Mutability:
#' This DOES NOT modify the \code{netsim_dat} object in place. The result must
#' be assigned back to \code{dat} in order to be registered:
#' \code{dat <- increment_timestep(dat)}.
#'
#' @export
increment_timestep <- function(dat) {
dat$run$current_timestep <- dat$run$current_timestep + 1
return(dat)
}