.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/p2f/epid/plot_dyn_init.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_p2f_epid_plot_dyn_init.py: Initialize FMUPointToFieldFunction ================================== .. GENERATED FROM PYTHON SOURCE LINES 7-10 The interest of using FMUs in Python lies in the ease to change its input / parameter values. This notably enables to study the behavior of the FMU with uncertain inputs / parameters. .. GENERATED FROM PYTHON SOURCE LINES 12-17 Initialization scripts can gather a large number of initial values. The use of initialization scripts (*.mos* files) is common in Dymola : - to save the value of all the variables of a model after simulation, - to restart simulation from a given point. .. GENERATED FROM PYTHON SOURCE LINES 19-20 First, retrieve the path to the FMU *epid.fmu*. .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: Python import otfmi.example.utility import otfmi import openturns as ot from os.path import abspath import openturns.viewer as viewer path_fmu = otfmi.example.utility.get_path_fmu("epid") .. GENERATED FROM PYTHON SOURCE LINES 29-31 The initialization script must be provided to `FMUPointToFieldFunction` constructor. We thus create it now (using Python for clarity). .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. note:: The initialization script can be automatically created in Dymola. .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. code-block:: Python temporary_file = "initialization.mos" with open(temporary_file, "w") as f: f.write("total_pop = 500;\n") f.write("healing_rate = 0.5;\n") .. GENERATED FROM PYTHON SOURCE LINES 42-44 If no initial value is provided for an input / parameter, it is set to its default initial value (as set in the FMU). .. GENERATED FROM PYTHON SOURCE LINES 46-49 We are interested in the model output during the 2 first seconds of simulation (which corresponds to a specific moment in the epidemic spreading). We must create the time grid on which the model output will be interpolated. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python mesh = ot.RegularGrid(0.0, 0.1, 20) meshSample = mesh.getVertices() print(meshSample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ t ] 0 : [ 0 ] 1 : [ 0.1 ] 2 : [ 0.2 ] 3 : [ 0.3 ] 4 : [ 0.4 ] 5 : [ 0.5 ] 6 : [ 0.6 ] 7 : [ 0.7 ] 8 : [ 0.8 ] 9 : [ 0.9 ] 10 : [ 1 ] 11 : [ 1.1 ] 12 : [ 1.2 ] 13 : [ 1.3 ] 14 : [ 1.4 ] 15 : [ 1.5 ] 16 : [ 1.6 ] 17 : [ 1.7 ] 18 : [ 1.8 ] 19 : [ 1.9 ] .. GENERATED FROM PYTHON SOURCE LINES 55-59 We can now build the `FMUPointToFieldFunction`. In the example below, we use the initialization script to fix the (non-default) values of ``total_pop`` and ``healing_rate`` in the FMU. We can thus observe the evolution of ``infected`` depending on the ``infection_rate``. .. GENERATED FROM PYTHON SOURCE LINES 59-70 .. code-block:: Python function = otfmi.FMUPointToFieldFunction( path_fmu, mesh, inputs_fmu=["infection_rate"], outputs_fmu=["infected"], initialization_script=abspath("initialization.mos"), start_time=0.0, final_time=5.0, ) .. GENERATED FROM PYTHON SOURCE LINES 71-75 ``total_pop`` and ``healing_rate`` values are defined in the initialization script, and remain constant over time. We can now set probability laws on the function input variable ``infection_rate`` to propagate its uncertainty through the model: .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: Python law_infection_rate = ot.Normal(2.0, 0.25) inputSample = law_infection_rate.getSample(10) outputProcessSample = function(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 81-83 Visualize the time evolution of the ``infected`` over time, depending on the `ìnfection_rate`` value: .. GENERATED FROM PYTHON SOURCE LINES 83-91 .. code-block:: Python gridLayout = outputProcessSample.draw() graph = gridLayout.getGraph(0, 0) graph.setTitle("") graph.setXTitle("FMU simulation time (s)") graph.setYTitle("Number of infected") graph.setLegends([f"{line[0]:.4f}" for line in inputSample]) view = viewer.View(graph, legend_kw={"title": "infection rate", "loc": "upper left"}) view.ShowAll() .. image-sg:: /auto_example/p2f/epid/images/sphx_glr_plot_dyn_init_001.png :alt: plot dyn init :srcset: /auto_example/p2f/epid/images/sphx_glr_plot_dyn_init_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.283 seconds) .. _sphx_glr_download_auto_example_p2f_epid_plot_dyn_init.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dyn_init.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dyn_init.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_dyn_init.zip `