Reputation: 313
I get the error IndexError: -1
I'm trying to run a Python script in command prompt. When I run it through Abaqus CAE, it works. When I try run using command prompt without GUI i get the error.
I am able to complete the job, but my script also opens the output database. It is at this step error appears.
I've tried googling it, but I didn't get any smarter. What does this generally mean in Python? And why does my script work in abaqus, but only halfway through command prompt?
Thank you!
from time import gmtime, strftime
import time, os
from abaqus import *
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
from odbAccess import *
mdb = openMdb(pathName='X:\MinPreload-VaryingBC')
modelName = 'minPreload'
cMdb = mdb.models[modelName]
date = strftime("%Y-%d-%b", gmtime())
logPath_bu = 'C:\Users\yyt\maxMin-%s.txt' %date
logObj_bu = open(logPath_bu, 'a')
TopOfThePage = 'JobName\tCFNM at Preload\tTime Finished\tPreload\n\n'
logObj_bu.write(TopOfThePage)
logObj_bu.close()
output = open('C:\yyt\Output-%s.txt' %date, 'a')
output.write('Job \t\t\t'+'PS723\t\t'+'PS721\t\t'+'PL\t\t'+'FRIC\n\n')
output.close()
jobName = ""
maxCf = 0.0
#Temperature
minTemp = -49
maxTemp = 101
temperature = [minTemp, maxTemp]
bcTemp = 'Temperature'
#Friction
friction = [0.1, 0.1, 0.2, 0.2, 0.3, 0,3, 0.4, 0,4, 0.5, 0.5]
interactionProperty = 'Contact Seal Coupler'
#PressureSwitcher
Pressure = True
internalPressure = 76
cMdb.loads['IP'].resume('Pressure')
internalPressBC = cMdb.loads['IP']
setInternalPressureBC = internalPressBC.setValues(magnitude=internalPressure)
externalPressure = 33
cMdb.loads['ExtP'].resume('Pressure')
externalPressBC = cMdb.loads['ExtP']
setExternalPressureBC = externalPressBC.setValues(magnitude=externalPressure)
# Preload
adjustedPreload = 0
PLipht = 10
PLiplt = 10
PLextpht = 10
PLextplt = 10
for temp in temperature:
output = open('C:\yyt\Output-%s.txt' %date, 'a')
output.write('\n')
output.close()
tempBC = cMdb.boundaryConditions[bcTemp]
setTemp = tempBC.setValues(magnitude=temp)
if temp == minTemp:
adjustedPreload = 0.10315
else:
adjustedPreload = -0.21288
for fric in friction:
tangProp = cMdb.interactionProperties[interactionProperty].tangentialBehavior
tangProp.setValues(table=((fric,),))
if Pressure:
cMdb.loads['IP'].resume('Pressure')
cMdb.loads['ExtP'].suppress('Pressure')
press = 'IP'
else:
cMdb.loads['ExtP'].resume('Pressure')
cMdb.loads['IP'].suppress('Pressure')
press = 'ExtP'
if temp == maxTemp and Pressure:
PLi = PLipht
elif temp == minTemp and Pressure:
PLi = PLiplt
elif temp == maxTemp and Pressure:
PLi = PLextpht
elif temp == minTemp and Pressure:
PLi = PLextplt
for PL in range (PLi, 100):
axPL = float(PL)/100
preloadBC = cMdb.boundaryConditions['axial_move']
setPreloadBC = preloadBC.setValuesInStep('Pre', u2=axPL)
clampBC = cMdb.boundaryConditions['clamp']
modifyBottomCoupler = clampBC.setValuesInStep('Temp', -adjustedPreload )
setPreloadBC = preloadBC.setValuesInStep('Temp', u2=(axPL+adjustedPreload))
#Create a job
fricInt = fric*10
jobName = date + '_%dC-0_%df-%s-0_%dPLl' %(temp, fricInt, press, PL)
mdb.Job(model=modelName, name=jobName)
#submit Job
mdb.jobs[jobName].submit(consistencyChecking=OFF)
#Wait for job to complete
status = str(mdb.jobs[jobName].messages[-1].type)
mdb.jobs[jobName].waitForCompletion()
#Collect data from job
lastStepName = 'Pressure'
odb = session.openOdb(name=jobName+'.odb')
region = odb.steps[lastStepName].historyRegions['NodeSet Z000002']
region2 = odb.steps[lastStepName].historyRegions['NodeSet Z000001']
CFNM1 = region.historyOutputs['CFNM ASSEMBLY_SLAVEBOT/ASSEMBLY_MASTERBOT'].data
CFNM2 = region2.historyOutputs['CFNM ASSEMBLY_SLAVESURFTOP/ASSEMBLY_MASTERTOP'].data
for i, var in enumerate(CFNM1):
if i == len(CFNM1) - 1:
maxCf1 = var[-1]
print str(maxCf1) + 'CFNM1 724/723'
for i, var in enumerate(CFNM2):
if i == len(CFNM2) - 1:
maxCf2 = var[-1]
print str(maxCf2) + 'CFNM2 722/721'
output = open('C:\yyt\Output-%s.txt' %date, 'a')
output.write(jobName+'\t\t'+str(maxCf1)+'\t\t'+str(maxCf2)+'\t\t'+'0.'+str(PL)+'\t\t'+str(fric)+'\n')
output.close()
if maxCf1 >= 10000 and maxCf2 >= 10000:
time_done = strftime("%H:%M:%S", gmtime())
logObj_bu = open(logPath_bu, 'a')
logObj_bu.write(jobName+'\t'+' CFNM1 = ' + str(maxCf1)+' CFNM2 = ' + str(maxCf2)+'\t' + time_done + '%d\n\n' %PL)
logObj_bu.close()
Pressure = not Pressure
if temp == maxTemp and Pressure:
PLipht = axPL-0.02
elif temp == minTemp and Pressure:
PLiplt = axPL-0.02
elif temp == maxTemp and Pressure:
PLextpht = axPL-0.02
elif temp == minTemp and Pressure:
PLextplt = axPL-0.02
break
What I get
End Abaqus/Standard Analysis
Abaqus JOB 2014-06-Mar_-49C-0_1f-IP-0_10PLl COMPLETED
IndexError: -1
Abaqus Error: cae exited with an error.
Upvotes: 0
Views: 1684
Reputation: 39853
You have three potential pitfalls concerning the IndexError: -1
[...]
status = str(mdb.jobs[jobName].messages[-1].type)
[...]
for i, var in enumerate(CFNM1):
if i == len(CFNM1) - 1:
maxCf1 = var[-1]
print str(maxCf1) + 'CFNM1 724/723'
for i, var in enumerate(CFNM2):
if i == len(CFNM2) - 1:
maxCf2 = var[-1]
print str(maxCf2) + 'CFNM2 722/721'
[...]
The IndexError itself indicates, that you are trying to access an elment of a list or sequence, which is out of range. Since you are using an index of -1, you want to access the last element of the list or sequence, so you should check for none emptiness before accessing any element, e.g.:
[...]
for i, var in enumerate(CFNM2):
if i == len(CFNM2) - 1 and var:
maxCf2 = var[-1]
print str(maxCf2) + 'CFNM2 722/721'
[...]
Note the and var
at the end of the if clause.
Upvotes: 1