Skip to content

Commit b0ba1d6

Browse files
update the plotting_a_graph.rst to the tutorials guidelines
1 parent 7189c8f commit b0ba1d6

File tree

1 file changed

+150
-68
lines changed

1 file changed

+150
-68
lines changed

doc/source/user_guide/tutorials/plot/plotting_a_graph.rst

+150-68
Original file line numberDiff line numberDiff line change
@@ -4,167 +4,249 @@
44
Plotting data on a graph
55
========================
66

7-
.. |DpfPlotter| replace:: :class:`DpfPlotter<ansys.dpf.core.plotter.DpfPlotter>`
87
.. |Line| replace:: :class:`Line <ansys.dpf.core.geometry.Line>`
9-
.. |MeshedRegion| replace:: :class:`MeshedRegion <ansys.dpf.core.meshed_region.MeshedRegion>`
10-
.. |Model| replace:: :class:`Model <ansys.dpf.core.model.Model>`
118
.. |mapping| replace:: :class:`mapping <ansys.dpf.core.operators.mapping.on_coordinates.on_coordinates>`
12-
.. |Examples| replace:: :mod:`Examples<ansys.dpf.core.examples>`
9+
.. |Line.path| replace:: :func:`Line.path<ansys.dpf.core.geometry.Line.path>`
10+
.. |min_max_fc| replace:: :class:`min_max_fc <ansys.dpf.core.operators.min_max.min_max_fc.min_max_fc>`
1311

14-
This part shows how to get a result plotted on a graph.
12+
This tutorial explains how to plot a graph with data in DPF.
1513

1614
The current |DpfPlotter| module don't have method to plotting graphs. Thus, you need to import the
17-
`matplotlib <https://github.com/matplotlib/matplotlib>`_ library to plot a graph with PyDPF-Core.
15+
`matplotlib <matplotlib_github_>`_ library to plot a graph with PyDPF-Core.
1816

19-
There is a large range of data types you can represent on the graph coordinates. Here we plot:
17+
There is a large range of graphs you can plot. Here, we plot:
2018

21-
- `Results data vs. space position`_ graph
22-
- `Results data vs. time`_ graph
19+
- :ref:`Results data vs. space position graph <ref_graph_result_space>`
20+
- :ref:`Results data vs. time graph <ref_graph_result_time>`
21+
22+
.. _ref_graph_result_space:
2323

2424
Results data vs. space position
2525
-------------------------------
2626

27-
We will plot the displacement results on a |Line|. To understand how this object can
28-
be defined check the :ref:`ref_plotting_data_on_specific_placements` tutorial.
27+
In this tutorial, we plot the norm of the displacement results on a |Line|. For more information about how
28+
this object can be defined, see the :ref:`ref_plotting_data_on_specific_placements` tutorial.
2929

30-
Define the data
31-
^^^^^^^^^^^^^^^
30+
Define the results data
31+
^^^^^^^^^^^^^^^^^^^^^^^
3232

33-
We will download a simple simulation result file available in our |Examples| package:
33+
First, import a results file. For this tutorial, you can use the one available in the |Examples| module.
34+
For more information about how to import your own result file in DPF, see
35+
the :ref:`ref_tutorials_import_data` tutorials section.
3436

3537
.. jupyter-execute::
3638

37-
# Import the ``ansys.dpf.core`` module, including examples files, the operators subpackage, the geometry module and the matplotlib
39+
# Import the ``ansys.dpf.core`` module
3840
from ansys.dpf import core as dpf
41+
# Import the examples module
3942
from ansys.dpf.core import examples
43+
# Import the operators module
4044
from ansys.dpf.core import operators as ops
45+
# Import the geometry module
4146
from ansys.dpf.core import geometry as geo
47+
48+
# Import the ``matplotlib.pyplot`` module
4249
import matplotlib.pyplot as plt
43-
# Define the result file
44-
result_file = examples.find_static_rst()
4550

46-
The results will be mapped over a defined path of coordinates. So, start by creating
47-
a |Model| with the result file and extract the |MeshedRegion| from it:
51+
# Define the result file path
52+
result_file_path_1 = examples.find_static_rst()
53+
54+
The results will be mapped over a defined set of coordinates. Thus, we need the spatial support to
55+
those coordinates: the mesh. The mesh object in DPF is a |MeshedRegion|.
56+
57+
You can obtain a |MeshedRegion| by creating your own from scratch or by getting it from a result file.
58+
For more information, see the :ref:`ref_tutorials_create_a_mesh_from_scratch` and
59+
:ref:`ref_tutorials_get_mesh_from_result_file` tutorials.
60+
61+
Here, we extract it from the result file.
4862

4963
.. jupyter-execute::
5064

5165
# Create the model
52-
my_model = dpf.Model(data_sources=result_file)
53-
my_meshed_region = my_model.metadata.meshed_region
66+
model_1 = dpf.Model(data_sources=result_file_path_1)
67+
68+
# Extract the mesh
69+
meshed_region_1 = model_1.metadata.meshed_region
5470

55-
We choose to plot the displacement results field. Extract the displacements results from the model:
71+
Extract the results to be plotted on the graph. In this tutorial, we plot the norm of the
72+
displacement results over time.
5673

5774
.. jupyter-execute::
5875

5976
# Get the displacement results
60-
my_disp = my_model.results.displacement.eval()
77+
disp_results_1 = model_1.results.displacement.eval()
6178

62-
Create the line
79+
Define the line
6380
^^^^^^^^^^^^^^^
6481

6582
Create a |Line| passing through the mesh diagonal.
6683

6784
.. jupyter-execute::
6885

6986
# Create the Line object
70-
my_line = geo.Line(coordinates=[[0.0, 0.06, 0.0], [0.03, 0.03, 0.03]],
87+
line_1 = geo.Line(coordinates=[[0.0, 0.06, 0.0], [0.03, 0.03, 0.03]],
7188
n_points=50
7289
)
7390

74-
Map displacement field to the line
75-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
Map the results to the line
92+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
93+
94+
Map the displacement results to the |Line| using the |mapping| operator. This operator
95+
retrieves the results of the entities located in the given coordinates. If the given coordinates don't
96+
match with any entity coordinate, the operator interpolates the results inside elements with shape functions.
7697

77-
Compute the mapped displacement data using the |mapping| operator.
98+
The displacement results are defined in a *`nodal`* location. Thus, each node has a coordinate in the
99+
mesh and a corresponding displacement data.
100+
101+
The |mapping| operator takes the coordinates stored in a |Field|. Thus, we must create a |Field| with the
102+
|Line| coordinates.
78103

79104
.. jupyter-execute::
80105

81-
# Map the line coordinates with the displacement results and get the field
82-
mapped_disp_line = ops.mapping.on_coordinates(fields_container=my_disp,
83-
coordinates=my_line.mesh.nodes.coordinates_field,
106+
# Get the coordinates field
107+
line_coords_field = line_1.mesh.nodes.coordinates_field
108+
109+
# Map the line coordinates with the displacement results
110+
mapped_disp_line = ops.mapping.on_coordinates(fields_container=disp_results_1,
111+
coordinates=line_coords_field,
84112
create_support=True,
85-
mesh=my_meshed_region
113+
mesh=meshed_region_1
86114
).eval()[0]
87115

88-
Plot a graph of the displacement results along the specified line
89-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
Plot the graph
117+
^^^^^^^^^^^^^^
90118

91-
Plot a graph of the displacement field along the specified |Line| length using the matplotlib library.
119+
Plot a graph of the norm of the displacement results along the |Line| length using the
120+
`matplotlib <matplotlib_github_>`_ library.
92121

93-
To get the |Line| length you can use the |Line| property :func:`path<ansys.dpf.core.geometry.Line.path>`.
94-
It gives the 1D line coordinates, by the number of points the line was discretized.
122+
To get the |Line| length you can use the |Line.path| method. It gives the 1D line coordinates, based on
123+
the points where the line was discretized.
95124

96125
.. jupyter-execute::
97126

98-
# Define the norm of the displacement field
127+
# Define the norm of the displacement results
99128
norm_disp = ops.math.norm(field=mapped_disp_line).eval()
100-
# Define the line points on the its length
101-
line_length_points = my_line.path
102-
# Plot the graph
129+
130+
# Define the point coordinates on the line length
131+
line_length_points = line_1.path
132+
133+
# Define the plot figure
103134
plt.plot(line_length_points, norm_disp.data)
135+
104136
# Graph formating
105137
plt.xlabel("Line length"); plt.ylabel("Displacement norm field"); plt.title("Displacement evolution on the line")
138+
139+
# Display the graph
106140
plt.show()
107141

142+
.. _ref_graph_result_time:
143+
108144
Results data vs. time
109145
---------------------
110146

111-
We will plot the displacement results over time for a transient analysis. To understand more about using PyDPF-Core
112-
with a transient analysis check the :ref:`static_transient_examples` examples.
147+
In this tutorial, we plot the displacement results over time for a transient analysis.
148+
For more information about using PyDPF-Core with a transient analysis, see the :ref:`static_transient_examples` examples.
113149

114-
Define the data
115-
^^^^^^^^^^^^^^^
150+
Define the results data
151+
^^^^^^^^^^^^^^^^^^^^^^^
116152

117-
Download the transient result example. This example is not included in DPF-Core
118-
by default to speed up the installation. Downloading this example should take only a few seconds.
153+
First, import a transient results file. For this tutorial, you can use the one available in the |Examples| module.
154+
For more information about how to import your own result file in DPF, see
155+
the :ref:`ref_tutorials_import_data` tutorials section.
119156

120157
.. jupyter-execute::
121158

122-
# Import the ``ansys.dpf.core`` module, including examples files, the operators subpackage and the matplotlib
159+
# Import the ``ansys.dpf.core`` module
123160
from ansys.dpf import core as dpf
161+
# Import the examples module
124162
from ansys.dpf.core import examples
163+
# Import the operators module
125164
from ansys.dpf.core import operators as ops
165+
166+
# Import the ``matplotlib.pyplot`` module
126167
import matplotlib.pyplot as plt
127-
# Define the result file
128-
result_file = examples.download_transient_result()
129168

130-
The results will be mapped over a defined path of coordinates. So, start by creating
131-
a |Model| with the result file and extract the |MeshedRegion| from it:
169+
# Define the result file path
170+
result_file_path_2 = examples.download_transient_result()
171+
172+
The results will be mapped over a defined path of coordinates. Thus, we need the spatial support to
173+
those coordinates: the mesh. The mesh object in DPF is a |MeshedRegion|.
174+
175+
You can obtain a |MeshedRegion| by creating your own from scratch or by getting it from a result file.
176+
For more information, see the :ref:`ref_tutorials_create_a_mesh_from_scratch` and
177+
:ref:`ref_tutorials_get_mesh_from_result_file` tutorials.
178+
179+
Here, we extract it from the result file.
132180

133181
.. jupyter-execute::
134182

135183
# Create the model
136-
my_model = dpf.Model(data_sources=result_file)
137-
my_meshed_region = my_model.metadata.meshed_region
184+
model_2 = dpf.Model(data_sources=result_file_path_2)
185+
186+
# Extract the mesh
187+
meshed_region_2 = model_2.metadata.meshed_region
188+
189+
Extract the results to be plotted on the graph. Here, we plot the maximum and minimum
190+
displacement results over time.
138191

139-
We choose to plot the maximum and minimum displacement results over time.
140-
Extract the displacements results from the model for all the time frequencies:
192+
First extract the displacement results for all the time frequencies.
141193

142194
.. jupyter-execute::
143195

144196
# Get the displacement results
145-
my_disp = my_model.results.displacement.on_all_time_freqs.eval()
197+
disp_results_2 = model_2.results.displacement.on_all_time_freqs.eval()
146198

147-
Define the minimum and maximum displacements for all results:
199+
Next, define the minimal and maximal displacements for each time step by using the |min_max_fc|
200+
operator.
148201

149202
.. jupyter-execute::
150203

151-
# Define the min_max operator with the normed displacement
152-
min_max_op = ops.min_max.min_max_fc(fields_container=ops.math.norm_fc(my_disp))
153-
# Get the max and min displacements
204+
# Define the min_max operator and give the normed displacement results
205+
min_max_op = ops.min_max.min_max_fc(fields_container=ops.math.norm_fc(disp_results_2))
206+
207+
# Get the max displacement results
154208
max_disp = min_max_op.eval(pin=1)
209+
210+
# Get the min displacement results
155211
min_disp = min_max_op.eval(pin=0)
156212

157-
Plot a graph of the minimum and maximum displacements over time
158-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
213+
Define the time data
214+
^^^^^^^^^^^^^^^^^^^^
215+
216+
The results time steps in DPF are given by the |TimeFreqSupport| object. You can extract it
217+
from the displacement results |Field|.
218+
219+
.. jupyter-execute::
220+
221+
# Define the time steps
222+
time_steps_1 = disp_results_2.time_freq_support.time_frequencies
159223

160-
Plot a graph of the minimum and maximum displacements over time using the matplotlib library.
224+
# Print the time frequencies
225+
print(time_steps_1)
226+
227+
The time steps are given in a |Field|. To plot the graph you need to extract the
228+
|Field| data.
161229

162230
.. jupyter-execute::
163231

164-
# Define the time frequencies from the model
165-
time_data = my_model.metadata.time_freq_support.time_frequencies.data
166-
# Plot the graph
232+
# Get the time steps data
233+
time_data = time_steps_1.data
234+
235+
236+
Plot the graph
237+
^^^^^^^^^^^^^^
238+
239+
Plot a graph of the minimal and maximal displacements over time using the
240+
`matplotlib <matplotlib_github_>`_ library.
241+
242+
.. jupyter-execute::
243+
244+
# Define the plot figure
167245
plt.plot(time_data, max_disp.data, "r", label="Max")
168246
plt.plot(time_data, min_disp.data, "b", label="Min")
247+
169248
# Graph formating
170-
plt.xlabel("Time (s)"); plt.ylabel("Displacement (m)"); plt.legend(); plt.show()
249+
plt.xlabel("Time (s)"); plt.ylabel("Displacement (m)"); plt.legend();
250+
251+
# Display the graph
252+
plt.show()

0 commit comments

Comments
 (0)