Abaqus Python 后处理将位移数据输出到excel/求平均/逐帧输出求平均

本文介绍了如何在ABAQUS中使用Python的xlwt库进行后处理,包括安装xlwt扩展、从ODB文件读取数据、计算节点集合的位移平均值,并以表格形式存储。作者还展示了如何优化输出结果,控制数值的小数位数。
摘要由CSDN通过智能技术生成

1.确保你的abaqus装了此拓展库,地址:https://xlwt.readthedocs.io/en/latest/installation.html

关于ABAQUS的python二次开发xlwt的安装_abaqus安装 xlwt-CSDN博客

我是直接pip然后在文件夹找到这个放到安装目录的,如下:

2.将xlwt文件夹放入安装目录,我的在 

C:\SIMULIA\EstProducts\2023\win_b64\tools\SMApy\python2.7\lib\site-packages

3.代码:

from odbAccess import *
from textRepr import *

my_odb = openOdb(r"E:\240426slice\0425s3000t250slice.odb")
step = my_odb.steps['Step-1'] 
frame = step.frames[-1] 
dis_field = frame.fieldOutputs['U']

with open('3_250.txt','a') as f:  
    for i in range(1, 21):
        setname = 'SET-N' + str(i)
        NodeSet = my_odb.rootAssembly.nodeSets[setname]
        local_dis_values = dis_field.getSubset(region=NodeSet)
        list_num = [node_value.data[2] for node_value in local_dis_values.values]  
        list_avg = sum(list_num) / len(list_num)
        txt_line = "{}, {}\n".format(setname, list_avg)
        f.write(txt_line)  

改成表格输出:

from odbAccess import *
import xlwt
from textRepr import *

my_odb = openOdb(r"E:\240426slice\0425s3000t250slice.odb")
step = my_odb.steps['Step-1'] 
frame = step.frames[-1] 
dis_field = frame.fieldOutputs['U']

# Create a new Excel workbook and a sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Data')

# Write headers
sheet.write(0, 0, 'Node Set')  # Column A
sheet.write(0, 1, 'Average Value')  # Column B

# Write data to the sheet
row_index = 1  # Start from row 1
for i in range(1, 21):
    setname = 'SET-N' + str(i)
    NodeSet = my_odb.rootAssembly.nodeSets[setname]
    local_dis_values = dis_field.getSubset(region=NodeSet)
    list_num = [node_value.data[2] for node_value in local_dis_values.values]
    list_avg = sum(list_num) / len(list_num)

    # Write to the sheet
    sheet.write(row_index, 0, setname)
    sheet.write(row_index, 1, list_avg)

    row_index += 1  # Move to the next row

# Save the workbook to a file
workbook.save('3_250.xls')

----------------------------------------------------------------------------------------------------------------------------- 

接上回:Abaqus Python:后处理用py导出n个集合在某个方向的位移U1/U2/U3数据+ 逐帧输出-CSDN博客 

对需要的帧数的位移进行平均后按照set输出,还要按照帧数输出,继续修改代码: 

from odbAccess import *
from textRepr import *
my_odb = openOdb(r"E:\240426slice\0425s3000t250slice.odb")
step = my_odb.steps['Step-1']
with open('test1.txt', 'a') as f:
    frame_numbers = list(range(44, 61))
    setnames = ['SET-N' + str(i) for i in range(1, 21)]
    f.write("Frame,")
    f.write(",".join(setnames))
    f.write("\n")
    for frame_num in frame_numbers:  
        frame = step.frames[frame_num]
        dis_field = frame.fieldOutputs['U']
        avg_dis_values = []
        for i in range(1, 21):
            setname = 'SET-N' + str(i)
            NodeSet = my_odb.rootAssembly.nodeSets[setname]
            local_dis_values = dis_field.getSubset(region=NodeSet)
            list_num = [node_value.data[2] for node_value in local_dis_values.values]
            list_avg = sum(list_num) / len(list_num)
            avg_dis_values.append(str(list_avg))
        txt_line = "{},".format(frame_num)
        txt_line += ",".join(avg_dis_values)
        f.write(txt_line + "\n")

 解释如下:

from odbAccess import *
from textRepr import *
my_odb = openOdb(r"E:\240426slice\0425s3000t250slice.odb")
step = my_odb.steps['Step-1']
with open('test1.txt', 'a') as f:
# 这一行打开了一个名为“3_250_1.txt”的文本文件,
#如果文件不存在则创建它,如果文件已经存在则在文件末尾追加内容,
#并将文件句柄保存在变量f中
    frame_numbers = list(range(44, 61))
    setnames = ['SET-N' + str(i) for i in range(1, 21)]
    f.write("Frame,")
    f.write(",".join(setnames))
    f.write("\n")
#这段代码生成了一些数字和字符串的列表,
#然后将这些内容写入了文件中。
#首先将“Frame,”写入文件,
#然后将setnames列表中的内容用逗号连接成字符串写入文件,
#最后写入一个换行符
    for frame_num in frame_numbers:  
        frame = step.frames[frame_num]
        dis_field = frame.fieldOutputs['U']
        avg_dis_values = []
        for i in range(1, 21):
            setname = 'SET-N' + str(i)
            NodeSet = my_odb.rootAssembly.nodeSets[setname]
            local_dis_values = dis_field.getSubset(region=NodeSet)
            list_num = [node_value.data[2] for node_value in local_dis_values.values]
            list_avg = sum(list_num) / len(list_num)
            avg_dis_values.append(str(list_avg))
        txt_line = "{},".format(frame_num)
        txt_line += ",".join(avg_dis_values)
        f.write(txt_line + "\n")
#这部分是一个循环,针对frame_numbers列表中的每个元素,
#代码都会执行一遍。每次循环中,
#代码都会从step中获取相应帧数的frame对象,
#然后从该帧中提取位移数据,
#并计算每个节点集合(NodeSet)中的位移平均值。
#最后,将这些值写入文件中。

输出结果直接拖到origin中如图:

 导入excel如图:

表格横向按照set排列,纵向按照帧数排列,中间是每个set的U3的平均值,是我想要的结果。

但是在此基础上还可以优化一下,只保留小数点后5位: 

from odbAccess import *
from textRepr import *
my_odb = openOdb(r"E:\240426slice\0425s3000t250slice.odb")
step = my_odb.steps['Step-1']
with open('test2.txt', 'a') as f:
    frame_numbers = list(range(44, 61))
    setnames = ['SET-N' + str(i) for i in range(1, 21)]
    f.write("Frame,")
    f.write(",".join(setnames))
    f.write("\n")
    for frame_num in frame_numbers:
        frame = step.frames[frame_num]
        dis_field = frame.fieldOutputs['U']
        avg_dis_values = []
        for i in range(1, 21):
            setname = 'SET-N' + str(i)
            NodeSet = my_odb.rootAssembly.nodeSets[setname]
            local_dis_values = dis_field.getSubset(region=NodeSet)
            list_num = [node_value.data[2] for node_value in local_dis_values.values]
            avg = sum(list_num) / len(list_num)
            avg_with_three_decimals = "{:.5f}".format(avg)  # 5
            avg_dis_values.append(avg_with_three_decimals)
        txt_line = "{},".format(frame_num)
        txt_line += ",".join(avg_dis_values)
        f.write(txt_line + "\n")

 优化后结果就是上面那个excel,数值太小,我的数据无法只保留5位。

今天的python就学到这里!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值