Ejemplo n.º 1
0
    def addSBMLToCell(self,_modelFile,_modelName='',_cell=None,_stepSize=1.0,_initialConditions={},_coreModelName='',_modelPathNormalized='',_options=None):
        '''
        _options is a dictionary with the following keys:
        
        absolute - determines absolute tolerance default 1e-10
        relative - determines relative tolerance default 1e-5
        stiff - determines if using stiff solver or not default False
        '''
        import os
        import sys
        import CompuCell
        
        
        coreModelName=_modelName
        if coreModelName=='':
            coreModelName,ext=os.path.splitext(os.path.basename(_modelFile))
            
        
        modelPathNormalized=self.normalizePath(_modelFile)
        
        dict_attrib = CompuCell.getPyAttrib(_cell)
        
        sbmlDict={}
        if dict_attrib.has_key('SBMLSolver'):
            sbmlDict=dict_attrib['SBMLSolver']
        else:
            dict_attrib['SBMLSolver']=sbmlDict    
        
                
        from RoadRunnerPy import RoadRunnerPy         
        rr=RoadRunnerPy(_path=_modelFile)
        
        #setting stepSize
        rr.stepSize=_stepSize                    
        #loading SBML and LLVM-ing it           
        rr.loadSBML(_externalPath=modelPathNormalized)
        

        #storing rr instance in the cell dictionary
        sbmlDict[coreModelName]=rr
            
        #setting initial conditions - this has to be done after loadingSBML       
        for name,value in _initialConditions.iteritems():                                 
            try: # have to catch exceptions in case initial conditions contain "unsettable" entries such as reaction rate etc...
                rr.model[name]=value            
            except :                    
                pass
        # we are turning off dynamic python properties because rr is not used in the interactive mode.         
        # try:
            # rr.options.disablePythonDynamicProperties = True
        # except:
            # pass    
        
        
        # setting output results array size 
        rr.selections=[] # by default we do not request any output array at each intergration step
        
        if _options:    
            for name , value in _options.iteritems():
                # setattr(rr.simulateOptions,name,value)
                # print ' 1 name , value=',(name , value)
                # if name=='steps':
                    # print ' 1 name , value=',(name , value)
                    # continue
                
                try:
                    setattr(rr.getIntegrator(),name,value)
                except AttributeError:
                    setattr(rr.getIntegrator(),self.option_name_dict[name],value)
        else: # check for global options
            globalOptions=self.getSBMLGlobalOptions()
            if globalOptions:
                for name , value in globalOptions.iteritems():
                    # print ' 2 name , value=',(name , value)
                    # print 'name=',name,' value=',value
                    # if name=='steps':
                        # continue
                    try:
                        setattr(rr.getIntegrator(),name,value)
                    except (AttributeError,ValueError) as e:
                        setattr(rr.getIntegrator(),self.option_name_dict[name],value)
Ejemplo n.º 2
0
     # pass    
     
 # setting output results array size 
 rr.selections=[] # by default we do not request any output array at each intergration step
 
 # in case user passes simulate options we set the here        
 if _options:    
     for name , value in _options.iteritems():
         # setattr(rr.simulateOptions,name,value)
         # print ' 1 name , value=',(name , value)
         # if name=='steps':
             # print ' 1 name , value=',(name , value)
             # continue
         
         try:
             setattr(rr.getIntegrator(),name,value)
         except (AttributeError,ValueError) as e:
             setattr(rr.getIntegrator(),self.option_name_dict[name],value)
 else: # check for global options
     globalOptions=self.getSBMLGlobalOptions()
     if globalOptions:
         for name , value in globalOptions.iteritems():
             # print ' 2 name , value=',(name , value)
             # print 'name=',name,' value=',value
             # if name=='steps':
                 # continue
             try:
                 setattr(rr.getIntegrator(),name,value)
             except (AttributeError,ValueError) as e:
                 setattr(rr.getIntegrator(),self.option_name_dict[name],value)
             # setattr(rr.simulateOptions,name,value)
        for name,value in _initialConditions.iteritems():
            rr.model[name]=value
            
        try:
            rr.options.disablePythonDynamicProperties = True
        except:
            pass    
            
        # setting output results array size 
        rr.selections=[] # by default we do not request any output array at each intergration step
        
        # in case user passes simulate options we set the here        
        if _options:    
            for name , value in _options.iteritems():
                if name == "relative":
                    rr.getIntegrator().setValue('relative_tolerance',value)
                elif name == "absolute":
                    rr.getIntegrator().setValue('absolute_tolerance',value)   
                elif name == "stiff":
                    rr.getIntegrator().setValue('stiff',value)   
                else:
                    setattr(rr.simulateOptions,name,value)                    

        else: # check for global options
            globalOptions=self.getSBMLGlobalOptions()
            if globalOptions:
                for name , value in globalOptions.iteritems():
                    if name == "relative":
                        rr.getIntegrator().setValue('relative_tolerance',value)
                    elif name == "absolute":
                        rr.getIntegrator().setValue('absolute_tolerance',value)   
Ejemplo n.º 4
0
    def addSBMLToCell(self, _modelFile='', _modelName='', _cell=None, _stepSize=1.0, _initialConditions={},
                      _coreModelName='', _modelPathNormalized='', _options=None, _currentStateSBML=None):

        """
        Attaches RoadRunner SBML solver to a particular cell. The sbml solver is stored as an element
        of the cell's dictionary - cell.dict['SBMLSolver'][_modelName]. The function has a dual operation mode. 
        When user provides _currentStateSBML, _cell _modelName, _stepSize the addSBMLToCell function creates a clone 
        of a solver whose state is described by the _currentStateSBML . If _currentStateSBML is None then the new SBML solver 
        is being created,  SBML file (_modelFile) loaded and initial conditions are applied. It is important to always set 
        _stepSize to make sure that after calling timestep() fcn the solver advances appropriate delta time

        :param _modelFile {str}: name of the SBML file - can be relative path (e.g. Simulation/file.sbml) or absolute path 
        :param _modelName {str}: name of the model - this is a label used to store mode in the cell.dict['SBMLSolver'] dictionary  
        :param _cell {CellG object}: cc3d cell object 
        :param _stepSize {float}: time step- determines how much in "real" time units timestep() fcn advances SBML solver 
        :param _initialConditions {dict}: initial conditions dictionary
        :param _coreModelName {str}: deprecated, kept for backward compatibility reasons 
        :param _modelPathNormalized {str}: deprecated, kept for backward compatibility reasons: 
        :param _options {dict}: dictionary that currently only defines what type of ODE solver to choose. In the newer versions of RR this might be not necessary. The keys that are supported are the following: 
             

        absolute - determines absolute tolerance default 1e-10
        relative - determines relative tolerance default 1e-5
        stiff - determines if using stiff solver or not default False
        
        :param _currentStateSBML {str}: string representation  of the SBML representing current state of the solver. 
         
        :return: None 
        """

        import CompuCell

        coreModelName = _modelName
        if coreModelName == '':
            coreModelName, ext = os.path.splitext(os.path.basename(_modelFile))

        if not _modelFile:
            warnings.warn('\n\n\n _modelFile argument not provided to addSBMLToCell. This will prevent proper restart of the simulation'
                          'You may ignore this warning if you are not serializing simulation for future restarts', RuntimeWarning)

        modelPathNormalized = self.normalizePath(_modelFile)

        dict_attrib = CompuCell.getPyAttrib(_cell)

        sbmlDict = {}
        if dict_attrib.has_key('SBMLSolver'):
            sbmlDict = dict_attrib['SBMLSolver']
        else:
            dict_attrib['SBMLSolver'] = sbmlDict

        from RoadRunnerPy import RoadRunnerPy
        if _currentStateSBML is None:
            rr = RoadRunnerPy(_path=_modelFile)
            # setting stepSize
            rr.stepSize = _stepSize
            # loading SBML and LLVM-ing it
            rr.loadSBML(_externalPath=modelPathNormalized)

        else:
            rr = RoadRunnerPy(sbml=_currentStateSBML)
            # setting stepSize
            rr.stepSize = _stepSize

            # setting up paths - IMPORTANT FOR RESTARTING
            rr.path = _modelFile
            if os.path.exists(modelPathNormalized):
                rr.absPath = modelPathNormalized


        # storing rr instance in the cell dictionary
        sbmlDict[coreModelName] = rr

        # setting initial conditions - this has to be done after loadingSBML
        for name, value in _initialConditions.iteritems():
            try:  # have to catch exceptions in case initial conditions contain "unsettable" entries such as reaction rate etc...
                rr.model[name] = value
            except:
                pass
                # we are turning off dynamic python properties because rr is not used in the interactive mode.
                # rr.options.disablePythonDynamicProperties = True

        # setting output results array size 
        rr.selections = []  # by default we do not request any output array at each intergration step

        if _options:
            for name, value in _options.iteritems():

                try:
                    setattr(rr.getIntegrator(), name, value)
                except AttributeError:
                    setattr(rr.getIntegrator(), self.option_name_dict[name], value)
        else:

            # check for global options

            globalOptions = self.getSBMLGlobalOptions()
            if globalOptions:
                for name, value in globalOptions.iteritems():
                    try:
                        setattr(rr.getIntegrator(), name, value)
                    except (AttributeError, ValueError) as e:
                        setattr(rr.getIntegrator(), self.option_name_dict[name], value)
Ejemplo n.º 5
0
        CompuCellSetup.freeFloatingSBMLSimulator[_modelName] = rr

        # setting initial conditions - this has to be done after loadingSBML
        for name, value in _initialConditions.iteritems():
            rr.model[name] = value
            # rr.options.disablePythonDynamicProperties = True

        # setting output results array size 
        rr.selections = []  # by default we do not request any output array at each intergration step

        # in case user passes simulate options we set the here        
        if _options:
            for name, value in _options.iteritems():

                try:
                    setattr(rr.getIntegrator(), name, value)
                except (AttributeError, ValueError) as e:
                    setattr(rr.getIntegrator(), self.option_name_dict[name], value)
        else:
            # check for global options

            globalOptions = self.getSBMLGlobalOptions()
            if globalOptions:
                for name, value in globalOptions.iteritems():
                    # print ' 2 name , value=',(name , value)
                    # print 'name=',name,' value=',value
                    # if name=='steps':
                    # continue
                    try:
                        setattr(rr.getIntegrator(), name, value)
                    except (AttributeError, ValueError) as e:
Ejemplo n.º 6
0
    def addSBMLToCell(self,
                      _modelFile,
                      _modelName='',
                      _cell=None,
                      _stepSize=1.0,
                      _initialConditions={},
                      _coreModelName='',
                      _modelPathNormalized='',
                      _options=None):
        '''
        _options is a dictionary with the following keys:
        
        absolute - determines absolute tolerance default 1e-10
        relative - determines relative tolerance default 1e-5
        stiff - determines if using stiff solver or not default False
        '''
        import os
        import sys
        import CompuCell

        coreModelName = _modelName
        if coreModelName == '':
            coreModelName, ext = os.path.splitext(os.path.basename(_modelFile))

        modelPathNormalized = self.normalizePath(_modelFile)

        dict_attrib = CompuCell.getPyAttrib(_cell)

        sbmlDict = {}
        if dict_attrib.has_key('SBMLSolver'):
            sbmlDict = dict_attrib['SBMLSolver']
        else:
            dict_attrib['SBMLSolver'] = sbmlDict

        from RoadRunnerPy import RoadRunnerPy
        rr = RoadRunnerPy(_path=_modelFile)

        #setting stepSize
        rr.stepSize = _stepSize
        #loading SBML and LLVM-ing it
        rr.loadSBML(_externalPath=modelPathNormalized)

        #storing rr instance in the cell dictionary
        sbmlDict[coreModelName] = rr

        #setting initial conditions - this has to be done after loadingSBML
        for name, value in _initialConditions.iteritems():
            try:  # have to catch exceptions in case initial conditions contain "unsettable" entries such as reaction rate etc...
                rr.model[name] = value
            except:
                pass
        # we are turning off dynamic python properties because rr is not used in the interactive mode.
        # try:
        # rr.options.disablePythonDynamicProperties = True
        # except:
        # pass

        # setting output results array size
        rr.selections = [
        ]  # by default we do not request any output array at each intergration step

        if _options:
            for name, value in _options.iteritems():
                # setattr(rr.simulateOptions,name,value)
                # print ' 1 name , value=',(name , value)
                # if name=='steps':
                # print ' 1 name , value=',(name , value)
                # continue

                try:
                    setattr(rr.getIntegrator(), name, value)
                except AttributeError:
                    setattr(rr.getIntegrator(), self.option_name_dict[name],
                            value)
        else:  # check for global options
            globalOptions = self.getSBMLGlobalOptions()
            if globalOptions:
                for name, value in globalOptions.iteritems():
                    # print ' 2 name , value=',(name , value)
                    # print 'name=',name,' value=',value
                    # if name=='steps':
                    # continue
                    try:
                        setattr(rr.getIntegrator(), name, value)
                    except (AttributeError, ValueError) as e:
                        setattr(rr.getIntegrator(),
                                self.option_name_dict[name], value)