metrumresearchgroup / mrgsolve

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

ev.now events not executed when ev.time is prior to current time #1151

Closed kylebaron closed 10 months ago

kylebaron commented 10 months ago

Summary

BUG: now doses are ignored when time is in the past. We'll fix this by skipping this time check when now is true.

Before fix

library(mrgsolve)
#> 
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#> 
#>     filter
code <- '
$CMT A
$MAIN
if(TIME==1) {
  mrgsolve::evdata ev(0, 1); 
  ev.now = true;
  ev.amt = 100;
  self.mevector.push_back(ev);
}
'

mod <- mcode("ev", code)
#> Building ev ...
#> done.
out <- mrgsim(mod)
head(out)
#>   ID time A
#> 1  1    0 0
#> 2  1    1 0
#> 3  1    2 0
#> 4  1    3 0
#> 5  1    4 0
#> 6  1    5 0
all(out$A==0)
#> [1] TRUE

Created on 2024-01-06 with reprex v2.0.2

After fix

library(mrgsolve)
#> 
#> Attaching package: 'mrgsolve'
#> The following object is masked from 'package:stats':
#> 
#>     filter
code <- '
$CMT A
$MAIN
if(TIME==1) {
  mrgsolve::evdata ev(0, 1); 
  ev.now = true;
  ev.amt = 100;
  self.mevector.push_back(ev);
}
'

mod <- mcode("ev", code)
#> Building ev ...
#> done.
out <- mrgsim(mod)
head(out)
#>   ID time   A
#> 1  1    0   0
#> 2  1    1 100
#> 3  1    2 100
#> 4  1    3 100
#> 5  1    4 100
#> 6  1    5 100
all(out$A==0)
#> [1] FALSE

Created on 2024-01-06 with reprex v2.0.2