metrumresearchgroup / mrgsolve

Simulate from ODE-based population PK/PD and QSP models in R
https://mrgsolve.org
GNU General Public License v2.0
132 stars 36 forks source link

mevent history doesn't get cleaned up properly #1117

Closed kylebaron closed 1 year ago

kylebaron commented 1 year ago

User reported this problem with the mevent history: for the first ID, everything works fine; for the second ID, we don't get any of the events triggered from within the model because the event history clean up code is never reached in this case.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(mrgsolve)
#> 
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#> 
#>     filter

code <- '

$MAIN 
if(NEWIND <= 1) {
  int dosen = 0;
  double dose = 0;
}

$TABLE
if(EVID> 30 && dosen < 11) {
    if(dosen <= 1) dose = 100;
    if(dosen==2)   dose = 200;  
    if(dosen ==3)  dose = 300;
    if(dosen > 3)  dose = 400;
    if(dosen > 5)  dose =   0;
    mrg::evdata ev(TIME,1);
    ev.cmt = 1;
    ev.amt = dose;
    ev.now = true;
    self.mevector.push_back(ev);
    ++dosen;
    self.mevent(TIME + 3, 33);
}

$CMT A
$CAPTURE dosen dose
'

mod <- mcode("foo", code)
#> Building foo ...
#> done.
data <- expand.ev(amt = 0, evid = 33, time = 0, ID = 1:2)

mrgsim(mod, data = data, end = 33, delta = 0.1) %>% plot()

Created on 2023-09-27 with reprex v2.0.2

Strategy to address