I reproduced a one-compartment model describing drug injection from "Solving Differential Equations in R" (see code below).
What is unclear to me is how to check that mass balance is 0 in case of events. Any advice?
library(deSolve)
b <- 0.6
yini <- c(blood = 0)
pharmaco2 <- function(t, blood, p) {
dblood <-- b * blood
list(dblood) }
injectevents <- data.frame(var = "blood", time = 0:20, value = 40, method = "add")
times <- seq(from = 0, to = 10, by = 1/24)
out2 <- ode(func = pharmaco2,
times = times,
y = yini,
parms = NULL,
method = "impAdams",
events = list(data = injectevents))
plot(out2, lwd = 2, xlab="days")
Checking mass balance requires to close the system. This can be done by adding explicit state variables for source and sink, where
sourcerepresents the stock of the drug andsinkthe accumulated amount that leaves the blood, either by decomposition or excretion. The source variable can be set to zero to check mass balance, or any positive value to represent the amount of drug available in stock.Then add corresponding processes to the model, i.e. decomposition/excretion as state equation and removal from the stock during injection in the events. The model reads then as follows:
And the mass balance check can be done by adding the values of all columns, except time. If initial
sourcewas set to zero, then the balance should also be zero, up to numerical rounding errors:Note also that I use default
lsodaas a solver. Solveradamsorbdfwork as well, and alsoimpAdamsbut I would not recommend the latter in this case.