Skip to content

Commit 4b8d8e5

Browse files
function to return all dynamic variables
1 parent 3df6fcd commit 4b8d8e5

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: macpan2
22
Title: Fast and Flexible Compartmental Modelling
3-
Version: 3.4.0
3+
Version: 3.5.0
44
Authors@R: c(
55
person("Steve Walker", email="[email protected]", role=c("cre", "aut")),
66
person("Weiguang Guan", role="aut"),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export(mp_discrete_stoch)
241241
export(mp_dot_layout)
242242
export(mp_dynamic_model)
243243
export(mp_dynamic_simulator)
244+
export(mp_dynamic_vars)
244245
export(mp_effects_descr)
245246
export(mp_euler)
246247
export(mp_euler_multinomial)
@@ -290,6 +291,7 @@ export(mp_opt_attempted)
290291
export(mp_optimize)
291292
export(mp_optimized_spec)
292293
export(mp_optimizer_output)
294+
export(mp_other_dynamic_vars)
293295
export(mp_outflow)
294296
export(mp_par)
295297
export(mp_parameterization)

R/flow_frame.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,21 @@ mp_state_dependence_frame = function(spec) {
159159
)
160160
}
161161

162-
#' State and Flow Variable Names
162+
#' Dynamic Variable Names
163163
#'
164-
#' Get the state and/or flow variables in a model specification.
164+
#' Get the state, flow, and other dynamic variables in a model specification.
165+
#' Dynamic variables are any variable that is updated every time step.
166+
#' State and flow variables are special dynamic variables involved in
167+
#' compartmental models that have been explicitly represented using functions
168+
#' like \code{\link{mp_per_capita_flow}} that define flows among compartments
169+
#' (i.e., states).
170+
#'
171+
#' State and flow variables will be identical regardless of the state update
172+
#' method (e.g., \code{\link{mp_rk4}}), but other dynamic variables might
173+
#' appear for one particular state update method that does not appear for
174+
#' another. For example, the first Runge Kutta step for a state in a model
175+
#' with an \code{\link{mp_rk4}} updater, will not appear with an
176+
#' \code{\link{mp_euler}} updater.
165177
#'
166178
#' @param spec Model specification (\code{\link{mp_tmb_model_spec}}).
167179
#' @param topological_sort Should the states and flows be
@@ -175,9 +187,7 @@ mp_state_dependence_frame = function(spec) {
175187
#' @param trans Add a prefix to the names for indicating if a transformed
176188
#' version of the variables is preferred.
177189
#'
178-
#' @return Character vector of names of all state and/or flow variables that
179-
#' have been explicitly represented in the model using functions like
180-
#' \code{\link{mp_per_capita_flow}}.
190+
#' @return Character vector of names of all requested variables.
181191
#'
182192
#' @examples
183193
#' si = mp_tmb_library("starter_models", "si", package = "macpan2")
@@ -223,6 +233,20 @@ mp_state_flow_vars = function(spec, topological_sort = FALSE, loops = "^$", tran
223233
)
224234
}
225235

236+
#' @describeIn mp_vars All variables that are updated once per time-step.
237+
#' @export
238+
mp_dynamic_vars = function(spec) spec$all_dynamic_vars()
239+
240+
#' @describeIn mp_vars All variables that are updated once per time-step,
241+
#' excluding those that are state and flow variables.
242+
#' @export
243+
mp_other_dynamic_vars = function(spec) {
244+
setdiff(
245+
mp_dynamic_vars(spec)
246+
, mp_state_flow_vars(spec)
247+
)
248+
}
249+
226250
#' Data Frame Describing Each Change to Each State Variable
227251
#'
228252
#' Get a data frame with one row for each change made to each state variable

R/mp_tmb_model_spec.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ TMBModelSpec = function(
4444
, self$update_method$after()
4545
)
4646
}
47+
self$expr_list_during = function() ExprList(self$update_method$during())
4748

4849
self$all_derived_vars = function() {
4950
self$expr_list()$all_derived_vars()
@@ -60,6 +61,9 @@ TMBModelSpec = function(
6061
self$all_rhs_vars = function() {
6162
self$expr_list()$all_formula_vars("right")
6263
}
64+
self$all_dynamic_vars = function() {
65+
self$expr_list_during()$all_formula_vars("left")
66+
}
6367
self$all_default_mats = function() {
6468
setdiff(
6569
self$all_default_vars()

man/mp_vars.Rd

Lines changed: 27 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-dynamic-vars.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
test_that("dynamic variables interact properly with the state updater", {
2+
spec = test_cache_read("SPEC-macpan_base.rds")
3+
euler_dyn_vars = spec |> mp_dynamic_vars()
4+
euler_state_flow_vars = spec |> mp_state_flow_vars()
5+
expect_in(euler_dyn_vars
6+
, spec |> mp_rk4() |> mp_dynamic_vars()
7+
)
8+
expect_setequal(euler_dyn_vars
9+
, spec |> mp_hazard() |> mp_dynamic_vars()
10+
)
11+
expect_setequal(euler_dyn_vars
12+
, spec |> mp_discrete_stoch() |> mp_dynamic_vars()
13+
)
14+
expect_setequal(euler_state_flow_vars
15+
, spec |> mp_rk4() |> mp_state_flow_vars()
16+
)
17+
expect_setequal(euler_state_flow_vars
18+
, spec |> mp_hazard() |> mp_state_flow_vars()
19+
)
20+
expect_setequal(euler_state_flow_vars
21+
, spec |> mp_discrete_stoch() |> mp_state_flow_vars()
22+
)
23+
24+
})

0 commit comments

Comments
 (0)