.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/export/plot_fmu_exporter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_example_export_plot_fmu_exporter.py: Export a function as FMU ======================== .. GENERATED FROM PYTHON SOURCE LINES 7-15 ``otfmi.FunctionExporter`` enables you to export OpenTURNS functions as FMUs. The main interest of this class is to integrate OpenTURNS models in another models. One application of the inclusion of a metamodel in `OpenModelica GUI `_ is described in `this paper `_. .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: Python import openturns as ot import otfmi import tempfile from os.path import join .. GENERATED FROM PYTHON SOURCE LINES 23-26 First, we create the OpenTURNS function to export as FMU. This example being solely a demonstrator, we consider a very simple exponential function. .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python func = ot.SymbolicFunction(["x"], ["exp(x)"]) inputPoint = [2.0] print(func(inputPoint)) .. rst-class:: sphx-glr-script-out .. code-block:: none [7.38906] .. GENERATED FROM PYTHON SOURCE LINES 31-32 We create the FMU constructor and the folder in which save the FMU: .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python fmuExporter = otfmi.FunctionExporter(func) fmu_path = join(tempfile.mkdtemp(), "myExponential.fmu") print(fmu_path) .. rst-class:: sphx-glr-script-out .. code-block:: none /tmp/tmpn_p_lhrt/myExponential.fmu .. GENERATED FROM PYTHON SOURCE LINES 37-42 We export the OpenTURNS function as a Model Exchange FMU. The counterpart is to export as CoSimulation FMU (embeds a solver). Another option would be to export the function via pythonfmu (see the "mode" keyword) allowing export of temporal functions (not a concern here for our function). .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python fmuExporter.export_fmu(fmu_path, fmuType="me") .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. note:: The export requires `CMake `_ build system, a C compiler (Visual Studio or GCC for instance), and the OpenModelica compiler `OMC `_. .. GENERATED FROM PYTHON SOURCE LINES 51-86 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, - export the Modelica model as FMU using OpenModelica compiler. .. note:: To retrieve only the Modelica model, see :doc:`here `. .. warning:: The path to the C-wrapper is hard-coded in the model & in the FMU. We import the fmu in OpenModelica GUI. We connect the wrapper to an input sine signal (*Modelica.Blocks.Sources.Sine*) and to an output block (*Modelica.Blocks.Interfaces.RealOutput*): .. image:: /_static/TestMyExponentialFMU.png :scale: 100 % :alt: alternate text :align: center 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. .. image:: /_static/show_output.png :scale: 80 % :alt: alternate text :align: center .. warning:: Compared to native Modelica functions, the included OpenTURNS function is slow. Using the FMU, 11 seconds of simulation were required for 50 time steps (i.e. 50 function calls). .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.908 seconds) .. _sphx_glr_download_auto_example_export_plot_fmu_exporter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_fmu_exporter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_fmu_exporter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_fmu_exporter.zip `