def writeDictionaryContents(self,directory,name,contents): """Writes the contents of a dictionary @param directory: Sub-directory of the case @param name: name of the dictionary file @param contents: Python-dictionary with the dictionary contents""" theDir=self.name if directory: theDir=path.join(theDir,directory) result=WriteParameterFile(path.join(theDir,name)) result.content=contents result.writeFile()
def run(self): method = self.config.attrib["method"] n = self.config.attrib["n"] self.logger.info("Decomposing case for %s domains using method %s.", n, method) decomposeParDict = WriteParameterFile(os.path.join(self.case, "system/decomposeParDict"), createZipped=False) decomposeParDict["method"] = method decomposeParDict["numberOfSubdomains"] = n coeffs = {} for i in self.config: coeffs[i.tag] = i.text decomposeParDict[method + "Coeffs"] = coeffs decomposeParDict.writeFile() self.start_process("decomposePar -case %s" % self.case)
def testOrderOfAdditionsPreserved(self): orig=WriteParameterFile(self.dest) orig["a"]=1; orig["b"]=2; orig["d"]=3; orig["c"]=4; self.assertEqual(str(orig),"""// -*- C++ -*- // File generated by PyFoam - sorry for the ugliness FoamFile { class dictionary; format ascii; object testDictionary; version 2.0; } a 1; b 2; d 3; c 4; """)
def _mutable_data_file_manager(path, create_class=FileClass.DICTIONARY, create=True): """Context manager for mutating an OpenFOAM dict. If the dictionary is created, create_class is used to specify the class of the created dictionary. >>> dict_path = getfixture('tmpdir').join('tmpDict').strpath >>> with _mutable_data_file_manager(dict_path, create=True) as d: ... d['test'] = 'foo' >>> read_data_file(dict_path)['test'] 'foo' Args: path (str): path to OpenFOAM dict create_class (str or FileClass): specify the class of created files create (bool): create file if it does not exist Returns: A context manager which reads (or optionally creates) an OpenFOAM dict file, returns a Python representation. When the context is left, the content is written back to disk. """ if create and not os.path.isfile(path): dir_path = os.path.dirname(path) if not os.path.isdir(dir_path): os.makedirs(dir_path) try: create_class = create_class.value except AttributeError: create_class = create_class foam_file = WriteParameterFile(path, className=create_class) else: foam_file = ParsedParameterFile(path) yield foam_file.content foam_file.writeFile()
"p_{0:05} {{start ({1:} {2:} {3:}); end ({1:} {2:} {4:}); type face; axis z; nPoints {5:}; }}" .format(i, *p) for i, p in enumerate(waveProbesList) ] return d def createLinearWaveProbesList(xMin, xMax, nX, y, zMin, zMax, nZ): waveProbesList = [] deltaX = (xMax - xMin) / (nX - 1) for i in range(0, nX): waveProbesList.append([xMin + i * deltaX, y, zMin, zMax, nZ]) return waveProbesList if __name__ == "__main__": # works with tuples or with lists #waveProbesList = ( (10.,0.,-1.,+1 , 100) , (15.,0.,-1.,+1 , 100) ) waveProbesList = [[10., 0., -1., +1, 100], [15., 0., -1., +1, 100]] print(waveProbesList) waveProbesList = createLinearWaveProbesList(-100.0, 100.0, 201, 0.05, -3.0, 3.0, 100) print(waveProbesList) d = setWaveProbes(waveProbesList, "foamStar", writeProbesInterval=0.01) waveProbFile = WriteParameterFile("waveProb.inc") waveProbFile["functions"] = d waveProbFile.writeFile()
def prepare(self, sol, cName=None, overrideParameters=None, numberOfProcessors=None): """Do the actual preparing :param numberOfProcessors: If set this overrides the value set in the command line""" if cName == None: cName = sol.name if self.opts.onlyVariables: self.opts.verbose = True vals = {} vals, self.metaData = self.getDefaultValues(cName) vals.update( self.addDictValues( "System", "Automatically defined values", { "casePath": '"' + path.abspath(cName) + '"', "caseName": '"' + path.basename(path.abspath(cName)) + '"', "foamVersion": foamVersion(), "foamFork": foamFork(), "numberOfProcessors": numberOfProcessors if numberOfProcessors != None else self.opts.numberOfProcessors })) if len(self.opts.extensionAddition) > 0: vals.update( self.addDictValues( "ExtensionAdditions", "Additional extensions to be processed", dict((e, True) for e in self.opts.extensionAddition))) valsWithDefaults = set(vals.keys()) self.info("Looking for template values", cName) for f in self.opts.valuesDicts: self.info("Reading values from", f) vals.update( ParsedParameterFile(f, noHeader=True, doMacroExpansion=True).getValueDict()) setValues = {} for v in self.opts.values: self.info("Updating values", v) vals.update(eval(v)) setValues.update(eval(v)) if overrideParameters: vals.update(overrideParameters) unknownValues = set(vals.keys()) - valsWithDefaults if len(unknownValues) > 0: self.warning("Values for which no default was specified: " + ", ".join(unknownValues)) if self.opts.verbose and len(vals) > 0: print_("\nUsed values\n") nameLen = max(len("Name"), max(*[len(k) for k in vals.keys()])) format = "%%%ds - %%s" % nameLen print_(format % ("Name", "Value")) print_("-" * 40) for k, v in sorted(iteritems(vals)): print_(format % (k, v)) print_("") else: self.info("\nNo values specified\n") self.checkCorrectOptions(vals) derivedScript = path.join(cName, self.opts.derivedParametersScript) derivedAdded = None derivedChanged = None if path.exists(derivedScript): self.info("Deriving variables in script", derivedScript) scriptText = open(derivedScript).read() glob = {} oldVals = vals.copy() exec_(scriptText, glob, vals) derivedAdded = [] derivedChanged = [] for k, v in iteritems(vals): if k not in oldVals: derivedAdded.append(k) elif vals[k] != oldVals[k]: derivedChanged.append(k) if len(derivedChanged) > 0 and ( not self.opts.allowDerivedChanges and not configuration().getboolean("PrepareCase", "AllowDerivedChanges")): self.error( self.opts.derivedParametersScript, "changed values of", " ".join(derivedChanged), "\nTo allow this set --allow-derived-changes or the configuration item 'AllowDerivedChanges'" ) if len(derivedAdded) > 0: self.info("Added values:", " ".join(derivedAdded)) if len(derivedChanged) > 0: self.info("Changed values:", " ".join(derivedChanged)) if len(derivedAdded) == 0 and len(derivedChanged) == 0: self.info("Nothing added or changed") else: self.info("No script", derivedScript, "for derived values") if self.opts.onlyVariables: return self.__writeToStateFile(sol, "Starting") if self.opts.doClear: self.info("Clearing", cName) self.__writeToStateFile(sol, "Clearing") sol.clear(processor=True, pyfoam=True, vtk=True, removeAnalyzed=True, keepParallel=False, clearHistory=False, clearParameters=True, additional=["postProcessing"]) self.__writeToStateFile(sol, "Done clearing") if self.opts.writeParameters: fName = path.join(cName, self.parameterOutFile) self.info("Writing parameters to", fName) with WriteParameterFile(fName, noHeader=True) as w: w.content.update(vals, toString=True) w["foamVersion"] = vals["foamVersion"] w.writeFile() if self.opts.writeReport: fName = path.join(cName, self.parameterOutFile + ".rst") self.info("Writing report to", fName) with open(fName, "w") as w: helper = RestructuredTextHelper(defaultHeading=1) w.write(".. title:: " + self.__strip(vals["caseName"]) + "\n") w.write(".. sectnum::\n") w.write(".. header:: " + self.__strip(vals["caseName"]) + "\n") w.write(".. header:: " + time.asctime() + "\n") w.write(".. footer:: ###Page### / ###Total###\n\n") w.write("Parameters set in case directory " + helper.literal(self.__strip(vals["casePath"])) + " at " + helper.emphasis(time.asctime()) + "\n\n") w.write(".. contents::\n\n") if len(self.opts.valuesDicts): w.write(helper.heading("Parameter files")) w.write("Parameters read from files\n\n") w.write( helper.enumerateList([ helper.literal(f) for f in self.opts.valuesDicts ])) w.write("\n") if len(setValues) > 0: w.write(helper.heading("Overwritten parameters")) w.write( "These parameters were set from the command line\n\n") w.write(helper.definitionList(setValues)) w.write("\n") w.write(helper.heading("Parameters with defaults")) w.write(self.makeReport(vals)) if len(unknownValues) > 0: w.write(helper.heading("Unspecified parameters")) w.write( "If these parameters are actually used then specify them in " + helper.literal(self.defaultParameterFile) + "\n\n") tab = helper.table(True) for u in unknownValues: tab.addRow(u) tab.addItem("Value", vals[u]) w.write(str(tab)) if not derivedAdded is None: w.write(helper.heading("Derived Variables")) w.write("Script with derived Parameters" + helper.literal(derivedScript) + "\n\n") if len(derivedAdded) > 0: w.write("These values were added:\n") tab = helper.table(True) for a in derivedAdded: tab.addRow(a) tab.addItem("Value", str(vals[a])) w.write(str(tab)) if len(derivedChanged) > 0: w.write("These values were changed:\n") tab = helper.table(True) for a in derivedChanged: tab.addRow(a) tab.addItem("Value", str(vals[a])) tab.addItem("Old", str(oldVals[a])) w.write(str(tab)) w.write("The code of the script:\n") w.write(helper.code(scriptText)) self.addToCaseLog(cName) for over in self.opts.overloadDirs: self.info("Overloading files from", over) self.__writeToStateFile(sol, "Overloading") self.overloadDir(sol.name, over) self.__writeToStateFile(sol, "Initial") zeroOrig = path.join(sol.name, "0.org") hasOrig = path.exists(zeroOrig) cleanZero = True if not hasOrig: self.info("Not going to clean '0'") self.opts.cleanDirectories.remove("0") cleanZero = False if self.opts.doCopy: if hasOrig: self.info("Found 0.org. Clearing 0") zeroDir = path.join(sol.name, "0") if path.exists(zeroDir): rmtree(zeroDir) else: self.info("No 0-directory") self.info("") else: cleanZero = False if self.opts.doTemplates: self.__writeToStateFile(sol, "Templates") self.searchAndReplaceTemplates( sol.name, vals, self.opts.templateExt, ignoreDirectories=self.opts.ignoreDirectories) self.info("") backupZeroDir = None if self.opts.doMeshCreate: self.__writeToStateFile(sol, "Meshing") if self.opts.meshCreateScript: scriptName = path.join(sol.name, self.opts.meshCreateScript) if not path.exists(scriptName): self.error("Script", scriptName, "does not exist") elif path.exists(path.join(sol.name, self.defaultMeshCreate)): scriptName = path.join(sol.name, self.defaultMeshCreate) else: scriptName = None if scriptName: self.info("Executing", scriptName, "for mesh creation") if self.opts.verbose: echo = "Mesh: " else: echo = None self.executeScript(scriptName, workdir=sol.name, echo=echo) else: self.info( "No script for mesh creation found. Looking for 'blockMeshDict'" ) if sol.blockMesh() != "": self.info(sol.blockMesh(), "found. Executing 'blockMesh'") bm = BasicRunner(argv=["blockMesh", "-case", sol.name]) bm.start() if not bm.runOK(): self.error("Problem with blockMesh") for r in sol.regions(): self.info("Checking region", r) s = SolutionDirectory(sol.name, region=r, archive=None, paraviewLink=False) if s.blockMesh() != "": self.info(s.blockMesh(), "found. Executing 'blockMesh'") bm = BasicRunner(argv=[ "blockMesh", "-case", sol.name, "-region", r ]) bm.start() if not bm.runOK(): self.error("Problem with blockMesh") self.info("") if cleanZero and path.exists(zeroDir): self.warning("Mesh creation recreated 0-directory") if self.opts.keepZeroDirectoryFromMesh: backupZeroDir = zeroDir + ".bakByPyFoam" self.info("Backing up", zeroDir, "to", backupZeroDir) move(zeroDir, backupZeroDir) else: self.info("Data in", zeroDir, "will be removed") self.__writeToStateFile(sol, "Done Meshing") if self.opts.doCopy: self.__writeToStateFile(sol, "Copying") self.copyOriginals(sol.name) self.info("") if backupZeroDir: self.info("Copying backups from", backupZeroDir, "to", zeroDir) self.overloadDir(zeroDir, backupZeroDir) self.info("Removing backup", backupZeroDir) rmtree(backupZeroDir) if self.opts.doPostTemplates: self.__writeToStateFile(sol, "Post-templates") self.searchAndReplaceTemplates( sol.name, vals, self.opts.postTemplateExt, ignoreDirectories=self.opts.ignoreDirectories) self.info("") if self.opts.doCaseSetup: self.__writeToStateFile(sol, "Case setup") if self.opts.caseSetupScript: scriptName = path.join(sol.name, self.opts.caseSetupScript) if not path.exists(scriptName): self.error("Script", scriptName, "does not exist") elif path.exists(path.join(sol.name, self.defaultCaseSetup)): scriptName = path.join(sol.name, self.defaultCaseSetup) else: scriptName = None if scriptName: self.info("Executing", scriptName, "for case setup") if self.opts.verbose: echo = "Case:" else: echo = None self.executeScript(scriptName, workdir=sol.name, echo=echo) elif path.exists(path.join(sol.name, "system", "setFieldsDict")): self.info( "So setup script found. But 'setFieldsDict'. Executing setFields" ) sf = BasicRunner(argv=["setFields", "-case", sol.name]) sf.start() if not sf.runOK(): self.error("Problem with setFields") else: self.info("No script for case-setup found. Nothing done") self.info("") self.__writeToStateFile(sol, "Done case setup") if self.opts.doFinalTemplates: self.__writeToStateFile(sol, "Final templates") self.searchAndReplaceTemplates( sol.name, vals, self.opts.finalTemplateExt, ignoreDirectories=self.opts.ignoreDirectories) if self.opts.doTemplateClean: self.info("Clearing templates") for d in self.opts.cleanDirectories: for e in [ self.opts.templateExt, self.opts.postTemplateExt, self.opts.finalTemplateExt ]: self.cleanExtension(path.join(sol.name, d), e) self.info("") self.info("Case setup finished") self.__writeToStateFile(sol, "Finished OK")
def prepare(self, sol, cName=None, overrideParameters=None): if cName == None: cName = sol.name if self.opts.onlyVariables: self.opts.verbose = True vals = {} vals["casePath"] = '"' + path.abspath(cName) + '"' vals["caseName"] = '"' + path.basename(path.abspath(cName)) + '"' vals["foamVersion"] = foamVersion() vals["foamFork"] = foamFork() if self.opts.verbose: print_("Looking for template values", cName) for f in self.opts.valuesDicts: if self.opts.verbose: print_("Reading values from", f) vals.update( ParsedParameterFile(f, noHeader=True, doMacroExpansion=True).getValueDict()) for v in self.opts.values: if self.opts.verbose: print_("Updating values", v) vals.update(eval(v)) if overrideParameters: vals.update(overrideParameters) if self.opts.verbose and len(vals) > 0: print_("\nUsed values\n") nameLen = max(len("Name"), max(*[len(k) for k in vals.keys()])) format = "%%%ds - %%s" % nameLen print_(format % ("Name", "Value")) print_("-" * 40) for k, v in sorted(iteritems(vals)): print_(format % (k, v)) print_("") elif self.opts.verbose: print_("\nNo values specified\n") if self.opts.onlyVariables: return if self.opts.doClear: if self.opts.verbose: print_("Clearing", cName) sol.clear(processor=True, pyfoam=True, vtk=True, removeAnalyzed=True, keepParallel=False, clearHistory=False, clearParameters=True, additional=["postProcessing"]) if self.opts.writeParameters: fName = path.join(cName, self.parameterOutFile) if self.opts.verbose: print_("Writing parameters to", fName) with WriteParameterFile(fName, noHeader=True) as w: w.content.update(vals, toString=True) w["foamVersion"] = vals["foamVersion"] w.writeFile() self.addToCaseLog(cName) for over in self.opts.overloadDirs: if self.opts.verbose: print_("Overloading files from", over) self.overloadDir(sol.name, over) zeroOrig = path.join(sol.name, "0.org") hasOrig = path.exists(zeroOrig) if not hasOrig: if self.opts.verbose: print_("Not going to clean '0'") self.opts.cleanDirectories.remove("0") if self.opts.doCopy: if hasOrig: if self.opts.verbose: print_("Found 0.org. Clearing 0") zeroDir = path.join(sol.name, "0") if path.exists(zeroDir): rmtree(zeroDir) elif self.opts.verbose: print_("No 0-directory") if self.opts.verbose: print_("") if self.opts.doTemplates: self.searchAndReplaceTemplates(sol.name, vals, self.opts.templateExt) if self.opts.verbose: print_("") if self.opts.doMeshCreate: if self.opts.meshCreateScript: scriptName = path.join(sol.name, self.opts.meshCreateScript) if not path.exists(scriptName): self.error("Script", scriptName, "does not exist") elif path.exists(path.join(sol.name, self.defaultMeshCreate)): scriptName = path.join(sol.name, self.defaultMeshCreate) else: scriptName = None if scriptName: if self.opts.verbose: print_("Executing", scriptName, "for mesh creation") if self.opts.verbose: echo = "Mesh: " else: echo = None result = "".join( execute([scriptName], workdir=sol.name, echo=echo)) open(scriptName + ".log", "w").write(result) else: if self.opts.verbose: print_( "No script for mesh creation found. Looking for 'blockMeshDict'" ) if sol.blockMesh() != "": if self.opts.verbose: print_(sol.blockMesh(), "found. Executing 'blockMesh'") bm = BasicRunner(argv=["blockMesh", "-case", sol.name]) bm.start() if not bm.runOK(): self.error("Problem with blockMesh") if self.opts.verbose: print_("") if self.opts.doCopy: self.copyOriginals(sol.name) if self.opts.verbose: print_("") if self.opts.doPostTemplates: self.searchAndReplaceTemplates(sol.name, vals, self.opts.postTemplateExt) if self.opts.verbose: print_("") if self.opts.doCaseSetup: if self.opts.caseSetupScript: scriptName = path.join(sol.name, self.opts.caseSetupScript) if not path.exists(scriptName): self.error("Script", scriptName, "does not exist") elif path.exists(path.join(sol.name, self.defaultCaseSetup)): scriptName = path.join(sol.name, self.defaultCaseSetup) else: scriptName = None if scriptName: if self.opts.verbose: print_("Executing", scriptName, "for case setup") if self.opts.verbose: echo = "Case:" else: echo = None result = "".join( execute([scriptName], workdir=sol.name, echo=echo)) open(scriptName + ".log", "w").write(result) else: if self.opts.verbose: print_("No script for case-setup found. Nothing done") if self.opts.verbose: print_("") if self.opts.doTemplateClean: if self.opts.verbose: print_("Clearing templates") for d in self.opts.cleanDirectories: for e in [self.opts.templateExt, self.opts.postTemplateExt]: self.cleanExtension(path.join(sol.name, d), e) if self.opts.verbose: print_("") if self.opts.verbose: print_("Case setup finished")
from PyFoam.RunDictionary.ParsedParameterFile import WriteParameterFile from PyFoam.Basics.DataStructures import Dimension, Vector from os.path import join """ Convenience class to simply write "g" """ class Gravity(WriteParameterFile) : """ Gravity dictionnary """ def __init__(self , case, g = 9.81, version = "foamStar") : WriteParameterFile.__init__(self, name = join(case, "constant" , "g" ) ) self.header["class"] = "uniformDimensionedVectorField" self["dimensions"] = Dimension(*[0,1,-2,0,0,0,0]) self["value"] = Vector(*[0,0,-g]) if __name__ == "__main__" : print(Gravity("test"))