.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\plot\plot_contour.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_contour.py: .. _ref_tutorials_plot_contour: Plot contours ============= This tutorial shows different commands for plotting data contours on meshes. 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 36-41 Load data to plot ------------------ Load a result file in a model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python import ansys.dpf.core as dpf from ansys.dpf.core import examples, operators as ops result_file_path_1 = examples.download_piston_rod() model_1 = dpf.Model(data_sources=result_file_path_1) .. GENERATED FROM PYTHON SOURCE LINES 49-60 Extract data for the contour ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ For more information about extracting results from a result file, see the :ref:`ref_tutorials_import_data` tutorials section. .. note:: Only the ``elemental`` or ``nodal`` locations are supported for plotting. Here, we choose to plot the XX component of the stress tensor. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python stress_XX_op = ops.result.stress_X(data_sources=model_1) # The default behavior is to return data as ElementalNodal print(stress_XX_op.eval()) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF stress(s)Fields Container with 1 field(s) defined on labels: time with: - field 0 {time: 3} with Nodal location, 1 components and 33337 entities. .. GENERATED FROM PYTHON SOURCE LINES 67-72 Request the stress in a ``nodal`` location (the default ``ElementalNodal`` location is not supported for plotting). We define the new location using the operator input. Another option would be using the :class:`to_nodal_fc` averaging operator on the output of the stress operator. .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. code-block:: Python stress_XX_op.inputs.requested_location(dpf.locations.nodal) stress_XX_fc = stress_XX_op.eval() .. GENERATED FROM PYTHON SOURCE LINES 77-79 Extract the mesh ^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python meshed_region_1 = model_1.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 83-97 Plot a contour of a single field --------------------------------- There are three methods to plot a single :class:`Field`: - :py:meth:`Field.plot()` - :py:meth:`MeshedRegion.plot()` with the field as argument - :class:`DpfPlotter` with :py:meth:`add_field()` (more performant) Get a single field from the ``FieldsContainer``. .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python stress_XX = stress_XX_fc[0] .. GENERATED FROM PYTHON SOURCE LINES 101-108 Plot using ``Field.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the :class:`Field` does not have an associated mesh support (see :py:attr:`Field.meshed_region`), provide a mesh with the ``meshed_region`` argument. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: Python stress_XX.plot(meshed_region=meshed_region_1) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_001.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (None, ) .. GENERATED FROM PYTHON SOURCE LINES 112-116 Plot using ``MeshedRegion.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the ``field_or_fields_container`` argument to pass the field. .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: Python meshed_region_1.plot(field_or_fields_container=stress_XX) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_002.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (None, ) .. GENERATED FROM PYTHON SOURCE LINES 120-135 Plot using ``DpfPlotter`` ^^^^^^^^^^^^^^^^^^^^^^^^^^ 1. Create an instance of :class:`DpfPlotter`. 2. Add the field using :py:meth:`add_field()`. If the field has no associated mesh support, provide a mesh with the ``meshed_region`` argument. 3. Render and show the figure using :py:meth:`show_figure()`. You can also first call :py:meth:`add_mesh()` to add the mesh and then call ``add_field()`` without the ``meshed_region`` argument. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python plotter_1 = dpf.plotter.DpfPlotter() plotter_1.add_field(field=stress_XX, meshed_region=meshed_region_1) plotter_1.show_figure() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_003.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 141-164 Plot a contour of multiple fields ---------------------------------- Prepare a collection of fields ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. warning:: The fields should not have conflicting data — you cannot build a contour for two fields with two different sets of data for the same mesh entities (intersecting scopings). These methods are therefore not available for a collection of fields varying across time, or for different shell layers of the same elements. Here we split the field for XX stress based on material to get a collection of fields with non-conflicting associated mesh entities. We use the :class:`split_fields` operator together with the :class:`split_mesh` operator. For MAPDL results, a split on material is equivalent to a split on bodies. .. GENERATED FROM PYTHON SOURCE LINES 164-173 .. code-block:: Python fields = ( ops.mesh.split_fields( field_or_fields_container=stress_XX_fc, meshes=ops.mesh.split_mesh(mesh=meshed_region_1, property="mat"), ) ).eval() print(fields) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Fields Container with 2 field(s) defined on labels: body mat time with: - field 0 {mat: 1, body: 1, time: 3} with Nodal location, 1 components and 17281 entities. - field 1 {mat: 2, body: 2, time: 3} with Nodal location, 1 components and 17610 entities. .. GENERATED FROM PYTHON SOURCE LINES 174-179 Plot the contour using ``FieldsContainer.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use :py:meth:`FieldsContainer.plot()`. .. GENERATED FROM PYTHON SOURCE LINES 179-182 .. code-block:: Python fields.plot() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_004.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 183-184 The ``label_space`` argument provides further field filtering capabilities. .. GENERATED FROM PYTHON SOURCE LINES 184-187 .. code-block:: Python fields.plot(label_space={"mat": 1}) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_005.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 188-192 Plot the contour using ``MeshedRegion.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the ``field_or_fields_container`` argument. .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: Python meshed_region_1.plot(field_or_fields_container=fields) .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_006.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (None, ) .. GENERATED FROM PYTHON SOURCE LINES 196-201 Plot the contour using ``DpfPlotter`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add each field individually using :py:meth:`add_field()`. .. GENERATED FROM PYTHON SOURCE LINES 201-206 .. code-block:: Python plotter_2 = dpf.plotter.DpfPlotter() plotter_2.add_field(field=fields[0]) plotter_2.add_field(field=fields[1]) plotter_2.show_figure() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_contour_007.png :alt: plot contour :srcset: /tutorials/plot/images/sphx_glr_plot_contour_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 14.088 seconds) .. _sphx_glr_download_tutorials_plot_plot_contour.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_contour.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_contour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_contour.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_