Beispiel #1
0
    def run(self):
        fName=self.parser.getArgs()[0]

        dom=parse(fName)
        doc=dom.documentElement

        if doc.tagName!='comparator':
            error("Wrong root-element",doc.tagName,"Expected: 'comparator'")

        self.data=ComparatorData(doc)

        purge=False
        if doc.hasAttribute('purge'):
            purge=eval(doc.getAttribute('purge'))
        if self.opts.purge:
            purge=self.opts.purge
        if self.opts.nopurge:
            purge=False

        steady=False
        if doc.hasAttribute('steady'):
            steady=eval(doc.getAttribute('steady'))
        if self.opts.steady:
            purge=self.opts.steady

        print_(" Parameters read OK ")
        print_()

        aLog=open(self.data.id+".overview","w")
        csv=CSVCollection(self.data.id+".csv")

        rDir=self.data.id+".results"
        rmtree(rDir)
        mkdir(rDir)

        calculated=0
        format="%%0%dd" % len(str(len(self.data)))

        for i in range(len(self.data)):
            runID=(format % i)
            print_(runID,end=" ",file=aLog)
            csv["ID"]=runID

            use,para=self.data[i]
            para["template"]=self.data.template
            para["extension"]=self.data.extension
            para["id"]=self.data.id

            if use:
                calculated+=1

            print_("Executing Variation",i+1,"of",len(self.data),end=" ")
            if calculated!=i+1:
                print_("(",calculated,"actually calculated)")
            else:
                print_()

            print_("Parameters:",end=" ")
            for k,v in iteritems(para):
                print_("%s='%s' " % (k,v),end=" ")
                if v.find(" ")>=0 or v.find("\t")>=0:
                    v="'"+v+"'"
                print_(v,end=" ",file=aLog)
                csv[k]=v

            print_()

            if not use:
                print_("Skipping because not all conditions are satisfied")
                csv.clear()
                print_()
                continue

            cName=("%s."+format) % (self.data.id, i)
            log=open(cName+".log","w")

            para["case"]=cName
            print_("Case-directory:",cName)
            para["results"]=path.join(rDir,runID)
            print_("Results directory:",para["results"])
            mkdir(para["results"])

            if path.exists(cName):
                if self.opts.removeOld:
                    print_("   Removing old case-directory")
                    rmtree(cName)
                else:
                    error("Case-directory",cName,"exists")

            print_("   copying template")
            out=copytree(self.data.template,cName)
            print_("---- Copying",file=log)
            for l in out:
                print_(l,end=" ",file=log)

            print_("   preparing")
            ok,erg=self.data.prep.execute(para,log)
            print_(ok,end=" ",file=aLog)
            csv["prepare OK"]=ok

            for i in range(len(erg)):
                print_(erg[i],end=" ",file=aLog)
                csv["Prepare %02d" % i]=erg[i]

            aLog.flush()

            if self.opts.test:
                print_("   Skipping execution")
            else:
                print_("   running the solver")
                sys.stdout.flush()

                if steady:
                    runnerClass=ConvergenceRunner
                else:
                    runnerClass=AnalyzedRunner

                run=runnerClass(BoundingLogAnalyzer(doTimelines=True,progress=True),
                                argv=[self.data.solver,".",cName],
                                silent=True,
                                lam=Command.parallel,
                                server=self.opts.server)

                run.start()
                ok=run.runOK()
                if ok:
                    print_("   executed OK")
                else:
                    print_("   fatal error")

                for aName in run.listAnalyzers():
                    a=run.getAnalyzer(aName)
                    if 'titles' in dir(a):
                        for tit in a.lines.getValueNames():
                            t,v=a.getTimeline(tit)
                            if len(v)>0:
                                para["result_"+aName+"_"+tit]=v[-1]

                print_(run.runOK(),run.lastTime(),run.run.wallTime(),end=" ",file=aLog)
                csv["Run OK"]=run.runOK()
                csv["End Time"]=run.lastTime()
                csv["Wall Time"]=run.run.wallTime()
                csv["Wall Time (Foam)"]=run.totalClockTime()
                csv["CPU Time"]=run.totalCpuTime()
                csv["Wall Time First Step"]=run.firstClockTime()
                csv["CPU Time First Step"]=run.firstCpuTime()

                para["endTime"]=run.lastTime()
                para["runlog"]=run.logFile

                if self.opts.showDict:
                    print_(para)

                print_("   evaluating results")

                ok,erg=self.data.post.execute(para,log)

                if Command.parallel!=None:
                    print_("  Stoping LAM")
                    Command.parallel.stop()
                    Command.parallel=None

                if ok:
                    print_("  Evaluation OK",end=" ")
                else:
                    print_("  Evaluation failed",end=" ")

                if len(erg)>0:
                    print_(":",erg,end=" ")
                print_()

                print_(ok,end=" ",file=aLog)
                for i in range(len(erg)):
                    print_(erg[i],end=" ",file=aLog)
                    csv["Post %02d" % i]=erg[i]

            if purge:
                print_("   removing the case-directory")
                out=rmtree(cName)
                print_("---- Removing",file=log)
                for l in out:
                    print_(l,end=" ",file=log)

            log.close()
            print_()
            print_(file=log)
            aLog.flush()
            csv.write()

        aLog.close()