Mechanical 2022R2 DPF VTK Export of Assembly & Plotting Parts.
Can we add something to the Mechanical DPF "VTK Export" operation so that it is easy to plot individual parts if wanted. For example if we were using PyVista.
Answers
-
This is possible by adding an identifier to the VTK Export such as the element material ID number. As an example we can use the Vise geometry that is included in the Ansys installation and which has 12 parts. There will be 36 contact pairs in the model for a total of 48 material IDs:
Using the Mechanical Scripting tool we can define a Field and fill it in with the internal element and material ID:
import mech_dpf import Ans.DataProcessing as dpf analysis = ExtAPI.DataModel.Project.Model.Analyses[0] mech_dpf.setExtAPI(ExtAPI) dataSource = dpf.DataSources(analysis.ResultFileName) model =dpf.Model(dataSource) mesh = model.Mesh path = str("D:\\Temp\\") e_field = dpf.FieldsFactory.CreateScalarField(12237, "Elemental") for item in range(0,12237): a = [float(mesh.GetPropertyField("mat").Data[item])] b = mesh.ElementScoping.Ids[item] e_field.Add(b, a)
The VonMises stress will be gathered and it along with the 'e_field' can then be written to a VTK file:
vm_op = dpf.operators.result.stress_von_mises() vm_op.inputs.data_sources.Connect(dataSource) vm_fc = vm_op.outputs.fields_container.GetData() vm_op.Run() vtk_op = dpf.operators.serialization.vtk_export() # operator instantiation vtk_op.inputs.file_path.Connect(path + "seqv.vtk") vtk_op.inputs.fields1.Connect(vm_fc) vtk_op.inputs.fields2.Connect(e_field) vtk_op.Run()
Moving to an Python IDE like VSC we can read in the VTK and plot the result:
import pyvista as pv import numpy as np path = str("D:\\Temp\\") part_1 = pv.read(path + 'seqv.vtk') part_1.plot(scalars = "2_stress_1.s_eqv_(psi)") print(part_1.array_names)
The list of arrays in the VTK file is:
['node_id', '2_stress_1.s_eqv_(psi)', 'element_id', '3_DimensionLess()']'3_DimensionLess()' is the name of the array of material IDs.
The resulting plot:
The second part of the assembly is the SlidingJaw. To plot just it a numpy array of all the other material IDs can be created (here named ghosts), and the PyVista "remove cell" used to remove them from the plot:
ghosts = np.argwhere(part_1["3_DimensionLess()"] != 2.0 ) part_1 = part_1.remove_cells(ghosts) print(part_1.array_names) part_1.plot(scalars = "2_stress_1.s_eqv_(psi)")
This will add vtkGhostType array to our VTK object:
['node_id', '2_stress_1.s_eqv_(psi)', 'element_id', '3_DimensionLess()', 'vtkGhostType']
Then plotting just the single part show with the scalar bar (legend) updated:
7 -
excellent colormaps!
0 -
Sorry I just realized that I left in a 'magic number' on lines 14 & 15 of the fist shown code snippet. Creating the size of the element field and looping over all the elements. The number 12237 can be replaced by
len(mesh.Elements)
.
Mike0 -
@Mike Rife , can you comment about what VTK files are and how they are used? Can they be used in the ANSYS Viewer, in ppt files, html files etc…? Do they require any license or additional software to use?
0 -
@Mike.Thompson VTK is the Visualization Toolkit, an open source scientific data manipulation/visualization tool. It's cross platform and supports several different languages. PyVista is an open source streamlined interface to VTK for 3D plotting and mesh analysis. You can think of PyVista is to VTK as PyDPF Post is to PyDPF Core.
PyVista is used by both PyMAPDL and PyDPF in interactive visualization of meshes and data.
Do a search for VTK in the Ansys help...you might be surprised.
1