[R-sig-dyn-mod] deSolve solution status updates

Thomas Petzoldt thom@@@petzoldt @ending from tu-dre@den@de
Thu Aug 2 12:26:37 CEST 2018


Hello,

thank you for the suggestion. There are plenty of options that probably 
solve your request. The easiest is to integrate a cat or print statement 
or a call to txtProgressbar() in your model code and then evaluate the 
time variable.

Examples can be found below. It is of course also possible to pass a 
user-defined parameter, e.g. interactive=c(FALSE, TRUE), to your model 
for switching the status information off and on, or to evaluate the 
actual time and then print the information only periodically and not 
during each step.

More options are to use deSolve's event mechanism, another what we call 
"stop and go mode", i.e. split the model integration into segments and 
run the model for part of the time and then put the last output as 
initial value for the next step.

Hope it helps,

Thomas



On 02.08.2018 at 11:02 Matsievskiy S.V. wrote:
> Hello!
> 
> I use deSolve in a context of a shiny interactive application. It would 
> de nice to have some kind of "interactive=TRUE" switch to make solver 
> functions periodically print information about current status of the 
> solution. This way it would be possible to estimate time needed to solve 
> the problem and present it to user.
> 



library(deSolve)

lv <- function (t, x, parms) {
    with(as.list(c(x, parms)),{
      dp <- a * p - b * k * p
      dk <- b * p * k - d * k
      list(c(dp, dk))
    })
}


lv_cat <- function (t, x, parms) {
    cat(t, "\n")
    with(as.list(c(x, parms)),{
      dp <- a * p - b * k * p
      dk <- b * p * k - d * k
      list(c(dp, dk))
    })
}

lv_progress <- function (t, x, parms) {
    setTxtProgressBar(pb, t)
    with(as.list(c(x, parms)),{
      dp <- a * p - b * k * p
      dk <- b * p * k - d * k
      list(c(dp, dk))
    })
}


parms  <- c(a=0.1, b=0.1, d=0.1)
times  <- seq(0, 2000, by=0.5)
init   <- c(p=1, k=0.5)

pb <- txtProgressBar(times[1], max(times), times[1], style=3)

system.time({
    out <- ode(func = lv_cat, y = init, times = times, parms = parms)
})

system.time({
    out <- ode(func = lv_progress, y = init, times = times, parms = parms)
})


system.time({
    out <- ode(func = lv, y = init, times = times, parms = parms)
})


plot(out)



More information about the R-sig-dynamic-models mailing list