Exemplo n.º 1
0
    def run(self):
        if not self.opts.keepPostprocessing:
            self.opts.additional.append("postProcessing")

        for cName in self.parser.getArgs():
            if self.checkCase(cName,
                              fatal=self.opts.fatal,
                              verbose=not self.opts.silent):
                self.addLocalConfig(cName)

                if self.opts.verbose:
                    print_("Clearing", cName)

                sol = SolutionDirectory(cName,
                                        archive=None,
                                        paraviewLink=False)
                sol.clear(
                    after=self.parser.getOptions().after,
                    processor=self.parser.getOptions().processor,
                    pyfoam=self.parser.getOptions().pyfoam,
                    vtk=self.parser.getOptions().vtk,
                    removeAnalyzed=self.parser.getOptions().removeAnalyzed,
                    keepRegular=self.parser.getOptions().keepRegular,
                    keepParallel=self.parser.getOptions().keepParallel,
                    keepLast=self.parser.getOptions().latest,
                    keepInterval=self.parser.getOptions().keepInterval,
                    clearHistory=self.parser.getOptions().clearHistory,
                    clearParameters=self.parser.getOptions().clearParameters,
                    additional=self.parser.getOptions().additional,
                    functionObjectData=self.parser.getOptions(
                    ).functionObjectData)

                self.addToCaseLog(cName)
Exemplo n.º 2
0
    def recursiveCompress(self, dirName):
        if self.verbose > 1:
            print_("Recursively checking", dirName)
        if path.isdir(dirName):
            s = SolutionDirectory(dirName,
                                  archive=None,
                                  paraviewLink=False,
                                  parallel=True)
            if s.isValid():
                try:
                    self.compressCase(dirName)
                except OSError:
                    e = sys.exc_info()[
                        1]  # Needed because python 2.5 does not support 'as e'
                    self.warning("Problem processing", dirName, ":", e)
                return

        for f in listdir(dirName):
            name = path.join(dirName, f)
            try:
                if path.isdir(name):
                    self.recursiveCompress(name)
            except OSError:
                e = sys.exc_info()[
                    1]  # Needed because python 2.5 does not support 'as e'
                self.warning("Problem processing", name, ":", e)
Exemplo n.º 3
0
    def __init__(self,sol,correctors,tolerance,relTol,pRefValue=None,pRefCell=None):
        self.solution=ParsedParameterFile(path.join(sol.systemDir(),"fvSolution"),backup=True)
        self.schemes=ParsedParameterFile(path.join(sol.systemDir(),"fvSchemes"),backup=True)
        self.control=ParsedParameterFile(path.join(sol.systemDir(),"controlDict"),backup=True)
        self.controlOrig=ParsedParameterFile(path.join(sol.systemDir(),"controlDict"),backup=False)
        
        pre=environ["FOAM_TUTORIALS"]
        if not oldTutorialStructure():
            pre=path.join(pre,"basic")
        pot=SolutionDirectory(path.join(pre,"potentialFoam","cylinder"),archive=None,paraviewLink=False)
        
        self.fresh=True
        
        try:
            if "SIMPLE" not in self.solution:
                self.solution["SIMPLE"]=ParsedParameterFile(path.join(pot.systemDir(),"fvSolution"),backup=False)["SIMPLE"]

            if "nNonOrthogonalCorrectors" not in self.solution["SIMPLE"] and correctors==None:
                correctors=3
                warning("Setting number of correctors to default value",correctors)
            if correctors!=None:
                self.solution["SIMPLE"]["nNonOrthogonalCorrectors"]=correctors

            if pRefCell!=None:
                self.solution["SIMPLE"]["pRefCell"]=pRefCell
            if pRefValue!=None:
                self.solution["SIMPLE"]["pRefValue"]=pRefValue
                
            if tolerance!=None:
                try:
                    self.solution["solvers"]["p"][1]["tolerance"]=tolerance
                except KeyError:
                    # 1.6 format
                    self.solution["solvers"]["p"]["tolerance"]=tolerance
                    
            if relTol!=None:
                try:
                    self.solution["solvers"]["p"][1]["relTol"]=relTol
                except KeyError:
                    # 1.6 format
                    self.solution["solvers"]["p"]["relTol"]=relTol
                    
            self.schemes.content=ParsedParameterFile(path.join(pot.systemDir(),"fvSchemes"),backup=False).content
            self.control.content=ParsedParameterFile(path.join(pot.systemDir(),"controlDict"),backup=False).content
            if "functions" in self.controlOrig:
                print "Copying functions over"
                self.control["functions"]=self.controlOrig["functions"]
            if "libs" in self.controlOrig:
                print "Copying libs over"
                self.control["libs"]=self.controlOrig["libs"]
                
            self.solution.writeFile()
            self.schemes.writeFile()
            self.control.writeFile()
        except Exception,e:
            warning("Restoring defaults")
            self.solution.restore()
            self.schemes.restore()
            self.control.restore()
            raise e
Exemplo n.º 4
0
def case_setup(ci):
    template_case = SolutionDirectory("template",
                                      archive=None,
                                      paraviewLink=False)
    case = template_case.cloneCase("{0}{1}".format(ci.name, ci.nr_classes))

    phase_properties = ParsedParameterFile(
        path.join("./diffs", ci.phase_properties_name))
    phase_properties["air"]["PBEDiameterCoeffs"]["MOCCoeffs"][
        "numberOfClasses"] = ci.nr_classes
    phase_properties["air"]["PBEDiameterCoeffs"]["MOCCoeffs"]["xi1"] = ci.dv

    # manually fix bad pyfoam parsing
    phase_properties["blending"]["default"]["type"] = "none"
    phase_properties["drag"][1]["swarmCorrection"]["type"] = "none"
    phase_properties.writeFileAs(
        path.join(case.name, "constant", "phaseProperties"))

    v = ci.dv + ci.dv * arange(ci.nr_classes)
    n0 = ParsedParameterFile(path.join(template_case.name, "0", "n0"))
    for i in range(ci.nr_classes):
        n0.header["object"] = "n" + str(i)
        n0["internalField"].setUniform(ci.Ninit(v[i]))
        n0.writeFileAs(path.join(case.name, "0", "n" + str(i)))

    controlDict = ParsedParameterFile(
        path.join(case.name, "system", "controlDict"))
    controlDict["functions"]["probes"]["fields"] = [
        "n{0}".format(n) for n in range(ci.nr_classes)
    ]
    controlDict["endTime"] = ci.end_time
    controlDict["deltaT"] = ci.delta_t
    controlDict.writeFile()
Exemplo n.º 5
0
def case_setup(ci):
    template_case = SolutionDirectory(
        "template", archive=None, paraviewLink=False)
    case = template_case.cloneCase(
        "{0}NC{1}".format(ci.name, ci.nr_classes)
    )

    phase_properties = ParsedParameterFile(
        path.join(template_case.name, "constant", "phaseProperties"))
    phase_properties["oil"]["PBEDiameterCoeffs"]["MOCCoeffs"]["numberOfClasses"] = ci.nr_classes
    phase_properties["oil"]["PBEDiameterCoeffs"]["MOCCoeffs"]["xi1"] = ci.dv

    # manually fix bad pyfoam parsing
    phase_properties["blending"]["default"]["type"] = "none"
    phase_properties["drag"][1]["swarmCorrection"]["type"] = "none"
    phase_properties.writeFileAs(path.join(
        case.name, "constant", "phaseProperties"
    ))

    n0 = ParsedParameterFile(path.join(template_case.name, "0", "n0"))
    for i in range(ci.nr_classes):
        n0.header["object"] = "n" + str(i)
        n0["internalField"].setUniform(ci.Ninit[i])
        n0.writeFileAs(path.join(case.name, "0", "n" + str(i)))

    controlDict = ParsedParameterFile(
        path.join(case.name, "system", "controlDict")
    )
    controlDict["functions"]["probes"]["fields"] = [
        "n{0}".format(n) for n in range(ci.nr_classes)]
    controlDict.writeFile()
Exemplo n.º 6
0
    def run(self):
        sol = SolutionDirectory(self.parser.getArgs()[0])
        if not self.opts.init:
            vcs = sol.determineVCS()
            if vcs == None:
                self.error("not under version control")
            if not vcs in self.vcsChoices:
                self.error("Unsupported VCS", vcs)
        else:
            vcs = self.opts.vcs

        vcsInter = getVCS(vcs, path=sol.name, init=self.opts.init)

        vcsInter.addPath(path.join(sol.name, "constant"), rules=ruleList)
        vcsInter.addPath(path.join(sol.name, "system"), rules=ruleList)
        if sol.initialDir() != None:
            vcsInter.addPath(sol.initialDir(), rules=ruleList)
        else:
            self.warning("No initial-directory found")

        # special PyFoam-files
        for f in ["customRegexp", "LocalConfigPyFoam"]:
            p = path.join(sol.name, f)
            if path.exists(p):
                vcsInter.addPath(p, rules=ruleList)

        # Add the usual files from the tutorials
        for g in ["Allrun*", "Allclean*"]:
            for f in glob(path.join(sol.name, g)):
                vcsInter.addPath(f, rules=ruleList)

        for a in self.opts.additional:
            vcsInter.addPath(a, rules=ruleList)

        vcsInter.commit(self.opts.commitMessage)
Exemplo n.º 7
0
 def set_name(self, name):
     ''' handle the case renaming '''
     new_path = os.path.join(os.path.dirname(self._path), name)
     os.rename(self._path, new_path)
     self._case = SolutionDirectory(new_path)
     self._path = new_path
     self.name = name
Exemplo n.º 8
0
    def run(self):
        if not self.opts.keepPostprocessing:
            self.opts.additional.append("postProcessing")

        for cName in self.parser.getArgs():
            if self.checkCase(cName,fatal=self.opts.fatal,verbose=not self.opts.silent):
                self.addLocalConfig(cName)

                if self.opts.verbose:
                    print_("Clearing",cName)

                sol=SolutionDirectory(cName,archive=None,paraviewLink=False)
                sol.clear(after=self.parser.getOptions().after,
                          processor=self.parser.getOptions().processor,
                          pyfoam=self.parser.getOptions().pyfoam,
                          vtk=self.parser.getOptions().vtk,
                          removeAnalyzed=self.parser.getOptions().removeAnalyzed,
                          keepRegular=self.parser.getOptions().keepRegular,
                          keepParallel=self.parser.getOptions().keepParallel,
                          keepLast=self.parser.getOptions().latest,
                          keepInterval=self.parser.getOptions().keepInterval,
                          clearHistory=self.parser.getOptions().clearHistory,
                          clearParameters=self.parser.getOptions().clearParameters,
                          additional=self.parser.getOptions().additional,
                          functionObjectData=self.parser.getOptions().functionObjectData)

                self.addToCaseLog(cName)
Exemplo n.º 9
0
 def run(self):
     cName = self.parser.getArgs()[0]
     if self.opts.cloneCase:
         if self.opts.autoCasename:
             cName = path.join(
                 cName,
                 path.basename(self.opts.cloneCase) +
                 buildFilenameExtension(self.opts.valuesDicts,
                                        self.opts.values))
         if path.exists(cName):
             self.error(
                 cName,
                 "already existing (case should not exist when used with --clone-case)"
             )
         if self.checkCase(self.opts.cloneCase,
                           fatal=self.opts.fatal,
                           verbose=not self.opts.noComplain):
             self.addLocalConfig(self.opts.cloneCase)
         orig = SolutionDirectory(self.opts.cloneCase,
                                  archive=None,
                                  paraviewLink=False)
         sol = orig.cloneCase(cName)
     else:
         if self.checkCase(cName,
                           fatal=self.opts.fatal,
                           verbose=not self.opts.noComplain):
             self.addLocalConfig(cName)
         sol = SolutionDirectory(cName, archive=None, paraviewLink=False)
     try:
         self.__lastMessage = None
         self.prepare(sol, cName=cName)
     except:
         if self.__lastMessage:
             self.__writeToStateFile(sol, self.__lastMessage + " failed")
         raise
Exemplo n.º 10
0
def case_setup(ci):
    template_case = SolutionDirectory(
        "template", archive=None, paraviewLink=False)
    case = template_case.cloneCase(
        "{0}{1}".format(ci.name, ci.quadrature_order)
    )

    phase_properties = ParsedParameterFile(
        path.join("./diffs", ci.phase_properties_name))
    phase_properties["air"]["PBEDiameterCoeffs"]["QMOMCoeffs"]["quadratureOrder"] = ci.quadrature_order

    # manually fix bad pyfoam parsing
    phase_properties["blending"]["default"]["type"] = "none"
    phase_properties["drag"][1]["swarmCorrection"]["type"] = "none"
    phase_properties.writeFileAs(path.join(
        case.name, "constant", "phaseProperties"
    ))

    m0 = ParsedParameterFile(path.join(template_case.name, "0", "m0"))
    for i in range(ci.number_of_moments):
        m0.header["object"] = "m" + str(i)
        m0["internalField"].setUniform(ci.initial_moments[i])
        m0["dimensions"] = "[0 {0} 0 0 0 0 0]".format(3 * i)
        m0.writeFileAs(path.join(case.name, "0", "m" + str(i)))

    controlDict = ParsedParameterFile(
        path.join(case.name, "system", "controlDict")
    )
    controlDict["functions"]["probes"]["fields"] = [
        "m{0}".format(m) for m in range(ci.number_of_moments)]
    controlDict["endTime"] = ci.end_time
    controlDict["deltaT"] = ci.delta_t
    controlDict.writeFile()
Exemplo n.º 11
0
    def run(self):
        cName = self.parser.casePath()
        self.checkCase(cName)

        self.processPlotLineOptions(autoPath=cName)

        sol = SolutionDirectory(cName, archive=None)

        lam = self.getParallel(sol)

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

        self.setLogname()

        self.checkAndCommit(sol)

        run = ConvergenceRunner(BoundingLogAnalyzer(
            progress=self.opts.progress,
            doFiles=self.opts.writeFiles,
            singleFile=self.opts.singleDataFilesOnly,
            doTimelines=True),
                                silent=self.opts.progress or self.opts.silent,
                                argv=self.parser.getArgs(),
                                restart=self.opts.restart,
                                server=self.opts.server,
                                logname=self.opts.logname,
                                compressLog=self.opts.compress,
                                lam=lam,
                                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_,
                        writeFiles=self.opts.writeFiles)

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

        self.addSafeTrigger(run, sol)
        self.addWriteAllTrigger(run, sol)

        self.addToCaseLog(cName, "Starting")

        run.start()

        self.addToCaseLog(cName, "Ending")

        self.setData(run.data)

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

        return run.data
Exemplo n.º 12
0
    def reset(self):        
        pid = self.worker_index #os.getpid()
#        logger_g.info(f'{pid}')
        
        self.casename = 'baseCase_'+str(pid)
        self.csvfile = 'progress_'+str(pid)+'.csv'  
        orig = SolutionDirectory(origcase,archive=None,paraviewLink=False)
        case = orig.cloneCase(self.casename )
        
        control_dict = ParsedParameterFile(path.join(self.casename,"system", "controlDict"))
        control_dict["endTime"] = self.end_time
        control_dict["writeInterval"] = self.write_interval
        control_dict.writeFile()
            
        # remove old log files
        with open(f'{self.casename}/logr.remove', 'a') as fp:
            subprocess.run(
                f'rm {self.casename}/log.*',
                shell=True,
                stdout=fp,
                stderr=subprocess.STDOUT
            )
            
        # remove old solution directories
        with open(f'{self.casename}/logr.remove', 'a') as fp:
            subprocess.run(
                f'rm -r {self.casename}/0.* {self.casename}/[1-9]*',
                shell=True,
                stdout=fp,
                stderr=subprocess.STDOUT
            )
        
        # remove old solution directories
        with open(f'{self.casename}/logr.remove', 'a') as fp:
            subprocess.run(
                f'rm -r {self.casename}/VTK',
                shell=True,
                stdout=fp,
                stderr=subprocess.STDOUT
            )
        
        # remove old solution directories
#        subprocess.run(
#            f'rm -r postProcessing',
#            shell=True,
#            stderr=subprocess.STDOUT
#        )
        
        self.count_parameters = 0
        if self.states_type == 1:
            #self.actions[self.count_parameters] = self.a1[0]
            self.state = np.array([self.a1[0]]) 
        elif self.states_type == 2:
            self.state = np.hstack((self.a1,self.reward))
                    
        return np.array(self.state)
    def run(self):
        files=self.parser.getArgs()[0:]
        if len(files)==1 and path.isdir(files[0]):
            sol=SolutionDirectory(
                self.parser.getArgs()[0],
                archive=None,
                parallel=self.opts.parallelTimes,
                paraviewLink=False)
            self.processTimestepOptions(sol)
            if len(self.opts.time)<1:
                self.error("No time specified")
            globStr=self.parser.getArgs()[0]
            if self.opts.parallelTimes:
                globStr=path.join(globStr,"processor*")
            usedTime=sol.timeName(self.opts.time[0])
            globStr=path.join(globStr,
                              usedTime,
                              "uniform","profiling*")

            files=glob(globStr)
            print_("Profiling info from time",usedTime)
        if len(files)<1:
            self.error("No profiling data found")
        elif len(files)>1:
            lst=[]
            for f in files:
                lst.append(self.readProfilingInfo(f))
            dataAll,children0,root0=lst[0]
            for i in dataAll:
                d=dataAll[i]
                d["totalTimeMin"]=d["totalTime"]
                d["totalTimeMax"]=d["totalTime"]
                d["callsMin"]=d["calls"]
                d["callsMax"]=d["calls"]
            for data,children,root in lst[1:]:
                if root0!=root or children!=children0 or data.keys()!=dataAll.keys():
                    self.error("Inconsistent profiling data. Probably not from same run/timestep")
                for i in data:
                    d=data[i]
                    s=dataAll[i]
                    s["totalTime"]+=d["totalTime"]
                    s["totalTimeMin"]=min(s["totalTimeMin"],d["totalTime"])
                    s["totalTimeMax"]=max(s["totalTimeMax"],d["totalTime"])
                    s["calls"]+=d["calls"]
                    s["callsMin"]=min(s["callsMin"],d["calls"])
                    s["callsMax"]=max(s["callsMax"],d["calls"])
                    s["childTime"]+=d["childTime"]
            for i in dataAll:
                d=dataAll[i]
                d["totalTime"]=d["totalTime"]/len(lst)
                d["childTime"]=d["childTime"]/len(lst)
                d["calls"]=d["calls"]/len(lst)
            self.printProfilingInfo(dataAll,children,root,True)
        else:
            data,children,root=self.readProfilingInfo(files[0])
            self.printProfilingInfo(data,children,root)
    def run(self):
        files = self.parser.getArgs()[0:]
        if len(files) == 1 and path.isdir(files[0]):
            sol = SolutionDirectory(self.parser.getArgs()[0],
                                    archive=None,
                                    parallel=self.opts.parallelTimes,
                                    paraviewLink=False)
            self.processTimestepOptions(sol)
            if len(self.opts.time) < 1:
                self.error("No time specified")
            globStr = self.parser.getArgs()[0]
            if self.opts.parallelTimes:
                globStr = path.join(globStr, "processor*")
            usedTime = sol.timeName(self.opts.time[0])
            globStr = path.join(globStr, usedTime, "uniform", "profiling*")

            files = glob(globStr)
            print_("Profiling info from time", usedTime)
        if len(files) < 1:
            self.error("No profiling data found")
        elif len(files) > 1:
            lst = []
            for f in files:
                lst.append(self.readProfilingInfo(f))
            dataAll, children0, root0 = lst[0]
            for i in dataAll:
                d = dataAll[i]
                d["totalTimeMin"] = d["totalTime"]
                d["totalTimeMax"] = d["totalTime"]
                d["callsMin"] = d["calls"]
                d["callsMax"] = d["calls"]
            for data, children, root in lst[1:]:
                if root0 != root or children != children0 or data.keys(
                ) != dataAll.keys():
                    self.error(
                        "Inconsistent profiling data. Probably not from same run/timestep"
                    )
                for i in data:
                    d = data[i]
                    s = dataAll[i]
                    s["totalTime"] += d["totalTime"]
                    s["totalTimeMin"] = min(s["totalTimeMin"], d["totalTime"])
                    s["totalTimeMax"] = max(s["totalTimeMax"], d["totalTime"])
                    s["calls"] += d["calls"]
                    s["callsMin"] = min(s["callsMin"], d["calls"])
                    s["callsMax"] = max(s["callsMax"], d["calls"])
                    s["childTime"] += d["childTime"]
            for i in dataAll:
                d = dataAll[i]
                d["totalTime"] = d["totalTime"] / len(lst)
                d["childTime"] = d["childTime"] / len(lst)
                d["calls"] = d["calls"] / len(lst)
            self.printProfilingInfo(dataAll, children, root, True)
        else:
            data, children, root = self.readProfilingInfo(files[0])
            self.printProfilingInfo(data, children, root)
 def additionalReconstruct(self,parameters):
     sol=SolutionDirectory(self.casename())
     if len(sol.processorDirs())>0:
         for t in listdir(path.join(self.casename(),sol.processorDirs()[0])):
             try:
                 tm=float(t)
                 self.foamRun("reconstructParMesh",foamArgs=["-time",t])
                 self.foamRun("reconstructPar",foamArgs=["-time",t])
             except ValueError:
                 print "Skipping",t
    def test_with_broyden(self):
        """
        broyden test
        """
        if not foamVersionNumber() in [(2,3),(2,2)]:
            raise unittest.SkipTest("need ver.2.3 or 2.2 for this unittest.")
        
        cavityTut = os.path.join(foamTutorials(),
                                "incompressible/icoFoam/cavity")

        if not os.path.exists(cavityTut):
            raise unittest.SkipTest("need $FOAM_TUTORIALS/incompressible/cavity \
                               for unittest.")

        try:
            shutil.copytree(cavityTut, os.path.join(self.tmpDir,"cavity"))
            cavityCase = SolutionDirectory(os.path.join(self.tmpDir,"cavity"))
        except:
            raise unittest.SkipTest("can not copy cavity case to temp_dir.")   
                 
        #create Allrun 
        with open(os.path.join(cavityCase.name,"Allrun"),'w') as fp:
            fp.write('#!/bin/sh\nblockMesh>log.blockMesh\nicoFoam>log.icoFoam\n')
        os.chmod(os.path.join(cavityCase.name,"Allrun"),0777)
         
        #append controlDict
        fObj="""
functions
{
    probes
    {
        type            probes;
        functionObjectLibs ("libsampling.so");
        enabled         true;
        outputControl   timeStep;
        outputInterval  1;
        fields
        (
            p
        );
        probeLocations
        (
            ( 0.1 0.0925 0.005 )
        );
    }
}
"""
        with open(cavityCase.controlDict(),'a') as fp:
            fp.write(fObj)
        
        #test start
        sim = set_as_top(BroydenCavityInstance())
        sim.cavity.case_dir = cavityCase.name
        sim.run()
        self.assertEqual(round(sim.cavity.nu,4),0.01)
Exemplo n.º 17
0
 def additionalReconstruct(self, parameters):
     sol = SolutionDirectory(self.casename())
     if len(sol.processorDirs()) > 0:
         for t in listdir(path.join(self.casename(),
                                    sol.processorDirs()[0])):
             try:
                 tm = float(t)
                 self.foamRun("reconstructParMesh", foamArgs=["-time", t])
                 self.foamRun("reconstructPar", foamArgs=["-time", t])
             except ValueError:
                 print "Skipping", t
Exemplo n.º 18
0
 def __init__(self, input):
     """:param input: either a SolutionDirectory-instance or a string
     with a pathname"""
     if isinstance(input, SolutionDirectory):
         self.__sol = input
     elif isinstance(input, string_types):
         self.__sol = SolutionDirectory(input,
                                        paraviewLink=False,
                                        archive=None)
     else:
         error(type(input), "not supported")
Exemplo n.º 19
0
def case_setup(ci):
    template_case = SolutionDirectory(
        "template", archive=None, paraviewLink=False)
    case = template_case.cloneCase("{0}".format(ci.name))

    stfproperties = ParsedParameterFile(path.join(
        case.name, "constant", "STFProperties"))

    stfproperties["apparentMass"] = ci.ma
    stfproperties["mass"] = ci.m
    stfproperties.writeFile()
Exemplo n.º 20
0
    def run(self):
        if not self.opts.keepPostprocessing:
            self.opts.additional.append("postProcessing")

        notCleared=[]

        if self.opts.dryRun:
            self.opts.verbose=True
            self.opts.verboseClear=True

        for cName in self.parser.getArgs():
            if self.checkCase(cName,fatal=self.opts.fatal,verbose=not self.opts.silent):
                if self.opts.allcleanScript:
                    scr=path.join(cName,"Allclean")
                    if(path.exists(scr)):
                        if self.opts.verbose or self.opts.verboseClear:
                            print("Executing",scr)
                    execute(scr,workdir=cName)
                try:
                    self.addLocalConfig(cName)

                    if self.opts.verbose:
                        print_("Clearing",cName)
                    sol=SolutionDirectory(cName,
                                          archive=None,
                                          parallel=True,
                                          paraviewLink=False)
                    sol.clear(after=self.parser.getOptions().after,
                              processor=self.parser.getOptions().processor,
                              pyfoam=self.parser.getOptions().pyfoam,
                              vtk=self.parser.getOptions().vtk,
                              verbose=self.parser.getOptions().verboseClear,
                              removeAnalyzed=self.parser.getOptions().removeAnalyzed,
                              keepRegular=self.parser.getOptions().keepRegular,
                              keepParallel=self.parser.getOptions().keepParallel,
                              keepLast=self.parser.getOptions().latest,
                              keepInterval=self.parser.getOptions().keepInterval,
                              keepTimes=self.opts.keepTimes,
                              clearHistory=self.parser.getOptions().clearHistory,
                              clearParameters=self.parser.getOptions().clearParameters,
                              additional=self.parser.getOptions().additional,
                              dryRun=self.opts.dryRun,
                              functionObjectData=self.parser.getOptions().functionObjectData)

                    self.addToCaseLog(cName)
                except OSError:
                    e = sys.exc_info()[1] # compatible with 2.x and 3.x
                    self.warning("Can not clear",cName,"because of OSError",e)
                    notCleared.append(cName)

        if len(notCleared)>0:
            self.warning("These case not cleared because of OS-problems:",
                         ", ".join(notCleared))
Exemplo n.º 21
0
    def run(self):
        if self.opts.file:
            sys.stdout = open(self.opts.file, "w")

        if self.opts.allRegions:
            sol = SolutionDirectory(
                self.parser.getArgs()[0], archive=None, parallel=self.opts.parallel, paraviewLink=False
            )
            for r in sol.getRegions():
                self.doRegion(r)
        else:
            self.doRegion(self.opts.region)
Exemplo n.º 22
0
    def __init__(self, folder):
        self._folder = folder
        self._case = SolutionDirectory(folder)
        self._path = self._case.name
        self.name = os.path.basename(self._path)

        self.__dict__.update({
            'set_snappyHexMesh':
            partial(self.set_dictSettings, 'snappyHexMeshDict'),
            'set_fvSchemes':
            partial(self.set_dictSettings, 'fvSchemes'),
            'set_fvSolution':
            partial(self.set_dictSettings, 'fvSolution'),
        })
Exemplo n.º 23
0
    def run(self):
        sName = self.parser.getArgs()[0]
        if sName[-1] == path.sep:
            sName = sName[:-1]

        if self.parser.getOptions().tarname != None:
            dName = self.parser.getOptions().tarname
        else:
            if sName == path.curdir:
                dName = path.basename(path.abspath(sName))
            else:
                dName = sName
            dName += ".tgz"
        if self.parser.getOptions().pyfoam:
            self.parser.getOptions().additional.append("PyFoam*")

        sol = SolutionDirectory(sName,
                                archive=None,
                                addLocalConfig=True,
                                paraviewLink=False)
        if not sol.isValid():
            self.error(sName, "does not look like real OpenFOAM-case because",
                       sol.missingFiles(), "are missing or of the wrong type")

        if self.parser.getOptions().chemkin:
            sol.addToClone("chemkin")

        if self.opts.noPloyMesh:
            self.parser.getOptions().exclude.append("polyMesh")

        sol.packCase(dName,
                     last=self.parser.getOptions().last,
                     additional=self.parser.getOptions().additional,
                     exclude=self.parser.getOptions().exclude,
                     base=self.parser.getOptions().basename)
    def timeChanged(self):
        self.nSteps += 1
        self.currTime = self.analyzer.time
        self.progressString = self.analyzer.progressOut.lastProgress()

        if self.analyzer.hasAnalyzer("Execution"):
            self.clockTime = self.analyzer.getAnalyzer(
                "Execution").clockTotal()

        if self.startTime is None:
            if self.runner:
                self.startTime = self.runner.createTime
            else:
                self.startTime = self.analyzer.getAnalyzer("Time").createTime()

        if self.endTime is None:
            sol = None
            if self.runner:
                sol = self.runner.getSolutionDirectory()
            else:
                if self.analyzer.hasAnalyzer("ExecName"):
                    caseName = self.analyzer.getAnalyzer("ExecName").caseName
                    if caseName and path.isdir(caseName):
                        from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
                        sol = SolutionDirectory(caseName, paraviewLink=False)
            if sol:
                from PyFoam.RunDictionary.ParameterFile import ParameterFile
                control = ParameterFile(sol.controlDict())
                try:
                    self.endTime = float(control.readParameter("endTime"))
                except ValueError:
                    self.endTime = -1

        if self.caseName is None or self.execName is None:
            if self.analyzer.hasAnalyzer("ExecName"):
                self.caseName = self.analyzer.getAnalyzer("ExecName").caseName
                self.execName = self.analyzer.getAnalyzer("ExecName").execName
                self.headerChanged = True

        from PyFoam.LogAnalysis.LogLineAnalyzer import LogLineAnalyzer
        for e in LogLineAnalyzer.allRegexp:
            addExpr(e)

        if self.firstTime:
            self.update(resize=True)
            self.firstTime = False
        else:
            self._checkHeaders(force=True)
Exemplo n.º 25
0
    def create_case(self, wind_dict, params):
        """
        0. cloning case
        1. creating snappyHexMeshDict and blockMeshdict according to flow direction and other parameters
        2. creating the blockMesh
        3. change the boundary conditions
        4. decomposing the domain
        5. creating the snappyHexMesh - running in parallel (sfoam.py or not - depending on user input)
        6. decomposing the created mesh
        """
        #--------------------------------------------------------------------------------------
        # cloning case
        #--------------------------------------------------------------------------------------
        target = params['case_dir']
        target = os.path.realpath(target)
        if not os.path.exists(target):
            makedirs(target)
        template = read_dict_string(wind_dict, 'template')
        self._r.debug("template = %r, target = %r" % (template, target))
        orig = SolutionDirectory(template, archive=None, paraviewLink=False)
        work = orig.cloneCase(target)

        #--
        # creating dictionaries
        #--
        if wind_dict['procnr'] > multiprocessing.cpu_count():
            self._r.warn(
                'wind_dict contains a higher processor number then the machine has'
            )
            wind_dict['procnr'] = min(wind_dict['procnr'],
                                      multiprocessing.cpu_count())
        phi = params['wind_dir'] * pi / 180
        params['phi'] = phi  # - pi/180 * 90
        self._r.status('creating block mesh dictionary')
        self.create_block_mesh_dict(work, wind_dict, params)
        self._r.status('creating snappy hex mesh dictionary')
        self.create_SHM_dict(work, wind_dict, params)
        self._r.status('creating boundary conditions dictionary')
        self.create_boundary_conditions_dict(work, wind_dict, params)
        self._r.status('running block mesh')
        self.run_block_mesh(work)
        self._r.status('running decompose')
        self.run_decompose(work, wind_dict)
        self._r.status('running snappy hex mesh')
        self.run_SHM(work, wind_dict)
        self._r.status('running second decompose')
        self.run_decompose(work, wind_dict)
        return work
Exemplo n.º 26
0
 def solution(self):
     """Access to a SolutionDirectory-object that represents the
     current solution"""
     if not hasattr(self,"_solution"):
         self._solution=SolutionDirectory(self.caseDir,
                                           archive=None)
     return self._solution
    def timeChanged(self):
        self.nSteps+=1
        self.currTime=self.analyzer.time
        self.progressString=self.analyzer.progressOut.lastProgress()

        if self.analyzer.hasAnalyzer("Execution"):
            self.clockTime=self.analyzer.getAnalyzer("Execution").clockTotal()

        if self.startTime is None:
            if self.runner:
                self.startTime=self.runner.createTime
            else:
                self.startTime=self.analyzer.getAnalyzer("Time").createTime()

        if self.endTime is None:
            sol=None
            if self.runner:
                sol=self.runner.getSolutionDirectory()
            else:
                if self.analyzer.hasAnalyzer("ExecName"):
                    caseName=self.analyzer.getAnalyzer("ExecName").caseName
                    if caseName and path.isdir(caseName):
                        from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
                        sol=SolutionDirectory(caseName,paraviewLink=False)
            if sol:
                from PyFoam.RunDictionary.ParameterFile import ParameterFile
                control=ParameterFile(sol.controlDict())
                try:
                    self.endTime=float(control.readParameter("endTime"))
                except ValueError:
                    self.endTime=-1

        if self.caseName is None or self.execName is None:
            if self.analyzer.hasAnalyzer("ExecName"):
                self.caseName=self.analyzer.getAnalyzer("ExecName").caseName
                self.execName=self.analyzer.getAnalyzer("ExecName").execName
                self.headerChanged=True

        from PyFoam.LogAnalysis.LogLineAnalyzer import LogLineAnalyzer
        for e in LogLineAnalyzer.allRegexp:
            addExpr(e)

        if self.firstTime:
            self.update(resize=True)
            self.firstTime=False
        else:
            self._checkHeaders(force=True)
Exemplo n.º 28
0
    def create_case(self, wind_dict, params):
        """
        0. cloning case
        1. creating snappyHexMeshDict and blockMeshdict according to flow direction and other parameters
        2. creating the blockMesh
        3. change the boundary conditions
        4. decomposing the domain
        5. creating the snappyHexMesh - running in parallel (sfoam.py or not - depending on user input)
        6. decomposing the created mesh
        """
        #--------------------------------------------------------------------------------------
        # cloning case
        #--------------------------------------------------------------------------------------
        target = params['case_dir']
        target = os.path.realpath(target)
        if not os.path.exists(target):
            makedirs(target)
        template = read_dict_string(wind_dict, 'template')
        self._r.debug("template = %r, target = %r" % (template, target))
        orig = SolutionDirectory(template,
                                archive=None,
                                paraviewLink=False)
        work = orig.cloneCase(target)

        #--
        # creating dictionaries
        #--
        if wind_dict['procnr'] > multiprocessing.cpu_count():
            self._r.warn('wind_dict contains a higher processor number then the machine has')
            wind_dict['procnr'] = min(wind_dict['procnr'], multiprocessing.cpu_count())
        phi = params['wind_dir'] * pi / 180
        params['phi'] = phi # - pi/180 * 90
        self._r.status('creating block mesh dictionary')
        self.create_block_mesh_dict(work, wind_dict, params)
        self._r.status('creating snappy hex mesh dictionary')
        self.create_SHM_dict(work, wind_dict, params)
        self._r.status('creating boundary conditions dictionary')
        self.create_boundary_conditions_dict(work, wind_dict, params)
        self._r.status('running block mesh')
        self.run_block_mesh(work)
        self._r.status('running decompose')
        self.run_decompose(work, wind_dict)
        self._r.status('running snappy hex mesh')
        self.run_SHM(work, wind_dict)
        self._r.status('running second decompose')
        self.run_decompose(work, wind_dict)
        return work
Exemplo n.º 29
0
 def run(self):
     cName = self.parser.getArgs()[0]
     if self.checkCase(cName,
                       fatal=self.opts.fatal,
                       verbose=not self.opts.noComplain):
         self.addLocalConfig(cName)
     sol = SolutionDirectory(cName, archive=None, paraviewLink=False)
     self.prepare(sol, cName=cName)
 def testSolutionDirectoryBasicContainerStuff(self):
     test=SolutionDirectory(self.theFile)
     self.assertEqual(len(test),1)
     self.assert_("0" in test)
     self.assert_("1e-7" in test)
     self.assert_("1e-4" not in test)
     self.assert_(0. in test)
     td=test["0"]
     self.assertEqual(type(td),TimeDirectory)
     self.assertRaises(KeyError,test.__getitem__,"42")
     td=test[-1]
     self.assertEqual(type(td),TimeDirectory)
     lst=[]
     for t in test:
         lst.append(t.baseName())
     self.assertEqual(len(test),len(lst))
     self.assertEqual(lst,test.getTimes())
Exemplo n.º 31
0
 def testRoundtripBlockMesh(self):
     blk = ParsedBlockMeshDict(SolutionDirectory(self.dest).blockMesh())
     txt = str(blk)
     try:
         i = int(txt.split("blocks")[1].split("(")[0])
         self.assertTrue(False)
     except ValueError:
         pass
Exemplo n.º 32
0
 def cloneCase(self, name, svnRemove=True, followSymlinks=False):
     out = SolutionDirectory.cloneCase(self,
                                       name,
                                       svnRemove=svnRemove,
                                       followSymlinks=followSymlinks)
     out.inletPatch = self.inletPatch
     out.updateInletVelocity()
     return out
Exemplo n.º 33
0
 def testSolutionDirectoryBasicContainerStuff(self):
     test=SolutionDirectory(self.theFile)
     self.assertEqual(len(test),1)
     self.assert_("0" in test)
     self.assert_("1e-7" in test)
     self.assert_("1e-4" not in test)
     self.assert_(0. in test)
     td=test["0"]
     self.assertEqual(type(td),TimeDirectory)
     self.assertRaises(KeyError,test.__getitem__,"42")
     td=test[-1]
     self.assertEqual(type(td),TimeDirectory)
     lst=[]
     for t in test:
         lst.append(t.baseName())
     self.assertEqual(len(test),len(lst))
     self.assertEqual(lst,test.getTimes())
Exemplo n.º 34
0
    def run(self):
        sName=self.parser.getArgs()[0]
        if sName[-1]==path.sep:
            sName=sName[:-1]
            
        if self.parser.getOptions().tarname!=None:
            dName=self.parser.getOptions().tarname
        else:
            dName=sName+".tgz"
        if self.parser.getOptions().pyfoam:
            self.parser.getOptions().additional.append("PyFoam*")
            
        sol=SolutionDirectory(sName,archive=None,paraviewLink=False)
        if not sol.isValid():
            self.error(sName,"does not look like real OpenFOAM-case because",sol.missingFiles(),"are missing or of the wrong type")
            
        if self.parser.getOptions().chemkin:
            sol.addToClone("chemkin")

        if self.opts.noPloyMesh:
            self.parser.getOptions().exclude.append("polyMesh")
            
        sol.packCase(dName,
                     last=self.parser.getOptions().last,
                     additional=self.parser.getOptions().additional,
                     exclude=self.parser.getOptions().exclude,
                     base=self.parser.getOptions().basename)
Exemplo n.º 35
0
    def run(self):
        cName = self.parser.getArgs()[0]
        sol = SolutionDirectory(cName, archive=None)
        self.addLocalConfig(cName)
        initial = sol[0]
        if "U" not in initial or "p" not in initial:
            error("Either 'p' or 'U' missing from the initial directory",
                  initial.baseName())
        if self.opts.writep:
            initial["p.prepotential"] = initial["p"]
        initial["U.prepotential"] = initial["U"]

        lam = self.getParallel(sol)

        if self.opts.writep:
            writep = ["-writep"]
        else:
            writep = []

        argv = ["potentialFoam"]
        if oldApp():
            argv += [".", cName]
        else:
            argv += ["-case", cName]

        self.setLogname(default="Potential", useApplication=False)

        self.checkAndCommit(sol)

        run = BasicRunner(argv=argv + writep,
                          server=self.opts.server,
                          logname=self.opts.logname,
                          compressLog=self.opts.compress,
                          silent=self.opts.progress or self.opts.silent,
                          lam=lam,
                          logTail=self.opts.logTail,
                          echoCommandLine=self.opts.echoCommandPrefix,
                          noLog=self.opts.noLog)

        print_("Setting system-directory for potentialFoam")
        trig = PotentialTrigger(sol,
                                self.opts.noCorr,
                                self.opts.tolerance,
                                self.opts.relTol,
                                pRefCell=self.opts.pRefCell,
                                pRefValue=self.opts.pRefValue,
                                removeLibs=self.opts.removeLibs,
                                removeFunctions=self.opts.removeFunctions)
        run.addEndTrigger(trig.resetIt)

        self.addToCaseLog(cName, "Starting")

        run.start()

        self.setData(run.data)

        self.addToCaseLog(cName, "Ending")
Exemplo n.º 36
0
def caseDirectory():
    """The directory in which the case is stored"""
    try:
        # old school paraview
        fName = path.dirname(paraFoamReader().FileName.GetData())
    except AttributeError:
        fName = path.dirname(paraFoamReader().FileName)

    return SolutionDirectory(fName, archive=None, paraviewLink=False)
Exemplo n.º 37
0
    def run(self):
        if len(self.parser.getArgs()) != 2:
            error("Need two arguments.", len(self.parser.getArgs()), "found")

        sName = self.parser.getArgs()[0]
        dName = self.parser.getArgs()[1]

        if self.opts.include == None:
            include = ["*"]
        else:
            include = self.opts.include

        if self.opts.exclude == None:
            exclude = []
        else:
            exclude = self.opts.exclude

        source = SolutionDirectory(sName, archive=None, paraviewLink=False)
        dest = SolutionDirectory(dName, archive=None, paraviewLink=False)

        sDir = source[-1]
        dDir = dest[0]

        if self.opts.verbose:
            print_("   Copying from source-time", sDir.baseName(),
                   "to destination-time", dDir.baseName())

        copied = dDir.copy(sDir,
                           include=include,
                           exclude=exclude,
                           overwrite=self.opts.overwrite,
                           mustExist=self.opts.mustExist,
                           purge=self.opts.purge)

        if self.opts.verbose:
            if len(copied) > 0:
                print_("  Copied the fields", end=" ")
                for v in copied:
                    print_(v, end=" ")
                print_()
            else:
                print_("  Nothing copied")

        self.addToCaseLog(dest.name, "From", sDir.name, "to", dDir.name)
Exemplo n.º 38
0
    def compressCase(self, dirName, warn=False):
        if not path.exists(dirName):
            self.error("Directory", dirName, "does not exist")
        s = SolutionDirectory(dirName,
                              archive=None,
                              paraviewLink=False,
                              parallel=True,
                              tolerant=True)
        if not s.isValid():
            if warn:
                print_("Directory", dirName, "is not an OpenFOAM-case")
            return

        self.nrDir += 1
        oldNr = self.nrFiles
        oldUnc = self.prevSize
        oldCon = self.nowSize

        if self.verbose > 0:
            print_("Processing case", dirName)

        # compress meshes
        for d in glob(path.join(dirName, "*", "polyMesh")) + glob(
                path.join(dirName, "*", "*", "polyMesh")):
            if path.isdir(d):
                self.compressDirectory(d)

        # compress times
        for t in s:
            self.compressDirectory(t.name)

        # compress logfiles if requested
        if self.opts.logfile:
            for f in glob(path.join(dirName, "*.logfile")):
                self.compressFile(path.join(dirName, f))

        # processor direcories
        for p in s.procDirs:
            self.compressDirectory(path.join(dirName, p))
        if self.nrFiles > oldNr and self.verbose > 0:
            print_(
                "  -> ", self.nrFiles - oldNr, "files compressed.",
                humanReadableSize((self.prevSize - oldUnc) -
                                  (self.nowSize - oldCon)), "gained")
Exemplo n.º 39
0
    def create(self, caseName, input_parameters):
        direTemplate = SolutionDirectory(self.template)
        for f in self.clone_files:
            direTemplate.addToClone(f)

        dire = direTemplate.cloneCase(caseName)

        parameters = ParsedParameterFile(os.path.join(dire.name, "parameters"))
        for key, value in input_parameters.items():
            parameters[key] = value
        parameters.writeFile()

        if self.np > 1:
            decomposeParDict = ParsedParameterFile(
                os.path.join(dire.systemDir(), "decomposeParDict"))
            decomposeParDict["numberOfSubdomains"] = self.np
            decomposeParDict.writeFile()

        self.dire = dire
Exemplo n.º 40
0
 def testBoundaryRead(self):
     blk = ParsedBlockMeshDict(SolutionDirectory(self.dest).blockMesh())
     self.assertEqual(blk.convertToMeters(), 1.)
     self.assertEqual(len(blk.vertices()), 22)
     self.assertEqual(len(blk.blocks()), 5)
     self.assertEqual(len(blk.patches()), 6)
     self.assertEqual(len(blk.arcs()), 8)
     self.assertEqual(blk.typicalLength(), 1.25)
     self.assertEqual(str(blk.getBounds()),
                      "([0.0, 0.0, 0.0], [2.0, 2.0, 0.5])")
Exemplo n.º 41
0
 def testTimeReplacingZippedFile(self):
     test = SolutionDirectory(self.theFile)["0"]
     self.assertEqual(len(test), 4)
     if foamFork() in ["openfoam", "openfoamplus"
                       ] and foamVersionNumber() >= (4, ):
         extension = ".orig"
     else:
         extension = ".org"
     test[gammaName()] = test[gammaName() + extension]
     self.assertEqual(len(test), 4)
 def testTimeCopy(self):
     test=SolutionDirectory(self.theFile)
     self.assertEqual(len(test),1)
     test["42"]=test[0]
     self.assertEqual(len(test),2)
     self.assertEqual(len(test["42"]),len(test[0]))
     del test["42"]
     self.assertEqual(len(test),1)
     del test[-1]
     self.assertEqual(len(test),0)
Exemplo n.º 43
0
    def run(self):
        for cName in self.parser.getArgs():
            if self.checkCase(cName,fatal=self.opts.fatal,verbose=not self.opts.silent):
                self.addLocalConfig(cName)

                if self.opts.verbose:
                    print "Clearing",cName

                sol=SolutionDirectory(cName,archive=None,paraviewLink=False)
                sol.clear(after=self.parser.getOptions().after,
                          processor=self.parser.getOptions().processor,
                          pyfoam=self.parser.getOptions().pyfoam,
                          vtk=self.parser.getOptions().vtk,
                          keepRegular=self.parser.getOptions().keepRegular,
                          keepLast=self.parser.getOptions().latest,
                          clearHistory=self.parser.getOptions().clearHistory,
                          functionObjectData=self.parser.getOptions().functionObjectData)
        
                self.addToCaseLog(cName)
Exemplo n.º 44
0
 def __init__(self,
              case,
              time="constant",
              processor=None,
              region=None):
     """@param case: Path to the case-directory
     @param time: Time for which the  mesh should be looked at
     @param processor: Name of the processor directory for decomposed cases"""
     self.sol=SolutionDirectory(case,paraviewLink=False,archive=None,region=region)
     self.time=time
     self.processor=processor
 def __init__(self,input):
     """@param input: either a SolutionDirectory-instance or a string
     with a pathname"""
     if isinstance(input,SolutionDirectory):
         self.__sol=input
     elif isinstance(input,string_types):
         self.__sol=SolutionDirectory(input,
                                      paraviewLink=False,
                                      archive=None)
     else:
         error(type(input),"not supported")
Exemplo n.º 46
0
    def compressCase(self,dirName,warn=False):
        if not path.exists(dirName):
            self.error("Directory",dirName,"does not exist")
        s=SolutionDirectory(dirName,
                            archive=None,
                            paraviewLink=False,
                            parallel=True,
                            tolerant=True)
        if not s.isValid():
            if warn:
                print_("Directory",dirName,"is not an OpenFOAM-case")
            return

        self.nrDir+=1
        oldNr=self.nrFiles
        oldUnc=self.prevSize
        oldCon=self.nowSize

        if self.verbose>0:
            print_("Processing case",dirName)

        # compress meshes
        for d in glob(path.join(dirName,"*","polyMesh"))+glob(path.join(dirName,"*","*","polyMesh")):
            if path.isdir(d):
                self.compressDirectory(d)

        # compress times
        for t in s:
            self.compressDirectory(t.name)

        # compress logfiles if requested
        if self.opts.logfile:
            for f in glob(path.join(dirName,"*.logfile")):
                self.compressFile(path.join(dirName,f))

        # processor direcories
        for p in s.procDirs:
            self.compressDirectory(path.join(dirName,p))
        if self.nrFiles>oldNr and self.verbose>0:
            print_("  -> ",self.nrFiles-oldNr,"files compressed.",
                   humanReadableSize((self.prevSize-oldUnc)-(self.nowSize-oldCon)),"gained")
Exemplo n.º 47
0
    def recursiveCompress(self,dirName):
        if self.verbose>1:
            print_("Recursively checking",dirName)
        if path.isdir(dirName):
            s=SolutionDirectory(dirName,archive=None,paraviewLink=False,parallel=True)
            if s.isValid():
                try:
                    self.compressCase(dirName)
                except OSError:
                    e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                    self.warning("Problem processing",dirName,":",e)
                return

        for f in listdir(dirName):
            name=path.join(dirName,f)
            try:
                if path.isdir(name):
                    self.recursiveCompress(name)
            except OSError:
                e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                self.warning("Problem processing",name,":",e)
 def _input_trait_modified(self, obj, name, old, new):
     """hook th changing a trait."""
     super(FoamBaseComponent, self)._input_trait_modified(obj, name, old, new)
     if name == 'case_dir':
         caseDir = self.getPath(new,False)
         
         if not caseDir == None:
             #type(caseDir) is str or unicode
             self.foam_case = SolutionDirectory(str(caseDir))
         else:
             self.raise_exception("case_dir '%s' was"
                                      " not found." % new, RuntimeError)
Exemplo n.º 49
0
    def run(self):
        oldStdout=None

        try:
            if self.opts.file:
                oldStdout=sys.stdout
                if isinstance(self.opts.file,string_types):
                    sys.stdout=open(self.opts.file,"w")
                else:
                    sys.stdout=self.opts.file

            if self.opts.allRegions:
                sol=SolutionDirectory(self.parser.getArgs()[0],
                                      archive=None,
                                      parallel=self.opts.parallel,
                                      paraviewLink=False)
                for r in sol.getRegions():
                    self.doRegion(r)
            else:
                self.doRegion(self.opts.region)
        finally:
            if oldStdout:
                sys.stdout=oldStdout
Exemplo n.º 50
0
    def run(self):
        sol=SolutionDirectory(self.parser.getArgs()[0])
        if not self.opts.init:
            vcs=sol.determineVCS()
            if vcs==None:
                self.error("not under version control")                
            if not vcs in self.vcsChoices:
                self.error("Unsupported VCS",vcs)
        else:
            vcs=self.opts.vcs

        vcsInter=getVCS(vcs,
                        path=sol.name,
                        init=self.opts.init)

        vcsInter.addPath(path.join(sol.name,"constant"),rules=ruleList)
        vcsInter.addPath(path.join(sol.name,"system"),rules=ruleList)
        if sol.initialDir()!=None:
            vcsInter.addPath(sol.initialDir(),rules=ruleList)
        else:
            self.warning("No initial-directory found")

        # special PyFoam-files
        for f in ["customRegexp","LocalConfigPyFoam"]:
            p=path.join(sol.name,f)
            if path.exists(p):
                vcsInter.addPath(p,rules=ruleList)

        # Add the usual files from the tutorials
        for g in ["Allrun*","Allclean*"]:
            for f in glob(path.join(sol.name,g)):
                vcsInter.addPath(f,rules=ruleList)
                
        for a in self.opts.additional:
            vcsInter.addPath(a,rules=ruleList)
            
        vcsInter.commit(self.opts.commitMessage)
 def run(self):
     cName=self.parser.getArgs()[0]
     if self.opts.cloneCase:
         if self.opts.autoCasename:
             cName=path.join(cName,
                             path.basename(self.opts.cloneCase)+
                             buildFilenameExtension(self.opts.valuesDicts,
                                                    self.opts.values))
         if path.exists(cName):
             self.error(cName,"already existing (case should not exist when used with --clone-case)")
         if self.checkCase(self.opts.cloneCase,
                           fatal=self.opts.fatal,
                           verbose=not self.opts.noComplain):
             self.addLocalConfig(self.opts.cloneCase)
         orig=SolutionDirectory(self.opts.cloneCase,
                                archive=None,paraviewLink=False)
         sol=orig.cloneCase(cName)
     else:
         if self.checkCase(cName,
                           fatal=self.opts.fatal,
                           verbose=not self.opts.noComplain):
             self.addLocalConfig(cName)
         sol=SolutionDirectory(cName,archive=None,paraviewLink=False)
     self.prepare(sol,cName=cName)
    def __init__(self):
        """Component.__init__() and check the path to icoFoam."""
        super(FoamBaseComponent, self).__init__()

        self.foam_case = None
        #Check OpenFOAM commands in $PATH.
        if not self._which("icoFoam"):
            self.raise_exception("OpenFOAM command is not found. Check $PATH.", RuntimeError)

        if not self.case_dir == '':
            caseDir = self.getPath(self.case_dir, False)
            
            if not caseDir == None:
                #type(caseDir) is str or unicode
                self.foam_case = SolutionDirectory(str(caseDir))
Exemplo n.º 53
0
class MeshInformation:
    """Reads Information about the mesh on demand"""
    
    def __init__(self,
                 case,
                 time="constant",
                 processor=None,
                 region=None):
        """@param case: Path to the case-directory
        @param time: Time for which the  mesh should be looked at
        @param processor: Name of the processor directory for decomposed cases"""
        self.sol=SolutionDirectory(case,paraviewLink=False,archive=None,region=region)
        self.time=time
        self.processor=processor
        
    def nrOfFaces(self):
        try:
            return self.faces
        except AttributeError:
            try:
                faces=ListFile(self.sol.polyMeshDir(time=self.time,processor=self.processor),"faces")
                self.faces=faces.getSize()
            except IOError:
                faces=ListFile(self.sol.polyMeshDir(processor=self.processor),"faces")
                self.faces=faces.getSize()
                
            return self.faces

    def nrOfPoints(self):
        try:
            return self.points
        except AttributeError:
            try:
                points=ListFile(self.sol.polyMeshDir(time=self.time,processor=self.processor),"points")
                self.points=points.getSize()
            except IOError:
                points=ListFile(self.sol.polyMeshDir(processor=self.processor),"points")
                self.points=points.getSize()

            return self.points

    def nrOfCells(self):
        try:
            return self.cells
        except:
            try:
                try:
                    owner=ParsedFileHeader(path.join(self.sol.polyMeshDir(time=self.time,processor=self.processor),"owner"))
                except IOError:
                    owner=ParsedFileHeader(path.join(self.sol.polyMeshDir(processor=self.processor),"owner"))

                mat=re.compile('.+nCells: *([0-9]+) .+').match(owner["note"])
                self.cells=int(mat.group(1))
                return self.cells
            except:
                raise PyFoamException("Not Implemented")
Exemplo n.º 54
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.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)
        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={}
        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 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()
                        d[name]=v
                        variation.append(d)
                return variation
            else:
                return [{name:v} for v in vals]

        variations=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.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.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,
                               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_,
                            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")
Exemplo n.º 55
0
    def run(self):
        decomposeParWithRegion=(foamVersion()>=(1,6))

        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 decomposeParWithRegion and self.opts.keeppseudo:
            warning("Option --keep-pseudocases doesn't make sense since OpenFOAM 1.6 because decomposePar supports regions")

        nr=int(self.parser.getArgs()[1])
        if nr<2:
            error("Number of processors",nr,"too small (at least 2)")

        case=path.abspath(self.parser.getArgs()[0])
        method=self.opts.method

        result={}
        result["numberOfSubdomains"]=nr
        result["method"]=method

        coeff={}
        result[method+"Coeffs"]=coeff

        if self.opts.globalFaceZones!=None:
            try:
                fZones=eval(self.opts.globalFaceZones)
            except SyntaxError:
                fZones=FoamStringParser(
                    self.opts.globalFaceZones,
                    listDict=True
                ).data

            result["globalFaceZones"]=fZones

        if method in ["metis","scotch","parMetis"]:
            if self.opts.processorWeights!=None:
                weigh=eval(self.opts.processorWeights)
                if nr!=len(weigh):
                    error("Number of processors",nr,"and length of",weigh,"differ")
                coeff["processorWeights"]=weigh
        elif method=="manual":
            if self.opts.dataFile==None:
                error("Missing required option dataFile")
            else:
                coeff["dataFile"]="\""+self.opts.dataFile+"\""
        elif method=="simple" or method=="hierarchical":
            if self.opts.n==None or self.opts.delta==None:
                error("Missing required option n or delta")
            n=eval(self.opts.n)
            if len(n)!=3:
                error("Needs to be three elements, not",n)
            if nr!=n[0]*n[1]*n[2]:
                error("Subdomains",n,"inconsistent with processor number",nr)
            coeff["n"]="(%d %d %d)" % (n[0],n[1],n[2])

            coeff["delta"]=float(self.opts.delta)
            if method=="hierarchical":
                if self.opts.order==None:
                    error("Missing reuired option order")
                if len(self.opts.order)!=3:
                    error("Order needs to be three characters")
                coeff["order"]=self.opts.order
        else:
            error("Method",method,"not yet implementes")

        gen=FoamFileGenerator(result)

        if self.opts.test:
            print_(str(gen))
            return -1
        else:
            f=open(path.join(case,"system","decomposeParDict"),"w")
            writeDictionaryHeader(f)
            f.write(str(gen))
            f.close()

        if self.opts.clear:
            print_("Clearing processors")
            for p in glob(path.join(case,"processor*")):
                print_("Removing",p)
                rmtree(p,ignore_errors=True)

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

        if self.opts.doDecompose:
            if self.opts.region:
                regionNames=self.opts.region[:]
                while True:
                    try:
                        i=regionNames.index("region0")
                        regionNames[i]=None
                    except ValueError:
                        break
            else:
                regionNames=[None]

            regions=None

            sol=SolutionDirectory(case)
            if not decomposeParWithRegion:
                if self.opts.regions or self.opts.region!=None:
                    print_("Building Pseudocases")
                    regions=RegionCases(sol,clean=True,processorDirs=False)

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

            for theRegion in regionNames:
                theCase=path.normpath(case)
                if theRegion!=None and not decomposeParWithRegion:
                    theCase+="."+theRegion

                if oldApp():
                    argv=[self.opts.decomposer,".",theCase]
                else:
                    argv=[self.opts.decomposer,"-case",theCase]
                    if foamVersion()>=(2,0) and not self.opts.doFunctionObjects:
                        argv+=["-noFunctionObjects"]
                    if theRegion!=None and decomposeParWithRegion:
                        argv+=["-region",theRegion]

                        f=open(path.join(case,"system",theRegion,"decomposeParDict"),"w")
                        writeDictionaryHeader(f)
                        f.write(str(gen))
                        f.close()

                self.setLogname(default="Decomposer",useApplication=False)

                run=UtilityRunner(argv=argv,
                                  silent=self.opts.progress or self.opts.silent,
                                  logname=self.opts.logname,
                                  compressLog=self.opts.compress,
                                  server=self.opts.server,
                                  noLog=self.opts.noLog,
                                  logTail=self.opts.logTail,
                                  echoCommandLine=self.opts.echoCommandPrefix,
                                  jobId=self.opts.jobId)
                run.start()

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

            if regions!=None and not decomposeParWithRegion:
                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)

            if self.opts.doConstantLinks:
                print_("Adding symlinks in the constant directories")
                constPath=path.join(case,"constant")
                for f in listdir(constPath):
                    srcExpr=path.join(path.pardir,path.pardir,"constant",f)
                    for p in range(nr):
                        dest=path.join(case,"processor%d"%p,"constant",f)
                        if not path.exists(dest):
                            symlink(srcExpr,dest)

            self.addToCaseLog(case)
        def lookForCases(d):
            for n in tqdm(listdir(d),
                          unit="entries",
                          leave=False,
                          desc=path.basename(path.abspath(d)),
                          disable=not self.opts.progressBar):
                if not self.fnmatch(n):
                    continue
                cName=path.join(d,n)
                if path.isdir(cName):
                    try:
                        sol=SolutionDirectory(cName,archive=None,paraviewLink=False)
                        if sol.isValid():
                            if self.opts.progress:
                                print_("Processing",cName)

                            data={}

                            data["mtime"]=stat(cName)[ST_MTIME]
                            times=sol.getTimes()
                            try:
                                data["first"]=times[0]
                            except IndexError:
                                data["first"]="None"
                            try:
                                data["last"]=times[-1]
                            except IndexError:
                                data["last"]="None"
                            data["nrSteps"]=len(times)
                            data["procs"]=sol.nrProcs()
                            data["pFirst"]=-1
                            data["pLast"]=-1
                            data["nrParallel"]=-1
                            if self.opts.parallel:
                                pTimes=sol.getParallelTimes()
                                data["nrParallel"]=len(pTimes)
                                if len(pTimes)>0:
                                    data["pFirst"]=pTimes[0]
                                    data["pLast"]=pTimes[-1]
                            data["name"]=cName
                            data["diskusage"]=-1
                            if self.opts.diskusage:
                                data["diskusage"]=diskUsage(cName)
                                totalDiskusage+=data["diskusage"]
                            if self.opts.parallel:
                                for f in listdir(cName):
                                    if re.compile("processor[0-9]+").match(f):
                                        data["mtime"]=max(stat(path.join(cName,f))[ST_MTIME],data["mtime"])

                            if self.opts.state or self.opts.estimateEndTime:
                                try:
                                    data["startedAt"]=time.mktime(time.strptime(self.readState(sol,"StartedAt")))
                                except ValueError:
                                    data["startedAt"]="nix"

                            if self.opts.state:
                                try:
                                    data["nowTime"]=float(self.readState(sol,"CurrentTime"))
                                except ValueError:
                                    data["nowTime"]=None

                                try:
                                    data["lastOutput"]=time.mktime(time.strptime(self.readState(sol,"LastOutputSeen")))
                                except ValueError:
                                    data["lastOutput"]="nix"

                                data["state"]=self.readState(sol,"TheState")
                                if data["state"]=="Running":
                                    try:
                                        gone=time.time()-data["lastOutput"]
                                        if gone>self.opts.deadThreshold:
                                            data["state"]="Dead "+humanReadableDuration(gone)
                                    except KeyError:
                                        pass
                                    except TypeError:
                                        pass

                            if self.opts.startEndTime or self.opts.estimateEndTime:
                                try:
                                    ctrlDict=ParsedParameterFile(sol.controlDict(),doMacroExpansion=True)
                                except PyFoamParserError:
                                    # Didn't work with Macro expansion. Let's try without
                                    try:
                                        ctrlDict=ParsedParameterFile(sol.controlDict())
                                    except PyFoamParserError:
                                        ctrlDict=None
                                if ctrlDict:
                                    data["startTime"]=ctrlDict["startTime"]
                                    data["endTime"]=ctrlDict["endTime"]
                                else:
                                    data["startTime"]=None
                                    data["endTime"]=None

                            if self.opts.estimateEndTime:
                                data["endTimeEstimate"]=None
                                if self.readState(sol,"TheState")=="Running":
                                    gone=time.time()-data["startedAt"]
                                    try:
                                        current=float(self.readState(sol,"CurrentTime"))
                                        frac=(current-data["startTime"])/(data["endTime"]-data["startTime"])
                                    except ValueError:
                                        frac=0
                                    if frac>0:
                                        data["endTimeEstimate"]=data["startedAt"]+gone/frac

                            if self.opts.hgInfo:
                                if path.isdir(path.join(cName,".hg")):
                                    from stat import ST_ATIME
                                    prevStat=stat(cName)
                                    try:
                                        data["hgInfo"]=sub.Popen(["hg", "id",
                                                                  "-R",cName,
                                                                  "-b","-n","-i"], stdout=sub.PIPE).communicate()[0].strip()
                                    except OSError:
                                        data["hgInfo"]="<hg not working>"
                                    postStat=stat(cName)
                                    if prevStat[ST_MTIME]!=postStat[ST_MTIME]:
                                        # hg seems to modify the modification time of the directory. So reset it
                                        os.utime(cName,(postStat[ST_ATIME],prevStat[ST_MTIME]))
                                else:
                                    data["hgInfo"]="<no .hg directory>"

                            if len(customData)>0 or self.opts.hostname:
                                fn=None
                                pickleFile=None
                                if useSolverInData:
                                    data["solver"]="none found"
                                    # try to find the oldest pickled file
                                    dirAndTime=[]
                                    for f in ["pickledData","pickledUnfinishedData","pickledStartData"]:
                                        for g in glob(path.join(cName,"*.analyzed")):
                                            pName=path.join(g,f)
                                            base=path.basename(g)
                                            if base.find("PyFoamRunner.")==0:
                                                solverName=base[len("PyFoamRunner."):-len(".analyzed")]
                                            else:
                                                solverName=None
                                            if path.exists(pName):
                                                dirAndTime.append((path.getmtime(pName),solverName,pName))
                                    dirAndTime.sort(key=lambda x:x[0])

                                    if len(dirAndTime)>0:
                                        data["solver"]=dirAndTime[-1][1]
                                        pickleFile=dirAndTime[-1][2]

                                    solverName=data["solver"]
                                else:
                                    solverName=self.opts.solverNameForCustom

                                if pickleFile:
                                    fn=pickleFile
                                else:
                                    for f in ["pickledData","pickledUnfinishedData","pickledStartData"]:
                                        fp=path.join(cName,"PyFoamRunner."+solverName+".analyzed",f)
                                        if path.exists(fp):
                                            fn=fp
                                            break
                                pickleOK=False
                                if fn:
                                    try:
                                        raw=pickle.Unpickler(open(fn,"rb")).load()
                                        pickleOK=True
                                        for n,spec in customData:
                                            dt=raw
                                            for k in spec:
                                                try:
                                                    dt=dt[k]
                                                except KeyError:
                                                    dt="No key '"+k+"'"
                                                    break
                                                if isinstance(dt,string_types):
                                                    break
                                            data[n]=dt
                                        if self.opts.hostname:
                                            try:
                                                data["hostname"]=raw["hostname"].split(".")[0]
                                            except KeyError:
                                                data["hostname"]="<unspecified>"
                                    except ValueError:
                                        pass
                                if not pickleOK:
                                    for n,spec in customData:
                                        data[n]="<no file>"
                                    if self.opts.hostname:
                                        data["hostname"]="<no file>"
                            cData.append(data)
                        elif self.opts.recursive:
                            # print("Recurse",cName)
                            lookForCases(cName)
                    except OSError:
                        print_(cName,"is unreadable")
Exemplo n.º 57
0
    def run(self):
        config=ConfigParser.ConfigParser()
        files=self.parser.getArgs()

        good=config.read(files)
        # will work with 2.4
        # if len(good)!=len(files):
        #    print "Problem while trying to parse files",files
        #    print "Only ",good," could be parsed"
        #    sys.exit(-1)

        benchName=config.get("General","name")
        if self.opts.nameAddition!=None:
            benchName+="_"+self.opts.nameAddition
        if self.opts.foamVersion!=None:
            benchName+="_v"+self.opts.foamVersion
            
        isParallel=config.getboolean("General","parallel")
        lam=None

        if isParallel:
            nrCpus=config.getint("General","nProcs")
            machineFile=config.get("General","machines")
            if not path.exists(machineFile):
                self.error("Machine file ",machineFile,"needed for parallel run")
            lam=LAMMachine(machineFile,nr=nrCpus)
            if lam.cpuNr()>nrCpus:
                self.error("Wrong number of CPUs: ",lam.cpuNr())

            print "Running parallel on",lam.cpuNr(),"CPUs"

        if config.has_option("General","casesDirectory"):
            casesDirectory=path.expanduser(config.get("General","casesDirectory"))
        else:
            casesDirectory=foamTutorials()

        if not path.exists(casesDirectory):
            self.error("Directory",casesDirectory,"needed with the benchmark cases is missing")
        else:
            print "Using cases from directory",casesDirectory

        benchCases=[]
        config.remove_section("General")

        for sec in config.sections():
            print "Reading: ",sec
            skipIt=False
            skipReason=""
            if config.has_option(sec,"skip"):
                skipIt=config.getboolean(sec,"skip")
                skipReason="Switched off in file"
            if self.opts.excases!=None and not skipIt:
                for p in self.opts.excases:
                    if fnmatch(sec,p):
                        skipIt=True
                        skipReason="Switched off by pattern '"+p+"'"
            if self.opts.cases!=None:
                for p in self.opts.cases:
                    if fnmatch(sec,p):
                        skipIt=False
                        skipReason=""
                
            if skipIt:
                print "Skipping case ..... Reason:"+skipReason
                continue
            sol=config.get(sec,"solver")
            cas=config.get(sec,"case")
            pre=eval(config.get(sec,"prepare"))
            preCon=[]
            if config.has_option(sec,"preControlDict"):
                preCon=eval(config.get(sec,"preControlDict"))
            con=eval(config.get(sec,"controlDict"))
            bas=config.getfloat(sec,"baseline")
            wei=config.getfloat(sec,"weight")
            add=[]
            if config.has_option(sec,"additional"):
                add=eval(config.get(sec,"additional"))
                print "Adding: ", add
            util=[]
            if config.has_option(sec,"utilities"):
                util=eval(config.get(sec,"utilities"))
                print "Utilities: ", util    
            nr=99999
            if config.has_option(sec,"nr"):
                nr=eval(config.get(sec,"nr"))
            sp=None
            if config.has_option(sec,"blockSplit"):
                sp=eval(config.get(sec,"blockSplit"))
            toRm=[]
            if config.has_option(sec,"filesToRemove"):
                toRm=eval(config.get(sec,"filesToRemove"))
            setInit=[]
            if config.has_option(sec,"setInitial"):
                setInit=eval(config.get(sec,"setInitial"))

            parallelOK=False
            if config.has_option(sec,"parallelOK"):
                parallelOK=config.getboolean(sec,"parallelOK")

            deMet=["metis"]
            if config.has_option(sec,"decomposition"):
                deMet=config.get(sec,"decomposition").split()

            if deMet[0]=="metis":
                pass
            elif deMet[0]=="simple":
                if len(deMet)<2:
                    deMet.append(0)
                else:
                    deMet[1]=int(deMet[1])
            else:
                print "Unimplemented decomposition method",deMet[0],"switching to metis"
                deMet=["metis"]

            if isParallel==False or parallelOK==True:
                if path.exists(path.join(casesDirectory,sol,cas)):
                    benchCases.append( (nr,sec,sol,cas,pre,con,preCon,bas,wei,add,util,sp,toRm,setInit,deMet) )
                else:
                    print "Skipping",sec,"because directory",path.join(casesDirectory,sol,cas),"could not be found"
            else:
                print "Skipping",sec,"because not parallel"

        benchCases.sort()

        parallelString=""
        if isParallel:
            parallelString=".cpus="+str(nrCpus)

        resultFile=open("Benchmark."+benchName+"."+uname()[1]+parallelString+".results","w")

        totalSpeedup=0
        minSpeedup=None
        maxSpeedup=None
        totalWeight =0
        runsOK=0
        currentEstimate = 1.

        print "\nStart Benching\n"
        
        csv=CSVCollection("Benchmark."+benchName+"."+uname()[1]+parallelString+".csv")
        
#        csvHeaders=["description","solver","case","caseDir","base",
#                    "benchmark","machine","arch","cpus","os","version",
#                    "wallclocktime","cputime","cputimeuser","cputimesystem","maxmemory","cpuusage","speedup"]

        for nr,description,solver,case,prepare,control,preControl,base,weight,additional,utilities,split,toRemove,setInit,decomposition in benchCases:
            #    control.append( ("endTime",-2000) )
            print "Running Benchmark: ",description
            print "Solver: ",solver
            print "Case: ",case
            caseName=solver+"_"+case+"_"+benchName+"."+uname()[1]+".case"
            print "Short name: ",caseName
            caseDir=caseName+".runDir"

            csv["description"]=description
            csv["solver"]=solver
            csv["case"]=case
            csv["caseDir"]=caseDir
            csv["base"]=base

            csv["benchmark"]=benchName
            csv["machine"]=uname()[1]
            csv["arch"]=uname()[4]
            if lam==None:
                csv["cpus"]=1
            else:
                csv["cpus"]=lam.cpuNr()
            csv["os"]=uname()[0]
            csv["version"]=uname()[2]
            
            workDir=path.realpath(path.curdir)

            orig=SolutionDirectory(path.join(casesDirectory,solver,case),
                                   archive=None,
                                   paraviewLink=False)
            for a in additional+utilities:
                orig.addToClone(a)
            orig.cloneCase(path.join(workDir,caseDir))

            if oldApp():
                argv=[solver,workDir,caseDir]
            else:
                argv=[solver,"-case",path.join(workDir,caseDir)]
                
            run=BasicRunner(silent=True,argv=argv,logname="BenchRunning",lam=lam)
            runDir=run.getSolutionDirectory()
            controlFile=ParameterFile(runDir.controlDict())

            for name,value in preControl:
                print "Setting parameter",name,"to",value,"in controlDict"
                controlFile.replaceParameter(name,value)

            for rm in toRemove:
                fn=path.join(caseDir,rm)
                print "Removing file",fn
                remove(fn)

            for field,bc,val in setInit:
                print "Setting",field,"on",bc,"to",val
                SolutionFile(runDir.initialDir(),field).replaceBoundary(bc,val)

            oldDeltaT=controlFile.replaceParameter("deltaT",0)

            for u in utilities:
                print "Building utility ",u
                execute("wmake 2>&1 >%s %s" % (path.join(caseDir,"BenchCompile."+u),path.join(caseDir,u)))

            print "Preparing the case: "
            if lam!=None:
                prepare=prepare+[("decomposePar","")]
                if decomposition[0]=="metis":
                    lam.writeMetis(SolutionDirectory(path.join(workDir,caseDir)))
                elif decomposition[0]=="simple":
                    lam.writeSimple(SolutionDirectory(path.join(workDir,caseDir)),decomposition[1])

            if split:
                print "Splitting the mesh:",split
                bm=BlockMesh(runDir.blockMesh())
                bm.refineMesh(split)

            for pre,post in prepare:
                print "Doing ",pre," ...."
                post=post.replace("%case%",caseDir)
                if oldApp():
                    args=string.split("%s %s %s %s" % (pre,workDir,caseDir,post))
                else:
                    args=string.split("%s -case %s %s" % (pre,path.join(workDir,caseDir),post))
                util=BasicRunner(silent=True,argv=args,logname="BenchPrepare_"+pre)
                util.start()

            controlFile.replaceParameter("deltaT",oldDeltaT)

            #    control.append(("endTime",-1000))
            for name,value in control:
                print "Setting parameter",name,"to",value,"in controlDict"
                controlFile.replaceParameter(name,value)

            print "Starting at ",asctime(localtime(time()))
            print " Baseline is %f, estimated speedup %f -> estimated end at %s " % (base,currentEstimate,asctime(localtime(time()+base/currentEstimate)))
            print "Running the case ...."
            run.start()

            speedup=None
            cpuUsage=0
            speedupOut=-1

            try:
                speedup=base/run.run.wallTime()
                cpuUsage=100.*run.run.cpuTime()/run.run.wallTime()
            except ZeroDivisionError:
                print "Division by Zero: ",run.run.wallTime()

            if not run.runOK():
                print "\nWARNING!!!!"
                print "Run had a problem, not using the results. Check the log\n"
                speedup=None

            if speedup!=None:
                speedupOut=speedup

                totalSpeedup+=speedup*weight
                totalWeight +=weight
                runsOK+=1
                if maxSpeedup==None:
                    maxSpeedup=speedup
                elif speedup>maxSpeedup:
                    maxSpeedup=speedup
                if minSpeedup==None:
                    minSpeedup=speedup
                elif speedup<minSpeedup:
                    minSpeedup=speedup

            print "Wall clock: ",run.run.wallTime()
            print "Speedup: ",speedup," (Baseline: ",base,")"
            print "CPU Time: ",run.run.cpuTime()
            print "CPU Time User: "******"CPU Time System: ",run.run.cpuSystemTime()
            print "Memory: ",run.run.usedMemory()
            print "CPU Usage: %6.2f%%" % (cpuUsage)

            csv["wallclocktime"]=run.run.wallTime()
            csv["cputime"]=run.run.cpuTime()
            csv["cputimeuser"]=run.run.cpuUserTime()
            csv["cputimesystem"]=run.run.cpuSystemTime()
            csv["maxmemory"]=run.run.usedMemory()
            csv["cpuusage"]=cpuUsage
            if speedup!=None:
                csv["speedup"]=speedup
            else:
                csv["speedup"]="##"
                
            csv.write()
            
            resultFile.write("Case %s WallTime %g CPUTime %g UserTime %g SystemTime %g Memory %g MB  Speedup %g\n" %(caseName,run.run.wallTime(),run.run.cpuTime(),run.run.cpuUserTime(),run.run.cpuSystemTime(),run.run.usedMemory(),speedupOut))

            resultFile.flush()
            
            if speedup!=None:
                currentEstimate=totalSpeedup/totalWeight

            if self.opts.removeCases:
                print "Clearing case",
                if speedup==None:
                    print "not ... because it failed"
                else:
                    print "completely"
                    rmtree(caseDir,ignore_errors=True)

            print
            print
        
        if lam!=None:
            lam.stop()

        print "Total Speedup: ",currentEstimate," ( ",totalSpeedup," / ",totalWeight, " ) Range: [",minSpeedup,",",maxSpeedup,"]"

        print runsOK,"of",len(benchCases),"ran OK"

        resultFile.write("Total Speedup: %g\n" % (currentEstimate))
        if minSpeedup and maxSpeedup:
            resultFile.write("Range: [ %g , %g ]\n" % (minSpeedup,maxSpeedup))

        resultFile.close()
class FoamBaseComponent(Component):
    """This class is base Component to execute OpenFOAM pre,main and post commands"""
    case_dir = Str("", iotype="in", desc='OpenFOAM Case Dir. Absolute path or relative path in $FOAM_RUN.')
    force_fd = Bool(True, iotype='in', framework_var=True, deriv_ignore=True,
                    desc="If True, always finite difference this component.")

    def __init__(self):
        """Component.__init__() and check the path to icoFoam."""
        super(FoamBaseComponent, self).__init__()

        self.foam_case = None
        #Check OpenFOAM commands in $PATH.
        if not self._which("icoFoam"):
            self.raise_exception("OpenFOAM command is not found. Check $PATH.", RuntimeError)

        if not self.case_dir == '':
            caseDir = self.getPath(self.case_dir, False)
            
            if not caseDir == None:
                #type(caseDir) is str or unicode
                self.foam_case = SolutionDirectory(str(caseDir))

    def check_config(self):
        if not self.foam_case and self.case_dir == "":
            self.raise_exception("Not set self.case_dir.", RuntimeError)
    
        if not os.path.exists(self.foam_case.controlDict()):
            self.raise_exception("%s is not found. Check self.case_dir." % (self.foam_case.controlDict()),
                                 RuntimeError)

    def _which(self, cmd):
        """which command with python."""
        def is_exe(val):
            return os.path.isfile(val) and os.access(val, os.X_OK)

        fpath, fname = os.path.split(cmd)
        if fpath:
            if is_exe(cmd):
                return cmd
        else:        
            for path in os.environ["PATH"].split(os.pathsep):
                path = path.strip('"')
                exe_file = os.path.join(path, cmd)
                if is_exe(exe_file):
                    return exe_file
        return None
        
    def getPath(self, fdpath, riseError=True):
        """check and get the absolute path or relative path in $FOAM_RUN.""" 
        if os.path.exists(fdpath):
            return fdpath
            
        bool_foam_run = True
        foam_run = ""
        try:
            foam_run = os.environ['FOAM_RUN']
            fpath = os.path.join(foam_run, fdpath)
        except KeyError:
            bool_foam_run = False
        
        if bool_foam_run and os.path.exists(fpath):
            return fpath
            
        if riseError:
            self.raise_exception(" '%s' was not found." % fdpath, RuntimeError)
        return None
                
    def _input_trait_modified(self, obj, name, old, new):
        """hook th changing a trait."""
        super(FoamBaseComponent, self)._input_trait_modified(obj, name, old, new)
        if name == 'case_dir':
            caseDir = self.getPath(new,False)
            
            if not caseDir == None:
                #type(caseDir) is str or unicode
                self.foam_case = SolutionDirectory(str(caseDir))
            else:
                self.raise_exception("case_dir '%s' was"
                                         " not found." % new, RuntimeError)