# inst/doc/examples/diffusion_B.R In simecol: Simulation of Ecological (and Other) Dynamic Systems

```########################################################################
## Random walk model which uses "initfunc"
########################################################################

diffusion <- rwalkModel(
main = function(time, init, parms, inputs) {
speed   <- parms\$speed
xleft   <- parms\$area[1]
xright  <- parms\$area[2]
ybottom <- parms\$area[3]
ytop    <- parms\$area[4]

x <- init\$x  # x coordinate
y <- init\$y  # y coordinate
a <- init\$a  # angle (in radians)
n <- length(a)

## Rule 1: respect environment (grid as given in "inputs")
## 1a) identify location on "environmental 2D grid" for each individual
i.j <- array(c(pmax(1, ceiling(x)), pmax(1, ceiling(y))), dim=c(n, 2))
# print(i.j)

## 1b) speed dependend on "environmental conditions"
speed <- speed * inputs[i.j]

## Rule 2: Random Walk
a  <- (a + 2 * pi / runif(a)) %% (2 * pi)
dx <- speed * cos(a)
dy <- speed * sin(a)
x  <- x + dx
y  <- y + dy

## Rule 3: Wrap Around
x <- ifelse(x > xright, xleft, x)
y <- ifelse(y > ytop, ybottom, y)
x <- ifelse(x < xleft, xright, x)
y <- ifelse(y < ybottom, ytop, y)
data.frame(x=x, y=y, a=a)
},
times  = c(from=0, to=100, by=1),
parms  = list(ninds=50, speed = 1, area = c(0, 100, 0, 100)),
solver = "iteration",
initfunc = function(obj) {
ninds   <- obj@parms\$ninds
xleft   <- obj@parms\$area[1]
xright  <- obj@parms\$area[2]
ybottom <- obj@parms\$area[3]
ytop    <- obj@parms\$area[4]
obj@init <- data.frame(x = runif(ninds) * (xright - xleft) + xleft,
y = runif(ninds) * (ytop - ybottom) + ybottom,
a = runif(ninds) * 2 * pi)
inp <- matrix(1, nrow=100, ncol=100)
inp[, 45:55] <- 0.2
inputs(obj) <- inp
obj
}
)
```

## Try the simecol package in your browser

Any scripts or data that you put into this service are public.

simecol documentation built on June 3, 2018, 5:07 p.m.