.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-basic\03-create_entities.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_examples_00-basic_03-create_entities.py: .. _ref_create_entities_example: Create your own entities using DPF operators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can create your field, fields container, or meshed region to use DPF operators with your own data. The ability to use scripting to create any DPF entity means that you are not dependent on result files and can connect the DPF environment with any Python tool. .. GENERATED FROM PYTHON SOURCE LINES 14-21 .. code-block:: Python # Import necessary modules import numpy as np from ansys.dpf import core as dpf from ansys.dpf.core import operators as ops .. GENERATED FROM PYTHON SOURCE LINES 22-23 Create a parallel piped mesh made of linear hexa: .. GENERATED FROM PYTHON SOURCE LINES 23-42 .. code-block:: Python length = 0.1 width = 0.05 depth = 0.1 num_nodes_in_length = 10 num_nodes_in_width = 5 num_nodes_in_depth = 10 mesh = dpf.MeshedRegion() def search_sequence_numpy(arr, seq): """Find a sequence in an array and return its index.""" indexes = np.where(np.isclose(arr, seq[0])) for index in np.nditer(indexes[0]): if index % 3 == 0: if np.allclose(arr[index + 1], seq[1]) and np.allclose(arr[index + 2], seq[2]): return index return -1 .. GENERATED FROM PYTHON SOURCE LINES 43-44 Add nodes: .. GENERATED FROM PYTHON SOURCE LINES 44-57 .. code-block:: Python n_id = 1 for i, x in enumerate( [float(i) * length / float(num_nodes_in_length) for i in range(0, num_nodes_in_length)] ): for j, y in enumerate( [float(i) * width / float(num_nodes_in_width) for i in range(0, num_nodes_in_width)] ): for k, z in enumerate( [float(i) * depth / float(num_nodes_in_depth) for i in range(0, num_nodes_in_depth)] ): mesh.nodes.add_node(n_id, [x, y, z]) n_id += 1 .. GENERATED FROM PYTHON SOURCE LINES 58-59 Get the nodes' coordinates field: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python coordinates = mesh.nodes.coordinates_field .. GENERATED FROM PYTHON SOURCE LINES 62-63 Set the mesh unit: .. GENERATED FROM PYTHON SOURCE LINES 63-69 .. code-block:: Python mesh.unit = "mm" coordinates_data = coordinates.data flat_coordinates_data = coordinates_data.reshape(coordinates_data.size) coordinates_scoping = coordinates.scoping .. GENERATED FROM PYTHON SOURCE LINES 70-71 Add solid elements (linear hexa with eight nodes): .. GENERATED FROM PYTHON SOURCE LINES 71-100 .. code-block:: Python e_id = 1 for i, x in enumerate( [float(i) * length / float(num_nodes_in_length) for i in range(num_nodes_in_length - 1)] ): for j, y in enumerate( [float(i) * width / float(num_nodes_in_width) for i in range(num_nodes_in_width - 1)] ): for k, z in enumerate( [float(i) * depth / float(num_nodes_in_depth) for i in range(num_nodes_in_depth - 1)] ): coord1 = np.array([x, y, z]) connectivity = [] for xx in [x, x + length / float(num_nodes_in_length)]: for yy in [y, y + width / float(num_nodes_in_width)]: for zz in [z, z + depth / float(num_nodes_in_depth)]: data_index = search_sequence_numpy(flat_coordinates_data, [xx, yy, zz]) scoping_index = int(data_index / 3) # 3components connectivity.append(scoping_index) # rearrange connectivity tmp = connectivity[2] connectivity[2] = connectivity[3] connectivity[3] = tmp tmp = connectivity[6] connectivity[6] = connectivity[7] connectivity[7] = tmp mesh.elements.add_solid_element(e_id, connectivity) e_id += 1 mesh.plot() .. image-sg:: /examples/00-basic/images/sphx_glr_03-create_entities_001.png :alt: 03 create entities :srcset: /examples/00-basic/images/sphx_glr_03-create_entities_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-108 Create displacement fields over time with three time sets. For the first time set, the displacement on each node is the value of its x, y, and z coordinates. For the second time set, the displacement on each node is two times the value of its x, y, and z coordinates. For the third time set, the displacement on each node is three times the value of its x, y, and z coordinates. .. GENERATED FROM PYTHON SOURCE LINES 108-129 .. code-block:: Python num_nodes = mesh.nodes.n_nodes time1_array = coordinates_data time2_array = 2.0 * coordinates_data time3_array = 3.0 * coordinates_data time1_field = dpf.fields_factory.create_3d_vector_field(num_nodes) time2_field = dpf.fields_factory.create_3d_vector_field(num_nodes) time3_field = dpf.fields_factory.create_3d_vector_field(num_nodes) time1_field.scoping = coordinates.scoping time2_field.scoping = coordinates.scoping time3_field.scoping = coordinates.scoping time1_field.data = time1_array time2_field.data = time2_array time3_field.data = time3_array time1_field.unit = mesh.unit time2_field.unit = mesh.unit time3_field.unit = mesh.unit .. GENERATED FROM PYTHON SOURCE LINES 130-131 Create results over times in a fields container with its time frequency support: .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python fc = dpf.fields_container_factory.over_time_freq_fields_container( {0.1: time1_field, 0.2: time2_field, 0.3: time3_field}, "s" ) .. GENERATED FROM PYTHON SOURCE LINES 136-137 Check that the time frequency support has been built: .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python print(fc.time_freq_support) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Time/Freq Support: Number of sets: 3 Cumulative Time (s) LoadStep Substep 1 0.100000 1 1 2 0.200000 1 2 3 0.300000 1 3 .. GENERATED FROM PYTHON SOURCE LINES 140-141 Plot the norm over time of the fields container: .. GENERATED FROM PYTHON SOURCE LINES 141-146 .. code-block:: Python norm = ops.math.norm_fc(fc) fc_norm = norm.outputs.fields_container() mesh.plot(fc_norm.get_field_by_time_complex_ids(1)) mesh.plot(fc_norm.get_field_by_time_complex_ids(2)) mesh.plot(fc_norm.get_field_by_time_complex_ids(3)) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/00-basic/images/sphx_glr_03-create_entities_002.png :alt: 03 create entities :srcset: /examples/00-basic/images/sphx_glr_03-create_entities_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/00-basic/images/sphx_glr_03-create_entities_003.png :alt: 03 create entities :srcset: /examples/00-basic/images/sphx_glr_03-create_entities_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/00-basic/images/sphx_glr_03-create_entities_004.png :alt: 03 create entities :srcset: /examples/00-basic/images/sphx_glr_03-create_entities_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.207 seconds) .. _sphx_glr_download_examples_00-basic_03-create_entities.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 03-create_entities.ipynb <03-create_entities.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03-create_entities.py <03-create_entities.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_