]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Changed the Modelica simulation result format from .plt to .csv. With a complex and...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 May 2011 06:43:16 +0000 (06:43 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 May 2011 06:43:16 +0000 (06:43 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20732 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.modelica/src/org/simantics/modelica/data/CSVSimulationResult.java [new file with mode: 0644]
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java

index f8e4b18e91772591f6a01fd860fe72aca7bc42b7..ee9e4738f5894f1d34854dbf97e1194e8a0823c3 100644 (file)
@@ -243,7 +243,7 @@ public class ModelicaManager {
                modelName = modelName.replace(" ", "");\r
                File modelFile = new File(simulationDir, modelName + ".mo");\r
                File scriptFile = new File(simulationDir, modelName + ".mos");\r
-\r
+               String outputFormat = inits.containsKey("outputFormat") ? inits.get("outputFormat") : "\"plt\"";\r
                {\r
                        PrintStream s = new PrintStream(modelFile);\r
                        s.print(modelText);\r
@@ -259,7 +259,7 @@ public class ModelicaManager {
                                        ",startTime="+inits.get("start value")+\r
                                        ",stopTime="+inits.get("stop value")+\r
                                        ",method="+inits.get("method")+\r
-                                       ",outputFormat=\"plt\""\r
+                                       ",outputFormat="+ outputFormat\r
                        );\r
                        if(inits.containsKey("tolerance")) {\r
                                s.print(",tolerance="+inits.get("tolerance"));\r
@@ -275,7 +275,7 @@ 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." + outputFormat.substring(1, outputFormat.length()-1)),\r
                                new File(simulationDir, modelName + "_init.txt"),\r
                                new File(simulationDir, modelName + suffix)\r
                );\r
diff --git a/org.simantics.modelica/src/org/simantics/modelica/data/CSVSimulationResult.java b/org.simantics.modelica/src/org/simantics/modelica/data/CSVSimulationResult.java
new file mode 100644 (file)
index 0000000..49abb2b
--- /dev/null
@@ -0,0 +1,55 @@
+package org.simantics.modelica.data;\r
+\r
+import java.io.InputStream;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+\r
+\r
+public class CSVSimulationResult extends SimulationResult {\r
+\r
+       HashMap<String, List<Double>> valueMap = new HashMap<String, List<Double>>();\r
+       \r
+       @Override\r
+    public void read(InputStream stream) {\r
+               \r
+               // First line contains the variable names in format "name" (including quotes);\r
+               String line = getLine(stream);\r
+               if(line == null)\r
+                       return;\r
+               \r
+               // FIXME: yks elementti liikaa alusta.\r
+               line = line.substring(1, line.lastIndexOf("\""));\r
+               String[] names = line.split("\",\"");\r
+               \r
+               // Create lists for receiving values for each variable. Names still with quotes.\r
+               for(String name : names) {\r
+                       if(!name.isEmpty())\r
+                               valueMap.put(name, new ArrayList<Double>());\r
+               }\r
+\r
+        // Data sets\r
+               while((line = getLine(stream)) != null) {      \r
+            if(line.isEmpty())\r
+                break;\r
+                       String[] values = line.split(",");\r
+                       for(int i = 0; i<values.length; i++) {\r
+                               if(values[i] != null && !values[i].isEmpty() && i < names.length) {\r
+                                       try {\r
+                                               valueMap.get(names[i]).add(Double.parseDouble(values[i]));\r
+                                       } catch (NumberFormatException e) {\r
+                                               e.printStackTrace();\r
+                                               break;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+        \r
+        // Create datasets for variables. Substring quotes from variable names.\r
+        for(String key : valueMap.keySet()) {\r
+               DataSet ds = new DataSet(key, valueMap.get("time"), valueMap.get(key));\r
+               variables.add(ds);\r
+        }\r
+    }\r
+    \r
+}\r
index 094d29a590bd625260649ef5433930d89e2c4ce8..ed8941b4b31f3a3387a8bcefa9efbede522e8373 100644 (file)
@@ -37,6 +37,7 @@ import org.simantics.modelica.IModelicaMonitor;
 import org.simantics.modelica.ModelicaException;\r
 import org.simantics.modelica.ModelicaManager;\r
 import org.simantics.modelica.SimulationLocation;\r
+import org.simantics.modelica.data.CSVSimulationResult;\r
 import org.simantics.modelica.data.SimulationResult;\r
 import org.simantics.objmap.IMapping;\r
 import org.simantics.objmap.IMappingListener;\r
@@ -172,6 +173,8 @@ public class SysdynModel implements IMappingListener, IModel {
                Double numberOfIntervals = model.getOutputInterval();\r
                inits.put("start value", startTime.toString());\r
                inits.put("stop value", stopTime.toString());\r
+               String outputFormat = "\"csv\"";\r
+               inits.put("outputFormat", outputFormat);\r
                if(numberOfIntervals != null) {\r
                        inits.put("step value", numberOfIntervals.toString());\r
                } else {\r
@@ -213,6 +216,7 @@ public class SysdynModel implements IMappingListener, IModel {
                                previousImportantInits.put("start value", startTime.toString());\r
                                previousImportantInits.put("stop value", stopTime.toString());\r
                                previousImportantInits.put("method", method);\r
+                               previousImportantInits.put("outputFormat", outputFormat);\r
                        } catch (ModelicaException e) {\r
                                if(e.getMessage() != null)\r
                                        monitor.message(e.getMessage());\r
@@ -242,13 +246,12 @@ public class SysdynModel implements IMappingListener, IModel {
                                                if(!canceled) {\r
                                                        progressMonitor.worked(1);\r
                                                        progressMonitor.subTask("Read results");\r
-                                                       result = new SimulationResult();\r
+                                                       result = new CSVSimulationResult();\r
                                                        result.read(simulationLocation.outputFile);\r
                                                        result.readInits(simulationLocation.initFile);\r
                                                        result.filter();\r
                                                        sysdynResult.setResult(result);\r
                                                        progressMonitor.worked(1);\r
-                                                       \r
                                                        resultChanged();\r
                                                        \r
                                                        setExperimentStopped(experiment);\r