From: miettinen Date: Mon, 15 Oct 2012 07:23:14 +0000 (+0000) Subject: Changed sysdyn result reader to buffered reader. Removed line counting of result... X-Git-Tag: simantics-1.10.1~126 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=cebae5726b29adaec7d4208b09b0a1745e9db79a;p=simantics%2Fsysdyn.git Changed sysdyn result reader to buffered reader. Removed line counting of result file for .mat files. (refs #3699) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26007 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 a21a2155..03325da6 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.modelica.data; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -45,13 +46,14 @@ import org.xml.sax.SAXException; * * Will be replaced with a common OpenModelica plugin * @author Hannu Niemistö + * @author Tuomas Miettinen */ -public class SimulationResult { +public class SimulationResult { protected List variables = new ArrayList(); protected List initials = new ArrayList(); protected int numberOfLines = 0; - + /** * Private class used in displaying errors */ @@ -83,7 +85,7 @@ public class SimulationResult { stream = null; return b.toString(); } - else if(c == '\n') + else if(c == '\n') return b.toString(); else if(c == '\r') ; @@ -92,7 +94,7 @@ public class SimulationResult { } } catch (IOException e) { return null; - } + } } @@ -102,13 +104,13 @@ public class SimulationResult { * Supports both txt and xml type inits. * * @param file inits-file - * @throws FileNotFoundException + * @throws FileNotFoundException * @throws IOException */ public void readInits(File file) throws FileNotFoundException, IOException { if(file.getName().endsWith("txt")) { - InputStream is = new FileInputStream(file); + InputStream is = openStream(file); readInitsTXT(is); is.close(); } else if(file.getName().endsWith("xml")) { @@ -134,24 +136,24 @@ public class SimulationResult { // Find start time node = (Node)xpath.evaluate ("//fmiModelDescription/DefaultExperiment/@startTime", - doc, + doc, XPathConstants.NODE); times[0] = Double.parseDouble(node.getNodeValue()); // Find end time node = (Node)xpath.evaluate ("//fmiModelDescription/DefaultExperiment/@stopTime", - doc, + doc, XPathConstants.NODE); times[1] = Double.parseDouble(node.getNodeValue()); - + NodeList nodes; - + // Find parameters and constants nodes = (NodeList)xpath.evaluate ("//fmiModelDescription/ModelVariables/ScalarVariable[@variability='parameter']", - doc, + doc, XPathConstants.NODESET); - + // Add parameters and constants to initials for (int idx = 0; idx < nodes.getLength(); idx++) { Node n = nodes.item(idx); @@ -159,13 +161,13 @@ public class SimulationResult { addToInitials(name, n, times); } - + // Find all alias variables NodeList allAliasVariables = (NodeList)xpath.evaluate ("//fmiModelDescription/ModelVariables/ScalarVariable[@alias='alias']", - doc, + doc, XPathConstants.NODESET); - + for(int i = 0; i < allAliasVariables.getLength(); i++) { Node n = allAliasVariables.item(i); String name = n.getAttributes().getNamedItem("name").getNodeValue(); @@ -175,13 +177,13 @@ public class SimulationResult { String aliasVariableName = aliasVariableProperty.getNodeValue(); n = (Node)xpath.evaluate ("//fmiModelDescription/ModelVariables/ScalarVariable[@name='" + aliasVariableName +"' and @variability='parameter']", - doc, + doc, XPathConstants.NODE); // Add the value of the original value to initials with the name of the alias variable if(n != null) addToInitials(name, n, times); } - + } catch (SAXException e) { e.printStackTrace(); @@ -195,10 +197,10 @@ public class SimulationResult { e.printStackTrace(); } } - + /** - * Adds a new dataset to initials using start name, value from - * node and times. + * Adds a new dataset to initials using start name, value from + * node and times. * * @param name Name for the dataset to be added to initials * @param node Node containing the start value attribute @@ -215,8 +217,8 @@ public class SimulationResult { Double d = Double.parseDouble(child.getAttributes().getNamedItem("start").getNodeValue()); values = new double[] {d,d}; initials.add( - new DataSet(name, - times , + new DataSet(name, + times , values)); break; } @@ -262,16 +264,16 @@ public class SimulationResult { } - + /** * Read result file. Read all values in the result file. - * + * * @param file result file * @throws FileNotFoundException * @throws IOException */ public void read(File file) throws FileNotFoundException, IOException { - read(file, 1); + read(file, 1); } @@ -284,24 +286,31 @@ public class SimulationResult { * @throws IOException */ public void read(File file, int interval) throws FileNotFoundException, IOException { - // First check the number of time steps - FileReader fr = new FileReader(file); - LineNumberReader lnr = new LineNumberReader(fr); - lnr.skip(Long.MAX_VALUE); - numberOfLines = lnr.getLineNumber() - 1; // minus the first row, which is for names - lnr.close(); - fr.close(); - - InputStream is = new FileInputStream(file); + // First check the number of time steps. + // With *.mat result file this is not necessary + if (!(this instanceof MatSimulationResult)) { + FileReader fr = new FileReader(file); + LineNumberReader lnr = new LineNumberReader(fr); + lnr.skip(Long.MAX_VALUE); + numberOfLines = lnr.getLineNumber() - 1; // minus the first row, which is for names + lnr.close(); + fr.close(); + } + InputStream is = openStream(file); + //InputStream is = new FileInputStream(file); read(is, interval); is.close(); } + private InputStream openStream(File file) throws FileNotFoundException { + return new BufferedInputStream(new FileInputStream(file)); + } + final static Pattern p1 = Pattern.compile("DataSet: ([^ ]*)"); /** * Read result file. The basic implementation supports - * plt-formatted results. Overridden to support other formats. + * plt-formatted results. Overridden to support other formats. * @param stream FileInputStream for the result file * @param interval the interval of results to be read. 1 reads all time steps, 2 reads every other time step, etc... */ @@ -315,12 +324,11 @@ public class SimulationResult { } // Data sets - while(true) { + while(true) { Matcher matcher = p1.matcher(getLine(stream)); if(!matcher.matches()) return; String name = matcher.group(1); - double[] dtimes = new double[numberOfLines]; double[] dvalues = new double[numberOfLines]; int i = 0; @@ -369,7 +377,7 @@ public class SimulationResult { } /** - * Gets DataSet for variable. Loops first the variables and then initials. + * Gets DataSet for variable. Loops first the variables and then initials. * @param name the name of the variable * @return DataSet for the variable or null if DataSet not found */ @@ -377,10 +385,10 @@ public class SimulationResult { for(DataSet set : variables) if(set.name.equals(name)) return set; - for(DataSet set : initials) - if(set.name.equals(name)) - return set; - return null; + for(DataSet set : initials) + if(set.name.equals(name)) + return set; + return null; }