示例#1
0
    def run(self):
        origPath = self.parser.getArgs()[0]
        parameterFile = self.parser.getArgs()[1]

        self.addLocalConfig(origPath)
        self.checkCase(origPath)

        nrModes=(1 if self.opts.inplaceExecution else 0) + \
                (1 if self.opts.oneClonedCase else 0) + \
                (1 if self.opts.listVariations else 0) + \
                (1 if self.opts.everyVariantOneCase else 0)
        if nrModes == 0:
            self.error(
                "Specify one of the modes --list-variations, --inplace-execution, --one-cloned-case-execution or --every-variant-one-case-execution"
            )
        elif nrModes > 1:
            self.error(
                "The modes --list-variations, --inplace-execution, --one-cloned-case-execution or --every-variant-one-case-execution are mutual exclusive"
            )
        if self.opts.noExecuteSolver:
            if not self.opts.everyVariantOneCase and self.opts.singleVariation == None and not self.opts.listVariations:
                self.error(
                    "--no-execute-solver only works with --every-variant-one-case-execution"
                )

        if not self.opts.clonedCasePrefix:
            self.opts.clonedCasePrefix = path.basename(parameterFile)
        if not self.opts.clonedCasePostfix:
            self.opts.clonedCasePostfix = ""
        else:
            self.opts.clonedCasePostfix = "." + self.opts.clonedCasePostfix
        if not self.opts.cloneToDirectory:
            self.opts.cloneToDirectory = path.dirname(path.abspath(origPath))
        if not self.opts.database:
            self.opts.database = parameterFile + ".database"

        variationData = ParsedParameterFile(
            parameterFile, noHeader=True,
            noVectorOrTensor=True).getValueDict()
        if not "values" in variationData:
            self.error("Entry 'values' (dictionary) needed in", parameterFile)
        if not "solver" in variationData["values"]:
            self.error("Entry 'solver' (list or string) needed in 'values' in",
                       parameterFile)

        fixed = {}
        defaults = {}
        varied = []
        nrVariations = 1

        for k in variationData["values"]:
            v = variationData["values"][k]
            if type(v) != list:
                self.error("Entry", k, "is not a list")
            if len(v) == 1:
                fixed[k] = v[0]
            elif len(v) > 1:
                varied.append((k, v))
                nrVariations *= len(v)
            else:
                self.warning("Entry", k, "is empty")

        if "defaults" in variationData:
            defaults = variationData["defaults"]

        if len(varied) == 0:
            self.error("No parameters to vary")

        self.printPhase(nrVariations, "variations with", len(varied),
                        "parameters")

        def makeVariations(vList):
            name, vals = vList[0]
            if len(vList) > 1:
                var = makeVariations(vList[1:])
                variation = []
                for orig in var:
                    for v in vals:
                        d = orig.copy()
                        if isinstance(v, (dict, DictProxy)):
                            d.update(v)
                        else:
                            d[name] = v
                        variation.append(d)
                return variation
            else:
                return [
                    v if isinstance(v, (dict, DictProxy)) else {
                        name: v
                    } for v in vals
                ]

        variations = [dict(defaults, **d) for d in makeVariations(varied)]
        self["variations"] = variations
        self["fixed"] = fixed

        if self.opts.startVariation != None:
            start = self.opts.startVariation
        else:
            start = 0
        if self.opts.endVariation != None:
            end = self.opts.endVariation
            if end >= len(variations):
                end = len(variations) - 1
        else:
            end = len(variations) - 1

        if self.opts.singleVariation != None:
            if self.opts.startVariation or self.opts.endVariation:
                self.error(
                    "--single-variation not possible with --end-variation-number or --start-variation-number"
                )
            if self.opts.singleVariation < 0:
                self.error("--single-variation must be greater or equal to 0")
            if self.opts.singleVariation >= len(variations):
                self.error("Only", len(variations))
            start = self.opts.singleVariation
            end = self.opts.singleVariation

        if end < start:
            self.error("Start value", start, "bigger than end value", end)

        if self.opts.listVariations:
            self.printPhase("Listing variations")
            for i in range(start, end + 1):
                print_("Variation", i, ":", variations[i])
            return

        if not hasDatabase or self.opts.noDatabaseWrite:
            if path.exists(self.opts.database) and self.opts.createDatabase:
                self.error("database-file", self.opts.database,
                           "exists already.")
            elif not path.exists(
                    self.opts.database
            ) and not self.opts.createDatabase and not self.opts.autoCreateDatabase:
                self.error("database-file", self.opts.database,
                           "does not exist")

        createDatabase = self.opts.createDatabase
        if self.opts.autoCreateDatabase and not path.exists(
                self.opts.database):
            createDatabase = True

        if not hasDatabase or self.opts.noDatabaseWrite:
            db = None
        else:
            db = RunDatabase(self.opts.database,
                             create=createDatabase,
                             verbose=self.opts.verbose)

        origCase = SolutionDirectory(origPath, archive=None)
        if self.opts.oneClonedCase:
            self.printPhase("Cloning work case")
            workCase = origCase.cloneCase(
                path.join(
                    self.opts.cloneToDirectory, self.opts.clonedCasePrefix +
                    "_" + path.basename(origPath)) +
                self.opts.clonedCasePostfix)

        self.printPhase("Starting actual variations")

        for i in range(start, end + 1):
            self.printPhase("Variation", i, "of [", start, ",", end, "]")

            usedVals = variations[i].copy()
            usedVals.update(fixed)

            self.prepareHooks()

            clone = False
            if self.opts.inplaceExecution:
                workCase = origCase
            elif self.opts.oneClonedCase:
                pass
            else:
                self.printPhase("Cloning work case")
                workCase = origCase.cloneCase(
                    path.join(
                        self.opts.cloneToDirectory,
                        self.opts.clonedCasePrefix + "_" +
                        ("%05d" % i) + "_" + path.basename(origPath)) +
                    self.opts.clonedCasePostfix)

            self.processPlotLineOptions(autoPath=workCase.name)

            self.printPhase("Setting up the case")

            self.prepare(workCase, overrideParameters=usedVals)

            if self.opts.noExecuteSolver:
                self.printPhase("Not executing the solver")
                continue

            if self.opts.oneClonedCase or self.opts.inplaceExecution:
                self.setLogname(self.opts.clonedCasePrefix + ("_%05d_" % i) +
                                usedVals["solver"],
                                useApplication=False,
                                force=True)
            else:
                self.setLogname(self.opts.clonedCasePrefix + "_" +
                                usedVals["solver"],
                                useApplication=False,
                                force=True)

            lam = self.getParallel(workCase)

            allLines().clear()
            allPlots().clear()
            resetCustomCounter()

            run = AnalyzedRunner(
                BoundingLogAnalyzer(progress=self.opts.progress,
                                    doFiles=self.opts.writeFiles,
                                    singleFile=self.opts.singleDataFilesOnly,
                                    doTimelines=True),
                silent=self.opts.progress or self.opts.silent,
                splitThres=self.opts.splitDataPointsThreshold
                if self.opts.doSplitDataPoints else None,
                argv=[usedVals["solver"], "-case", workCase.name],
                server=self.opts.server,
                lam=lam,
                logname=self.opts.logname,
                compressLog=self.opts.compress,
                logTail=self.opts.logTail,
                noLog=self.opts.noLog,
                remark=self.opts.remark,
                parameters=usedVals,
                echoCommandLine=self.opts.echoCommandPrefix,
                jobId=self.opts.jobId)

            run.createPlots(customRegexp=self.lines_,
                            splitThres=self.opts.splitDataPointsThreshold
                            if self.opts.doSplitDataPoints else None,
                            writeFiles=self.opts.writeFiles)

            self.runPreHooks()

            self.printPhase("Running")

            run.start()

            self.printPhase("Getting data")

            self["run%05d" % i] = run.data
            if db:
                db.add(run.data)

            self.runPostHooks()

            self.reportUsage(run)
            self.reportRunnerData(run)

        self.printPhase("Ending variation")
示例#2
0
    def doAnalysis(self, line):
        m = self.exp.match(line)
        if m != None:
            name = m.group(2)
            resid = m.group(3)
            time = self.getTime()
            if time != self.told:
                self.told = time
                print("\n t = %6g : " % (float(time))),
            print(" %5s: %6e " % (name, float(resid))),
            sys.stdout.flush()


class CompactAnalyzer(BoundingLogAnalyzer):
    def __init__(self):
        BoundingLogAnalyzer.__init__(self)
        self.addAnalyzer("Compact", CompactLineAnalyzer())


solver = "advDiffMicellesPimpleFoam"
case = "BDCoHBC"
#
#dire=SolutionDirectory(case,archive="InletVariation")
#dire.clearResults()
#dire.addBackup("PyFoamSolve.logfile")
#dire.addBackup("PyFoamSolve.analyzed")
#dire.addBackup("Pressure.analyzed")
#dire.addBackup("MassFlow.analyzed")

run = AnalyzedRunner(CompactAnalyzer(), silent=True)
run.start()
示例#3
0
    def run(self):
        if self.opts.keeppseudo and (not self.opts.regions
                                     and self.opts.region == None):
            warning(
                "Option --keep-pseudocases only makes sense for multi-region-cases"
            )

        if self.opts.region:
            regionNames = self.opts.region
        else:
            regionNames = [None]

        regions = None

        casePath = self.parser.casePath()
        self.checkCase(casePath)
        #        self.addLocalConfig(casePath)

        self.addToCaseLog(casePath, "Starting")
        self.prepareHooks()

        if self.opts.regions or self.opts.region != None:
            print_("Building Pseudocases")
            sol = SolutionDirectory(casePath, archive=None)
            regions = RegionCases(sol, clean=True)

            if self.opts.regions:
                regionNames = sol.getRegions()

        self.processPlotLineOptions(autoPath=casePath)

        lam = self.getParallel(SolutionDirectory(casePath, archive=None))

        self.clearCase(SolutionDirectory(casePath,
                                         archive=None,
                                         parallel=lam is not None),
                       runParallel=lam is not None)

        self.checkAndCommit(SolutionDirectory(casePath, archive=None))

        self.initBlink()

        for theRegion in regionNames:
            args = self.buildRegionArgv(casePath, theRegion)
            self.setLogname()
            run = AnalyzedRunner(BoundingLogAnalyzer(
                progress=self.opts.progress,
                doFiles=self.opts.writeFiles,
                singleFile=self.opts.singleDataFilesOnly,
                doTimelines=True),
                                 silent=self.opts.progress or self.opts.silent,
                                 splitThres=self.opts.splitDataPointsThreshold
                                 if self.opts.doSplitDataPoints else None,
                                 argv=self.replaceAutoInArgs(args),
                                 server=self.opts.server,
                                 lam=lam,
                                 restart=self.opts.restart,
                                 logname=self.opts.logname,
                                 compressLog=self.opts.compress,
                                 logTail=self.opts.logTail,
                                 noLog=self.opts.noLog,
                                 remark=self.opts.remark,
                                 parameters=self.getRunParameters(),
                                 echoCommandLine=self.opts.echoCommandPrefix,
                                 jobId=self.opts.jobId)

            run.createPlots(customRegexp=self.lines_,
                            splitThres=self.opts.splitDataPointsThreshold
                            if self.opts.doSplitDataPoints else None,
                            writeFiles=self.opts.writeFiles)

            if self.cursesWindow:
                self.cursesWindow.setAnalyzer(run.analyzer)
                self.cursesWindow.setRunner(run)
                run.analyzer.addTimeListener(self.cursesWindow)

            self.addWriteAllTrigger(run,
                                    SolutionDirectory(casePath, archive=None))
            self.addLibFunctionTrigger(
                run, SolutionDirectory(casePath, archive=None))
            self.runPreHooks()

            if self.blink1:
                run.addTicker(lambda: self.blink1.ticToc())

            run.start()

            if len(regionNames) > 1:
                self.setData({theRegion: run.data})
            else:
                self.setData(run.data)

            self.runPostHooks()

            self.reportUsage(run)
            self.reportRunnerData(run)

            if theRegion != None:
                print_("Syncing into master case")
                regions.resync(theRegion)

        self.stopBlink()

        if regions != None:
            if not self.opts.keeppseudo:
                print_("Removing pseudo-regions")
                regions.cleanAll()
            else:
                for r in sol.getRegions():
                    if r not in regionNames:
                        regions.clean(r)

        self.addToCaseLog(casePath, "Ended")
示例#4
0
    def run(self):
        casePath = self.parser.casePath()
        self.checkCase(casePath)
        #        self.addLocalConfig(casePath)

        self.addToCaseLog(casePath, "Starting")
        self.prepareHooks()

        self.processPlotLineOptions(autoPath=casePath)

        lam = self.getParallel(SolutionDirectory(casePath, archive=None))

        isParallel = lam is not None

        self.lastWrittenTime = None

        sol = SolutionDirectory(casePath, archive=None, parallel=isParallel)
        ctrlDict = ParameterFile(sol.controlDict(), backup=False)
        if ctrlDict.readParameter("startFrom") != "latestTime":
            self.error(
                "In", casePath,
                "the value of 'startFrom' is not 'latestTime' (required for this script)"
            )

        args = self.replaceAutoInArgs(self.parser.getArgs())

        def checkRestart(data=None):
            lastTimeName = sol.getLast()
            lastTime = float(lastTimeName)
            ctrlDict = ParameterFile(sol.controlDict(), backup=False)
            endTime = float(ctrlDict.readParameter("endTime"))
            if abs(endTime - lastTime) / endTime < 1e-5:
                return "Reached endTime {}".format(endTime)
            logfile = calcLogname(self.opts.logname, args)
            isRestart, restartnr, restartName, lastlog = findRestartFiles(
                logfile, sol)
            # TODO: look into the logfile
            if self.lastWrittenTime is not None:
                if self.lastWrittenTime == lastTimeName:
                    return "Last restart didn't improve on {}. Further restarts make no sense".format(
                        lastTime)
            self.lastWrittenTime = lastTimeName
            if data:
                if "stepNr" in data and data["stepNr"] < self.opts.minimumSteps:
                    return "Only {} steps done while {} are required".format(
                        data["stepNr"], self.opts.minimumSteps)

        redo = True

        reason = checkRestart()
        if reason is not None:
            self.warning("Not starting:", reason)
            redo = False

        self.checkAndCommit(sol)

        self.initBlink()

        startNr = 0

        self.setLogname()

        while redo:
            startNr += 1
            print_()
            print_("Starting restart nr", startNr, "on case", casePath)
            print_()
            self.addToCaseLog(casePath, "Restart nr", startNr, "started")
            run = AnalyzedRunner(BoundingLogAnalyzer(
                progress=self.opts.progress,
                doFiles=self.opts.writeFiles,
                singleFile=self.opts.singleDataFilesOnly,
                doTimelines=True),
                                 silent=self.opts.progress or self.opts.silent,
                                 splitThres=self.opts.splitDataPointsThreshold
                                 if self.opts.doSplitDataPoints else None,
                                 argv=args,
                                 server=self.opts.server,
                                 lam=lam,
                                 logname=self.opts.logname,
                                 compressLog=self.opts.compress,
                                 logTail=self.opts.logTail,
                                 noLog=self.opts.noLog,
                                 remark=self.opts.remark,
                                 parameters=self.getRunParameters(),
                                 echoCommandLine=self.opts.echoCommandPrefix,
                                 jobId=self.opts.jobId)

            run.createPlots(customRegexp=self.lines_,
                            splitThres=self.opts.splitDataPointsThreshold
                            if self.opts.doSplitDataPoints else None,
                            writeFiles=self.opts.writeFiles)

            if self.cursesWindow:
                self.cursesWindow.setAnalyzer(run.analyzer)
                self.cursesWindow.setRunner(run)
                run.analyzer.addTimeListener(self.cursesWindow)

            self.addWriteAllTrigger(run,
                                    SolutionDirectory(casePath, archive=None))
            self.addLibFunctionTrigger(
                run, SolutionDirectory(casePath, archive=None))
            self.runPreHooks()

            if self.blink1:
                run.addTicker(lambda: self.blink1.ticToc())

            run.start()

            if run.data["keyboardInterrupt"]:
                print_()
                self.warning("Not restarting because of keyboard interrupt")

                redo = False

            self.setData({startNr: run.data})

            self.runPostHooks()

            self.reportUsage(run)
            self.reportRunnerData(run)

            self.addToCaseLog(casePath, "Restart nr", startNr, "ended")

            reason = checkRestart(data=run.data)
            if reason is not None:
                print_()
                self.warning("Not starting:", reason)
                self.addToCaseLog(casePath, "Stopping because of", reason)

                redo = False

            if startNr >= self.opts.maximumRestarts:
                print_()
                self.warning("Maximum number", self.opts.maximumRestarts,
                             "restarts reached")
                self.addToCaseLog(casePath, "Stopping because maximum number",
                                  self.opts.maximumRestarts,
                                  "of restarts reached")
                redo = False

        self.stopBlink()

        self.addToCaseLog(casePath, "Ended")

        print_()
        print_("Ended after", startNr, "restarts")
        print_()