How to use openmodelica compiler in CLI? - modelica

I am trying to use OpenModelica compiler(omc) in the CLI, so I use the Terminalprovided in OMEdit.
I tried to debug Modelica.Blocks.Examples.PID_Controller, but it seems omc couldn't find this model.
So I duplicated the PID_Controller model and put into a folder, then switched working directory in the CLI. At this time, omc could access the model, but it couldn't find the base model of Modelica.Icon.Example, I am guessing that omc doesn't have access to the Modelica Standard Library(MSL) loaded in the OMEdit.
My question is how to let omc load the MSL correctly.

-d are debug flags used in general by the developers
If you want to use omc from command line the easiest is to use .mos scripts:
The script: c:\writable\directory\script.mos
loadModel(Modelica); getErrorString(); // load the Modelica Standard Library (MSL)
simulate(Modelica.Blocks.Examples.PID_Controller); getErrorString(); // simulate a model
plotAll(); getErroString(); // plot all variables
Then from the command line, go to a directory where you can generate some output:
cd c:\writable\directory\
omc script.mos
See all the available API (the commands you can put in a mos script) here:
https://build.openmodelica.org/Documentation/OpenModelica.Scripting.html

Related

Simulink RSIM target does not load new solver options

I'm stuck up using RSim target options.
I've created an .exe from a Simulink model "my_model" using RSim target.
Now my aim is to run this .exe and control the solver options using a .mat file.
I am trying to run the following command on CMD:
my_model.exe -i input.mat -tf 10 -o output.mat -S solver_options.mat
But the output.mat file of the model does not correspond to the sampling time I've provided in sover_options.mat.
I am following the inputs arguments provided by RSim, as shown below:
It seems that RSim is not reading new solver_options parameters.
My solver_options.mat file is provided below:
Could anyone help me, please?

Want to activate a virtual environment from terminal with Octave/Matlab

I would like to execute a bash command to activate a virtual environment with Octave using Linux. What I actually want to do is run DeepSpeech using Octave/Matlab.
The command I want to use is
source $HOME/tmp/deepspeech-venv/bin/activate
The line of code I tried on my own is system("source $HOME/tmp/deepspeech-venv/bin/activate")
And the output I'm getting is sh: 1: source: not found
I saw this answer on a post and tried this command setenv('PATH', ['/source $HOME/tmp/deepspeech-venv/bin/activate', pathsep, getenv('PATH')]) but with no help it returned the same error.
It's not completely clear from your question, but I'm assuming you're trying to do is run python commands within octave/matlab, and you'd like to use a python virtual environment for that.
Unfortunately, when you run a system command from within octave, what most likely happens is that this creates a subshell to execute your command, which is discarded once the command has finished.
You have several options to rectify this, but I think the easiest one would be to activate the python virtual environment first, and run your octave instance from within that environment. This then inherits all environmental variables as they existed when octave was run. You can confirm this by doing getenv( 'VIRTUAL_ENV' ).
If that's not an option, then you could make sure that all system commands intended to run python scripts, are prefixed with a call to the virtual environment first (e.g. something like system( 'source ./my/venv/activate; python3 ./myscript.py') ).
Alternatively, you can try to recreate the virtual environment from its exported variables manually, using the setenv command.

How can I execute a simulink diagram running in external mode from a Matlab script?

Matlab provides the sim command that can be used in a Matlab script to call and execute a Simulink model. But the function seems to be restricted to models that only run in Normal mode. When I try to call a Simulink model that runs in External mode, Matlab halts the script and flags it as an error. Besides being a nuisance it seems to me an unnecessary restriction on what could be a very useful application.
In any event is there a work around, perhaps a different command that I can use to run the Simulink diagram in External mode from the Matlab script?
I did try using the Matlab DOS shell command (using !) but it requires opening another instance of Matlab.
External Mode doesn't run a simulation, rather it is a mechanism for using the Simulink model's front end as a way of changing and viewing data that is running elsewhere (e.g. an executable running on the same machine as the model, or code running on an external processor).
To do that from the command line (or within code) you need to use a combination of the following commands:
>> set_param(gcs,'SimulationMode','external') % put model into External Mode
>> set_param(gcs,'SimulationCommand','connect') % connect to the executable
>> set_param(gcs,'SimulationCommand','start') % Start the executable
>> set_param(gcs,'SimulationCommand','stop') % Stop the executable

Running a C++ program from an m.file

For a project I am working on I'm preparing data in MATLAB, then running the data through a separate external application (written in C++) named Model v2.exe, then performing further analysis with the output in MATLAB. I'm trying to create an M-file which does all of this, but I am struggling to get the C++ program to launch from my MATLAB code.
How can I launch an external application from within my MATLAB code?
You can either make use of the ! operator, or the system() command.
First, rename you application to something that has no spaces in the name, like modelv2.exe. Next, either make sure it is in the system path, as defined by your system environment variables, or generate a full path to it (eg: C:\Users\Phil\Desktop\modelv2.exe).
You can call an executable program from the command line using the exclamation point or the system command:
!modelv2
or:
!C:\Users\Phil\Desktop\modelv2.exe
will cause Windows to execute the program hello.exe if there is such a file in the current directory or on the system path. Alternatively:
system('modelv2');
or
system('C:\Users\Phil\Desktop\modelv2.exe');
will do the same thing.
References
"MATLAB Central - call and run an external program in matlab under windows", Accessed 2014-03-19, <http://www.mathworks.com/matlabcentral/answers/11568-call-and-run-an-external-program-in-matlab-under-windows>

`GLIBCXX_3.4.11' not found, run system call from MATLAB that links to glibc different than what's in matlab bin path

I'm trying to circumvent using MEX to link to MATLAB and just call a binary using "!" as in:
>> !template_image_rigid -args ....
template_image_rigid: /opt/MatlabR2010a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libboost_program_options.so.1.40.0)
template_image_rigid: /opt/MatlabR2010a/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/lib/libdirac_encoder.so.0)
Is there a way to easily fix this dynamic link issue from within MATLAB? I know I can recompile the source with MATLAB and use a MEX call, but since it takes a while to run the solver I just want to run it as shell command and import text data later into MATLAB.
If it helps, the source & CMakeLists.txt can be found here: https://github.com/pkarasev3/nlmagick/tree/master/samples
Grr, community = fail.
Diagnoising: do !gnome-terminal from within matlab and look at "env":
env | grep Matlab
which gives
XKEYSYMDB=/opt/MatlabR2010a/X11/app-defaults/XKeysymDB
MATLABPATH=/opt/MatlabR2010a/toolbox/local
XAPPLRESDIR=/opt/MatlabR2010a/X11/app-defaults
LD_LIBRARY_PATH=/opt/MatlabR2010a/sys/os/glnxa64:/opt/MatlabR2010a/bin/glnxa64:/opt/MatlabR2010a/extern/lib/glnxa64:/opt/MatlabR2010a/runtime/glnxa64:/opt/MatlabR2010a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/opt/MatlabR2010a/sys/java/jre/glnxa64/jre/lib/a md64/server:/opt/MatlabR2010a/sys/java/jre/glnxa64/jre/lib/amd64
OSG_LD_LIBRARY_PATH=/opt/MatlabR2010a/sys/openscenegraph/lib/glnxa64
TOOLBOX=/opt/MatlabR2010a/toolbox
XFILESEARCHPATH=/opt/MatlabR2010a/sys/java/jre/glnxa64/jre/lib/locale/%L/%T/%N%S::/usr/dt/app-defaults/%L/Dt
MATLAB=/opt/MatlabR2010a
Ok so the LD_LIBRARY_PATH is bad.
Trick: write a poltergeist script and run it from gnome-terminal, Launch it from Matlab with:
!./hack.sh RunStuffThatLinksElsewhere
where hack.sh is a script with something like:
#!/bin/bash
source ~/.bashrc
export LD_LIBRARY_PATH=''
gnome-terminal --command="${1}"
so an easy test is to try it with "eog", this hack gets around the link issue and lets you run it from within matlab...
Simpler:
setenv('foo',num2str(some_value) )
!LD_LIBRARY_PATH="" && ./my_binary -f $foo
disp('done with external program!')
I solved this problem by replacing the version of libstdc++.so.6 with a newer version from my system (I use ubuntu 12.04).
First find the system version of libstdc++.so.6.
From the command line type:
locate libstdc++.so.6
My system version of libstdc was
/usr/lib/i386-linux-gnu/libstdc++.so.6
Then replace the matlab libstdc version with a link to the system libstdc.
From the command line type (replace [....] with you settings):
cd [matlab_dir]/sys/os/glnx86
mv libstdc++.so.6 libstdc++.so.6-OLD
ln -s [your_system_version_of_libstdc] libstdc++.so.6
I recently ran into the same problem. My solution also uses a poltergeist script like other answers. The script is as follows (poltergeist.sh):
#!/bin/bash
export LD_LIBRARY_PATH=''
eval "$#"
It basically resets the library path and subsequently evaluates the call given by the arguments to the script. From within matlab I then call in this manner:
system([pwd,'/poltergeist.sh echo hello world!']);
The advantage to this approach is that you can dynamically modify the call command within matlab. As far as I know this is not possible using the bang syntax in the currently provided answers.