# Run a model with default parameters
mod <- ca_library("musselbed")
im <- generate_initmat(mod, c(0.4, 0.6, 0), nrow = 100, ncol = 50)
out <- run_camodel(mod, im, times = seq(0, 100))
plot(out)
# Disable console output
opts <- list(console_output_every = 0)
out <- run_camodel(mod, im, times = seq(0, 100), control = opts)
#
# \donttest{
# Run the same model with the 'compiled' engine, and save snapshots. This
# requires a compiler on your computer (typically available by installing
# 'Rtools' on Windows)
ctrl <- list(engine = "compiled", save_covers_every = 1, save_snapshots_every = 100)
run <- run_camodel(mod, im, times = seq(0, 100), control = ctrl)
plot(run)
#
oldpar <- par(mfrow = c(1, 2))
image(run, snapshot_time = 0)
image(run, snapshot_time = 100)
par(oldpar)
# Disable console output
ctrl <- list(console_output_every = 0)
run <- run_camodel(mod, im, times = seq(0, 100), control = ctrl)
plot(run)
# Very verbose console output (display compilation information, etc.)
ctrl <- list(console_output_every = 1,
verbose_compilation = TRUE,
engine = "compiled",
force_compilation = TRUE)
run <- run_camodel(mod, im, times = seq(0, 100), control = ctrl)
# Turn on or off the memoisation of transition probabilities (mind the speed
# difference)
ctrl <- list(engine = "compiled", precompute_probas = FALSE)
run <- run_camodel(mod, im, times = seq(0, 256), control = ctrl)
ctrl2 <- list(engine = "compiled", precompute_probas = TRUE)
run2 <- run_camodel(mod, im, times = seq(0, 256), control = ctrl2)
# Use a custom function to compute statistics while the simulation is running
fun <- function(t, mat) {
# Disturbed cell to mussel cell ratio
ratio <- mean(mat == "DISTURB") / mean(mat == "MUSSEL")
data.frame(t = t, ratio = ratio)
}
ctrl <- list(custom_output_fun = fun, custom_output_every = 1)
run <- run_camodel(mod, im, times = seq(0, 256), control = ctrl)
stats <- do.call(rbind, run[["output"]][["custom"]])
plot(stats[ ,1], stats[ ,2], ylab = "DISTURB/MUSSEL ratio", xlab = "time", type = "l")
# }
Run the code above in your browser using DataLab