Skip to content

Commit eb67b57

Browse files
update the extract_and_explore_results_data.rst to the tutorials guidelines
1 parent b81a7e4 commit eb67b57

File tree

1 file changed

+81
-60
lines changed

1 file changed

+81
-60
lines changed

doc/source/user_guide/tutorials/import_data/extract_and_explore_results_data.rst

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,140 +4,161 @@
44
Extract and explore results data
55
================================
66

7-
.. |Field| replace:: :class:`Field<ansys.dpf.core.field.Field>`
8-
.. |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>`
7+
.. include:: ../../../links_and_refs.rst
118
.. |get_entity_data| replace:: :func:`get_entity_data()<ansys.dpf.core.field.Field.get_entity_data>`
129
.. |get_entity_data_by_id| replace:: :func:`get_entity_data_by_id()<ansys.dpf.core.field.Field.get_entity_data_by_id>`
1310

1411
This tutorial shows how to extract and explore results data from a result file.
1512

1613
When you extract a result from a result file DPF stores it in a |Field|.
17-
This |Field| will contain the data of the result associated with it.
14+
Thus, this |Field| contains the data of the result associated with it.
1815

19-
When DPF-Core returns the |Field| object, what Python actually has is a client-side
20-
representation of the |Field|, not the entirety of the |Field| itself. This means
21-
that all the data of the field is stored within the DPF service. This is important
22-
because when building your workflows, the most efficient way of interacting with result data
23-
is to minimize the exchange of data between Python and DPF, either by using operators
24-
or by accessing exclusively the data that is needed.
16+
.. note::
2517

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).
18+
When DPF-Core returns the |Field| object, what Python actually has is a client-side
19+
representation of the |Field|, not the entirety of the |Field| itself. This means
20+
that all the data of the field is stored within the DPF service. This is important
21+
because when building your workflows, the most efficient way of interacting with result data
22+
is to minimize the exchange of data between Python and DPF, either by using operators
23+
or by accessing exclusively the data that is needed.
2824

29-
Get the results
30-
---------------
25+
:jupyter-download-script:`Download tutorial as Python script<extract_and_explore_results_data>`
26+
:jupyter-download-notebook:`Download tutorial as Jupyter notebook<extract_and_explore_results_data>`
3127

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.
28+
Get the result file
29+
-------------------
3530

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|.
31+
First, import a result file. For this tutorial, you can use one available in the |Examples| module.
32+
For more information about how to import your own result file in DPF, see the :ref:`ref_tutorials_import_result_file`
33+
tutorial.
34+
35+
Here, we extract the displacement results. The displacement |Result| object gives a |FieldsContainer| when evaluated.
36+
Thus, we get a |Field| from this |FieldsContainer|.
3837

3938
.. jupyter-execute::
4039

41-
# Import the ``ansys.dpf.core`` module, including examples files and the operators subpackage
40+
# Import the ``ansys.dpf.core`` module
4241
from ansys.dpf import core as dpf
42+
# Import the examples module
4343
from ansys.dpf.core import examples
44+
# Import the operators module
4445
from ansys.dpf.core import operators as ops
4546

46-
# Define the result file
47+
# Define the result file path
4748
result_file_path_1 = examples.download_transient_result()
4849

4950
# Create the model
50-
my_model_1 = dpf.Model(data_sources=result_file_path_1)
51+
model_1 = dpf.Model(data_sources=result_file_path_1)
5152

5253
# Extract the displacement results for the last time step
53-
disp_results = my_model_1.results.displacement.on_last_time_freq.eval()
54+
disp_results = model_1.results.displacement.on_last_time_freq.eval()
5455

5556
# Get the displacement field for the last time step
56-
my_disp_field = disp_results[0]
57+
disp_field = disp_results[0]
58+
59+
# Print the displacement Field
60+
print(disp_field)
5761

58-
print(my_disp_field)
62+
Extract all the data from a |Field|
63+
-----------------------------------
5964

60-
Extract all data from a field
61-
-----------------------------
65+
You can extract the entire data in a |Field| as:
6266

63-
You can extract the the entire data in the |Field| as an array (numpy array) or as a list.
67+
- An array (numpy array);
68+
- A list.
6469

6570
Data as an array
6671
^^^^^^^^^^^^^^^^
6772

6873
.. jupyter-execute::
6974

7075
# 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)
76+
data_array = disp_field.data
77+
78+
# Print the data as an array
79+
print("Displacement data as an array: ", '\n', data_array)
7380

7481
Note that this array is a genuine, local, numpy array (overloaded by the DPFArray):
7582

7683
.. jupyter-execute::
7784

78-
print("Array type: ", type(my_data_array))
85+
# Print the array type
86+
print("Array type: ", type(data_array))
7987

8088
Data as a list
8189
^^^^^^^^^^^^^^
8290

8391
.. jupyter-execute::
8492

8593
# 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)
94+
data_list = disp_field.data_as_list
95+
# Print the data as a list
96+
print("Displacement data as a list: ", '\n', data_list)
8897

8998
Extract specific data from a field
9099
----------------------------------
91100

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).
101+
If you need to access data for specific entities (node, element ...), you can extract it with two approaches:
95102

96-
Get the data by the entity index
97-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103+
- :ref:`Based on its index <ref_extract_specific_data_by_index>` (data position on the |Field|) by using the |get_entity_data| method;
104+
- :ref:`Based on the entities id <ref_extract_specific_data_by_id>` by using the |get_entity_data_by_id| method.
105+
106+
The |Field| data is organized with respect to its scoping ids. Note that the element with id=533
107+
would correspond to an index=2 within the |Field|.
98108

99109
.. jupyter-execute::
100110

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)
111+
# Get the index of the entity with id=533
112+
index_533_entity = disp_field.scoping.index(id=533)
113+
# Print the index
114+
print("Index entity id=533: ",index_533_entity)
104115

105-
Get the data by the entity ind
106-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
Be aware that scoping IDs are not sequential. You would get the id of the element in the 533
117+
position of the |Field| with:
107118

108119
.. jupyter-execute::
109120

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)
121+
# Get the id of the entity with index=533
122+
id_533_entity = disp_field.scoping.id(index=533)
123+
print("Id entity index=533: ",id_533_entity)
124+
125+
.. _ref_extract_specific_data_by_index:
113126

114-
Note that the element with id=533 would correspond to an index=2 within the |Field|.
127+
Get the data by the entity index
128+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115129

116130
.. jupyter-execute::
117131

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)
132+
# Get the data from the third entity in the field
133+
data_3_entity = disp_field.get_entity_data(index=3)
134+
# Print the data
135+
print("Data entity index=3: ", data_3_entity)
121136

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:
137+
.. _ref_extract_specific_data_by_id:
138+
139+
Get the data by the entity id
140+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
124141

125142
.. jupyter-execute::
126143

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)
144+
# Get the data from the entity with id=533
145+
data_533_entity = disp_field.get_entity_data_by_id(id=533)
146+
# Print the data
147+
print("Data entity id=533: ", data_533_entity)
130148

149+
Extract specific data from a field using a loop over the array
150+
--------------------------------------------------------------
131151

132-
While these methods are acceptable when requesting data for a few elements
152+
While the methods above are acceptable when requesting data for a few elements
133153
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:
154+
a |Field| data can be recovered locally before sending a large number of requests:
135155

136156
.. jupyter-execute::
137157

138158
# 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]:
159+
with disp_field.as_local_field() as f:
160+
for i in disp_field.scoping.ids[2:50]:
141161
f.get_entity_data_by_id(i)
142162

163+
# Print the field
143164
print(f)

0 commit comments

Comments
 (0)