Skip to content

Commit

Permalink
Merge pull request #1230 from metrumresearchgroup/event-block
Browse files Browse the repository at this point in the history
Add $EVENT block
  • Loading branch information
kylebaron authored Sep 25, 2024
2 parents b24a2f8 + 3f41e40 commit 683bf7b
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrgsolve
Title: Simulate from ODE-Based Models
Version: 1.5.1.9001
Version: 1.5.1.9002
Authors@R:
c(person(given = "Kyle T", family = "Baron",
role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ readme:
doc:
Rscript -e "roxygen2::roxygenize()"

.PHONY: build
build:
R CMD build --md5 $(PKGDIR) --no-manual

Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ S3method(handle_spec_block,specBLOCK)
S3method(handle_spec_block,specCAPTURE)
S3method(handle_spec_block,specCMT)
S3method(handle_spec_block,specCMTN)
S3method(handle_spec_block,specEVENT)
S3method(handle_spec_block,specFIXED)
S3method(handle_spec_block,specINCLUDE)
S3method(handle_spec_block,specINIT)
Expand Down
2 changes: 1 addition & 1 deletion R/Aaaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ block_list <- c("ENV", "PROB", "PARAM", "INIT",
"PKMODEL", "PLUGIN", "INCLUDE", "NAMESPACE",
"OMEGA", "SIGMA", "SET","GLOBAL", "CAPTURE",
"PREAMBLE", "PRED", "BLOCK", "TRANSIT", "YAML", "NMEXT",
"INPUT")
"INPUT", "EVENT")

Reserved_cvar <- c("SOLVERTIME","table","ETA","EPS", "AMT", "CMT",
"ID", "TIME", "EVID","simeps", "self", "simeta",
Expand Down
7 changes: 4 additions & 3 deletions R/class_mrgmod.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# along with mrgsolve. If not, see <http://www.gnu.org/licenses/>.

valid_funs <- function(x) {
x1 <- length(x)==4
x2 <- identical(names(x), c("main", "ode", "table", "config"))
x1 <- length(x)==5
x2 <- identical(names(x), c("main", "ode", "table", "event", "config"))
if(x1 & x2) return(list(TRUE,NULL))
msg <- c(
"Invalid functions specification.",
Expand Down Expand Up @@ -794,7 +794,8 @@ parin <- function(x) {
ss_n = 500,
ss_fixed = FALSE,
interrupt = -1,
etasrc = "omega"
etasrc = "omega",
call_event = x@shlib$call_event
)
}

Expand Down
18 changes: 11 additions & 7 deletions R/compile.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2013 - 2023 Metrum Research Group
# Copyright (C) 2013 - 2024 Metrum Research Group
#
# This file is part of mrgsolve.
#
Expand All @@ -17,12 +17,15 @@

generate_rdefs <- function(pars,
cmt,
func,
init_fun="",
table_fun="",
config_fun="",
model="",omats,smats,
set=list(),
func = "",
init_fun = "",
table_fun = "",
event_fun = "",
config_fun = "",
model = "",
omats,
smats,
set = list(),
plugin = NULL,
dbsyms = FALSE, ...) {

Expand Down Expand Up @@ -91,6 +94,7 @@ generate_rdefs <- function(pars,
c(paste0("#define __INITFUN___ ",init_fun),
paste0("#define __ODEFUN___ ",func),
paste0("#define __TABLECODE___ ", table_fun),
paste0("#define __EVENTFUN___ ", event_fun),
paste0("#define __CONFIGFUN___ ", config_fun),
paste0("#define __REGISTERFUN___ ", register_fun(model)),
paste0("#define _nEQ ", ncmt),
Expand Down
7 changes: 4 additions & 3 deletions R/funset.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2013 - 2019 Metrum Research Group, LLC
# Copyright (C) 2013 - 2024 Metrum Research Group
#
# This file is part of mrgsolve.
#
Expand Down Expand Up @@ -30,6 +30,7 @@ There was a problem accessing the model shared object.
main_func <- function(x) x@funs["main"]
ode_func <- function(x) x@funs["ode"]
table_func <- function(x) x@funs["table"]
event_func <- function(x) x@funs["event"]
config_func <- function(x) x@funs["config"]
info_func <- function(x) x@funs["info"]
#nocov end
Expand All @@ -42,7 +43,7 @@ clean_symbol <- function(x) {
gsub("[[:punct:]]", "__", x)
}

funs_create <- function(model, what = c("main", "ode", "table", "config")) {
funs_create <- function(model, what = c("main", "ode", "table", "event", "config")) {
setNames(paste0("_model_", clean_symbol(model), "_", what ,"__"),what)
}

Expand All @@ -55,7 +56,7 @@ package_loaded <- function(x) {
}

funs <- function(x) {
x@funs[c("main", "ode", "table", "config")]
x@funs[c("main", "ode", "table", "event", "config")]
}

model_loaded <- function(x) {
Expand Down
11 changes: 11 additions & 0 deletions R/handle_spec_block.R
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ handle_spec_block.specTABLE <- function(x, env, ...) {
return(x)
}

#' @export
handle_spec_block.specEVENT <- function(x, env, ...) {

x <- dump_opts(x)

pos <- attr(x,"pos")

check_block_data(x, env, pos)

return(x)
}

# NMXML --------------------------------
#' @export
Expand Down
7 changes: 4 additions & 3 deletions R/modlib.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2013 - 2021 Metrum Research Group
# Copyright (C) 2013 - 2024 Metrum Research Group
#
# This file is part of mrgsolve.
#
Expand Down Expand Up @@ -55,8 +55,9 @@
##' mod <- mread("viral2", modlib())
##' mod <- mread("pred1", modlib())
##' mod <- mread("pbpk", modlib())
##' mod <- mread("1005", modlib()) # embedded NONMEM result
##' mod <- mread("1005", modlib()) # embedded NONMEM result
##' mod <- mread("nm-like", modlib()) # model with nonmem-like syntax
##' mod <- mread("evtools", modlib())
##'
##' mrgsolve:::code(mod)
##' }
Expand All @@ -75,7 +76,7 @@ modlib <- function(model = NULL,...,list=FALSE) {
modlib_models <- c(
"pk1cmt", "pk2cmt", "pk3cmt", "pk", "pk1", "pk2", "popex",
"irm1", "irm2", "irm3", "pred1", "emax", "tmdd", "viral1",
"viral2", "effect", "1005", "nm-like"
"viral2", "effect", "1005", "nm-like", "evtools"
)
#nocov end

Expand Down
11 changes: 8 additions & 3 deletions R/modspec.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ param_re_find <- "\\bparam\\s+\\w+\\s*="
# please-deprecate
move_global <- function(x,env) {

what <- intersect(c("PREAMBLE","MAIN", "ODE", "TABLE", "PRED"),names(x))
what <- intersect(c("PREAMBLE","MAIN", "ODE", "TABLE", "EVENT", "PRED"),names(x))

if(length(what)==0) return(x)

Expand Down Expand Up @@ -495,12 +495,17 @@ move_global2 <- function(spec, env, build) {
if(!is.null(table$code)) {
spec$TABLE <- table$code
}
event <- c_vars(spec[["EVENT"]], context = "event")
if(!is.null(event$code)) {
spec$EVENT <- event$code
}
to_ns <- bind_rows(
pream$vars,
pred$vars,
main$vars,
ode$vars,
table$vars
table$vars,
event$vars
)
vars <- bind_rows(glob$vars, to_ns)
if(any(cap <- to_ns$type=="capture")) {
Expand Down Expand Up @@ -536,7 +541,7 @@ move_global2 <- function(spec, env, build) {
}

find_cpp_dot <- function(spec, env) {
to_check <- c("PREAMBLE", "MAIN", "PRED", "ODE", "TABLE", "GLOBAL")
to_check <- c("PREAMBLE", "MAIN", "PRED", "ODE", "EVENT", "TABLE", "GLOBAL")
x <- spec[names(spec) %in% to_check]
x <- unlist(x, use.names = FALSE)
# Narrow the search first; 10x speed up when searching for `pattern`
Expand Down
16 changes: 11 additions & 5 deletions R/mread.R
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
rd <- generate_rdefs(
pars = Pars(x),
cmt = Cmt(x),
ode_func(x),
main_func(x),
table_func(x),
config_func(x),
func = ode_func(x),
init_fun = main_func(x),
table_fun = table_func(x),
event_fun = event_func(x),
config_fun = config_func(x),
model = model(x),
omats = omat(x),
smats = smat(x),
Expand Down Expand Up @@ -375,7 +376,7 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
}
# autodec
if("autodec" %in% names(plugin)) {
auto_blocks <- c("PREAMBLE", "MAIN", "PRED", "ODE", "TABLE")
auto_blocks <- c("PREAMBLE", "MAIN", "PRED", "ODE", "TABLE", "EVENT")
auto_skip <- cvec_cs(ENV[["MRGSOLVE_AUTODEC_SKIP"]])
autov <- autodec_vars(spec, blocks = auto_blocks)
autov <- autodec_clean(
Expand Down Expand Up @@ -461,6 +462,7 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
x@shlib[["nm_import"]] <- mread.env[["nm_import"]]
x@shlib[["source"]] <- file.path(build[["soloc"]],build[["compfile"]])
x@shlib[["md5"]] <- build[["md5"]]
x@shlib[["call_event"]] <- "EVENT" %in% names(spec)

# build----
# In soloc directory
Expand Down Expand Up @@ -536,6 +538,10 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
dbs[["ode"]],
spec[["ODE"]],
"__END_ode__",
"\n// MODELED EVENTS:",
"__BEGIN_event__",
spec[["EVENT"]],
"__END_event__",
"\n// TABLE CODE BLOCK:",
"__BEGIN_table__",
dbs[["cmt"]],
Expand Down
2 changes: 1 addition & 1 deletion R/nm-mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ find_nm_vars <- function(spec) {
ans[["has_ode"]] <- "ODE" %in% names(spec)
FRDA <- c("F", "R", "D", "ALAG")
# CHeck non-ODE
blocks_to_check <- c("PREAMBLE", "MAIN", "TABLE")
blocks_to_check <- c("PREAMBLE", "MAIN", "TABLE", "EVENT")
pmt <- unlist(spec[blocks_to_check], use.names = FALSE)
m1 <- find_nm_vars_impl(pmt)
# Check ODE
Expand Down
2 changes: 2 additions & 0 deletions inst/base/modelheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef double capture;
#define __END_main__ __DONE__
#define __BEGIN_table__ extern "C" { void __TABLECODE___(MRGSOLVE_TABLE_SIGNATURE) {
#define __END_table__ __DONE__
#define __BEGIN_event__ extern "C" {void __EVENTFUN___(MRGSOLVE_EVENT_SIGNATURE) {
#define __END_event__ __DONE__
#define __DONE__ }}


Expand Down
4 changes: 4 additions & 0 deletions inst/base/mrgsolv.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ typedef std::vector<double> dvec;
#define MRGSOLVE_TABLE_SIGNATURE const dvec& _A_, const dvec& _A_0_, dvec& _THETA_, const dvec& _F_, const dvec& _R_, databox& self, const dvec& _pred_, dvec& _capture_, mrgsolve::resim& simeps
#define MRGSOLVE_TABLE_SIGNATURE_N 9

//! signature for <code>$EVENT</code> same as what we use for <code>$TABLE</code>
#define MRGSOLVE_EVENT_SIGNATURE MRGSOLVE_TABLE_SIGNATURE
#define MRGSOLVE_EVENT_SIGNATURE_N MRGSOLVE_TABLE_SIGNATURE_N

//! signature for <code>$ODE</code>
#define MRGSOLVE_ODE_SIGNATURE const double* _ODETIME_, const double* _A_, double* _DADT_, const dvec& _A_0_, const dvec& _THETA_, const bool _ss_flag_
#define MRGSOLVE_ODE_SIGNATURE_N 6
Expand Down
5 changes: 5 additions & 0 deletions inst/include/odeproblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ typedef void (*init_func)(MRGSOLVE_INIT_SIGNATURE);
//! <code>$TABLE</code> function
typedef void (*table_func)(MRGSOLVE_TABLE_SIGNATURE);

//! <code>$EVENT</code> function
typedef void (*event_func)(MRGSOLVE_EVENT_SIGNATURE);

//! <code>$ODE</code> function
typedef void (*deriv_func)(MRGSOLVE_ODE_SIGNATURE);

Expand Down Expand Up @@ -115,6 +118,7 @@ class odeproblem {

void table_call();
void table_init_call();
void event_call();
void config_call();

void set_d(rec_ptr this_rec);
Expand Down Expand Up @@ -249,6 +253,7 @@ class odeproblem {
deriv_func Derivs; ///< <code>$ODE</code> function
init_func Inits; ///< <code>$MAIN</code> function
table_func Table; ///< <code>$TABLE</code> function
event_func Event; ///< <code>$EVENT</code> function
config_func Config; ///< <code>$PREAMBLE</code> function

bool Do_Init_Calc; ///< Flag regulating whether or not initials are taken from <code>$MAIN</code>
Expand Down
Loading

0 comments on commit 683bf7b

Please sign in to comment.