Opened 7 years ago

Closed 2 years ago

#4210 closed defect (fixed)

Modelica.Utilities.Streams.readRealMatrix not working

Reported by: anonymous Owned by: somebody
Priority: normal Milestone: 1.19.0
Component: Run-time Version: v1.10.0-dev-nightly
Keywords: Cc:

Description

The function Modelica.Utilities.Streams.readRealMatrix raises an error when used in the example Modelica.Utilities.Examples.ReadRealMatrixFromFile from standard Modelica, and fails to build when used in a minimal example.

Error from example in standard Modelica:

build/openmodelica-WGhRBY/openmodelica-1.10.0~dev-710-g833bf4b/OMCompiler/Compiler/SimCode/SimCodeUtil.mo:537:5-537:146:writable Error: Internal error function createSimCode failed [Transformation from optimised DAE to simulation code structure failed]

Output when used in a minimal example:

$ omc run_mos.mos
true
true
""
record SimulationResult
resultFile = "",
simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'ReadRealMatrixFromFile', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
messages = "Failed to build model: ReadRealMatrixFromFile",
timeFrontend = 0.0,
timeBackend = 0.0,
timeSimCode = 0.0,
timeTemplates = 0.0,
timeCompile = 0.0,
timeSimulation = 0.0,
timeTotal = 0.0
end SimulationResult;
"[/usr/lib/omlibrary/Modelica 3.2.2/Utilities/Streams.mo:210:5-210:61:readonly] Warning: Unused input variable nrow in function .Modelica.Utilities.Streams.readRealMatrix.
[/usr/lib/omlibrary/Modelica 3.2.2/Utilities/Streams.mo:211:5-211:64:readonly] Warning: Unused input variable ncol in function .Modelica.Utilities.Streams.readRealMatrix.
[/tmp/asd/readRealMatrixFromFile_min.mo:6:5-6:116:writable] Warning: Failed to deduce dimensions of A due to unknown dimensions of modifier Modelica.Utilities.Streams.readRealMatrix(file, matrixName, dim[1], dim[2], true)[1].
"

The first 2 warnings occur because in Modelica.Utilities.Streams.readRealMatrix the dimensions inputs (nrow and ncol) are not used in the call to the external function.

function readRealMatrix "Read Real matrix from MATLAB MAT file"
extends Modelica.Icons.Function;
input String fileName "File where external data is stored" annotation(Dialog(loadSelector(filter="MATLAB MAT files (*.mat)", caption="Open MATLAB MAT file")));
input String matrixName "Name / identifier of the 2D Real array on the file";
input Integer nrow "Number of rows of the 2D Real array";
input Integer ncol "Number of columns of the 2D Real array";
input Boolean verboseRead = true
"= true: Print info message; = false: No info message";
output Real matrix[nrow, ncol] "2D Real array";
external "C" ModelicaIO_readRealMatrix(fileName, matrixName, matrix, size(matrix, 1), size(matrix, 2), verboseRead)
annotation(...)
annotation(...);
end readRealMatrix;

The example from standard Modelica was tested from omc and OMEdit. The minimal example was tested only in omc.

Platform and OpenModelica version:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:        14.04
Codename:       trusty
$ uname -a
Linux <user>-desktop 3.13.0-49-generic #81-Ubuntu SMP Tue Mar 24 19:29:48 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ omc --version
OpenModelica 1.10.0~dev-700-gcaa32ec
}}}''

Attachments (2)

run_mos.mos (194 bytes) - added by anonymous 7 years ago.
readRealMatrixFromFile_min.mo (671 bytes) - added by anonymous 7 years ago.

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by anonymous

Changed 7 years ago by anonymous

comment:1 Changed 6 years ago by peter.harman@…

I'm finding with the following example (I've tried the matrix as either a parameter or a discrete variable set at initialisation) I get no errors, but the matrix is all zeros (which it shouldn't be):

model TestReadMatrix
parameter String filename = "aMatFile.mat";
parameter String tablename = "table";
parameter Integer[2] tableSize=Modelica.Utilities.Streams.readMatrixSize(filename, tablename);
  Real[tableSize[1], tableSize[2]] data(start=zeros(tableSize[1], tableSize[2]));
equation
when initial() then
  data = Modelica.Utilities.Streams.readRealMatrix(
        filename,
        tablename,
        tableSize[1],
        tableSize[2]);
        end when;
end TestReadMatrix;

Is there a workaround for this or should I write my own function to read external files?

comment:2 Changed 6 years ago by sjoelund.se

comment:1 seems similar to:

model TestReadMatrix
parameter String filename = Modelica.Utilities.Files.loadResource("modelica://Modelica/Resources/Data/Utilities/Test_RealMatrix_v4.mat");
parameter String tablename = "Matrix_A";
parameter Integer[2] tableSize=Modelica.Utilities.Streams.readMatrixSize(filename, tablename);
  Real[tableSize[1], tableSize[2]] data(start=zeros(tableSize[1], tableSize[2]));
equation
when initial() then
  data = Modelica.Utilities.Streams.readRealMatrix(
        filename,
        tablename,
        tableSize[1],
        tableSize[2]);
        end when;
end TestReadMatrix;

This either gives:

Error: Could not evaluate structural parameter (or constant): tableSize[1] which gives dimensions of array: data[tableSize[1]]. Array dimensions must be known at compile time.

Or when running with -d=gen (in order to evaluate unknown external functions):

marsj@marsj-Precision-5510:~/tmp$ ./TestReadMatrix -output data[1,1],data[1,2],data[2,1],data[2,2]
stdout            | info    | ... loading "Matrix_A" from "/home/marsj/OpenModelica/build/lib/omlibrary/Modelica 3.2.2/Resources/Data/Utilities/Test_RealMatrix_v4.mat"
LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
time=1,data[1,1]=11,data[1,2]=12,data[2,1]=21,data[2,2]=22
LOG_SUCCESS       | info    | The simulation finished successfully.

Most probably the ticket can be resolved by having a special case to evaluate the call at compile-time.

comment:3 Changed 6 years ago by sjoelund.se

Possibly we could implement something like http://www.dyncall.org to handle all functions already linked with OMC.

comment:4 Changed 2 years ago by perost

  • Milestone changed from Future to 1.19.0
  • Resolution set to fixed
  • Status changed from new to closed

This has been working for a while now, see simulation results from our automatic testing.

Note: See TracTickets for help on using tickets.