.. 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 29-32 .. image:: /_static/TBROM_cosim_pymapdl.png :width: 400pt :align: center .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/TBROM_cosim_pymapdl.png' .. GENERATED FROM PYTHON SOURCE LINES 36-40 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 40-54 .. code-block:: Python from ansys.mapdl.core import launch_mapdl import numpy as np import pandas as pd from pytwin import TwinModel, download_file, read_binary 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 Product: Ansys Mechanical Enterprise MAPDL Version: 22.2 ansys.mapdl Version: 0.65.2 .. GENERATED FROM PYTHON SOURCE LINES 55-58 Define inputs ~~~~~~~~~~~~~ Define inputs. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. 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 63-67 Define auxiliary functions ~~~~~~~~~~~~~~~~~~~~~~~~~~ Define an auxiliary function for converting the ROM snapshot for data mapping on an FEA mesh. .. GENERATED FROM PYTHON SOURCE LINES 67-80 .. code-block:: Python def snapshot_to_fea(snapshot_file, geometry_file): """Create a Pandas dataframe containing the x, y, z coordinates for the ROM and snapshot file results.""" geometry_data = read_binary(geometry_file).reshape(-1, 3) snapshot_data = read_binary(snapshot_file).reshape(-1, 1) res_list = np.hstack((geometry_data, snapshot_data)) return pd.DataFrame(res_list) .. GENERATED FROM PYTHON SOURCE LINES 81-84 Import and save the mesh ~~~~~~~~~~~~~~~~~~~~~~~~~ Reset MAPDL and import the geometry. .. GENERATED FROM PYTHON SOURCE LINES 84-92 .. 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 93-96 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 96-108 .. 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) temperature_file = snapshot_to_fea(snapshot, geometry) .. 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 109-112 Map temperature data to FEA mesh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Map the temperature data to the FEA mesh. .. GENERATED FROM PYTHON SOURCE LINES 112-131 .. code-block:: Python temperature_data = temperature_file.values # 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>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_