Skip to content

Commit

Permalink
Allow event handling w/o early return in FMI 3.0 CS (#423)
Browse files Browse the repository at this point in the history
fixes #236
  • Loading branch information
t-sommer authored Jan 12, 2024
1 parent b4bbc4c commit c7bec6f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
6 changes: 4 additions & 2 deletions fmusim/FMIModelDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,15 @@ static FMIModelDescription* readModelDescriptionFMI3(xmlNodePtr root) {
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression((xmlChar*)"/fmiModelDescription/CoSimulation", xpathCtx);
if (xpathObj->nodesetval->nodeNr == 1) {
CALL(FMICalloc((void**)&modelDescription->coSimulation, 1, sizeof(FMICoSimulationInterface)));
modelDescription->coSimulation->modelIdentifier = (char*)xmlGetProp(xpathObj->nodesetval->nodeTab[0], (xmlChar*)"modelIdentifier");
const xmlNodePtr node = xpathObj->nodesetval->nodeTab[0];
modelDescription->coSimulation->modelIdentifier = (char*)xmlGetProp(node, (xmlChar*)"modelIdentifier");
modelDescription->coSimulation->hasEventMode = getBooleanAttribute(node, "hasEventMode");
}
xmlXPathFreeObject(xpathObj);

xpathObj = xmlXPathEvalExpression((xmlChar*)"/fmiModelDescription/ModelExchange", xpathCtx);
if (xpathObj->nodesetval->nodeNr == 1) {
xmlNodePtr node = xpathObj->nodesetval->nodeTab[0];
const xmlNodePtr node = xpathObj->nodesetval->nodeTab[0];
CALL(FMICalloc((void**)&modelDescription->modelExchange, 1, sizeof(FMIModelExchangeInterface)));
modelDescription->modelExchange->modelIdentifier = (char*)xmlGetProp(node, (xmlChar*)"modelIdentifier");
modelDescription->modelExchange->providesDirectionalDerivatives = getBooleanAttribute(node, "providesDirectionalDerivatives");
Expand Down
1 change: 1 addition & 0 deletions fmusim/FMIModelDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef struct {
typedef struct {

const char* modelIdentifier;
bool hasEventMode;

} FMICoSimulationInterface;

Expand Down
1 change: 0 additions & 1 deletion fmusim/fmusim.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ int main(int argc, const char* argv[]) {
} else if (!strcmp(v, "--early-return-allowed")) {
earlyReturnAllowed = true;
} else if (!strcmp(v, "--event-mode-used")) {
earlyReturnAllowed = true;
eventModeUsed = true;
} else if (!strcmp(v, "--record-intermediate-values")) {
recordIntermediateValues = true;
Expand Down
13 changes: 7 additions & 6 deletions fmusim/fmusim_fmi3_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ FMIStatus simulateFMI3CS(FMIInstance* S,

nextInputEventTime = FMINextInputEvent(input, time);

inputEvent = nextCommunicationPoint > nextInputEventTime;
inputEvent = nextCommunicationPoint >= nextInputEventTime;

if (inputEvent) {
nextCommunicationPoint = nextInputEventTime;
}

stepSize = nextCommunicationPoint - time;

if (settings->eventModeUsed) {
CALL(FMIApplyInput(S, input, time, false, true, false));
} else {
CALL(FMIApplyInput(S, input, time, true, true, true));
}
CALL(FMIApplyInput(S, input, time,
!settings->eventModeUsed, // discrete
true, // continuous
!settings->eventModeUsed // afterEvent
));

CALL(FMI3DoStep(S,
time, // currentCommunicationPoint
Expand All @@ -167,6 +167,7 @@ FMIStatus simulateFMI3CS(FMIInstance* S,
));

if (earlyReturn && !settings->earlyReturnAllowed) {
FMILogError("The FMU returned early from fmi3DoStep() but early return is not allowed.");
status = FMIError;
goto TERMINATE;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmusim.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ def test_event_mode_time_events(platform):
interface_type='cs',
test_name='test_event_mode_input_events',
args=[
'--event-mode-used',
'--stop-time', '5',
'--output-interval', '2.5',
'--event-mode-used'
'--event-mode-used',
'--early-return-allowed'
],
model='Stair.fmu'
)
Expand Down

0 comments on commit c7bec6f

Please sign in to comment.