Skip to content

Commit

Permalink
Issue #3682 Only enforce Sundials stop time if possible
Browse files Browse the repository at this point in the history
The event handling can cause the stopping time to decrease to a smaller value than the
internal time reached by Sundials variable step size solver.
When this happens, simply do not enforce the stopping time
  • Loading branch information
jaeandersson committed May 10, 2024
1 parent 26f2e95 commit 935e4ae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions casadi/core/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ int Integrator::eval(const double** arg, double** res,
if (next_event(m)) return 1;
}
// Advance solution
if (verbose_) casadi_message("Integrating forward to output time " + str(m->k) + ": t_next = "
+ str(m->t_next) + ", t_stop = " + str(m->t_stop));
if (verbose_) casadi_message("Interval " + str(m->k) + ": Integrating forward from "
+ str(m->t) + " to " + str(m->t_next) + ", t_stop = " + str(m->t_stop));
advance(m);
// Update current time
m->t = m->t_next;
Expand Down
7 changes: 5 additions & 2 deletions casadi/interfaces/sundials/cvodes_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ void CvodesInterface::reset(IntegratorMemory* mem, bool first_call) const {
// Only reinitialize solver at first call or if event handling is required
// May want to always enable this after more testing
if (first_call || ne_ > 0) {
casadi_message("reinitializing cvodes")
// Re-initialize forward integration
THROWING(CVodeReInit, m->mem, m->t, m->v_xz);

Expand All @@ -261,7 +260,11 @@ void CvodesInterface::advance(IntegratorMemory* mem) const {
auto m = to_mem(mem);

// Do not integrate past change in input signals or past the end
THROWING(CVodeSetStopTime, m->mem, m->t_stop);
// The event handling may cause the stop time to become smaller than internal time reached,
// in which case the stop time cannot be enforced
if (m->t_stop >= m->tcur) {
THROWING(CVodeSetStopTime, m->mem, m->t_stop);
}

// Integrate, unless already at desired time
const double ttol = 1e-9;
Expand Down
7 changes: 6 additions & 1 deletion casadi/interfaces/sundials/idas_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,13 @@ void IdasInterface::reset(IntegratorMemory* mem, bool first_call) const {
void IdasInterface::advance(IntegratorMemory* mem) const {
auto m = to_mem(mem);


// Do not integrate past change in input signals or past the end
THROWING(IDASetStopTime, m->mem, m->t_stop);
// The event handling may cause the stop time to become smaller than internal time reached,
// in which case the stop time cannot be enforced
if (m->t_stop >= m->tcur) {
THROWING(IDASetStopTime, m->mem, m->t_stop);
}

// Integrate, unless already at desired time
double ttol = 1e-9; // tolerance
Expand Down
3 changes: 2 additions & 1 deletion casadi/interfaces/sundials/sundials_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ void SundialsInterface::reset_stats(SundialsMemory* m) const {
// Reset stats, forward problem
m->nsteps = m->nfevals = m->nlinsetups = m->netfails = 0;
m->qlast = m->qcur = -1;
m->hinused = m->hlast = m->hcur = m->tcur = casadi::nan;
m->tcur = t0_;
m->hinused = m->hlast = m->hcur = casadi::nan;
m->nniters = m->nncfails = 0;

// Reset stats, backward problem
Expand Down

0 comments on commit 935e4ae

Please sign in to comment.