.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\import_data\load_custom_data.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_import_data_load_custom_data.py: .. _ref_tutorials_load_custom_data: Load custom data in DPF ======================= This tutorial shows how to represent your custom data in DPF data storage structures. To import your custom data in DPF, you must create a DPF data structure to store it. DPF uses :class:`Field` and :class:`FieldsContainer` objects to handle data. The ``Field`` is a homogeneous array and a ``FieldsContainer`` is a labeled collection of ``Field`` objects. For more information on DPF data structures, see the :ref:`ref_tutorials_data_structures` tutorials section. .. GENERATED FROM PYTHON SOURCE LINES 40-44 Define the data --------------- Create Python lists with the data to be stored in the Fields. .. GENERATED FROM PYTHON SOURCE LINES 44-59 .. code-block:: Python # Data for the scalar Fields (lists with 1 and 2 dimensions) data_1 = [6.0, 5.0, 4.0, 3.0, 2.0, 1.0] data_2 = [[12.0, 7.0, 8.0], [9.0, 31.0, 1.0]] # Data for the vector Fields (lists with 1 and 2 dimensions) data_3 = [4.0, 1.0, 8.0, 5.0, 7.0, 9.0] data_4 = [6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 9.0, 7.0, 8.0, 10.0] data_5 = [[8.0, 4.0, 3.0], [31.0, 5.0, 7.0]] # Data for the matrix Fields data_6 = [3.0, 2.0, 1.0, 7.0] data_7 = [15.0, 3.0, 9.0, 31.0, 1.0, 42.0, 5.0, 68.0, 13.0] data_8 = [[12.0, 7.0, 8.0], [1.0, 4.0, 27.0], [98.0, 4.0, 6.0]] .. GENERATED FROM PYTHON SOURCE LINES 60-61 Create Python lists with the data to be *appended* to the Fields. .. GENERATED FROM PYTHON SOURCE LINES 61-71 .. code-block:: Python # Data for the scalar Fields data_9 = [24.0] # Data for the vector Fields data_10 = [47.0, 33.0, 5.0] # Data for the matrix Fields data_11 = [8.0, 2.0, 4.0, 64.0, 32.0, 47.0, 11.0, 23.0, 1.0] .. GENERATED FROM PYTHON SOURCE LINES 72-76 Import the PyDPF-Core library ----------------------------- Import the ``ansys.dpf.core`` module. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python from ansys.dpf import core as dpf .. GENERATED FROM PYTHON SOURCE LINES 80-92 Define the Fields sizing ------------------------ A :class:`Field` must always be given: - A :class:`location` and a :class:`Scoping`. - A :class:`nature` and a :class:`dimensionality` (number of data components for each entity). Define the number of entities for scalar, vector, and matrix Fields. .. GENERATED FROM PYTHON SOURCE LINES 92-102 .. code-block:: Python # Scalar Fields: 6 entities with 1 component each num_entities_1 = 6 # Vector Fields: 2 entities with 3 or 5 components each num_entities_2 = 2 # Matrix Fields: 1 entity num_entities_3 = 1 .. GENERATED FROM PYTHON SOURCE LINES 103-112 Create scalar Fields -------------------- **Create a scalar Field by instantiating the** ``Field`` **object** The default ``nature`` of the ``Field`` object is ``'vector'``. Use the ``nature`` argument or the :func:`Field.dimensionality` method to create a scalar Field. .. GENERATED FROM PYTHON SOURCE LINES 112-122 .. code-block:: Python # Instantiate the Field with a scalar nature field_11 = dpf.Field(nentities=num_entities_1, nature=dpf.common.natures.scalar) # Set the scoping ids field_11.scoping.ids = range(num_entities_1) # Print the Field print("Scalar Field: ", "\n", field_11, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field: DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 123-124 Instantiate the Field and use the ``dimensionality`` method to set scalar nature. .. GENERATED FROM PYTHON SOURCE LINES 124-130 .. code-block:: Python field_12 = dpf.Field(nentities=num_entities_1) field_12.dimensionality = dpf.Dimensionality([1]) field_12.scoping.ids = range(num_entities_1) print("Scalar Field: ", "\n", field_12, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field: DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 131-135 **Create a scalar Field using the** ``fields_factory`` **module** Use the :func:`create_scalar_field()` function. Its default ``nature`` is ``'scalar'`` with a ``'1D'`` dimensionality. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python field_13 = dpf.fields_factory.create_scalar_field(num_entities=num_entities_1) field_13.scoping.ids = range(num_entities_1) print("Scalar Field: ", "\n", field_13, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field: DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 141-143 Use the :func:`field_from_array()` function, which takes the data directly as input. .. GENERATED FROM PYTHON SOURCE LINES 143-148 .. code-block:: Python field_14 = dpf.fields_factory.field_from_array(arr=data_1) field_14.scoping.ids = range(num_entities_1) print("Scalar Field from array: ", "\n", field_14, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field from array: DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 6 elementary data IDs data ------------ ---------- 0 6.000000e+00 1 5.000000e+00 2 4.000000e+00 ... .. GENERATED FROM PYTHON SOURCE LINES 149-157 Create vector Fields -------------------- **Create vector Fields by instantiating the** ``Field`` **object** The default ``nature`` is ``'vector'`` and the default dimensionality is ``'3D'``. Use the :func:`Field.dimensionality` method to set a different dimensionality (here ``'5D'``). .. GENERATED FROM PYTHON SOURCE LINES 157-169 .. code-block:: Python # 3D vector Field (default dimensionality) field_21 = dpf.Field(nentities=num_entities_2) field_21.scoping.ids = range(num_entities_2) print("3D vector Field: ", "\n", field_21, "\n") # 5D vector Field field_31 = dpf.Field(nentities=num_entities_2) field_31.dimensionality = dpf.Dimensionality([5]) field_31.scoping.ids = range(num_entities_2) print("5D vector Field: ", "\n", field_31, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none 3D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 0 elementary data 5D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 5 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 170-174 **Create vector Fields using the** ``fields_factory`` **module** Use the :func:`create_vector_field()` function with the ``num_comp`` argument to set the dimensionality. .. GENERATED FROM PYTHON SOURCE LINES 174-183 .. code-block:: Python field_22 = dpf.fields_factory.create_vector_field(num_entities=num_entities_2, num_comp=3) field_22.scoping.ids = range(num_entities_2) print("3D vector Field: ", "\n", field_22, "\n") field_32 = dpf.fields_factory.create_vector_field(num_entities=num_entities_2, num_comp=5) field_32.scoping.ids = range(num_entities_2) print("5D vector Field: ", "\n", field_32, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none 3D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 0 elementary data 5D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 5 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 184-187 Use the :func:`create_3d_vector_field()` function to create a 3D vector Field directly. .. GENERATED FROM PYTHON SOURCE LINES 187-192 .. code-block:: Python field_25 = dpf.fields_factory.create_3d_vector_field(num_entities=num_entities_2) field_25.scoping.ids = range(num_entities_2) print("3D vector Field: ", "\n", field_25, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none 3D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 193-200 Create matrix Fields -------------------- Use the :func:`create_matrix_field()` function from the ``fields_factory`` module. Use the ``num_lines`` and ``num_col`` arguments to define the matrix dimensionality. .. GENERATED FROM PYTHON SOURCE LINES 200-222 .. code-block:: Python # (2, 2) matrix Field field_41 = dpf.fields_factory.create_matrix_field( num_entities=num_entities_3, num_lines=2, num_col=2 ) field_41.scoping.ids = range(num_entities_3) print("Matrix Field (2,2): ", "\n", field_41, "\n") # (3, 3) matrix Fields field_51 = dpf.fields_factory.create_matrix_field( num_entities=num_entities_3, num_lines=3, num_col=3 ) field_51.scoping.ids = range(num_entities_3) field_52 = dpf.fields_factory.create_matrix_field( num_entities=num_entities_3, num_lines=3, num_col=3 ) field_52.scoping.ids = range(num_entities_3) print("Matrix Field 1 (3,3): ", "\n", field_51, "\n") print("Matrix Field 2 (3,3): ", "\n", field_52, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Matrix Field (2,2): DPF Field Location: Nodal Unit: 1 entities Data: 4 components and 0 elementary data Matrix Field 1 (3,3): DPF Field Location: Nodal Unit: 1 entities Data: 9 components and 0 elementary data Matrix Field 2 (3,3): DPF Field Location: Nodal Unit: 1 entities Data: 9 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 223-229 Set data to the Fields ---------------------- Use the :attr:`Field.data` attribute to set a data array to a ``Field``. The data can be a 1D or 2D Numpy array or Python list. .. GENERATED FROM PYTHON SOURCE LINES 229-235 .. code-block:: Python # Set data to the scalar Field field_11.data = data_1 print("Scalar Field: ", "\n", field_11, "\n") print("Data scalar Field: ", "\n", field_11.data, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field: DPF Field Location: Nodal Unit: 6 entities Data: 1 components and 6 elementary data IDs data ------------ ---------- 0 6.000000e+00 1 5.000000e+00 2 4.000000e+00 ... Data scalar Field: [6. 5. 4. 3. 2. 1.] .. GENERATED FROM PYTHON SOURCE LINES 236-237 Set data to the vector Fields. .. GENERATED FROM PYTHON SOURCE LINES 237-248 .. code-block:: Python field_21.data = data_3 print("3D vector Field: ", "\n", field_21, "\n") field_31.data = data_4 print("5D vector Field: ", "\n", field_31, "\n") # Set 2D data to a 3D vector Field field_22.data = data_5 print("3D vector Field (from 2D list): ", "\n", field_22, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none 3D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 2 elementary data IDs data ------------ ---------- 0 4.000000e+00 1.000000e+00 8.000000e+00 1 5.000000e+00 7.000000e+00 9.000000e+00 5D vector Field: DPF Field Location: Nodal Unit: 2 entities Data: 5 components and 2 elementary data IDs data ------------ ---------- 0 6.000000e+00 5.000000e+00 4.000000e+00 3.000000e+00 2.000000e+00 1 1.000000e+00 9.000000e+00 7.000000e+00 8.000000e+00 1.000000e+01 3D vector Field (from 2D list): DPF Field Location: Nodal Unit: 2 entities Data: 3 components and 2 elementary data IDs data ------------ ---------- 0 8.000000e+00 4.000000e+00 3.000000e+00 1 3.100000e+01 5.000000e+00 7.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 249-250 Set data to the matrix Fields. .. GENERATED FROM PYTHON SOURCE LINES 250-260 .. code-block:: Python field_41.data = data_6 print("Matrix Field (2,2): ", "\n", field_41.data, "\n") field_51.data = data_7 print("Matrix Field 1 (3,3): ", "\n", field_51.data, "\n") field_52.data = data_8 print("Matrix Field 2 (3,3): ", "\n", field_52.data, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Matrix Field (2,2): [[3. 2. 1. 7.]] Matrix Field 1 (3,3): [[15. 3. 9. 31. 1. 42. 5. 68. 13.]] Matrix Field 2 (3,3): [[12. 7. 8. 1. 4. 27. 98. 4. 6.]] .. GENERATED FROM PYTHON SOURCE LINES 261-267 Append data to the Fields ------------------------- Use the :func:`append()` method to add a new entity with data to the Field. You must provide the ``scopingid`` that this entity will have. .. GENERATED FROM PYTHON SOURCE LINES 267-273 .. code-block:: Python # Append data to the scalar Field field_11.append(scopingid=6, data=data_9) print("Scalar Field after append: ", "\n", field_11, "\n") print("Data scalar Field: ", "\n", field_11.data, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Scalar Field after append: DPF Field Location: Nodal Unit: 7 entities Data: 1 components and 7 elementary data IDs data ------------ ---------- 0 6.000000e+00 1 5.000000e+00 2 4.000000e+00 ... Data scalar Field: [ 6. 5. 4. 3. 2. 1. 24.] .. GENERATED FROM PYTHON SOURCE LINES 274-275 Append data to a vector Field. .. GENERATED FROM PYTHON SOURCE LINES 275-279 .. code-block:: Python field_21.append(scopingid=2, data=data_10) print("Vector Field after append: ", "\n", field_21, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Vector Field after append: DPF Field Location: Nodal Unit: 3 entities Data: 3 components and 3 elementary data IDs data ------------ ---------- 0 4.000000e+00 1.000000e+00 8.000000e+00 1 5.000000e+00 7.000000e+00 9.000000e+00 2 4.700000e+01 3.300000e+01 5.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 280-281 Append data to a matrix Field. .. GENERATED FROM PYTHON SOURCE LINES 281-285 .. code-block:: Python field_51.append(scopingid=1, data=data_11) print("Matrix Field after append: ", "\n", field_51, "\n") .. rst-class:: sphx-glr-script-out .. code-block:: none Matrix Field after append: DPF Field Location: Nodal Unit: 2 entities Data: 9 components and 2 elementary data IDs data ------------ ---------- 0 1.500000e+01 3.000000e+00 9.000000e+00 3.100000e+01 1.000000e+00 4.200000e+01 5.000000e+00 6.800000e+01 1.300000e+01 1 8.000000e+00 2.000000e+00 4.000000e+00 6.400000e+01 3.200000e+01 4.700000e+01 1.100000e+01 2.300000e+01 1.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 286-299 Create a ``FieldsContainer`` ---------------------------- A :class:`FieldsContainer` is a collection of ``Field`` objects ordered by labels. Each ``Field`` in the ``FieldsContainer`` has an ID for each label. The most common ``FieldsContainer`` use the label ``'time'`` with IDs corresponding to time sets. **Create a** ``FieldsContainer`` **by instantiating the object** After instantiation, set the labels and then add fields with their label space. .. GENERATED FROM PYTHON SOURCE LINES 299-306 .. code-block:: Python fc_1 = dpf.FieldsContainer() fc_1.add_label(label="time") fc_1.add_field(label_space={"time": 0}, field=field_21) fc_1.add_field(label_space={"time": 1}, field=field_31) print(fc_1) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Fields Container with 2 field(s) defined on labels: time with: - field 0 {time: 0} with Nodal location, 3 components and 3 entities. - field 1 {time: 1} with Nodal location, 5 components and 2 entities. .. GENERATED FROM PYTHON SOURCE LINES 307-312 **Create a** ``FieldsContainer`` **using the** ``fields_container_factory`` **module** Use the :func:`over_time_freq_fields_container()` function to create a ``FieldsContainer`` with a predefined ``'time'`` label. .. GENERATED FROM PYTHON SOURCE LINES 312-315 .. code-block:: Python fc_2 = dpf.fields_container_factory.over_time_freq_fields_container(fields=[field_21, field_31]) print(fc_2) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Fields Container with 2 field(s) defined on labels: time with: - field 0 {time: 1} with Nodal location, 3 components and 3 entities. - field 1 {time: 2} with Nodal location, 5 components and 2 entities. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.459 seconds) .. _sphx_glr_download_tutorials_import_data_load_custom_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: load_custom_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: load_custom_data.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: load_custom_data.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_