Skip to content

[R-sig-dyn-mod] event location in simecol and discrete event, process based simulation

2 messages · jose romero, Thomas Petzoldt

#
Hi Jos?!
On 03.09.2010 16:58, jose romero wrote:
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.
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.
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)