Example #1
0
def ParseInp(fname, bIsolate):
    ######## Parse input file
    with open(args.inp_filename) as fid:
        lines = fid.readlines()

    for i, line in enumerate(lines):
        if line.startswith("*Node"):
            inodes = i + 1
        if line.startswith("*Element"):
            iel = i + 1
            break
    nlines = len(lines)

    nvertex = iel - inodes - 1
    vertex = np.zeros((nvertex, 3), dtype=object)
    vid = np.zeros((nvertex), dtype=int)

    # Fill in vertex array
    vid_lookup = {}
    for i0, i in enumerate(range(inodes, iel - 1)):
        vals = lines[i].split(",")
        vid_lookup[int(vals[0])] = i0
        vertex[i0, :] = [float(val) for val in vals[1:4]]

    # Fill in the triangles array
    faces = []
    triangles = []

    for i in range(iel, nlines):
        line = lines[i]
        if line.startswith("*Element"):
            if bIsolate:
                myFace = Face(vertex=None, connect=np.asarray(triangles))
                faces.append(myFace)
                triangles = []
            continue
        val = [int(v) for v in lines[i].split(",")[1:4]]
        triangles.append(val)

    myFace = Face(vertex=None, connect=np.asarray(triangles))
    faces.append(myFace)

    for myFace in faces:
        myFace.reindex(vid_lookup)
        # reorder vertex from 0 to n where n is the number of vertice in the face
        unique_vid = list(set(list(myFace.connect.flatten())))
        vid_lu = {unique_vid[k]: k for k in range(len(unique_vid))}
        myFace.reindex(vid_lu)
        vertex0 = vertex[unique_vid, :]
        myFace.vertex = vertex0
    return faces