I have a 2D mesh of triangles used in Finite Element method to discretize the domain. I want to calculate the midpoints of all the edges because I want to use $\mathbb{P}^2$ elements. I am using MatLab. The data that I have is:Matrix containing nodes coordinates, $x$ and $y$. The size is $N\times 2$, where $N$ is the number of nodes.Matrix containing each triangle nodes. The size is $NT\times 3$, where $NT$ is the number of triangles in the mesh and 3 columns because each triangle has three nodes. For example, a row of this matrix could have $[10, 4 ,22]$ so the triangle is formed by these nodes and in order to know their coordinates I just take the rows $10, 4$ and $22$ from the node coordinate matrix (the previous one).Array with boundary nodes.I know in advance that the number of nodes (including midpoints) is going to be $2N\times N_b -3$ where $N_b$ is the number of nodes in the boundary of the mesh, so I can preallocate the matrix. The new node matrix will contain the previous nodes coordinates and the new ones and the element matrix will now have 6 columns because one node will be added per edge.What my code is doing is to run across all triangles and calculate their edges midpoints and if it is not yet in the coordinate node matrix include it (here I am losing many time checking if it is yet inside or not). The main problem is that as interior edges are common to two triangles many times the midpoint of an edge is already calculated so I am wasting time calculating the midpoint and after it checking that is already in the matrix.Could you give me any advice about how to calculate more efficiently the midpoints of edges and include them in the matrix. Note: I do not have a matrix with the edges of the mesh.Thanks!
How to calculate efficiently mesh edges midpoints?
finite element;mesh generation;graph theory
If you have an array that stores the indices of the 3 neighbors of each cell, then you would only compute the midpoint of an edge of the neighbor cell has a higher index than the current cell, or if there is no neighbor at all. This way you have an easy tie breaker to decide which of the two cells is responsible for computing the edge midpoint.