Skip to content

Commit 477a354

Browse files
authored
Handle terminateSimulation = true (#487)
* 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
1 parent 47e8cc7 commit 477a354

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

fmusim/fmusim_fmi2_cs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ FMIStatus simulateFMI2CS(
6262

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

65-
CALL(FMISample(S, time, result));
65+
CALL(FMISample(S, lastSuccessfulTime, result));
6666

6767
break;
6868
}

fmusim/fmusim_fmi3_cs.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
126126
}
127127
}
128128

129+
CALL(FMISample(S, time, recorder));
130+
129131
size_t nSteps = 0;
130132

131133
for (;;) {
132134

133-
CALL(FMISample(S, time, recorder));
134-
135135
if (time >= settings->stopTime) {
136136
break;
137137
}
@@ -161,7 +161,7 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
161161
stepSize, // communicationStepSize
162162
fmi3True, // noSetFMUStatePriorToCurrentPoint
163163
&eventEncountered, // eventEncountered
164-
&terminateSimulation, // terminate
164+
&terminateSimulation, // terminateSimulation
165165
&earlyReturn, // earlyReturn
166166
&lastSuccessfulTime // lastSuccessfulTime
167167
));
@@ -172,10 +172,6 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
172172
goto TERMINATE;
173173
}
174174

175-
if (terminateSimulation) {
176-
break;
177-
}
178-
179175
if (earlyReturn && lastSuccessfulTime < nextCommunicationPoint) {
180176
time = lastSuccessfulTime;
181177
} else {
@@ -186,9 +182,13 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
186182
nSteps++;
187183
}
188184

189-
if (settings->eventModeUsed && (inputEvent || eventEncountered)) {
185+
CALL(FMISample(S, time, recorder));
190186

191-
CALL(FMISample(S, time, recorder));
187+
if (terminateSimulation) {
188+
goto TERMINATE;
189+
}
190+
191+
if (settings->eventModeUsed && (inputEvent || eventEncountered)) {
192192

193193
CALL(FMI3EnterEventMode(S));
194194

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

213213
if (terminateSimulation) {
214-
break;
214+
CALL(FMISample(S, time, recorder));
215+
goto TERMINATE;
215216
}
216217

217218
} while (discreteStatesNeedUpdate);
@@ -221,6 +222,8 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
221222
}
222223

223224
CALL(FMI3EnterStepMode(S));
225+
226+
CALL(FMISample(S, time, recorder));
224227
}
225228
}
226229

src/fmi2Functions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@ fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint,
660660
CALL(eventUpdate(S));
661661
}
662662
#endif
663+
664+
if (S->terminateSimulation) {
665+
status = Discard;
666+
goto TERMINATE;
667+
}
663668
}
664669

665670
S->nextCommunicationPoint = currentCommunicationPoint + communicationStepSize;

src/fmi3Functions.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ fmi3Status fmi3DoStep(fmi3Instance instance,
13461346

13471347
*eventHandlingNeeded = fmi3False;
13481348
*terminateSimulation = fmi3False;
1349+
*earlyReturn = fmi3False;
13491350

13501351
while (true) {
13511352

@@ -1385,18 +1386,22 @@ fmi3Status fmi3DoStep(fmi3Instance instance,
13851386
}
13861387
}
13871388
#endif
1389+
1390+
if (S->terminateSimulation) {
1391+
break;
1392+
}
13881393
}
13891394

1390-
*earlyReturn = !nextCommunicationPointReached;
1395+
*terminateSimulation = S->terminateSimulation;
1396+
*earlyReturn = S->earlyReturnAllowed && !nextCommunicationPointReached;
1397+
*lastSuccessfulTime = S->time;
13911398

13921399
if (nextCommunicationPointReached) {
13931400
S->nextCommunicationPoint = currentCommunicationPoint + communicationStepSize;
13941401
} else {
13951402
S->nextCommunicationPoint = S->time;
13961403
}
13971404

1398-
*lastSuccessfulTime = S->time;
1399-
14001405
END_FUNCTION();
14011406
}
14021407

0 commit comments

Comments
 (0)