.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\plot\plot_deformed_mesh.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_plot_plot_deformed_mesh.py: .. _ref_tutorials_plot_deformed_mesh: Plot with mesh deformation ========================== This tutorial shows different commands for plotting a deformed mesh without data. A mesh is represented in DPF by a :class:`MeshedRegion`. You can store multiple ``MeshedRegion`` objects in a DPF collection called :class:`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 :ref:`ref_tutorials_create_a_mesh_from_scratch` and :ref:`ref_tutorials_get_mesh_from_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. .. GENERATED FROM PYTHON SOURCE LINES 46-48 Load data to plot ------------------ .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python import ansys.dpf.core as dpf from ansys.dpf.core import examples, 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) .. GENERATED FROM PYTHON SOURCE LINES 59-80 Get the deformation field -------------------------- To deform the mesh, a nodal 3D vector field specifying the translation of each node in the mesh is needed. The following DPF objects can represent such a field and are accepted as inputs for the ``deform_by`` parameter of all plot methods: - A :class:`Field` - A :class:`FieldsContainer` - A result object - An :class:`Operator` Here, we use the :py:class:`displacement` operator, which outputs a nodal 3D vector field of distances. One can get the operator from the :class:`Model` with the data source already connected. For more information about extracting results from a result file, see the :ref:`ref_tutorials_import_data` tutorials section. .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. code-block:: Python disp_op = model_1.results.displacement() # Define the scale factor to apply to the deformation scl_fct = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 87-103 .. _ref_plot_deformed_mesh_with_model: Plot a deformed model ---------------------- Plot the overall mesh loaded by the model with :py:meth:`Model.plot()`. To add deformation, pass the displacement operator to the ``deform_by`` argument. .. note:: The :class:`DpfPlotter` displays the mesh with edges, lighting and axis widget enabled by default. You can pass additional PyVista arguments to all plotting methods to change the default behavior (see options for `pyvista.plot() `_). .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: Python model_1.plot(deform_by=disp_op, scale_factor=scl_fct) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_001.png :alt: plot deformed mesh :srcset: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 107-114 .. _ref_plot_deformed_mesh_with_meshed_region: Plot a single mesh ------------------- Get the mesh ^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python meshed_region_1 = model_1.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 118-124 Plot the deformed mesh using ``MeshedRegion.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the :py:meth:`MeshedRegion.plot()` method and pass the displacement operator to the ``deform_by`` argument. .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: Python meshed_region_1.plot(deform_by=disp_op, scale_factor=scl_fct) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_002.png :alt: plot deformed mesh :srcset: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 128-138 Plot the deformed mesh using ``DpfPlotter`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1. Create an instance of :class:`DpfPlotter`. 2. Add the mesh to the scene using :py:meth:`add_mesh()`, passing the displacement operator to the ``deform_by`` argument. 3. Render and show the figure using :py:meth:`show_figure()`. .. GENERATED FROM PYTHON SOURCE LINES 138-143 .. code-block:: Python plotter_1 = dpf.plotter.DpfPlotter() plotter_1.add_mesh(meshed_region=meshed_region_1, deform_by=disp_op, scale_factor=scl_fct) plotter_1.show_figure() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_003.png :alt: plot deformed mesh :srcset: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 144-146 You can also plot data contours on a deformed mesh. For more information, see :ref:`ref_tutorials_plot_contour`. .. GENERATED FROM PYTHON SOURCE LINES 148-164 .. _ref_plot_deformed_mesh_with_meshes_container: Plot several meshes -------------------- Build a collection of meshes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the :class:`split_mesh` operator to split the mesh based on the material of each element. This operator returns a :class:`MeshesContainer` with meshes labeled according to the split criterion. For more information, see the :ref:`ref_tutorials_split_mesh` and :ref:`ref_tutorials_extract_mesh_in_split_parts` tutorials. .. GENERATED FROM PYTHON SOURCE LINES 164-168 .. code-block:: Python meshes = ops.mesh.split_mesh(mesh=meshed_region_1, property="mat").eval() print(meshes) .. rst-class:: sphx-glr-script-out .. code-block:: none 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. .. GENERATED FROM PYTHON SOURCE LINES 169-177 Plot the deformed meshes ^^^^^^^^^^^^^^^^^^^^^^^^^ Use :py:meth:`MeshesContainer.plot()` and pass the displacement operator to ``deform_by``. This plots all ``MeshedRegion`` objects in the ``MeshesContainer`` and colors them based on the split criterion. .. GENERATED FROM PYTHON SOURCE LINES 177-180 .. code-block:: Python meshes.plot(deform_by=disp_op, scale_factor=scl_fct) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_004.png :alt: plot deformed mesh :srcset: /tutorials/plot/images/sphx_glr_plot_deformed_mesh_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 181-183 You can also plot data on a collection of deformed meshes. For more information, see :ref:`ref_tutorials_plot_contour`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.943 seconds) .. _sphx_glr_download_tutorials_plot_plot_deformed_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_deformed_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_deformed_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_deformed_mesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_