auxiliary/modelfiles/DSAIRM/simulators/simulate_Basic_Bacteria_Model_stochastic.R

#' Basic Bacteria Model
#' 
#' @description A basic bacteria infection model with 2 compartments
#' 
#' @details The model includes bacteria and an immune response. The processes are bacteria growth, death and killing, and immune response activation and decay. This is a predator-prey type model.
#' 
#' This code was generated by the modelbuilder R package.  
#' The model is implemented as a set of stochastic equations using the adaptivetau package. 
 
#' The following R packages need to be loaded for the function to work: adpativetau 
#' 
#' @param B : starting value for Bacteria : numeric
#' @param I : starting value for Immune Response : numeric
#' @param g : maximum rate of bacteria growth : numeric
#' @param maxB : bacteria carrying capacity : numeric
#' @param dB : bacteria death rate : numeric
#' @param k : bacteria kill rate : numeric
#' @param r : immune response growth rate : numeric
#' @param dI : immune response decay rate : numeric
#' @param tfinal : Final time of simulation : numeric
#' @param rngseed : set random number seed for reproducibility : numeric
#' @return The function returns the output as a list. 
#' The time-series from the simulation is returned as a dataframe saved as list element \code{ts}. 
#' The \code{ts} dataframe has one column per compartment/variable. The first column is time.   
#' @examples  
#' # To run the simulation with default parameters:  
#' result <- simulate_Basic_Bacteria_Model_stochastic() 
#' # To choose values other than the standard one, specify them like this:  
#' result <- simulate_Basic_Bacteria_Model_stochastic(B = 20,I = 2) 
#' # You can display or further process the result, like this:  
#' plot(result$ts[,'time'],result$ts[,'B'],xlab='Time',ylab='Numbers',type='l') 
#' print(paste('Max number of B: ',max(result$ts[,'B']))) 
#' @section Warning: This function does not perform any error checking. So if you try to do something nonsensical (e.g. have negative values for parameters), the code will likely abort with an error message.
#' @section Model Author: Andreas Handel
#' @section Model creation date: 2022-05-09
#' @section Code Author: generated by the \code{modelbuilder} R package 
#' @section Code creation date: 2022-05-09
#' @export 
 
simulate_Basic_Bacteria_Model_stochastic <- function(B = 10, I = 1, g = 1, maxB = 1e+06, dB = 0.1, k = 1e-07, r = 0.001, dI = 1, tfinal = 30, rngseed = 123) 
{ 
  #Block of ODE equations for adaptivetau 
  Basic_Bacteria_Model_fct <- function(y, parms, t) 
  {
    with(as.list(c(y,parms)),   
     { 
       #specify each rate/transition/reaction that can happen in the system 
     rates = c(dB*B, dI*I, g*B*(1-B/maxB), k*B*I, r*B*I)
     return(rates) 
      }
	 	)   
  } # end function specifying rates used by adaptive tau 

   #specify for each reaction/rate/transition how the different variables change 
  #needs to be in exactly the same order as the rates listed in the rate function 
  transitions = list(c(B = -1), 
 	 	 				c(I = -1), 
 	 	 				c(B = +1), 
 	 	 				c(B = -1), 
 	 	 				c(I = +1)) 
 
  ############################## 
  #Main function code block 
  ############################## 
  set.seed(rngseed) #set random number seed for reproducibility 
  #Creating named vectors 
  varvec = c(B = B, I = I) 
  parvec = c(g = g, maxB = maxB, dB = dB, k = k, r = r, dI = dI) 
  #Running the model 
  simout = adaptivetau::ssa.adaptivetau(init.values = varvec, transitions = transitions,
                  	 	 	 rateFunc = Basic_Bacteria_Model_fct, params = parvec, tf = tfinal) 
  #Setting up empty list and returning result as data frame called ts 
  result <- list() 
  result$ts <- as.data.frame(simout) 
  return(result) 
 }  
 
ahgroup/modelbuilder documentation built on April 14, 2024, 2:29 p.m.