GeMa
GeMa

Reputation: 159

Running out of memory python Abaqus

I have written a script in python to postprocess a group of files in Abaqus. The script opens in sequence the abaqus output-database-files, reads the results of several nodes, write these results in a .txt file and closes the odbs (output databases. At the beginning the procedure is very quickly. After several opening and closing of files (after i.e. 50 times) the procedure becomes even more slow and at the end (maybe because of a missing command) the programm (Abaqus) crashes.

I have added the gc.collect command and somehow it helped but the problem remains.

I have also noticed that during the procedure space in disc C: is occupied although the written files are in another disc (D). The space is free after restarting the computer.

Does anyone have an idea why does it happen and how could I solve it? I mean how can I avoid the memory getting full and the procedure slow. Here is my script:

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

Upvotes: 1

Views: 1719

Answers (1)

DougR
DougR

Reputation: 3479

Here is some suggestions:

1 - Open odbs from a abaqus python command prompt using odbAccess. Not using Abaqus Viewer might save some memory

2 - Extract the data from the frames in the odb directly. Do not use the XY Data list operations.

The code below is not tested but it might give you some ideas. You will most likely have to debug it if you want to use it. I didn't know exactly what data you were trying to extract from the odb but I made a guess.

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

Upvotes: 1

Related Questions