How can I compute a curvilinear abscissa along a non-straight path in IronPython?

Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 107
25 Answers Second Anniversary 10 Comments 25 Likes
✭✭✭✭
edited June 2023 in Structures

Say I have an edge or set of edges in Mechanical which are not straight . How can I derive an abscissa to plot some quantity against?

Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 107
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    edited January 2023

    There would be many libraries available in C-Python to achieve this. I'm proposing the following simple code. For a set of nodes, nodes_coords would be a dictionary whose keys are the node numbers and the values are a list of three coordinates [x,y,z]

    def findClosestNode(ref_coord,node_coords):
        mindist=1e31
        minnode=-1
        for nd,coord in node_coords.items():
            dist=(ref_coord[0]-coord[0])**2+(ref_coord[1]-coord[1])**2+(ref_coord[2]-coord[2])**2
            if dist<mindist:
                mindist=dist
                minnode=nd
                
        return minnode,mindist
    
    def getCurvilinearAbscissa(node_coords):
        local_node_coords=node_coords
        curv_absc=[]
        distances={}
        # find node with lowest X
        xmin=1e31
        for nd,coord in node_coords.items():
            if xmin>coord[0]:
                xmin=coord[0]
                ndmin=nd
        dist_idx=0
        ref_coord=node_coords[ndmin]
        distances[dist_idx]=[ndmin,0.]
        dist_idx+=1
        del local_node_coords[ndmin]
        # Now compute distance between nodes
        while len(local_node_coords.keys())>0:
            next_node,dist=findClosestNode(ref_coord,local_node_coords)
            distances[dist_idx]=[next_node,math.sqrt(dist)]
            ref_coord=node_coords[next_node]
            del local_node_coords[next_node]
            dist_idx+=1
        
        
        curv_absc=[[distances[0][0],0.]]
        
        for i in range(1,max(distances.keys())+1):
            curv_absc.append([distances[i][0],curv_absc[-1][1]+distances[i][1]])
        return curv_absc
            
    mesh=ExtAPI.DataModel.MeshDataByName(ExtAPI.DataModel.MeshDataNames[0])
        
    for i in my_node_ids:
        nd=mesh.NodeById(i)
        node_coords[i]=[nd.X,nd.Y,nd.Z]
        
    
    curv_abs=getCurvilinearAbscissa(node_coords)