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)