The vtk_helper.py module#
Summary#
Dataclass containing the results of a call to vtk_mesh_is_valid. |
Error raised when PyVista could not be imported during plotting. |
Return a pyvista UnstructuredGrid given a MeshedRegion. |
|
Run a vtk.CellValidator filter on the input grid. |
|
Update coordinates in vtk. |
|
Return a pyvista UnstructuredGrid given a MeshedRegion. |
|
Return a pyvista UnstructuredGrid given a DPF Field. |
|
Return a pyvista UnstructuredGrid given a DPF FieldsContainer. |
|
Return a pyvista UnstructuredGrid given a DPF PropertyField. |
|
Append field data to a VTK UnstructuredGrid based on a MeshedRegion. |
|
Append fields data to a VTK UnstructuredGrid based on a MeshedRegion. |
Description#
Helpers for interactions between DPF and VTK.
This module provides functions and utilities for converting DPF mesh and field data to VTK/PyVista formats for visualization and analysis. It includes mappings between DPF element types and VTK cell types, mesh and field conversion routines, mesh validity checking, and helpers for appending DPF data to VTK grids.
Main features: - Conversion of DPF MeshedRegion, Field, FieldsContainer, PropertyField, and MeshesContainer to PyVista UnstructuredGrid. - Mapping between DPF element types and VTK cell types (linear and quadratic). - Mesh validity checking using VTK’s CellValidator. - Utilities for updating coordinates and appending field data to VTK grids.
Module detail#
- vtk_helper.dpf_mesh_to_vtk(mesh: ansys.dpf.core.MeshedRegion, nodes: ansys.dpf.core.Field = None, as_linear: bool = True, check_validity: bool = False) pyvista.UnstructuredGrid#
Return a pyvista UnstructuredGrid given a MeshedRegion.
- Parameters:
mesh – Meshed Region to export to pyVista format.
nodes – Field containing the node coordinates of the mesh (useful to get a deformed geometry).
as_linear – Export quadratic surface elements as linear.
check_validity – Whether to run the VTK cell validity check on the generated mesh and warn if not valid.
- Returns:
UnstructuredGrid corresponding to the DPF mesh.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_mesh_to_vtk >>> model = dpf.Model(examples.find_simple_bar()) >>> mesh = model.metadata.meshed_region >>> grid = dpf_mesh_to_vtk(mesh)
- vtk_helper.vtk_mesh_is_valid(grid: pyvista.UnstructuredGrid, verbose: bool = False) VTKMeshValidity#
Run a vtk.CellValidator filter on the input grid.
- Parameters:
grid – A vtk mesh to validate.
verbose – Whether to print the complete validation.
- Returns:
A dataclass containing the results of the validator.
- Return type:
validity
- vtk_helper.vtk_update_coordinates(vtk_grid: pyvista.UnstructuredGrid, coordinates_array: numpy.ndarray)#
Update coordinates in vtk.
- vtk_helper.dpf_meshes_to_vtk(meshes_container: ansys.dpf.core.MeshesContainer, nodes: ansys.dpf.core.FieldsContainer = None, as_linear: bool = True) pyvista.UnstructuredGrid#
Return a pyvista UnstructuredGrid given a MeshedRegion.
- Parameters:
meshes_container – MeshesContainer to export to pyVista format.
nodes – FieldsContainer containing the node coordinates for each mesh (useful to get a deformed geometry). The labels must match a field to a mesh.
as_linear (bool, optional) – Export quadratic surface elements as linear.
- Returns:
UnstructuredGrid corresponding to the DPF meshes.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_meshes_to_vtk >>> model = dpf.Model(examples.download_all_kinds_of_complexity()) >>> meshes = dpf.operators.mesh.split_mesh( ... mesh=model.metadata.meshed_region, ... property=dpf.elements.elemental_properties.element_type ... ).eval() >>> grid = dpf_meshes_to_vtk(meshes)
- vtk_helper.dpf_field_to_vtk(field: ansys.dpf.core.Field, meshed_region: ansys.dpf.core.MeshedRegion = None, nodes: ansys.dpf.core.Field = None, as_linear: bool = True, field_name: str = '') pyvista.UnstructuredGrid#
Return a pyvista UnstructuredGrid given a DPF Field.
- Parameters:
field – Field to export to pyVista format.
meshed_region – Mesh to associate to the field. Useful for fluid results where the field is not automatically associated to its mesh.
nodes – Field containing the node coordinates of the mesh (useful to get a deformed geometry).
as_linear – Export quadratic surface elements as linear.
field_name – Override the default field name with this.
- Returns:
UnstructuredGrid corresponding to the DPF Field.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_field_to_vtk >>> model = dpf.Model(examples.find_simple_bar()) >>> field = model.results.displacement().eval()[0] >>> grid = dpf_field_to_vtk(field, field_name="displacement")
- vtk_helper.dpf_fieldscontainer_to_vtk(fields_container: ansys.dpf.core.FieldsContainer, meshes_container: ansys.dpf.core.MeshesContainer = None, nodes: ansys.dpf.core.Field = None, as_linear: bool = True, field_name: str = '') pyvista.UnstructuredGrid#
Return a pyvista UnstructuredGrid given a DPF FieldsContainer.
If the fields have different mesh supports, a global merged mesh support is created.
- Parameters:
fields_container – FieldsContainer to export to pyVista format.
meshes_container – MeshesContainer with meshes to associate to the fields in the FieldsContainer. Useful for fluid results where the fields are not automatically associated to their mesh.
nodes – Field containing the node coordinates of the mesh (useful to get a deformed geometry).
as_linear – Export quadratic surface elements as linear.
field_name – Override the default field name with this.
- Returns:
UnstructuredGrid corresponding to the DPF Field.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_field_to_vtk >>> model = dpf.Model(examples.download_transient_result()) >>> fc = model.results.displacement().eval() >>> grid = dpf_fieldscontainer_to_vtk(fc, field_name="displacement")
- vtk_helper.dpf_property_field_to_vtk(property_field: ansys.dpf.core.PropertyField, meshed_region: ansys.dpf.core.MeshedRegion, nodes: ansys.dpf.core.Field = None, as_linear: bool = True, field_name: str = '') pyvista.UnstructuredGrid#
Return a pyvista UnstructuredGrid given a DPF PropertyField.
- ..note:
Available starting with DPF 2024.2.pre1.
- Parameters:
property_field – PropertyField to export to pyVista format.
meshed_region – Mesh to associate to the property field.
nodes – Field containing the node coordinates of the mesh (useful to get a deformed geometry).
as_linear – Export quadratic surface elements as linear.
field_name – Override the default field name with this.
- Returns:
UnstructuredGrid corresponding to the DPF PropertyField.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_property_field_to_vtk >>> model = dpf.Model(examples.find_simple_bar()) >>> prop_field = model.metadata.meshed_region.property_field("eltype") >>> grid = dpf_property_field_to_vtk(prop_field, model.metadata.meshed_region)
- vtk_helper.append_field_to_grid(field: ansys.dpf.core.Field | ansys.dpf.core.PropertyField, meshed_region: ansys.dpf.core.MeshedRegion, grid: pyvista.UnstructuredGrid, field_name: str = '') pyvista.UnstructuredGrid#
Append field data to a VTK UnstructuredGrid based on a MeshedRegion.
- Parameters:
field – Field to append to the VTK UnstructuredGrid.
meshed_region – MeshedRegion corresponding to the grid.
grid – VTK UnstructuredGrid to append the field data to.
field_name – Override the default field name with this.
- Returns:
Updated UnstructuredGrid with the field data appended.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_mesh_to_vtk >>> model = dpf.Model(examples.find_simple_bar()) >>> mesh = model.metadata.meshed_region >>> grid = dpf_mesh_to_vtk(mesh) >>> field = model.results.displacement().eval()[0] >>> grid = append_field_to_grid(field, mesh, grid, field_name="displacement")
- vtk_helper.append_fieldscontainer_to_grid(fields_container: ansys.dpf.core.FieldsContainer, meshed_region: ansys.dpf.core.MeshedRegion, grid: pyvista.UnstructuredGrid, field_name: str = '') pyvista.UnstructuredGrid#
Append fields data to a VTK UnstructuredGrid based on a MeshedRegion.
- Parameters:
fields_container – FieldsContainer to append to the VTK UnstructuredGrid.
meshed_region – MeshedRegion corresponding to the grid.
grid – VTK UnstructuredGrid to append the fields data to.
field_name – Override the default field name with this. The final field name is a combination of this base name and the label space of the field.
- Returns:
Updated UnstructuredGrid with the fields data appended.
- Return type:
grid
Examples
>>> import ansys.dpf.core as dpf >>> from ansys.dpf.core import examples >>> from ansys.dpf.core.vtk_helper import dpf_mesh_to_vtk, append_fieldscontainer_to_grid >>> model = dpf.Model(examples.find_simple_bar()) >>> mesh = model.metadata.meshed_region >>> grid = dpf_mesh_to_vtk(mesh) >>> fc = model.results.displacement().eval() >>> grid = append_fieldscontainer_to_grid(fc, mesh, grid, field_name="displacement")
- vtk_helper.VTK9#
- vtk_helper.VTK_MAPPING#
- vtk_helper.VTK_LINEAR_MAPPING#