From 3001e8f718cffe96f1b3dfc9c32f1f31836cc34c Mon Sep 17 00:00:00 2001 From: jkauttio Date: Fri, 24 Apr 2015 10:35:30 +0000 Subject: [PATCH] Add an epsilon to simulation result filtering fixes #5785 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31206 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../src/org/simantics/modelica/data/DataSet.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java b/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java index ff5b332b..86ff325d 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java @@ -39,7 +39,7 @@ public class DataSet { * @param timeStep */ public void filter(double startTime, double stopTime, double timeStep) { - // TODO: this makes implicit assumptions about the values, probably not kosher + // construct a new time array with the "expected" time steps int size = (int)Math.round((stopTime - startTime) / timeStep) + 1; if (times.length < size) { // if the original data has less time steps than requested, there is no need @@ -51,16 +51,21 @@ public class DataSet { double[] newTimes = new double[size]; double[] newValues = new double[size]; - // simply skip all simulation steps that are not at the expected intervals + // generate a new value array from the original value array by walking + // through it and including only time steps that occur at the expected + // intervals (with an appropriate epsilon which is based on the + // simulation step) + double epsilon = timeStep / 1000; int old = 0; for (int i = 0; i < size; i++) { newTimes[i] = startTime + i * timeStep; - while (times[old] < newTimes[i] && old != times.length - 1) { + while (times[old] < newTimes[i] - epsilon && old != times.length - 1) { old++; } newValues[i] = values[old]; } + // replace the original arrays with the new ones times = newTimes; values = newValues; } -- 2.47.1