def regenerateFixture(self, jobRunner): """Do a run to create the reference solution to use.""" resParams = ("elementResI", "elementResJ", "elementResK") ffile = stgxml.createFlattenedXML(self.inputFiles) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() origRes = [0] * 3 for ii, resParam in enumerate(resParams): origRes[ii] = stgxml.getParamValue(stgRoot, resParam, int) os.remove(ffile) highRes = [int(self.highResRatio * res) for res in origRes] print "Running the model to create a high-res reference solution " " after %d steps and with res %g times the original, " " and saving in dir '%s'" % ( self.runSteps, self.highResRatio, self.expectedSolnPath, ) mRun = self._createDefaultModelRun(self.testName + "-createReference", self.expectedSolnPath) mRun.simParams = SimParams(nsteps=self.runSteps, cpevery=self.runSteps, dumpevery=0) for ii, resParam in enumerate(resParams): mRun.paramOverrides[resParam] = highRes[ii] mRun.cpFields = self.fieldsToTest mRun.writeInfoXML() mRun.analysisXMLGen() result = jobRunner.runModel(mRun) # It's conceivable this could be useful, if we store results about # e.g. solver solution times etc. result.writeRecordXML()
def regenerateFixture(self, jobRunner): '''Do a run to create the reference solution to use.''' resParams = ("elementResI", "elementResJ", "elementResK") ffile = stgxml.createFlattenedXML(self.inputFiles) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() origRes = [0] * 3 for ii, resParam in enumerate(resParams): origRes[ii] = stgxml.getParamValue(stgRoot, resParam, int) os.remove(ffile) highRes = [int(self.highResRatio * res) for res in origRes] print "Running the model to create a high-res reference solution "\ " after %d steps and with res %g times the original, "\ " and saving in dir '%s'" % \ (self.runSteps, self.highResRatio, self.expectedSolnPath) mRun = self._createDefaultModelRun(self.testName + "-createReference", self.expectedSolnPath) mRun.simParams = SimParams(nsteps=self.runSteps, cpevery=self.runSteps, dumpevery=0) for ii, resParam in enumerate(resParams): mRun.paramOverrides[resParam] = highRes[ii] mRun.cpFields = self.fieldsToTest mRun.writeInfoXML() mRun.analysisXMLGen() result = jobRunner.runModel(mRun) # It's conceivable this could be useful, if we store results about # e.g. solver solution times etc. result.writeRecordXML()
def readFromStgXML(self, inputFilesList, basePath): '''Read in the list of fields that have already been specified to be tested from a set of StGermain input files. Useful when e.g. working with an Analytic Solution plugin.''' self.fromXML = True # create a flattened file absInputFiles = stgpath.convertLocalXMLFilesToAbsPaths( inputFilesList, basePath) ffile = stgxml.createFlattenedXML(absInputFiles) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() # Go and grab necessary info from XML file componentEl = stgxml.getStructNode(stgRoot, self.stgXMLCompListName) fieldTestDataEl = stgxml.getStructNode(componentEl, self.stgXMLCompName) fieldTestListEl = stgxml.getListNode(fieldTestDataEl, self.stgXMLSpecFList) fieldTestStructEls = fieldTestListEl.getchildren() for fieldStruct in fieldTestStructEls: numericField = stgxml.getParamValue(fieldStruct, 'NumericField', str) self.fields[numericField] = FieldComparisonOp(numericField) # NB: not reading in all the other specifying stuff currently. Possibly # would be useful to do this in future. os.remove(ffile)
def readFromStgXML(self, inputFilesList, basePath): '''Read in the list of fields that have already been specified to be tested from a set of StGermain input files. Useful when e.g. working with an Analytic Solution plugin.''' self.fromXML = True # create a flattened file absInputFiles = stgpath.convertLocalXMLFilesToAbsPaths(inputFilesList, basePath) ffile=stgxml.createFlattenedXML(absInputFiles) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() # Go and grab necessary info from XML file componentEl = stgxml.getStructNode(stgRoot, self.stgXMLCompListName) fieldTestDataEl = stgxml.getStructNode(componentEl, self.stgXMLCompName) fieldTestListEl = stgxml.getListNode(fieldTestDataEl, self.stgXMLSpecFList) fieldTestStructEls = fieldTestListEl.getchildren() for fieldStruct in fieldTestStructEls: numericField=stgxml.getParamValue(fieldStruct,'NumericField',str) self.fields[numericField]=FieldComparisonOp(numericField) # NB: not reading in all the other specifying stuff currently. Possibly # would be useful to do this in future. os.remove(ffile)
def getNumEls(mRun): """Calculate the number of elements used by a model run.""" #TODO: this should be abstracted a service of models perhaps ... #res = mRun.getResolution() from credo.io import stgxml from credo.io import stgpath import credo.modelrun as mrun from xml.etree import ElementTree as etree paramOverridesStr = mrun.getParamOverridesAsStr(mRun.paramOverrides) iFiles = mRun.modelInputFiles if mRun.analysisXML is not None: iFiles += [mRun.analysisXML] absInputFiles = stgpath.convertLocalXMLFilesToAbsPaths( iFiles, mRun.basePath) ffile = stgxml.createFlattenedXML(absInputFiles, paramOverridesStr) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() resX = stgxml.getParamValue(stgRoot, "elementResI", int) resY = stgxml.getParamValue(stgRoot, "elementResJ", int) resZ = stgxml.getParamValue(stgRoot, "elementResK", int) if resZ == 0: resZ = 1 #(Just in case) numEls = resX * resY * resZ return numEls
def readFromStgXML(self, inputFilesList, basePath, cmdLineOverrides): '''Reads all the parameters of this class from a given StGermain set of input files''' absInputFiles = stgpath.convertLocalXMLFilesToAbsPaths( inputFilesList, basePath) ffile = stgxml.createFlattenedXML(absInputFiles, cmdLineOverrides) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() for param, stgParam in self.stgParamInfos.iteritems(): # some of these may be none, but is ok since will check below val = stgxml.getParamValue(stgRoot, stgParam.stgName,\ stgParam.pType) self.setParam(param, val) self.checkValidParams() os.remove(ffile)
def readFromStgXML(self, inputFilesList, basePath, cmdLineOverrides): '''Reads all the parameters of this class from a given StGermain set of input files''' absInputFiles = stgpath.convertLocalXMLFilesToAbsPaths( inputFilesList, basePath) ffile=stgxml.createFlattenedXML(absInputFiles, cmdLineOverrides) xmlDoc = etree.parse(ffile) stgRoot = xmlDoc.getroot() for param, stgParam in self.stgParamInfos.iteritems(): # some of these may be none, but is ok since will check below val = stgxml.getParamValue(stgRoot, stgParam.stgName,\ stgParam.pType) self.setParam(param, val) self.checkValidParams() os.remove(ffile)
def test_getParamValue(self): dimVal = stgxml.getParamValue(self.flatXMLRoot, "dim", int) self.assertEqual(dimVal, 2) # Should be able to cast as a string fine. dimVal = stgxml.getParamValue(self.flatXMLRoot, "dim", str) self.assertEqual(dimVal, "2") # Try to get non-existent noneVal = stgxml.getParamValue(self.flatXMLRoot, "voodoo", int) self.assertEqual(noneVal, None) # Try to get a struct noneVal = stgxml.getParamValue(self.flatXMLRoot, "components", int) self.assertEqual(noneVal, None) noneVal = stgxml.getParamValue(self.flatXMLRoot, "temperatureBCs", int) self.assertEqual(noneVal, None) # Try from the non-flattened file dimVal = None dimVal = stgxml.getParamValue(self.inXMLRoot, "dim", int) self.assertEqual(dimVal, 2) dimVal = stgxml.getParamValue(self.flatXMLRoot, "dim", str) self.assertEqual(dimVal, "2")