Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

python - A better way for sorting and arranging specific mesh data using DataFrames

I'm currently using a specific FEM software. The post-processing tool is quite outdated, and it can run only on a dedicated machine. I want to visualize some of the results on my own laptop (for better presentation), using the result files the software produces. I'm using the Pandas library with Python.

I was able to get to the point where I have two different DataFrames, one with the element ID and the nodes that construct it, and the second with nodes ID, and x,y coordinates -

elementDF - includes {index, element ID, node1, node2, node3} # elements have 3 nodes
coordsDF - includes {index, node ID, x, y}

and I was able to combine the two into a single DataFrame -

df - includes {index, element ID, x1, y1, x2, y2, x3, y3} # where x1 and y1 are the
                                                            coordinates of node1, etc

I will later use this DataFrame to build polygons and visualize the mesh.

The thing is, I believe I used a very costly loop to search for each node by its ID, extract the x & y coordinates, and then combine everything. I know this because the dedicated post-processing program does that in a few seconds (for a large mesh - 10,000 elements or more) and mine takes around 40~60 seconds for the same number of elements. I would like to know if there is a quicker and more efficient way to construct the final DataFrame.

Sample input DataFrames:

elementDF = pd.DataFrame({
    'element': [1,2,3,4,5,6,7,8,9,10],
    'node1': [2,33,33,32,183,183,183,185,185,36],
    'node2': [34,34,183,183,34,35,185,35,36,37],
    'node3': [33,183,32,184,35,185,186,36,187,187]
    })

coordsDF = pd.DataFrame({
    'node': [2,32,33,34,35,36,37,183,184,185,186,187],
    'x': [-1, 1, 1, -1, -1.1, 1.1, 1.1, -1.1, -1.1, 1.1, 2, 2.2],
    'y': [0,0,2,2,-0.2,-0.2,0,0,2,2, 4, 4.4]
    })

Sample code:

import pandas as pd

def extractXY(nodeNumber,df):
   # extract x,y data from node location
   nodeData = df.loc[df['node'] == nodeNumber]
   x = nodeData.x
   y = nodeData.y

return x, y

#main#

df = pd.DataFrame(columns = ['x1','y1','x2','y2','x3','y3'])
for i in range(len(elementDF)):
    nodeNumber1 = elementDF.loc[i].node1
    x1, y1 = extractXY(nodeNumber1, coordsDF)

    nodeNumber2 = elementDF.loc[i].node2
    x2, y2 = extractXY(nodeNumber2, coordsDF)

    nodeNumber3 = elementDF.loc[i].node3
    x3, y3 = extractXY(nodeNumber3, coordsDF)
    
    df = df.append({'x1': float(x1), 'y1': float(y1),
                    'x2': float(x2), 'y2': float(y2) ,
                    'x3': float(x3), 'y3': float(y3)}, ignore_index = True)


df = pd.concat([elementDF['element'],df], axis = 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's try this:

import pandas as pd
elementDF = pd.DataFrame({
    'element': [1,2,3,4,5,6,7,8,9,10],
    'node1': [2,33,33,32,183,183,183,185,185,36],
    'node2': [34,34,183,183,34,35,185,35,36,37],
    'node3': [33,183,32,184,35,185,186,36,187,187]
    })

coordsDF = pd.DataFrame({
    'node': [2,32,33,34,35,36,37,183,184,185,186,187],
    'x': [-1, 1, 1, -1, -1.1, 1.1, 1.1, -1.1, -1.1, 1.1, 2, 2.2],
    'y': [0,0,2,2,-0.2,-0.2,0,0,2,2, 4, 4.4]
    })
mapx = coordsDF.set_index('node')['x']
mapy = coordsDF.set_index('node')['y']

df = pd.concat([
elementDF.set_index('element').replace(mapx).rename(columns=lambda x: x.replace('node','x')),
elementDF.set_index('element').replace(mapy).rename(columns=lambda y: y.replace('node','y')),
],
axis=1)
df

Output:

          x1   x2   x3   y1   y2   y3
element                              
1       -1.0 -1.0  1.0  0.0  2.0  2.0
2        1.0 -1.0 -1.1  2.0  2.0  0.0
3        1.0 -1.1  1.0  2.0  0.0  0.0
4        1.0 -1.1 -1.1  0.0  0.0  2.0
5       -1.1 -1.0 -1.1  0.0  2.0 -0.2
6       -1.1 -1.1  1.1  0.0 -0.2  2.0
7       -1.1  1.1  2.0  0.0  2.0  4.0
8        1.1 -1.1  1.1  2.0 -0.2 -0.2
9        1.1  1.1  2.2  2.0 -0.2  4.4
10       1.1  1.1  2.2 -0.2  0.0  4.4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...