Skip to contents

This function generates a closure that can be used as the func argument in ode or related solvers. The returned function computes derivatives from a model specification (ODEs and assignment rules), while optionally including selected compartment and parameter values in the output.

Usage

create_desolve_func(model, outputs = NULL)

Arguments

model

SBML model S3 class.

outputs

Optional named list of equations (character strings). Each element defines a derived output, e.g. concentrations or calculated parameters: list(CGut = "Agut / Gut", BW = "BW").

Value

A function with signature function(time, state, parameters) suitable for use with ode. The returned function produces a list with:

  • First element: vector of state derivatives (in the same order as state).

  • Subsequent elements: named numeric values of selected compartments and/or parameters (if requested).

See also

Examples

library(deSolve)

# Load example model
model_file <- system.file("extdata/", "simple_oral.sbml", package = "sbmlpbk")
model <- load_sbml(model_file)

# Create deSolve-compatible function
func <- create_desolve_func(
  model,
  list(
    CGut = "AGut / Gut",
    BW = "BW"
  )
)

# Set initial states
initial_states <- setNames(rep(0, length(model$species)), names(model$species))

# Set input events (single unit bolus at time 1)
eventdat <- data.frame(var = c("AGut"), time = c(1), value = c(1), method = c("add"))

# Run simulation
out <- ode(
  y = initial_states,
  times = seq(0, 24, by = 1),
  func = func,
  parms = model$params,
  events = list(data = eventdat)
)
head(out)
#>      time      AGut      ABlood      ALiver      ARest      AUrine      CGut BW
#> [1,]    0 0.0000000 0.000000000 0.000000000 0.00000000 0.000000000 0.0000000 70
#> [2,]    1 0.0000000 0.000000000 0.000000000 0.00000000 0.000000000 0.0000000 70
#> [3,]    2 0.9512294 0.003257892 0.004269830 0.03752682 0.003716044 0.9512294 70
#> [4,]    3 0.9048374 0.005489790 0.005730590 0.07171887 0.012223327 0.9048374 70
#> [5,]    4 0.8607080 0.007352787 0.006938563 0.10032517 0.024675474 0.8607080 70
#> [6,]    5 0.8187307 0.008893254 0.007925930 0.12404544 0.040404630 0.8187307 70