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

Fix crashes when errors occur at output timepoints #2555

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
18 changes: 16 additions & 2 deletions include/amici/forwardproblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define AMICI_FORWARDPROBLEM_H

#include "amici/defines.h"
#include "amici/edata.h"
#include "amici/misc.h"
#include "amici/model.h"
#include "amici/vector.h"
Expand Down Expand Up @@ -195,7 +196,7 @@
if (model->getTimepoint(it) == initial_state_.t)
return getInitialSimulationState();
auto map_iter = timepoint_states_.find(model->getTimepoint(it));
assert(map_iter != timepoint_states_.end());
Ensures(map_iter != timepoint_states_.end());
return map_iter->second;
};

Expand Down Expand Up @@ -441,8 +442,21 @@
* @brief destructor, stores simulation state
*/
~FinalStateStorer() {
if (fwd_)
if (fwd_) {
fwd_->final_state_ = fwd_->getSimulationState();
// if there is an associated data point, also store it in
// timepoint_states if it's not present there.
// this may happen if there is an error just at
// (or indistinguishably before) an output timepoint
auto final_time = fwd_->getFinalTime();
if (!fwd_->timepoint_states_.count(final_time) && fwd_->edata
&& std::find(
fwd_->edata->ts_.cbegin(), fwd_->edata->ts_.cend(),
final_time
) != fwd_->edata->ts_.cend()) {
dweindl marked this conversation as resolved.
Show resolved Hide resolved
fwd_->timepoint_states_[final_time] = fwd_->final_state_;

Check warning on line 457 in include/amici/forwardproblem.h

View check run for this annotation

Codecov / codecov/patch

include/amici/forwardproblem.h#L457

Added line #L457 was not covered by tests
}
}
}

private:
Expand Down
6 changes: 3 additions & 3 deletions src/rdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void ReturnData::processForwardProblem(
if (edata)
initializeObjectiveFunction(model.hasQuadraticLLH());

auto initialState = fwd.getInitialSimulationState();
auto const& initialState = fwd.getInitialSimulationState();
if (initialState.x.getLength() == 0 && model.nx_solver > 0)
return; // if x wasn't set forward problem failed during initialization

Expand All @@ -259,7 +259,7 @@ void ReturnData::processForwardProblem(
realtype tf = fwd.getFinalTime();
for (int it = 0; it < model.nt(); it++) {
if (model.getTimepoint(it) <= tf) {
auto simulation_state = fwd.getSimulationStateTimepoint(it);
auto const simulation_state = fwd.getSimulationStateTimepoint(it);
model.setModelState(simulation_state.state);
getDataOutput(it, model, simulation_state, edata);
} else {
Expand All @@ -273,7 +273,7 @@ void ReturnData::processForwardProblem(
if (nz > 0) {
auto rootidx = fwd.getRootIndexes();
for (int iroot = 0; iroot <= fwd.getEventCounter(); iroot++) {
auto simulation_state = fwd.getSimulationStateEvent(iroot);
auto const simulation_state = fwd.getSimulationStateEvent(iroot);
model.setModelState(simulation_state.state);
getEventOutput(
simulation_state.t, rootidx.at(iroot), model, simulation_state,
Expand Down
Loading