Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $EVENT block #1230

Merged
merged 28 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
30ed899
model pointers are now stored in the model object
kylebaron Sep 8, 2024
e6968f4
add pointers getter
kylebaron Sep 8, 2024
50eabb3
add
kylebaron Sep 8, 2024
8e83538
tweak implementation
kylebaron Sep 8, 2024
ba67c9e
move call to after events are processed
kylebaron Sep 9, 2024
665def8
different calling sequence when EVENT is in the mix
kylebaron Sep 10, 2024
6ea4d71
tweak logic
kylebaron Sep 10, 2024
ce6bc9c
add handler for EVENT blocks
kylebaron Sep 10, 2024
017b67a
more changes for EVENT block
kylebaron Sep 10, 2024
b8de0f2
drop pointer cache; will re-do later
kylebaron Sep 10, 2024
7a29e1a
revert changes in funset
kylebaron Sep 10, 2024
59935fb
revert more funset
kylebaron Sep 10, 2024
f900793
revert more
kylebaron Sep 10, 2024
aae568a
revert
kylebaron Sep 10, 2024
efbbf4e
spaces
kylebaron Sep 10, 2024
e78c79c
revert
kylebaron Sep 10, 2024
da42b01
don't save x back
kylebaron Sep 10, 2024
99160c9
tests for event blocjk
kylebaron Sep 12, 2024
78da749
more tests on event block
kylebaron Sep 12, 2024
e49ddd7
fix comment
kylebaron Sep 12, 2024
3614e1e
tests for event-block
kylebaron Sep 13, 2024
4dc7cad
fix tests
kylebaron Sep 13, 2024
9eeed73
don't pass by position; make defaults consistent
kylebaron Sep 14, 2024
9f4637d
bump version for testing
kylebaron Sep 14, 2024
5dc2327
move EVENT
kylebaron Sep 14, 2024
1d02ff2
revert compiled check
kylebaron Sep 24, 2024
78341f5
update file header
kylebaron Sep 24, 2024
3f41e40
event and table have the same signature
kylebaron Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've saved a logical value indicating of $EVENT is in the model; if so, tell whether or not to call.

)
}

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,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just light touchup; this code is really old and probably don't need to pass all the functions separately.

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))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this, but it's not in use any more (see comment). I'd rather get rid of it at this point, but it may or may not be in scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a quick look at move_global, I think a dedicated/separate PR would be better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.


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
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this is happening in the new version (move_global2()).

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),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added argument names for safety ... was getting called by position before.

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will pass this into the C+ simulation code to tell if we are using $EVENT to create modeled doses.


# 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re ordered so that EVENT is by TABLE and re-using the definition so that it's in sync.

* using log directory 'd:/RCompile/CRANguest/R-release/mrgsolve.Rcheck'
* using R version 4.4.1 (2024-06-14 ucrt)
* using platform: x86_64-w64-mingw32
* R was compiled by
    gcc.exe (GCC) 13.2.0
    GNU Fortran (GCC) 13.2.0
* running under: Windows Server 2022 x64 (build 20348)
* using session charset: UTF-8
* checking for file 'mrgsolve/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'mrgsolve' version '1.5.1.9002'
* package encoding: UTF-8
* checking CRAN incoming feasibility ... [11s] NOTE
Maintainer: 'Kyle T Baron <kyleb@metrumrg.com>'

Version contains large components (1.5.1.9002)
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking whether package 'mrgsolve' can be installed ... OK
* used C++ compiler: 'g++.exe (GCC) 13.2.0'
* checking installed package size ... OK
* checking package directory ... OK
* checking for future file timestamps ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking code files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking whether startup messages can be suppressed ... OK
* checking use of S3 registration ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... [21s] OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd line widths ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking contents of 'data' directory ... OK
* checking data for non-ASCII characters ... OK
* checking data for ASCII and uncompressed saves ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking line endings in Makefiles ... OK
* checking compilation flags in Makevars ... OK
* checking for GNU extensions in Makefiles ... OK
* checking for portable use of $(BLAS_LIBS) and $(LAPACK_LIBS) ... OK
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking use of SHLIB_OPENMP_*FLAGS in Makefiles ... OK
* checking pragmas in C/C++ headers and code ... OK
* checking compilation flags used ... OK
* checking compiled code ... OK
* checking examples ... [16s] OK
* checking for unstated dependencies in 'tests' ... OK
* checking tests ... [21s] OK
  Running 'testthat.R' [21s]
* checking PDF version of manual ... [18s] OK
* checking HTML version of manual ... [20s] OK
* DONE
Status: 1 NOTE

//! 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
Loading