]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Initial simulation data is gathered and added to datasets
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 10 Mar 2010 13:23:36 +0000 (13:23 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 10 Mar 2010 13:23:36 +0000 (13:23 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14783 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java

index 0244e434959181f4c1186d02ca4113158b6f99a8..38def44abcb0dd42a666a088f6f148de0e3ce095 100644 (file)
@@ -61,12 +61,14 @@ public class ModelicaManager {
         File simulationDir;\r
         File inputFile;\r
         File outputFile;\r
+        File initFile;\r
         \r
         public SimulationLocation(File simulationDir, File inputFile,\r
-                File outputFile) {\r
+                File outputFile, File initFile) {\r
             this.simulationDir = simulationDir;\r
             this.inputFile = inputFile;\r
             this.outputFile = outputFile;\r
+            this.initFile = initFile;\r
         }\r
     }\r
     \r
@@ -95,7 +97,8 @@ public class ModelicaManager {
         return new SimulationLocation(\r
                 simulationDir,\r
                 new File(simulationDir, modelName + ".mos"),\r
-                new File(simulationDir, modelName + "_res.plt")\r
+                new File(simulationDir, modelName + "_res.plt"),\r
+                new File(simulationDir, modelName + "_init.txt")\r
                 );\r
     }\r
     \r
@@ -119,7 +122,7 @@ public class ModelicaManager {
             \r
             SimulationResult result = new SimulationResult();\r
             result.read(simulationLocation.outputFile);\r
-            \r
+            result.readInits(simulationLocation.initFile);\r
             result.filter(); \r
             \r
             return result;\r
index 0676555b2d8b71d200ff5648fbf187a16590bc9e..761319fcd3fda8dfda086a86fd4638881c725838 100644 (file)
@@ -8,6 +8,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;\r
 import java.io.InputStream;\r
 import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
@@ -17,7 +18,7 @@ import java.util.regex.Pattern;
  * @author Hannu Niemistö\r
  */\r
 public class SimulationResult {    \r
-    \r
+\r
     List<DataSet> dataSets = new ArrayList<DataSet>();\r
 \r
     static String getLine(InputStream stream) {\r
@@ -41,15 +42,54 @@ public class SimulationResult {
         } catch (IOException e) {\r
             return null;\r
         }           \r
+\r
+    }\r
+\r
+\r
+    public void readInits(File file) throws FileNotFoundException {\r
+        readInits(new FileInputStream(file));\r
+    }\r
+\r
+    public void readInits(InputStream stream) {\r
+        HashMap<String, Double> mappedInitials = new HashMap<String, Double>();\r
+        while(true) {\r
+            String line = getLine(stream);\r
+            if(line == null)\r
+                return;\r
+            if(line.isEmpty())\r
+                break;\r
+\r
+            if(line.contains("//")) {\r
+                String[] nn = line.split("//", 2);\r
+                try {\r
+                    double value = Double.parseDouble(nn[0].trim());\r
+                    String key = nn[1].trim();\r
+                    mappedInitials.put(key, value);\r
+                } catch (NumberFormatException nfe) {\r
+                    continue; // Not a valid double\r
+                }\r
+                \r
+            }\r
+\r
+        }\r
         \r
+        double startTime = mappedInitials.get("start value");\r
+        double stopTime = mappedInitials.get("stop value");\r
+        double[] times = {startTime, stopTime};\r
+        \r
+        for(String key : mappedInitials.keySet()) {\r
+            double[] values = {mappedInitials.get(key), mappedInitials.get(key)};\r
+            dataSets.add(new DataSet(key, times , values));\r
+        }\r
     }\r
-    \r
+\r
+\r
     final static Pattern p1 = Pattern.compile("DataSet: ([^ ]*)");\r
-    \r
+\r
     public void read(File file) throws FileNotFoundException {\r
         read(new FileInputStream(file));\r
     }\r
-    \r
+\r
     public void read(InputStream stream) {\r
         while(true) {\r
             String line = getLine(stream);\r
@@ -58,14 +98,14 @@ public class SimulationResult {
             if(line.isEmpty())\r
                 break;\r
         }\r
-        \r
+\r
         // Data sets\r
         while(true) {            \r
             Matcher matcher = p1.matcher(getLine(stream));\r
             if(!matcher.matches())\r
                 return;\r
             String name = matcher.group(1);\r
-            \r
+\r
             TDoubleArrayList times = new TDoubleArrayList();\r
             TDoubleArrayList values = new TDoubleArrayList();\r
             while(true) {\r
@@ -78,11 +118,11 @@ public class SimulationResult {
                 times.add(Double.parseDouble(nn[0]));\r
                 values.add(Double.parseDouble(nn[1]));\r
             }\r
-            \r
+\r
             dataSets.add(new DataSet(name, times.toNativeArray(), values.toNativeArray()));\r
         }\r
     }\r
-        \r
+\r
     public void filter() {\r
         ArrayList<DataSet> newDataSets = new ArrayList<DataSet>();\r
         for(DataSet dataSet : dataSets) {\r
@@ -91,11 +131,11 @@ public class SimulationResult {
         }\r
         dataSets = newDataSets;\r
     }\r
-    \r
+\r
     public List<DataSet> getDataSets() {\r
         return dataSets;\r
     }\r
-    \r
+\r
     public DataSet getDataSet(String name) {\r
         for(DataSet set : dataSets)\r
             if(set.name.equals(name))\r