Skip to content

Commit

Permalink
Use FMIIsClose() to compare time values (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer authored Sep 27, 2024
1 parent 310cba4 commit 96b1a67
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 77 deletions.
8 changes: 4 additions & 4 deletions fmusim/FMI1CSSimulation.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "FMIUtil.h"
#include "FMI1.h"
#include "FMI1CSSimulation.h"

#define FMI_PATH_MAX 4096

#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)


FMIStatus FMI1CSSimulate(const FMISimulationSettings* s) {

FMIStatus status = FMIOK;
Expand Down Expand Up @@ -42,12 +42,12 @@ FMIStatus FMI1CSSimulate(const FMISimulationSettings* s) {

CALL(FMISample(S, time, s->recorder));

CALL(FMIApplyInput(S, s->input, time, true, true, false));

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

CALL(FMIApplyInput(S, s->input, time, true, true, false));

const FMIStatus doStepStatus = FMI1DoStep(S, time, s->outputInterval, fmi1True);

if (doStepStatus == fmi1Discard) {
Expand Down
32 changes: 14 additions & 18 deletions fmusim/FMI1MESimulation.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <stdlib.h>
#include <math.h>

#include "FMIUtil.h"
#include "FMI1.h"
#include "FMI1MESimulation.h"


#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)


FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {

FMIStatus status = FMIOK;
Expand Down Expand Up @@ -57,10 +56,6 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {
// initialize
CALL(FMI1Initialize(S, s->tolerance > 0, s->tolerance, &eventInfo));

if (!eventInfo.upcomingTimeEvent) {
eventInfo.nextEventTime = INFINITY;
}

const FMISolverParameters solverFunctions = {
.modelInstance = S,
.input = s->input,
Expand Down Expand Up @@ -91,7 +86,7 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {

CALL(FMISample(S, time, s->recorder));

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

Expand All @@ -101,14 +96,19 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {

nextInputEventTime = FMINextInputEvent(s->input, time);

inputEvent = nextCommunicationPoint >= nextInputEventTime;

timeEvent = nextCommunicationPoint >= eventInfo.nextEventTime;
if (nextCommunicationPoint > nextInputEventTime && !FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}

if (inputEvent || timeEvent) {
nextCommunicationPoint = fmin(nextInputEventTime, eventInfo.nextEventTime);
if (eventInfo.upcomingTimeEvent && nextCommunicationPoint > eventInfo.nextEventTime && !FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime)) {
nextCommunicationPoint = eventInfo.nextEventTime;
}

inputEvent = FMIIsClose(nextCommunicationPoint, nextInputEventTime);

timeEvent = eventInfo.upcomingTimeEvent && FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime);


CALL(s->solverStep(solver, nextCommunicationPoint, &time, &stateEvent));

CALL(FMI1SetTime(S, time));
Expand All @@ -119,7 +119,7 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {
false // after event
));

if (time == nextRegularPoint) {
if (FMIIsClose(time, nextRegularPoint)) {
nSteps++;
}

Expand All @@ -144,8 +144,8 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {

resetSolver = fmi1False;

// event iteration
do {

CALL(FMI1EventUpdate(S, fmi1True, &eventInfo));

if (eventInfo.terminateSimulation) {
Expand All @@ -157,10 +157,6 @@ FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {

} while (!eventInfo.iterationConverged);

if (!eventInfo.upcomingTimeEvent) {
eventInfo.nextEventTime = INFINITY;
}

if (resetSolver) {
s->solverReset(solver, time);
}
Expand Down
7 changes: 3 additions & 4 deletions fmusim/FMI2CSSimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "FMI2.h"
#include "FMI2CSSimulation.h"


#define FMI_PATH_MAX 4096

#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
Expand Down Expand Up @@ -53,12 +52,12 @@ FMIStatus FMI2CSSimulate(const FMISimulationSettings* s) {

CALL(FMISample(S, time, s->recorder));

CALL(FMIApplyInput(S, s->input, time, true, true, false));

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

CALL(FMIApplyInput(S, s->input, time, true, true, false));

const FMIStatus doStepStatus = FMI2DoStep(S, time, s->outputInterval, fmi2True);

if (doStepStatus == fmi2Discard) {
Expand Down
29 changes: 12 additions & 17 deletions fmusim/FMI2MESimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {

} while (eventInfo.newDiscreteStatesNeeded);

if (!eventInfo.nextEventTimeDefined) {
eventInfo.nextEventTime = INFINITY;
}

CALL(FMI2EnterContinuousTimeMode(S));
}

Expand Down Expand Up @@ -130,7 +126,7 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {

CALL(FMISample(S, time, s->recorder));

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

Expand All @@ -140,14 +136,18 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {

nextInputEventTime = FMINextInputEvent(s->input, time);

inputEvent = nextCommunicationPoint >= nextInputEventTime;

timeEvent = nextCommunicationPoint >= eventInfo.nextEventTime;
if (nextCommunicationPoint > nextInputEventTime && !FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}

if (inputEvent || timeEvent) {
nextCommunicationPoint = fmin(nextInputEventTime, eventInfo.nextEventTime);
if (eventInfo.nextEventTimeDefined && nextCommunicationPoint > eventInfo.nextEventTime && !FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime)) {
nextCommunicationPoint = eventInfo.nextEventTime;
}

inputEvent = FMIIsClose(nextCommunicationPoint, nextInputEventTime);

timeEvent = eventInfo.nextEventTimeDefined && FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime);

CALL(s->solverStep(solver, nextCommunicationPoint, &time, &stateEvent));

CALL(FMI2SetTime(S, time));
Expand All @@ -158,7 +158,7 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {
false // after event
));

if (time == nextRegularPoint) {
if (FMIIsClose(time, nextRegularPoint)) {
nSteps++;
}

Expand Down Expand Up @@ -188,8 +188,8 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {

resetSolver = fmi2False;

// event iteration
do {

CALL(FMI2NewDiscreteStates(S, &eventInfo));

if (eventInfo.terminateSimulation) {
Expand All @@ -201,11 +201,6 @@ FMIStatus FMI2MESimulate(const FMISimulationSettings* s) {

} while (eventInfo.newDiscreteStatesNeeded);

if (!eventInfo.nextEventTimeDefined) {
eventInfo.nextEventTime = INFINITY;
}

// enter Continuous-Time Mode
CALL(FMI2EnterContinuousTimeMode(S));

if (resetSolver) {
Expand Down
18 changes: 5 additions & 13 deletions fmusim/FMI3CSSimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {

} while (discreteStatesNeedUpdate);

if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}

CALL(FMI3EnterStepMode(S));
}
}
Expand All @@ -135,7 +131,7 @@ FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {

for (;;) {

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

Expand All @@ -145,12 +141,12 @@ FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {

nextInputEventTime = FMINextInputEvent(s->input, time);

inputEvent = nextCommunicationPoint >= nextInputEventTime;

if (inputEvent) {
if (nextCommunicationPoint > nextInputEventTime && !FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}

inputEvent = FMIIsClose(nextCommunicationPoint, nextInputEventTime);

stepSize = nextCommunicationPoint - time;

CALL(FMIApplyInput(S, s->input, time,
Expand Down Expand Up @@ -181,7 +177,7 @@ FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {
time = nextCommunicationPoint;
}

if (time == nextRegularPoint) {
if (FMIIsClose(time, nextRegularPoint)) {
nSteps++;
}

Expand Down Expand Up @@ -220,10 +216,6 @@ FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {

} while (discreteStatesNeedUpdate);

if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}

CALL(FMI3EnterStepMode(S));

CALL(FMISample(S, time, s->recorder));
Expand Down
27 changes: 12 additions & 15 deletions fmusim/FMI3MESimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {

} while (discreteStatesNeedUpdate);

if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}

CALL(FMI3EnterContinuousTimeMode(S));
}

Expand Down Expand Up @@ -138,7 +134,7 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {

CALL(FMISample(S, time, s->recorder));

if (time >= s->stopTime) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}

Expand All @@ -148,14 +144,18 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {

nextInputEventTime = FMINextInputEvent(s->input, time);

inputEvent = nextCommunicationPoint >= nextInputEventTime;

timeEvent = nextCommunicationPoint >= nextEventTime;
if (nextCommunicationPoint > nextInputEventTime && !FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}

if (inputEvent || timeEvent) {
nextCommunicationPoint = fmin(nextInputEventTime, nextEventTime);
if (nextEventTimeDefined && nextCommunicationPoint > nextEventTime && !FMIIsClose(nextCommunicationPoint, nextEventTime)) {
nextCommunicationPoint = nextEventTime;
}

inputEvent = FMIIsClose(nextCommunicationPoint, nextInputEventTime);

timeEvent = nextEventTimeDefined && FMIIsClose(nextCommunicationPoint, nextEventTime);

CALL(s->solverStep(solver, nextCommunicationPoint, &time, &stateEvent));

CALL(FMI3SetTime(S, time));
Expand All @@ -166,7 +166,7 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {
false // after event
));

if (time == nextRegularPoint) {
if (FMIIsClose(time, nextRegularPoint)) {
nSteps++;
}

Expand All @@ -181,6 +181,7 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {

if (inputEvent || timeEvent || stateEvent || stepEvent) {

// record the values before the event
CALL(FMISample(S, time, s->recorder));

CALL(FMI3EnterEventMode(S));
Expand Down Expand Up @@ -214,10 +215,6 @@ FMIStatus FMI3MESimulate(const FMISimulationSettings* s) {

} while (discreteStatesNeedUpdate);

if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}

CALL(FMI3EnterContinuousTimeMode(S));

if (resetSolver) {
Expand Down
14 changes: 8 additions & 6 deletions fmusim/FMIStaticInput.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ double FMINextInputEvent(const FMIStaticInput* input, double time) {
const double t0 = input->time[i];
const double t1 = input->time[i + 1];

if (time >= t1) {
if (time > t1 || FMIIsClose(time, t1)) {
continue;
}

Expand Down Expand Up @@ -266,22 +266,24 @@ FMIStatus FMIApplyInput(FMIInstance* instance, const FMIStaticInput* input, doub

size_t row = 0;

for (size_t i = 1; i < input->nRows; i++) {
for (size_t j = 1; j < input->nRows; j++) {

const double t = input->time[i];
const double t = input->time[j];

if (t >= time) {
if (t > time || FMIIsClose(t, time)) {
break;
}

row = i;
row = j;
}

if (afterEvent) {

while (row < input->nRows - 1) {

if (input->time[row + 1] > time) {
const double t = input->time[row + 1];

if (t > time && !FMIIsClose(t, time)) {
break;
}

Expand Down
Loading

0 comments on commit 96b1a67

Please sign in to comment.