initialize-methods          package:simecol          R Documentation

_M_e_t_h_o_d_s _f_o_r _F_u_n_c_t_i_o_n '_i_n_i_t_i_a_l_i_z_e' _i_n _P_a_c_k_a_g_e '_s_i_m_e_c_o_l'

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

     This function is used to initialize objects derived from the
     'simObj' superclass.

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

       ## S4 method for signature 'simObj':
       initialize(.Object, ...)

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

 .Object: The 'simObj' instance which is to be re-initialized.

     ...: Provided for compatibility with the default method of
          'initialize', or slots of the object which is to be created
          (in case of 'new')

_M_e_t_h_o_d_s:

     ._O_b_j_e_c_t = "_A_N_Y" Generic function: see 'new'.

     ._O_b_j_e_c_t = "_s_i_m_O_b_j" The 'initialize' function is normally called
          implicitly by 'new' to create new objects. It may also be
          called explicitly to return a cloned and re-initialized
          object. The 'simecol' version of 'initialize' provides an
          additonal mechanism to call a user specified function
          provided in the 'initfun' slot of a 'simObj' instance that
          can perform computations during the object creation process.
          The 'initfunc' must have 'obj' as its only argument and must
          return the modified version of this 'obj', see examples
          below.

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

     'simObj', 'new'

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

         # Note: new calls initialize and initialize calls initfunc(obj)
         lv_efr <- new("odeModel",
           main = function (time, init, parms, ...) {
             x <- init
             p <- parms
             S <- approxTime1(inputs, time, rule=2)["s.in"]
             dx1 <-   S * p["k1"] * x[1] - p["k2"] * x[1] * x[2]
             dx2 <-     - p["k3"] * x[2] + p["k2"] * x[1] * x[2]
             list(c(dx1, dx2))
           },
           parms  = c(k1=0.2, k2=0.2, k3=0.2),
           times  = c(from=0, to=100, by=0.5),
           init   = c(prey=0.5, predator=1),
           solver = "rk4",
           initfunc = function(obj) {
             tt <- fromtoby(times(obj))
             inputs(obj) <- as.matrix(data.frame(
                     time = tt,
                     s.in = pmax(rnorm(tt, mean=1, sd=0.5), 0)
                   ))
             obj
           }
         )

         plot(sim(lv_efr))           # first call
         plot(sim(lv_efr))           # same figure with identical object

         lv_efr<- initialize(lv_efr) # re-initializes, new random inputs
         plot(sim(lv_efr))           # different figure

