FMU machine learning model [closed]

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.



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

model A
 output Real x (start=1,fixed=true);
  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;

  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;
  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.