.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\02-tbrom_examples\01-TBROM_dataTransfer_pyMAPDL.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_01-TBROM_dataTransfer_pyMAPDL.py: .. _ref_example_TBROM_cosim_pyMAPDL: Twin evaluation of a 3D field ROM and data transfer to FEA model inputs ----------------------------------------------------------------------- This example shows how PyTwin can be used to load and evaluate a twin model to predict CFD results in the form of temperature fields. Temperature fields are used as inputs for an FEA thermal structural analysis of a T-junction that considers the mixing of two different flow temperatures. The example uses PyTwin to evaluate the twin results and convert them to an appropriate format. It then uses PyMAPDL to load the FEA model, apply the temperature loads coming from the twin, and perform the thermal structural analysis. .. note:: To generate snapshot files at initialization time, the ROM included in the twin must have its parameter ``field_data_storage_period`` set to ``0`` and its parameter ``store_snapshots`` set to ``1``. To generate images files at initialization time, the ROM included in the twin must have the **Embed Geometry** and **Generate Image** options enabled at export time. Additionally, its parameter ``viewX_storage_period`` must be set to ``0``. These parameters can be defined in the Twin Builder subsheet before twin compilation or be exposed as twin parameters. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. image:: /_static/TBROM_cosim_pymapdl.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/TBROM_cosim_pymapdl.png' .. GENERATED FROM PYTHON SOURCE LINES 58-62 Perform required imports and launch an instance of MAPDL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports, which include downloading and importing the input files, and launch an instance of MAPDL. .. GENERATED FROM PYTHON SOURCE LINES 62-75 .. code-block:: Python from ansys.mapdl.core import launch_mapdl import numpy as np from pytwin import TwinModel, download_file, snapshot_to_array import pyvista as pv twin_file = download_file("ThermalTBROM_23R1_other.twin", "twin_files", force_download=True) fea_file = download_file("ThermalTBROM.dat", "other_files", force_download=True) # start mapdl mapdl = launch_mapdl() print(mapdl) .. rst-class:: sphx-glr-script-out .. code-block:: none Mapdl ----- PyMAPDL Version: 0.70.2 Interface: grpc Product: Ansys Mechanical Enterprise MAPDL Version: 22.2 Running on: localhost (127.0.0.1) .. GENERATED FROM PYTHON SOURCE LINES 76-79 Define inputs ~~~~~~~~~~~~~ Define inputs. .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python cfd_inputs = {"main_inlet_temperature": 353.15, "side_inlet_temperature": 293.15} rom_parameters = {"ThermalROM23R1_1_store_snapshots": 1} .. GENERATED FROM PYTHON SOURCE LINES 85-88 Import and save the mesh ~~~~~~~~~~~~~~~~~~~~~~~~~ Reset MAPDL and import the geometry. .. GENERATED FROM PYTHON SOURCE LINES 88-96 .. code-block:: Python mapdl.clear() mapdl.input(fea_file) # Save the mesh as a VTK object. print(mapdl.mesh) grid = mapdl.mesh.grid # Save mesh as a VTK object .. rst-class:: sphx-glr-script-out .. code-block:: none ANSYS Mesh Number of Nodes: 30685 Number of Elements: 20256 Number of Element Types: 143 Number of Node Components: 0 Number of Element Components: 0 .. GENERATED FROM PYTHON SOURCE LINES 97-100 Load the twin runtime and generate temperature results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the twin runtime and generate temperature results for the FEA mesh. .. GENERATED FROM PYTHON SOURCE LINES 100-110 .. code-block:: Python print("Loading model: {}".format(twin_file)) twin_model = TwinModel(twin_file) twin_model.initialize_evaluation(inputs=cfd_inputs, parameters=rom_parameters) rom_name = twin_model.tbrom_names[0] snapshot = twin_model.get_snapshot_filepath(rom_name=rom_name) geometry = twin_model.get_geometry_filepath(rom_name=rom_name) .. rst-class:: sphx-glr-script-out .. code-block:: none Loading model: C:\Users\ansys\AppData\Local\Temp\TwinExamples\twin_files\ThermalTBROM_23R1_other.twin .. GENERATED FROM PYTHON SOURCE LINES 111-114 Map temperature data to FEA mesh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Map the temperature data to the FEA mesh. .. GENERATED FROM PYTHON SOURCE LINES 114-133 .. code-block:: Python temperature_data = snapshot_to_array(snapshot, geometry) # Save data to a NumPy array nd_temp_data = temperature_data[:, :].astype(float) # Change data type to float # Map temperature data to the FE mesh # Convert imported data into PolyData format wrapped = pv.PolyData(nd_temp_data[:, :3]) # Convert NumPy array to PolyData format wrapped["temperature"] = nd_temp_data[:, 3] # Add a scalar variable 'temperature' to PolyData # Perform data mapping inter_grid = grid.interpolate( wrapped, sharpness=5, radius=0.0001, strategy="closest_point", progress_bar=True ) # Map the imported data to MAPDL grid inter_grid.plot(show_edges=False) # Plot the interpolated data on MAPDL grid temperature_load_val = pv.convert_array( pv.convert_array(inter_grid.active_scalars) ) # Save temperatures interpolated to each node as a NumPy array node_num = inter_grid.point_data["ansys_node_num"] # Save node numbers as a NumPy array .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01-TBROM_dataTransfer_pyMAPDL.py <01-TBROM_dataTransfer_pyMAPDL.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01-TBROM_dataTransfer_pyMAPDL.zip <01-TBROM_dataTransfer_pyMAPDL.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_