From 468ef1f5bd400b6e05e68757c14fb15912afd251 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 14 Mar 2013 14:58:27 +0000 Subject: [PATCH] Store current valus from fmu to Game Experiment. This speeds up reading current values because you do not need the jni interface. (fixes #4175) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27098 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/adapter/ValueIndexVariable.java | 17 +------ .../sysdyn/adapter/ValuesIndexVariable.java | 18 ++++--- .../sysdyn/manager/SysdynGameExperiment.java | 48 ++++++++++++++++--- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java index 61e5d9f4..9ce2169b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java @@ -29,27 +29,14 @@ public class ValueIndexVariable extends IndexVariable { if(experiment == null) return new double[0]; - FMUControlJNI control = null; - if(experiment instanceof SysdynGameExperiment) { - SysdynGameExperiment exp = (SysdynGameExperiment)experiment; - ExperimentState state = exp.getSysdynExperimentState(); - // Get value from control only if the simulation is running or stopped, not before initialization - if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state)) - control = ((SysdynGameExperiment)this.experiment).getFMUControl(); - } - Collection results = experiment.getActiveResults(); ArrayList variableNames = getVariableNamesWithIndexNumbers(); double[] result = new double[variableNames.size()]; for(int i = 0; i < variableNames.size(); i++) { for(SysdynResult r : results) { - if(control != null) { - try { - result[i] = control.getRealValue(variableNames.get(i)); - } catch (FMUJNIException e) { - result[i] = 0; - } + if(experiment instanceof SysdynGameExperiment) { + result[i] = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i)); } else { SysdynDataSet ds = r.getDataSet(variableNames.get(i)); if(ds != null && ds.values != null && ds.values.length > 0) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java index cd237d2c..52218d04 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java @@ -7,7 +7,6 @@ import org.simantics.db.ReadGraph; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; -import org.simantics.fmu.FMUJNIException; import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.manager.SysdynGameExperiment; import org.simantics.sysdyn.manager.SysdynResult; @@ -34,17 +33,16 @@ public class ValuesIndexVariable extends IndexVariable { result[i][j] = ds.values[j]; } - // if this is the actual simulation result and experiment is game experiment, - // get the last value from fmu control. + /* + * If this is the actual simulation result and experiment is game experiment, + * get the last value from experiment. It might be different from the result + * file, if it has been modified in current time step + */ if(experiment instanceof SysdynGameExperiment && ds.result == null) { - try { - double d = ((SysdynGameExperiment)experiment).getFMUControl().getRealValue(variableNames.get(i)); - result[i][ds.values.length - 1] = d; - } catch (FMUJNIException e) { - e.printStackTrace(); - } + double d = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i)); + result[i][ds.values.length - 1] = d; } - + } else { result[i] = new double[0]; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java index f1058978..dd8ffe26 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java @@ -45,6 +45,7 @@ public class SysdynGameExperiment extends SysdynExperiment { private FMUControlJNI control; private String[] subscription; + private HashMap subscriptionIndexes; private HashMap> results; private double stepLength = DEFAULT_STEP_LENGTH; private double stepDuration = DEFAULT_STEP_DURATION; @@ -54,6 +55,8 @@ public class SysdynGameExperiment extends SysdynExperiment { public static double DEFAULT_STEP_LENGTH = 0.1; public static int DEFAULT_OUTPUT_INTERVAL = 1; + double[] currentValues; // Current values from FMU. Updated with updateSubscriptions + public SysdynGameExperiment(Resource experiment, Resource model) { super(experiment, model); } @@ -254,7 +257,6 @@ public class SysdynGameExperiment extends SysdynExperiment { double time = control.getTime(); double eTime = time + duration; - double[] results = new double[subscription.length]; monitor.worked(1); @@ -266,12 +268,12 @@ public class SysdynGameExperiment extends SysdynExperiment { if(stepNumber % savePer == 0) { monitor.subTask("Get results (time = " + control.getTime() + ")"); - results = control.getSubscribedResults(results); + currentValues = control.getSubscribedResults(currentValues); monitor.worked(1); monitor.subTask("Save results (time = " + control.getTime() + ")"); for(int k = 0; k < subscription.length; k++) { - SysdynGameExperiment.this.results.get(subscription[k]).add(results[k]); + SysdynGameExperiment.this.results.get(subscription[k]).add(currentValues[k]); } } else { monitor.worked(1); @@ -360,7 +362,21 @@ public class SysdynGameExperiment extends SysdynExperiment { subscription = control.getAllVariables(); else subscription = control.filterVariables(inits.get("variableFilter")); - control.subscribe(subscription); // subscribe all variables + + // Initialize subscription indexes map for fast result reading in getValue() + if(subscriptionIndexes == null) + subscriptionIndexes = new HashMap(); + + subscriptionIndexes.clear(); + for(int i = 0; i < subscription.length; i++) { + subscriptionIndexes.put(subscription[i], i); + } + + // Initialize container for current simulation results + currentValues = new double[subscription.length]; + + // subscribe all variables + control.subscribe(subscription); getInitialResultValues(); @@ -374,11 +390,10 @@ public class SysdynGameExperiment extends SysdynExperiment { // Initialize results results.clear(); - double[] initialValues = new double[subscription.length]; - initialValues = control.getSubscribedResults(initialValues); + currentValues = control.getSubscribedResults(currentValues); for(int k = 0; k < subscription.length; k++) { results.put(subscription[k], new ArrayList()); - results.get(subscription[k]).add(initialValues[k]); + results.get(subscription[k]).add(currentValues[k]); } getCurrentResult().setResult(new GameResult(this.results, this.subscription)); @@ -387,4 +402,23 @@ public class SysdynGameExperiment extends SysdynExperiment { System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage()); } } + + @Override + public void updateSubscriptions() { + try { + currentValues = control.getSubscribedResults(currentValues); + } catch (FMUJNIException e) { + e.printStackTrace(); + } + super.updateSubscriptions(); + } + + public Double getCurrentValue(String name) { + Integer index = subscriptionIndexes.get(name); + if(index != null) { + return currentValues[index]; + } else { + return null; + } + } } -- 2.47.1