]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Store current valus from fmu to Game Experiment. This speeds up reading current value...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 14 Mar 2013 14:58:27 +0000 (14:58 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 14 Mar 2013 14:58:27 +0000 (14:58 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27098 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java

index 61e5d9f414cc0c28f96aee95c5a9a44e77fc3278..9ce2169bd735ba0445cd7f36b2f7d5babee95c14 100644 (file)
@@ -29,27 +29,14 @@ public class ValueIndexVariable extends IndexVariable<double[]> {
            if(experiment == null)\r
                return new double[0];\r
 \r
-           FMUControlJNI control = null;\r
-           if(experiment instanceof SysdynGameExperiment) {\r
-               SysdynGameExperiment exp = (SysdynGameExperiment)experiment;\r
-               ExperimentState state = exp.getSysdynExperimentState();\r
-               // Get value from control only if the simulation is running or stopped, not before initialization\r
-               if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state))\r
-                   control = ((SysdynGameExperiment)this.experiment).getFMUControl();\r
-               }\r
-\r
         Collection<SysdynResult> results = experiment.getActiveResults();\r
                ArrayList<String> variableNames = getVariableNamesWithIndexNumbers();\r
 \r
                double[] result = new double[variableNames.size()];\r
                for(int i = 0; i < variableNames.size(); i++) {\r
                    for(SysdynResult r : results) {            \r
-                       if(control != null) {\r
-                           try {\r
-                               result[i] = control.getRealValue(variableNames.get(i));\r
-                           } catch (FMUJNIException e) {\r
-                               result[i] = 0;\r
-                           }\r
+                       if(experiment instanceof SysdynGameExperiment) {\r
+                           result[i] = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i));\r
                        } else {\r
                            SysdynDataSet ds = r.getDataSet(variableNames.get(i));\r
                            if(ds != null && ds.values != null && ds.values.length > 0) {\r
index cd237d2c44e992d8223698367532642dfc73afd5..52218d04757e4e18ac50c8f9101b972a9f2cdd9e 100644 (file)
@@ -7,7 +7,6 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.fmu.FMUJNIException;\r
 import org.simantics.sysdyn.manager.SysdynDataSet;\r
 import org.simantics.sysdyn.manager.SysdynGameExperiment;\r
 import org.simantics.sysdyn.manager.SysdynResult;\r
@@ -34,17 +33,16 @@ public class ValuesIndexVariable extends IndexVariable<double[][]> {
                         result[i][j] = ds.values[j];\r
                     }\r
                     \r
-                    // if this is the actual simulation result and experiment is game experiment, \r
-                    // get the last value from fmu control.\r
+                    /*\r
+                     * If this is the actual simulation result and experiment is game experiment, \r
+                     * get the last value from experiment. It might be different from the result\r
+                     * file, if it has been modified in current time step \r
+                     */\r
                     if(experiment instanceof SysdynGameExperiment && ds.result == null) {\r
-                        try {\r
-                            double d = ((SysdynGameExperiment)experiment).getFMUControl().getRealValue(variableNames.get(i));\r
-                            result[i][ds.values.length - 1] = d;\r
-                        } catch (FMUJNIException e) {\r
-                            e.printStackTrace();\r
-                        }\r
+                        double d = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i));\r
+                        result[i][ds.values.length - 1] = d;\r
                     }\r
-                    \r
+\r
                 } else {\r
                     result[i] = new double[0];\r
                 }\r
index f105897817f32608df12318c313deda5139d1fec..dd8ffe26f74f7c58d73608bc4bc85c07bf6bdf2f 100644 (file)
@@ -45,6 +45,7 @@ public class SysdynGameExperiment extends SysdynExperiment {
 \r
        private FMUControlJNI control;\r
        private String[] subscription;\r
+       private HashMap<String, Integer> subscriptionIndexes;\r
        private HashMap<String, ArrayList<Double>> results;\r
        private double stepLength = DEFAULT_STEP_LENGTH;\r
        private double stepDuration = DEFAULT_STEP_DURATION;\r
@@ -54,6 +55,8 @@ public class SysdynGameExperiment extends SysdynExperiment {
        public static double DEFAULT_STEP_LENGTH        = 0.1;\r
        public static int DEFAULT_OUTPUT_INTERVAL       = 1;\r
        \r
+       double[] currentValues; // Current values from FMU. Updated with updateSubscriptions\r
+       \r
        public SysdynGameExperiment(Resource experiment, Resource model) {\r
                super(experiment, model);\r
        }\r
@@ -254,7 +257,6 @@ public class SysdynGameExperiment extends SysdynExperiment {
 \r
                                double time = control.getTime();\r
                                double eTime = time + duration;\r
-                               double[] results = new double[subscription.length];\r
                                \r
                                monitor.worked(1);\r
                                \r
@@ -266,12 +268,12 @@ public class SysdynGameExperiment extends SysdynExperiment {
                                        \r
                                        if(stepNumber % savePer == 0) {\r
                                                monitor.subTask("Get results (time = " + control.getTime() + ")");\r
-                                               results = control.getSubscribedResults(results);\r
+                                               currentValues = control.getSubscribedResults(currentValues);\r
                                                monitor.worked(1);\r
 \r
                                                monitor.subTask("Save results (time = " + control.getTime() + ")");\r
                                                for(int k = 0; k < subscription.length; k++) {\r
-                                                       SysdynGameExperiment.this.results.get(subscription[k]).add(results[k]);\r
+                                                       SysdynGameExperiment.this.results.get(subscription[k]).add(currentValues[k]);\r
                                                }\r
                                        } else {\r
                                                monitor.worked(1);\r
@@ -360,7 +362,21 @@ public class SysdynGameExperiment extends SysdynExperiment {
                 subscription = control.getAllVariables();\r
             else\r
                 subscription = control.filterVariables(inits.get("variableFilter"));\r
-            control.subscribe(subscription); // subscribe all variables\r
+            \r
+            // Initialize subscription indexes map for fast result reading in getValue()\r
+            if(subscriptionIndexes == null)\r
+                subscriptionIndexes = new HashMap<String, Integer>();\r
+            \r
+            subscriptionIndexes.clear();\r
+            for(int i = 0; i < subscription.length; i++) {\r
+                subscriptionIndexes.put(subscription[i], i);\r
+            }\r
+\r
+            // Initialize container for current simulation results\r
+            currentValues = new double[subscription.length];\r
+            \r
+            // subscribe all variables\r
+            control.subscribe(subscription); \r
             \r
             getInitialResultValues();\r
             \r
@@ -374,11 +390,10 @@ public class SysdynGameExperiment extends SysdynExperiment {
             // Initialize results\r
             results.clear();\r
 \r
-            double[] initialValues = new double[subscription.length];\r
-            initialValues = control.getSubscribedResults(initialValues);\r
+            currentValues = control.getSubscribedResults(currentValues);\r
             for(int k = 0; k < subscription.length; k++) {\r
                 results.put(subscription[k], new ArrayList<Double>());\r
-                results.get(subscription[k]).add(initialValues[k]);\r
+                results.get(subscription[k]).add(currentValues[k]);\r
             }\r
 \r
             getCurrentResult().setResult(new GameResult(this.results, this.subscription));\r
@@ -387,4 +402,23 @@ public class SysdynGameExperiment extends SysdynExperiment {
             System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage());\r
         }\r
     }\r
+    \r
+    @Override\r
+    public void updateSubscriptions() {\r
+        try {\r
+            currentValues = control.getSubscribedResults(currentValues);\r
+        } catch (FMUJNIException e) {\r
+            e.printStackTrace();\r
+        }\r
+        super.updateSubscriptions();\r
+    }\r
+    \r
+    public Double getCurrentValue(String name) {\r
+        Integer index = subscriptionIndexes.get(name);\r
+        if(index != null) {\r
+            return currentValues[index];\r
+        } else {\r
+            return null;\r
+        }\r
+    }\r
  }\r