odeModel               package:simecol               R Documentation

_G_e_n_e_r_a_t_i_n_g-_f_u_n_c_t_i_o_n_s (_C_o_n_s_t_r_u_c_t_o_r_s) _t_o _C_r_e_a_t_e _O_b_j_e_c_t_s _o_f
_C_l_a_s_s_e_s _o_d_e_M_o_d_e_l, _r_w_a_l_k_M_o_d_e_l _a_n_d _g_r_i_d_M_o_d_e_l.

_D_e_s_c_r_i_p_t_i_o_n:

     These functions can be used to create 'simObj' instances without
     using 'new' explicitly.

_U_s_a_g_e:

     odeModel(obj = NULL, main = NULL,
             equations = NULL, times = c(from = 0, to = 10, by = 1),
             init = numeric(0), parms = numeric(0),
             inputs = NULL, solver = "rk4", initfunc = NULL)

     gridModel(obj = NULL, main = NULL,
             equations = NULL, times = c(from=0, to=10, by=1),
             init = matrix(0), parms = list(),
             inputs = NULL, solver = "iteration", initfunc = NULL)

     rwalkModel(obj = NULL, main = NULL, 
             equations = NULL, times = c(from = 0, to = 10, by = 1),
             init = NULL, parms = list(),
             inputs = NULL, solver = "iteration", initfunc = NULL)
             
     indbasedModel(obj = NULL, main = NULL, 
             equations = NULL, times = c(from = 0, to = 10, by = 1),
             init = NULL, parms = list(),
             inputs = NULL, solver = "iteration", initfunc = NULL)        

_A_r_g_u_m_e_n_t_s:

     obj: Unnamed arguments are regarded as objects of the
          corresponding class. If 'obj' is omitted, the new object is
          created from scratch.

    main: The main equations of the model.

equations: The sub-models (sub-equations and of the model).

   times: A vector of time steps or a vector with three named values
          'from', 'to', 'by' specifying the simulation time steps. The
          `from-to-by' form can be edited with 'fixParms'.

    init: Initial values (start values) of the state variable given as
          named vector.

   parms: A vector or list (depending on the respective class) of
          constant parameters.

  inputs: Optional time-dependend input variables (matrix or data
          frame).

  solver: The solver used to integrate the model.

initfunc: The function is called by the 'initialize' mechanism and
          allows direct access and manipulation of all slots of the
          object in creation

_D_e_t_a_i_l_s:

     These functions provide an alternative way to create 'simObj'
     instances in addition to the standard S4 'new' mechanism. The
     functions are provided mainly for compatibility with older
     versions of 'simecol'.

     See 'simecol-package' and the examples for details about the
     slots.

_V_a_l_u_e:

     The function returns an S4 object of type 'odeModel',
     'rwalkModel', 'gridModel'

_S_e_e _A_l_s_o:

     'new', 'simecol-package'

_E_x_a_m_p_l_e_s:

     ## (1) Define and run your own simecol model with new ==================

     lv <- new("odeModel", 
       main = function (time, init, parms) {
         with(as.list(c(init, parms)), {
           dn1 <-   k1 * N1 - k2 * N1 * N2
           dn2 <- - k3 * N2 + k2 * N1 * N2
           list(c(dn1, dn2))
         })
       },
       parms  = c(k1 = 0.2, k2 = 0.2, k3 = 0.2),
       times  = c(from = 0, to = 100, by = 0.5),
       init   = c(N1 = 0.5, N2 = 1),
       solver = "lsoda"
     )

     ## ... or use the generating function ----------------------------------

     lv <- odeModel( 
       main = function (time, init, parms) {
         with(as.list(c(init, parms)), {
           dn1 <-   k1 * N1 - k2 * N1 * N2
           dn2 <- - k3 * N2 + k2 * N1 * N2
           list(c(dn1, dn2))
         })
       },
       parms  = c(k1 = 0.2, k2 = 0.2, k3 = 0.2),
       times  = c(from = 0, to = 100, by = 0.5),
       init   = c(N1 = 0.5, N2 = 1),
       solver = "lsoda"
     )

     lv <- sim(lv)
     plot(lv)

     ## (2) Conway's Game of Life ==========================================

     set.seed(23)  # to make it reproducible

     conway <- new("gridModel",
       main = function(time, x, parms) {
         nb     <- eightneighbours(x)
         surviv <- (x >  0 & (nb %in% parms$srv))
         gener  <- (x == 0 & (nb %in% parms$gen))
         x      <- as.numeric((surviv + gener) > 0)
         dim(x) <- dim(init)
         return(x)
       },
       parms  = list(srv = c(2, 3), gen = 3),
       times  = 1:17,
       init   = matrix(round(runif(1000)), ncol=40),
       solver = "iteration"
     )

     sim(conway, animate=TRUE)

