.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\export_data\vtk_helper.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_tutorials_export_data_vtk_helper.py: .. _ref_tutorials_export_data_vtk_helper: Convert DPF data to PyVista objects =================================== Convert DPF meshes and fields to PyVista ``UnstructuredGrid`` objects for in-memory manipulation and custom visualization. Unlike the VTU export operators, which write files to disk, the :mod:`vtk_helper ` module works entirely in memory: it converts |MeshedRegion|, |Field|, |FieldsContainer|, and |PropertyField| objects to ``pyvista.UnstructuredGrid`` objects that you can plot, filter, combine, or save to any format that PyVista supports. This tutorial demonstrates how to convert a mesh, validate it, enrich it with multiple fields incrementally, and batch-convert all time steps of a transient result at once. .. GENERATED FROM PYTHON SOURCE LINES 45-50 Import required modules ----------------------- Import the required modules and the helper functions from :mod:`vtk_helper `. .. GENERATED FROM PYTHON SOURCE LINES 50-61 .. code-block:: Python from ansys.dpf import core as dpf from ansys.dpf.core import examples from ansys.dpf.core.vtk_helper import ( append_field_to_grid, dpf_field_to_vtk, dpf_fieldscontainer_to_vtk, dpf_mesh_to_vtk, vtk_mesh_is_valid, ) .. GENERATED FROM PYTHON SOURCE LINES 62-67 Load the result file -------------------- Load a static structural result file and extract the |MeshedRegion| and |Model| that are reused throughout the first part of this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 67-80 .. code-block:: Python # Find and load the static structural result file result_file = examples.find_static_rst() # Create a DataSources object ds = dpf.DataSources(result_path=result_file) # Create a Model for result access my_model = dpf.Model(data_sources=ds) # Get the MeshedRegion mesh = my_model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 81-88 Convert a mesh to a PyVista grid --------------------------------- Use :func:`dpf_mesh_to_vtk ` to convert a |MeshedRegion| to a ``pyvista.UnstructuredGrid``. The resulting object contains only the geometry (nodes and connectivity); no field data is attached yet. .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: Python # Convert the MeshedRegion to a PyVista UnstructuredGrid grid = dpf_mesh_to_vtk(mesh=mesh) print(grid) .. rst-class:: sphx-glr-script-out .. code-block:: none UnstructuredGrid (0x13b81df0520) N Cells: 8 N Points: 81 X Bounds: 0.000e+00, 3.000e-02 Y Bounds: 3.000e-02, 6.000e-02 Z Bounds: 0.000e+00, 3.000e-02 N Arrays: 0 .. GENERATED FROM PYTHON SOURCE LINES 94-101 Validate the mesh ----------------- Use :func:`vtk_mesh_is_valid ` to check whether the converted grid passes VTK's built-in cell validator. For a valid mesh it prints a confirmation; for an invalid one it lists the elements that fail each geometric check. .. GENERATED FROM PYTHON SOURCE LINES 101-105 .. code-block:: Python # Run the cell validity check and print the result validity = vtk_mesh_is_valid(grid=grid, verbose=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh is valid. .. GENERATED FROM PYTHON SOURCE LINES 106-114 Convert a field to a PyVista grid ---------------------------------- Use :func:`dpf_field_to_vtk ` to convert a displacement |Field| and its associated mesh in a single call. Nodal data is automatically stored in ``grid.point_data``; elemental data goes into ``grid.cell_data``. Elemental-nodal data is not supported by VTK objects. .. GENERATED FROM PYTHON SOURCE LINES 114-123 .. code-block:: Python # Extract the displacement FieldsContainer and get the first time step disp_fc = my_model.results.displacement.eval() disp_field = disp_fc[0] # Convert the Field and its mesh to a single UnstructuredGrid disp_grid = dpf_field_to_vtk(field=disp_field, field_name="displacement") print(disp_grid.point_data) .. rst-class:: sphx-glr-script-out .. code-block:: none pyvista DataSetAttributes Association : POINT Active Scalars : displacement Active Vectors : None Active Texture : None Active Normals : None Contains arrays : displacement float64 (81, 3) SCALARS .. GENERATED FROM PYTHON SOURCE LINES 124-130 Visualize the displacement magnitude ------------------------------------ Use the DPF :class:`norm ` operator to compute the Euclidean norm of the displacement vector at each node, then append the resulting scalar |Field| to the grid and plot it. .. GENERATED FROM PYTHON SOURCE LINES 130-142 .. code-block:: Python # Compute the displacement magnitude using the DPF norm operator norm_field = dpf.operators.math.norm(field=disp_field).eval() # Append the scalar magnitude field to the displacement grid disp_grid = append_field_to_grid( field=norm_field, meshed_region=mesh, grid=disp_grid, field_name="displacement_magnitude" ) # Plot the magnitude on the mesh disp_grid.plot(scalars="displacement_magnitude", show_edges=True) .. image-sg:: /tutorials/export_data/images/sphx_glr_vtk_helper_001.png :alt: vtk helper :srcset: /tutorials/export_data/images/sphx_glr_vtk_helper_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 143-150 Build a grid incrementally with multiple fields ------------------------------------------------ Use :func:`append_field_to_grid ` to enrich an existing grid with additional field data. This approach is useful when you want to attach several results to the same ``UnstructuredGrid`` — for example nodal and elemental quantities together. .. GENERATED FROM PYTHON SOURCE LINES 150-169 .. code-block:: Python # Create a bare grid from the MeshedRegion rich_grid = dpf_mesh_to_vtk(mesh=mesh) # Append the displacement field (nodal location — stored in point_data) rich_grid = append_field_to_grid( field=disp_field, meshed_region=mesh, grid=rich_grid, field_name="displacement" ) # Append the element-type property field (elemental location — stored in cell_data) eltype_pf = mesh.property_field(property_name="eltype") rich_grid = append_field_to_grid( field=eltype_pf, meshed_region=mesh, grid=rich_grid, field_name="element_type" ) # Inspect the resulting data arrays on the grid print("Nodal arrays: ", list(rich_grid.point_data.keys())) print("Elemental arrays:", list(rich_grid.cell_data.keys())) .. rst-class:: sphx-glr-script-out .. code-block:: none Nodal arrays: ['displacement'] Elemental arrays: ['element_type'] .. GENERATED FROM PYTHON SOURCE LINES 170-179 Convert all time steps of a FieldsContainer -------------------------------------------- Use :func:`dpf_fieldscontainer_to_vtk ` to convert every |Field| in a |FieldsContainer| to a single ``UnstructuredGrid``. Each time step becomes a separate data array keyed by its label space, so all time steps are accessible from the same object. .. GENERATED FROM PYTHON SOURCE LINES 179-194 .. code-block:: Python # Load a transient result file with multiple time steps transient_result = examples.download_transient_result() trans_ds = dpf.DataSources(result_path=transient_result) trans_model = dpf.Model(data_sources=trans_ds) # Extract displacement for all time steps all_disp_fc = trans_model.results.displacement.on_all_time_freqs.eval() # Convert the entire FieldsContainer to a single UnstructuredGrid fc_grid = dpf_fieldscontainer_to_vtk(fields_container=all_disp_fc, field_name="displacement") # Each time step is stored as a separate point-data array print(f"Number of time-step arrays: {len(fc_grid.point_data)}") print("Array names (first 3):", list(fc_grid.point_data.keys())[:3]) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of time-step arrays: 35 Array names (first 3): ["displacement {'time': 1}", "displacement {'time': 2}", "displacement {'time': 3}"] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.528 seconds) .. _sphx_glr_download_tutorials_export_data_vtk_helper.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: vtk_helper.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: vtk_helper.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: vtk_helper.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_