Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement early return and update example #170

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions examples/cs_early_return.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
#include "util.h"


void intermediateUpdate(fmi3InstanceEnvironment instanceEnvironment,
fmi3Float64 intermediateUpdateTime,
fmi3Boolean clocksTicked,
fmi3Boolean intermediateVariableSetRequested,
fmi3Boolean intermediateVariableGetAllowed,
fmi3Boolean intermediateStepFinished,
fmi3Boolean canReturnEarly,
fmi3Boolean *earlyReturnRequested,
fmi3Float64 *earlyReturnTime) {

// stop here
*earlyReturnRequested = fmi3False;
*earlyReturnTime = intermediateUpdateTime;
}

int main(int argc, char* argv[]) {

CALL(setUp());
Expand All @@ -29,32 +14,48 @@ int main(int argc, char* argv[]) {
fmi3False, // visible
fmi3False, // loggingOn
fmi3False, // eventModeUsed
fmi3False, // earlyReturnAllowed
fmi3True, // earlyReturnAllowed
NULL, // requiredIntermediateVariables
0, // nRequiredIntermediateVariables
intermediateUpdate // intermediateUpdate
NULL // intermediateUpdate
));

// set start values
CALL(applyStartValues(S));

// initialize the FMU
CALL(FMI3EnterInitializationMode(S, fmi3False, 0.0, startTime, fmi3True, stopTime));

CALL(FMI3ExitInitializationMode(S));

for (int step = 0;; step++) {
int grid_point = 0; // current point on the grid

// calculate the current time
const fmi3Float64 time = step * h;
fmi3Float64 time = 0;

while (!terminateSimulation) {

CALL(recordVariables(S, outputFile));

if (time >= stopTime) {
break;
break; // stop time has been reached
}

// call instance s1 and check status
CALL(FMI3DoStep(S, time, h, fmi3True, &eventEncountered, &terminateSimulation, &earlyReturn, &lastSuccessfulTime));
// calculate the distance to the next grid point
const fmi3Float64 step_size = (grid_point + 1) * 0.1 - time;

// do one grid_point
CALL(FMI3DoStep(S,
time, // currentCommunicationPoint
step_size, // communicationStepSize
fmi3True, // noSetFMUStatePriorToCurrentPoint
&eventEncountered, // eventEncountered
&terminateSimulation, // terminate
&earlyReturn, // earlyReturn
&time // lastSuccessfulTime
));

if (terminateSimulation) {
printf("The FMU requested to terminate the simulation.");
break;
if (!earlyReturn) {
grid_point++; // grid point has been reached
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ typedef struct {
// internal solver steps
int nSteps;

// co-simulation
bool returnEarly;
// Co-Simulation
bool earlyReturnAllowed;

} ModelInstance;

Expand Down
10 changes: 7 additions & 3 deletions src/cosimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ModelInstance *createModelInstance(
comp->logEvents = loggingOn;
comp->logErrors = true; // always log errors
comp->nSteps = 0;
comp->returnEarly = false;
comp->earlyReturnAllowed = false;
}

if (!comp || !comp->modelData || !comp->instanceName) {
Expand Down Expand Up @@ -503,14 +503,18 @@ Status doStep(ModelInstance *comp,

eventUpdate(comp);

comp->returnEarly = comp->nextEventTime < t + tNext;

#if NZ > 0
// update previous event indicators
getEventIndicators(comp, comp->prez, NZ);
#endif

#if FMI_VERSION == 3

if (comp->earlyReturnAllowed) {
*earlyReturn = true;
break;
}

if (comp->intermediateUpdate) {

comp->state = IntermediateUpdateMode;
Expand Down
11 changes: 7 additions & 4 deletions src/fmi3Functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ fmi3Instance fmi3InstantiateCoSimulation(
fmi3CallbackLogMessage logMessage,
fmi3CallbackIntermediateUpdate intermediateUpdate) {

UNUSED(visible);
UNUSED(eventModeUsed);
UNUSED(requiredIntermediateVariables);
UNUSED(nRequiredIntermediateVariables);

ModelInstance *instance = createModelInstance(
(loggerType)logMessage,
(intermediateUpdateType)intermediateUpdate,
Expand All @@ -244,6 +249,7 @@ fmi3Instance fmi3InstantiateCoSimulation(
);

if (instance) {
instance->earlyReturnAllowed = earlyReturnAllowed;
instance->state = Instantiated;
}

Expand Down Expand Up @@ -1055,10 +1061,7 @@ fmi3Status fmi3DoStep(fmi3Instance instance,
return fmi3Error;
}

// TODO: pass to doStep()
*terminateSimulation = fmi3False;

return (fmi3Status) doStep(S, currentCommunicationPoint, currentCommunicationPoint + communicationStepSize, eventEncountered, earlyReturn, terminateSimulation, lastSuccessfulTime);
return (fmi3Status) doStep(S, currentCommunicationPoint, currentCommunicationPoint + communicationStepSize, eventEncountered, terminateSimulation, earlyReturn, lastSuccessfulTime);
}

fmi3Status fmi3ActivateModelPartition(fmi3Instance instance,
Expand Down