我收到错误IndexError: -1
我正在尝试在命令提示符下运行Python脚本。当我通过Abaqus CAE运行时,它可以工作。当我尝试使用没有GUI的命令提示符运行时,我得到了错误。
我能够完成这项工作,但我的脚本也会打开输出数据库。出现此步骤时出现错误。
我试过谷歌搜索它,但我没有更聪明。这在Python中通常意味着什么?为什么我的脚本在abaqus中运行,但只在命令提示符的一半处?
谢谢!
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
我得到了什么
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.
答案 0 :(得分:1)
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'
[...]
IndexError 本身表示您正在尝试访问超出范围的列表或序列的元素。由于您使用的索引为 -1 ,因此您希望访问列表或序列的最后一个元素,因此在访问任何元素之前应检查是否为空,例如:
[...]
for i, var in enumerate(CFNM2):
if i == len(CFNM2) - 1 and var:
maxCf2 = var[-1]
print str(maxCf2) + 'CFNM2 722/721'
[...]
请注意if子句末尾的and var
。