-1

I had some success in deploying my machine learning model (already trained) in a simulation environment (OpenModelica, in this context) via an external C-function.

However, to standardise the process, I am aiming to use the FMI standard instead of the external C-function. Therefore I need to wrap my ML model as an FMU.

Is there any way that I can do that? I read about PyFMI, however, it seems that it only controls the Co-simulation in a Python environment, instead of wrapping your ML model as an FMU.

The goal is to produce FMU from a trained ML model, and then deploy this FMU in a simulation environment (OpenModelica, for example). Any help will be very much appreciated.

Thanks

philgun
  • 149
  • 8
  • It's a bit hard to figure out what you are trying to do. Can you provide a minimal working example? I'm not sure what part you want to wrap in an FMU. You can't wrap only a external function call as an FMU. The FMI standard is used to wrap whole simulation models (set of ordinary differential equations) in some standardized way. Checkout the overview of the spec https://github.com/modelica/fmi-standard/releases/download/v2.0.2/FMI-Specification-2.0.2.pdf – AnHeuermann Aug 30 '21 at 11:17
  • Hi An, since yesterday, my question has grown, so I will try to explain it. I have 2 component models written in Modelica. The state of A is affected by the state of B. B receives Real inputs (scalars), these inputs are used to calculate the state of B. To do so, within B, an external C function calls a pre-trained neural network, and propagates the Real inputs to calculate state of B. Instead of using external C call, is it possible to replace the whole component B (with a neural net inside) with an FMU? – philgun Aug 31 '21 at 12:08
  • You can replace model A and B with FMUs since they are complete models. FMU B can have external function calls, it doesn't matter where they are coming from as long as they are contained inside the FMU. But it doesn't make sense to create a FMU for one external function. – AnHeuermann Sep 01 '21 at 10:49

1 Answers1

0

Let's say you have two Modelica models A and B.

model A
 output Real x (start=1,fixed=true);
equation
  der(x) = 2*x;
end A;
model B
  input Real x;
  Real y;
  
  function myExternalFunction
    input Real x;
    output Real y;
    external "C" annotation(Library="myExternalLib");
  end myExternalFunction;

equation
  y = myExternalFunction(x);
end B;

where B is using some external function myExternalFunction from myExternalLib.dll. This could be a function using your trained network to predict some y from some value x.

External functions in Modelica are standardized, see the Modelica specification 3.4, Section 12.9.

Now you can of course connect these models in a new Modelica model

model connectedAB
  A a;
  B b;
equation
  a.x = b.x;
end connectedAB;

Now to FMUs. The Functional Mock-up Interface is a standard that defines a container to exchange dynamic models.

There are different tools available, some can export a dynamic model as FMU (e.g. OpenModelica, Dymola,...) and some can import FMUs to simulate them (e.g. PyFMI, OMSimulator). Of course some tools can do both.

So models A and B could be exported as FMUs. However it's not intended to export a single external function like myExternalFunction.

So in OpenModelica you can export A.fmu and B.fmu, create a new strongly connected SSP Model and add these FMUs as submodels. Under the hood OMEdit will use OMSimulator to simulate SSP/FMUs.

enter image description here

Even while it is not intended to do so you could probably still export your external function as a FMU. Just create a tiny model that will only call the external function (more or less like I did in B). But you are not getting any benefits from doing that.

AnHeuermann
  • 761
  • 3
  • 14
  • If I may, I tried to follow https://openmodelica.org/doc/OpenModelicaUsersGuide/v1.11.0/tlmcosimulation.html to test connecting FMUs together and simulate it. One of the FMU was obtained by exporting DirectInertia model from Modelica.Mechanics.Rotational.Examples.GenerationOfFMUs as an FMU. Importing the FMU back to OMEdit was OK, but I could not fetch the interface data. The progression bar was stuck at 0% even after I wait for one night. Are you aware about this issue? In the documentation, it seems that it should be working, considering the Modelica model I use is the default example.Thanks – philgun Sep 02 '21 at 06:30
  • You should try with a more recent version of OpenModelica. Version 1.11.0 is from 2017. Back then there was no SSP support available. Also the "old" FMU import is quiet different to the approach from my answer using SSP and OMSimulator. And yeah, in many cases the import in OMEdit File->Import->FMU is simply not working. – AnHeuermann Sep 03 '21 at 06:54
  • Hi An, I am using 1.14.2. What is the OpenModelica version that you use? Are OMSimulator and SSP shipped together when we install OpenModelica, or should they be installed separately? It seems that you can successfully generate the fetching data interface. I tried to use composite model framework instead of SSP approach that you used. Could you please point me to the documentation about SSP + FMUs simulation? – philgun Sep 06 '21 at 01:03
  • Maybe you should ask a new and more clear question on StackOverflow or in the OpenModelica forum. Your initial question regarding ML and FMUs is not really related to this. But check out openmodelica.org for the latest version of OpenModelica (1.18). OMSimulator is shipped together with OMEdit, SSP is an standard used by OpenModelica/OMSimulator (https://ssp-standard.org/). For documentation check out the user's guide of OpenModelica: https://www.openmodelica.org/doc/OpenModelicaUsersGuide/latest/omsimulator.html#new-omsimulator-model – AnHeuermann Sep 06 '21 at 12:51
  • Thanks An, actually I have asked the question in OpenModelica forum, about not able to fetch the interface data. I also sent a message to your OpenModelica account, if you are free, kindly check it out. I think I sent it a week ago. Here is the link to my post https://openmodelica.org/forum/default-topic/3344-can-not-fetch-interface-data-from-an-fmu-generated-by-openmodelica Thanks – philgun Sep 07 '21 at 11:30