Skip to content

Commit

Permalink
Calculate counter in getInt32() for FMI 3.0 (#606)
Browse files Browse the repository at this point in the history
fixes #554
  • Loading branch information
t-sommer authored Oct 10, 2024
1 parent 10d78bb commit 0f4eeb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions Stair/model.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <float.h> // for DBL_EPSILON
#include <math.h> // for fabs()
#include "config.h"
#include "model.h"


static void calculateCounter(ModelInstance* comp) {

if (comp->nextEventTimeDefined && isClose(comp->time, comp->nextEventTime)) {
M(counter)++;
comp->nextEventTime += 1;
}
}

void setStartValues(ModelInstance *comp) {
M(counter) = 1;

Expand Down Expand Up @@ -38,6 +43,11 @@ Status getInt32(ModelInstance* comp, ValueReference vr, int32_t values[], size_t

switch (vr) {
case vr_counter:
#if FMI_VERSION == 3
if (comp->state == EventMode) {
calculateCounter(comp);
}
#endif
values[(*index)++] = M(counter);
return OK;
default:
Expand Down Expand Up @@ -79,18 +89,13 @@ Status setInt32(ModelInstance* comp, ValueReference vr, const int32_t values[],

Status eventUpdate(ModelInstance *comp) {

if (M(counter) >= 10) {
calculateCounter(comp);

if (M(counter) > 10) {
logError(comp, "Variable \"counter\" cannot be incremented for values >= 10.");
return Error;
}

const double epsilon = (1.0 + fabs(comp->time)) * DBL_EPSILON;

if (comp->nextEventTimeDefined && comp->time + epsilon >= comp->nextEventTime) {
M(counter)++;
comp->nextEventTime += 1;
}

comp->valuesOfContinuousStatesChanged = false;
comp->nominalsOfContinuousStatesChanged = false;
comp->terminateSimulation = M(counter) >= 10;
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fmusim.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ def test_event_mode_time_events(platform):
model='Stair.fmu'
)

assert np.all(result['time'] == [0, 1, 1, 2, 2, 2.5, 3, 3, 4, 4, 5, 5])
assert np.all(result['counter'] == [1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6])
assert np.all(result['time'] == [0, 1, 1, 2, 2, 2.5, 3, 3, 4, 4, 5, 5])
assert np.all(result['counter'] == [1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6])


@pytest.mark.parametrize('fmi_version, interface_type', product([2, 3], ['cs', 'me']))
Expand Down

0 comments on commit 0f4eeb8

Please sign in to comment.