1

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

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

我还注意到,尽管写入的文件在另一张光盘(D)中,但在光盘 C: 中的程序空间被占用。重新启动计算机后空间可用。

有谁知道为什么会发生这种情况,我该如何解决?我的意思是如何避免内存变满和程序变慢。这是我的脚本:

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()
4

1 回答 1

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()
于 2015-11-25T11:24:19.653 回答