Export a function as Modelica model

otfmi.FunctionExporter enables to export OpenTURNS functions as Modelica model. The main interest is to use OpenTURNS metamodels in a simulation environment.

Currently, the inclusion of a metamodel in OpenModelica GUI has been performed once (see this paper).


Model generation

First, we create the OpenTURNS function to export as Modelica model.

import openturns as ot
import otfmi
import tempfile
from os.path import join


func = ot.SymbolicFunction("x", "exp(x)")
inputPoint = [2.0]
print(func(inputPoint))
[7.38906]

We create the model constructor and the folder in which save the model:

fmuExporter = otfmi.FunctionExporter(func)

model_path = join(tempfile.mkdtemp(), "myExponential.mo")
print(model_path)
/tmp/tmpe_erd3eh/myExponential.mo

We create the FunctionExporter instance and export the exponential function. We specify gui=True to use the model in a Modelica GUI in connection with other components.

modelExporter = otfmi.FunctionExporter(func)
modelExporter.export_model(model_path, gui=True)

Simple as it looks, this function actually does the following:

  • write a C-wrapper for the OpenTURNS function,

  • write a Modelica model calling the C-wrapper as External function.

Note

The export requires CMake, a C compiler, and the OpenModelica compiler OMC.


Model validation

We import this model in OpenModelica GUI. We can check the Modelica code:

alternate text

Note

The path to the C-wrapper is hard-coded in the model.

We can also check the connectors position:

alternate text

We connect the wrapper to an input sine signal (Modelica.Blocks.Sources.Sine) and to an output block (Modelica.Blocks.Interfaces.RealOutput):

alternate text

We simulate the model on 1 second, with 50 time steps. We can verify that y output corresponds to the exponential of the sine signal.

alternate text

Warning

⚠️ Compared to native Modelica functions, the included OpenTURNS function is slow. In this case, 11 seconds of simulation were required for 50 time steps (i.e. 50 function calls).

Note that faster export modes are available with the “mode” keyword, depending on your setup.

Total running time of the script: (0 minutes 0.409 seconds)