|
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-packages3.代码:fromodbAccessimport*fromtextReprimport*my_odb=openOdb(r"E:\240426slice\0425s3000t250slice.odb")step=my_odb.steps['Step-1']frame=step.frames[-1]dis_field=frame.fieldOutputs['U']withopen('3_250.txt','a')asf:foriinrange(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]fornode_valueinlocal_dis_values.values]list_avg=sum(list_num)/len(list_num)txt_line="{},{}\n".format(setname,list_avg)f.write(txt_line)改成表格输出:fromodbAccessimport*importxlwtfromtextReprimport*my_odb=openOdb(r"E:\240426slice\0425s3000t250slice.odb")step=my_odb.steps['Step-1']frame=step.frames[-1]dis_field=frame.fieldOutputs['U']#CreateanewExcelworkbookandasheetworkbook=xlwt.Workbook()sheet=workbook.add_sheet('Data')#Writeheaderssheet.write(0,0,'NodeSet')#ColumnAsheet.write(0,1,'AverageValue')#ColumnB#Writedatatothesheetrow_index=1#Startfromrow1foriinrange(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]fornode_valueinlocal_dis_values.values]list_avg=sum(list_num)/len(list_num)#Writetothesheetsheet.write(row_index,0,setname)sheet.write(row_index,1,list_avg)row_index+=1#Movetothenextrow#Savetheworkbooktoafileworkbook.save('3_250.xls')----------------------------------------------------------------------------------------------------------------------------- 接上回:AbaqusPython:后处理用py导出n个集合在某个方向的位移U1/U2/U3数据+逐帧输出-CSDN博客 对需要的帧数的位移进行平均后按照set输出,还要按照帧数输出,继续修改代码: fromodbAccessimport*fromtextReprimport*my_odb=openOdb(r"E:\240426slice\0425s3000t250slice.odb")step=my_odb.steps['Step-1']withopen('test1.txt','a')asf:frame_numbers=list(range(44,61))setnames=['SET-N'+str(i)foriinrange(1,21)]f.write("Frame,")f.write(",".join(setnames))f.write("\n")forframe_numinframe_numbers:frame=step.frames[frame_num]dis_field=frame.fieldOutputs['U']avg_dis_values=[]foriinrange(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]fornode_valueinlocal_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") 解释如下:fromodbAccessimport*fromtextReprimport*my_odb=openOdb(r"E:\240426slice\0425s3000t250slice.odb")step=my_odb.steps['Step-1']withopen('test1.txt','a')asf:# 这一行打开了一个名为“3_250_1.txt”的文本文件,#如果文件不存在则创建它,如果文件已经存在则在文件末尾追加内容,#并将文件句柄保存在变量f中frame_numbers=list(range(44,61))setnames=['SET-N'+str(i)foriinrange(1,21)]f.write("Frame,")f.write(",".join(setnames))f.write("\n")#这段代码生成了一些数字和字符串的列表,#然后将这些内容写入了文件中。#首先将“Frame,”写入文件,#然后将setnames列表中的内容用逗号连接成字符串写入文件,#最后写入一个换行符forframe_numinframe_numbers:frame=step.frames[frame_num]dis_field=frame.fieldOutputs['U']avg_dis_values=[]foriinrange(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]fornode_valueinlocal_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位: fromodbAccessimport*fromtextReprimport*my_odb=openOdb(r"E:\240426slice\0425s3000t250slice.odb")step=my_odb.steps['Step-1']withopen('test2.txt','a')asf:frame_numbers=list(range(44,61))setnames=['SET-N'+str(i)foriinrange(1,21)]f.write("Frame,")f.write(",".join(setnames))f.write("\n")forframe_numinframe_numbers:frame=step.frames[frame_num]dis_field=frame.fieldOutputs['U']avg_dis_values=[]foriinrange(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]fornode_valueinlocal_dis_values.values]avg=sum(list_num)/len(list_num)avg_with_three_decimals="{:.5f}".format(avg)#5avg_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就学到这里!
|
|