.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\02-tbrom_examples\07-TBROM_parametric_field_history.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_02-tbrom_examples_07-TBROM_parametric_field_history.py: .. _ref_example_TBROM_parametric_field_history: 3D parametric field history ROM example --------------------------------------- This example shows how to use PyTwin to load and evaluate a twin model built upon a parametric field history ROM. Such ROM, created with Static ROM Builder, has parameters that can be changed from one evaluation to another, and will output field predictions over a time grid. The example shows how to evaluate and post process the results at different time points. .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. image:: /_static/TBROM_pfieldhistory_t250.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/TBROM_pfieldhistory_t250.png' .. GENERATED FROM PYTHON SOURCE LINES 43-51 The example model is a simplified 3D mechanical frame tightened by two bolts. The bolts are represented as forces applied to the tips of the component. The magnitude of the deformation is dependent on the magnitude of both force parameters. Additionally, this component is made of a material that exhibits time-dependent behavior, allowing the structure to undergo a mechanical phenomenon known as creep. Essentially, the deformation of the structure changes over time, even under constant applied forces, which makes the problem suitable for parametric field history ROM. .. GENERATED FROM PYTHON SOURCE LINES 53-66 .. note:: To be able to use the functionalities illustrated in this example, you must have a twin with one or more TBROMs. The output mode coefficients for the TBROMs must be enabled when exporting the TBROMs and connected to twin outputs following these conventions: - If there are multiple TBROMs in the twin, the format for the name of the twin output must be ``outField_mode_{mode_index}_{tbrom_name}``. - If there is a single TBROM in the twin, the format for the name of the twin output must be ``outField_mode_{mode_index}``. .. image:: /_static/snapshot_generation.png :width: 300pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. note:: To be able to use the functionalities to visualize the results, you need to have a Twin with 1 or more TBROM, for which its geometry is embedded when exporting the TBROMs to Twin Builder .. image:: /_static/point_generation.png :width: 200pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 76-79 Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports, which include downloading and importing the input files. .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from pytwin import TwinModel, download_file import pyvista as pv twin_file = download_file("TwinPFieldHistory_wGeo_25R2.twin", "twin_files", force_download=True) .. GENERATED FROM PYTHON SOURCE LINES 88-91 Define ROM scalar inputs ~~~~~~~~~~~~~~~~~~~~~~~~ Define the ROM scalar inputs. .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python rom_inputs = {"testROM_25r2_1_force_1_Magnitude": 9500, "testROM_25r2_1_force_2_Magnitude": 16500} .. GENERATED FROM PYTHON SOURCE LINES 95-98 Load the twin runtime and generate displacement results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the twin runtime, initialize the evaluation and display displacement results from the TBROM. .. GENERATED FROM PYTHON SOURCE LINES 98-112 .. code-block:: Python print("Loading model: {}".format(twin_file)) twin_model = TwinModel(twin_file) romname = twin_model.tbrom_names[0] twin_model.initialize_evaluation(parameters=rom_inputs) field_data = twin_model.get_tbrom_output_field(romname) plotter = pv.Plotter() plotter.set_background("white") plotter.add_axes() plotter.add_mesh(field_data, scalar_bar_args={"color": "black"}, clim=[0.0, 6.0]) plotter.add_title("Time = {}".format(twin_model.evaluation_time)) plotter.show() print(max(field_data.active_scalars)) .. rst-class:: sphx-glr-script-out .. code-block:: none Loading model: C:\Users\ansys\AppData\Local\Temp\TwinExamples\twin_files\TwinPFieldHistory_wGeo_25R2.twin 0.8973744667566538 .. GENERATED FROM PYTHON SOURCE LINES 113-116 .. image:: /_static/TBROM_pfieldhistory_t0.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 118-125 Evaluate the twin at different time points (3D field visualization) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Because the twin is based on a parametric field history ROM, the entire field history has been computed during the Twin initialization. In order to visualize the results at different time points, the Twin can be advanced and simulated over time. A linear interpolation is performed in case the time selected is between two time steps of the field history. .. GENERATED FROM PYTHON SOURCE LINES 125-138 .. code-block:: Python # After 100 seconds twin_model.evaluate_step_by_step(100.0) field_data = twin_model.get_tbrom_output_field(romname) plotter = pv.Plotter() plotter.set_background("white") plotter.add_axes() plotter.add_mesh(field_data, scalar_bar_args={"color": "black"}, clim=[0.0, 6.0]) plotter.add_title("Time = {}".format(twin_model.evaluation_time)) plotter.show() print(max(field_data.active_scalars)) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.685669230751107 .. GENERATED FROM PYTHON SOURCE LINES 139-142 .. image:: /_static/TBROM_pfieldhistory_t100.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 142-155 .. code-block:: Python # After 250 seconds twin_model.evaluate_step_by_step(150.0) field_data = twin_model.get_tbrom_output_field(romname) plotter = pv.Plotter() plotter.set_background("white") plotter.add_axes() plotter.add_mesh(field_data, scalar_bar_args={"color": "black"}, clim=[0.0, 6.0]) plotter.add_title("Time = {}".format(twin_model.evaluation_time)) plotter.show() print(max(field_data.active_scalars)) .. rst-class:: sphx-glr-script-out .. code-block:: none 5.635884051349383 .. GENERATED FROM PYTHON SOURCE LINES 156-159 .. image:: /_static/TBROM_pfieldhistory_t250.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 161-168 Evaluate the twin at different time points (time series) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If we continue simulating the Twin over time, the field results won't change anymore since we have reached the end time of the field history. To evaluate the ROM again, it needs to be re-initialized, with the possibility to change input parameters values. In this section, we will see how to build and visualize a transient prediction at a given location. .. GENERATED FROM PYTHON SOURCE LINES 168-192 .. code-block:: Python timegrid = twin_model.get_tbrom_time_grid(romname) print(timegrid) fieldName = twin_model.get_field_output_name(romname) + "-normed" point = np.array([0.0, 0.0, 0.0]) idx = field_data.find_closest_point(point) outputValues = [] closest_pt = field_data.points[idx] print(closest_pt) twin_model.initialize_evaluation(parameters=rom_inputs) outputValues.append(field_data.point_data[fieldName][idx]) for i in range(1, len(timegrid)): step = timegrid[i] - timegrid[i - 1] twin_model.evaluate_step_by_step(step) outputValues.append(field_data.point_data[fieldName][idx]) plt.plot(timegrid, outputValues) plt.title("Displacement vs Time for point {}".format(np.round(closest_pt, 3))) plt.show() .. image-sg:: /examples/02-tbrom_examples/images/sphx_glr_07-TBROM_parametric_field_history_001.png :alt: Displacement vs Time for point [0. 0. 0.] :srcset: /examples/02-tbrom_examples/images/sphx_glr_07-TBROM_parametric_field_history_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [0.0, 2.00000099, 9.12500096, 31.78125091, 61.78125091, 91.78125091, 120.3635355, 142.480949, 160.55882932, 176.11231065, 189.71636929, 201.93378105, 213.22661155, 223.71653536, 233.52522703, 242.67983024, 250.0] [0.00000000e+00 3.50599992e-05 0.00000000e+00] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.513 seconds) .. _sphx_glr_download_examples_02-tbrom_examples_07-TBROM_parametric_field_history.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 07-TBROM_parametric_field_history.ipynb <07-TBROM_parametric_field_history.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 07-TBROM_parametric_field_history.py <07-TBROM_parametric_field_history.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 07-TBROM_parametric_field_history.zip <07-TBROM_parametric_field_history.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_