|
4 | 4 | Plotting data on a graph
|
5 | 5 | ========================
|
6 | 6 |
|
7 |
| -.. |DpfPlotter| replace:: :class:`DpfPlotter<ansys.dpf.core.plotter.DpfPlotter>` |
8 | 7 | .. |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 | 8 | .. |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>` |
13 | 11 |
|
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. |
15 | 13 |
|
16 | 14 | 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. |
18 | 16 |
|
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: |
20 | 18 |
|
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: |
23 | 23 |
|
24 | 24 | Results data vs. space position
|
25 | 25 | -------------------------------
|
26 | 26 |
|
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. |
29 | 29 |
|
30 |
| -Define the data |
31 |
| -^^^^^^^^^^^^^^^ |
| 30 | +Define the results data |
| 31 | +^^^^^^^^^^^^^^^^^^^^^^^ |
32 | 32 |
|
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. |
34 | 36 |
|
35 | 37 | .. jupyter-execute::
|
36 | 38 |
|
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 |
38 | 40 | from ansys.dpf import core as dpf
|
| 41 | + # Import the examples module |
39 | 42 | from ansys.dpf.core import examples
|
| 43 | + # Import the operators module |
40 | 44 | from ansys.dpf.core import operators as ops
|
| 45 | + # Import the geometry module |
41 | 46 | from ansys.dpf.core import geometry as geo
|
| 47 | + |
| 48 | + # Import the ``matplotlib.pyplot`` module |
42 | 49 | import matplotlib.pyplot as plt
|
43 |
| - # Define the result file |
44 |
| - result_file = examples.find_static_rst() |
45 | 50 |
|
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. |
48 | 62 |
|
49 | 63 | .. jupyter-execute::
|
50 | 64 |
|
51 | 65 | # 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 |
54 | 70 |
|
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. |
56 | 73 |
|
57 | 74 | .. jupyter-execute::
|
58 | 75 |
|
59 | 76 | # Get the displacement results
|
60 |
| - my_disp = my_model.results.displacement.eval() |
| 77 | + disp_results_1 = model_1.results.displacement.eval() |
61 | 78 |
|
62 |
| -Create the line |
| 79 | +Define the line |
63 | 80 | ^^^^^^^^^^^^^^^
|
64 | 81 |
|
65 | 82 | Create a |Line| passing through the mesh diagonal.
|
66 | 83 |
|
67 | 84 | .. jupyter-execute::
|
68 | 85 |
|
69 | 86 | # 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]], |
71 | 88 | n_points=50
|
72 | 89 | )
|
73 | 90 |
|
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. |
76 | 97 |
|
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. |
78 | 103 |
|
79 | 104 | .. jupyter-execute::
|
80 | 105 |
|
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, |
84 | 112 | create_support=True,
|
85 |
| - mesh=my_meshed_region |
| 113 | + mesh=meshed_region_1 |
86 | 114 | ).eval()[0]
|
87 | 115 |
|
88 |
| -Plot a graph of the displacement results along the specified line |
89 |
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 116 | +Plot the graph |
| 117 | +^^^^^^^^^^^^^^ |
90 | 118 |
|
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. |
92 | 121 |
|
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. |
95 | 124 |
|
96 | 125 | .. jupyter-execute::
|
97 | 126 |
|
98 |
| - # Define the norm of the displacement field |
| 127 | + # Define the norm of the displacement results |
99 | 128 | 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 |
103 | 134 | plt.plot(line_length_points, norm_disp.data)
|
| 135 | + |
104 | 136 | # Graph formating
|
105 | 137 | plt.xlabel("Line length"); plt.ylabel("Displacement norm field"); plt.title("Displacement evolution on the line")
|
| 138 | + |
| 139 | + # Display the graph |
106 | 140 | plt.show()
|
107 | 141 |
|
| 142 | +.. _ref_graph_result_time: |
| 143 | + |
108 | 144 | Results data vs. time
|
109 | 145 | ---------------------
|
110 | 146 |
|
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. |
113 | 149 |
|
114 |
| -Define the data |
115 |
| -^^^^^^^^^^^^^^^ |
| 150 | +Define the results data |
| 151 | +^^^^^^^^^^^^^^^^^^^^^^^ |
116 | 152 |
|
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. |
119 | 156 |
|
120 | 157 | .. jupyter-execute::
|
121 | 158 |
|
122 |
| - # Import the ``ansys.dpf.core`` module, including examples files, the operators subpackage and the matplotlib |
| 159 | + # Import the ``ansys.dpf.core`` module |
123 | 160 | from ansys.dpf import core as dpf
|
| 161 | + # Import the examples module |
124 | 162 | from ansys.dpf.core import examples
|
| 163 | + # Import the operators module |
125 | 164 | from ansys.dpf.core import operators as ops
|
| 165 | + |
| 166 | + # Import the ``matplotlib.pyplot`` module |
126 | 167 | import matplotlib.pyplot as plt
|
127 |
| - # Define the result file |
128 |
| - result_file = examples.download_transient_result() |
129 | 168 |
|
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. |
132 | 180 |
|
133 | 181 | .. jupyter-execute::
|
134 | 182 |
|
135 | 183 | # 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. |
138 | 191 |
|
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. |
141 | 193 |
|
142 | 194 | .. jupyter-execute::
|
143 | 195 |
|
144 | 196 | # 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() |
146 | 198 |
|
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. |
148 | 201 |
|
149 | 202 | .. jupyter-execute::
|
150 | 203 |
|
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 |
154 | 208 | max_disp = min_max_op.eval(pin=1)
|
| 209 | + |
| 210 | + # Get the min displacement results |
155 | 211 | min_disp = min_max_op.eval(pin=0)
|
156 | 212 |
|
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 |
159 | 223 |
|
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. |
161 | 229 |
|
162 | 230 | .. jupyter-execute::
|
163 | 231 |
|
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 |
167 | 245 | plt.plot(time_data, max_disp.data, "r", label="Max")
|
168 | 246 | plt.plot(time_data, min_disp.data, "b", label="Min")
|
| 247 | + |
169 | 248 | # 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