Skip to content

Commit

Permalink
Fix paras vector in manager function for Issue #46 and #47
Browse files Browse the repository at this point in the history
  • Loading branch information
bradduthie committed Dec 1, 2018
1 parent 4b6076f commit bdb2601
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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) */
Expand Down Expand Up @@ -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) );
Expand Down Expand Up @@ -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 */
/* ====================================================================== */

Expand Down Expand Up @@ -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++;
Expand All @@ -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]);
Expand Down

0 comments on commit bdb2601

Please sign in to comment.