OpenModelica / OMMatlab

Matlab scripting OpenModelica interface
12 stars 4 forks source link

Error using OMMatlab #3

Closed adrcarrui closed 6 years ago

adrcarrui commented 6 years ago

Hi,

I'm trying to execute the commands import OMMatlab.*" and "omc=OMMatlab() but I got errors:

Error using OMMatlab
Error: File: OMMatlab.m Line:14 Column: 22
The input character is not valid in MATLAB statements or expressions.

I did all the installation:

1- I wrote pathtool on the Matlab Command Window. A window appears, I added the directory where I have the OMMatlab folder: "C:\Downloads\OMMatlab"

2- Then I wrote prefdir. I created the "javaclasspath.txt" file in the location that prefdir showed me. Inside the file I wrote "C:\Downloads\OMMatlab\jeromq-0.4.4-SNAPSHOT.jar"

3- I tried to execute the commands like import OMMatlab.* and omc=OMMatlab() but I always get the same error (I wrote at the beginning). I though about if I need to put the OMMatlab folder into a specific location. I tried to change it to "C:\OpenModelica1.12.0-64bit\OMMatlab\jeromq-0.4.4-SNAPSHOT.jar" into the 1 and 2 installation steps, but it doesn't work.

I opened the OMMatlab.m file in Matlab and I saw some ",I changed it to ' but it doesn´t work. Am I missing something?

Thanks in advance.

arun3688 commented 6 years ago

which matlab version you are having

adrcarrui commented 6 years ago

Matlab 2015

arun3688 commented 6 years ago

i think there are some problems in concatenation of char and strings in matlab earlier versions, I tested in matlab 2016 and 2017 it is working fine .I can get back to you tomorrow

adrcarrui commented 6 years ago

Ok, I'll try it tomorrow in the morning and I'll post here the results. Thanks for the fast answer!

adrcarrui commented 6 years ago

The error changed to:

Error using system
Argument must contain a character vector.

Error in OMMatlab (line 24)
                      system(cmd);

I'm using Matlab2017 and OpenModelica1.13.0. Moreover I'm working on Windows. You said you tested in Matlab2016 and 2017 but which OS?

arun3688 commented 6 years ago

i am using windows, have you set OPENMODELICAHOME in your environment variable

adrcarrui commented 6 years ago

My OPENMODELICAHOME is "C:\OpenModelica1.13.0-dev-64bit" and the OMMatlab folder is "C:\Users\GEPOC-PC3\Documents\OMEdit\OMMatlab"

UPDATED: I just upload a picture about my enviroment variables.

enviroment varibles

adrcarrui commented 6 years ago

I tried to move the OMMatlab folder to "C:\OpenModelica1.13.0-dev-64bit" and do all the installation process again to change the path. It doesn't work.

arun3688 commented 6 years ago

by the way did you change this line 14. cmd ="START /b "+omhomepath +" --interactive=zmq +z=matlab."+randomstring; if not can you just add this line after line 14, disp(cmd) and check what is the output it gives

adrcarrui commented 6 years ago

I put it right after line 14 but it gave me the same error that before, so I changed it to line 24.

classdef OMMatlab < handle
    properties
        context
        requester
        portfile
        fileid
    end
    methods
        function obj = OMMatlab()
            randomstring = char(97 + floor(26 .* rand(10,1)))';
            if ispc
                omhome = getenv('OPENMODELICAHOME');
                omhomepath = replace(fullfile(omhome,'bin','omc.exe'),'\','/');
                cmd ="START /b "+omhomepath +" --interactive=zmq +z=matlab."+randomstring;
                portfile = strcat('openmodelica.port.matlab.',randomstring);
            else
                if ismac && system("which omc") ~= 0
                  cmd ="/opt/openmodelica/bin/omc --interactive=zmq -z=matlab."+randomstring+" &";
                else
                  cmd ="omc --interactive=zmq -z=matlab."+randomstring+" &";
                end
                portfile = strcat('openmodelica.',getenv('USER'),'.port.matlab.',randomstring);
            end
            disp(cmd)
            system(cmd);
            pause(0.2);
            import org.zeromq.*
            obj.context=ZMQ.context(1);
            obj.requester =obj.context.socket(ZMQ.REQ);
            obj.portfile=replace(fullfile(tempdir,portfile),'\','/');
            obj.fileid=fileread(obj.portfile);
            obj.requester.connect(obj.fileid);
        end
        function reply = sendExpression(obj,expr)
            obj.requester.send(expr,0);
            reply=obj.requester.recvStr(0);
        end
        function delete(obj)
            delete(obj.portfile);
            obj.requester.close();
            delete(obj);
        end
    end
end

The answer is the following:

START /b C:/OpenModelica1.13.0-dev-64bit/bin/omc.exe --interactive=zmq +z=matlab.jvpoxhttjo

arun3688 commented 6 years ago

can you just run this in your command prompt and see

C:/OpenModelica1.13.0-dev-64bit/bin/omc.exe --interactive=zmq

adrcarrui commented 6 years ago
Created ZeroMQ Server.
Dumped server port in file: C:\\Users\\GEPOC-~1\\AppData\\Local\\Temp\\/openmodelica.port
arun3688 commented 6 years ago

ok the omc server is working, can you try to run this from the matlab terminal and see whether the omc process is started

system("C:/OpenModelica1.13.0-dev-64bit/bin/omc.exe --interactive=zmq +z=matlab.jvpoxhttjo&")

adrcarrui commented 6 years ago

github

I just copied and pasted what you told me, but I don't know what happen.

arun3688 commented 6 years ago

instead can you use single quotes instead of double like this

system('C:/OpenModelica1.13.0-dev-64bit/bin/omc.exe --interactive=zmq +z=matlab.jvpoxhttjo&')

adrcarrui commented 6 years ago

The command prompt appears: cp

And in matlab appears ans=0

arun3688 commented 6 years ago

It Works, ok i understood the problem is with the char and string representation and i am still not sure about this, I am using MatlabR2017b version, I try to find a solution , which matlab 2017 you are using

adrcarrui commented 6 years ago

Thanks! I'll be waiting for the answer. Awesome attention!

UPDATE: I'm using MatlabR2017a version.

arun3688 commented 6 years ago

hi again i hope this solution should work can you comment the line 14

% cmd ="START /b "+omhomepath +" --interactive=zmq +z=matlab."+randomstring; and add this line and test it and see

cmd = ['START /b',' ',omhomepath,' --interactive=zmq +z=matlab.',randomstring];

adrcarrui commented 6 years ago

github2

When I write import OMMatlab.* it should load something into Matlab right?

arun3688 commented 6 years ago

ok the old error is fixed, now the problem is finding the ZMQ library, have you changed the path of the .jar files in the javaclasspath.txt file in the prefdir, I hope you changed the location of jar file and did not update

adrcarrui commented 6 years ago

In the javaclasspath.txt the path have to be with double quotes or without them?

arun3688 commented 6 years ago

without double quotes, for reference on how to set the static java classed, see this https://se.mathworks.com/help/matlab/matlab_external/static-path.html

adrcarrui commented 6 years ago

I changed it, but still getting the same error.

arun3688 commented 6 years ago

Ok, can you type from the matlab terminal

 >>> which classpath.txt
C:\Program Files\MATLAB\R2017b\toolbox\local\classpath.txt

it will show the path where the file exists and try to open the file in admin mode and add the jar file path to the end with quotes

adrcarrui commented 6 years ago

github3

I added it. Should I write like the others .jar? Because I tried again and still doesn't work.

arun3688 commented 6 years ago

Yes, but without any quotes (example: C:/OPENMODELICAGIT/OpenModelica/OMMatlab/jeromq-0.4.4-SNAPSHOT.jar) add the path with Backward slash ('/') and restart the matlab

adrcarrui commented 6 years ago

github4

I think is working now. I tried omc=sendExpression("getVersion()") and I got no errors.

arun3688 commented 6 years ago

Great, good luck. I will soon update the readme file

arun3688 commented 6 years ago

@adrcarrui regarding setting the jar file location in prefdir using javaclasspath.txt, It seems that the path should be added to the file without any quotes either single or double, just the plaintextpath and then it must work

adrcarrui commented 6 years ago

Yeah, I have it without quotes. However I am trying to execute some commands and I don't get any answer.

I wrote: import OMMatlab.* omc = OMMatlab() omc.sendExpression("getVersion()") omc.sendExpression("C:\Users\GEPOC-PC3\Documents\OMEdit\producto.mo") omc.sendExpression("simulate(producto)")

In this point, I simulated the model producto, but I want to see the results into Matlab, so I executed omc.sendExpression("readSimulationResult()"). I saw the function into the API, I realized I have to tell the function a filename. So I tried omc.senExpression("readSimulationResult(producto_res.mat)") and omc.senExpression("readSimulationResult(producto)") and other choices. I couldn't get the result from the simulation. What am I doing wrong?

arun3688 commented 6 years ago

To get the results from the mat file you should use the API something like this

omc.sendExpression('readSimulationResult("C:/OPENMODELICAGIT/OpenModelica/OMMatlab/test/BouncingBall_res.mat",{h,impact})')

see https://build.openmodelica.org/Documentation/OpenModelica.Scripting.html

adrcarrui commented 6 years ago

github5

I'm getting this error. Is the hyphen a problem in the path?

UPDATE:

I wrote the command wrong. Now, omc.sendExpression('readSimulationResult("C:\Users\GEPOC-PC3\Documents\OMEdit\OMMatlab\producto_res.mat",{m_flow_nominal, hea.Qflow})') but I'm getting fail()

arun3688 commented 6 years ago

you should use double quotes to the filename "producto_res.mat", something like this omc.sendExpression('readSimulationResult("C:/OPENMODELICAGIT/OpenModelica/OMMatlab/test/BouncingBall_res.mat",{h,impact})')

adrcarrui commented 6 years ago

Before writing into the _res.mat file I should close it with closeSimulationResult() I want to write into _res.mat file to change some values and simulate again. I wrote omc.sendExpression('writeFile("C:\Users\GEPOC-PC3\Documents\OMEdit\OMMatlab\producto_res.mat",{m_flow_nominal=10})')

But I got this error:

Error occurred building AST
Syntax Error
[<interactive>:1:10-1:14:writable] Warning: Lexer treating \ as \\, since \U is not a valid Modelica escape sequence.
[<interactive>:1:10-1:20:writable] Warning: Lexer treating \ as \\, since \G is not a valid Modelica escape sequence.
[<interactive>:1:10-1:30:writable] Warning: Lexer treating \ as \\, since \D is not a valid Modelica escape sequence.
[<interactive>:1:10-1:40:writable] Warning: Lexer treating \ as \\, since \O is not a valid Modelica escape sequence.
[<interactive>:1:10-1:47:writable] Warning: Lexer treating \ as \\, since \O is not a valid Modelica escape sequence.
[<interactive>:1:10-1:56:writable] Warning: Lexer treating \ as \\, since \p is not a valid Modelica escape sequence.
[<interactive>:1:0-1:8:writable] Error: Parser error: Unexpected token near: writeFile (IDENT)
arun3688 commented 6 years ago

Yes, you should close the resultfile, But you cannot do it like this, the ones which you are trying to do are some advanced options and they cannot be done like this, look into this for examples https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html?highlight=override#simulation-parameter-sweep

adrpo commented 6 years ago

You cannot use writeFile for Matlab mat files. writeFile takes a text file and a string that needs to write into file. Maybe you can use some Matlab functions for that. For the file path, use forward slash / instead of \, or double it \\.

hkiel commented 6 years ago

I have a matlab function that reads in the generated mat file and creates variables with the name of the signals.

adrcarrui commented 6 years ago

I'm dealing with that right now. It is like hell to understand the order T.T. It could be fantastic if you could share your code!

UPDATE: I didn't want to close the issue, but I think is better to close this one and, if necesary, open another one for functions and more.

adrcarrui commented 6 years ago

Hi guys!

I'm working on my own file to read the generated mat file. I load the mat like this: a=load('BouncingBall_res.mat');

I got it that a.name is the name of the variables, a.description are the comments which explain a bit the variables and a.data_1 contains the values of the parameters in the startTime and stopTime.

I'm trying to understand the content of a.data_2 and a.dataInfo. I looked it into the scripting api that was written in another comment, but I didn't find an explanation about the structure of a.

Moreover, I have another question. When I'll be able to read and understand the content of BouncingBall_res.mat, I'll want to change it so could I change the _res.mat and then execute the simulate command? Something like loading the matrix into matlab change it and then simulate? (I don't know when the _res.mat file is created or updated, so I don't know if I should compile again or not)

or I have to do something like this:

https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/scripting_api.html?highlight=override#simulation-parameter-sweep

Thanks!

UPDATE: I understood the content of a.data_2 but I'm still missing with a.dataInfo

sjoelund commented 6 years ago

I added some details on the MATv4 format that will become part of the documentation soon. You can see it here until then: https://github.com/OpenModelica/OpenModelica-doc/blob/master/UsersGuide/source/technical_details.rst

adrcarrui commented 6 years ago

I'll close the issue. I'll be trying to do some stuff with OMMatlab if I have other issues I'll open a new one.

Thanks!!

gptshubham595 commented 3 years ago

what should I do I am still getting that error ZMQ one please HELP WINDOWS10 MATLAB R2020a OpenModelica All steps done as stated above

PLEASE HELP

image

gptshubham595 commented 3 years ago

cool that worked god knows why it was not working before!! Hope it will help others to see!! 😆

Closing and reopening Matlab worked