auxiliary/modelfiles/DSAIRM/simulators/simulate_Basic_Bacteria_Model_ode.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 ordinary differential equations using the deSolve package. 
#' The following R packages need to be loaded for the function to work: deSolve. 
#' 
#' @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 tstart : Start time of simulation : numeric
#' @param tfinal : Final time of simulation : numeric
#' @param dt : Time step : 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_ode() 
#' # To choose values other than the standard one, specify them like this:  
#' result <- simulate_Basic_Bacteria_Model_ode(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_ode <- function(B = 10, I = 1, g = 1, maxB = 1e+06, dB = 0.1, k = 1e-07, r = 0.001, dI = 1, tstart = 0, tfinal = 30, dt = 0.01) 
{ 
  ############################## 
  #Block of ODE equations for deSolve 
  ############################## 
  Basic_Bacteria_Model_ode_fct <- function(t, y, parms) 
  {
    with( as.list(c(y,parms)), { #lets us access variables and parameters stored in y and parms by name 
    #StartODES
    #Bacteria : bacteria growth : bacteria death : immune response killing :
    dB_mb = +g*B*(1-B/maxB) -dB*B -k*B*I
    #Immune Response : immune response growth : immune response decay :
    dI_mb = +r*B*I -dI*I
    #EndODES
    list(c(dB_mb,dI_mb)) 
  } ) } #close with statement, end ODE code block 
 
  ############################## 
  #Main function code block 
  ############################## 
  #Creating named vectors 
  varvec_mb = c(B = B, I = I) 
  parvec_mb = c(g = g, maxB = maxB, dB = dB, k = k, r = r, dI = dI) 
  timevec_mb = seq(tstart, tfinal,by = dt) 
  #Running the model 
  simout = deSolve::ode(y = varvec_mb, parms = parvec_mb, times = timevec_mb,  func = Basic_Bacteria_Model_ode_fct, rtol = 1e-12, atol = 1e-12) 
  #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.