.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\data_structures\data_arrays.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_data_structures_data_arrays.py: .. _ref_tutorials_data_arrays: Data Arrays =========== To process your data with DPF, you must format it according to the DPF data model. You can achieve this either by using DPF data readers on result files, or by using data to build DPF data storage containers. It is important to be aware of how the data is structured in those containers to understand how to create them and how operators process them. The data containers can be: - **Raw data storage structures**: data arrays (such as a :class:`Field `) or data maps (such as a ``DataTree``) - **Collections**: homogeneous groups of labeled raw data storage structures (such as a :class:`FieldsContainer ` for a group of labeled fields) This tutorial presents how to define and manipulate DPF data arrays specifically. .. GENERATED FROM PYTHON SOURCE LINES 45-81 Introduction ------------ A data array in DPF usually represents a mathematical field, hence the base name :class:`Field `. Different types of :class:`Field ` store different data types: - a :class:`Field ` stores float values - a :class:`StringField ` stores string values - a :class:`PropertyField ` stores integer values - a :class:`CustomTypeField ` stores values of a custom type (among valid ``numpy.dtype``) A :class:`Field ` is always associated to: - a ``location``, which defines the type of entity the data applies to. Check the :class:`locations ` list to know what is available. Locations related to mesh entities include: ``nodal``, ``elemental``, ``elemental_nodal``, ``zone``, and ``faces``. Locations related to time, frequency, or mode are ``modal``, ``time_freq``, and ``time_freq_step``. - a ``scoping``, which is the list of entity IDs each data point in the :class:`Field ` relates to. For example, the ``scoping`` of a ``nodal`` :class:`Field ` represents a list of node IDs. It can represent a subset of the ``support`` of the field. The data in a :class:`Field ` is ordered the same way as the IDs in its ``scoping``. - a ``support``, which is a data container holding information about the model for the type of entity the ``location`` targets. If the ``location`` relates to mesh entities such as nodes or elements, the ``support`` of the :class:`Field ` is a :class:`MeshedRegion `. - a ``dimensionality``, which gives the structure of the data based on the number of components and dimensions. A DPF :class:`Field ` can store data for a 3D vector field, a scalar field, a matrix field, or a multi-component field (for example, a symmetrical matrix field for each component of the stress field). - a ``data`` array, which holds the actual data in a vector, accessed according to the ``dimensionality``. .. GENERATED FROM PYTHON SOURCE LINES 83-93 Create fields based on result files ------------------------------------ In this section we use the result file from a fluid analysis to showcase the :class:`Field `, :class:`PropertyField `, and :class:`StringField `. The :class:`Model ` class creates and evaluates common readers for the files it is given, such as a mesh provider, a result info provider, and a streams provider. It provides dynamically built methods to extract the results available in the files, as well as many shortcuts to facilitate exploration of the available data. .. GENERATED FROM PYTHON SOURCE LINES 93-107 .. code-block:: Python # Import the ansys.dpf.core module as ``dpf`` from ansys.dpf import core as dpf # Import the examples module from ansys.dpf.core import examples # Create a data source targeting the example file my_data_sources = dpf.DataSources(result_path=examples.download_fluent_axial_comp()["flprj"]) # Create a model from the data source my_model = dpf.Model(data_sources=my_data_sources) # Print information available for the analysis print(my_model) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Model ------------------------------ Transient analysis Unit system: Custom: m, kg, N, s, V, A, K Physics Type: Fluid Available results: - enthalpy: Elemental Enthalpy - mass_flow_rate: Faces Mass Flow Rate - static_pressure: ElementalAndFaces Static Pressure - mean_static_pressure: Elemental Mean Static Pressure - rms_static_pressure: Elemental Rms Static Pressure - surface_heat_rate: Faces Surface Heat Rate - density: ElementalAndFaces Density - face_artificial_wall_flag: Faces Face Artificial Wall Flag - body_force: Elemental Body Force - density_t_1: Elemental Density T 1 - pressure_discontinuity_sensor: Elemental Pressure Discontinuity Sensor - dpm_partition: Elemental Dpm Partition - nucleation_rate_of_water_droplet: Faces Nucleation Rate Of Water Droplet - energy_m1: Elemental Energy M1 - mass_flux_m1: Faces Mass Flux M1 - pressure_m1: Elemental Pressure M1 - radiation_heat_flow_rate: Faces Radiation Heat Flow Rate - temperature_m1: ElementalAndFaces Temperature M1 - temperature_m2: Faces Temperature M2 - x_velocity_m1: ElementalAndFaces X Velocity M1 - y_velocity_m1: ElementalAndFaces Y Velocity M1 - wall_velocity: Faces Wall Velocity - original_wall_velocity: Faces Original Wall Velocity - z_velocity_m1: ElementalAndFaces Z Velocity M1 - wall_shear_stress: Faces Wall Shear Stress - temperature: ElementalAndFaces Temperature - mean_temperature: ElementalAndFaces Mean Temperature - rms_temperature: Elemental Rms Temperature - velocity: ElementalAndFaces Velocity - mean_velocity: Elemental Mean Velocity - rms_velocity: Elemental Rms Velocity Available qualifier labels: - zone: default-interior:0 (2), rotor-hub (3), rotor-shroud (4), rotor-inlet (5), rotor-interface (6), rotor-blade-1 (7), rotor-blade-2 (8), rotor-per-1-shadow (9), rotor-per-1 (10), rotor-per-2-shadow (11), rotor-per-2 (12), fluid-rotor (13), default-interior (15), stator-hub (16), stator-shroud (17), stator-interface (18), stator-outlet (19), stator-blade-1 (20), stator-blade-2 (21), stator-blade-3 (22), stator-blade-4 (23), stator-per-2 (24), stator-per-2-shadow (25), stator-per-1 (26), stator-per-1-shadow (27), fluid-stator (28) - phase: phase-1 (1) ------------------------------ DPF Meshed Region: 16660 nodes 13856 elements 44242 faces Unit: m With solid (3D) elements ------------------------------ DPF Time/Freq Support: Number of sets: 3 Cumulative Time (s) LoadStep Substep 1 0.009587 1 1 2 0.009593 2 1 3 0.009600 3 1 .. GENERATED FROM PYTHON SOURCE LINES 108-110 The :class:`MeshInfo ` class stores information relative to the :class:`MeshedRegion ` of the analysis. It stores some of its data as fields of strings or fields of integers, which we extract next. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. code-block:: Python # Get the mesh metadata my_mesh_info = my_model.metadata.mesh_info print(my_mesh_info) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF MeshInfo ------------------------------ with properties: num_cells int num_nodes int num_faces int body_names StringField body_cell_topology PropertyField body_face_topology PropertyField body_scoping Scoping cell_zone_names StringField cell_zone_elements PropertyField cell_zone_scoping Scoping face_zone_names StringField face_zone_elements PropertyField face_zone_scoping Scoping zone_names StringField num_elem_zone PropertyField zone_scoping Scoping splittable_by StringField .. GENERATED FROM PYTHON SOURCE LINES 116-123 The following shows how to obtain the three field types from an existing analysis. Field ^^^^^ You can obtain a :class:`Field ` from a model by requesting a result. The field is located on nodes since it stores the temperature at each node. .. GENERATED FROM PYTHON SOURCE LINES 123-128 .. code-block:: Python # Request the collection of temperature result fields from the model and take the first one. my_temp_field = my_model.results.temperature.eval()[0] print(my_temp_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF T Field Location: Elemental Unit: K 13856 entities Data: 1 components and 13856 elementary data Elemental IDs data(K) ------------ ---------- 1 2.761285e+02 2 2.760320e+02 3 2.761719e+02 ... .. GENERATED FROM PYTHON SOURCE LINES 129-135 StringField ^^^^^^^^^^^ You can obtain a :class:`StringField ` from a :class:`MeshInfo ` by requesting the names of the zones in the model. The field is located on zones since it stores the name of each zone. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python # Request the name of the face zones in the fluid analysis my_string_field = my_mesh_info.get_property(property_name="face_zone_names") print(my_string_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF String Field 24 zone entities Data:24 elementary data zone IDs data ------------ ---------- 2 default-interior:0 3 rotor-hub 4 rotor-shroud ... .. GENERATED FROM PYTHON SOURCE LINES 141-147 PropertyField ^^^^^^^^^^^^^ You can obtain a :class:`PropertyField ` from a :class:`MeshInfo ` by requesting the element types in the mesh. The field is located on elements since it stores the element type ID for each element. .. GENERATED FROM PYTHON SOURCE LINES 147-152 .. code-block:: Python # Get the body_face_topology property field my_property_field = my_mesh_info.get_property(property_name="body_face_topology") print(my_property_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Property Field 2 entities Data: 1 components and 24 elementary data Body IDs data ------------ ---------- 13 2 3 4 ... 28 15 16 17 ... .. GENERATED FROM PYTHON SOURCE LINES 153-165 Create fields from scratch -------------------------- You can also create a :class:`Field `, :class:`StringField `, or :class:`PropertyField ` from scratch based on your data. Field ^^^^^ Create a 3D vector field defined for two nodes: .. GENERATED FROM PYTHON SOURCE LINES 165-179 .. code-block:: Python # Create a 3D vector field ready to hold data for two entities # The constructor creates 3D vector fields by default my_scratch_field = dpf.Field(nentities=2) # Set the data values as a flat vector my_scratch_field.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Associate the data to nodes my_scratch_field.location = dpf.locations.nodal # Set the IDs of the nodes the data applies to my_scratch_field.scoping.ids = [1, 2] # Define the unit (only available for the Field type) my_scratch_field.unit = "m" print(my_scratch_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: m 2 entities Data: 3 components and 2 elementary data IDs data(m) ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 2 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 180-181 Create a 3x3 symmetric matrix field defined for a single element: .. GENERATED FROM PYTHON SOURCE LINES 181-191 .. code-block:: Python # Set the nature to symmatrix my_symmatrix_field = dpf.Field(nentities=1, nature=dpf.natures.symmatrix) # The symmatrix dimensions default to 3x3 my_symmatrix_field.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] my_symmatrix_field.location = dpf.locations.elemental my_symmatrix_field.scoping.ids = [1] my_symmatrix_field.unit = "Pa" print(my_symmatrix_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Elemental Unit: Pa 1 entities Data: 6 components and 1 elementary data IDs data(Pa) ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 192-193 Create a 2x3 matrix field defined for a single fluid element face: .. GENERATED FROM PYTHON SOURCE LINES 193-203 .. code-block:: Python # Set the nature to matrix my_matrix_field = dpf.Field(nentities=1, nature=dpf.natures.matrix) my_matrix_field.dimensionality = dpf.Dimensionality(dim_vec=[2, 3], nature=dpf.natures.matrix) my_matrix_field.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] my_matrix_field.location = dpf.locations.faces my_matrix_field.scoping.ids = [1] my_matrix_field.unit = "mm" print(my_matrix_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Faces Unit: mm 1 entities Data: 6 components and 1 elementary data IDs data(mm) ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 204-206 StringField ^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 206-214 .. code-block:: Python # Create a string field with data for two elements my_scratch_string_field = dpf.StringField(nentities=2) my_scratch_string_field.data = ["string_1", "string_2"] my_scratch_string_field.location = dpf.locations.elemental my_scratch_string_field.scoping.ids = [1, 2] print(my_scratch_string_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF String Field 2 Elemental entities Data:2 elementary data Elemental IDs data ------------ ---------- 1 string_1 2 string_2 .. GENERATED FROM PYTHON SOURCE LINES 215-217 PropertyField ^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 217-232 .. code-block:: Python # Create a property field with data for two modes from ansys.dpf.core.check_version import meets_version my_scratch_property_field = dpf.PropertyField(nentities=2) my_scratch_property_field.data = [12, 25] # For DPF 26R1 and above, directly set the location of the PropertyField if meets_version(dpf.SERVER.version, "11.0"): my_scratch_property_field.location = dpf.locations.modal # For DPF older than 26R1, you must set the location with a Scoping else: my_scratch_property_field.scoping = dpf.Scoping(location=dpf.locations.modal) my_scratch_property_field.scoping.ids = [1, 2] print(my_scratch_property_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Property Field 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 12 2 25 .. GENERATED FROM PYTHON SOURCE LINES 233-244 Create fields with the fields_factory -------------------------------------- The :mod:`fields_factory ` module provides helpers to create a :class:`Field `. Scalar field ^^^^^^^^^^^^ Use :func:`create_scalar_field ` to create a scalar field: .. GENERATED FROM PYTHON SOURCE LINES 244-250 .. code-block:: Python my_scalar_field = dpf.fields_factory.create_scalar_field(num_entities=2) my_scalar_field.data = [1.0, 2.0] my_scalar_field.scoping.ids = [1, 2] print(my_scalar_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 251-256 Generic vector field ^^^^^^^^^^^^^^^^^^^^ Use :func:`create_vector_field ` to create a generic vector field with a custom number of components: .. GENERATED FROM PYTHON SOURCE LINES 256-262 .. code-block:: Python my_vector_field = dpf.fields_factory.create_vector_field(num_entities=2, num_comp=2) my_vector_field.data = [1.0, 2.0, 3.0, 4.0] my_vector_field.scoping.ids = [1, 2] print(my_vector_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 2 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 2 3.000000e+00 4.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 263-268 3D vector field ^^^^^^^^^^^^^^^ Use :func:`create_3d_vector_field ` to create a 3D vector field (3 components per entity): .. GENERATED FROM PYTHON SOURCE LINES 268-274 .. code-block:: Python my_3d_field = dpf.fields_factory.create_3d_vector_field(num_entities=2) my_3d_field.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] my_3d_field.scoping.ids = [1, 2] print(my_3d_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 2 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 275-280 Generic matrix field ^^^^^^^^^^^^^^^^^^^^ Use :func:`create_matrix_field ` to create a generic matrix field with custom dimensions: .. GENERATED FROM PYTHON SOURCE LINES 280-286 .. code-block:: Python my_matrix_ff_field = dpf.fields_factory.create_matrix_field(num_entities=2, num_lines=2, num_col=3) my_matrix_ff_field.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0] my_matrix_ff_field.scoping.ids = [1, 2] print(my_matrix_ff_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 6 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 4.000000e+00 5.000000e+00 6.000000e+00 2 7.000000e+00 8.000000e+00 9.000000e+00 1.000000e+01 1.100000e+01 1.200000e+01 .. GENERATED FROM PYTHON SOURCE LINES 287-292 3x3 matrix field (tensor) ^^^^^^^^^^^^^^^^^^^^^^^^^ Use :func:`create_tensor_field ` to create a 3x3 matrix field: .. GENERATED FROM PYTHON SOURCE LINES 292-317 .. code-block:: Python my_tensor_field = dpf.fields_factory.create_tensor_field(num_entities=2) my_tensor_field.data = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, ] my_tensor_field.scoping.ids = [1, 2] print(my_tensor_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 6 components and 3 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 4.000000e+00 5.000000e+00 6.000000e+00 2 7.000000e+00 8.000000e+00 9.000000e+00 1.000000e+01 1.100000e+01 1.200000e+01 .. GENERATED FROM PYTHON SOURCE LINES 318-323 Overall field ^^^^^^^^^^^^^ Use :func:`create_overall_field ` to create a field with a single value applied to the whole support: .. GENERATED FROM PYTHON SOURCE LINES 323-327 .. code-block:: Python my_overall_field = dpf.fields_factory.create_overall_field(value=1.0) print(my_overall_field) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: overall Unit: 1 entities Data: 1 components and 1 elementary data IDs data ------------ ---------- 0 1.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 328-334 Field from array ^^^^^^^^^^^^^^^^ Use :func:`field_from_array ` to create a scalar, 3D vector, or symmetric matrix field directly from a numpy array or a Python list: .. GENERATED FROM PYTHON SOURCE LINES 334-340 .. code-block:: Python # Scalar field from a 1D list arr = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] my_field_from_array = dpf.fields_factory.field_from_array(arr=arr) print(my_field_from_array) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 6 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 3 3.000000e+00 ... .. GENERATED FROM PYTHON SOURCE LINES 341-342 3D vector field from a 2D list: .. GENERATED FROM PYTHON SOURCE LINES 342-347 .. code-block:: Python arr = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] my_field_from_array = dpf.fields_factory.field_from_array(arr=arr) print(my_field_from_array) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 2 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 348-349 Symmetric matrix field from a 2D list: .. GENERATED FROM PYTHON SOURCE LINES 349-354 .. code-block:: Python arr = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]] my_field_from_array = dpf.fields_factory.field_from_array(arr=arr) print(my_field_from_array) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 1 entities Data: 6 components and 1 elementary data IDs data ------------ ---------- 1 1.000000e+00 2.000000e+00 3.000000e+00 4.000000e+00 5.000000e+00 6.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 355-363 Access the field metadata ------------------------- The metadata associated to a field includes its location, its scoping, the shape of the data stored, its number of components, and its unit. Field ^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 363-381 .. code-block:: Python # Location of the field's data print("location\n", my_temp_field.location, "\n") # Field scoping (entity type and IDs) print("scoping\n", my_temp_field.scoping, "\n") # Available IDs of location entities print("scoping.ids\n", my_temp_field.scoping.ids, "\n") # Number of location entities (how many data vectors we have) print("elementary_data_count\n", my_temp_field.elementary_data_count, "\n") # Number of components per entity (e.g. 1 for temperature) print("components_count\n", my_temp_field.component_count, "\n") # Total length of data (elementary_data_count * component_count) print("size\n", my_temp_field.size, "\n") # Shape as (elementary_data_count, component_count) print("shape\n", my_temp_field.shape, "\n") # Unit (only available on Field, not StringField or PropertyField) print("unit\n", my_temp_field.unit, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none location Elemental scoping DPF Scoping: with Elemental location and 13856 entities scoping.ids [ 1 2 3 ... 13854 13855 13856] elementary_data_count 13856 components_count 1 size 13856 shape 13856 unit K .. GENERATED FROM PYTHON SOURCE LINES 382-384 StringField ^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 384-393 .. code-block:: Python print("location\n", my_string_field.location, "\n") print("scoping\n", my_string_field.scoping, "\n") print("scoping.ids\n", my_string_field.scoping.ids, "\n") print("elementary_data_count\n", my_string_field.elementary_data_count, "\n") print("components_count\n", my_string_field.component_count, "\n") print("size\n", my_string_field.size, "\n") print("shape\n", my_string_field.shape, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none location zone scoping DPF Scoping: with zone location and 24 entities scoping.ids [ 2 3 4 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22 23 24 25 26 27] elementary_data_count 24 components_count 1 size 24 shape 24 .. GENERATED FROM PYTHON SOURCE LINES 394-396 PropertyField ^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 396-405 .. code-block:: Python print("location\n", my_property_field.location, "\n") print("scoping\n", my_property_field.scoping, "\n") print("scoping.ids\n", my_property_field.scoping.ids, "\n") print("elementary_data_count\n", my_property_field.elementary_data_count, "\n") print("components_count\n", my_property_field.component_count, "\n") print("size\n", my_property_field.size, "\n") print("shape\n", my_property_field.shape, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none location none scoping DPF Scoping: with Body location and 2 entities scoping.ids [13 28] elementary_data_count 24 components_count 1 size 24 shape 24 .. GENERATED FROM PYTHON SOURCE LINES 406-424 Access the field data --------------------- A :class:`Field ` object is a client-side representation of the field on the server side. When a remote DPF server is used, the data of the field is also stored remotely. To build efficient remote postprocessing workflows, the amount of data exchanged between the client and the remote server has to be minimal. This is managed with operators and a completely remote workflow, requesting only the initial data needed to build the workflow, and the output of the workflow. It is important when interacting with remote data to remember that any PyDPF request for ``Field.data`` downloads the whole array to your local machine. This is particularly inefficient within scripts handling large amounts of data where the request is made to perform an action locally which could have been made remotely with a DPF operator. For example, if you want the entity-wise maximum of the field, prefer the ``min_max.min_max_by_entity`` operator to ``array.max()`` from NumPy. .. GENERATED FROM PYTHON SOURCE LINES 426-434 Get the complete array ^^^^^^^^^^^^^^^^^^^^^^ The field's ``data`` is ordered with respect to its ``scoping ids``. To access the entire data in the field as a NumPy array: Field ''''' .. GENERATED FROM PYTHON SOURCE LINES 434-441 .. code-block:: Python # For a Field, .data returns a DPFArray (a local numpy array) my_data_array = my_temp_field.data print(my_data_array) # Note: this array is a genuine, local numpy array (overloaded as DPFArray). print(type(my_data_array)) .. rst-class:: sphx-glr-script-out .. code-block:: none [276.12852414 276.03204537 276.17186547 ... 304.13925596 324.34370722 316.09748279] .. GENERATED FROM PYTHON SOURCE LINES 442-444 StringField ''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 444-448 .. code-block:: Python my_string_data_array = my_string_field.data print(my_string_data_array) .. rst-class:: sphx-glr-script-out .. code-block:: none DPFVectorString['default-interior:0', 'rotor-hub', 'rotor-shroud', 'rotor-inlet', 'rotor-interface', ..., 'stator-per-1-shadow'] .. GENERATED FROM PYTHON SOURCE LINES 449-451 PropertyField ''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 451-455 .. code-block:: Python my_property_data_array = my_property_field.data print(my_property_data_array) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 2 3 4 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22 23 24 25 26 27] .. GENERATED FROM PYTHON SOURCE LINES 456-462 Get data for a single entity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If you need to access an individual node or element, request it using either :func:`get_entity_data()` or :func:`get_entity_data_by_id()`: .. GENERATED FROM PYTHON SOURCE LINES 462-467 .. code-block:: Python # Get the data from the element at index 3 in the field entity_data = my_temp_field.get_entity_data(index=3) print(entity_data) .. rst-class:: sphx-glr-script-out .. code-block:: none [275.3473736] .. GENERATED FROM PYTHON SOURCE LINES 468-473 .. code-block:: Python # Get the data from the element with ID 533 entity_data_by_id = my_temp_field.get_entity_data_by_id(id=533) print(entity_data_by_id) .. rst-class:: sphx-glr-script-out .. code-block:: none [276.30236262] .. GENERATED FROM PYTHON SOURCE LINES 474-477 Note that this would correspond to an index of 2 within the field. Be aware that scoping IDs are not sequential. You would get the index of element 532 in the field with: .. GENERATED FROM PYTHON SOURCE LINES 477-482 .. code-block:: Python # Get the index of element 533 in the field entity_index = my_temp_field.scoping.index(id=533) print(entity_index) .. rst-class:: sphx-glr-script-out .. code-block:: none 532 .. GENERATED FROM PYTHON SOURCE LINES 483-486 While these methods are acceptable when requesting data for a few elements or nodes, they should not be used when looping over the entire array. For efficiency, a field's data can be recovered locally before sending a large number of requests: .. GENERATED FROM PYTHON SOURCE LINES 486-492 .. code-block:: Python # Create a deep copy of the field that can be accessed and modified locally. with my_temp_field.as_local_field() as f: for i in range(1, 100): f.get_entity_data_by_id(i) .. GENERATED FROM PYTHON SOURCE LINES 493-500 .. tip:: When using a remote DPF server, accessing a field's data within the ``with`` context manager ensures deletion of local data when exiting the ``with`` block. Following this approach is advisable for efficient remote processing workflows since it guarantees non-persistence of unnecessary local data, especially if the data is not needed beyond the code being executed within the ``with`` block. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.130 seconds) .. _sphx_glr_download_tutorials_data_structures_data_arrays.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: data_arrays.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: data_arrays.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: data_arrays.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_