# Two-dimensional mesh in fem: generating P1, P2, P3,... mesh from a P1 mesh

I have a two-dimensional mesh generated by triangle (the mesh generator software is not relevant). This software generates a perfect mesh for approximate the solution by piecewice linear functions (P1 mesh).

For example, for the unit square [0,1]x[0,1] in 2D I have a file with the coordinates of its nodes (for example, a mesh with 5 nodes):

1   0.0 0.0  # coordinates of node 1
2   1.0 0.0  # coordinates of node 2
3   1.0 1.0  # coordinates of node 3
4   0.0 1.0  # coordinates of node 4
5   0.5 0.5  # coordinates of node 5


called coordinate.dat, which have 4 elements (triangles) whose conectivity is given by the file called element.dat:

1   1 5 4  # vertices of triangle 1
2   1 2 5  # vertices of triangle 2
3   2 3 5  # vertices of triangle 3
4   5 2 4  # vertices of triangle 4


Now, I need to program problems where the finite element spaces belong to arbitrary polynomial degree, for example, approximate the solution by polynomials of degree 2, 3, etc.

To do this, I need to build a new mesh with new nodes and a new conectivity file with more nodes. An example of the general mesh that I need:

I already know how to calculate the coordinate of all new nodes on each element (it is just a convex combination easy to calculate) but I can't get a easy way to generate the new conectivity file.

I've been thinking for several days how to program a general method for this, but I could not come up with any.

Do you know some method to create meshes P2, P3, ... from a mesh P1? or more precisely, How can I build the new conectivity file?