# How to do co-simulation with two FMU models

I have two FMU developed with different simulation tools. I would like to connect them through some variables and run a co-simulation with an open source tool as OpenModelica. This co-simulation has to be rather tight as both FMU have discontinuities.

Is that possible?. Could you provide a simple sample?

• What is an FMU? – Geoff Oxberry Jun 21 '13 at 9:28
• Functional Mock-up Interface is a standard to interface simulations made in different tools (Symulink, Dymola, AMESim, ...) in the same co-simulation. – Koldo Jun 21 '13 at 9:32
• I've added a tag for FMI with an explanation (still pending review). – Florian Brucker Jun 21 '13 at 9:35

# Model Exchange vs. Co-Simulation

This depends on how you export your FMUs: You can either use FMI for model-exchange or FMI for co-simulation.

In the model-exchange scenario, the FMU contains only the model and no solver. Therefore the solver of the importing simulator is used.

In the co-simulation scenario, the FMU contains both the model and a solver. Here the importing simulator acts as the master of the co-simulation.

In both cases you will need a simulator that supports the import of the respective FMU type. OpenModelica has support for import of model-exchange FMUs since version 1.8.0. Hence, if you have two FMUs that you have exported using FMI for model-exchange, you can import them both into OpenModelica and simulate them using the OpenModelica solver. Note that strictly speaking, this isn't a real co-simulation since only one solver is involved.

If you want to use a separate solver for each FMU (i.e. perform a real co-simulation) then you should only export one of the models as an FMU using FMI for co-simulation. Import that FMU into the simulator of the other model. Note that OpenModelica 1.8.0 doesn't support FMI for co-simulation.

# Model Exchange Example

Here's a working example (tested in Dymola, I don't have OpenModelica installed here right now):

File TestFMU1.mo:

model TestFMU1
parameter Real p = 1.0;

connector TestOutputConnector
output Real value;
end TestOutputConnector;
TestOutputConnector c;

equation
c.value = p;

end TestFMU1;


File TestFMU2.mo:

model TestFMU2
Real result;

connector TestInputConnector
input Real value;
end TestInputConnector;
TestInputConnector c;

equation
result = c.value;

end TestFMU2;


Export both of these as FMUs. Then import them and combine them as follows:

model TestConnection
TestFMU1_fmu OutputFMU;
TestFMU2_fmu InputFMU;
equation
connect(OutputFMU.c_value, InputFMU.c_value);
end TestConnection;


# OpenModelica

It seems that OpenModelica currently does not support simulating two FMUs simultaneously. This is a known bug.

• The problem is how to link two models in Modelica language. As both FMU have the same number of equations and unknowns, adding an additional equation to put, lets say, Force_FMU_1 = Force_FMU_2, would add an additional equation so it would be overdetermined. – Koldo Jun 21 '13 at 10:28
• FMI only supports directed in- and outputs. Undirected ports, like the standard ports in Modelica, are currently not supported. In addition, OpenModelica cannot "see" the equations used to build your FMUs. Hence there is no problem connecting the outputs of one FMU with the inputs of the other. – Florian Brucker Jun 21 '13 at 11:23
• Thank you Florian. Could you put a simple sample?. For example, this do not run because it is overdetermined: model Master FMU_Model1_me_FMU model1; // Loaded from FMU_Model1_me_FMU.mo FMU_Model2_me_FMU model2; // Loaded from FMU_Model2_me_FMU.mo equation model1.out = model2.in; end Master; – Koldo Jun 22 '13 at 6:26
• @Koldo: See my update. – Florian Brucker Jun 24 '13 at 15:13
• Excellent!. Thank you Florian. That is what I wanted!. Unfortunately it does not work in OpenModelica. The generated .exe for TestConnection gets crashed. I will report them. – Koldo Jun 25 '13 at 6:39