[R-sig-dyn-mod] event location in simecol and discrete event, process based simulation
Hi Jos?!
On 03.09.2010 16:58, jose romero wrote:
Hello list: Regarding the example of a bouncing ball with event location (see bellow), how would it be implemented with simecol? Specifically, how do you "pass" the event location (be it "root finding" as in the example, or as a data-frame "schedule" described in lsodar) to the solver slot in an ode simecol object?
The implementation of such a model is straightforward if you define a user-specified solver function. This can either a function in the user workspace that is then referenced by its name (as character string) in the solver slot or you assign the specific solver directly as in the example below. The same applies to the root function and the event function. They can be either defined globally in the user workspace or as local functions in the solver slot. Be careful to use consistent naming of the arguments, either (time, init) or (t, y), both versions are possible. Note also that the animation is carried out afterwards as it would be rather inefficient to interrupt the ode solver, though this would also be possible by using events.
My other question is about discrete event simulation. I use R for most of my simulation course. However, when I come to the topic of discrete event simulation through the process approach, I have to use SimPy, which means switching to Python. In a bank queue, for example, the servers and clients are implemented as processes running concurrently and passing messages to each other, without having to schedule which events come before others. Is there a possibility of extending simecol to do such process based discrete event simulation? Or is it too far out of the simecol simulation object framework (the sim object templates)?
I think it is possible, at least in a quasi-parallel manner by using vectorized computations. If you have a simple model example for this we may try to implement it.
Thanks and regards, jos? romero
Hope it helps,
Thomas Petzoldt
###############################
## bouncing ball in simecol
###############################
library(simecol)
ballode <- new("odeModel",
main = function (t, y, parms) {
dy1 <- y[2]
dy2 <- -9.8
list(c(dy1, dy2))
},
parms = 0,
times = seq(0, 40, 0.01),
init = c(height = 0, v = 20),
solver = function(t, y, func, parms) {
root <- function(t, y, parms) y[1]
event <- function(t, y, parms) {
y[1] <- 0
y[2] <- -0.9 * y[2]
return(y)
}
lsodar(t, y, func, parms = NULL,
events = list(func = event, root = TRUE),
rootfun = root)
}
)
ballode <- sim(ballode)
## animation
o <- out(ballode)
for (i in 1:length(times(ballode)))
plot(1, o[i, 2], col="red", cex=2, pch=16, ylim=c(0,20))
## it would be also possible to derive a special class
## and use a class specific plot method ...
## The default plot function plots the time series of states
plot(ballode)