From bdb2601d2b75a1699892e8989db7be52f269a37b Mon Sep 17 00:00:00 2001 From: Brad Duthie Date: Sat, 1 Dec 2018 15:04:29 +0000 Subject: [PATCH] Fix paras vector in manager function for Issue #46 and #47 --- src/manager.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/manager.c b/src/manager.c index 7e797fb4..670c0eb9 100644 --- a/src/manager.c +++ b/src/manager.c @@ -399,6 +399,7 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, int jacobian_dim; /* Dimensions of the (square) Jacobian matrix */ int protected_n; /* Number of protected R objects */ int vec_pos; /* Vector position for making arrays */ + int len_PARAMETERS; /* Length of the parameters vector */ int *dim_RESOURCE; /* Dimensions of the RESOURCE array incoming */ int *dim_LANDSCAPE; /* Dimensions of the LANDSCAPE array incoming */ int *dim_AGENT; /* Dimensions of the AGENT array incoming */ @@ -410,7 +411,9 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, int **lookup; /* Lookup table for resource & land interactions */ double *R_ptr; /* Pointer to RESOURCE (interface R and C) */ double *land_ptr; /* Pointer to LANDSCAPE (interface R and C) */ + double *paras_ptr; /* Pointer to PARAMETERS (interface R and C) */ double *paras; /* Pointer to PARAMETER (interface R and C) */ + double *paras_ptr_new; /* Pointer to new paras (interface R and C) */ double *agent_ptr; /* Pointer to AGENT (interface R and C) */ double *cost_ptr; /* Pointer to COST (interface R and C) */ double *action_ptr; /* Pointer to ACTION (interface R and C) */ @@ -472,8 +475,9 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, PROTECT( PARAMETERS = AS_NUMERIC(PARAMETERS) ); protected_n++; - paras = REAL(PARAMETERS); + paras_ptr = REAL(PARAMETERS); + len_PARAMETERS = GET_LENGTH(PARAMETERS); dim_RESOURCE = INTEGER( GET_DIM(RESOURCE) ); dim_LANDSCAPE = INTEGER( GET_DIM(LANDSCAPE) ); dim_AGENT = INTEGER( GET_DIM(AGENT) ); @@ -625,6 +629,14 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, } } + /* Code below copies the paras vector into C */ + paras = malloc(len_PARAMETERS * sizeof(double *)); + vec_pos = 0; + for(xloc = 0; xloc < len_PARAMETERS; xloc++){ + paras[xloc] = paras_ptr[vec_pos]; + vec_pos++; + } /* The parameters vector is now copied into C */ + /* Do the biology here now */ /* ====================================================================== */ @@ -725,6 +737,18 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, } } + SEXP PARAMETERS_NEW; + PROTECT( PARAMETERS_NEW = allocVector(REALSXP, len_PARAMETERS) ); + protected_n++; + + paras_ptr_new = REAL(PARAMETERS_NEW); + + vec_pos = 0; + for(xloc = 0; xloc < len_PARAMETERS; xloc++){ + paras_ptr_new[vec_pos] = paras[xloc]; + vec_pos++; + } + SEXP EVERYTHING; EVERYTHING = PROTECT( allocVector(VECSXP, 6) ); protected_n++; @@ -733,10 +757,11 @@ SEXP manager(SEXP RESOURCE, SEXP LANDSCAPE, SEXP PARAMETERS, SEXP AGENT, SET_VECTOR_ELT(EVERYTHING, 2, NEW_LANDSCAPE); SET_VECTOR_ELT(EVERYTHING, 3, NEW_ACTIONS); SET_VECTOR_ELT(EVERYTHING, 4, NEW_COSTS); - SET_VECTOR_ELT(EVERYTHING, 5, PARAMETERS); + SET_VECTOR_ELT(EVERYTHING, 5, PARAMETERS_NEW); UNPROTECT(protected_n); + free(paras); /* Free all of the allocated memory used in the observation array */ for(row = 0; row < obs_d0; row++){ free(obs_array[row]);