|
| 1 | +.. _ref_tutorials_plot_mesh_scopings: |
| 2 | + |
| 3 | +================== |
| 4 | +Plot mesh scopings |
| 5 | +================== |
| 6 | + |
| 7 | +.. include:: ../../../links_and_refs.rst |
| 8 | + |
| 9 | +This tutorial shows different commands for plotting mesh entities targeted by mesh scopings. |
| 10 | + |
| 11 | +A mesh scoping is a |Scoping| with a location related to mesh entities. |
| 12 | + |
| 13 | +PyDPF-Core has a variety of plotting methods for generating 3D plots with Python. |
| 14 | +These methods use VTK and leverage the `PyVista <https://github.com/pyvista/pyvista>`_ library. |
| 15 | + |
| 16 | +:jupyter-download-script:`Download tutorial as Python script<plot_mesh_scopings>` |
| 17 | +:jupyter-download-notebook:`Download tutorial as Jupyter notebook<plot_mesh_scopings>` |
| 18 | + |
| 19 | +Load data to plot |
| 20 | +----------------- |
| 21 | + |
| 22 | +For this tutorial, we use mesh information from a case available in the |Examples| module. |
| 23 | +For more information see the :ref:`ref_tutorials_get_mesh_from_result_file` tutorial. |
| 24 | + |
| 25 | +.. jupyter-execute:: |
| 26 | + |
| 27 | + # Import the ``ansys.dpf.core`` module |
| 28 | + import ansys.dpf.core as dpf |
| 29 | + # Import the examples module |
| 30 | + from ansys.dpf.core import examples |
| 31 | + |
| 32 | + # Download and get the path to an example result file |
| 33 | + result_file_path_1 = examples.download_piston_rod() |
| 34 | + |
| 35 | + # Create a model from the result file |
| 36 | + model_1 = dpf.Model(data_sources=result_file_path_1) |
| 37 | + |
| 38 | + # Get the mesh of the model |
| 39 | + mesh_1 = model_1.metadata.meshed_region |
| 40 | + |
| 41 | +Plot a single mesh scoping |
| 42 | +-------------------------- |
| 43 | + |
| 44 | +Create a single |Scoping| and plot the targeted entities when applied to a single |MeshedRegion|. |
| 45 | + |
| 46 | +.. jupyter-execute:: |
| 47 | + |
| 48 | + node_scoping = core.Scoping(location=core.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 49 | + node_scoping.plot(mesh=mesh_1, color="red") |
| 50 | + element_scoping = core.Scoping( |
| 51 | + location=core.locations.elemental, ids=mesh_1.elements.scoping.ids[0:100] |
| 52 | + ) |
| 53 | + element_scoping.plot(mesh=mesh_1, color="green") |
| 54 | + |
| 55 | + |
| 56 | +Plot a collection of mesh scopings |
| 57 | +---------------------------------- |
| 58 | + |
| 59 | +First create a |ScopingsContainer| with several mesh scopings |
| 60 | +and plot targeted entities of a |MeshedRegion|. |
| 61 | + |
| 62 | +.. jupyter-execute:: |
| 63 | + |
| 64 | + node_scoping_1 = core.Scoping(location=core.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 65 | + node_scoping_2 = core.Scoping( |
| 66 | + location=core.locations.nodal, ids=mesh_1.nodes.scoping.ids[300:400] |
| 67 | + ) |
| 68 | + node_sc = core.ScopingsContainer() |
| 69 | + node_sc.add_label(label="scoping", default_value=1) |
| 70 | + node_sc.add_scoping(label_space={"scoping": 1}, scoping=node_scoping_1) |
| 71 | + node_sc.add_scoping(label_space={"scoping": 2}, scoping=node_scoping_2) |
| 72 | + node_sc.plot(mesh=mesh_1, show_mesh=True) |
| 73 | + |
| 74 | +Then plot the |ScopingsContainer| applied to a |MeshesContainer| with similarly labeled meshes. |
| 75 | + |
| 76 | +.. jupyter-execute:: |
| 77 | + |
| 78 | + meshes: dpf.MeshesContainer = ops.mesh.split_mesh(mesh=mesh_1, property="mat").eval() |
| 79 | + |
| 80 | + label_space = {"mat": 1, "body": 1} |
| 81 | + node_scoping_3 = core.Scoping( |
| 82 | + location=core.locations.nodal, ids=meshes.get_mesh(label_space).nodes.scoping.ids[0:100] |
| 83 | + ) |
| 84 | + node_sc_2 = core.ScopingsContainer() |
| 85 | + node_sc_2.add_label(label="mat") |
| 86 | + node_sc_2.add_label(label="body") |
| 87 | + node_sc_2.add_scoping(label_space=label_space, scoping=node_scoping_3) |
| 88 | + node_sc_2.plot(mesh=meshes) |
| 89 | + |
| 90 | +Use DpfPlotter.add_scoping |
| 91 | +-------------------------- |
| 92 | + |
| 93 | +We now use the |DpfPlotter| to add scopings applied to |MeshedRegion| to a scene. |
| 94 | + |
| 95 | +.. jupyter-execute:: |
| 96 | + |
| 97 | + node_scoping = core.Scoping(location=core.locations.nodal, ids=mesh_1.nodes.scoping.ids[0:100]) |
| 98 | + element_scoping = core.Scoping( |
| 99 | + location=core.locations.elemental, ids=mesh_1.elements.scoping.ids[0:100] |
| 100 | + ) |
| 101 | + |
| 102 | + from ansys.dpf.core.plotter import DpfPlotter |
| 103 | + |
| 104 | + plt = DpfPlotter() |
| 105 | + plt.add_scoping(node_scoping, mesh_1, show_mesh=True, color="red") |
| 106 | + plt.add_scoping(element_scoping, mesh_1, color="green") |
| 107 | + plt.show_figure() |
0 commit comments