From 2824c46fc07f746f8720807cc556f70bda826001 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 10 Mar 2010 13:23:36 +0000 Subject: [PATCH] Initial simulation data is gathered and added to datasets git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14783 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/modelica/ModelicaManager.java | 9 ++- .../modelica/data/SimulationResult.java | 60 +++++++++++++++---- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 0244e434..38def44a 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -61,12 +61,14 @@ public class ModelicaManager { File simulationDir; File inputFile; File outputFile; + File initFile; public SimulationLocation(File simulationDir, File inputFile, - File outputFile) { + File outputFile, File initFile) { this.simulationDir = simulationDir; this.inputFile = inputFile; this.outputFile = outputFile; + this.initFile = initFile; } } @@ -95,7 +97,8 @@ public class ModelicaManager { return new SimulationLocation( simulationDir, new File(simulationDir, modelName + ".mos"), - new File(simulationDir, modelName + "_res.plt") + new File(simulationDir, modelName + "_res.plt"), + new File(simulationDir, modelName + "_init.txt") ); } @@ -119,7 +122,7 @@ public class ModelicaManager { SimulationResult result = new SimulationResult(); result.read(simulationLocation.outputFile); - + result.readInits(simulationLocation.initFile); result.filter(); return result; diff --git a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java index 0676555b..761319fc 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -8,6 +8,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -17,7 +18,7 @@ import java.util.regex.Pattern; * @author Hannu Niemistö */ public class SimulationResult { - + List dataSets = new ArrayList(); static String getLine(InputStream stream) { @@ -41,15 +42,54 @@ public class SimulationResult { } catch (IOException e) { return null; } + + } + + + public void readInits(File file) throws FileNotFoundException { + readInits(new FileInputStream(file)); + } + + public void readInits(InputStream stream) { + HashMap mappedInitials = new HashMap(); + while(true) { + String line = getLine(stream); + if(line == null) + return; + if(line.isEmpty()) + break; + + if(line.contains("//")) { + String[] nn = line.split("//", 2); + try { + double value = Double.parseDouble(nn[0].trim()); + String key = nn[1].trim(); + mappedInitials.put(key, value); + } catch (NumberFormatException nfe) { + continue; // Not a valid double + } + + } + + } + double startTime = mappedInitials.get("start value"); + double stopTime = mappedInitials.get("stop value"); + double[] times = {startTime, stopTime}; + + for(String key : mappedInitials.keySet()) { + double[] values = {mappedInitials.get(key), mappedInitials.get(key)}; + dataSets.add(new DataSet(key, times , values)); + } } - + + final static Pattern p1 = Pattern.compile("DataSet: ([^ ]*)"); - + public void read(File file) throws FileNotFoundException { read(new FileInputStream(file)); } - + public void read(InputStream stream) { while(true) { String line = getLine(stream); @@ -58,14 +98,14 @@ public class SimulationResult { if(line.isEmpty()) break; } - + // Data sets while(true) { Matcher matcher = p1.matcher(getLine(stream)); if(!matcher.matches()) return; String name = matcher.group(1); - + TDoubleArrayList times = new TDoubleArrayList(); TDoubleArrayList values = new TDoubleArrayList(); while(true) { @@ -78,11 +118,11 @@ public class SimulationResult { times.add(Double.parseDouble(nn[0])); values.add(Double.parseDouble(nn[1])); } - + dataSets.add(new DataSet(name, times.toNativeArray(), values.toNativeArray())); } } - + public void filter() { ArrayList newDataSets = new ArrayList(); for(DataSet dataSet : dataSets) { @@ -91,11 +131,11 @@ public class SimulationResult { } dataSets = newDataSets; } - + public List getDataSets() { return dataSets; } - + public DataSet getDataSet(String name) { for(DataSet set : dataSets) if(set.name.equals(name)) -- 2.47.1