From 2183c5af27a74187f2b1f0044ac090028dd5ab3a Mon Sep 17 00:00:00 2001 From: luukkainen Date: Wed, 4 Jul 2012 12:31:28 +0000 Subject: [PATCH] fixes #3544 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25275 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.modelica/META-INF/MANIFEST.MF | 3 +- .../modelica/reader/MatFileReader.java | 293 ++++++++++++++++++ .../modelica/reader/ResultFileReader.java | 11 + 3 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java create mode 100644 org.simantics.modelica/src/org/simantics/modelica/reader/ResultFileReader.java diff --git a/org.simantics.modelica/META-INF/MANIFEST.MF b/org.simantics.modelica/META-INF/MANIFEST.MF index e9bb53c4..a3c07a6a 100644 --- a/org.simantics.modelica/META-INF/MANIFEST.MF +++ b/org.simantics.modelica/META-INF/MANIFEST.MF @@ -12,7 +12,8 @@ Require-Bundle: gnu.trove2;bundle-version="2.0.4", org.simantics.utils;bundle-version="1.1.0" Export-Package: org.simantics.modelica, org.simantics.modelica.data, - org.simantics.modelica.fmi + org.simantics.modelica.fmi, + org.simantics.modelica.reader Bundle-Activator: org.simantics.modelica.Activator Bundle-ActivationPolicy: lazy Bundle-Vendor: VTT Technical Research Centre of Finland diff --git a/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java b/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java new file mode 100644 index 00000000..314d1ae8 --- /dev/null +++ b/org.simantics.modelica/src/org/simantics/modelica/reader/MatFileReader.java @@ -0,0 +1,293 @@ +package org.simantics.modelica.reader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.simantics.modelica.data.DoubleMatrix; +import org.simantics.modelica.data.IntMatrix; +import org.simantics.modelica.data.Matrix; +import org.simantics.modelica.data.StringMatrix; + +public class MatFileReader implements ResultFileReader { + private File file; + + private List names = new ArrayList(); + private Map indices = new HashMap(); + + + int readBytes = 0; // tracking read data + int dataMark = 0; // mark for actual data + + IntMatrix info; + int[] infoData; + + MatrixHeader dataHeader; + + public MatFileReader(File file) throws IOException { + this.file = file; + readVariables(); + } + + public List getNames() { + return names; + } + + public double[] readData(String item) throws IOException { + readBytes = 0; + Integer index = indices.get(item); + if (index == null) + throw new IOException("Unknown item: " + item); + InputStream in = new FileInputStream(file); + skip(dataMark, in); + + Object data = readColumn(dataHeader, index, in); + + in.close(); + + return (double[])data; + + } + + + private void readVariables() throws IOException { + InputStream in = new FileInputStream(file); + readMatrix(in); // Header + StringMatrix names = (StringMatrix)readMatrix(in); // Variable names + readMatrix(in); // Variable descriptions + + for (int i = 0; i < names.data.length; i++) { + String s = names.data[i]; + this.names.add(s); + indices.put(s, i); + } + + + + info = (IntMatrix)readMatrix(in); // Data info + + if(info.rows != 4 || info.columns != names.rows ) + throw new IOException("Invalid result data."); + + infoData = info.data; + + readMatrix(in); // Some result data matrix? + + dataHeader = new MatrixHeader(in); + + dataMark = readBytes; + + in.close(); + + // filter incompatible? data. + for (int i = this.names.size() -1; i >= 0; i--) { + if (infoData[i * 4] != 2) { + String name = this.names.remove(i); + this.indices.remove(name); + } + + } + + Collections.sort(this.names); + + } + + private final int getInt(InputStream in) throws IOException { + int ch1 = in.read(); + int ch2 = in.read(); + int ch3 = in.read(); + int ch4 = in.read(); + readBytes += 4; + return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24)); + } + + private final long getLong(InputStream in) throws IOException { + int ch1 = in.read(); + int ch2 = in.read(); + int ch3 = in.read(); + int ch4 = in.read(); + int ch5 = in.read(); + int ch6 = in.read(); + int ch7 = in.read(); + int ch8 = in.read(); + readBytes += 8; + return ((((long)ch1) << 0) + (((long)ch2) << 8) + (((long)ch3) << 16) + (((long)ch4) << 24) + + (((long)ch5) << 32) + (((long)ch6) << 40) + (((long)ch7) << 48) + (((long)ch8) << 56)); + } + + private final double getDouble(InputStream in) throws IOException { + return Double.longBitsToDouble(getLong(in)); + } + + private String getString(InputStream in,int length) throws IOException { + byte[] buffer = new byte[length]; + int pos = 0; + while(pos < length) + pos += in.read(buffer, pos, length-pos); + readBytes += length; + return new String(buffer, "UTF-8"); + } + + private Matrix readMatrix(InputStream in) throws IOException { + + + int type = getInt(in); + int rows = getInt(in); + int columns = getInt(in); + int imagf = getInt(in); + int namlen = getInt(in); + + String name = getString(in,namlen-1); + in.read(); + readBytes++; + + + + if(imagf > 0) + throw new IOException("Imaginary part of the matrix is not supported (matrix " + name + ")."); + + switch(type) { + case 0: { + DoubleMatrix matrix = new DoubleMatrix(name, rows, columns); + int size = rows*columns; + double[] data = matrix.data; + for(int i=0;i 0 ? sc-1 : 1-sc; + + if (fast) { + + skip(c * 8, in); + for (int j = 0; j < v.length; ++j) { + + double d = getDouble(in); + v[j] = d; + skip((rows-1)*8,in); + } + + } else { + DoubleMatrix values = (DoubleMatrix)readMatrix(header, in); + + double[] valueData = values.data; + + + for(int j=0;j 0) + throw new IOException("Imaginary part of the matrix is not supported (matrix " + name + ")."); + + } + } +} diff --git a/org.simantics.modelica/src/org/simantics/modelica/reader/ResultFileReader.java b/org.simantics.modelica/src/org/simantics/modelica/reader/ResultFileReader.java new file mode 100644 index 00000000..008074da --- /dev/null +++ b/org.simantics.modelica/src/org/simantics/modelica/reader/ResultFileReader.java @@ -0,0 +1,11 @@ +package org.simantics.modelica.reader; + +import java.io.IOException; +import java.util.List; + +public interface ResultFileReader { + + public List getNames(); + public double[] readData(String item) throws IOException ; + +} -- 2.47.1