Skip to content

Commit 9ddc658

Browse files
authored
Fix root check for initial events (#2885)
Don't unnecessarily store a "discontinuity" if there is none. First, check if any root was found.
1 parent aaa3af0 commit 9ddc658

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

include/amici/forwardproblem.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,24 @@ class EventHandlingSimulator {
304304
int it_ = -1;
305305

306306
/**
307-
* @brief Execute everything necessary for the handling of events
307+
* @brief Execute everything necessary for the handling of events.
308+
*
309+
* Assumes that at least one event triggered.
308310
*
309311
* @param initial_event initial event flag
310312
* @param edata experimental data
311313
*/
312-
void handle_event(bool initial_event, ExpData const* edata);
314+
void handle_events(bool initial_event, ExpData const* edata);
315+
316+
/**
317+
* @brief Handle initial events.
318+
*
319+
* Check if there are any initial events that need to be handled
320+
* and execute the necessary steps.
321+
*
322+
* @param edata experimental data
323+
*/
324+
void handle_initial_events(ExpData const* edata);
313325

314326
private:
315327
/**

src/forwardproblem.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ void EventHandlingSimulator::run(
6161
ws_->t = t0;
6262
ws_->tlastroot = t0;
6363

64-
// handle initial events
65-
if (model_->ne && std::ranges::any_of(ws_->roots_found, [](int rf) {
66-
return rf == 1;
67-
})) {
68-
handle_event(true, edata);
69-
}
64+
handle_initial_events(edata);
7065

7166
// store initial state and sensitivity
7267
result.initial_state_ = get_simulation_state();
@@ -137,7 +132,7 @@ void EventHandlingSimulator::run(
137132
++it_trigger_timepoints;
138133
}
139134

140-
handle_event(false, edata);
135+
handle_events(false, edata);
141136
}
142137
}
143138
}
@@ -221,7 +216,7 @@ void EventHandlingSimulator::run_steady_state(
221216
++it_trigger_timepoints;
222217
}
223218

224-
handle_event(false, nullptr);
219+
handle_events(false, nullptr);
225220
}
226221

227222
solver_->writeSolution(ws_->t, ws_->x, ws_->dx, ws_->sx);
@@ -365,7 +360,7 @@ void ForwardProblem::handlePostequilibration() {
365360
}
366361
}
367362

368-
void EventHandlingSimulator::handle_event(
363+
void EventHandlingSimulator::handle_events(
369364
bool const initial_event, ExpData const* edata
370365
) {
371366
// Some event triggered. This may be due to some discontinuity, a bolus to
@@ -493,6 +488,14 @@ void EventHandlingSimulator::handle_event(
493488
}
494489
}
495490

491+
void EventHandlingSimulator::handle_initial_events(ExpData const* edata) {
492+
if (model_->ne && std::ranges::any_of(ws_->roots_found, [](int const rf) {
493+
return rf == 1;
494+
})) {
495+
handle_events(true, edata);
496+
}
497+
}
498+
496499
void EventHandlingSimulator::store_event(ExpData const* edata) {
497500
bool const is_last_timepoint
498501
= (ws_->t == model_->getTimepoint(model_->nt() - 1));
@@ -1063,8 +1066,7 @@ void SteadystateProblem::runSteadystateSimulationFwd() {
10631066
simulator_.emplace(model_, solver_, ws_, nullptr);
10641067
simulator_->result.initial_state_ = period_result_.initial_state_;
10651068

1066-
// handle initial events
1067-
simulator_->handle_event(true, nullptr);
1069+
simulator_->handle_initial_events(nullptr);
10681070

10691071
updateRightHandSide();
10701072

@@ -1141,7 +1143,7 @@ void SteadystateProblem::findSteadyStateByNewtonsMethod(bool newton_retry) {
11411143
simulator_->result.initial_state_.dx = ws_->dx;
11421144
simulator_->result.initial_state_.sx = ws_->sx;
11431145

1144-
simulator_->handle_event(true, nullptr);
1146+
simulator_->handle_initial_events(nullptr);
11451147
period_result_ = simulator_->result;
11461148
}
11471149

0 commit comments

Comments
 (0)