简体   繁体   English

在Abaqus宏(Python)中编写for循环

[英]Write a for loop in Abaqus Macro (Python)

I've been using Abaqus for a while but I'm new to macros and python script. 我已经使用Abaqus一段时间了,但是我是宏和python脚本的新手。 I'm sorry if this kind of question has already been asked, I did search on google to see if there was a similar problem but nothing works.. 很抱歉,如果您已经问过这种问题,我确实在Google上进行了搜索,看看是否存在类似的问题,但是没有任何效果。

My problem is the following : 我的问题如下:

I have a model in Abaqus, I've ran an analysis with 2 steps and I created a path in it and I'd like to extract the value of the Von Mises stress along this path for each frame of each step. 我在Abaqus中有一个模型,我用2个步骤进行了分析,并在其中创建了一条路径,我想针对每个步骤的每一帧沿此路径提取冯·米塞斯应力值。 Ideally I'd love to save it into an Excel or a .txt file for easy further analysis (eg in Matlab). 理想情况下,我希望将其保存到Excel或.txt文件中以便于进一步分析(例如在Matlab中)。

Edit : I solved part of the problem, my macro works and all my data is correctly saved in the XY-Data manager. 编辑:我解决了部分问题,我的宏工作并且我的所有数据都正确保存在XY数据管理器中。

Now I'd like to save all the "Y" data in an excel or text file and I have no clue on how to do that. 现在,我想将所有“ Y”数据保存在excel或文本文件中,但我不知道该怎么做。 I'll keep digging but if anyone has an idea I'll take it ! 我会继续挖掘,但是如果有人有想法,我会接受!

Here's the code from the abaqusMacros.py file : 这是abaqusMacros.py文件中的代码:

# -*- coding: mbcs -*-
# Do not delete the following import lines
from abaqus import *
from abaqusConstants import *
import __main__



def VonMises():
    import section
    import regionToolset
    import displayGroupMdbToolset as dgm
    import part
    import material
    import assembly
    import step
    import interaction
    import load
    import mesh
    import optimization
    import job
    import sketch
    import visualization
    import xyPlot
    import displayGroupOdbToolset as dgo
    import connectorBehavior

    odbFile = session.openOdb(name='C:/Temp/Job-1.odb')
    stepsName = odbFile.steps.keys()
    for stepId in range(len(stepsName)):
            numberOfFrames = len(odbFile.steps.values()[stepId].frames)
            for frameId in range(numberOfFrames):
                    session.viewports['Viewport: 1'].odbDisplay.setPrimaryVariable(
                        variableLabel='S', outputPosition=INTEGRATION_POINT, refinement=(
                            INVARIANT, 'Mises'))
                    session.viewports['Viewport: 1'].odbDisplay.setFrame(step=stepId, frame=frameId)
                    pth = session.paths['Path-1']
                    session.XYDataFromPath(name='Step_'+str(stepId)+'_'+str(frameId), path=pth, includeIntersections=False, 
                        projectOntoMesh=False, pathStyle=PATH_POINTS, numIntervals=10, 
                        projectionTolerance=0, shape=DEFORMED, labelType=TRUE_DISTANCE)

First of all, your function VonMises contains only import statements, other parts of the code are not properly indented, so they are outside of the function. 首先,您的函数VonMises仅包含import语句,代码的其他部分未正确缩进,因此它们不在函数之外。

Second of all, the function is never called. 第二,该函数永远不会被调用。 If you run your script using 'File > Run script', then you should call the function at the end of your file. 如果使用“文件>运行脚本”运行脚本,则应在文件末尾调用该函数。

There two thing seem like obvious errors, but there are some other bad things as well. 有两件事看似很明显的错误,但也有其他一些不好的事情。

Also, I don't see the point of writing import __name__ at the top of your file because I really doubt you have a module name __name__ ; 另外,我看不到在文件顶部写import __name__ ,因为我真的怀疑您是否具有模块名称__name__ Python environment used by Abaqus probably doesn't either. Abaqus使用的Python环境可能也不是。

There are some other things that might be improved as well, but you should try to fix the errors first. 还有一些其他方面可能会得到改进,但是您应该首先尝试修复错误。

If you got an actual error message from Abaqus (either in the window or in abaqus.rpy file), it would be helpful if you posted it here. 如果您收到来自Abaqus的实际错误消息(在窗口或abaqus.rpy文件中),则将其张贴在此处会很有帮助。

Got it : 得到它了 :

I'll use the code posted above, repeated here : 我将使用上面发布的代码,在此重复:

# -*- coding: mbcs -*-
# Do not delete the following import lines
from abaqus import *
from abaqusConstants import *
import __main__



def VonMises():
    import section
    import regionToolset
    import displayGroupMdbToolset as dgm
    import part
    import material
    import assembly
    import step
    import interaction
    import load
    import mesh
    import optimization
    import job
    import sketch
    import visualization
    import xyPlot
    import displayGroupOdbToolset as dgo
    import connectorBehavior

    odbFile = session.openOdb(name='C:/Temp/Job-1.odb')
    stepsName = odbFile.steps.keys()
    for stepId in range(len(stepsName)):
            numberOfFrames = len(odbFile.steps.values()[stepId].frames)
            for frameId in range(numberOfFrames):
                    session.viewports['Viewport: 1'].odbDisplay.setPrimaryVariable(
                        variableLabel='S', outputPosition=INTEGRATION_POINT, refinement=(
                            INVARIANT, 'Mises'))
                    session.viewports['Viewport: 1'].odbDisplay.setFrame(step=stepId, frame=frameId)
                    pth = session.paths['Path-1']
                    session.XYDataFromPath(name='Step_'+str(stepId)+'_'+str(frameId), path=pth, includeIntersections=False, 
                        projectOntoMesh=False, pathStyle=PATH_POINTS, numIntervals=10, 
                        projectionTolerance=0, shape=DEFORMED, labelType=TRUE_DISTANCE)

And I just discovered the "Excel Utilities" tool on Abaqus which is enough for what I want to do. 我刚刚在Abaqus上发现了“ Excel Utilities”工具,足以满足我的需求。

Thanks y'all for your input. 谢谢大家的投入。

Here is how you extract XY-data 这是提取XY数据的方法

from odbAccess import *        


odb = session.odbs['C:/Job-Directory/Job-1.odb']        
output = open('Result.dat', 'w')        

for i in range (0,Number-of-XYData-to-extract):        
    xy1 = odb.userData.xyDataObjects['XYData-'+str(i)]        
    for x in range (0,len(xy1)):        
        output.write ( str(xy1[x]) + "\n" )        
output.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM