Skip to content

Commit

Permalink
Fix zero-crossing detection in CS FMUs
Browse files Browse the repository at this point in the history
fixes #233
  • Loading branch information
t-sommer committed Apr 8, 2022
1 parent 2ddad46 commit 55a94b6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions BouncingBall/model.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <math.h> // for fabs()
#include <float.h> // for DBL_MIN
#include "config.h"
#include "model.h"

Expand Down Expand Up @@ -115,7 +116,7 @@ void eventUpdate(ModelInstance *comp) {

if (M(h) <= 0 && M(v) < 0) {

M(h) = 0;
M(h) = DBL_MIN; // slightly above 0 to avoid zero-crossing
M(v) = -M(v) * M(e);

if (M(v) < V_MIN) {
Expand All @@ -124,12 +125,10 @@ void eventUpdate(ModelInstance *comp) {
M(g) = 0;
}

comp->valuesOfContinuousStatesChanged = true;
}
// reset previous event indicators
getEventIndicators(comp, comp->z, NZ);

// reset the previous event indicators
for (int i = 0; i < NZ; i++) {
comp->z[i] = 0;
comp->valuesOfContinuousStatesChanged = true;
}

comp->nominalsOfContinuousStatesChanged = false;
Expand Down
2 changes: 1 addition & 1 deletion src/cosimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ void doFixedStep(ModelInstance *comp, bool* stateEvent, bool* timeEvent) {

// check for zero-crossings
for (int i = 0; i < NZ; i++) {
*stateEvent |= comp->z[i] * z[i] < 0;
*stateEvent |= (comp->z[i] <= 0 && z[i] > 0) || (comp->z[i] > 0 && z[i] <= 0);
}

// remember the current event indicators
Expand Down

0 comments on commit 55a94b6

Please sign in to comment.