The simecol package is intended to give users (scientists and
students) an interactive environment to implement, distribute,
simulate and document ecological and other dynamic models without the
need to write long simulation programs. For this purpose, an object
oriented approach using the S4 class system is proposed, which should
provide a consistent but still flexible and extensible way to
implement simulation models of different types, namely:
- ordinary differential equation (ODE) models (class
odeModel
), - grid-oriented individual-based models (class
gridModel
), and - particle diffusion-type models (class
rwalkModel
), - individual-based models (class
indbasedModel
), - other model types by deriving a user specified subclass from
simObj
.
Each simulation model is implemented as S4 object (superclass simObj
)
with the following slots:
main = function(time, init, parms, ...)
: a function holding the
main equations of the model,equations
: an optional non-nested list holding arbitrary
sub-equations (sub-models) of the
model. Sub-equations can be interdependent and can be called
directly from withinmain
orinitfunc
.parms
: a list (or vector for some classes) with
constant model parameters,times
: vector of time steps or vector with three
named valuesfrom
,to
,by
specifying the
simulation time steps. The from-to-by form can be edited withfixParms
.init
: initial state (start values) of the
simulation. This is typically a named vector (state variables inodeModel
s) or matrix (e.g. initial grid ofgridModel
s).inputs
: time dependend or spatially resolved external
inputs can be specified as data frame or matrix (more efficient). It
is up to the user to evaluate the corresponding values within themain
function. Performance optimized versions ofapprox
(seeapproxTime
) are available.solver
: character string specifying the numerical
algorithm used, e.g."lsoda"
,"rk4"
or"euler"
from packagedeSolve
). In addition, solvers
from package"ddesolve"
can also be used. In contrast to"euler"
that can be used for difference equations
(i.e.main
returns derivatives),"iterator"
is
intended for models where main returns the new state (i.e for
individual-based models). It is also possible to reference own
algorithms (solvers) that are defined in the useer workspace or to
assign solver functions directly.observer
: optional slot which determines the data
stored during the simulation. A user-providedobserver
function can also be used to write logging information to the
screen or to the hard-disk, to perform run-time visualisation, or
statistical analysis during the simulation.
Note that theobserver
-mechanism works only withiteration
solvers. It is generally not avalable forodeModel
s.out
: this slot holds the simulation results after a
simulation run as data frame (if the return value ofmain
is a vector) or as list (otherwise). The type of data stored inout
can be manipulated by providing a user-defindedobserver
function.initfunc
: this slot can hold an optional function which
is called automatically when a new object is created bynew
or when it is re-initialized byinitialize
orsim
.
simObj
model objects should be defined and created using the
common S4 mechanisms (new
).
In this way, a simObj
object may contain all data, which are
needed to run simulations simply by entering the model object via
source()
or data()
and then to run and plot the model
for example via plot(sim(obj))
.
Accessor functions (with names identical to the slot names) are
provided to get or set model parameters, time steps, initial values,
inputs, the solver, the main and sub-equations or initfunc or to
extract the model outputs. It is also possible to modify the
components of the simecol objects directly, e.g. the model equations
of a model lv
with lv@main
, but there is no
guarantee that this will work in a compatible way in future versions.
Models of different type are provided as data and some more in source
code (see directory examples).
The examples are intended as a starting point to write your own
simObj
objects and to distribute them to whomever you wish.
The package is supplemented with several utility functions
(e.g. seedfill
or neighbours
), which can
be used independently from simObj
objects.