|
| 1 | +.. _ref_tutorials_plot_mesh_scopings: |
| 2 | + |
| 3 | +================== |
| 4 | +Plot mesh scopings |
| 5 | +================== |
| 6 | + |
| 7 | +.. include:: ../../../links_and_refs.rst |
| 8 | +.. |add_scoping| replace:: :py:meth:`DpfPlotter.add_scoping() <ansys.dpf.core.plotter.DpfPlotter.add_scoping>` |
| 9 | + |
| 10 | +This tutorial shows different commands for plotting mesh entities targeted by mesh scopings. |
| 11 | + |
| 12 | +A mesh scoping is a |Scoping| with a location related to mesh entities. |
| 13 | + |
| 14 | +The entities shown correspond to the intersection of the IDs in the scoping of the mesh |
| 15 | +and the IDs in the provided scoping. |
| 16 | + |
| 17 | +If the scoping and the mesh do not have entity IDs in common, nothing is shown. |
| 18 | + |
| 19 | +For example, a scoping on elements associated to a mesh without elements results in an empty plot. |
| 20 | + |
| 21 | +A scoping on node IDs 1 to 2 associated to a mesh whose node IDs start at 3 |
| 22 | +results in an empty plot. |
| 23 | + |
| 24 | +.. note:: |
| 25 | + |
| 26 | + Scopings of faces are not supported. |
| 27 | + |
| 28 | +PyDPF-Core has a variety of plotting methods for generating 3D plots with Python. |
| 29 | +These methods use VTK and leverage the `PyVista <https://github.com/pyvista/pyvista>`_ library. |
| 30 | + |
| 31 | +:jupyter-download-script:`Download tutorial as Python script<plot_mesh_scopings>` |
| 32 | +:jupyter-download-notebook:`Download tutorial as Jupyter notebook<plot_mesh_scopings>` |
| 33 | + |
| 34 | +Load data to plot |
| 35 | +----------------- |
| 36 | + |
| 37 | +For this tutorial, we use mesh information from a case available in the |Examples| module. |
| 38 | +For more information see the :ref:`ref_tutorials_get_mesh_from_result_file` tutorial. |
| 39 | + |
| 40 | +.. jupyter-execute:: |
| 41 | + |
| 42 | + # Import the ``ansys.dpf.core`` module |
| 43 | + import ansys.dpf.core as dpf |
| 44 | + # Import the examples module |
| 45 | + from ansys.dpf.core import examples |
| 46 | + # Import the operator module |
| 47 | + import ansys.dpf.core.operators as ops |
| 48 | + |
| 49 | + # Download and get the path to an example result file |
| 50 | + result_file_path_1 = examples.download_piston_rod() |
| 51 | + |
| 52 | + # Create a model from the result file |
| 53 | + model_1 = dpf.Model(data_sources=result_file_path_1) |
| 54 | + |
| 55 | + # Get the mesh of the model |
| 56 | + mesh_1 = model_1.metadata.meshed_region |
| 57 | + |
| 58 | +Plot a single mesh scoping |
| 59 | +-------------------------- |
| 60 | + |
| 61 | +Create a single |Scoping| and plot the targeted entities when applied to a single |MeshedRegion|. |
| 62 | + |
| 63 | +First for a node scoping: |
| 64 | + |
| 65 | +.. jupyter-execute:: |
| 66 | + |
| 67 | + # Create a scoping of the first 100 node IDs of the mesh |
| 68 | + node_scoping = dpf.Scoping(location=dpf.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 69 | + # Plot the node scoping applied to the mesh, with nodes shown as red dots |
| 70 | + node_scoping.plot(mesh=mesh_1, color="red", show_mesh=True) |
| 71 | + |
| 72 | +Then for an element scoping: |
| 73 | + |
| 74 | +.. jupyter-execute:: |
| 75 | + |
| 76 | + # Create a scoping of the first 100 elements IDs of the mesh |
| 77 | + element_scoping = dpf.Scoping( |
| 78 | + location=dpf.locations.elemental, ids=mesh_1.elements.scoping.ids[0:100] |
| 79 | + ) |
| 80 | + # Plot the element scoping applied to the mesh, with elements shown in green |
| 81 | + element_scoping.plot(mesh=mesh_1, color="green", show_mesh=True) |
| 82 | + |
| 83 | + |
| 84 | +Plot a collection of mesh scopings |
| 85 | +---------------------------------- |
| 86 | + |
| 87 | +First create a |ScopingsContainer| with several mesh scopings |
| 88 | +and plot targeted entities of a |MeshedRegion|. |
| 89 | + |
| 90 | +.. jupyter-execute:: |
| 91 | + |
| 92 | + # Create a scoping of the first 100 node IDs of the mesh |
| 93 | + node_scoping_1 = dpf.Scoping(location=dpf.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 94 | + # Create a scoping of the 300th to 400th node IDs of the mesh |
| 95 | + node_scoping_2 = dpf.Scoping( |
| 96 | + location=dpf.locations.nodal, ids=mesh_1.nodes.scoping.ids[300:400] |
| 97 | + ) |
| 98 | + # Create a ScopingsContainer |
| 99 | + node_sc = dpf.ScopingsContainer() |
| 100 | + # Add at least one label to the collection to identify entries |
| 101 | + node_sc.add_label(label="scoping", default_value=1) |
| 102 | + # Add the first node scoping to the collection |
| 103 | + node_sc.add_scoping(label_space={"scoping": 1}, scoping=node_scoping_1) |
| 104 | + # Add the second node scoping to the collection |
| 105 | + node_sc.add_scoping(label_space={"scoping": 2}, scoping=node_scoping_2) |
| 106 | + # Plot the scoping collection applied to the mesh |
| 107 | + node_sc.plot(mesh=mesh_1, show_mesh=True) |
| 108 | + |
| 109 | +Then plot the |ScopingsContainer| applied to a |MeshesContainer| with similarly labeled meshes. |
| 110 | + |
| 111 | +.. jupyter-execute:: |
| 112 | + |
| 113 | + # Create a collection of meshes based on the initial mesh by splitting it by material |
| 114 | + meshes: dpf.MeshesContainer = ops.mesh.split_mesh(mesh=mesh_1, property="mat").eval() |
| 115 | + # Create a node scoping targeting the first 100 node IDs of the mesh for material 1 |
| 116 | + node_scoping_3 = dpf.Scoping( |
| 117 | + location=dpf.locations.nodal, |
| 118 | + ids=meshes.get_mesh({"mat": 1, "body": 1}).nodes.scoping.ids[0:100], |
| 119 | + ) |
| 120 | + # Create a node scoping targeting the first 100 node IDs of the mesh for material 2 |
| 121 | + node_scoping_4 = dpf.Scoping( |
| 122 | + location=dpf.locations.nodal, |
| 123 | + ids=meshes.get_mesh({"mat": 2, "body": 2}).nodes.scoping.ids[0:100], |
| 124 | + ) |
| 125 | + # Create a collection of scopings |
| 126 | + node_sc_2 = dpf.ScopingsContainer() |
| 127 | + # Add the appropriate labels to the collection |
| 128 | + node_sc_2.add_label(label="mat") |
| 129 | + node_sc_2.add_label(label="body") |
| 130 | + # Add the scoping associated to material 1 |
| 131 | + node_sc_2.add_scoping(label_space={"mat": 1, "body": 1}, scoping=node_scoping_3) |
| 132 | + # Add the scoping associated to material 2 |
| 133 | + node_sc_2.add_scoping(label_space={"mat": 2, "body": 2}, scoping=node_scoping_4) |
| 134 | + # Plot the collection of scopings applied to the collection of meshes |
| 135 | + node_sc_2.plot(mesh=meshes) |
| 136 | + |
| 137 | +Use DpfPlotter.add_scoping |
| 138 | +-------------------------- |
| 139 | + |
| 140 | +We now use the |add_scoping| to add scopings applied to |MeshedRegion| to a scene. |
| 141 | + |
| 142 | +.. jupyter-execute:: |
| 143 | + |
| 144 | + # Create a node scoping for the first 100 node IDs of the mesh |
| 145 | + node_scoping = dpf.Scoping(location=dpf.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 146 | + # Create an element scoping for the first 100 elements IDs of the mesh |
| 147 | + element_scoping = dpf.Scoping( |
| 148 | + location=dpf.locations.elemental, ids=mesh_1.elements.scoping.ids[0:100] |
| 149 | + ) |
| 150 | + |
| 151 | + # Import the DpfPlotter |
| 152 | + from ansys.dpf.core.plotter import DpfPlotter |
| 153 | + |
| 154 | + # Instantiate a DpfPlotter |
| 155 | + plt = DpfPlotter() |
| 156 | + # Tell the plotter to also show the mesh associated with the first scoping |
| 157 | + plt.add_scoping(node_scoping, mesh_1, show_mesh=True, color="red") |
| 158 | + # Do not show the mesh for the second scoping as it is the same |
| 159 | + plt.add_scoping(element_scoping, mesh_1, color="green") |
| 160 | + # Show the resulting scene |
| 161 | + plt.show_figure() |
0 commit comments