|
| 1 | +.. _ref_tutorials_create_a_mesh_from_scratch: |
| 2 | + |
| 3 | +========================== |
| 4 | +Create a mesh from scratch |
| 5 | +========================== |
| 6 | + |
| 7 | +.. include:: ../../../links_and_refs.rst |
| 8 | + |
| 9 | +This tutorial demonstrates how to build a |MeshedRegion| from scratch. |
| 10 | + |
| 11 | +The mesh object in DPF is a |MeshedRegion|. You can create your own |MeshedRegion| object and use it |
| 12 | +with DPF operators. The ability to use scripting to create any DPF entity means |
| 13 | +that you are not dependent on result files and can connect the DPF environment |
| 14 | +with any Python tool. |
| 15 | + |
| 16 | +In this tutorial, we create a parallel piped mesh made of linear hexa elements. |
| 17 | + |
| 18 | +:jupyter-download-script:`Download tutorial as Python script<create_a_mesh_from_scratch>` |
| 19 | +:jupyter-download-notebook:`Download tutorial as Jupyter notebook<create_a_mesh_from_scratch>` |
| 20 | + |
| 21 | +Import the necessary modules |
| 22 | +---------------------------- |
| 23 | + |
| 24 | +Import the ``ansys.dpf.core`` module, including the operators module and the numpy library. |
| 25 | + |
| 26 | +.. jupyter-execute:: |
| 27 | + |
| 28 | + # Import the numpy library |
| 29 | + import numpy as np |
| 30 | + # Import the ``ansys.dpf.core`` module |
| 31 | + from ansys.dpf import core as dpf |
| 32 | + # Import the operators module |
| 33 | + from ansys.dpf.core import operators as ops |
| 34 | + |
| 35 | +Define the mesh dimensions |
| 36 | +-------------------------- |
| 37 | + |
| 38 | +.. jupyter-execute:: |
| 39 | + |
| 40 | + # Define the mesh dimensions |
| 41 | + length = 0.1 |
| 42 | + width = 0.05 |
| 43 | + depth = 0.1 |
| 44 | + num_nodes_in_length = 10 |
| 45 | + num_nodes_in_width = 5 |
| 46 | + num_nodes_in_depth = 10 |
| 47 | + # Create a MeshedRegion object |
| 48 | + my_meshed_region = dpf.MeshedRegion() |
| 49 | + |
| 50 | +Define the connectivity function |
| 51 | +-------------------------------- |
| 52 | + |
| 53 | +To create a mesh you must define the nodes connectivity. This means to define |
| 54 | +the nodes ids connected to each element. |
| 55 | + |
| 56 | +Here, we create a function that will find this connectivity. |
| 57 | + |
| 58 | +.. jupyter-execute:: |
| 59 | + |
| 60 | + def search_sequence_numpy(arr, node): |
| 61 | + """Find the node location in an array of nodes and return its index.""" |
| 62 | + indexes = np.isclose(arr, seq) |
| 63 | + match = np.all(indexes, axis=1).nonzero() |
| 64 | + return int(match[0][0]) |
| 65 | + |
| 66 | +Add nodes |
| 67 | +--------- |
| 68 | + |
| 69 | +Add |Nodes| to the |MeshedRegion| object. |
| 70 | + |
| 71 | +.. jupyter-execute:: |
| 72 | + |
| 73 | + node_id = 1 |
| 74 | + for i, x in enumerate( |
| 75 | + [float(i) * length / float(num_nodes_in_length) for i in range(0, num_nodes_in_length)] |
| 76 | + ): |
| 77 | + for j, y in enumerate( |
| 78 | + [float(i) * width / float(num_nodes_in_width) for i in range(0, num_nodes_in_width)] |
| 79 | + ): |
| 80 | + for k, z in enumerate( |
| 81 | + [float(i) * depth / float(num_nodes_in_depth) for i in range(0, num_nodes_in_depth)] |
| 82 | + ): |
| 83 | + my_meshed_region.nodes.add_node(node_id, [x, y, z]) |
| 84 | + node_id += 1 |
| 85 | + |
| 86 | +Get the nodes coordinates field. |
| 87 | + |
| 88 | +.. jupyter-execute:: |
| 89 | + |
| 90 | + my_nodes_coordinates = my_meshed_region.nodes.coordinates_field |
| 91 | + |
| 92 | +Set the mesh properties |
| 93 | +----------------------- |
| 94 | + |
| 95 | +Set the mesh unit. |
| 96 | + |
| 97 | +.. jupyter-execute:: |
| 98 | + |
| 99 | + my_meshed_region.unit = "mm" |
| 100 | + |
| 101 | +Set the nodes coordinates. |
| 102 | + |
| 103 | +.. jupyter-execute:: |
| 104 | + |
| 105 | + # Get the nodes coordinates data |
| 106 | + my_nodes_coordinates_data = my_nodes_coordinates.data |
| 107 | + # As we use the connectivity function we need to get the data as a list |
| 108 | + my_nodes_coordinates_data_list = my_nodes_coordinates.data_as_list |
| 109 | + # Set the nodes scoping |
| 110 | + my_coordinates_scoping = my_nodes_coordinates.scoping |
| 111 | + |
| 112 | +Add elements |
| 113 | +------------ |
| 114 | +Add |Elements| to the |MeshedRegion| object. |
| 115 | + |
| 116 | +.. jupyter-execute:: |
| 117 | + |
| 118 | + # Add solid elements (linear hexa with eight nodes): |
| 119 | + element_id = 1 |
| 120 | + # Precompute node spacings |
| 121 | + dx = length / float(num_nodes_in_length) |
| 122 | + dy = width / float(num_nodes_in_width) |
| 123 | + dz = depth / float(num_nodes_in_depth) |
| 124 | + # Generate node coordinates |
| 125 | + x_coords = [i * dx for i in range(num_nodes_in_length - 1)] |
| 126 | + y_coords = [j * dy for j in range(num_nodes_in_width - 1)] |
| 127 | + z_coords = [k * dz for k in range(num_nodes_in_depth - 1)] |
| 128 | + # Iterate through the grid |
| 129 | + for x in x_coords: |
| 130 | + for y in y_coords: |
| 131 | + for z in z_coords: |
| 132 | + coord1 = np.array([x, y, z]) |
| 133 | + connectivity = [] |
| 134 | + # Generate connectivity for the current element |
| 135 | + for xx in [x, x + dx]: |
| 136 | + for yy in [y, y + dy]: |
| 137 | + for zz in [z, z + dz]: |
| 138 | + scoping_index = search_sequence_numpy(my_nodes_coordinates_data, |
| 139 | + [xx, yy, zz]) |
| 140 | + connectivity.append(scoping_index) |
| 141 | + # Rearrange connectivity to maintain element orientation |
| 142 | + connectivity[2], connectivity[3] = connectivity[3], connectivity[2] |
| 143 | + connectivity[6], connectivity[7] = connectivity[7], connectivity[6] |
| 144 | + # Add the solid element |
| 145 | + my_meshed_region.elements.add_solid_element(element_id, connectivity) |
| 146 | + element_id += 1 |
| 147 | + |
| 148 | +Plot the mesh |
| 149 | +------------- |
| 150 | + |
| 151 | +You can check the mesh we just created with a plot. For more information on how to plot a mesh see |
| 152 | +the :ref:`ref_tutorials_plotting_meshes` tutorial. |
| 153 | + |
| 154 | +.. jupyter-execute:: |
| 155 | + |
| 156 | + # Plot the mesh |
| 157 | + my_meshed_region.plot() |
0 commit comments