import os import shutil batchVars=[] batchVar1=[] batchVar2=[] batchVar3=[] batchVar4=[] batchVar5=[] bcNames=[] variableNames=[] vFlag=[0,0,0,0,0] n=[0,0,0,0,0] rCount=0 inputFileName = "inputFile.txt" inputFile = open(inputFileName,'r') for line in inputFile: words=line.split() if len(words)==0: continue if words[0]=="resultsName": resName=words[1] if words[0]=="numAve": numAve=int(words[1]) if words[0]=="baseName": baseName=words[1] if words[0]=="batchName": batchName=words[1] if words[0]=="restartFromSolution": restartFlag=1 if words[1]=="yes" else 0 if words[0]=="continueRun": continueFlag=1 if words[1]=="yes" else 0 if words[0]=="mode": runFlag=1 if words[1]=="run" else 0 if words[0]=="resultsRead": resultsEnd="_integrals.txt" if words[1]=="integrals" else "_points.txt" if words[0]=="plPath": words.pop(0) plString = " ".join(words) plPath='"'+plString+'"'+" -run " if words[0]=="batchVars": for i in xrange(1,len(words)): batchVars.append(words[i]) if words[0]=="batchVar1": vFlag[0]=1 for i in xrange(1,len(words)): batchVar1.append(float(words[i])) if words[0]=="batchVar2": vFlag[1]=1 for i in xrange(1,len(words)): batchVar2.append(float(words[i])) if words[0]=="batchVar3": vFlag[2]=1 for i in xrange(1,len(words)): batchVar3.append(float(words[i])) if words[0]=="batchVar4": vFlag[3]=1 for i in xrange(1,len(words)): batchVar4.append(float(words[i])) if words[0]=="batchVar5": vFlag[4]=1 for i in xrange(1,len(words)): batchVar5.append(float(words[i])) if words[0]=="variableNames": for i in xrange(1,len(words)): variableNames.append(words[i]) if vFlag[0]==0: batchVar1.append(1) if vFlag[1]==0: batchVar2.append(1) if vFlag[2]==0: batchVar3.append(1) if vFlag[3]==0: batchVar4.append(1) if vFlag[4]==0: batchVar5.append(1) origFile = baseName+'.spro' resFile = open(resName,'w') header = [] delim="," # delimter can be "," or "\t" rCount=0 for v in batchVars: if vFlag[rCount]==1: resFile.write(v+delim) rCount=rCount+1 for i in variableNames: header.append(i) resFile.write(header[-1]+delim) resFile.close() batchFile = open(batchName,'w') for in5 in xrange(0,len(batchVar5)): n[4] = batchVar5[in5] for in4 in xrange(0,len(batchVar4)): n[3] = batchVar4[in4] for in3 in xrange(0,len(batchVar3)): n[2] = batchVar3[in3] for in2 in xrange(0,len(batchVar2)): n[1] = batchVar2[in2] loopCounter=0 for in1 in xrange(0,len(batchVar1)): n[0] = batchVar1[in1] startFlag = 0 if loopCounter<1 else 1 loopCounter=loopCounter+1 baseNameNew=baseName for i in xrange(0,5): if vFlag[i]==1: baseNameNew = baseNameNew+"_"+str(n[i]) newFile= baseNameNew+'.spro' newFileRes= baseNameNew+'.sres' outFile= open(newFile, 'w') inFile = open(origFile) if restartFlag==0: if continueFlag==0: batchOut = plPath+newFile+"\n" else: batchOut = plPath+newFileRes+"\n" elif startFlag==0: batchOut = plPath+newFile+"\n" else: batchOut = plPath+newFile+" "+readResBase+".sres"+"\n" batchFile.write(batchOut) for line in inFile: words = line.split() if len(words)==0: outFile.write(line) continue wrtFlag=0 for i in xrange(0,5): if vFlag[i]==1: if words[0]== batchVars[i]: newString = batchVars[i]+ " = " +str(n[i])+"\n" outFile.write(newString) wrtFlag=1 if wrtFlag==0: outFile.write(line) outFile.close() print batchOut if runFlag==1: os.system(batchOut) resFile = open(resName,'a') resFile.write("\n") for i in xrange(0,5): if vFlag[i]==1: resFile.write(str(n[i])+delim) # resFile.write(str(n1)+delim+str(n2)+delim+str(n3)+delim+str(n4)+delim+str(n5)+delim) readResBase = baseNameNew readResName = readResBase+resultsEnd firstLine = file(readResName, 'r').readline() for i in xrange(0,len(header)): firstWords = firstLine.split() idxRes=firstWords.index(header[i]) resCollect = [] for j in xrange (0,numAve): colIdx = -numAve+j lastLine = file(readResName, 'r').readlines()[colIdx] lastWords = lastLine.split() resCollect.append(float(lastWords[idxRes])) resAve = sum(resCollect)/float(len(resCollect)) resFile.write(str(resAve)+delim) resFile.close() inFile.close() batchFile.close() resFile.close() raw_input("Press enter to continue")