Beispiel #1
0
    def localInputAndChecks(self, xmlNode):
        """
      Function to read the portion of the xml input that belongs to this specialized class
      and initialize some stuff based on the inputs got
      @ In, xmlNode, xml.etree.ElementTree.Element, Xml element node
      @ Out, None
    """
        #Model._readMoreXML(self, xmlNode)
        paramInput = ExternalModel.getInputSpecification()()
        paramInput.parseNode(xmlNode)
        if 'ModuleToLoad' in paramInput.parameterValues:
            self.ModuleToLoad = paramInput.parameterValues['ModuleToLoad']
            moduleToLoadString, self.ModuleToLoad = utils.identifyIfExternalModelExists(
                self, self.ModuleToLoad, self.workingDir)
            # load the external module and point it to self.sim
            self.sim = utils.importFromPath(
                moduleToLoadString,
                self.messageHandler.getDesiredVerbosity(self) > 1)
        elif len(paramInput.parameterValues['subType'].strip()) > 0:
            # it is a plugin. Look for the type in the plugins class list
            if paramInput.parameterValues[
                    'subType'] not in ExternalModel.plugins.knownTypes():
                self.raiseAnError(
                    IOError, 'The "subType" named "' +
                    paramInput.parameterValues['subType'] +
                    '" does not belong to any ExternalModel plugin available. '
                    + 'Available plugins are "' +
                    ','.join(ExternalModel.plugins.knownTypes()))
            self.sim = ExternalModel.plugins.returnPlugin(
                "ExternalModel", paramInput.parameterValues['subType'], self)
        else:
            self.raiseAnError(
                IOError,
                '"ModuleToLoad" attribute or "subType" not provided for Model "ExternalModel" named "'
                + self.name + '"!')

        # check if there are variables and, in case, load them
        for child in paramInput.subparts:
            if child.getName() == 'variable':
                self.raiseAnError(
                    IOError,
                    '"variable" node included but has been depreciated!  Please list variables in a "variables" node instead.  Remove this message by Dec 2016.'
                )
            elif child.getName() == 'variables':
                if len(child.parameterValues) > 0:
                    self.raiseAnError(
                        IOError, 'the block ' + child.getName() + ' named ' +
                        child.value + ' should not have attributes!!!!!')
                for var in child.value.split(','):
                    var = var.strip()
                    self.modelVariableType[var] = None
        # adjust model-aware variables based on aliases
        self._replaceVariablesNamesWithAliasSystem(self.modelVariableType,
                                                   'inout')
        self.listOfRavenAwareVars.extend(self.modelVariableType.keys())
        # check if there are other information that the external module wants to load
        #TODO this needs to be converted to work with paramInput
        if '_readMoreXML' in dir(self.sim):
            self.sim._readMoreXML(self.initExtSelf, xmlNode)
Beispiel #2
0
 def _readMoreXML(self,xmlNode):
   """
     Function to read the portion of the xml input that belongs to this
     specialized class and initialize some variables based on the inputs
     received.
     @ In, xmlNode, xml.etree.ElementTree.Element, XML element node that represents the portion of the input that belongs to this class
     @ Out, None
   """
   if 'file' in xmlNode.attrib.keys():
     self.functionFile = xmlNode.attrib['file']
     # get the module to load and the filename without path
     moduleToLoadString, self.functionFile = utils.identifyIfExternalModelExists(self, self.functionFile, self.workingDir)
     # import the external function
     importedModule = utils.importFromPath(moduleToLoadString,self.messageHandler.getDesiredVerbosity(self)>1)
     if not importedModule:
       self.raiseAnError(IOError,'Failed to import the module '+moduleToLoadString+' supposed to contain the function: '+self.name)
     #here the methods in the imported file are brought inside the class
     for method in importedModule.__dict__.keys():
       if method in ['__residuumSign__','__residuumSign','residuumSign',
                     '__supportBoundingTest__','__supportBoundingTest','supportBoundingTest',
                     '__residuum__','__residuum','residuum','__gradient__','__gradient','gradient']:
         if method in ['__residuumSign__','__residuumSign','residuumSign']:
           self.__residuumSign                                =  importedModule.__dict__[method]
           self.__actionDictionary['residuumSign' ]           = self.__residuumSign
           self.__actionImplemented['residuumSign']           = True
         if method in ['__supportBoundingTest__','__supportBoundingTest','supportBoundingTest']:
           self.__supportBoundingTest                         =  importedModule.__dict__[method]
           self.__actionDictionary['supportBoundingTest' ]    = self.__supportBoundingTest
           self.__actionImplemented['supportBoundingTest']    = True
         if method in ['__residuum__','__residuum','residuum']:
           self.__residuum                                    =  importedModule.__dict__[method]
           self.__actionDictionary['residuum' ]               = self.__residuum
           self.__actionImplemented['residuum']               = True
         if method in ['__gradient__','__gradient','gradient']:
           self.__gradient                                    =  importedModule.__dict__[method]
           self.__actionDictionary['gradient']                = self.__gradient
           self.__actionImplemented['gradient']               = True
       else:
         #custom
         self.__actionDictionary[method]                    = importedModule.__dict__[method]
         self.__actionImplemented[method]                   = True
   else:
     self.raiseAnError(IOError,'No file name for the external function has been provided for external function '+self.name+' of type '+self.type)
   cnt = 0
   for child in xmlNode:
     if child.tag=='variable':
       execCommand('self.'+child.text+' = None',self=self)
       self.__inputVariables.append(child.text)
       cnt +=1
       if len(child.attrib.keys()) > 0:
         self.raiseAnError(IOError,'variable block in the definition of the function '+self.name + ' should not have any attribute!')
   if cnt == 0:
     self.raiseAnError(IOError,'not variable found in the definition of the function '+self.name)
Beispiel #3
0
 def _handleInput(self, paramInput):
     """
   Method to handle the External Function parameter input.
   @ In, paramInput, InputData.ParameterInput, the already parsed input.
   @ Out, None
 """
     self.functionFile = paramInput.parameterValues["file"]
     # get the module to load and the filename without path
     moduleToLoadString, self.functionFile = utils.identifyIfExternalModelExists(
         self, self.functionFile, self.workingDir)
     # import the external function
     importedModule = utils.importFromPath(
         moduleToLoadString,
         self.messageHandler.getDesiredVerbosity(self) > 1)
     if not importedModule:
         self.raiseAnError(
             IOError, 'Failed to import the module ' + moduleToLoadString +
             ' supposed to contain the function: ' + self.name)
     #here the methods in the imported file are brought inside the class
     for method, action in importedModule.__dict__.items():
         if method in [
                 '__residuumSign__', '__residuumSign', 'residuumSign',
                 '__supportBoundingTest__', '__supportBoundingTest',
                 'supportBoundingTest', '__residuum__', '__residuum',
                 'residuum', '__gradient__', '__gradient', 'gradient'
         ]:
             if method in [
                     '__residuumSign__', '__residuumSign', 'residuumSign'
             ]:
                 self.__actionDictionary['residuumSign'] = action
                 self.__actionImplemented['residuumSign'] = True
             if method in [
                     '__supportBoundingTest__', '__supportBoundingTest',
                     'supportBoundingTest'
             ]:
                 self.__actionDictionary['supportBoundingTest'] = action
                 self.__actionImplemented['supportBoundingTest'] = True
             if method in ['__residuum__', '__residuum', 'residuum']:
                 self.__actionDictionary['residuum'] = action
                 self.__actionImplemented['residuum'] = True
             if method in ['__gradient__', '__gradient', 'gradient']:
                 self.__actionDictionary['gradient'] = action
                 self.__actionImplemented['gradient'] = True
         else:
             #custom
             self.__actionDictionary[method] = action
             self.__actionImplemented[method] = True
     # get variables
     self.__inputVariables = paramInput.findFirst("variables").value
     # initialize variables
     for var in self.__inputVariables:
         execCommand('self.' + var + ' = None', self=self)
Beispiel #4
0
 def read_input(self, xml):
   """
     Sets settings from input file
     @ In, xml, xml.etree.ElementTree.Element, input from user
     @ Out, None
   """
   Placeholder.read_input(self, xml)
   # load module
   load_string, _ = utils.identifyIfExternalModelExists(self, self._source, self._workingDir)
   module = utils.importFromPath(load_string, True)
   if not module:
     raise IOError('Module "{}" for function "{}" was not found!'.format(self._source, self.name))
   # TODO do we need to set the var_names? self._var_names = _var_names
   self._set_callables(module)
Beispiel #5
0
 def localInputAndChecks(self, xmlNode):
     """
   Function to read the portion of the xml input that belongs to this specialized class
   and initialize some stuff based on the inputs got
   @ In, xmlNode, xml.etree.ElementTree.Element, Xml element node
   @ Out, None
 """
     #Model._readMoreXML(self, xmlNode)
     paramInput = ExternalModel.getInputSpecification()()
     paramInput.parseNode(xmlNode)
     if 'ModuleToLoad' in paramInput.parameterValues:
         self.ModuleToLoad = paramInput.parameterValues['ModuleToLoad']
         moduleToLoadString, self.ModuleToLoad = utils.identifyIfExternalModelExists(
             self, self.ModuleToLoad, self.workingDir)
     else:
         self.raiseAnError(
             IOError, 'ModuleToLoad not provided for module externalModule')
     # load the external module and point it to self.sim
     self.sim = utils.importFromPath(
         moduleToLoadString,
         self.messageHandler.getDesiredVerbosity(self) > 1)
     # check if there are variables and, in case, load them
     for child in paramInput.subparts:
         if child.getName() == 'variable':
             self.raiseAnError(
                 IOError,
                 '"variable" node included but has been depreciated!  Please list variables in a "variables" node instead.  Remove this message by Dec 2016.'
             )
         elif child.getName() == 'variables':
             if len(child.parameterValues) > 0:
                 self.raiseAnError(
                     IOError, 'the block ' + child.getName() + ' named ' +
                     child.value + ' should not have attributes!!!!!')
             for var in child.value.split(','):
                 var = var.strip()
                 self.modelVariableType[var] = None
     self.listOfRavenAwareVars.extend(self.modelVariableType.keys())
     # check if there are other information that the external module wants to load
     #TODO this needs to be converted to work with paramInput
     if '_readMoreXML' in dir(self.sim):
         self.sim._readMoreXML(self.initExtSelf, xmlNode)