Skip to content

Commit 414b5a6

Browse files
Fix condition for resetting the CVODE solver
refs #1516 It is necessary to reset the solver when the state variable values change.
1 parent 3989562 commit 414b5a6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/OMSimulatorLib/SystemSC.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,22 +962,33 @@ oms_status_enu_t oms::SystemSC::doStepCVODE(double stopTime)
962962
if (0 == nStates[i])
963963
continue;
964964

965+
std::vector<double> prev_values;
966+
prev_values.reserve(nStates[i]);
967+
968+
// Check whether state values have changed due to the event
969+
prev_values.assign(states[i], states[i] + nStates[i]);
970+
965971
status = fmus[i]->getContinuousStates(states[i]);
966972
if (oms_status_ok != status) return status;
967973

968-
// Check whether dervative values have changed due to the event
969-
std::vector<double> prevDer;
970-
prevDer.reserve(nStates[i]);
971-
prevDer.assign(states_der[i], states_der[i] + nStates[i]);
974+
for (int k = 0; k < nStates[i]; k++) {
975+
double diff = states[i][k] - prev_values[k];
976+
if (fabs(diff) > absoluteTolerance && fabs(diff) > relativeTolerance * fabs(prev_values[k]))
977+
resetSolver = true;
978+
}
979+
980+
// Check whether derivative values have changed due to the event
981+
prev_values.assign(states_der[i], states_der[i] + nStates[i]);
972982

973983
status = fmus[i]->getDerivatives(states_der[i]);
974984
if (oms_status_ok != status) return status;
975985

976986
for (int k = 0; k < nStates[i]; k++) {
977-
double diff = states_der[i][k] - prevDer[k];
978-
if (fabs(diff) > absoluteTolerance && fabs(diff) > relativeTolerance * fabs(prevDer[k]))
987+
double diff = states_der[i][k] - prev_values[k];
988+
if (fabs(diff) > absoluteTolerance && fabs(diff) > relativeTolerance * fabs(prev_values[k]))
979989
resetSolver = true;
980990
}
991+
981992
}
982993

983994
for (size_t j=0, k=0; j < fmus.size(); ++j)

0 commit comments

Comments
 (0)