Skip to content

Commit

Permalink
Handle terminateSimulation = true (#487)
Browse files Browse the repository at this point in the history
* Fix handling of  terminateSimulation = true in FMI 3.0 CS
* Use lastSuccessfulTime when sampling after fmi2Terminated = true
* Return fmi2Discard from fmi2DoStep() if terminateSimulation = true

fixes #482
  • Loading branch information
t-sommer authored Apr 17, 2024
1 parent 47e8cc7 commit 477a354
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fmusim/fmusim_fmi2_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ FMIStatus simulateFMI2CS(

CALL(FMI2GetRealStatus(S, fmi2LastSuccessfulTime, &lastSuccessfulTime));

CALL(FMISample(S, time, result));
CALL(FMISample(S, lastSuccessfulTime, result));

break;
}
Expand Down
23 changes: 13 additions & 10 deletions fmusim/fmusim_fmi3_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
stepSize, // communicationStepSize
fmi3True, // noSetFMUStatePriorToCurrentPoint
&eventEncountered, // eventEncountered
&terminateSimulation, // terminate
&terminateSimulation, // terminateSimulation
&earlyReturn, // earlyReturn
&lastSuccessfulTime // lastSuccessfulTime
));
Expand All @@ -172,10 +172,6 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
goto TERMINATE;
}

if (terminateSimulation) {
break;
}

if (earlyReturn && lastSuccessfulTime < nextCommunicationPoint) {
time = lastSuccessfulTime;
} else {
Expand All @@ -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));

Expand All @@ -211,7 +211,8 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
&nextEventTime));

if (terminateSimulation) {
break;
CALL(FMISample(S, time, recorder));
goto TERMINATE;
}

} while (discreteStatesNeedUpdate);
Expand All @@ -221,6 +222,8 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
}

CALL(FMI3EnterStepMode(S));

CALL(FMISample(S, time, recorder));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/fmi2Functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 8 additions & 3 deletions src/fmi3Functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ fmi3Status fmi3DoStep(fmi3Instance instance,

*eventHandlingNeeded = fmi3False;
*terminateSimulation = fmi3False;
*earlyReturn = fmi3False;

while (true) {

Expand Down Expand Up @@ -1385,18 +1386,22 @@ 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;
} else {
S->nextCommunicationPoint = S->time;
}

*lastSuccessfulTime = S->time;

END_FUNCTION();
}

Expand Down

0 comments on commit 477a354

Please sign in to comment.