2

I'm working with a commercial analysis software called Abaqus which has a Python interface to read the output values.

I have just given a sample code (which doesn't run) below:

myOdb contains all the information, from which I am extracting the data. The caveat is that i cannot open the file using 2 separate programs.

Code 1 and Code 2 shown below work independently of each other, all they need is myOdb.

Is there a way to parallelize the codes 1 and 2 after I read the odb ?

# Open the odb file 
myOdb = session.openOdb(name=odbPath)

# Code 1
for i in range(1, NoofSteps+1):
    frames = myOdb.steps[stepName].frames 
    lastframe=frames[-1]   
    RFD = lastframe.fieldOutputs['RF'] 

    sum1=0

    for value in RFD.values:
        sum1=sum1+value.data[1]   

# Code 2
for i in range(1, NoofSteps+1):
    frames = myOdb.steps[stepName].frames 
    lastframe=frames[-1]   

    for j in range(4,13):
        file2=open('Fp'+str(j)+stepName,'w')
        b=lastframe.fieldOutputs[var+str(j)]
        fieldValues=b.values
        for v in fieldValues:
            file2.write('%d %6.15f\n' % (v.elementLabel, v.data))
wwl
  • 2,025
  • 2
  • 30
  • 51
Mechanician
  • 525
  • 1
  • 6
  • 20

1 Answers1

2

If all you're trying to do is achieve a basic level of multiprocessing, this is what you need:

import multiprocessing

#Push the logic of code 1 and code 2 into 2 functions. Pass whatever you need
#these functions to access as arguments.

def code_1(odb_object, NoofSteps):
   for i in range(1, NoofSteps+1):
    frames = odb_object.steps[stepName].frames 
    #stepName? Where did this variable come from? Is it "i"?
    lastframe=frames[-1]   
    RFD = lastframe.fieldOutputs['RF'] 

    sum1=0

    for value in RFD.values:
        sum1=sum1+value.data[1]

def code_2(odb_object, NoofSteps):
    for i in range(1, NoofSteps+1):
        frames = odb_object.steps[stepName].frames 
        #stepName? Where did this variable come from? Is it "i"?
        lastframe=frames[-1]   

        for j in range(4,13):
            file2=open('Fp'+str(j)+stepName,'w')
            b=lastframe.fieldOutputs[var+str(j)]
            fieldValues=b.values
            for v in fieldValues:
                file2.write('%d %6.15f\n' % (v.elementLabel, v.data))

if __name__ == "__main__":
    # Open the odb file 
    myOdb = session.openOdb(name=odbPath)
    #Create process objects that lead to those functions and pass the
    #object as an argument.
    p1 = multiprocessing.Process(target=code_1, args=(myOdb,NoofSteps, )) 
    p2 = multiprocessing.Process(target=code_2, args=(myOdb,NoofSteps,)) 
    #start both jobs
    p1.start()
    p2.start()
    #Wait for each to finish.
    p1.join()
    p2.join()
    #Done

Isolate the "main" portion of your code into a main block like I have shown above, do not, and I mean absolutely, do not use global variables. Be sure that all the variables you're using are available in the namespace of each function.

I recommend learning more about Python and the GIL problem. Read about the multiprocessing module here.

stonecharioteer
  • 1,061
  • 11
  • 19