.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/export/plot_model_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_model_exporter.py: Export a function as Modelica model =================================== .. GENERATED FROM PYTHON SOURCE LINES 7-13 The method `export_model` of :class:`~otfmi.FunctionExporter` is another way to export OpenTURNS functions, but this time as Modelica model. This can be useful if you want to include a function produced with OpenTURNS into a Modelica model. Compared to `export_fmu`, here, you cannot export models with time-dependent outputs. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python # One application of the inclusion of a metamodel in # `OpenModelica GUI `_ # is described in # `this paper `_. .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python import openturns as ot import otfmi import tempfile from os.path import join .. GENERATED FROM PYTHON SOURCE LINES 26-27 First, we create the OpenTURNS function to export as Modelica model. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. 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 32-33 We create the model constructor and the folder in which save the model: .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python fmuExporter = otfmi.FunctionExporter(func) model_path = join(tempfile.mkdtemp(), "myExponential.mo") print(model_path) .. rst-class:: sphx-glr-script-out .. code-block:: none /tmp/tmpl3tcdirk/myExponential.mo .. GENERATED FROM PYTHON SOURCE LINES 39-42 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. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python modelExporter = otfmi.FunctionExporter(func) modelExporter.export_model(model_path, gui=True) .. GENERATED FROM PYTHON SOURCE LINES 46-95 Simple as it looks, this function actually writes a C-wrapper for the OpenTURNS function, then writes a Modelica model calling the C-wrapper as `External function `_. .. note:: The export requires `CMake `_, a C compiler, and the OpenModelica compiler `OMC `_. We import this model in OpenModelica GUI. We can check the Modelica code: .. image:: /_static/modelica_gui_code.png :scale: 80 % :alt: alternate text :align: center .. note:: The path to the C-wrapper is hard-coded in the model. We can also check the connectors position: .. image:: /_static/modelica_gui_interface.png :scale: 60 % :alt: alternate text :align: center We connect the wrapper to an input sine signal (*Modelica.Blocks.Sources.Sine*) and to an output block (*Modelica.Blocks.Interfaces.RealOutput*): .. image:: /_static/TestMyExponential.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 .. note:: 3 modes are available to export the function (see :class:`~otfmi.FunctionExporter`). By default, the mode used to export the function is 'cxx'. This mode leads to the fastest version of the model, but you need to install OpenTURNS with conda. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.358 seconds) .. _sphx_glr_download_auto_example_export_plot_model_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_model_exporter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_model_exporter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_model_exporter.zip `