def addNewPath(trees, target, value): """ Adds a path and value to existing trees. Creates starting only at missing portion. @ In, trees, list(TreeStructure.InputTree), existing information @ In, target, list(string), nodal path to desired target as [Block1, Block2, entry] @ In, value, object, desired value for entry @ Ou, trees, list(TreeStructure.InputTree), modified list of trees """ foundSoFar = None for t, targ in enumerate(target): found = findInGetpot(trees, target[:t + 1]) if found is not None: foundSoFar = found else: break # if starting from scratch ... if foundSoFar is None: base = TreeStructure.InputNode(target[0]) tree = TreeStructure.InputTree(base) trees.append(tree) foundSoFar = [base] t = 1 # add missing nodes current = foundSoFar[-1] for a, addTarg in enumerate(target[t:]): new = TreeStructure.InputNode(addTarg) current.append(new) if t + a == len(target) - 1: new.text = str(value) current = new return trees
def localInitialize(self): """ Will perform all initialization specific to this Sampler. For instance, creating an empty container to hold the identified surface points, error checking the optionally provided solution export and other preset values, and initializing the limit surface Post-Processor used by this sampler. @ In, None @ Out, None """ if len(self.hybridStrategyToApply.keys()) > 0: hybridlistoflist = [] for cnt, preckey in enumerate(self.hybridStrategyToApply.keys()): hybridsampler = self.hybridStrategyToApply[preckey] hybridlistoflist.append([]) hybridsampler.initialize() self.hybridNumberSamplers *= hybridsampler.limit while hybridsampler.amIreadyToProvideAnInput(): hybridsampler.counter +=1 hybridsampler.localGenerateInput(None,None) hybridsampler.inputInfo['prefix'] = hybridsampler.counter hybridlistoflist[cnt].append(copy.deepcopy(hybridsampler.inputInfo)) if self.hybridNumberSamplers > 0: self.raiseAMessage('Number of Hybrid Samples are ' + str(self.hybridNumberSamplers) + '!') hybridNumber = self.hybridNumberSamplers combinations = list(itertools.product(*hybridlistoflist)) else: hybridNumber = 1 self.TreeInfo = {} for precSample in range(hybridNumber): elm = ETS.HierarchicalNode(self.messageHandler,self.name + '_' + str(precSample+1)) elm.add('name', self.name + '_'+ str(precSample+1)) elm.add('startTime', str(0.0)) # Initialize the endTime to be equal to the start one... # It will modified at the end of each branch elm.add('endTime', str(0.0)) elm.add('runEnded',False) elm.add('running',True) elm.add('queue',False) # if preconditioned DET, add the sampled from hybridsampler samplers if self.hybridNumberSamplers > 0: elm.add('hybridsamplerCoordinate', combinations[precSample]) for point in combinations[precSample]: for epistVar, val in point['SampledVars'].items(): self.epistemicVariables[epistVar][elm.get('name')] = val # The dictionary branchedLevel is stored in the xml tree too. That's because # the advancement of the thresholds must follow the tree structure elm.add('branchedLevel', self.branchedLevel[0]) # Here it is stored all the info regarding the DET => we create the info for all the # branchings and we store them self.TreeInfo[self.name + '_' + str(precSample+1)] = ETS.HierarchicalTree(self.messageHandler,elm) for key in self.branchProbabilities.keys(): #kk = self.toBeSampled.values().index(key) #self.branchValues[key] = [self.distDict[self.toBeSampled.keys()[self.toBeSampled.values().index(key)]].ppf(float(self.branchProbabilities[key][index])) for index in range(len(self.branchProbabilities[key]))] self.branchValues[key] = [self.distDict[key].ppf(float(self.branchProbabilities[key][index])) for index in range(len(self.branchProbabilities[key]))] for key in self.branchValues.keys(): #kk = self.toBeSampled.values().index(key) #self.branchProbabilities[key] = [self.distDict[self.toBeSampled.keys()[self.toBeSampled.values().index(key)]].cdf(float(self.branchValues[key][index])) for index in range(len(self.branchValues[key]))] self.branchProbabilities[key] = [self.distDict[key].cdf(float(self.branchValues[key][index])) for index in range(len(self.branchValues[key]))] self.limit = sys.maxsize
def ExternalXMLread(self,externalXMLFile,externalXMLNode,xmlFileName=None): """ parses the external xml input file @ In, externalXMLFile, string, the filename for the external xml file that will be loaded @ In, externalXMLNode, string, decribes which node will be loaded to raven input file @ In, xmlFileName, string, optional, the raven input file name @ Out, externalElemment, xml.etree.ElementTree.Element, object that will be added to the current tree of raven input """ #TODO make one for getpot too if '~' in externalXMLFile: externalXMLFile = os.path.expanduser(externalXMLFile) if not os.path.isabs(externalXMLFile): if xmlFileName == None: self.raiseAnError(IOError,'Relative working directory requested but input xmlFileName is None.') xmlDirectory = os.path.dirname(os.path.abspath(xmlFileName)) externalXMLFile = os.path.join(xmlDirectory,externalXMLFile) if os.path.exists(externalXMLFile): externalTree = TreeStructure.parse(externalXMLFile) externalElement = externalTree.getroot() if externalElement.tag != externalXMLNode: self.raiseAnError(IOError,'The required node is: ' + externalXMLNode + 'is different from the provided external xml type: ' + externalElement.tag) else: self.raiseAnError(IOError,'The external xml input file ' + externalXMLFile + ' does not exist!') return externalElement
#both are done return same,msg try: bl = next(genB) except StopIteration: return False,msg + ['file '+str(a)+' has more lines than '+str(b)] if al.rstrip('\n\r') != bl.rstrip('\n\r'): same = False print('al '+repr(al)+" != bl "+repr(bl)) msg += ['line '+str(i)+' is not the same!'] #first test XML to XML print('Testing XML to XML ...') tree = TS.parse(open(os.path.join('parse','example_xml.xml'),'r')) strTree = TS.tostring(tree) fname = os.path.join('parse','fromXmltoXML.xml') open(fname,'w').write(toString(strTree)) same,msg = checkSameFile(open(fname,'r'),open(os.path.join('gold',fname),'r')) if same: results['passed']+=1 print(' ... passed!') else: results['failed']+=1 print(' ... failures in XML to XML:') print(' ',msg[0]) getpot = open(os.path.join('parse','example_getpot.i'),'r')
def XMLread(self,xmlNode,runInfoSkip = set(),xmlFilename=None): """ parses the xml input file, instances the classes need to represent all objects in the simulation @ In, xmlNode, ElementTree.Element, xml node to read in @ In, runInfoSkip, set, optional, nodes to skip @ In, xmlFilename, string, optional, xml filename for relative directory @ Out, None """ #TODO update syntax to note that we read InputTrees not XmlTrees unknownAttribs = utils.checkIfUnknowElementsinList(['printTimeStamps','verbosity','color','profile'],list(xmlNode.attrib.keys())) if len(unknownAttribs) > 0: errorMsg = 'The following attributes are unknown:' for element in unknownAttribs: errorMsg += ' ' + element self.raiseAnError(IOError,errorMsg) self.verbosity = xmlNode.attrib.get('verbosity','all').lower() if 'printTimeStamps' in xmlNode.attrib.keys(): self.raiseADebug('Setting "printTimeStamps" to',xmlNode.attrib['printTimeStamps']) self.messageHandler.setTimePrint(xmlNode.attrib['printTimeStamps']) if 'color' in xmlNode.attrib.keys(): self.raiseADebug('Setting color output mode to',xmlNode.attrib['color']) self.messageHandler.setColor(xmlNode.attrib['color']) if 'profile' in xmlNode.attrib.keys(): thingsToProfile = list(p.strip().lower() for p in xmlNode.attrib['profile'].split(',')) if 'jobs' in thingsToProfile: self.jobHandler.setProfileJobs(True) self.messageHandler.verbosity = self.verbosity runInfoNode = xmlNode.find('RunInfo') if runInfoNode is None: self.raiseAnError(IOError,'The RunInfo node is missing!') self.__readRunInfo(runInfoNode,runInfoSkip,xmlFilename) ### expand variable groups before continuing ### ## build variable groups ## varGroupNode = xmlNode.find('VariableGroups') # init, read XML for variable groups if varGroupNode is not None: varGroups = xmlUtils.readVariableGroups(varGroupNode,self.messageHandler,self) else: varGroups={} # read other nodes for child in xmlNode: if child.tag=='VariableGroups': continue #we did these before the for loop if child.tag in list(self.whichDict.keys()): self.raiseADebug('-'*2+' Reading the block: {0:15}'.format(str(child.tag))+2*'-') Class = child.tag if len(child.attrib.keys()) == 0: globalAttributes = {} else: globalAttributes = child.attrib #if 'verbosity' in globalAttributes.keys(): self.verbosity = globalAttributes['verbosity'] if Class not in ['RunInfo','OutStreams'] and "returnInputParameter" in self.addWhatDict[Class].__dict__: paramInput = self.addWhatDict[Class].returnInputParameter() paramInput.parseNode(child) for childChild in paramInput.subparts: childName = childChild.getName() if "name" not in childChild.parameterValues: self.raiseAnError(IOError,'not found name attribute for '+childName +' in '+Class) name = childChild.parameterValues["name"] if "needsRunInfo" in self.addWhatDict[Class].__dict__: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self.runInfoDict,self) else: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self) self.whichDict[Class][name].handleInput(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes) elif Class != 'RunInfo': for childChild in child: subType = childChild.tag if 'name' in childChild.attrib.keys(): name = childChild.attrib['name'] self.raiseADebug('Reading type '+str(childChild.tag)+' with name '+name) #place the instance in the proper dictionary (self.whichDict[Type]) under his name as key, #the type is the general class (sampler, data, etc) while childChild.tag is the sub type #if name not in self.whichDict[Class].keys(): self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self) if Class != 'OutStreams': if name not in self.whichDict[Class].keys(): if "needsRunInfo" in self.addWhatDict[Class].__dict__: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self.runInfoDict,self) else: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self) else: self.raiseAnError(IOError,'Redundant naming in the input for class '+Class+' and name '+name) else: if name not in self.whichDict[Class][subType].keys(): self.whichDict[Class][subType][name] = self.addWhatDict[Class][subType].returnInstance(childChild.tag,self) else: self.raiseAnError(IOError,'Redundant naming in the input for class '+Class+' and sub Type'+subType+' and name '+name) #now we can read the info for this object #if globalAttributes and 'verbosity' in globalAttributes.keys(): localVerbosity = globalAttributes['verbosity'] #else : localVerbosity = self.verbosity if Class != 'OutStreams': self.whichDict[Class][name].readXML(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes) else: self.whichDict[Class][subType][name].readXML(childChild, self.messageHandler, globalAttributes=globalAttributes) else: self.raiseAnError(IOError,'not found name attribute for one '+Class) else: #tag not in whichDict, check if it's a documentation tag if child.tag not in ['TestInfo']: self.raiseAnError(IOError,'<'+child.tag+'> is not among the known simulation components '+repr(child)) # If requested, duplicate input # ###NOTE: All substitutions to the XML input tree should be done BEFORE this point!! if self.runInfoDict.get('printInput',False): fileName = os.path.join(self.runInfoDict['WorkingDir'],self.runInfoDict['printInput']) self.raiseAMessage('Writing duplicate input file:',fileName) outFile = open(fileName,'w') outFile.writelines(utils.toString(TreeStructure.tostring(xmlNode))+'\n') #\n for no-end-of-line issue outFile.close() if not set(self.stepSequenceList).issubset(set(self.stepsDict.keys())): self.raiseAnError(IOError,'The step list: '+str(self.stepSequenceList)+' contains steps that have not been declared: '+str(list(self.stepsDict.keys())))
inputFiles = [simulation.getDefaultInputFile()] else: inputFiles = sys.argv[1:] for i in range(len(inputFiles)): if not os.path.isabs(inputFiles[i]): inputFiles[i] = os.path.join(workingDir, inputFiles[i]) simulation.setInputFiles(inputFiles) #Parse the input #For future developers of this block, assure that useful, informative exceptions # are still thrown while parsing the XML tree. Otherwise any error made by # the developer or user might be obfuscated. for inputFile in inputFiles: try: tree = TS.parse(open(inputFile, 'r')) except TS.InputParsingError as e: print('\nInput Parsing error!', e, '\n') sys.exit(1) #except? riseanIOError('not possible to parse (xml based) the input file '+inputFile) if verbosity == 'debug': print('DRIVER', 'opened file ' + inputFile) root = tree.getroot() if root.tag != 'Simulation': e = IOError('The outermost block of the input file ' + inputFile + ' it is not Simulation') print('\nInput XML Error!', e, '\n') sys.exit(1)
if updateResults: results["fail"] += 1 return False else: if updateResults: results["pass"] += 1 return True ############## # Node Tests # ############## # TODO not complete! #test equivalency (eq, neq, hash) ## test all same are same a = TS.HierarchicalNode(mh,'rightTag',valuesIn={'attrib1':1,'attrib2':'2'},text='sampleText') b = TS.HierarchicalNode(mh,'rightTag',valuesIn={'attrib1':1,'attrib2':'2'},text='sampleText') checkSame('Equivalency of nodes ==:',a==b,True) checkSame('Equivalency of nodes !=:',a!=b,False) ## test different tag b = TS.HierarchicalNode(mh,'diffTag',valuesIn={'attrib1':1,'attrib2':'2'},text='sampleText') checkSame('Inequivalent tag ==:',a==b,False) checkSame('Inequivalent tag !=:',a!=b,True) ## test different attribute name b = TS.HierarchicalNode(mh,'rightTag',valuesIn={'attrib3':1,'attrib2':'2'},text='sampleText') checkSame('Inequivalent value name ==:',a==b,False) checkSame('Inequivalent value name !=:',a!=b,True) ## test different attribute value
def localGenerateInput(self, model, myInput): """ Function to select the next most informative point for refining the limit surface search. After this method is called, the self.inputInfo should be ready to be sent to the model @ In, model, model instance, an instance of a model @ In, myInput, list, a list of the original needed inputs for the model (e.g. list of files, etc.) @ Out, None """ if self.startAdaptive == True and self.adaptiveReady == True: LimitSurfaceSearch.localGenerateInput(self, model, myInput) #the adaptive sampler created the next point sampled vars #find the closest branch if self.hybridDETstrategy is not None: closestBranch, cdfValues, treer = self._checkClosestBranch() else: closestBranch, cdfValues = self._checkClosestBranch() if closestBranch is None: self.raiseADebug( 'An usable branch for next candidate has not been found => create a parallel branch!' ) # add pbthresholds in the grid investigatedPoint = {} for key, value in cdfValues.items(): try: ind = utils.first( np.atleast_1d( np.asarray(self.branchProbabilities[key]) <= value ).nonzero())[-1] except (IndexError, ValueError): ind = 0 if value not in self.branchProbabilities[key]: self.branchProbabilities[key].insert(ind, value) self.branchValues[key].insert( ind, self.distDict[key].ppf(value)) investigatedPoint[key] = value # collect investigated point self.investigatedPoints.append(investigatedPoint) if closestBranch: info = self._retrieveBranchInfo(closestBranch) self._constructEndInfoFromBranch(model, myInput, info, cdfValues) else: # create a new tree, since there are no branches that are close enough to the adaptive request elm = ETS.HierarchicalNode( self.messageHandler, self.name + '_' + str(len(self.TreeInfo.keys()) + 1)) elm.add('name', self.name + '_' + str(len(self.TreeInfo.keys()) + 1)) elm.add('startTime', 0.0) # Initialize the endTime to be equal to the start one... # It will modified at the end of each branch elm.add('endTime', 0.0) elm.add('runEnded', False) elm.add('running', True) elm.add('queue', False) elm.add('completedHistory', False) branchedLevel = {} for key, value in cdfValues.items(): branchedLevel[key] = utils.first( np.atleast_1d( np.asarray(self.branchProbabilities[key]) == value).nonzero())[-1] # The dictionary branchedLevel is stored in the xml tree too. That's because # the advancement of the thresholds must follow the tree structure elm.add('branchedLevel', branchedLevel) if self.hybridDETstrategy is not None and not self.foundEpistemicTree: # adaptive hybrid DET and not found a tree in the epistemic space # take the first tree and modify the hybridsamplerCoordinate hybridSampled = copy.deepcopy( utils.first(self.TreeInfo.values()).getrootnode().get( 'hybridsamplerCoordinate')) for hybridStrategy in hybridSampled: for key in self.epistemicVariables.keys(): if key in hybridStrategy['SampledVars'].keys(): self.raiseADebug("epistemic var " + str(key) + " value = " + str(self.values[key])) hybridStrategy['SampledVars'][key] = copy.copy( self.values[key]) hybridStrategy['SampledVarsPb'][ key] = self.distDict[key].pdf( self.values[key]) hybridStrategy['prefix'] = len( self.TreeInfo.values()) + 1 # TODO: find a strategy to recompute the probability weight here (for now == PointProbability) hybridStrategy['PointProbability'] = reduce( mul, self.inputInfo['SampledVarsPb'].values()) hybridStrategy['ProbabilityWeight'] = reduce( mul, self.inputInfo['SampledVarsPb'].values()) elm.add('hybridsamplerCoordinate', hybridSampled) self.inputInfo.update({ 'ProbabilityWeight-' + key.strip(): value for key, value in self.inputInfo['SampledVarsPb'].items() }) # Here it is stored all the info regarding the DET => we create the info for all the branchings and we store them self.TreeInfo[self.name + '_' + str(len(self.TreeInfo.keys()) + 1)] = ETS.HierarchicalTree( self.messageHandler, elm) self._createRunningQueueBeginOne( self.TreeInfo[self.name + '_' + str(len(self.TreeInfo.keys()))], branchedLevel, model, myInput) return DynamicEventTree.localGenerateInput(self, model, myInput)
def _constructEndInfoFromBranch(self, model, myInput, info, cdfValues): """ Method to construct the end information from the 'info' inputted @ In, model, Models object, the model that is used to explore the input space (e.g. a code, like RELAP-7) @ In, myInput, list, list of inputs for the Models object (passed through the Steps XML block) @ In, info, dict, dictionary of information at the end of a branch (information collected by the method _retrieveBranchInfo) @ In, cdfValues, dict, dictionary of CDF thresholds reached by the branch that just ended. @ Out, None """ endInfo = info['parentNode'].get('endInfo') del self.inputInfo self.counter += 1 self.branchCountOnLevel = info['actualBranchOnLevel'] + 1 # Get Parent node name => the branch name is creating appending to this name a comma and self.branchCountOnLevel counter rname = info['parentNode'].get('name') + '-' + str( self.branchCountOnLevel) info['parentNode'].add('completedHistory', False) self.raiseADebug(str(rname)) bcnt = self.branchCountOnLevel while info['parentNode'].isAnActualBranch(rname): bcnt += 1 rname = info['parentNode'].get('name') + '-' + str(bcnt) # create a subgroup that will be appended to the parent element in the xml tree structure subGroup = ETS.HierarchicalNode(self.messageHandler, rname) subGroup.add('parent', info['parentNode'].get('name')) subGroup.add('name', rname) self.raiseADebug('cond pb = ' + str(info['parentNode'].get('conditionalPbr'))) condPbC = float(info['parentNode'].get('conditionalPbr')) # Loop over branchChangedParams (events) and start storing information, # such as conditional pb, variable values, into the xml tree object branchChangedParamValue = [] branchChangedParamPb = [] branchParams = [] if endInfo: for key in endInfo['branchChangedParams'].keys(): branchParams.append(key) branchChangedParamPb.append(endInfo['branchChangedParams'][key] ['associatedProbability'][0]) branchChangedParamValue.append( endInfo['branchChangedParams'][key]['oldValue'][0]) subGroup.add('branchChangedParam', branchParams) subGroup.add('branchChangedParamValue', branchChangedParamValue) subGroup.add('branchChangedParamPb', branchChangedParamPb) else: pass # add conditional probability subGroup.add('conditionalPbr', condPbC) # add initiator distribution info, start time, etc. subGroup.add('startTime', info['parentNode'].get('endTime')) # initialize the endTime to be equal to the start one... It will modified at the end of this branch subGroup.add('endTime', info['parentNode'].get('endTime')) # add the branchedLevel dictionary to the subgroup # branch calculation info... running, queue, etc are set here subGroup.add('runEnded', False) subGroup.add('running', False) subGroup.add('queue', True) subGroup.add('completedHistory', False) # Append the new branch (subgroup) info to the parentNode in the tree object info['parentNode'].appendBranch(subGroup) # Fill the values dictionary that will be passed into the model in order to create an input # In this dictionary the info for changing the original input is stored self.inputInfo = { 'prefix': rname, 'endTimeStep': info['parentNode'].get('actualEndTimeStep'), 'branchChangedParam': subGroup.get('branchChangedParam'), 'branchChangedParamValue': subGroup.get('branchChangedParamValue'), 'conditionalPb': subGroup.get('conditionalPbr'), 'startTime': info['parentNode'].get('endTime'), 'RAVEN_parentID': subGroup.get('parent'), 'RAVEN_isEnding': True } # add the newer branch name to the map self.rootToJob[rname] = self.rootToJob[subGroup.get('parent')] # check if it is a preconditioned DET sampling, if so add the relative information # it exists only in case an hybridDET strategy is activated precSampled = info['parentNode'].get('hybridsamplerCoordinate') if precSampled: self.inputInfo['hybridsamplerCoordinate'] = copy.deepcopy( precSampled) subGroup.add('hybridsamplerCoordinate', copy.copy(precSampled)) # The probability Thresholds are stored here in the cdfValues dictionary... We are sure that they are whitin the ones defined in the grid # check is not needed self.inputInfo['initiatorDistribution'] = [ self.toBeSampled[key] for key in cdfValues.keys() ] self.inputInfo['PbThreshold'] = list(cdfValues.values()) self.inputInfo['ValueThreshold'] = [ self.distDict[key].ppf(value) for key, value in cdfValues.items() ] self.inputInfo['SampledVars'] = {} self.inputInfo['SampledVarsPb'] = {} for varname in self.standardDETvariables: self.inputInfo['SampledVars'][varname] = self.distDict[ varname].ppf(cdfValues[varname]) self.inputInfo['SampledVarsPb'][varname] = cdfValues[varname] # constant variables self._constantVariables() if precSampled: for precSample in precSampled: self.inputInfo['SampledVars'].update(precSample['SampledVars']) self.inputInfo['SampledVarsPb'].update( precSample['SampledVarsPb']) self.inputInfo['PointProbability'] = reduce( mul, self.inputInfo['SampledVarsPb'].values()) * subGroup.get( 'conditionalPbr') self.inputInfo['ProbabilityWeight'] = self.inputInfo[ 'PointProbability'] self.inputInfo.update({ 'ProbabilityWeight-' + key.strip(): value for key, value in self.inputInfo['SampledVarsPb'].items() }) # add additional edits if needed model.getAdditionalInputEdits(self.inputInfo) # Add the new input path into the RunQueue system newInputs = {'args': [str(self.type)], 'kwargs': dict(self.inputInfo)} self.RunQueue['queue'].append(newInputs) self.RunQueue['identifiers'].append(self.inputInfo['prefix']) for key, value in self.inputInfo.items(): subGroup.add(key, copy.copy(value)) if endInfo: subGroup.add('endInfo', copy.deepcopy(endInfo))
def _createRunningQueueBranch(self,model,myInput,forceEvent=False): """ Method to generate the running internal queue right after a branch occurred It generates the the information to insatiate the branches' continuation of the Deterministic Dynamic Event Tree @ In, model, Models object, the model that is used to explore the input space (e.g. a code, like RELAP-7) @ In, myInput, list, list of inputs for the Models object (passed through the Steps XML block) @ In, forceEvent, bool, if True the events are forced to happen (basically, the "unchanged event" is not created at all) @ Out, None """ # The first DET calculation branch has already been run' # Start the manipulation: # Pop out the last endInfo information and the branchedLevel branchedLevelParent = self.branchedLevel.pop(0) endInfo = self.endInfo.pop(0) self.branchCountOnLevel = 0 #? # n_branches = number of branches need to be run nBranches = endInfo['n_branches'] # Check if the distribution that just triggered hitted the last probability threshold . # In case we create a number of branches = endInfo['n_branches'] - 1 => the branch in # which the event did not occur is not going to be tracked if branchedLevelParent[endInfo['branchDist']] >= len(self.branchProbabilities[endInfo['branchDist']]): self.raiseADebug('Branch ' + endInfo['parentNode'].get('name') + ' hit last Threshold for distribution ' + endInfo['branchDist']) self.raiseADebug('Branch ' + endInfo['parentNode'].get('name') + ' is dead end.') self.branchCountOnLevel = 1 nBranches -= 1 else: if forceEvent == True: self.branchCountOnLevel = 1 nBranches -= 1 # Loop over the branches for which the inputs must be created for _ in range(nBranches): del self.inputInfo self.counter += 1 self.branchCountOnLevel += 1 branchedLevel = copy.deepcopy(branchedLevelParent) # Get Parent node name => the branch name is creating appending to this name a comma and self.branchCountOnLevel counter rname = endInfo['parentNode'].get('name') + '-' + str(self.branchCountOnLevel) # create a subgroup that will be appended to the parent element in the xml tree structure subGroup = ETS.HierarchicalNode(self.messageHandler,rname.encode()) subGroup.add('parent', endInfo['parentNode'].get('name')) subGroup.add('name', rname) subGroup.add('completedHistory', False) # condPbUn = conditional probability event not occur # condPbC = conditional probability event/s occur/s condPbUn = 0.0 condPbC = 0.0 # Loop over branchChangedParams (events) and start storing information, # such as conditional pb, variable values, into the xml tree object branchChangedParamValue = [] branchChangedParamPb = [] branchParams = [] #subGroup.add('branchChangedParam',endInfo['branchChangedParams'].keys()) for key in endInfo['branchChangedParams'].keys(): #subGroup.add('branchChangedParam',key) branchParams.append(key) if self.branchCountOnLevel != 1: branchChangedParamValue.append(endInfo['branchChangedParams'][key]['actualValue'][self.branchCountOnLevel-2]) branchChangedParamPb.append(endInfo['branchChangedParams'][key]['associatedProbability'][self.branchCountOnLevel-2]) #subGroup.add('branchChangedParamValue',endInfo['branchChangedParams'][key]['actualValue'][self.branchCountOnLevel-2]) #subGroup.add('branchChangedParamPb',endInfo['branchChangedParams'][key]['associatedProbability'][self.branchCountOnLevel-2]) #condPbC.append(endInfo['branchChangedParams'][key]['changedConditionalPb'][self.branchCountOnLevel-2]) condPbC = condPbC + endInfo['branchChangedParams'][key]['changedConditionalPb'][self.branchCountOnLevel-2] subGroup.add('happenedEvent',True) else: subGroup.add('happenedEvent',endInfo['parentNode'].get('happenedEvent')) branchChangedParamValue.append(endInfo['branchChangedParams'][key]['oldValue']) branchChangedParamPb.append(endInfo['branchChangedParams'][key]['unchangedPb']) #subGroup.add('branchChangedParamValue',endInfo['branchChangedParams'][key]['oldValue']) #subGroup.add('branchChangedParamPb',endInfo['branchChangedParams'][key]['unchangedPb']) #condPbUn.append(endInfo['branchChangedParams'][key]['unchangedConditionalPb']) condPbUn = condPbUn + endInfo['branchChangedParams'][key]['unchangedConditionalPb'] subGroup.add('branchChangedParam',branchParams) # add conditional probability if self.branchCountOnLevel != 1: subGroup.add('conditionalPbr',condPbC) subGroup.add('branchChangedParamValue',branchChangedParamValue) subGroup.add('branchChangedParamPb',branchChangedParamPb) else: subGroup.add('conditionalPbr',condPbUn) subGroup.add('branchChangedParamValue',branchChangedParamValue) subGroup.add('branchChangedParamPb',branchChangedParamPb) # add initiator distribution info, start time, etc. subGroup.add('initiatorDistribution',self.toBeSampled[endInfo['branchDist']]) subGroup.add('startTime', endInfo['parentNode'].get('endTime')) # initialize the endTime to be equal to the start one... It will modified at the end of this branch subGroup.add('endTime', endInfo['parentNode'].get('endTime')) # add the branchedLevel dictionary to the subgroup if self.branchCountOnLevel != 1: branchedLevel[endInfo['branchDist']] = branchedLevel[endInfo['branchDist']] - 1 # branch calculation info... running, queue, etc are set here subGroup.add('runEnded',False) subGroup.add('running',False) subGroup.add('queue',True) # subGroup.set('restartFileRoot',endInfo['restartRoot']) # Append the new branch (subgroup) info to the parentNode in the tree object endInfo['parentNode'].appendBranch(subGroup) # Fill the values dictionary that will be passed into the model in order to create an input # In this dictionary the info for changing the original input is stored self.inputInfo = {'prefix':rname.encode(),'endTimeStep':endInfo['endTimeStep'], 'branchChangedParam':subGroup.get('branchChangedParam'), 'branchChangedParamValue':subGroup.get('branchChangedParamValue'), 'conditionalPb':subGroup.get('conditionalPbr'), 'startTime':endInfo['parentNode'].get('endTime'), 'parentID':subGroup.get('parent')} self.inputInfo['happenedEvent'] = subGroup.get('happenedEvent') # add additional edits if needed model.getAdditionalInputEdits(self.inputInfo) # add the newer branch name to the map self.rootToJob[rname] = self.rootToJob[subGroup.get('parent')] # check if it is a preconditioned DET sampling, if so add the relative information precSampled = endInfo['parentNode'].get('hybridsamplerCoordinate') if precSampled: self.inputInfo['hybridsamplerCoordinate'] = copy.deepcopy(precSampled) subGroup.add('hybridsamplerCoordinate', precSampled) # Check if the distribution that just triggered hitted the last probability threshold . # In this case there is not a probability threshold that needs to be added in the input # for this particular distribution if not (branchedLevel[endInfo['branchDist']] >= len(self.branchProbabilities[endInfo['branchDist']])): self.inputInfo['initiatorDistribution'] = [self.toBeSampled[endInfo['branchDist']]] self.inputInfo['PbThreshold' ] = [self.branchProbabilities[endInfo['branchDist']][branchedLevel[endInfo['branchDist']]]] self.inputInfo['ValueThreshold' ] = [self.branchValues[endInfo['branchDist']][branchedLevel[endInfo['branchDist']]]] # For the other distributions, we put the unbranched thresholds # Before adding these thresholds, check if the keyword 'initiatorDistribution' is present... # (In the case the previous if statement is true, this keyword is not present yet # Add it otherwise if not ('initiatorDistribution' in self.inputInfo.keys()): self.inputInfo['initiatorDistribution' ] = [] self.inputInfo['PbThreshold' ] = [] self.inputInfo['ValueThreshold' ] = [] # Add the unbranched thresholds for key in self.branchProbabilities.keys(): if not (key in self.toBeSampled[endInfo['branchDist']]) and (branchedLevel[key] < len(self.branchProbabilities[key])): self.inputInfo['initiatorDistribution'].append(self.toBeSampled[key.encode()]) for key in self.branchProbabilities.keys(): if not (key in self.toBeSampled[endInfo['branchDist']]) and (branchedLevel[key] < len(self.branchProbabilities[key])): self.inputInfo['PbThreshold' ].append(self.branchProbabilities[key][branchedLevel[key]]) self.inputInfo['ValueThreshold'].append(self.branchValues[key][branchedLevel[key]]) self.inputInfo['SampledVars'] = {} self.inputInfo['SampledVarsPb'] = {} for varname in self.standardDETvariables: self.inputInfo['SampledVars'][varname] = self.branchValues[varname][branchedLevel[varname]] self.inputInfo['SampledVarsPb'][varname] = self.branchProbabilities[varname][branchedLevel[varname]] #self.inputInfo['SampledVars'][varname] = self.branchValues[self.toBeSampled[varname]][branchedLevel[self.toBeSampled[varname]]] #self.inputInfo['SampledVarsPb'][varname] = self.branchProbabilities[self.toBeSampled[varname]][branchedLevel[self.toBeSampled[varname]]] self._constantVariables() if precSampled: for precSample in precSampled: self.inputInfo['SampledVars' ].update(precSample['SampledVars']) self.inputInfo['SampledVarsPb'].update(precSample['SampledVarsPb']) self.inputInfo['PointProbability' ] = reduce(mul, self.inputInfo['SampledVarsPb'].values())*subGroup.get('conditionalPbr') self.inputInfo['ProbabilityWeight'] = self.inputInfo['PointProbability' ] # Call the model function "createNewInput" with the "values" dictionary just filled. # Add the new input path into the RunQueue system self.RunQueue['queue'].append(model.createNewInput(myInput,self.type,**self.inputInfo)) self.RunQueue['identifiers'].append(self.inputInfo['prefix']) for key,value in self.inputInfo.items(): subGroup.add(key,value) popped = endInfo.pop('parentNode') subGroup.add('endInfo',copy.deepcopy(endInfo)) endInfo['parentNode'] = popped del branchedLevel
inputFiles = [simulation.getDefaultInputFile()] else: inputFiles = sys.argv[1:] for i in range(len(inputFiles)): if not os.path.isabs(inputFiles[i]): inputFiles[i] = os.path.join(workingDir,inputFiles[i]) simulation.setInputFiles(inputFiles) #Parse the input #For future developers of this block, assure that useful, informative exceptions # are still thrown while parsing the XML tree. Otherwise any error made by # the developer or user might be obfuscated. for inputFile in inputFiles: try: tree = TS.parse(file(inputFile,'r')) except TS.InputParsingError as e: print('\nInput Parsing error!',e,'\n') sys.exit(1) #except? riseanIOError('not possible to parse (xml based) the input file '+inputFile) if verbosity=='debug': print('DRIVER','opened file '+inputFile) root = tree.getroot() if root.tag != 'Simulation': e=IOError('The outermost block of the input file '+inputFile+' it is not Simulation') print('\nInput XML Error!',e,'\n') sys.exit(1) # call the function to load the external xml files into the input tree
return same, msg try: bl = next(genB) except StopIteration: return False, msg + [ 'file ' + str(a) + ' has more lines than ' + str(b) ] if al.rstrip('\n\r') != bl.rstrip('\n\r'): same = False print('al ' + repr(al) + " != bl " + repr(bl)) msg += ['line ' + str(i) + ' is not the same!'] #first test XML to XML print('Testing XML to XML ...') tree = TS.parse(open(os.path.join('parse', 'example_xml.xml'), 'r')) strTree = TS.tostring(tree) fname = os.path.join('parse', 'fromXmltoXML.xml') open(fname, 'w').write(toString(strTree)) same, msg = checkSameFile(open(fname, 'r'), open(os.path.join('gold', fname), 'r')) if same: results['passed'] += 1 print(' ... passed!') else: results['failed'] += 1 print(' ... failures in XML to XML:') print(' ', msg[0]) print('Results:', list('%s: %i' % (k, v) for k, v in results.items())) sys.exit(results['failed'])
def getpotToInputTree(getpot): """ Converts getpot input to RAVEN InputTree structure @ In, getpot, string, string to read in (with newlines, could be open file object) @ Out, tree, TreeStructure.NodeTree, tree with information """ getpot = preprocessGetpot(getpot) roots = [] parentNodes = [ ] # stack trace of parent nodes, as [First, Second, Third] nested currentNode = None # current node having objects added to it # track multiline vectors multilineKey = None # attribute name multilineValue = None # multiline value multilineIndicator = None # indicator to open/close, either ' or " for line in getpot: if multilineValue is not None: if multilineIndicator in line: # the multiline is done, so close and record it value, leftover = line.split(multilineIndicator, maxsplit=1) addSpace = ' ' if value.strip() else '' multilineValue += addSpace + value.strip() + multilineIndicator # set up to close entry attribute = multilineKey value = multilineValue closeEntry = True # reset multilineKey = None multilineValue = None multilineIndicator = None else: # still open and not closed, so keep appending addSpace = ' ' if multilineValue[ -1] != multilineIndicator else '' multilineValue += addSpace + line.strip() closeEntry = False else: line = line.strip() #------------------ # starting new node if line.startswith( ('[./', '[')) and line.endswith(']') and line not in ('[../]', '[]'): #if child node, stash the parent for now if currentNode is not None: parentNodes.append(currentNode) currentNode = TreeStructure.InputNode(tag=line.strip('[]./')) closeEntry = False #------------------ # closing node elif line.startswith(('[../]', '[]')): if parentNodes: parentNodes[-1].append(currentNode) #make parent the active node currentNode = parentNodes.pop() else: #this is a root roots.append(currentNode) currentNode = None closeEntry = False #------------------ # attributes and values elif '=' in line: attribute, value = (x.strip() for x in line.split('=', maxsplit=1)) # TODO multilline, if "'" or '"' in line # ASSUME: both " and ' aren't used in the same line if any(x in line for x in multiIndicators): indicator = '\'' if '\'' in line else '\"' if line.count( indicator ) % 2 == 0: # NOTE this may be more complex than needed, can you really use 4, 6? # closes within line value = value.strip('\'\"') closeEntry = True else: # multiline multilineKey = attribute multilineValue = value multilineIndicator = indicator closeEntry = False # DO NOT continue, keep going until multiline is closed continue else: # single line entry with no vector representation value = value.strip() closeEntry = True #------------------ # we don't know what's going on here else: raise IOError('Unrecognized line syntax: "{}"'.format(line)) #------------------ # if the "value" if the "attribute" is closed, record the entry if closeEntry: if attribute in ( c.tag for c in currentNode.children): #currentNode.attrib: raise IOError( 'Multiple entries defined with same name "{a}"!'.format( a=attribute)) else: new = TreeStructure.InputNode(tag=attribute, text=value) currentNode.append(new) if multilineValue: raise IOError( 'There was a parsing error reading MOOSE input! Multiline attribute "{n}" opened by {i} but never closed!' .format(i=multilineIndicator, n=multilineKey)) return [TreeStructure.InputTree(root) for root in roots]