diff --git a/fmusim/fmusim_fmi2_cs.c b/fmusim/fmusim_fmi2_cs.c index 51985afc..177b2460 100644 --- a/fmusim/fmusim_fmi2_cs.c +++ b/fmusim/fmusim_fmi2_cs.c @@ -62,7 +62,7 @@ FMIStatus simulateFMI2CS( CALL(FMI2GetRealStatus(S, fmi2LastSuccessfulTime, &lastSuccessfulTime)); - CALL(FMISample(S, time, result)); + CALL(FMISample(S, lastSuccessfulTime, result)); break; } diff --git a/fmusim/fmusim_fmi3_cs.c b/fmusim/fmusim_fmi3_cs.c index 5051cd21..87a71232 100644 --- a/fmusim/fmusim_fmi3_cs.c +++ b/fmusim/fmusim_fmi3_cs.c @@ -126,12 +126,12 @@ FMIStatus simulateFMI3CS(FMIInstance* S, } } + CALL(FMISample(S, time, recorder)); + size_t nSteps = 0; for (;;) { - CALL(FMISample(S, time, recorder)); - if (time >= settings->stopTime) { break; } @@ -161,7 +161,7 @@ FMIStatus simulateFMI3CS(FMIInstance* S, stepSize, // communicationStepSize fmi3True, // noSetFMUStatePriorToCurrentPoint &eventEncountered, // eventEncountered - &terminateSimulation, // terminate + &terminateSimulation, // terminateSimulation &earlyReturn, // earlyReturn &lastSuccessfulTime // lastSuccessfulTime )); @@ -172,10 +172,6 @@ FMIStatus simulateFMI3CS(FMIInstance* S, goto TERMINATE; } - if (terminateSimulation) { - break; - } - if (earlyReturn && lastSuccessfulTime < nextCommunicationPoint) { time = lastSuccessfulTime; } else { @@ -186,9 +182,13 @@ FMIStatus simulateFMI3CS(FMIInstance* S, nSteps++; } - if (settings->eventModeUsed && (inputEvent || eventEncountered)) { + CALL(FMISample(S, time, recorder)); - CALL(FMISample(S, time, recorder)); + if (terminateSimulation) { + goto TERMINATE; + } + + if (settings->eventModeUsed && (inputEvent || eventEncountered)) { CALL(FMI3EnterEventMode(S)); @@ -211,7 +211,8 @@ FMIStatus simulateFMI3CS(FMIInstance* S, &nextEventTime)); if (terminateSimulation) { - break; + CALL(FMISample(S, time, recorder)); + goto TERMINATE; } } while (discreteStatesNeedUpdate); @@ -221,6 +222,8 @@ FMIStatus simulateFMI3CS(FMIInstance* S, } CALL(FMI3EnterStepMode(S)); + + CALL(FMISample(S, time, recorder)); } } diff --git a/src/fmi2Functions.c b/src/fmi2Functions.c index c756d153..234a1c03 100644 --- a/src/fmi2Functions.c +++ b/src/fmi2Functions.c @@ -660,6 +660,11 @@ fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint, CALL(eventUpdate(S)); } #endif + + if (S->terminateSimulation) { + status = Discard; + goto TERMINATE; + } } S->nextCommunicationPoint = currentCommunicationPoint + communicationStepSize; diff --git a/src/fmi3Functions.c b/src/fmi3Functions.c index 55447ed9..c2ae510b 100644 --- a/src/fmi3Functions.c +++ b/src/fmi3Functions.c @@ -1346,6 +1346,7 @@ fmi3Status fmi3DoStep(fmi3Instance instance, *eventHandlingNeeded = fmi3False; *terminateSimulation = fmi3False; + *earlyReturn = fmi3False; while (true) { @@ -1385,9 +1386,15 @@ fmi3Status fmi3DoStep(fmi3Instance instance, } } #endif + + if (S->terminateSimulation) { + break; + } } - *earlyReturn = !nextCommunicationPointReached; + *terminateSimulation = S->terminateSimulation; + *earlyReturn = S->earlyReturnAllowed && !nextCommunicationPointReached; + *lastSuccessfulTime = S->time; if (nextCommunicationPointReached) { S->nextCommunicationPoint = currentCommunicationPoint + communicationStepSize; @@ -1395,8 +1402,6 @@ fmi3Status fmi3DoStep(fmi3Instance instance, S->nextCommunicationPoint = S->time; } - *lastSuccessfulTime = S->time; - END_FUNCTION(); }