Skip to content

Commit

Permalink
Merge pull request #1118 from metrumresearchgroup/fix/hx-reset
Browse files Browse the repository at this point in the history
Fix mevent hx reset
  • Loading branch information
kylebaron authored Nov 14, 2023
2 parents d128f07 + 434f667 commit c52292b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions inst/maintenance/unit-cpp/test-cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ test_that("build a model with mrgx and nm-vars", {
)
})

code <- '
$PK
if(NEWIND <= 1) capture FLAG = 0;
$CMT A
$TABLE
if(TIME==2) self.mevent(2.001,33);
if(EVID==33) ++FLAG;
'
test_that("ev history is reset", {
mod <- mcode("test-cpp-re-event", code, end = -1, add = c(1,2,3))
out <- mrgsim_df(mod, ev(amt = 0, ID = 1:2, evid = 2))
expect_equal(sum(out$FLAG), 2)
out3 <- subset(out, time==3)
expect_length(out3$FLAG, 2)
expect_true(all(out3$FLAG==1))
})
5 changes: 4 additions & 1 deletion src/devtran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ Rcpp::List DEVTRAN(const Rcpp::List parin,

prob.config_call();
reclist mtimehx;
bool used_mtimehx = false;

bool has_idata = idat.nrow() > 0;
int this_idata_row = 0;
Expand All @@ -402,6 +403,7 @@ Rcpp::List DEVTRAN(const Rcpp::List parin,
dat.next_id(i);
prob.idn(i);
prob.reset_newid(id);
if(used_mtimehx) mtimehx.clear();

if(i==0) {
prob.newind(0);
Expand Down Expand Up @@ -616,7 +618,7 @@ Rcpp::List DEVTRAN(const Rcpp::List parin,
}

if(prob.any_mtime()) {
if(prob.newind() <=1) mtimehx.clear();
// Will set used_mtimehx only if we push back
std::vector<mrgsolve::evdata> mt = prob.mtimes();
for(size_t mti = 0; mti < mt.size(); ++mti) {
double this_time = (mt[mti]).time;
Expand All @@ -643,6 +645,7 @@ Rcpp::List DEVTRAN(const Rcpp::List parin,
}
}
}
used_mtimehx = mtimehx.size() > 0;
prob.clear_mtime();
}
if(this_rec->output()) {
Expand Down

0 comments on commit c52292b

Please sign in to comment.