.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_example/f2f/plot_heatexchanger.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_f2f_plot_heatexchanger.py: Run simulations with FMUFieldFunction ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 7-24 While :class:`~otfmi.FMUPointToFieldFunction` deals with scalar inputs (parameters) only, the :class:`~otfmi.FMUFieldFunction` enable you to define time-dependant inputs to study their effects on time-dependent outputs. We consider a model of heat exchanger. Basically, it models a flow of liquid coolant flowing through tubes, which are cooled by a flow of air. Here, we define time-dependent temperatures for air and coolant at inlet, and we study the evolution the temperature of air and coolant at the outlet. .. note:: In Modelica models, one can model time-dependent variables with table components. To use your model with OTFMI, you must set your time-dependent input as scalar inputs in the model. The temporal dependency is managed by OTFMI. .. GENERATED FROM PYTHON SOURCE LINES 24-32 .. code-block:: Python import math as m import otfmi import otfmi.example.utility import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 33-37 Prerequisites ~~~~~~~~~~~~~ We use the HeatExchanger example, with the function :class:`~otfmi.FMUFieldFunction`. .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python path_fmu = otfmi.example.utility.get_path_fmu("HeatExchanger") .. GENERATED FROM PYTHON SOURCE LINES 40-47 Define the `FMUFieldFunction` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We define the model with a `FMUFieldFunction` object, with 2 inputs and 2 outputs. You can change the time mesh (`input_mesh` and `output_mesh`). You can also set start and final times explicitly. For this example, We keep the time defined in the FMU. .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. code-block:: Python inputs_vars = ["Temp_air_inlet", "Temp_coolant_inlet"] outputs_vars = ["Temp_air_outlet", "Temp_coolant_outlet"] HX_model = otfmi.FMUFieldFunction(path_fmu, inputs_fmu=inputs_vars, outputs_fmu=outputs_vars) print(HX_model) .. rst-class:: sphx-glr-script-out .. code-block:: none FieldFunction : class=PythonFieldFunction name=OpenTURNSFMUFieldFunction .. GENERATED FROM PYTHON SOURCE LINES 58-63 Define the time grid ~~~~~~~~~~~~~~~~~~~~ The first work consists in defining the time grids on which the model will be evaluated (for both input and output fields). You can recover the default time grids defined in the model. .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python input_mesh = HX_model.getInputMesh() output_mesh = HX_model.getOutputMesh() .. GENERATED FROM PYTHON SOURCE LINES 67-69 If you want to override this with your own timegrid, you can use :py:class:`openturns.RegularGrid` .. GENERATED FROM PYTHON SOURCE LINES 71-84 Define the inputs ~~~~~~~~~~~~~~~~~ We want to drive inlet air and coolant temperatures, to see their effects on outlet temperatures. Inputs are defined in one list. Each element is itself a list, giving for each time the values of the inputs. Here we suppose a sinusoidal evolution of temperatures. You can change the frequency (Hz). The phase is randomly set. .. note:: The inputs given to otfmi must be declared as `input` in the Modelica model. If you try to change parameters, it won't work. .. GENERATED FROM PYTHON SOURCE LINES 84-113 .. code-block:: Python freq_air = 0.5 omega_air = 2 * m.pi * freq_air freq_cool = 1.5 omega_cool = 2 * m.pi * freq_cool phi = 3.78 input_timeseries = ot.Sample(0, 2) for time in input_mesh.getVertices().asPoint(): Temp_air_inlet = 25.0 + 4.0 * m.sin(omega_air * time + phi) Temp_coolant_inlet = 50.0 + 4.0 * m.sin(omega_cool * time + phi) input_timeseries.add([Temp_air_inlet, Temp_coolant_inlet]) graph_in = ot.Graph("Inlet Temperatures evolution", "FMU simulation time (s)", "Temperature (°C)", True) curve_TairIn = ot.Curve(input_mesh.getVertices(), input_timeseries[:, 0]) curve_TairIn.setColor("green") graph_in.add(curve_TairIn) curve_TcoolIn = ot.Curve(input_mesh.getVertices(), input_timeseries[:, 1]) curve_TcoolIn.setColor("blue") graph_in.add(curve_TcoolIn) graph_in.setIntegerXTick(True) graph_in.setLegends(inputs_vars) graph_in.setLegendPosition("center right") view = otv.View(graph_in) .. image-sg:: /auto_example/f2f/images/sphx_glr_plot_heatexchanger_001.png :alt: Inlet Temperatures evolution :srcset: /auto_example/f2f/images/sphx_glr_plot_heatexchanger_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-115 Run the simulation .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: Python outlet_temperatures = HX_model(input_timeseries) print(outlet_temperatures[-5:]) .. rst-class:: sphx-glr-script-out .. code-block:: none [ Temp_air_outlet Temp_coolant_outlet ] 0 : [ 41.4999 47.5558 ] 1 : [ 41.5455 47.612 ] 2 : [ 41.5906 47.6675 ] 3 : [ 41.6352 47.7225 ] 4 : [ 41.6793 47.7767 ] .. GENERATED FROM PYTHON SOURCE LINES 119-120 See results .. GENERATED FROM PYTHON SOURCE LINES 120-138 .. code-block:: Python graph_out = ot.Graph("Outlet Temperature evolution", "FMU simulation time (s)", "Temperature (°C)", True) curveTairOut = ot.Curve(output_mesh.getVertices(), outlet_temperatures[:, 0]) curveTairOut.setColor("red") graph_out.add(curveTairOut) curveTcoolOut = ot.Curve(output_mesh.getVertices(), outlet_temperatures[:, 1]) curveTcoolOut.setColor("orange") graph_out.add(curveTcoolOut) graph_out.setIntegerXTick(True) graph_out.setLegends(outputs_vars) graph_out.setLegendPosition("center right") view = otv.View(graph_out) .. image-sg:: /auto_example/f2f/images/sphx_glr_plot_heatexchanger_002.png :alt: Outlet Temperature evolution :srcset: /auto_example/f2f/images/sphx_glr_plot_heatexchanger_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-145 Alternative : Define the `FMUFieldToPointFunction` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The previous function support timeseries as inputs and outputs. If you are interested in only a scalar output, OTFMI offers a variant :class:`~otfmi.FMUFieldToPointFunction`, to get only and directly the output at the last timestep. .. GENERATED FROM PYTHON SOURCE LINES 145-150 .. code-block:: Python HX_model = otfmi.FMUFieldToPointFunction(path_fmu, input_mesh, inputs_fmu=inputs_vars, outputs_fmu=outputs_vars) .. GENERATED FROM PYTHON SOURCE LINES 151-152 Run the simulation .. GENERATED FROM PYTHON SOURCE LINES 152-155 .. code-block:: Python outlet_temperatures = HX_model(input_timeseries) print(outlet_temperatures) .. rst-class:: sphx-glr-script-out .. code-block:: none [41.6793,47.7767] .. GENERATED FROM PYTHON SOURCE LINES 156-157 .. code-block:: Python otv.View.ShowAll() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.270 seconds) .. _sphx_glr_download_auto_example_f2f_plot_heatexchanger.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_heatexchanger.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_heatexchanger.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_heatexchanger.zip `