.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\mesh\create_a_mesh_from_scratch.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_mesh_create_a_mesh_from_scratch.py: .. _ref_tutorials_create_a_mesh_from_scratch: Create a mesh from scratch ========================== This tutorial demonstrates how to build a :class:`MeshedRegion` from scratch. You can create your own ``MeshedRegion`` object and use it with DPF operators. 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. In this tutorial, we create a parallel piped mesh made of linear hexa elements. .. GENERATED FROM PYTHON SOURCE LINES 40-42 Import the necessary modules ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python import numpy as np from ansys.dpf import core as dpf from ansys.dpf.core import operators as ops .. GENERATED FROM PYTHON SOURCE LINES 49-51 Define the mesh dimensions -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. 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 # Create a MeshedRegion object my_meshed_region = dpf.MeshedRegion() .. GENERATED FROM PYTHON SOURCE LINES 63-69 Define the connectivity function --------------------------------- To create a mesh you must define the node connectivity: which node ids are connected to each element. Here, we create a helper function that finds the connectivity. .. GENERATED FROM PYTHON SOURCE LINES 69-78 .. code-block:: Python def search_sequence_numpy(arr, node): """Find the node location in an array of nodes and return its index.""" indexes = np.isclose(arr, node) match = np.all(indexes, axis=1).nonzero() return int(match[0][0]) .. GENERATED FROM PYTHON SOURCE LINES 79-83 Add nodes ---------- Add :class:`Nodes` to the ``MeshedRegion`` object. .. GENERATED FROM PYTHON SOURCE LINES 83-97 .. code-block:: Python node_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)] ): my_meshed_region.nodes.add_node(node_id, [x, y, z]) node_id += 1 .. GENERATED FROM PYTHON SOURCE LINES 98-99 Get the nodes coordinates field. .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: Python my_nodes_coordinates = my_meshed_region.nodes.coordinates_field .. GENERATED FROM PYTHON SOURCE LINES 103-107 Set the mesh properties ------------------------ Set the mesh unit. .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: Python my_meshed_region.unit = "mm" .. GENERATED FROM PYTHON SOURCE LINES 111-112 Get the nodes coordinates data as a list for use in the connectivity function. .. GENERATED FROM PYTHON SOURCE LINES 112-117 .. code-block:: Python my_nodes_coordinates_data = my_nodes_coordinates.data my_nodes_coordinates_data_list = my_nodes_coordinates.data_as_list my_coordinates_scoping = my_nodes_coordinates.scoping .. GENERATED FROM PYTHON SOURCE LINES 118-123 Add elements ------------ Add :class:`Elements` to the ``MeshedRegion`` object. Here, we add solid elements (linear hexa with eight nodes). .. GENERATED FROM PYTHON SOURCE LINES 123-150 .. code-block:: Python element_id = 1 dx = length / float(num_nodes_in_length) dy = width / float(num_nodes_in_width) dz = depth / float(num_nodes_in_depth) x_coords = [i * dx for i in range(num_nodes_in_length - 1)] y_coords = [j * dy for j in range(num_nodes_in_width - 1)] z_coords = [k * dz for k in range(num_nodes_in_depth - 1)] for x in x_coords: for y in y_coords: for z in z_coords: connectivity = [] for xx in [x, x + dx]: for yy in [y, y + dy]: for zz in [z, z + dz]: scoping_index = search_sequence_numpy( my_nodes_coordinates_data, [xx, yy, zz] ) connectivity.append(scoping_index) # Rearrange connectivity to maintain element orientation connectivity[2], connectivity[3] = connectivity[3], connectivity[2] connectivity[6], connectivity[7] = connectivity[7], connectivity[6] my_meshed_region.elements.add_solid_element(element_id, connectivity) element_id += 1 .. GENERATED FROM PYTHON SOURCE LINES 151-155 Plot the mesh ------------- Check the mesh we just created with a plot. .. GENERATED FROM PYTHON SOURCE LINES 155-157 .. code-block:: Python my_meshed_region.plot() .. image-sg:: /tutorials/mesh/images/sphx_glr_create_a_mesh_from_scratch_001.png :alt: create a mesh from scratch :srcset: /tutorials/mesh/images/sphx_glr_create_a_mesh_from_scratch_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.763 seconds) .. _sphx_glr_download_tutorials_mesh_create_a_mesh_from_scratch.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: create_a_mesh_from_scratch.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: create_a_mesh_from_scratch.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: create_a_mesh_from_scratch.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_