Kemp’s Blog

A technical blog about technical things

OpenModelica vs Dymola: connect

In general, I target Modelica simulation at OpenModelica for the simple reason that it’s free for me to run anywhere. Having had to retarget one of the simulations at Dymola, I encountered a few differences. This post gives a quick overview of one such difference in using the connect function.

I had a situation where I needed to use an existing model and operate on its inputs and outputs. For the purpose of this discussion assume there is a model Example with output out. In OpenModelica I’m allowed to do the following:

model UseExample
  Example e;
  Real localvar1;
  equation
    connect(e.out, localvar1);
    // The rest of the model here
end UseExample;

Now, technically, this may very well be against the Modelica language specification. OpenModelica allows it though, and it is something that I used to make my life a little easier. Dymola, on the other hand, is not so loose around this particular rule and throws an error when compiling the model.

The solution for following the language specification correctly is to have separate models for performing calculations and for connecting models together. For example:

model DoSomething
  input Modelica.Blocks.Interfaces.RealInput in;
  Real localvar1;
  equation
    // The rest of the model here
end DoSomething;

model UseExample
  Example e;
  DoSomething d;
  equation
    connect(e.out, d.in);
end UseExample;

Note that this really is a better way of doing things from a modularity and maintainability point of view.