Skip to content

Commit

Permalink
Calculate counter in getInt32()
Browse files Browse the repository at this point in the history
fixes #554
  • Loading branch information
t-sommer committed Oct 9, 2024
1 parent fda2fd1 commit 478d5f5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 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,7 @@ Status getInt32(ModelInstance* comp, ValueReference vr, int32_t values[], size_t

switch (vr) {
case vr_counter:
calculateCounter(comp);
values[(*index)++] = M(counter);
return OK;
default:
Expand Down Expand Up @@ -79,18 +85,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

0 comments on commit 478d5f5

Please sign in to comment.