Skip to content

Commit 137952f

Browse files
add extract_and_explore_results_data.rst tutorial
1 parent 098d3e1 commit 137952f

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed
Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
.. _ref_tutorials_extract_and_explore_results_data:
22

3-
====================
4-
Explore results data
5-
====================
3+
================================
4+
Extract and explore results data
5+
================================
66

77
.. |Field| replace:: :class:`Field<ansys.dpf.core.field.Field>`
88
.. |Examples| replace:: :mod:`Examples<ansys.dpf.core.examples>`
9+
.. |Result| replace:: :class:`Result <ansys.dpf.core.results.Result>`
10+
.. |FieldsContainer| replace:: :class:`FieldsContainer<ansys.dpf.core.fields_container.FieldsContainer>`
11+
.. |get_entity_data| replace:: :func:`get_entity_data()<ansys.dpf.core.field.Field.get_entity_data>`
12+
.. |get_entity_data_by_id| replace:: :func:`get_entity_data_by_id()<ansys.dpf.core.field.Field.get_entity_data_by_id>`
913

1014
This tutorial shows how to extract and explore results data from a result file.
1115

1216
When you extract a result from a result file DPF stores it in a |Field|.
13-
This |Field| will then contain the data of the result associated with it.
17+
This |Field| will contain the data of the result associated with it.
1418

1519
When DPF-Core returns the |Field| object, what Python actually has is a client-side
1620
representation of the |Field|, not the entirety of the |Field| itself. This means
@@ -19,5 +23,121 @@ because when building your workflows, the most efficient way of interacting with
1923
is to minimize the exchange of data between Python and DPF, either by using operators
2024
or by accessing exclusively the data that is needed.
2125

26+
The |Field| data is ordered with respect to its scoping ids (check the :ref:`reft_tutorials_narrow_down_data`
27+
tutorial for more information on scoping manipulations).
2228

29+
Get the results
30+
---------------
2331

32+
Here we will download a result file available in our |Examples| package.
33+
For more information about how to import your result file in DPF check
34+
the :ref:`ref_tutorials_import_result_file` tutorial.
35+
36+
Here we extract the displacement results. The displacement |Result| object gives a |FieldsContainer| when evaluated.
37+
Thus, we will get a |Field| from this |FieldsContainer|.
38+
39+
.. jupyter-execute::
40+
41+
# Import the ``ansys.dpf.core`` module, including examples files and the operators subpackage
42+
from ansys.dpf import core as dpf
43+
from ansys.dpf.core import examples
44+
from ansys.dpf.core import operators as ops
45+
46+
# Define the result file
47+
result_file_path_1 = examples.download_transient_result()
48+
49+
# Create the model
50+
my_model_1 = dpf.Model(data_sources=result_file_path_1)
51+
52+
# Extract the displacement results for the last time step
53+
disp_results = my_model_1.results.displacement.on_last_time_freq.eval()
54+
55+
# Get the displacement field for the last time step
56+
my_disp_field = disp_results[0]
57+
58+
print(my_disp_field)
59+
60+
Extract all data from a field
61+
-----------------------------
62+
63+
You can extract the the entire data in the |Field| as an array (numpy array) or as a list.
64+
65+
Data as an array
66+
^^^^^^^^^^^^^^^^
67+
68+
.. jupyter-execute::
69+
70+
# Get the displacement data as an array
71+
my_data_array = my_disp_field.data
72+
print("Displacement data as an array: ", '\n', my_data_array)
73+
74+
Note that this array is a genuine, local, numpy array (overloaded by the DPFArray):
75+
76+
.. jupyter-execute::
77+
78+
print("Array type: ", type(my_data_array))
79+
80+
Data as a list
81+
^^^^^^^^^^^^^^
82+
83+
.. jupyter-execute::
84+
85+
# Get the displacement data as a list
86+
my_data_list = my_disp_field.data_as_list
87+
print("Displacement data as a list: ", '\n', my_data_list)
88+
89+
Extract specific data from a field
90+
----------------------------------
91+
92+
If you need to access data for specific entities (node, element ...), you can extract it
93+
based on its index (data position on the |Field| by using the |get_entity_data| method), or based
94+
on the entities id (by using the |get_entity_data_by_id| method).
95+
96+
Get the data by the entity index
97+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98+
99+
.. jupyter-execute::
100+
101+
# Get the data from the third entity in the field
102+
data_3_entity = my_disp_field.get_entity_data(index=3)
103+
print("Data entity index=3: ", data_3_entity)
104+
105+
Get the data by the entity ind
106+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
108+
.. jupyter-execute::
109+
110+
# Get the data from the entity with id=533
111+
data_533_entity = my_disp_field.get_entity_data_by_id(id=533)
112+
print("Data entity id=533: ", data_533_entity)
113+
114+
Note that the element with id=533 would correspond to an index=2 within the |Field|.
115+
116+
.. jupyter-execute::
117+
118+
# Get the index of the entity with id=533
119+
index_533_entity = my_disp_field.scoping.index(id=533)
120+
print("Index entity id=533: ",index_533_entity)
121+
122+
Be aware that scoping IDs are not sequential. You would get the id of the element in the 533
123+
position of the |Field| with:
124+
125+
.. jupyter-execute::
126+
127+
# Get the id of the entity with index=533
128+
id_533_entity = my_disp_field.scoping.id(index=533)
129+
print("Id entity index=533: ",id_533_entity)
130+
131+
132+
While these methods are acceptable when requesting data for a few elements
133+
or nodes, they should not be used when looping over the entire array. For efficiency,
134+
a |Field|s data can be recovered locally before sending a large number of requests:
135+
136+
.. jupyter-execute::
137+
138+
# Create a deep copy of the field that can be accessed and modified locally.
139+
with my_disp_field.as_local_field() as f:
140+
for i in my_disp_field.scoping.ids[2:50]:
141+
f.get_entity_data_by_id(i)
142+
143+
print(f)

0 commit comments

Comments
 (0)