Devon
Devon

Reputation: 21

Modelica and Matlab Coupling with Modelica Error

I am trying to couple the Matlab with Modelica.

The overall process is: I have 5000 sets of data and each of them contains 20 parameters which correspond to my Modelica model.

  1. These data sets would read by Matlab and then send to Modelica.
  2. Modelica would proceed with the simulation and generate the result.
  3. Then, Matlab will catch the result from Modelica.

However, since the value of the parameter is generated randomly, errors could occur during the simulation (e.g., temperature out of boundary, convergence error, and etc).

I thought about "try-catch" to execute the simulation if the error occurs. However, I realized that Matlab would stop if an error exists from Modelica. I am wondering if there is a way to continue the "for loop" in the Matlab in order to get into the next input data set.

Thanks

Upvotes: 1

Views: 459

Answers (2)

Atiyah Elsheikh
Atiyah Elsheikh

Reputation: 578

Based on Dymola mfiles which provide a kind of low-level way of running and processing simulations, I have implemented an open-source software with more high-level functions with which it is easier to modify simulations parameters and to process the results.

Here is the link with a small tutorial given here.

Particularly interesting in this software would be the feature of running a two-threaded timed simulation. When modifying parameter values the simulation could be very slow, e.g. because of stiffness. It is possible to run a timed simulation for a specific given duration where the simulation is killed afterwards with a failure signal. In my case using older versions of Dymola (~2008-2014) I was able to run multiple simulations with few of them "getting killed". I am not sure about how it will run with the neuest Dymola versions.

The software, though individually implemented in an adhoc and quick way within an academic rather than a commercial environment, was so far useful for my purposes. Also, it is currently not maintained. Nevertheless I hope it could be useful probably in the presence of some hacking capabilities.

Upvotes: 0

Luigi Vanfretti
Luigi Vanfretti

Reputation: 31

There are several things you can do.

  1. Using the Dymola-Matlab tools under the installation directory as mentioned above.

To add to the above, in the installation folder, e.g., ./Program Files/Dymola 2020x/Mfiles, if you open the "Contents.m" file in that folder, you will get all the information on how to execute simulations using "dymosin" from MATLAB. The content file also points to other functions to send commands to Dymola, read and manipulate trajectories, etc.

  1. Export your Dymola model as an FMU into MATLAB.

Before doing this you need to define inputs and outputs using real input and real output connectors. Then, to export the model as an FMU, you can go to the Simulation menu, select the Translation Tab drop down, and choose the FMU option, or do Ctrl+F9.

From experience, I recommend you use the Co-Simulation option, unless you really need to use Matlab or Simulink solver options.

To run your simulations you have multiple options, I use the following:

2.1. Import the model into MATLAB using commands, or an FMU ME or FMU CS block from the FMI Toolbox from Modelon. This is a very reliable tool, I've been using it since 2012 in RaPId, and it has become very robust. Also, it comes with a great number of extra functions for model analysis and simulation automation that come in really handy.

2.2. Import the model into an FMU using FMIKit. I have been using FMIKit lately and I've found it very robust. I have only tested importing FMUs into Simulink, don't know if you can avoid that. Then you can use MATLAB commands to call the Simulink model holding the FMU.

2.3. Import the model using the built in FMU block under the Simulink/Extras library, and then use MATLAB commands to call the Simulink model as above. This feature did not come until 2017, so it is kind of unpredictable.

If you like the FMU option, and you don't have any "strong" dependencies, I would strongly recommend that you avoid MATLAB and move to Python. I wish I had done this 15 years ago... Dymola has a the Dymola-Python interface under ./Program Files/Dymola 2020x/Modelica/Library/python_interface, and OpenModelica has a good Python interface also. Here are some notebooks from one of our papers to get you started (link).

Upvotes: 3

Related Questions