.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\01-twin_examples\02-heatExchangerRS.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_examples_01-twin_examples_02-heatExchangerRS.py: .. _ref_example_heatExchangerRS: Parametric twin evaluation of a response surface ROM ---------------------------------------------------- This example shows how you can use PyTwin to load and evaluate a twin model and simulate multiple parametric variations. The model is based on a response surface ROM created from a steady state thermal model of a heat exchanger. The inputs are the minimum and maximum heat flows on the inner face. The outputs are the inner temperature and the temperatures from three temperature probes within the solid and outer temperature. The model is tested against different input values to evaluate the corresponding temperature responses. .. GENERATED FROM PYTHON SOURCE LINES 15-18 .. image:: /_static/heatExchangerRS.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/heatExchangerRS.png' .. GENERATED FROM PYTHON SOURCE LINES 22-25 Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports, which include downloading and importing the input files. .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python import matplotlib.pyplot as plt import numpy import pandas as pd from pytwin import TwinModel, download_file twin_file = download_file("HeatExchangerRS_23R1_other.twin", "twin_files", force_download=True) .. GENERATED FROM PYTHON SOURCE LINES 34-37 Define inputs and simulation settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Define the inputs and simulation settings. .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python heat_flow_min = 0.0 heat_flow_max = 50000.0 step = 50.0 .. GENERATED FROM PYTHON SOURCE LINES 43-47 Define auxiliary functions ~~~~~~~~~~~~~~~~~~~~~~~~~~ Define auxiliary functions for comparing and plotting the results from different input values evaluated on the twin model. .. GENERATED FROM PYTHON SOURCE LINES 47-89 .. code-block:: Python def plot_result_comparison(results: pd.DataFrame): """Compare the results obtained from the different input values evaluated on the twin model. The results datasets are provided as Pandas dataframes. The function plots the results for few variables of particular interest.""" pd.set_option("display.precision", 12) pd.set_option("display.max_columns", 20) pd.set_option("display.expand_frame_repr", False) color = ["g"] # Output ordering: T_inner, T1_out, T_outer, T2_out, T3_out x_ind = 0 y0_ind = 1 y1_ind = 2 y2_ind = 4 y3_ind = 5 y4_ind = 3 # Plot simulation results (outputs versus input) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(18, 7)) fig.subplots_adjust(hspace=0.5) fig.set_tight_layout({"pad": 0.0}) axes0 = ax results.plot(x=x_ind, y=y0_ind, ax=axes0, ls="-.", label="{}".format("T inner")) results.plot(x=x_ind, y=y1_ind, ax=axes0, ls="-.", label="{}".format("T1")) results.plot(x=x_ind, y=y2_ind, ax=axes0, ls="-.", label="{}".format("T2")) results.plot(x=x_ind, y=y3_ind, ax=axes0, ls="-.", label="{}".format("T3")) results.plot(x=x_ind, y=y4_ind, ax=axes0, ls="-.", label="{}".format("T outer")) axes0.set_title("Heat Exchanger thermal response") axes0.set_xlabel(results.columns[x_ind] + " [W]") axes0.set_ylabel("Temperature [deg C]") # Show plot plt.show() .. GENERATED FROM PYTHON SOURCE LINES 90-93 Load the twin runtime and instantiate it ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the twin runtime and instantiate it. .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: Python print("Loading model: {}".format(twin_file)) twin_model = TwinModel(twin_file) .. rst-class:: sphx-glr-script-out .. code-block:: none Loading model: C:\Users\ansys\AppData\Local\Temp\TwinExamples\twin_files\HeatExchangerRS_23R1_other.twin .. GENERATED FROM PYTHON SOURCE LINES 98-106 Evaluate the twin with different input values and collect corresponding outputs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Because the twin is based on a static model, two options can be considered: - Set the initial input value to evaluate and run the initialization function (current approach). - Create an input dataframe considering all input values to evaluate and run the batch function to evaluate. In this case, to execute the transient simulation, a time dimension must be arbitrarily defined. .. GENERATED FROM PYTHON SOURCE LINES 106-123 .. code-block:: Python results = [] input_name = list(twin_model.inputs.keys())[0] for dp in numpy.linspace(start=heat_flow_min, stop=heat_flow_max, num=int((heat_flow_max - heat_flow_min) / step + 1)): # Initialize twin with input values and collect output value dp_input = {input_name: dp} twin_model.initialize_evaluation(inputs=dp_input) outputs = [dp] for item in twin_model.outputs: outputs.append(twin_model.outputs[item]) results.append(outputs) if dp % 1000 == 0.0: print("Simulating the model with input {}".format(dp)) sim_results = pd.DataFrame(results, columns=[input_name] + list(twin_model.outputs), dtype=float) .. rst-class:: sphx-glr-script-out .. code-block:: none Simulating the model with input 0.0 Simulating the model with input 1000.0 Simulating the model with input 2000.0 Simulating the model with input 3000.0 Simulating the model with input 4000.0 Simulating the model with input 5000.0 Simulating the model with input 6000.0 Simulating the model with input 7000.0 Simulating the model with input 8000.0 Simulating the model with input 9000.0 Simulating the model with input 10000.0 Simulating the model with input 11000.0 Simulating the model with input 12000.0 Simulating the model with input 13000.0 Simulating the model with input 14000.0 Simulating the model with input 15000.0 Simulating the model with input 16000.0 Simulating the model with input 17000.0 Simulating the model with input 18000.0 Simulating the model with input 19000.0 Simulating the model with input 20000.0 Simulating the model with input 21000.0 Simulating the model with input 22000.0 Simulating the model with input 23000.0 Simulating the model with input 24000.0 Simulating the model with input 25000.0 Simulating the model with input 26000.0 Simulating the model with input 27000.0 Simulating the model with input 28000.0 Simulating the model with input 29000.0 Simulating the model with input 30000.0 Simulating the model with input 31000.0 Simulating the model with input 32000.0 Simulating the model with input 33000.0 Simulating the model with input 34000.0 Simulating the model with input 35000.0 Simulating the model with input 36000.0 Simulating the model with input 37000.0 Simulating the model with input 38000.0 Simulating the model with input 39000.0 Simulating the model with input 40000.0 Simulating the model with input 41000.0 Simulating the model with input 42000.0 Simulating the model with input 43000.0 Simulating the model with input 44000.0 Simulating the model with input 45000.0 Simulating the model with input 46000.0 Simulating the model with input 47000.0 Simulating the model with input 48000.0 Simulating the model with input 49000.0 Simulating the model with input 50000.0 .. GENERATED FROM PYTHON SOURCE LINES 124-127 Plot results ~~~~~~~~~~~~ Plot the results and save the image on disk. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python plot_result_comparison(sim_results) .. image-sg:: /examples/01-twin_examples/images/sphx_glr_02-heatExchangerRS_001.png :alt: Heat Exchanger thermal response :srcset: /examples/01-twin_examples/images/sphx_glr_02-heatExchangerRS_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.230 seconds) .. _sphx_glr_download_examples_01-twin_examples_02-heatExchangerRS.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02-heatExchangerRS.ipynb <02-heatExchangerRS.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02-heatExchangerRS.py <02-heatExchangerRS.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_