def writeToFile(self, outfile, vals, gzip=False): """In the template, replaces all the strings between $$ with the evaluation of the expressions and writes the results to a file :param outfile: the resulting output file :param vals: dictionary with the values :param gzip: Zip the file (and add a .gz to the name)""" from os import path output = self.getString(vals) if path.splitext(outfile) == ".gz": gzip = True elif path.exists(outfile + ".gz"): outfile += ".gz" gzip = True elif gzip: outfile += ".gz" if gzip: import gzip as gz if PY3: output = output.encode() gz.open(outfile, "wb").write(output) unzipped=path.splitext(outfile)[0] if path.exists(unzipped): warning("Removing",unzipped,"because it might shadow generated", outfile) from os import unlink unlink(unzipped) else: open(outfile, "w").write(output) return outfile
def additionalParameters(self): """Additional parameters @return: a dictionary with parameters for this task""" warning("Method 'additionalParameters' not implemented. Not a problem. Just saying") return {}
def p_dictbody(self, p): '''dictbody : dictbody dictline | dictline | empty''' if len(p) == 3: p[0] = p[1] if self.duplicateCheck: if p[2][0] in p[0]: if self.duplicateFail: error("Key", p[2][0], "already defined") else: warning("Key", p[2][0], "already defined") if type(p[2][0]) == DictRedirection and p[2][1] == '': p[0].addRedirection(p[2][0]) else: if type(p[2][1]) == DictRedirection: p[0][p[2][0]] = p[2][1].getContent() else: p[0][p[2][0]] = p[2][1] p[0].addDecoration(p[2][0], self.getDecoration()) else: p[0] = self.dictStack[-1] if p[1]: if type(p[1][0]) == DictRedirection and p[1][1] == '': p[0].addRedirection(p[1][0]) else: if type(p[1][1]) == DictRedirection: p[0][p[1][0]] = p[1][1].getContent() else: p[0][p[1][0]] = p[1][1]
def run(self): if len(self.parser.getArgs())>2: error("Too many arguments:",self.parser.getArgs()[2:],"can not be used") sName=self.parser.getArgs()[0] dName=self.parser.getArgs()[1] if path.exists(dName): if self.parser.getOptions().force: warning("Replacing",dName,"(--force option)") elif path.exists(path.join(dName,"system","controlDict")): error("Destination",dName,"already existing and a Foam-Case") elif path.isdir(dName): dName=path.join(dName,path.basename(sName)) if path.exists(dName) and not self.parser.getOptions().force: error(dName,"already existing") elif not path.exists(path.dirname(dName)): warning("Directory",path.dirname(dName),"does not exist. Creating") sol=SolutionDirectory(sName, archive=None, paraviewLink=False, addLocalConfig=True, parallel=self.opts.parallel) if sol.determineVCS()!=None and self.opts.vcs: if self.opts.chemkin or self.opts.additional or self.opts.latest: self.error("Using an unimplemented option together with VCS") vcsInter=getVCS(sol.determineVCS(), path=sol.name) vcsInter.clone(dName) return if self.parser.getOptions().chemkin: sol.addToClone("chemkin") if self.parser.getOptions().dopyfoam: sol.addToClone("customRegexp") for a in self.parser.getOptions().additional: sol.addToClone(a) if self.parser.getOptions().latest: sol.addToClone(sol.getLast()) if self.opts.symlinkMode: sol.symlinkCase( dName, followSymlinks=self.parser.getOptions().followSymlinks, maxLevel=self.opts.symlinkLevel, relPath=self.opts.symlinkRelative ) else: sol.cloneCase( dName, followSymlinks=self.parser.getOptions().followSymlinks ) self.addToCaseLog(dName,"Cloned to",dName)
def resetIt(self): if self.fresh: warning("Trigger called: Resetting fvSchemes and fvSolution") self.solution.restore() self.schemes.restore() self.control.restore() self.fresh = False
def resetIt(self): if self.fresh: warning("Trigger called: Resetting fvSchemes and fvSolution") self.solution.restore() self.schemes.restore() self.control.restore() self.fresh=False
def __init__(self, sol, factor): self.solution = ParsedParameterFile(path.join(sol.systemDir(), "fvSolution"), backup=True) self.schemes = ParsedParameterFile(path.join(sol.systemDir(), "fvSchemes"), backup=True) self.fresh = True try: relax = self.solution["relaxationFactors"] for var in relax: relax[var] *= factor cExp = re.compile("div\((.+),(.+)\)") conv = self.schemes["divSchemes"] for nm in conv: if cExp.match(nm) or nm == "default": conv[nm] = "Gauss upwind" self.solution.writeFile() self.schemes.writeFile() except Exception: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' warning("Restoring defaults") self.solution.restore() self.schemes.restore() raise e
def __init__(self, name, createZipped=True): """@param name: Name of the file. If the field is zipped the .gz is appended. Alternatively it can be a filehandle @param createZipped: if the file doesnot exist: should it be created as a zipped file?""" if hasattr(name, "read"): self.name = None self.exists = True self.zipped = None self.fh = name else: self.name = path.abspath(name) self.exists = False if path.exists(self.name): self.exists = True self.zipped = False if path.splitext(self.name)[1] == ".gz": self.zipped = True elif path.exists(self.name + ".gz"): warning(self.name + ".gz", "and", self.name, "existing - using the unzipped") elif path.exists(self.name + ".gz"): self.zipped = True self.exists = True else: self.zipped = createZipped if path.splitext(self.name)[1] == ".gz": self.name = self.name[:-3] self.fh = None self.content = None
def __init__(self, name, backup=False, createZipped=True, useBinary=False): """:param name: The name of the parameter file :type name: str :param backup: create a backup-copy of the file :type backup: boolean""" if hasattr(name, "read"): if backup: warning(str(name), "is a file-handle. No backup possible") backup = False FileBasis.__init__(self, name, createZipped=createZipped, useBinary=useBinary) if backup: self.backupName = self.name + ".backup" try: FileBasisBackup.counter[self.name] += 1 except KeyError: FileBasisBackup.counter[self.name] = 1 if self.zipped: self.copyfile(self.name + ".gz", self.backupName + ".gz") else: self.copyfile(self.name, self.backupName) else: self.backupName = None
def reread(self,force=False): """Scan the directory for files with valid names""" if not force and stat(self.name)[ST_CTIME]<=self.lastReread: return self.values=[] ex=["*~",".svn"] for f in listdir(self.name): matched=False for e in ex: if fnmatch(f,e): matched=True if path.isdir(path.join(self.name,f)): continue if not matched: nm=f if len(nm)>3: if nm[-3:]==".gz": nm=nm[:-3] if nm not in self.values: self.values.append(nm) else: if not self.tolerant: error(nm," already found, propably exists as zipped and unzipped") else: warning(nm," already found, propably exists as zipped and unzipped") self.values.sort() self.lastReread=stat(self.name)[ST_CTIME]
def getSeries(self, reindex=True): """Return a dictionary of the data-columns in the Series format of pandas :param: drop duplicate times (setting it to False might break certain Pandas-operations)""" try: import pandas except ImportError: warning("No pandas-library installed. Returning None") return None data = {} if reindex: realindex = numpy.unique(self.data[self.time]) for n in self.names(): if n != self.time: data[n] = pandas.Series(self.data[n], index=self.data[self.time], name=n) if reindex: if len(data[n]) != len(realindex): try: data[n].axes[0].is_unique = True except: # Newer Pandas versions don't allow setting this. Just drop duplicates data[n] = data[n].drop_duplicates() data[n] = data[n].reindex_axis(realindex) return data
def __init__(self, sol, writeAll, purge, until): self.control = ParsedParameterFile(path.join(sol.systemDir(), "controlDict"), backup=True, doMacroExpansion=True) self.fresh = True try: if writeAll: self.control["writeControl"] = "timeStep" self.control["writeInterval"] = "1" if purge != None: self.control["purgeWrite"] = purge if until != None: self.control["endTime"] = until self.control.writeFile() except Exception: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' warning("Restoring defaults") self.control.restore() raise e
def __init__(self, timelines, custom, showWindow=True, registry=None): """:param timelines: The timelines object :type timelines: TimeLineCollection :param custom: A CustomplotInfo-object. Values in this object usually override the other options """ MatplotlibTimelines.__init__(self, timelines, custom, showWindow=showWindow, registry=registry ) from matplotlib import pyplot try: pyplot.xkcd() except AttributeError: from matplotlib import __version__ warning("Installed version",__version__, " of Matplotlib does not support XKCD-mode (this is supported starting with version 1.3). Falling back to normal operations")
def __init__(self, timelines, custom, showWindow=True, registry=None): """@param timelines: The timelines object @type timelines: TimeLineCollection @param custom: A CustomplotInfo-object. Values in this object usually override the other options """ MatplotlibTimelines.__init__(self, timelines, custom, showWindow=showWindow, registry=registry ) from matplotlib import pyplot try: pyplot.xkcd() except AttributeError: from matplotlib import __version__ warning("Installed version",__version__, " of Matplotlib does not support XKCD-mode (this is supported starting with version 1.3). Falling back to normal operations")
def compare(self, src, dst, depth, name): if type(src) != type(dst): print_( f.diff + ">><<", name, ": Types differ" + f.reset + "\n+" + f.src + ">>Source:" + f.reset + "\n", makeString(src), "\n" + f.dst + "<<Destination:" + f.reset + "\n", makeString(dst) + f.reset) self.pling = True elif type(src) in [tuple, list, TupleProxy]: self.compareIterable(src, dst, depth, name) elif isinstance( src, (str, float, bool, BoolProxy) + integer_types) or src == None: self.comparePrimitive(src, dst, depth, name) elif src.__class__ in [Dimension, Tensor, SymmTensor, Vector]: self.comparePrimitive(src, dst, depth, name) elif src.__class__ == Field: self.compareField(src, dst, depth, name) elif type(src) in [DictProxy, dict]: self.compareDict(src, dst, depth, name) else: warning("Type of", name, "=", type(src), "unknown") if self.opts.debug: try: print_("Class of", name, "=", src.__class__, "unknown") except: pass
def __init__(self,sol,factor): self.solution=ParsedParameterFile(path.join(sol.systemDir(),"fvSolution"),backup=True) self.schemes=ParsedParameterFile(path.join(sol.systemDir(),"fvSchemes"),backup=True) self.fresh=True try: relax=self.solution["relaxationFactors"] for var in relax: relax[var]*=factor cExp=re.compile("div\((.+),(.+)\)") conv=self.schemes["divSchemes"] for nm in conv: if cExp.match(nm) or nm=="default": conv[nm]="Gauss upwind" self.solution.writeFile() self.schemes.writeFile() except Exception: e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' warning("Restoring defaults") self.solution.restore() self.schemes.restore() raise e
def doHardcopy(self, filename, form): """Write the contents of the plot to disk @param filename: Name of the file without type extension @param form: String describing the format""" if form == "png": self.hardcopy(terminal="png", filename=filename + ".png", small=True) elif form == "pdf": self.hardcopy(terminal="pdf", filename=filename + ".pdf", color=True) elif form == "svg": self.hardcopy(terminal="svg", filename=filename + ".svg") elif form == "postscript": self.hardcopy(terminal="postscript", filename=filename + ".ps", color=True) elif form == "eps": self.hardcopy(terminal="postscript", filename=filename + ".eps", color=True, eps=True) else: warning("Hardcopy format", form, "unknown. Falling back to postscript") self.hardcopy(filename=filename + ".ps", color=True)
def p_dictbody(self,p): '''dictbody : dictbody dictline | dictline | empty''' if len(p)==3: p[0]=p[1] if self.duplicateCheck: if p[2][0] in p[0]: if self.duplicateFail: error("Key",p[2][0],"already defined") else: warning("Key",p[2][0],"already defined") if type(p[2][0])==DictRedirection and p[2][1]=='': p[0].addRedirection(p[2][0]) else: if type(p[2][1])==DictRedirection: p[0][p[2][0]]=p[2][1].getContent() else: p[0][p[2][0]]=p[2][1] p[0].addDecoration(p[2][0],self.getDecoration()) else: p[0]=self.dictStack[-1] if p[1]: if type(p[1][0])==DictRedirection and p[1][1]=='': p[0].addRedirection(p[1][0]) else: if type(p[1][1])==DictRedirection: p[0][p[1][0]]=p[1][1].getContent() else: p[0][p[1][0]]=p[1][1]
def buildData(self,times,name,title,lastValid): """Build the implementation specific data @param times: The vector of times for which data exists @param name: the name under which the data is stored in the timeline @param title: the title under which this will be displayed""" a=self.axis1 if name in self.alternate: a=self.axis2 data=self.data.getValues(name) tm=times if len(tm)>0 and not lastValid: tm=tm[:-1] data=data[:-1] plotIt=True try: if self.spec.logscale and min(data)<=0: plotIt=False except AttributeError: pass if self.spec.start!=None or self.spec.end!=None: start=self.spec.start end=self.spec.end if start==None: start=tm[0] if end==None: end=tm[-1] self.axis1.set_xbound(lower=start,upper=end) self.axis1.set_autoscalex_on(False) if self.axis2: self.axis2.set_xbound(lower=start,upper=end) self.axis2.set_autoscalex_on(False) drawstyle='default' marker='' linestyle='-' if self.with_=='lines': pass elif self.with_=='steps': drawstyle='steps' elif self.with_=='points': linestyle='' marker='*' elif self.with_=='dots': linestyle='' marker='.' elif self.with_=='linespoints': marker='*' else: warning("'with'-style",self.with_,"not implemented, using 'lines'") if plotIt: a.plot(tm, data, label=title, drawstyle=drawstyle, marker=marker, linestyle=linestyle)
def foamInstalledVersions(): """:return: A list with the installed versions of OpenFOAM""" global __foamInstallations if __foamInstallations: return __foamInstallations __foamInstallations={} forks=config().getList("OpenFOAM","Forks") for fork in forks: currentFork=foamFork() if "WM_PROJECT_INST_DIR" in environ and currentFork==fork: basedir=environ["WM_PROJECT_INST_DIR"] else: basedir=path.expanduser(config().get("OpenFOAM","Installation-"+fork)) if not path.exists(basedir) or not path.isdir(basedir): warning("Basedir",basedir,"for fork",fork,"does not exist or is not a directory") # continue for bdir in [basedir]+config().getList("OpenFOAM","AdditionalInstallation-"+fork): for val in [re.compile(s) for s in config().getList("OpenFOAM","DirPatterns-"+fork)]: __foamInstallations.update(findInstalledVersions(bdir,val,fork)) return __foamInstallations
def doAnalysis(self,line): """General analysis method. Derived classes should instead override callbacks""" m=self.exp.match(line) if m!=None: self.startAnalysis(m) if self.doTimelines: try: time=float(self.getTime()) if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime): self.addToTimelines(m) except ValueError: e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' warning("Problem doing timelines",e) if self.doFiles: self.addToFiles(m) self.endAnalysis(m) if not self.didProgress and self.progressTemplate: myProgress=self.progressTemplate for i,g in enumerate(m.groups()): myProgress=myProgress.replace("$%d" % i,g) self.writeProgress(myProgress) self.didProgress=False
def doAnalysis(self,line): """General analysis method. Derived classes should instead override callbacks""" m=self.doMatch(line) if m!=None: self.startAnalysis(m) if self.doTimelines: if self.plotIterations: self.iterCounter+=1 self.lines.setTime(self.iterCounter) try: time=float(self.getTime()) try: if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime) or self.plotIterations: self.addToTimelines(m) except ValueError: e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' warning("Problem doing timelines",e) except ValueError: # no valid time information yet pass if self.doFiles: self.addToFiles(m) self.endAnalysis(m) if not self.didProgress and self.progressTemplate: self.writeProgress(self.processProgressTemplate(m)) self.didProgress=False
def doHardcopy(self, filename, form, termOpts=""): """Write the contents of the plot to disk :param filename: Name of the file without type extension :param form: String describing the format""" # print "Hardcopy",filename,form,termOpts if termOpts != "": termOpts = " " + termOpts if form == "png": self.internalHardcopy(terminal="png" + termOpts, filename=filename + ".png") elif form == "pdf": self.internalHardcopy(terminal="pdf" + termOpts, filename=filename + ".pdf") elif form == "svg": self.internalHardcopy(terminal="svg" + termOpts, filename=filename + ".svg") elif form == "postscript": self.internalHardcopy(terminal="postscript" + termOpts, filename=filename + ".ps") elif form == "eps": self.internalHardcopy(terminal="postscript eps" + termOpts, filename=filename + ".eps") else: warning("Hardcopy format", form, "unknown. Falling back to postscript") self.hardcopy(filename=filename + ".ps", color=True)
def buildMPIrun(self, argv, expandApplication=True): """Builds a list with a working mpirun command (for that MPI-Implementation) @param argv: the original arguments that are to be wrapped @param expandApplication: Expand the @return: list with the correct mpirun-command""" mpirun = ["mpprun"] progname = argv[0] if expandApplication: stat, progname = commands.getstatusoutput('which '+progname) if stat: progname = argv[0] warning( "which can not find a match for", progname, ". Hoping for the best" ) mpirun += [progname] + argv[1:3] + ["-parallel"] + argv[3:] if config().getdebug("ParallelExecution"): debug("MPI:", foamMPI()) debug("Arguments:", mpirun) system("which mpirun") system("which rsh") debug("Environment", environ) for a in mpirun: if a in environ: debug("Transfering variable", a, "with value", environ[a]) return mpirun
def __init__(self,name,createZipped=True): """@param name: Name of the file. If the field is zipped the .gz is appended @param createZipped: if the file doesnot exist: should it be created as a zipped file?""" self.name = path.abspath(name) self.exists = False if path.exists(self.name): self.exists = True self.zipped=False if path.splitext(self.name)[1]==".gz": self.zipped=True elif path.exists(self.name+".gz"): warning(self.name+".gz","and",self.name,"existing - using the unzipped") elif path.exists(self.name+".gz"): self.zipped=True self.exists = True else: self.zipped=createZipped if path.splitext(self.name)[1]==".gz": self.name=self.name[:-3] self.fh=None self.content=None
def doAnalysis(self, line): """General analysis method. Derived classes should instead override callbacks""" m = self.exp.match(line) if m != None: self.startAnalysis(m) if self.doTimelines: try: time = float(self.getTime()) if (self.startTime == None or time >= self.startTime) and ( self.endTime == None or time <= self.endTime): self.addToTimelines(m) except ValueError: e = sys.exc_info()[ 1] # Needed because python 2.5 does not support 'as e' warning("Problem doing timelines", e) if self.doFiles: self.addToFiles(m) self.endAnalysis(m) if not self.didProgress and self.progressTemplate: myProgress = self.progressTemplate for i, g in enumerate(m.groups()): myProgress = myProgress.replace("$%d" % i, g) self.writeProgress(myProgress) self.didProgress = False
def buildMPIrun(self,argv,expandApplication=True): """Builds a list with a working mpirun command (for that MPI-Implementation) @param argv: the original arguments that are to be wrapped @param expandApplication: Expand the @return: list with the correct mpirun-command""" nr=str(self.cpuNr()) mpirun=[config().get("MPI","run_"+foamMPI(),default="mpirun")] mpiRunPath=self.which(" ".join(mpirun)) if not mpiRunPath: error("Could not find a path for",mpirun, "Check configuration variable","run_"+foamMPI(), "in section 'MPI'") mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_pre",default="[]")) if(foamMPI()=="LAM"): mpirun+=["-np",nr] elif foamMPI().find("OPENMPI")>=0: nr=[] if "MPI_ARCH_PATH" in environ and config().getboolean("MPI","OpenMPI_add_prefix"): nr+=["--prefix",environ["MPI_ARCH_PATH"]] if self.procNr!=None: nr+=["--n",str(self.procNr)] machine=[] if self.mFile!=None: machine=["--machinefile",self.mFile] if config().getdebug("ParallelExecution"): debug("Start of",self.mFile) debug("\n"+open(self.mFile).read()) debug("End of",self.mFile) mpirun+=machine+nr else: error(" Unknown or missing MPI-Implementation for mpirun: "+foamMPI()) mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_post",default="[]")) progname=argv[0] if expandApplication: # old implementation: stat,progname=commands.getstatusoutput('which '+progname) progname=self.which(progname) if progname: progname=argv[0] warning("which can not find a match for",progname,". Hoping for the best") if oldAppConvention(): mpirun+=[progname]+argv[1:3]+["-parallel"]+argv[3:] else: mpirun+=[progname]+argv[1:]+["-parallel"] if config().getdebug("ParallelExecution"): debug("MPI:",foamMPI()) debug("Arguments:",mpirun) system("which mpirun") system("which rsh") debug("Environment",environ) for a in mpirun: if a in environ: debug("Transfering variable",a,"with value",environ[a]) return mpirun
def getSeries(self,reindex=True): """Return a dictionary of the data-columns in the Series format of pandas :param: drop duplicate times (setting it to False might break certain Pandas-operations)""" try: import pandas except ImportError: warning("No pandas-library installed. Returning None") return None data={} if reindex: realindex=numpy.unique(self.data[self.time]) for n in self.names(): if n!=self.time: data[n]=pandas.Series(self.data[n], index=self.data[self.time], name=n) if reindex: if len(data[n])!=len(realindex): try: data[n].axes[0].is_unique=True except: # Newer Pandas versions don't allow setting this. Just drop duplicates data[n]=data[n].drop_duplicates() data[n]=data[n].reindex_axis(realindex) return data
def __init__(self): CaseBuilderQt.__init__(self) self.descriptions = CaseBuilderDescriptionList() if len(self.descriptions) == 0: error("No description-files (.pfcb) found in path", config().get("CaseBuilder", "descriptionpath")) mainLayout = QtGui.QVBoxLayout() self.setLayout(mainLayout) self.descriptsList = QtGui.QListWidget() self.descriptsList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) mainLayout.addWidget(self.descriptsList) self.itemlist = [] for d in self.descriptions: item = QtGui.QListWidgetItem(d[2]) item.setToolTip(d[3]) self.descriptsList.addItem(item) self.itemlist.append((item, d)) buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel) mainLayout.addWidget(buttons) selectButton = QtGui.QPushButton("Select") selectButton.setToolTip("Select the case description that we want to work with") buttons.addButton(selectButton, QtGui.QDialogButtonBox.AcceptRole) try: buttons.accepted.connect(self.selectPressed) buttons.rejected.connect(self.reject) except AttributeError: warning("Old QT-version where QDialogButtonBox doesn't have the accepted/rejected-attributes") self.connect(buttons, QtCore.SIGNAL("accepted()"), self.selectPressed) self.connect(buttons, QtCore.SIGNAL("rejected()"), self.reject)
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
def __init__(self, timelines, custom, showWindow=True, registry=None): """@param timelines: The timelines object @type timelines: TimeLineCollection @param custom: A CustomplotInfo-object. Values in this object usually override the other options """ self.hasSubplotHost=True try: global plt,matplotlib,firstTimeImport,SubplotHost import matplotlib if not showWindow and firstTimeImport: # matplotlib.use("MacOSX") matplotlib.use("agg") firstTimeImport=False import matplotlib.pyplot as plt try: from mpl_toolkits.axes_grid.parasite_axes import SubplotHost except ImportError: self.hasSubplotHost=False warning("Matplotlib-Version does not support SubplotHost") except ImportError: error("Matplotlib not installed.") GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry) self.figNr=MatplotlibTimelines.figureNr MatplotlibTimelines.figureNr+=1 self.figure=None self.title="" self.ylabel="" self.ylabel2="" try: if self.spec.ylabel: self.setYLabel(self.spec.ylabel) except AttributeError: pass try: if self.spec.y2label: self.setYLabel2(self.spec.y2label) except AttributeError: pass self.axis1=None self.axis2=None self.setTitle(self.spec.theTitle) self.with_=self.spec.with_ if not self.with_ in ['lines','points','dots','steps','linespoints']: warning("'with'-style",self.with_,"not implemented, using 'lines'") self.with_='lines' self.redo()
def _eval(self, expr, data): """evalfunc with error-messages""" try: return self.evalfunc(expr, data) except (TypeError,NameError,IndexError,KeyError,AttributeError, SyntaxError): err = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' warning(self.reportString(expr,err)) return "Template evaluation ERROR: "+self.reportString(expr,err)
def __init__(self, timelines, custom, showWindow=True, registry=None): """@param timelines: The timelines object @type timelines: TimeLineCollection @param custom: A CustomplotInfo-object. Values in this object usually override the other options """ try: global Qt, Qwt, app from PyQt4 import Qt import PyQt4.Qwt5 as Qwt if showWindow and app == None: app = Qt.QApplication([]) # app.thread() except ImportError: error("Could not import Qt4 or Qwt") GeneralPlotTimelines.__init__(self, timelines, custom, showWindow=showWindow, registry=registry) self.figNr = QwtPlotTimelines.figureNr QwtPlotTimelines.figureNr += 1 self.figure = None self.title = "no title" self.ylabel = "no label" self.ylabel2 = "no label" try: if self.spec.ylabel: self.setYLabel(self.spec.ylabel) except AttributeError: pass try: if self.spec.y2label: self.setYLabel2(self.spec.y2label) except AttributeError: pass self.axis1 = None self.axis2 = None self.setTitle(self.spec.theTitle) self.with_ = self.spec.with_ if not self.with_ in ['lines']: warning("'with'-style", self.with_, "not implemented, using 'lines'") self.with_ = 'lines' self.curves = {} self.redo()
def manipulate(self,content): what,mix,trans,spec,therm,gas=self.analyzeThermoType(content) for nm in content: data=content[nm] used=0 if type(data) not in [tuple,TupleProxy]: continue if len(data)<5: continue transDict={} if trans=="constTransport": transDict["Pr"]=data[-1-used] transDict["mu"]=data[-2-used] used+=2 elif trans=="sutherlandTransport": transDict["Ts"]=data[-1-used] transDict["As"]=data[-2-used] used+=2 else: error("Transport type",trans,"not implemented") thermDict={} if therm=="hConstThermo": thermDict["Hf"]=data[-1-used] thermDict["Cp"]=data[-2-used] used+=2 elif therm=="eConstThermo": thermDict["Hf"]=data[-1-used] thermDict["Cv"]=data[-2-used] used+=2 elif therm=="janafThermo": thermDict["lowCpCoeffs"]=data[-7-used:-0-used] thermDict["highCpCoeffs"]=data[-14-used:-7-used] thermDict["Tcommon"]=data[-15-used] thermDict["Thigh"]=data[-16-used] thermDict["Tlow"]=data[-17-used] used+=2*7+3 else: error("Thermodynamics type",therm,"not implemented") specDict={} if spec=="specieThermo": specDict["molWeight"]=data[-1-used] specDict["nMoles"]=data[-2-used] used+=2 else: error("Specie type",spec,"not implemented") if len(data)!=used+1: warning("Not all data for",nm,"used") comment=self.makeComment(data) content[nm]={"specie":specDict, "thermodynamics":thermDict, "transport":transDict} content.addDecoration(nm,comment)
def getData(self): """Return a dictionary of the data in the DataFrame format of pandas""" try: import pandas except ImportError: warning("No pandas-library installed. Returning None") return None return pandas.DataFrame(self.getSeries())
def addSafeTrigger(self,run,sol,steady=True): if self.opts.safeUntil: if not steady: warning("This is an unsteady run. No safe settings set") else: warning("Adding Trigger and resetting to safer start-settings") trig=SafeTrigger(sol,self.opts.safeRelaxation) run.addTrigger(self.opts.safeUntil,trig.resetIt) run.addEndTrigger(trig.resetIt)
def checkDir(dName): c=case().name if path.exists(path.join(c,dName)): return dName elif path.exists(path.join(c,"postProcessing",dName)): return path.join("postProcessing",dName) else: warning("Could not find",dName,"in",c) return dName
def checkDir(dName): c = case().name if path.exists(path.join(c, dName)): return dName elif path.exists(path.join(c, "postProcessing", dName)): return path.join("postProcessing", dName) else: warning("Could not find", dName, "in", c) return dName
def stopJob(self): if self.listenToTimer: self.ordinaryEnd=False f=open(path.join(self.basename,"stop"),"w") f.write("Geh z'haus") f.close() unlink(self.stopFile()) else: warning("I'm not listening to your callbacks")
def __init__(self, timelines, custom, showWindow=True, registry=None): """:param timelines: The timelines object :type timelines: TimeLineCollection :param custom: A CustomplotInfo-object. Values in this object usually override the other options """ try: global Qt,Qwt,app from PyQt4 import Qt import PyQt4.Qwt5 as Qwt if showWindow and app==None: app = Qt.QApplication([]) # app.thread() except ImportError: error("Could not import Qt4 or Qwt") GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry) self.figNr=QwtPlotTimelines.figureNr QwtPlotTimelines.figureNr+=1 self.figure=None self.title="no title" self.ylabel="no label" self.ylabel2="no label" try: if self.spec.ylabel: self.setYLabel(self.spec.ylabel) except AttributeError: pass try: if self.spec.y2label: self.setYLabel2(self.spec.y2label) except AttributeError: pass self.axis1=None self.axis2=None self.setTitle(self.spec.theTitle) self.with_=self.spec.with_ if not self.with_ in ['lines']: warning("'with'-style",self.with_,"not implemented, using 'lines'") self.with_='lines' self.curves={} self.redo()
def copy(self, orig, purge=False, overwrite=True, mustExist=False, exclude=[], include=['*']): """Copy SolutionFiles from another TimeDirectory to the current TimeDirectory. Returns a list with the copied values @param orig: the TimeDirectory with the original files @param purge: remove all current files in this directory @param overwrite: if the file already exists it is overwritten @param mustExist: only if the file already exists it is overwritten @param exclude: List of fnmatch-patterns that should be excluded (Default: none) @param include: List of fnmatch-patterns that should be included (Default: all)""" if not overwrite and mustExist: warning("The options mustExist needs the option overwrite") overwrite = True if type(orig) != TimeDirectory: raise TypeError(type(value), "is not TimeDirectory") if purge: self.clear() copied = [] for v in orig: nm = v.baseName() doIt = False for p in include: if fnmatch(nm, p): doIt = True for p in exclude: if fnmatch(nm, p): doIt = False if not overwrite and nm in self: doIt = False if mustExist and nm not in self: doIt = False if doIt: copied.append(nm) self[nm] = v return copied # Should work with Python3 and Python2
def addSafeTrigger(self, run, sol, steady=True): if self.opts.safeUntil: if not steady: warning("This is an unsteady run. No safe settings set") else: warning("Adding Trigger and resetting to safer start-settings") trig = SafeTrigger(sol, self.opts.safeRelaxation) run.addTrigger(self.opts.safeUntil, trig.resetIt) run.addEndTrigger(trig.resetIt)
def parse(self, nr=None, exactNr=True): """ parse the options @param nr: minimum number of arguments that are to be passed to the application 3 is default for pre-1.5 versions of OpenFOAM """ (self.options, self.args) = self.parse_args(args=self.argLine) if "foamVersion" in dir(self.options): if self.options.foamVersion != None: if self.options.force32 and self.options.force64: error("A version can't be 32 and 64 bit at the same time") self.__foamVersionChanged = True self.__oldEnvironment = deepcopy(environ) changeFoamVersion(self.options.foamVersion, force64=self.options.force64, force32=self.options.force32, compileOption=self.options.compileOption, foamCompiler=self.options.foamCompiler, wmCompiler=self.options.wmCompiler) elif self.options.force32 or self.options.force64: warning( "Forcing version to be 32 or 64 bit, but no version chosen. Doing nothing" ) elif self.options.compileOption: warning( "No OpenFOAM-version chosen. Can't set compile-option to", self.options.compileOption) if nr == None: if oldApp(): nr = 3 else: nr = 1 if len(self.args) < nr: self.error("Too few arguments (%d needed, %d given)" % (nr, len(self.args))) maxNr = nr if not oldApp(): if "-case" in self.args: maxNr += 2 if exactNr and len(self.args) > maxNr: self.error("Too many arguments (%d needed, %d given)" % (nr, len(self.args))) tmp = self.args self.args = [] for a in tmp: if a.find(" ") >= 0 or a.find("(") >= 0: a = "\"" + a + "\"" self.args.append(a)
def foamRun(self,application, args=[], foamArgs=[], steady=False, multiRegion=None, progress=False, noLog=False): """Runs a foam utility on the case. If it is a parallel job and the grid has already been decomposed (and not yet reconstructed) it is run in parallel @param application: the Foam-Application that is to be run @param foamArgs: A list if with the additional arguments for the Foam-Application @param args: A list with additional arguments for the Runner-object @param steady: Use the steady-runner @param multiRegion: Run this on multiple regions (if None: I don't have an opinion on this) @param progress: Only output the time and nothing else @param noLog: Do not generate a logfile""" arglist=args[:] arglist+=["--job-id=%s" % self.fullJobId()] if self.isDecomposed and self.nproc>1: arglist+=["--procnr=%d" % self.nproc, "--machinefile=%s" % self.hostfile] if progress: arglist+=["--progress"] if noLog: arglist+=["--no-log"] if self.multiRegion: if multiRegion==None or multiRegion==True: arglist+=["--all-regions"] elif multiRegion and not self.multiRegion: warning("This is not a multi-region case, so trying to run stuff multi-region won't do any good") if self.restarted: arglist+=["--restart"] arglist+=[application] if oldApp(): arglist+=[".",self.casename()] else: arglist+=["-case",self.casename()] arglist+=foamArgs self.message("Executing",arglist) if steady: self.message("Running Steady") runner=SteadyRunner(args=arglist) else: runner=Runner(args=arglist)
def writeCheckpoint(self): if self.listenToTimer: f=open(path.join(self.basename,"write"),"w") f.write("Jetzt will ich's wissen") f.close() unlink(self.checkpointFile()) else: warning("I'm not listening to your callbacks") self.timer=Timer(1.,checkForMessageFromAbove,args=[self])
def addWriteAllTrigger(self, run, sol): if self.opts.purgeWrite != None and not self.opts.writeAll: warning("purgeWrite of", self.opts.purgeWrite, "ignored because write-all-timesteps unused") if self.opts.writeAll or self.opts.runUntil != None: warning("Adding Trigger and resetting to safer start-settings") trig = WriteAllTrigger(sol, self.opts.writeAll, self.opts.purgeWrite, self.opts.runUntil) run.addEndTrigger(trig.resetIt)
def getData(self, reindex=True): """Return a dictionary of the data in the DataFrame format of pandas :param: drop duplicate times (setting it to False might break certain Pandas-operations)""" try: from PyFoam.Wrappers.Pandas import PyFoamDataFrame except ImportError: warning("No pandas-library installed. Returning None") return None return PyFoamDataFrame(self.getSeries(reindex=reindex))
def boot(self): """Boots a LAM-machine using the machine-file""" if foamMPI() == "LAM": warning("LAM is untested. Any Feedback most welcome") self.execute("lamboot -s -v " + self.mFile) self.running = True elif foamMPI().find("OPENMPI") >= 0: self.running = True else: error(" Unknown or missing MPI-Implementation: " + foamMPI())
def boot(self): """Boots a LAM-machine using the machine-file""" if foamMPI()=="LAM": warning("LAM is untested. Any Feedback most welcome") self.execute("lamboot -s -v "+self.mFile) self.running=True elif foamMPI().find("OPENMPI")>=0: self.running=True else: error(" Unknown or missing MPI-Implementation: "+foamMPI())
def _writeStopAt(self,value,message): """Write stopAt to stop the run gracefully""" if not self.stopMe: self.stopMe=True if not self.isRestarted: if self.controlDict: warning("The controlDict has already been modified. Restoring will be problementic") self.controlDict=ParameterFile(path.join(self.dir,"system","controlDict"),backup=True) self.controlDict.replaceParameter("stopAt",value) warning(message)
def getData(self,reindex=True): """Return a dictionary of the data in the DataFrame format of pandas :param: drop duplicate times (setting it to False might break certain Pandas-operations)""" try: from PyFoam.Wrappers.Pandas import PyFoamDataFrame except ImportError: warning("No pandas-library installed. Returning None") return None return PyFoamDataFrame(self.getSeries(reindex=reindex))
def stopGracefully(self): """Tells the runner to stop at the next convenient time""" if not self.stopMe: self.stopMe=True if not self.isRestarted: if self.controlDict: warning("The controlDict has already been modified. Restoring will be problementic") self.controlDict=ParameterFile(path.join(self.dir,"system","controlDict"),backup=True) self.controlDict.replaceParameter("stopAt","writeNow") warning("Stopping run at next write")
def addWriteAllTrigger(self,run,sol): if self.opts.purgeWrite!=None and not self.opts.writeAll: warning("purgeWrite of",self.opts.purgeWrite,"ignored because write-all-timesteps unused") if self.opts.writeAll or self.opts.runUntil!=None: warning("Adding Trigger and resetting to safer start-settings") trig=WriteAllTrigger(sol, self.opts.writeAll, self.opts.purgeWrite, self.opts.runUntil) run.addEndTrigger(trig.resetIt)