# Path: stochpy/implementations/StochPyTools.py
# class Species():
# class StochPySSA_Shared(): 
#     def __init__(self):
#     def Parse(self,model_file,model_dir,IsTauleaping=False,IsNRM=False,IsDelayed = False,IsSMM = False,IsQuiet=False):
#     def SpeciesSelection(self):
#     def RateSelection(self):
#     def SetEvents(self):
#     def Propensities(self,IsTauleaping=False):
#     def BuildPropensityCodes(self, propensities = None): # 21-11-2013 
#     def HandleEvents(self,IsTauleapingStep=False):    
#     def AssignmentRules(self):
#     def rateFunc(self,rate_eval_code,r_vec):
#     def Initial_Conditions(self,IsTauleaping = False):              
#     def GenerateOutput(self,IsTauleaping = False,completion_delayed = False):


import sys,copy,time,os,operator
from .StochPyTools import __species__,StochPySSA_Shared,np
        elif self.count == 1000:
            self.randoms1 = np.random.random(1000)
            self.randoms2 = np.random.random(1000)
            self.count = 0       
            
        self.GenerateTau()                                                      # reaction time generation from self.randoms1
        if not self._IsPerformEvent:                                            # If event triggered by delay, this replaces the initiation of reaction. 
            self.sim_t += self.sim_tau           
            if self.sim_a_0 == 0:                                               # No reaction initiates (but delayed reaction can still complete in next iteration).
                self.reaction_index = np.nan                
            elif self.sim_t < self.settings.endtime:
                self.InitiateReaction()                                         # Pick reaction and perform it
                #TODO: reaction (sim_t) after event, reaction will never occur but delayed event is added. Problem: Delayed reaction started, at time of event..?
                self.count += 1
                self.timestep += 1            
            else: 
                self.sim_t = self.settings.endtime
                self.reaction_index = np.nan
                
            
    def GenerateTau(self): 
        """ Generates the waiting time, according to the improved equation 11 of Cai """
        self.sim_r1 = self.randoms1[self.count]     
        self._current_sim_t = self.sim_t                                        # Event handling: Remember sim_t at current timepoint.        
        self.i = 0
        if len(self.Tstruct) == 2:                                              # No ongoing delayed reaction
            self.sim_tau = -1 * np.log(self.sim_r1)/float(self.sim_a_0)           
        else:            
            at_previous = 0
            self.at = self.sim_a_0 * self.Tstruct[self.i+1][0]                  # Up to first delayed reaction

# Path: stochpy/implementations/StochPyTools.py
# class Species():
# class StochPySSA_Shared(): 
#     def __init__(self):
#     def Parse(self,model_file,model_dir,IsTauleaping=False,IsNRM=False,IsDelayed = False,IsSMM = False,IsQuiet=False):
#     def SpeciesSelection(self):
#     def RateSelection(self):
#     def SetEvents(self):
#     def Propensities(self,IsTauleaping=False):
#     def BuildPropensityCodes(self, propensities = None): # 21-11-2013 
#     def HandleEvents(self,IsTauleapingStep=False):    
#     def AssignmentRules(self):
#     def rateFunc(self,rate_eval_code,r_vec):
#     def Initial_Conditions(self,IsTauleaping = False):              
#     def GenerateOutput(self,IsTauleaping = False,completion_delayed = False):


# Path: stochpy/implementations/DelayedDirectMethod.py
import sys,copy,time,os,operator
from .StochPyTools import __species__,StochPySSA_Shared,np

        elif self.count == 1000:
            self.randoms1 = np.random.random(1000)
            self.randoms2 = np.random.random(1000)
            self.count = 0       
            
        self.GenerateTau()                                                      # reaction time generation from self.randoms1
        if not self._IsPerformEvent:                                            # If event triggered by delay, this replaces the initiation of reaction. 
            self.sim_t += self.sim_tau           
            if self.sim_a_0 == 0:                                               # No reaction initiates (but delayed reaction can still complete in next iteration).
                self.reaction_index = np.nan                
            elif self.sim_t < self.settings.endtime:
                self.InitiateReaction()                                         # Pick reaction and perform it
                #TODO: reaction (sim_t) after event, reaction will never occur but delayed event is added. Problem: Delayed reaction started, at time of event..?
                self.count += 1
                self.timestep += 1            
            else: 
                self.sim_t = self.settings.endtime
                self.reaction_index = np.nan
                
            
    def GenerateTau(self): 
        """ Generates the waiting time, according to the improved equation 11 of Cai """
        self.sim_r1 = self.randoms1[self.count]     
        self._current_sim_t = self.sim_t                                        # Event handling: Remember sim_t at current timepoint.        
        self.i = 0
        if len(self.Tstruct) == 2:                                              # No ongoing delayed reaction
            self.sim_tau = -1 * np.log(self.sim_r1)/float(self.sim_a_0)           
        else:            
            at_previous = 0
            self.at = self.sim_a_0 * self.Tstruct[self.i+1][0]                  # Up to first delayed reaction

            F = 1 - np.exp(-self.at)