有限元分析后处理-选择部分区域,提取应变,从应变文件中计算各种应变的均值(Python)

2023-12-13 21:46:15

选择部分区域,提取应变,将文件内容保存为.lis文件,修改为.txt文件。

使用Python从应变文件中计算各种应变的均值,代码如下:

# version2

fo = open("梁的局部应变-PRNSOL.txt", "r")
Average = {"EPTO1": 0, "EPTO2": 0, "EPTO3": 0, "EPTOINT": 0, "EPTOEQV": 0}

start = 0   # 应变所在第一行
end = 0     # 应变所在最后一行

# 应变值的确定起始行和终止行
line_num = 1
for line in fo:
    if "NODAL RESULTS ARE FOR MATERIAL" in line:
        start = line_num + 3
    elif "MINIMUM VALUES" in line:
        end = line_num - 2

    line_num += 1

# 文件读取完成后,当前文件指针指向文件末尾,所以需要再读一次,或者使用 <file>.seek(offset [,whence])命令,0:文件开头; 1: 当前位置; 2: 文件结尾
# fo.close()
# fo = open("区域应变值-PRNSOL.txt", "r")
fo.seek(0)

print("应变数值起始行:{}".format(start), "   应变终止行:{}".format(end))
# 计算各种应变的均值
line_num1 = 1   # 行数
negative = 0  # 判断主应力中负值的个数
count = 0   # 应变个数
for line in fo:
    if start <= line_num1 <= end:
        tmp = line.split()

        epto1 = 0
        epto2 = 0
        epto3 = 0

        # 找出存在主应力存在负值的索引,应变强度和等效应变根据定义都是正的,所以不用考虑负值的情况
        for j in range(len(tmp)):
            # 三个主应力中只有一个是负值,必定是第三主应力
            if len(tmp[j]) == 25:
                epto1 = tmp[j-1]
                epto2 = tmp[j][0:12]
                epto3 = tmp[j][12:]
                negative = 1
            # 两个是负值,第二第三主应力为负
            elif len(tmp[j]) == 38:
                epto1 = tmp[j][0:12]
                epto2 = tmp[j][12:25]
                epto3 = tmp[j][25:]
                negative = 2
            # 三个都是负值
            elif len(tmp[j]) == 39:
                epto1 = tmp[j][0:13]
                epto2 = tmp[j][13:26]
                epto3 = tmp[j][26:]
                negative = 3

        if negative == 0:
            # 不存在负值
            Average["EPTO1"] = float(tmp[1]) + Average["EPTO1"]
            Average["EPTO2"] = float(tmp[2]) + Average["EPTO2"]
            Average["EPTO3"] = float(tmp[3]) + Average["EPTO3"]
            Average["EPTOINT"] = float(tmp[4]) + Average["EPTOINT"]
            Average["EPTOEQV"] = float(tmp[5]) + Average["EPTOEQV"]

        else:
            # 存在负值
            Average["EPTO1"] = float(epto1) + Average["EPTO1"]
            Average["EPTO2"] = float(epto2) + Average["EPTO2"]
            Average["EPTO3"] = float(epto3) + Average["EPTO3"]
            if negative == 1:
                # 存在一个负值
                Average["EPTOINT"] = float(tmp[3]) + Average["EPTOINT"]
                Average["EPTOEQV"] = float(tmp[4]) + Average["EPTOEQV"]
            else:
                # 两个或三个负值时
                Average["EPTOINT"] = float(tmp[2]) + Average["EPTOINT"]
                Average["EPTOEQV"] = float(tmp[3]) + Average["EPTOEQV"]

        count += 1

    line_num1 += 1

if count:
    for key in Average.keys():
        Average[key] = float("{:.4E}".format(Average[key] / count))     # 限制精度小数点后4位

print("应变类型", "      应变值")
i = 1
for key, value in Average.items():
    if i == 1:
        print("", key, "   ", value)
    elif 2 <= i <= 3:
        print("", key, "  ", value)
    else:
        print(key, "  ", value)
    i += 1

fo.close()


# # version1
# fo = open("区域应变值-PRNSOL.txt", "r")
#
# i = 1
# Average = {"EPTO1": 0, "EPTO2": 0, "EPTO3": 0, "EPTOINT": 0, "EPTOEQV": 0}
# lst = []
# count = 0
# for line in fo:
#     if 12 <= i <= 35:
#         tmp = line.split()
#         print(tmp)
#         epto2 = tmp[2][0:12]
#         epto3 = tmp[2][12:]
#
#         for j in range(len(tmp)):
#             if j == 0:
#                 continue
#             elif j == 1:
#                 Average["EPTO1"] = float(tmp[1]) + Average["EPTO1"]
#             elif j == 2:
#                 Average["EPTO2"] = float(epto2) + Average["EPTO2"]
#                 Average["EPTO3"] = float(epto3) + Average["EPTO3"]
#             elif j == 3:
#                 Average["EPTOINT"] = float(tmp[3]) + Average["EPTOINT"]
#             elif j == 4:
#                 Average["EPTOEQV"] = float(tmp[4]) + Average["EPTOEQV"]
#
#         count += 1
#     i += 1
#
# for key in Average.keys():
#     Average[key] = Average[key] / count
#
# print(Average)
#
# fo.close()


# ------------------第0版------------------ #
# fo = open("区域应变值-PRNSOL.txt", "r")
#
# dic = {"EPTO1": 0, "EPTO2": 0, "EPTO3": 0, "EPTOINT": 0, "EPTOEQV": 0}
# lst = []
# tag = 0
# for line in fo:
#     if 12 <= i <= 35:
#         tmp = line.split()
#         tmp1 = tmp[2][0:12]
#         tmp2 = tmp[2][12:]
#         # print(tmp1, tmp2)
#
#         for j in range(len(tmp)):
#             if j == 0:
#                 continue
#             elif j > 0 and j != 2:
#                 lst.append(float(tmp[j]))
#             elif j == 2:
#                 lst.append(float(tmp1))
#                 lst.append(float(tmp2))
#     i += 1
#
# print(lst)
# Sum = sum(lst)
# Length = len(lst)
# Average = Sum / Length
# print(Average)
#
# fo.close()
# ------------------第0版------------------ #

文章来源:https://blog.csdn.net/ThreeS_tones/article/details/134980873
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。