|
4 | 4 | Extract and explore results data |
5 | 5 | ================================ |
6 | 6 |
|
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 |
11 | 8 | .. |get_entity_data| replace:: :func:`get_entity_data()<ansys.dpf.core.field.Field.get_entity_data>` |
12 | 9 | .. |get_entity_data_by_id| replace:: :func:`get_entity_data_by_id()<ansys.dpf.core.field.Field.get_entity_data_by_id>` |
13 | 10 |
|
14 | 11 | This tutorial shows how to extract and explore results data from a result file. |
15 | 12 |
|
16 | 13 | 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. |
18 | 15 |
|
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:: |
25 | 17 |
|
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. |
28 | 24 |
|
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>` |
31 | 27 |
|
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 | +------------------- |
35 | 30 |
|
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|. |
38 | 37 |
|
39 | 38 | .. jupyter-execute:: |
40 | 39 |
|
41 | | - # Import the ``ansys.dpf.core`` module, including examples files and the operators subpackage |
| 40 | + # Import the ``ansys.dpf.core`` module |
42 | 41 | from ansys.dpf import core as dpf |
| 42 | + # Import the examples module |
43 | 43 | from ansys.dpf.core import examples |
| 44 | + # Import the operators module |
44 | 45 | from ansys.dpf.core import operators as ops |
45 | 46 |
|
46 | | - # Define the result file |
| 47 | + # Define the result file path |
47 | 48 | result_file_path_1 = examples.download_transient_result() |
48 | 49 |
|
49 | 50 | # 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) |
51 | 52 |
|
52 | 53 | # 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() |
54 | 55 |
|
55 | 56 | # 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) |
57 | 61 |
|
58 | | - print(my_disp_field) |
| 62 | +Extract all the data from a |Field| |
| 63 | +----------------------------------- |
59 | 64 |
|
60 | | -Extract all data from a field |
61 | | ------------------------------ |
| 65 | +You can extract the entire data in a |Field| as: |
62 | 66 |
|
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. |
64 | 69 |
|
65 | 70 | Data as an array |
66 | 71 | ^^^^^^^^^^^^^^^^ |
67 | 72 |
|
68 | 73 | .. jupyter-execute:: |
69 | 74 |
|
70 | 75 | # 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) |
73 | 80 |
|
74 | 81 | Note that this array is a genuine, local, numpy array (overloaded by the DPFArray): |
75 | 82 |
|
76 | 83 | .. jupyter-execute:: |
77 | 84 |
|
78 | | - print("Array type: ", type(my_data_array)) |
| 85 | + # Print the array type |
| 86 | + print("Array type: ", type(data_array)) |
79 | 87 |
|
80 | 88 | Data as a list |
81 | 89 | ^^^^^^^^^^^^^^ |
82 | 90 |
|
83 | 91 | .. jupyter-execute:: |
84 | 92 |
|
85 | 93 | # 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) |
88 | 97 |
|
89 | 98 | Extract specific data from a field |
90 | 99 | ---------------------------------- |
91 | 100 |
|
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: |
95 | 102 |
|
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|. |
98 | 108 |
|
99 | 109 | .. jupyter-execute:: |
100 | 110 |
|
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) |
104 | 115 |
|
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: |
107 | 118 |
|
108 | 119 | .. jupyter-execute:: |
109 | 120 |
|
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: |
113 | 126 |
|
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 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
115 | 129 |
|
116 | 130 | .. jupyter-execute:: |
117 | 131 |
|
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) |
121 | 136 |
|
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 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
124 | 141 |
|
125 | 142 | .. jupyter-execute:: |
126 | 143 |
|
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) |
130 | 148 |
|
| 149 | +Extract specific data from a field using a loop over the array |
| 150 | +-------------------------------------------------------------- |
131 | 151 |
|
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 |
133 | 153 | 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: |
135 | 155 |
|
136 | 156 | .. jupyter-execute:: |
137 | 157 |
|
138 | 158 | # 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]: |
141 | 161 | f.get_entity_data_by_id(i) |
142 | 162 |
|
| 163 | + # Print the field |
143 | 164 | print(f) |
0 commit comments