From 04ca11b6dfc355e09d6fd9bc80a4b6601f5e1c7e Mon Sep 17 00:00:00 2001 From: Andrew Pulsipher Date: Thu, 14 Nov 2024 13:29:51 -0700 Subject: [PATCH] Implement today() in model-methods.R --- NAMESPACE | 2 ++ R/cpp11.R | 4 ++++ R/model-methods.R | 14 ++++++++++++++ man/epiworld-methods.Rd | 11 +++++++++++ src/cpp11.cpp | 8 ++++++++ src/model.cpp | 8 ++++++++ 6 files changed, 47 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 316e5938..888d6780 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -87,6 +87,7 @@ S3method(set_stats_names,epiworld_lfmcmc) S3method(set_summary_fun,epiworld_lfmcmc) S3method(size,epiworld_model) S3method(summary,epiworld_model) +S3method(today,epiworld_model) S3method(verbose_off,epiworld_model) S3method(verbose_on,epiworld_model) export(LFMCMC) @@ -220,6 +221,7 @@ export(set_transmission_reduction) export(set_transmission_reduction_fun) export(set_transmission_reduction_ptr) export(size) +export(today) export(tool) export(tool_fun_logit) export(use_kernel_fun_gaussian) diff --git a/R/cpp11.R b/R/cpp11.R index d699a510..3113ec6d 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -340,6 +340,10 @@ get_ndays_cpp <- function(model) { .Call(`_epiworldR_get_ndays_cpp`, model) } +today_cpp <- function(model) { + .Call(`_epiworldR_today_cpp`, model) +} + get_n_replicates_cpp <- function(model) { .Call(`_epiworldR_get_n_replicates_cpp`, model) } diff --git a/R/model-methods.R b/R/model-methods.R index 7bc71f22..29d68559 100644 --- a/R/model-methods.R +++ b/R/model-methods.R @@ -80,6 +80,10 @@ stopifnot_model <- function(model) { #' get_ndays(model_sirconn) # Returns the length of the simulation in days. This #' # will match "ndays" within the "run" function. #' +#' today(model_sirconn) # Returns the current day of the simulation. This will +#' # match "get_ndays()" if run at the end of a simulation, but will differ if run +#' # during a simulation +#' #' get_n_replicates(model_sirconn) # Returns the number of replicates of the #' # model. #' @@ -275,6 +279,16 @@ get_ndays <- function(x) UseMethod("get_ndays") get_ndays.epiworld_model <- function(x) get_ndays_cpp(x) +#' @export +#' @rdname epiworld-methods +#' @returns +#' - `today` returns the current model day +today <- function(x) UseMethod("today") + +#' @export +today.epiworld_model <- function(x) today_cpp(x) + + #' @export #' @rdname epiworld-methods #' @returns diff --git a/man/epiworld-methods.Rd b/man/epiworld-methods.Rd index 17cacfd1..ac2c0147 100644 --- a/man/epiworld-methods.Rd +++ b/man/epiworld-methods.Rd @@ -17,6 +17,7 @@ \alias{get_n_viruses} \alias{get_n_tools} \alias{get_ndays} +\alias{today} \alias{get_n_replicates} \alias{size} \alias{set_agents_data} @@ -55,6 +56,8 @@ get_n_tools(x) get_ndays(x) +today(x) + get_n_replicates(x) size(x) @@ -150,6 +153,10 @@ of \code{epiworld_model}. \item \code{get_ndays} returns the number of days of the model. } +\itemize{ +\item \code{today} returns the current model day +} + \itemize{ \item \code{get_n_replicates} returns the number of replicates of the model. } @@ -254,6 +261,10 @@ get_n_tools(model_sirconn) # Returns the number of tools in the model. In get_ndays(model_sirconn) # Returns the length of the simulation in days. This # will match "ndays" within the "run" function. +today(model_sirconn) # Returns the current day of the simulation. This will +# match "get_ndays()" if run at the end of a simulation, but will differ if run +# during a simulation + get_n_replicates(model_sirconn) # Returns the number of replicates of the # model. diff --git a/src/cpp11.cpp b/src/cpp11.cpp index a50bdc74..1ea60e82 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -601,6 +601,13 @@ extern "C" SEXP _epiworldR_get_ndays_cpp(SEXP model) { END_CPP11 } // model.cpp +int today_cpp(SEXP model); +extern "C" SEXP _epiworldR_today_cpp(SEXP model) { + BEGIN_CPP11 + return cpp11::as_sexp(today_cpp(cpp11::as_cpp>(model))); + END_CPP11 +} +// model.cpp int get_n_replicates_cpp(SEXP model); extern "C" SEXP _epiworldR_get_n_replicates_cpp(SEXP model) { BEGIN_CPP11 @@ -1127,6 +1134,7 @@ static const R_CallMethodDef CallEntries[] = { {"_epiworldR_set_transmission_reduction_fun_cpp", (DL_FUNC) &_epiworldR_set_transmission_reduction_fun_cpp, 3}, {"_epiworldR_set_transmission_reduction_ptr_cpp", (DL_FUNC) &_epiworldR_set_transmission_reduction_ptr_cpp, 3}, {"_epiworldR_size_cpp", (DL_FUNC) &_epiworldR_size_cpp, 1}, + {"_epiworldR_today_cpp", (DL_FUNC) &_epiworldR_today_cpp, 1}, {"_epiworldR_tool_cpp", (DL_FUNC) &_epiworldR_tool_cpp, 7}, {"_epiworldR_tool_fun_logit_cpp", (DL_FUNC) &_epiworldR_tool_fun_logit_cpp, 3}, {"_epiworldR_use_kernel_fun_gaussian_cpp", (DL_FUNC) &_epiworldR_use_kernel_fun_gaussian_cpp, 1}, diff --git a/src/model.cpp b/src/model.cpp index 44d0c8a5..34e84951 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -224,6 +224,14 @@ int get_ndays_cpp(SEXP model) { } +[[cpp11::register]] +int today_cpp(SEXP model) { + + external_pointer> ptr(model); + return static_cast(ptr->today()); + +} + [[cpp11::register]] int get_n_replicates_cpp(SEXP model) {