Transforming an Abaqus macro into a python script
<p>I am using Abaqus (6.13) to run FEM thermal simulations. I need to get the total external heat flux aplied on that model. My searches indicated that the only way to get it was to sum de RFLE history output on the whole model and it works fine. The problem is that I have a ~300 000 elements model and that the simple opening of the Report/XY window takes a couple of hours.</p>

<p>In order to simplify my exportations, I made an exporting macro with the macro manager of Abaqus. The recording starts before importing the odb in abaqus and ends after exporting the report containing the X/Y datas.
This generated macro is big (~900 000 lines) so I give you here a cropped version of it:</p>

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

def OdbMacro1():
    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
    import os
    os.chdir(r"C:\FolderPath")
    session.mdbData.summary()
    o1 = session.openOdb(name='C:\FolderPath\odb.odb')
    session.viewports['Viewport: 1'].setValues(displayedObject=o1)
    odb = session.odbs['C:\FolderPath\odb.odb']
    xy0 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10 in NSET PADSURF_BACK', 
        steps=('Step-2', ), suppressQuery=True)
    xy1 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10053 in NSET PADSURF_BACK', 
        steps=('Step-2', ), suppressQuery=True)
    xy2 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10054 in NSET PADSURF_BACK', 
        steps=('Step-2', ), suppressQuery=True)
    xy3 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10055 in NSET PADSURF_BACK', 
        steps=('Step-2', ), suppressQuery=True)
    xy4 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10056 in NSET PADSURF_BACK', 
        steps=('Step-2', ), suppressQuery=True)
    xy5 = avg((xy0, xy1, xy2, xy3, xy4, ), )
    session.XYData(name='x0.nt11', objectToCopy=xy5, 
        sourceDescription='avg((Nodal temperature: NT11 PI: PAD-1 Node 10 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10053 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10054 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10055 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10056 in NSET PADSURF_BACK, ),)')
    odb = session.odbs['C:\FolderPath\odb.odb']
    xy0 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 1', steps=('Step-2', 
        ), suppressQuery=True)
    xy1 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 2', steps=('Step-2', 
        ), suppressQuery=True)
    xy2 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 3', steps=('Step-2', 
        ), suppressQuery=True)
[...]
    xy280068 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='RFLE11: RFLE11 PI: SLIDER-1 Node 210034', steps=(
        'Step-2', ), suppressQuery=True)
    xy280069 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='RFLE11: RFLE11 PI: SLIDER-1 Node 210035', steps=(
        'Step-2', ), suppressQuery=True)
    xy280070 = sum((xy0, xy1, xy2, xy3, xy4, xy5, xy6, xy7, xy8, xy9, xy10, xy11, 
        xy12, xy13, xy14, xy15, xy16, xy17, xy18, xy19, xy20, xy21, xy22, xy23, 
        xy24, xy25, xy26, xy27, xy28, xy29, xy30, xy31, xy32, xy33, xy34, xy35, 
[...]
        xy280057, xy280058, xy280059, xy280060, xy280061, xy280062, xy280063, 
        xy280064, xy280065, xy280066, xy280067, xy280068, xy280069, ), )
    session.XYData(name='model.RFLE', objectToCopy=xy280070, 
        sourceDescription='sum((RFLE11: RFLE11 PI: PAD-1 Node 1, RFLE11: RFLE11 PI: PAD-1 Node 2, RFLE11: RFLE11 PI: PAD-1 Node 3,
[...]
 RFLE11: RFLE11 PI: SLIDER-1 Node 210033, RFLE11: RFLE11 PI: SLIDER-1 Node 210034, RFLE11: RFLE11 PI: SLIDER-1 Node 210035, ),)')
    odb = session.odbs['C:\FolderPath\odb.odb']
    xy0 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 5', 
        steps=('Step-2', ), suppressQuery=True)
    xy1 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 6', 
        steps=('Step-2', ), suppressQuery=True)
    xy2 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12
[................................................................]
=True)
    xy6000 = xyPlot.XYDataFromHistory(odb=odb, 
        outputVariableName='Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18048', 
        steps=('Step-2', ), suppressQuery=True)
    xy6001 = sum((xy0, xy1, xy2, xy3, xy4, xy5, xy6, xy7, xy8, xy9, xy10, xy11, 
        xy12, xy13, xy14, xy15, xy16, xy17, xy18, xy19, xy20, xy21, xy22, xy23, 

[................................................................]
        xy5991, xy5992, xy5993, xy5994, xy5995, xy5996, xy5997, xy5998, xy5999, 
        xy6000, ), )
    session.XYData(name='surf.hfla', objectToCopy=xy6001, 
        sourceDescription='sum((Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 5, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 6, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12050, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12051, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12052, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12053, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12054, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12055, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12056, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12057, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12058, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12059, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12060, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12061, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12062, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/
[................................................................]
37, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18038, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18039, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18040, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18041, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18042, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18043, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18044, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18045, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18046, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18047, Heat flux: HFLA     ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18048, ),)')
    x0 = session.xyDataObjects['surf.hfla']
    x1 = session.xyDataObjects['model.RFLE']
    x2 = session.xyDataObjects['x0.nt11']
    session.xyReportOptions.setValues(interpolation=ON)
    session.writeXYReport(fileName='C:\FolderPath\report.rpt', 
        appendMode=OFF, xyData=(x0, x1, x2))

OdbMacro1()
</code></pre>

<p>I added the call to OdbMacro1 at the end, following the search results I got here and there.</p>

<p>I want to run that macro (or at least the useful part) outside the GUI from a python file. When I do (with "C:\Path\to\python\file\folder>abaqus python macro.py"), I get an error: </p>

<pre><code>Traceback (most recent call last):
  File "macro.py", line 3, in &lt;module&gt;
    from abaqus import *
  File "SMAPyaModules\SMAPyaAbqPy.m\src\abaqus.py", line 15, in &lt;module&gt;
ImportError: abaqus module may only be imported in the Abaqus kernel process
</code></pre>

<p>. I don't understand what the problem is. I tried adding "import odbAccess" at the beginning of the file but I get the same error. I think I should be adding some code at the beginning but I can't get around which. Could you help me?</p>

<p>By the way, it is secondary, but I feel like I could simplify the function: </p>

<ul>
<li>Among the bunch of import at the beginning of the function I am not
sure all are needed may I delete some?</li>
<li>All the nodes whose history
output are summed (or averaged) in the same operation form a set.
Isn't there a way to use that to avoid using each individual history
output in the macro?</li>
</ul>

<p>Thank you for any lead to the answer. :)</p>

<p>here is a script that does essentially what you want:
(and you see we only need the three import's)</p>

<pre><code>from abaqus import *
from abaqusConstants import *
import visualization
odb=session.openOdb(name='/full/path/Job-1.odb')
session.viewports['Viewport: 1'].setValues(displayedObject=odb)
session.xyDataListFromField(odb=odb, outputPosition=NODAL,
          variable=(('NT11', NODAL),  ),
                   nodeSets=('PART-1-1.SETNAME', ))
keyname='From Field Data: NT11  at part instance PART-1-1'
# run this to see what the keys look like:
# [ o.description for o in session.xyDataObjects.values() ]
temp=[o for o in session.xyDataObjects.values() if
         o.description.find(keyname)==0]
#note if you only have requested one data type you could just do:
#temp=session.xyDataObjects.values()
session.writeXYReport(fileName='test.rpt', xyData=temp)
#alternate way to directly write data instead of using writexyreport:
f=open('test.dat','w')
for o in temp: f.write('%i %g\n'%
     (int(o.description.split()[-1]),o.data[-1][-1]))
f.close()
</code></pre>

<p>run with <code>abaqus cae -noGUI script.py</code> or <code>abaqus cae noGUI=script.py</code></p>

python macros abaqus