耗尽内存python Abaqus

时间:2015-11-24 18:02:00

标签: python memory abaqus

我在python中编写了一个脚本,用于在Abaqus中对一组文件进行后处理。该脚本按顺序打开abaqus输出数据库文件,读取几个节点的结果,将这些结果写入.txt文件并关闭odbs(输出数据库。一开始程序非常快。几次打开和关闭后文件(在50次之后)程序变得更加缓慢并且最后(可能是因为缺少命令)程序(Abaqus)崩溃。

我添加了gc.collect命令并以某种方式帮助了但问题仍然存在。

我还注意到在光盘C中的程序空间中,虽然写入的文件在另一张光盘(D)中,但是它被占用了。重新启动计算机后,该空间是免费的。

有没有人知道为什么会这样,我怎么能解决它?我的意思是如何避免内存变满并且程序变慢。这是我的剧本:

from abaqus import *
from abaqusConstants import *
from caeModules import *
from driverUtils import executeOnCaeStartup
import gc
executeOnCaeStartup()
session.viewports['Viewport: 1'].partDisplay.geometryOptions.setValues(
referenceRepresentation=ON)
nodes = [1001,1201,1401,1601,1801,2001,2201,2401,2601,2801,3001]
a = ["5"]
Kx = ["0001", "001","1"]
dth = ["0", "05" , "1" ,"5", "10"]
dw = ["0","1","5" ,"10" ,"40"]
fmt = "WD_dw{w}_dth{y}_Kx{z}_a{x}"
k = 0
j = 0
for x in a:
    j = j + 1
    print 'j =',j
    f = open("WD_sin_results_{j}.txt".format(j=j), 'w')
    for z in Kx:
        for y in dth:
            for w in dw:
                f.write("\n" + fmt.format(w=w, z=z, y=y, x=x)+ "\n")
                lista = []
                listb = []
                listc = []
                xy1 = []
                k = k + 1
                print 'k =',k
                o1 = session.openOdb(name='E:\Abaqus\WD_sin_p1_c{k}.odb'.format(k=k))
                session.viewports['Viewport: 1'].setValues(displayedObject=o1)
                leaf = dgo.LeafFromOdbElementMaterials(elementMaterials=('SOIL', ))
                session.viewports['Viewport: 1'].odbDisplay.displayGroup.replace(leaf=leaf)
                odbName=session.viewports[session.currentViewportName].odbDisplay.name
                session.odbData[odbName].setValues(activeFrames=(('Step-1', ('0:-1', )), ))
                odb = session.odbs['E:\Abaqus\WD_sin_p1_c{k}.odb'.format(k=k)]
                for i in nodes:
                    print 'i =',i
                    session.xyDataListFromField(odb=odb, outputPosition=NODAL, variable=(('CPRESS', ELEMENT_NODAL), ), nodeLabels=(('PART-1-1', ('%i'%(i), )), ))
                    xy1 = []
                    xy1 = session.xyDataObjects['CPRESS (Not averaged) PI: PART-1-1 N: %i'%(i)]
                    xy1.setValues(
                    sourceDescription='CPRESS (Not averaged) PI: PART-1-1 N: %i'%(i))
                    xy2 = xy1[530]
                    xy3 = xy2[1]
                    print 'xy3:', xy3
                    if xy3 < 0:                  
                        lista = currentMin(truncate(xyData=xy1, startX=5, endX=6))
                        lista.setValues(
                        sourceDescription='currentMin ( truncate ( xyData="CPRESS (Not averaged) PI: PART-1-1 N: {i}" ,startX=5, endX=6  ) )'.format(i=i))
                    else:
                        lista = currentMax(truncate(xyData=xy1, startX=5, endX=6))
                        lista.setValues(
                        sourceDescription='currentMax ( truncate ( xyData="CPRESS (Not averaged) PI: PART-1-1 N: {i}" ,startX=5, endX=6  ) )'.format(i=i))
                    listb = lista[100]
                    print "listb:", lista[100]
                    listc = listb[1]
                    print "listc:", listb[1]
                    f.write('%s'%listc+ "\n")
                    del session.xyDataObjects['CPRESS (Not averaged) PI: PART-1-1 N: %i'%(i)]
                    del session.xyDataObjects['CPRESS (Not averaged) PI: PART-1-1 N: %i'%(i)]
                    del session.xyDataObjects['_temp_1']
                    del session.xyDataObjects['_temp_2']
                    del session.xyDataObjects['_temp_3']
                    del session.xyDataObjects['_temp_4']
                    del session.xyDataObjects['_temp_5']
                    del session.xyDataObjects['_temp_6']
                    del session.xyDataObjects['_temp_7']
                    del session.xyDataObjects['_temp_8']
                    del session.xyDataObjects['_temp_9']
                    del session.xyDataObjects['_temp_10']
                    del session.xyDataObjects['_temp_11']
                    del session.xyDataObjects['_temp_12']
                    del session.xyDataObjects['_temp_13']
                    del session.xyDataObjects['_temp_14']
                    del session.xyDataObjects['_temp_15']
                    del session.xyDataObjects['_temp_16']
                    del session.xyDataObjects['_temp_17']
                    del session.xyDataObjects['_temp_18']
                    del session.xyDataObjects['_temp_19']
                    del session.xyDataObjects['_temp_20']
                    del session.xyDataObjects['_temp_21']
                    del session.xyDataObjects['_temp_22']
                session.odbs['E:\Abaqus\WD_sin_p1_c{k}.odb'.format(k=k)].close()
    gc.collect()
    f.close()

1 个答案:

答案 0 :(得分:1)

以下是一些建议:

1 - 使用odbAccess从abaqus python命令提示符打开odbs。不使用Abaqus Viewer可能会节省一些内存

2 - 直接从odb中的帧中提取数据。不要使用XY数据列表操作。

以下代码未经过测试,但可能会给您一些想法。如果要使用它,您很可能必须调试它。我不确切地知道你试图从odb中提取什么数据,但我做了一个猜测。

from abaqus import *
from abaqusConstants import *
from odbAccess import *
import odbAccess
from caeModules import *
from driverUtils import executeOnCaeStartup
import numpy as np

nodeLabels = [1001,1201,1401,1601,1801,2001,2201,2401,2601,2801,3001]
instanceName='PART-1-1'
a = ["5"]
Kx = ["0001", "001","1"]
dth = ["0", "05" , "1" ,"5", "10"]
dw = ["0","1","5" ,"10" ,"40"]
fmt = "WD_dw{w}_dth{y}_Kx{z}_a{x}"
k = 0
j = 0
for x in a:
    j += 1
    print 'j =',j
    with open("WD_sin_results_{j}.txt".format(j=j), 'w') as f:
        for z in Kx:
            for y in dth:
                for w in dw:
                    f.write("\n" + fmt.format(w=w, z=z, y=y, x=x)+ "\n")
                    k += 1
                    print 'k =',k
                    odb = odbAccess.openOdb(name='E:\Abaqus\WD_sin_p1_c{k}.odb'.format(k=k))
                    step=odb.steps.values()[0]  # evaluate 1st step in odb
                    frames=step.frames # evaluate all frames in step
                    inst=odb.rootAssembly.instances[instanceName]

                    matrix=np.zeros((len(nodeLabels),len(frames)))  #initialse NumPy array, array = node x frame
                    for frameNum,frame in enumerate(frames): 
                        CPRESSframe=frame.fieldOutputs['CPRESS']  
                        for nodeNum,nodeLabel in enumerate(nodeLabels):
                            node=inst.getNodeFromLabel(nodeLabel)
                            matrix[nodeNum,frameNum]=CPRESSframe.getSubset(region=node).values[0].data

                    # Write out the largest absolute cpress over all the frames for each node
                    for nodeNum,nodeLabel in enumerate(nodeLabels):
                        idx=np.argmax(abs(matrix[nodeNum,:])) # find the frame with the largest absolute cpress for this node

                        f.write('%s'%matrix[nodeNum,idx]+ "\n")      

                    odb.close()