0

I need lines and triangles (coordinates corresponding to them) as a list using Python API, how do I go about it?

I have tried these functions

gmsh.model.mesh.createEdges()
edgeTags, edgeNodes = gmsh.model.mesh.getAllEdges()
gmsh.model.mesh.createFaces()
faceTags, faceNodes = gmsh.model.mesh.getAllFaces(3)

And I am not sure how I can proceed to extract the coordinates from the output of these functions.

I did not really find any way to get the coordinates in any tutorials as well.

Shriraj Hegde
  • 829
  • 5
  • 18
  • As of 2022-10-11, I couldn't find a proper method to do this with the API, so, I convert the mesh to version 2 and parse it manually to get the elements and the nodes. – Shriraj Hegde Oct 11 '22 at 16:20

1 Answers1

0

This works for me for simple examples consisting only of triangle elements (like t1.py from https://gmsh.info/doc/texinfo/gmsh.html#index-gmsh_002fmodel_002fmesh_002fgetEdges)

getElementEdgeNodes returns node indexes (note indexing starts from 1 in gmsh) for each triangle element (mesh face) so there are repeated edges (since mesh faces share edges), hence get_unique_edges.

def get_unique_edges(lst):
    unique_entries = set()

    for entry in lst:
        sorted_entry = tuple(sorted(entry))
        unique_entries.add(sorted_entry)

    return np.array([list(entry) for entry in unique_entries])

nodeTags, nodeCoords, _ = gmsh.model.mesh.getNodes()
elementType = gmsh.model.mesh.getElementType("triangle", 1)
faceNodes = gmsh.model.mesh.getElementFaceNodes(elementType, 3)
edgeNodes = gmsh.model.mesh.getElementEdgeNodes(elementType)

nodes = np.reshape(nodeCoords, (int(len(nodeCoords)/3), 3))
faces = np.reshape(faceNodes, (int(len(faceNodes)/3), 3))
edges = np.reshape(edgeNodes, (int(len(edgeNodes)/2), 2))
print("Nodes:")
print(nodes)
print("Faces:")
print(faces)
print("Edges:")
print(get_unique_edges(edges))