|
3 | 3 |
|
4 | 4 | Explore the mesh |
5 | 5 | ================ |
6 | | -In this script a static simulation is used as an example to show how to |
7 | | -query mesh information such as connectivity, element IDs, element types and so on. |
| 6 | +This example shows how to explore and manipulate the mesh object to query mesh data |
| 7 | +such as connectivity tables, element IDs, element types and so on. |
8 | 8 | """ |
9 | 9 |
|
10 | 10 | ############################################################################### |
11 | 11 | # Perform required imports |
12 | 12 | # ------------------------ |
13 | | -# Perform required imports. # This example uses a supplied file that you can |
| 13 | +# Perform required imports. |
| 14 | +# This example uses a supplied file that you can |
14 | 15 | # get by importing the DPF ``examples`` package. |
15 | 16 |
|
16 | 17 | from ansys.dpf import post |
17 | 18 | from ansys.dpf.post import examples |
18 | 19 | from ansys.dpf.post.common import elemental_properties |
19 | 20 |
|
20 | 21 | ############################################################################### |
21 | | -# Get ``Simulation`` object |
22 | | -# ------------------------- |
23 | | -# Get the ``Simulation`` object that allows access to the result. The ``Simulation`` |
24 | | -# object must be instantiated with the path for the result file. For example, |
25 | | -# ``"C:/Users/user/my_result.rst"`` on Windows or ``"/home/user/my_result.rst"`` |
26 | | -# on Linux. |
27 | | - |
28 | | -example_path = examples.download_all_kinds_of_complexity() |
29 | | -simulation = post.StaticMechanicalSimulation(example_path) |
| 22 | +# Load the result file |
| 23 | +# -------------------- |
| 24 | +# Load the result file in a ``Simulation`` object that allows access to the results. |
| 25 | +# The ``Simulation`` object must be instantiated with the path for the result file. |
| 26 | +# For example, ``"C:/Users/user/my_result.rst"`` on Windows |
| 27 | +# or ``"/home/user/my_result.rst"`` on Linux. |
30 | 28 |
|
31 | | -# print the simulation to get an overview of what's available |
32 | | -print(simulation) |
| 29 | +example_path = examples.download_harmonic_clamped_pipe() |
| 30 | +simulation = post.HarmonicMechanicalSimulation(example_path) |
33 | 31 |
|
34 | 32 | ############################################################################### |
35 | 33 | # Get the mesh |
36 | 34 | # ------------ |
37 | | - |
38 | | -# Retrieve the actual mesh |
| 35 | +# Retrieve the mesh |
39 | 36 | mesh = simulation.mesh |
| 37 | +# Printing the mesh directly gives the same information already shown at the simulation level |
| 38 | +print(mesh) |
40 | 39 |
|
41 | 40 | ############################################################################### |
| 41 | +# Plot the mesh |
| 42 | +# ------------- |
| 43 | +# Plot the mesh to view the bare mesh of the model |
| 44 | +mesh.plot() |
| 45 | + |
| 46 | +#################################################################q############## |
42 | 47 | # Query basic information about the mesh |
43 | 48 | # -------------------------------------- |
| 49 | +# The ``Mesh`` object has several properties allowing access to different information such as: |
44 | 50 |
|
45 | | -# Node IDs |
46 | | -n_ids = mesh.node_ids |
| 51 | +# the number of nodes |
| 52 | +print(f"This mesh contains {mesh.num_nodes} nodes") |
47 | 53 |
|
48 | | -# Element IDs |
49 | | -e_ids = mesh.element_ids |
| 54 | +# the list of node IDs |
| 55 | +print(f"with IDs: {mesh.node_ids}.") |
50 | 56 |
|
51 | | -# Available named selection names |
52 | | -named_selections = mesh.named_selections.keys() |
| 57 | +# the number of elements |
| 58 | +print(f"This mesh contains {mesh.num_elements} elements") |
53 | 59 |
|
54 | | -# Number of nodes |
55 | | -n_nodes = mesh.num_nodes |
| 60 | +# the list of element IDs |
| 61 | +print(f"with IDs {mesh.element_ids}.") |
56 | 62 |
|
57 | | -# Number of elements |
58 | | -n_elements = mesh.num_elements |
| 63 | +# the unit of the mesh |
| 64 | +print(f"The mesh is in '{mesh.unit}'.") |
59 | 65 |
|
60 | | -# Number of named selections |
61 | | -n_ns = len(named_selections) |
62 | | - |
63 | | -# Unit (get and set) |
64 | | -mesh_unit = mesh.unit |
65 | | -mesh.unit = "mm" |
66 | | - |
67 | | -print(n_ids) |
68 | | -print(e_ids) |
| 66 | +############################################################################### |
| 67 | +# Named selections |
| 68 | +# ---------------- |
| 69 | +# The available named selections are given as a dictionary |
| 70 | +# with the names as keys and the actual ``NamedSelection`` objects as values. |
| 71 | +# Printing the dictionary informs you on the available names. |
| 72 | +named_selections = mesh.named_selections |
69 | 73 | print(named_selections) |
70 | | -print(n_nodes) |
71 | | -print(n_elements) |
72 | 74 |
|
73 | 75 | ############################################################################### |
74 | | -# Get Named Selections |
75 | | -# -------------------- |
76 | | -ns_list = mesh.named_selections.keys() |
77 | | -first_key = ns_list[0] |
78 | | -named_selection = mesh.named_selections[first_key] |
79 | | - |
80 | | -for k in mesh.named_selections.keys(): |
81 | | - print(k) |
82 | | -for v in mesh.named_selections.values(): |
83 | | - print(v) |
84 | | -for k, v in mesh.named_selections.items(): |
85 | | - print(f"{k} = {v}") |
| 76 | +# To get a specific named selection, query it using its name as key |
| 77 | +print(named_selections["_FIXEDSU"]) |
86 | 78 |
|
87 | 79 | ############################################################################### |
88 | | -# Get elements |
89 | | -# ------------ |
90 | | - |
91 | | -# Get an element by ID |
92 | | -el_by_id = mesh.elements.by_id[1] |
93 | | - |
94 | | -# Get an element by index |
95 | | -index = el_by_id.index |
96 | | -print(mesh.elements[index]) |
| 80 | +# Elements |
| 81 | +# -------- |
| 82 | +# Use ``mesh.elements`` to access the list of Element objects |
| 83 | +print(mesh.elements) |
97 | 84 |
|
98 | 85 | ############################################################################### |
99 | | -# Element Types and Materials |
100 | | -# --------------------------- |
101 | | - |
102 | | -# Get the element types |
103 | | -el_types = mesh.element_types |
104 | | -print(el_types) |
| 86 | +# You can then query a specific element by its ID |
| 87 | +print(mesh.elements.by_id[1]) |
105 | 88 |
|
106 | | -# Get the materials |
107 | | -e_materials = mesh.materials |
108 | | -print(e_materials) |
| 89 | +############################################################################### |
| 90 | +# or by its index |
| 91 | +element_0 = mesh.elements[0] |
| 92 | +print(element_0) |
109 | 93 |
|
110 | 94 | ############################################################################### |
111 | 95 | # Query information about one particular element |
112 | 96 | # ---------------------------------------------- |
| 97 | +# You can request the IDs of the nodes attached to an ``Element`` object |
| 98 | +print(element_0.node_ids) |
113 | 99 |
|
114 | | -# Get the nodes of an element |
115 | | -elem_0_nodes = mesh.elements[0].nodes |
| 100 | +# or the list of ``Node`` objects |
| 101 | +print(element_0.nodes) |
116 | 102 |
|
117 | | -# Get the node IDs of an element |
118 | | -elem_0_nodes_ids = mesh.elements[0].node_ids |
119 | | - |
120 | | -# Get the number of nodes of an element |
121 | | -num_nodes_elem_0 = mesh.elements[0].num_nodes |
| 103 | +# To get the number of nodes attached, use |
| 104 | +print(element_0.num_nodes) |
122 | 105 |
|
123 | 106 | # Get the type of the element |
124 | | -elem_0_type_info = mesh.elements[0].type_info |
125 | | -elem_0_type = mesh.elements[0].type |
| 107 | +print(element_0.type_info) |
| 108 | +print(element_0.type) |
126 | 109 |
|
127 | 110 | # Get the shape of the element |
128 | | -elem_0_shape = mesh.elements[0].shape |
| 111 | +print(element_0.shape) |
| 112 | + |
| 113 | +############################################################################### |
| 114 | +# Element types and materials |
| 115 | +# --------------------------- |
| 116 | +# The ``Mesh`` object provides access to properties defined on all elements, |
| 117 | +# such as their types or their associated materials. |
| 118 | + |
| 119 | +# Get the type of all elements |
| 120 | +print(mesh.element_types) |
| 121 | + |
| 122 | +# Get the materials of all elements |
| 123 | +print(mesh.materials) |
129 | 124 |
|
130 | 125 | ############################################################################### |
131 | | -# Get the elemental connectivity |
132 | | -# ------------------------------ |
| 126 | +# Elemental connectivity |
| 127 | +# ---------------------- |
| 128 | +# The elemental connectivity maps elements to connected nodes, either using IDs or indexes. |
| 129 | + |
| 130 | +# To access the indexes of the connected nodes using an element's index, use |
| 131 | +element_to_node_connectivity = mesh.element_to_node_connectivity |
| 132 | +print(element_to_node_connectivity[0]) |
133 | 133 |
|
134 | | -# get node indices from element index |
135 | | -conn1 = mesh.element_to_node_connectivity |
| 134 | +# To access the IDs of the connected nodes using an element's index, use |
| 135 | +element_to_node_ids_connectivity = mesh.element_to_node_ids_connectivity |
| 136 | +print(element_to_node_ids_connectivity[0]) |
136 | 137 |
|
137 | | -# get node IDs from element index |
138 | | -conn2 = mesh.element_to_node_ids_connectivity |
| 138 | +############################################################################### |
| 139 | +# Each connectivity object has a ``by_id`` property which changes the input from index to ID, thus: |
| 140 | +# To access the indexes of the connected nodes using an element's ID, use |
| 141 | +element_to_node_connectivity_by_id = mesh.element_to_node_connectivity.by_id |
| 142 | +print(element_to_node_connectivity_by_id[3487]) |
139 | 143 |
|
140 | | -el_idx_5 = mesh.elements[5] |
141 | | -# get node IDS from element ID |
142 | | -node_ids = conn2.by_id[el_idx_5.id] |
| 144 | +# To access the IDs of the connected nodes using an element's ID, use |
| 145 | +element_to_node_ids_connectivity_by_id = mesh.element_to_node_ids_connectivity.by_id |
| 146 | +print(element_to_node_ids_connectivity_by_id[3487]) |
143 | 147 |
|
144 | 148 | ############################################################################### |
145 | | -# Get nodes |
146 | | -# --------- |
| 149 | +# Nodes |
| 150 | +# ----- |
147 | 151 |
|
148 | | -# Get a node by ID |
149 | | -node_by_id = mesh.nodes.by_id[1] |
| 152 | +# Get a node by its ID |
| 153 | +node_1 = mesh.nodes.by_id[1] |
| 154 | +print(node_1) |
150 | 155 |
|
151 | | -# Get a node by index |
152 | | -node_by_index = mesh.nodes[0] |
| 156 | +############################################################################### |
| 157 | +# Get a node by its index |
| 158 | +print(mesh.nodes[0]) |
153 | 159 |
|
| 160 | +############################################################################### |
154 | 161 | # Get the coordinates of all nodes |
155 | 162 | print(mesh.coordinates) |
156 | 163 |
|
157 | 164 | ############################################################################### |
158 | 165 | # Query information about one particular node |
159 | 166 | # ------------------------------------------- |
| 167 | +# Get the coordinates of a node |
| 168 | +print(node_1.coordinates) |
160 | 169 |
|
161 | | -# Coordinates |
162 | | -node_1 = mesh.nodes[1].coordinates |
| 170 | +############################################################################### |
| 171 | +# Nodal connectivity |
| 172 | +# ------------------ |
| 173 | +# The nodal connectivity maps nodes to connected elements, either using IDs or indexes. |
163 | 174 |
|
164 | | -# Get Nodal connectivity |
165 | | -conn3 = mesh.node_to_element_connectivity |
166 | | -conn4 = mesh.node_to_element_ids_connectivity |
| 175 | +# To access the indexes of the connected elements using a node's index, use |
| 176 | +node_to_element_connectivity = mesh.node_to_element_connectivity |
| 177 | +print(node_to_element_connectivity[0]) |
167 | 178 |
|
168 | | -print(mesh.nodes[0]) |
| 179 | +# To access the IDs of the connected elements using a node's index, use |
| 180 | +node_to_element_ids_connectivity = mesh.node_to_element_ids_connectivity |
| 181 | +print(node_to_element_ids_connectivity[0]) |
169 | 182 |
|
170 | | -# elements indices to elem_id |
171 | | -print(conn3[0]) |
| 183 | +############################################################################### |
| 184 | +# Each connectivity object has a ``by_id`` property which changes the input from index to ID, thus: |
| 185 | +# To access the indexes of the connected elements using a node's ID, use |
| 186 | +node_to_element_connectivity_by_id = mesh.node_to_element_connectivity.by_id |
| 187 | +print(node_to_element_connectivity_by_id[1]) |
172 | 188 |
|
173 | | -# elements IDs from node index |
174 | | -print(conn4[0]) |
| 189 | +# To access the IDs of the connected elements using a node's ID, use |
| 190 | +node_to_element_ids_connectivity_by_id = mesh.node_to_element_ids_connectivity.by_id |
| 191 | +print(node_to_element_ids_connectivity_by_id[1]) |
175 | 192 |
|
176 | 193 | ############################################################################### |
177 | 194 | # Splitting into meshes |
178 | 195 | # --------------------- |
179 | | - |
180 | | -# Plot the mesh |
181 | | -mesh.plot() |
182 | | - |
183 | | -# Split the global mesh according to mesh properties |
| 196 | +# You can split the global mesh according to mesh properties to work on specific parts of the mesh |
184 | 197 | meshes = simulation.split_mesh_by_properties( |
185 | 198 | properties=[elemental_properties.material, elemental_properties.element_shape] |
186 | 199 | ) |
187 | | -meshes.plot() |
188 | | - |
189 | | -# Split the global mesh and select meshes for specific property values |
| 200 | +# The object obtained is a ``Meshes`` |
190 | 201 | print(meshes) |
191 | | -meshes = simulation.split_mesh_by_properties( |
| 202 | + |
| 203 | +# Plotting a ``Meshes`` object plots a combination of all the ``Mesh`` objects within |
| 204 | +meshes.plot(text="Mesh split") |
| 205 | + |
| 206 | +############################################################################### |
| 207 | +# Select a specific ``Mesh`` in the ``Meshes``, by index |
| 208 | +meshes[0].plot(text="First mesh in the split mesh") |
| 209 | + |
| 210 | +############################################################################### |
| 211 | +# You can split the global mesh and select meshes based on specific property values |
| 212 | +meshes_filtered = simulation.split_mesh_by_properties( |
192 | 213 | properties={ |
193 | | - elemental_properties.material: 1, |
194 | | - elemental_properties.element_shape: [0, 1], |
| 214 | + elemental_properties.material: [2, 3, 4], |
| 215 | + elemental_properties.element_shape: 1, |
195 | 216 | } |
196 | 217 | ) |
| 218 | +meshes_filtered.plot(text="Mesh split and filtered") |
197 | 219 |
|
198 | | -meshes.plot() |
199 | | - |
200 | | -# Select a specific Mesh in the Meshes, by index |
201 | | -meshes[0].plot() |
202 | | -# or by property values |
203 | | -meshes[{"mat": 1, "elshape": 0}].plot() |
| 220 | +############################################################################### |
| 221 | +# or with a unique combination of property values |
| 222 | +meshes[{"mat": 5, "elshape": 0}].plot(text="Mesh for mat=5 and elshape=0") |
0 commit comments