Plot with mesh deformation#

This tutorial shows different commands for plotting a deformed mesh without data.

A mesh is represented in DPF by a MeshedRegion object. You can store multiple MeshedRegion in a DPF collection called MeshesContainer.

You can obtain a MeshedRegion by creating your own from scratch or by getting it from a result file. For more information, see the Create a mesh from scratch and Get a mesh from a result file tutorials.

PyDPF-Core has a variety of plotting methods for generating 3D plots with Python. These methods use VTK and leverage the PyVista library.

Download tutorial as Python script Download tutorial as Jupyter notebook

Load data to plot#

For this tutorial, we use mesh information from a case available in the Examples module. For more information see the Get a mesh from a result file tutorial.

# Import the ``ansys.dpf.core`` module
import ansys.dpf.core as dpf
# Import the examples module
from ansys.dpf.core import examples
# Import the operators module
from ansys.dpf.core import operators as ops

# Download and get the path to an example result file
result_file_path_1 = examples.download_piston_rod()

# Create a model from the result file
model_1 = dpf.Model(data_sources=result_file_path_1)

Get the deformation field#

To deform the mesh, we need a nodal 3D vector field specifying the translation of each node in the mesh.

The following DPF objects are able to return or represent such a field and are accepted inputs for the deformation parameter of plot methods:

Here, we use the displacement operator which outputs a nodal 3D vector field of distances.

One can get the operator from the Model with the source of data already connected. For more information about extracting results from a result file, see the Import Data tutorials section.

# Get the displacement operator for this model
disp_op = model_1.results.displacement()

You can apply a scale factor to the deformation for every method in this tutorial by passing in the scale_factor argument.

# Define the scale factor
scl_fct = 2.0

Plot a deformed model#

You can directly plot the overall mesh loaded by the model with Model.plot() [1]. To plot it with deformation, use the ‘deform_by’ argument and provide the displacement operator.

# Plot the deformed mesh
model_1.plot(deform_by=disp_op, scale_factor=scl_fct)
../../../_images/plot_deformed_mesh_3_0.png
(None, <pyvista.plotting.plotter.Plotter at 0x21fcc6deff0>)

You can apply a scale factor to the deformation for every method in this tutorial.

# Define the scale factor
scl_fct = 2.0

Plot a single mesh#

Get the mesh#

Here we simply get the MeshedRegion object of the model, but any other MeshedRegion works.

# Extract the mesh
meshed_region_1 = model_1.metadata.meshed_region

Plot the mesh#

To plot the deformed MeshedRegion you can use:

  • The MeshedRegion.plot() method;

  • The DpfPlotter object.

Use the MeshedRegion.plot() method [1] of the MeshedRegion object we defined. Add deformation by providing our displacement operator to the ‘deform_by’ argument.

# Plot the deformed mesh
meshed_region_1.plot(deform_by=disp_op, scale_factor=scl_fct)
../../../_images/plot_deformed_mesh_6_0.png
(None, <pyvista.plotting.plotter.Plotter at 0x21fd1307650>)

To plot the mesh with this approach, first create an instance of DpfPlotter [2]. Then, add the MeshedRegion to the scene using the add_mesh() method. Add deformation by providing our displacement operator to the ‘deform_by’ argument.

To render and show the figure based on the current state of the plotter object, use the show_figure() method.

# Create a DpfPlotter instance
plotter_1 = dpf.plotter.DpfPlotter()

# Add the mesh to the scene with deformation
plotter_1.add_mesh(meshed_region=meshed_region_1,
                   deform_by=disp_op,
                   scale_factor=scl_fct)

# Display the scene
plotter_1.show_figure()
../../../_images/plot_deformed_mesh_7_0.png
(None, <pyvista.plotting.plotter.Plotter at 0x21fd1307b00>)

You can also plot data contours on a deformed mesh. For more information, see Plot contours

Plot several meshes#

Build a collection of meshes#

There are different ways to obtain a MeshesContainer. You can for example split a MeshedRegion using operators.

Here, we use the split_mesh operator to split the mesh based on the material of each element. This operator returns a MeshesContainer with meshes labeled according to the criterion for the split. In our case, each mesh has a ‘mat’ label. For more information about how to get a split mesh, see the Split a mesh and Extract a mesh in split parts tutorials.

# Split the mesh based on material property
meshes = ops.mesh.split_mesh(mesh=meshed_region_1, property="mat").eval()

# Show the result
print(meshes)
DPF  Meshes Container
  with 2 mesh(es)
  defined on labels: body mat 

  with:
  - mesh 0 {mat:  1, body:  1, } with 17281 nodes and 9026 elements.
  - mesh 1 {mat:  2, body:  2, } with 17610 nodes and 9209 elements.

Plot the meshes#

Use the MeshesContainer.plot() method [1] of the MeshesContainer object we defined. Provide the displacement operator to the ‘deform_by’ argument to add mesh deformation.

This method plots all the MeshedRegion objects stored in the MeshesContainer and colors them based on the property used to split the mesh.

# Plot the deformed mesh
meshes.plot(deform_by=disp_op, scale_factor=scl_fct)
../../../_images/plot_deformed_mesh_9_0.png
(None, <pyvista.plotting.plotter.Plotter at 0x21fd1321c40>)

You can also plot data on a collection of deformed meshes. For more information, see _ref_tutorials_plot_contour

Footnotes