R/generate_function_inputs.R

Defines functions generate_function_inputs

Documented in generate_function_inputs

#' Create function input/arguments part for a simulation model
#'
#' This function takes as input a modelbuilder model and writes code
#' that produces text needed to create the main function call and the var/par/time vectors
#'
#' @description The model needs to adhere to the structure specified by the modelbuilder package.
#' Models built using the modelbuilder package automatically have the right structure.
#' A user can also build a model list structure themselves following the modelbuilder specifications.
#' If the user provides a file name, this file needs to be an Rds file
#' and contain a valid modelbuilder model structure.
#' @param mbmodel modelbuilder model structure
#' @param modeltype Indicates type of model code that is generated. One of ode/stochastic/discrete
#' @return text that to be placed on top of function code generated by the calling function
#' @export

generate_function_inputs <- function(mbmodel, modeltype)
{


    nvars = length(mbmodel$var)  #number of variables/compartments in model
    npars = length(mbmodel$par)  #number of parameters in model
    ntime = length(mbmodel$time) #numer of parameters for time


    ##############################################################################
    #this creates the lines of code for the main function
    #text for main body of function
    varstring = ""
    varvec = ""
    for (n in 1:nvars)
    {
        varstring=paste0(varstring, mbmodel$var[[n]]$varname," = ", mbmodel$var[[n]]$varval,', ')
        varvec=paste0(varvec, mbmodel$var[[n]]$varname," = ", mbmodel$var[[n]]$varname,', ')
    }
    varvec = substr(varvec,1,nchar(varvec)-2)
    #varstring = paste0(varstring,'), ') #close parantheses

    parstring = ""
    parvec = ""
    for (n in 1:npars)
    {
        parstring=paste0(parstring, mbmodel$par[[n]]$parname," = ", mbmodel$par[[n]]$parval,', ')
        parvec=paste0(parvec, mbmodel$par[[n]]$parname," = ", mbmodel$par[[n]]$parname,', ')
    }
    parvec = substr(parvec,1,nchar(parvec)-2)
    #parstring = paste0(parstring,'), ') #close parantheses

    # Have start/final and timestep for ODE and discrete.
    # No start time and time step for adaptivetau/stochastic since that's not supported
    timestring = ""
    if (modeltype != "stochastic")
    {
        for (n in 1:ntime)
        {
            timestring=paste0(timestring, mbmodel$time[[n]]$timename," = ", mbmodel$time[[n]]$timeval,', ')
        }
    } else #for stochastic model, only use tfinal, no tstart and dt
    {
        timestring=paste0(timestring, mbmodel$time[[2]]$timename," = ", mbmodel$time[[2]]$timeval,', ')
    }

    timestring = substr(timestring,1,nchar(timestring)-2)
    #timestring = paste0(timestring,') ') #close parantheses

    if (modeltype == "ode")
    {
        stitle = paste0('simulate_',gsub(" ","_",mbmodel$title),"_",modeltype, " <- function(",varstring, parstring, timestring,') \n{ \n')
    }
    if (modeltype == "stochastic")
    {
        stitle = paste0('simulate_',gsub(" ","_",mbmodel$title),"_",modeltype, " <- function(",varstring, parstring, timestring,', rngseed = 123) \n{ \n')
    }
    if (modeltype == "discrete")
    {
        stitle = paste0('simulate_',gsub(" ","_",mbmodel$title),"_",modeltype, " <- function(",varstring, parstring, timestring,') \n{ \n')
    }


    textlist = list()
    textlist$stitle = stitle
    textlist$parvec = parvec
    textlist$varvec = varvec

    return(textlist)

}
andreashandel/modelbuilder documentation built on April 16, 2024, 8:48 a.m.