Skip to content

Commit 31d7034

Browse files
luisaFelixSallesPProfizicbellot000moe-adjorgepiloto
authored
Add mesh tutorials to the main tutorials branch (#1908)
* add new basic tutorial and jupyter_ sphinx extension * add tutorials files and update index page * add get_mesh_from_result_file.rst tutorial * updates on the index * updates on get_mesh_from_result_file.rst * add create_a_mesh_from_scratch.rst tutorial * updates the read the mesh tut * updates the read the mesh tut * add get_specific_part_mesh.rst and split_mesh.rst tutorials * updates on thr get_specific_part_mesh.rst tutorial * updates the create_a_mesh_from_scratch.rst tut * erases add and plot data parts from the create_a_mesh_from_scratch.rst tutorial * update references in get_mesh_from_result_file.rst tutorial * update references in read_mesh.rst tutorial * add second approach to the split_mesh.rst tutorial * updates on get_specific_part_mesh.rst * put a tab for each solver * update * put a tab for each solver in the get_specific_part_mesh.rst tutorial * put a tab for each solver in the split_mesh.rst tutorial * put a tab for each solver and update the read_mesh_metadata.rst tutorial * put a tab for each solver and update the explore_mesh.rst tutorial * put badge for each solver in the main page cards * put badge for each solver in the beginning of the tutorials * update title read_mesh_metadata.rst * update title get_specific_part_mesh.rst * update substitution text * Update doc/source/user_guide/tutorials/mesh/explore_mesh.rst * use only the jupyter sphinx extension * updates the examples package references * update badges in the index.rst * change the section name * add badges in each tutorial * update2 the create_a_mesh_from_scratch.rst tutorial to the tutorials guidelines * update the explore_mesh.rst to the tutorials guidelines * update the extract_mesh_in_split_parts.rst to the tutorials guidelines * update the get_mesh_from_result_file.rst to the tutorials guidelines * update the read_mesh_metadata.rst to the tutorials guidelines * update the split_mesh.rst to the tutorials guidelines * update the index page of the mesh tutorials section * changes the name of the explore_mesh_metadata.rst tutorial * add explanation on the MeshesContainer labels when you use the split_mesh operator on the split_mesh.rst tutorial * add more explanation for each approach on the split_mesh.rst tutorial * add more explanation on the metadata info on the explore_mesh.rst tutorial * add more explanation on the metadata info on the explore_mesh_metadata.rst tutorial * updates on the text of create_a_mesh_from_scratch.rst turorial * Update doc/source/user_guide/tutorials/mesh/explore_mesh_metadata.rst * Update doc/source/user_guide/tutorials/mesh/extract_mesh_in_split_parts.rst * delete file basic_tutorial.rst * correction of a reference in explore_mesh.rst * Fix sentence. Co-authored-by: Camille Bellot <[email protected]> * Refactor search_sequence_numpy in mesh from scratch tutorial Co-authored-by: Muhammed Adedigba <[email protected]> * Fix unordered list Co-authored-by: Jorge Martínez <[email protected]> * Reference nodes, elements, faces, and named selections getters * Refactor query of element type field * Fix unordered list Co-authored-by: Jorge Martínez <[email protected]> * parts -> regions * Fix unordered list Co-authored-by: Jorge Martínez <[email protected]> * Fix unordered list Co-authored-by: Jorge Martínez <[email protected]> * Fix typo Co-authored-by: Camille Bellot <[email protected]> * Fix code snippets formatting Co-authored-by: Camille Bellot <[email protected]> * Fix unordered list * Fix grammar * Fix sentence * Suggestion from Jorge --------- Co-authored-by: Paul Profizi <[email protected]> Co-authored-by: Camille Bellot <[email protected]> Co-authored-by: Muhammed Adedigba <[email protected]> Co-authored-by: Jorge Martínez <[email protected]>
1 parent 790db91 commit 31d7034

File tree

8 files changed

+1297
-18
lines changed

8 files changed

+1297
-18
lines changed

doc/source/user_guide/tutorials/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ of our package background so you can understand how to work with it.
6565

6666
Understand how to represent data in DPF: either from manual input either form result files.
6767

68-
.. grid-item-card:: Mesh analysis
68+
.. grid-item-card:: Mesh exploration
6969
:link: ref_tutorials_mesh
7070
:link-type: ref
7171
:text-align: center
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
.. _ref_tutorials_create_a_mesh_from_scratch:
2+
3+
==========================
4+
Create a mesh from scratch
5+
==========================
6+
7+
.. include:: ../../../links_and_refs.rst
8+
9+
This tutorial demonstrates how to build a |MeshedRegion| from scratch.
10+
11+
The mesh object in DPF is a |MeshedRegion|. You can create your own |MeshedRegion| object and use it
12+
with DPF operators. The ability to use scripting to create any DPF entity means
13+
that you are not dependent on result files and can connect the DPF environment
14+
with any Python tool.
15+
16+
In this tutorial, we create a parallel piped mesh made of linear hexa elements.
17+
18+
:jupyter-download-script:`Download tutorial as Python script<create_a_mesh_from_scratch>`
19+
:jupyter-download-notebook:`Download tutorial as Jupyter notebook<create_a_mesh_from_scratch>`
20+
21+
Import the necessary modules
22+
----------------------------
23+
24+
Import the ``ansys.dpf.core`` module, including the operators module and the numpy library.
25+
26+
.. jupyter-execute::
27+
28+
# Import the numpy library
29+
import numpy as np
30+
# Import the ``ansys.dpf.core`` module
31+
from ansys.dpf import core as dpf
32+
# Import the operators module
33+
from ansys.dpf.core import operators as ops
34+
35+
Define the mesh dimensions
36+
--------------------------
37+
38+
.. jupyter-execute::
39+
40+
# Define the mesh dimensions
41+
length = 0.1
42+
width = 0.05
43+
depth = 0.1
44+
num_nodes_in_length = 10
45+
num_nodes_in_width = 5
46+
num_nodes_in_depth = 10
47+
# Create a MeshedRegion object
48+
my_meshed_region = dpf.MeshedRegion()
49+
50+
Define the connectivity function
51+
--------------------------------
52+
53+
To create a mesh you must define the nodes connectivity. This means to define
54+
the nodes ids connected to each element.
55+
56+
Here, we create a function that will find this connectivity.
57+
58+
.. jupyter-execute::
59+
60+
def search_sequence_numpy(arr, node):
61+
"""Find the node location in an array of nodes and return its index."""
62+
indexes = np.isclose(arr, seq)
63+
match = np.all(indexes, axis=1).nonzero()
64+
return int(match[0][0])
65+
66+
Add nodes
67+
---------
68+
69+
Add |Nodes| to the |MeshedRegion| object.
70+
71+
.. jupyter-execute::
72+
73+
node_id = 1
74+
for i, x in enumerate(
75+
[float(i) * length / float(num_nodes_in_length) for i in range(0, num_nodes_in_length)]
76+
):
77+
for j, y in enumerate(
78+
[float(i) * width / float(num_nodes_in_width) for i in range(0, num_nodes_in_width)]
79+
):
80+
for k, z in enumerate(
81+
[float(i) * depth / float(num_nodes_in_depth) for i in range(0, num_nodes_in_depth)]
82+
):
83+
my_meshed_region.nodes.add_node(node_id, [x, y, z])
84+
node_id += 1
85+
86+
Get the nodes coordinates field.
87+
88+
.. jupyter-execute::
89+
90+
my_nodes_coordinates = my_meshed_region.nodes.coordinates_field
91+
92+
Set the mesh properties
93+
-----------------------
94+
95+
Set the mesh unit.
96+
97+
.. jupyter-execute::
98+
99+
my_meshed_region.unit = "mm"
100+
101+
Set the nodes coordinates.
102+
103+
.. jupyter-execute::
104+
105+
# Get the nodes coordinates data
106+
my_nodes_coordinates_data = my_nodes_coordinates.data
107+
# As we use the connectivity function we need to get the data as a list
108+
my_nodes_coordinates_data_list = my_nodes_coordinates.data_as_list
109+
# Set the nodes scoping
110+
my_coordinates_scoping = my_nodes_coordinates.scoping
111+
112+
Add elements
113+
------------
114+
Add |Elements| to the |MeshedRegion| object.
115+
116+
.. jupyter-execute::
117+
118+
# Add solid elements (linear hexa with eight nodes):
119+
element_id = 1
120+
# Precompute node spacings
121+
dx = length / float(num_nodes_in_length)
122+
dy = width / float(num_nodes_in_width)
123+
dz = depth / float(num_nodes_in_depth)
124+
# Generate node coordinates
125+
x_coords = [i * dx for i in range(num_nodes_in_length - 1)]
126+
y_coords = [j * dy for j in range(num_nodes_in_width - 1)]
127+
z_coords = [k * dz for k in range(num_nodes_in_depth - 1)]
128+
# Iterate through the grid
129+
for x in x_coords:
130+
for y in y_coords:
131+
for z in z_coords:
132+
coord1 = np.array([x, y, z])
133+
connectivity = []
134+
# Generate connectivity for the current element
135+
for xx in [x, x + dx]:
136+
for yy in [y, y + dy]:
137+
for zz in [z, z + dz]:
138+
scoping_index = search_sequence_numpy(my_nodes_coordinates_data,
139+
[xx, yy, zz])
140+
connectivity.append(scoping_index)
141+
# Rearrange connectivity to maintain element orientation
142+
connectivity[2], connectivity[3] = connectivity[3], connectivity[2]
143+
connectivity[6], connectivity[7] = connectivity[7], connectivity[6]
144+
# Add the solid element
145+
my_meshed_region.elements.add_solid_element(element_id, connectivity)
146+
element_id += 1
147+
148+
Plot the mesh
149+
-------------
150+
151+
You can check the mesh we just created with a plot. For more information on how to plot a mesh see
152+
the :ref:`ref_tutorials_plotting_meshes` tutorial.
153+
154+
.. jupyter-execute::
155+
156+
# Plot the mesh
157+
my_meshed_region.plot()

0 commit comments

Comments
 (0)