首页 > 解决方案 > 从复杂的 csv 文件创建智能矩阵

问题描述

我有一个 csv 文件,其中包含我使用 pandas 导入的 500 个时间点的 x、y、z、xy、yz 和 zx 中的对称应力张量。我需要的是在每个给定时间点为每个 ID 创建一个应力张量矩阵。图片是文件的片段。真实文件为 [502 行 x 2378 列]

我有另一个需要矩阵的 ID 列表。所以我在想脚本应该遍历 ID 列表,并且对于每个 ID,在第一行中搜索相同的后缀(例如列表中的 ID 可能是 947951),然后为每个时间点创建一个 3x3 矩阵,其中包含强调。矩阵 = [[x,xy,zx],[xy,y,yz],[zx,yz,z]]。

不知何故,新创建的矩阵仍应连接到它们的 ID,因为它们之后必须根据它们的 ID 旋转。如果您知道如何使用 numpy 函数进行 3x3 矩阵的旋转,我们将不胜感激

应力张量文件

标签: pandasnumpyfilematrixtransformation

解决方案


这就是我解决它的方法:

df_tensors = pd.read_csv('TensorExport500.csv', delimiter=",",skiprows=[0])
print(df_tensors)

# Element ID's from previous coordinateselection to look up stress tensors in LS Dyna data
ID = [947951,948046,948141,948236,948331,12]

# Empty dictinary for appending stress tensors in upcoming for loop
matrices = {}

# Empty list for appending element IDs that are not found in LS Dyna data
IDs_not_in_data = []

# Iteration through ID list for loop up in LS Dyna data
for i in ID:

    # If statement to make sure that all needed stresses are present in LS Dyna data for a given element ID for construction of stress tensor
    if "X-stress-"+str(i) and 'Y-stress-'+str(i) and 'Z-stress-'+str(i) and 'XY-stress-'+str(i) and 'YZ-stress-'+str(i) and'ZX-stress-'+str(i) in df_tensors:

        # Pull out columns of stresses and time for the given element ID and create a temporary dataframe inside iteration used for creating the stress tensor
        temporary_df = df_tensors[['Time','X-stress-'+str(i),'Y-stress-'+str(i),'Z-stress-'+str(i),'XY-stress-'+str(i),'YZ-stress-'+str(i),'ZX-stress-'+str(i)]]

        # Iterates through rows in the temporary dataframe
        for index, row in temporary_df.iterrows():

            # Converts scientific string to float so that numpy library can be applied for tensor/matrix rotation
            x = row[1]
            y = row[2]
            z = row[3]
            xy = row[4]
            yz = row[5]
            zx = row[6]

            # Creates the stress tensor for the given element ID for every timespot and names it "elementid,time" and adds it to matrices dictionary
            matrices[str(i)+","+str(row[0])] = np.matrix([[x,xy,zx],[xy,y,yz],[zx,yz,z]])
    else:
        # Append element ID if the element ID / stresses for the given element ID is not found in the LS Dyna data
        IDs_not_in_data.append(i)

# Function that prints stress tensor of given element at given time if it exist
def get_stresstensor(ID,Time):
    key = str(ID)+","+str(Time)
    matrix = matrices.get(key)
    if key in matrices:
        return "The stress tensor matrix for element ID: " + str(ID) + " at the time: " + str(Time) + " is: \n"  + str(matrix)
    else:
        return "Oh no! The stress tensor of the given element ID: " + str(ID) + " at the time: " + str(Time) + " does not exist because the data could not be found in LS Dyna data."

# Function that lists the element ID's that were not found in the LS Dyna data
def lost_IDs(list):
    if len(list) == 0:
        return "Hurray, stresses for all element ID's were found in the LS Dyna data"
    else:
        return "Oh ship, the following element IDs were not found in LSDyna export file: " + str(list)

print(get_stresstensor("947951","4.9949874665e-05"))
print(lost_IDs(IDs_not_in_data))

推荐阅读