Skip to content

Commit dfe5adf

Browse files
add plotting_a_graph.rst tut
1 parent d61ad5c commit dfe5adf

File tree

1 file changed

+216
-3
lines changed

1 file changed

+216
-3
lines changed
Lines changed: 216 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,218 @@
11
.. _ref_plotting_a_graph:
22

3-
====================================
4-
Plotting data on specific placements
5-
====================================
3+
========================
4+
Plotting data on a graph
5+
========================
6+
7+
.. |DpfPlotter| replace:: :class:`DpfPlotter<ansys.dpf.core.plotter.DpfPlotter>`
8+
.. |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>`
11+
.. |mapping| replace:: :class:`mapping <ansys.dpf.core.operators.mapping.on_coordinates.on_coordinates>`
12+
13+
This part shows how to get a result plotted on a graph.
14+
15+
The current |DpfPlotter| module don't have method to plotting graphs. Thus, you need to import the
16+
`matplotlib <https://github.com/matplotlib/matplotlib>`_ library to plot a graph with PyDPF-Core.
17+
18+
There is a large range of data types you can represent on the graph coordinates. Here we plot:
19+
20+
- `Results data vs. space position`_ graph
21+
- `Results data vs. time`_ graph
22+
23+
Results data vs. space position
24+
-------------------------------
25+
26+
We will plot the displacement results on a |Line|. To understand how this object can
27+
be defined check the :ref:`ref_plotting_data_on_specific_placements` tutorial.
28+
29+
Define the data
30+
^^^^^^^^^^^^^^^
31+
32+
We will download a simple simulation result file available in our `Examples` package:
33+
34+
.. code-block:: python
35+
36+
# Import the ``ansys.dpf.core`` module, including examples files, the operators subpackage, the geometry module and the matplotlib
37+
from ansys.dpf import core as dpf
38+
from ansys.dpf.core import examples
39+
from ansys.dpf.core import operators as ops
40+
from ansys.dpf.core import geometry as geo
41+
import matplotlib.pyplot as plt
42+
# Define the result file
43+
result_file = examples.find_static_rst()
44+
45+
The results will be mapped over a defined path of coordinates. So, start by creating
46+
a |Model| with the result file and extract the |MeshedRegion| from it:
47+
48+
.. code-block:: python
49+
50+
# Create the model
51+
my_model = dpf.Model(data_sources=result_file)
52+
my_meshed_region = my_model.metadata.meshed_region
53+
54+
We choose to plot the displacement results field. Extract the displacements results from the model:
55+
56+
.. code-block:: python
57+
58+
# Get the displacement results
59+
my_disp = my_model.results.displacement.eval()
60+
61+
Create the line
62+
^^^^^^^^^^^^^^^
63+
64+
Create a |Line| passing through the mesh diagonal.
65+
66+
.. code-block:: python
67+
68+
# Create the Line object
69+
my_line = geo.Line(coordinates=[[0.0, 0.06, 0.0], [0.03, 0.03, 0.03]],
70+
n_points=50
71+
)
72+
73+
Map displacement field to the line
74+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75+
76+
Compute the mapped displacement data using the |mapping| operator.
77+
78+
.. code-block:: python
79+
80+
# Map the line coordinates with the displacement results and get the field
81+
mapped_disp_line = ops.mapping.on_coordinates(fields_container=my_disp,
82+
coordinates=my_line.mesh.nodes.coordinates_field,
83+
create_support=True,
84+
mesh=my_meshed_region
85+
).eval()[0]
86+
87+
Plot a graph of the displacement results along the specified line
88+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
89+
90+
Plot a graph of the displacement field along the specified |Line| length using the matplotlib library.
91+
92+
To get the |Line| length you can use the |Line| property :func:`path<ansys.dpf.core.geometry.Line.path>`.
93+
It gives the 1D line coordinates, by the number of points the line was discretized.
94+
95+
.. code-block:: python
96+
97+
# Define the norm of the displacement field
98+
norm_disp = ops.math.norm(field=mapped_disp_line).eval()
99+
# Define the line points on the its length
100+
line_length_points = my_line.path
101+
# Plot the graph
102+
plt.plot(line_length_points, norm_disp)
103+
# Graph formating
104+
plt.xlabel("Line length"); plt.ylabel("Displacement norm field"); plt.title("Displacement evolution on the line")
105+
plt.show()
106+
107+
.. rst-class:: sphx-glr-script-out
108+
109+
.. jupyter-execute::
110+
:hide-code:
111+
112+
from ansys.dpf import core as dpf
113+
from ansys.dpf.core import examples
114+
from ansys.dpf.core import operators as ops
115+
from ansys.dpf.core import geometry as geo
116+
import matplotlib.pyplot as plt
117+
result_file = examples.find_static_rst()
118+
my_model = dpf.Model(data_sources=result_file)
119+
my_meshed_region = my_model.metadata.meshed_region
120+
my_disp = my_model.results.displacement.eval()
121+
my_line = geo.Line(coordinates=[[0.0, 0.06, 0.0], [0.03, 0.03, 0.03]],
122+
n_points=50
123+
)
124+
mapped_disp_line = ops.mapping.on_coordinates(fields_container=my_disp,
125+
coordinates=my_line.mesh.nodes.coordinates_field,
126+
create_support=True,
127+
mesh=my_meshed_region
128+
).eval()[0]
129+
norm_disp = ops.math.norm(field=mapped_disp_line).eval()
130+
line_length_points = my_line.path
131+
plt.plot(line_length_points, norm_disp.data)
132+
plt.xlabel("Line length"); plt.ylabel("Displacement norm field"); plt.title("Displacement evolution on the line")
133+
plt.show()
134+
135+
Results data vs. time
136+
---------------------
137+
138+
We will plot the displacement results over time for a transient analysis. To understand more about using PyDPF-Core
139+
with a transient analysis check the :ref:`static_transient_examples` examples.
140+
141+
Define the data
142+
^^^^^^^^^^^^^^^
143+
144+
Download the transient result example. This example is not included in DPF-Core
145+
by default to speed up the installation. Downloading this example should take only a few seconds.
146+
147+
.. code-block:: python
148+
149+
# Import the ``ansys.dpf.core`` module, including examples files, the operators subpackage and the matplotlib
150+
from ansys.dpf import core as dpf
151+
from ansys.dpf.core import examples
152+
from ansys.dpf.core import operators as ops
153+
import matplotlib.pyplot as plt
154+
# Define the result file
155+
result_file = examples.download_transient_result()
156+
157+
The results will be mapped over a defined path of coordinates. So, start by creating
158+
a |Model| with the result file and extract the |MeshedRegion| from it:
159+
160+
.. code-block:: python
161+
162+
# Create the model
163+
my_model = dpf.Model(data_sources=result_file)
164+
my_meshed_region = my_model.metadata.meshed_region
165+
166+
We choose to plot the maximum and minimum displacement results over time.
167+
Extract the displacements results from the model for all the time frequencies:
168+
169+
.. code-block:: python
170+
171+
# Get the displacement results
172+
my_disp = my_model.results.displacement.on_all_time_freqs.eval()
173+
174+
Define the minimum and maximum displacements for all results:
175+
176+
.. code-block:: python
177+
178+
# Define the min_max operator with the normed displacement
179+
min_max_op = ops.min_max.min_max_fc(fields_container=ops.math.norm_fc(my_disp))
180+
# Get the max and min displacements
181+
max_disp = min_max_op.eval(pin=1)
182+
min_disp = min_max_op.eval(pin=0)
183+
184+
Plot a graph of the minimum and maximum displacements over time
185+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
186+
187+
Plot a graph of the minimum and maximum displacements over time using the matplotlib library.
188+
189+
.. code-block:: python
190+
191+
# Define the time frequencies from the model
192+
time_data = my_model.metadata.time_freq_support.time_frequencies.data
193+
# Plot the graph
194+
plt.plot(time_data, max_disp.data, "r", label="Max")
195+
plt.plot(time_data, min_disp.data, "b", label="Min")
196+
# Graph formating
197+
plt.xlabel("Time (s)"); plt.ylabel("Displacement (m)"); plt.legend(); plt.show()
198+
199+
.. rst-class:: sphx-glr-script-out
200+
201+
.. jupyter-execute::
202+
:hide-code:
203+
204+
from ansys.dpf import core as dpf
205+
from ansys.dpf.core import examples
206+
from ansys.dpf.core import operators as ops
207+
import matplotlib.pyplot as plt
208+
result_file = examples.download_transient_result()
209+
my_model = dpf.Model(data_sources=result_file)
210+
my_meshed_region = my_model.metadata.meshed_region
211+
my_disp = my_model.results.displacement.on_all_time_freqs.eval()
212+
min_max_op = ops.min_max.min_max_fc(fields_container=ops.math.norm_fc(my_disp))
213+
max_disp = min_max_op.eval(pin=1)
214+
min_disp = min_max_op.eval(pin=0)
215+
time_data = my_model.metadata.time_freq_support.time_frequencies.data
216+
plt.plot(time_data, max_disp.data, "r", label="Max")
217+
plt.plot(time_data, min_disp.data, "b", label="Min")
218+
plt.xlabel("Time (s)"); plt.ylabel("Displacement (m)"); plt.legend(); plt.show()

0 commit comments

Comments
 (0)