Skip to content

Commit 19f2149

Browse files
committed
Working 05-mesh-exploration.py
1 parent 7d132e7 commit 19f2149

File tree

1 file changed

+138
-119
lines changed

1 file changed

+138
-119
lines changed

examples/01-Detailed-Examples/05-mesh-exploration.py

Lines changed: 138 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -3,201 +3,220 @@
33
44
Explore the mesh
55
================
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.
88
"""
99

1010
###############################################################################
1111
# Perform required imports
1212
# ------------------------
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
1415
# get by importing the DPF ``examples`` package.
1516

1617
from ansys.dpf import post
1718
from ansys.dpf.post import examples
1819
from ansys.dpf.post.common import elemental_properties
1920

2021
###############################################################################
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.
3028

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)
3331

3432
###############################################################################
3533
# Get the mesh
3634
# ------------
37-
38-
# Retrieve the actual mesh
35+
# Retrieve the mesh
3936
mesh = simulation.mesh
37+
# Printing the mesh directly gives the same information already shown at the simulation level
38+
print(mesh)
4039

4140
###############################################################################
41+
# Plot the mesh
42+
# -------------
43+
# Plot the mesh to view the bare mesh of the model
44+
mesh.plot()
45+
46+
#################################################################q##############
4247
# Query basic information about the mesh
4348
# --------------------------------------
49+
# The ``Mesh`` object has several properties allowing access to different information such as:
4450

45-
# Node IDs
46-
n_ids = mesh.node_ids
51+
# the number of nodes
52+
print(f"This mesh contains {mesh.num_nodes} nodes")
4753

48-
# Element IDs
49-
e_ids = mesh.element_ids
54+
# the list of node IDs
55+
print(f"with IDs: {mesh.node_ids}.")
5056

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")
5359

54-
# Number of nodes
55-
n_nodes = mesh.num_nodes
60+
# the list of element IDs
61+
print(f"with IDs {mesh.element_ids}.")
5662

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}'.")
5965

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
6973
print(named_selections)
70-
print(n_nodes)
71-
print(n_elements)
7274

7375
###############################################################################
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"])
8678

8779
###############################################################################
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)
9784

9885
###############################################################################
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])
10588

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)
10993

11094
###############################################################################
11195
# Query information about one particular element
11296
# ----------------------------------------------
97+
# You can request the IDs of the nodes attached to an ``Element`` object
98+
print(element_0.node_ids)
11399

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)
116102

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)
122105

123106
# 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)
126109

127110
# 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)
129124

130125
###############################################################################
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])
133133

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])
136137

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])
139143

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])
143147

144148
###############################################################################
145-
# Get nodes
146-
# ---------
149+
# Nodes
150+
# -----
147151

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)
150155

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])
153159

160+
###############################################################################
154161
# Get the coordinates of all nodes
155162
print(mesh.coordinates)
156163

157164
###############################################################################
158165
# Query information about one particular node
159166
# -------------------------------------------
167+
# Get the coordinates of a node
168+
print(node_1.coordinates)
160169

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.
163174

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])
167178

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])
169182

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])
172188

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])
175192

176193
###############################################################################
177194
# Splitting into meshes
178195
# ---------------------
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
184197
meshes = simulation.split_mesh_by_properties(
185198
properties=[elemental_properties.material, elemental_properties.element_shape]
186199
)
187-
meshes.plot()
188-
189-
# Split the global mesh and select meshes for specific property values
200+
# The object obtained is a ``Meshes``
190201
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(
192213
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,
195216
}
196217
)
218+
meshes_filtered.plot(text="Mesh split and filtered")
197219

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

Comments
 (0)