15 PROTECT(
args = VectorToPairList(
args));
21 for (v = LENGTH(Cnames)-1; v >= 0; v--) {
26 SET_TAG(
args,installChar(STRING_ELT(Cnames,v)));
30 for (v = LENGTH(Pnames)-1; v >= 0; v--) {
35 SET_TAG(
args,installChar(STRING_ELT(Pnames,v)));
39 for (v = LENGTH(
Snames)-1; v >= 0; v--) {
44 SET_TAG(
args,installChar(STRING_ELT(
Snames,v)));
52 SET_TAG(
args,install(
"t"));
59 SET_TAG(
args,install(
"j"));
66static void gillespie (
double *t,
double tmax,
double *f,
double *y,
67 const double *v,
int nvar,
int nevent, Rboolean hasvname,
76 for (j = 0; j < nevent; j++) fsum += f[j];
79 tstep = exp_rand()/fsum;
94 int jevent = nevent-1;
95 for (j = 0; j < jevent; j++) {
104 for (i = 0; i < nvar; i++) {
105 vv = v[i+nvar*jevent];
66static void gillespie (
double *t,
double tmax,
double *f,
double *y, {
…}
119 int nvar,
int nevent,
int npar,
int nrep,
int ntimes,
120 double *xstart,
double t,
const double *times,
const double *
params,
double *xout,
121 const double *v,
int nzero,
const int *izero,
122 const int *istate,
const int *ipar,
int ncovar,
const int *icovar,
123 Rboolean hasvname,
const int *ivmat,
128 double *covars = NULL;
133 if (mcov > 0) covars = (
double *) R_Calloc(mcov,
double);
134 if (nevent > 0) f = (
double *) R_Calloc(nevent,
double);
138 memcpy(y,xstart+nvar*irep,nvar*
sizeof(
double));
140 for (i = 0; i < nzero; i++) y[izero[i]] = 0.0;
144 for (j = 0; j < nevent; j++) {
145 f[j] = ratefun(j+1,t,y,par,istate,ipar,icovar,covars);
147 err(
"'rate.fun' returns a negative rate");
151 while (icount < ntimes) {
153 R_CheckUserInterrupt();
155 tmax = (tmax > times[icount]) ? times[icount] : tmax;
156 gillespie(&t,tmax,f,y,v,nvar,nevent,hasvname,ivmat);
160 for (j = 0; j < nevent; j++) {
161 f[j] = ratefun(j+1,t,y,par,istate,ipar,icovar,covars);
163 err(
"'rate.fun' returns a negative rate");
167 if (t >= times[icount]) {
168 memcpy(xout+nvar*(irep+nrep*icount),y,nvar*
sizeof(
double));
170 for (i = 0; i < nzero; i++) y[izero[i]] = 0;
176 if (mcov > 0) R_Free(covars);
177 if (nevent > 0) R_Free(f);
191#define NVAR (__ssa_nvar)
192#define NPAR (__ssa_npar)
193#define NCOV (__ssa_ncov)
194#define ARGS (__ssa_args)
195#define RATEFN (__ssa_ratefn)
196#define FIRST (__ssa_first)
197#define RXR (__ssa_rxrate)
200 int *stateindex,
int *parindex,
int *covindex,
double *c)
202 SEXP var =
ARGS, ans, ob;
206 *(INTEGER(CAR(var))) = j; var = CDR(var);
207 *(REAL(CAR(var))) = t; var = CDR(var);
208 for (v = 0; v <
NVAR; v++, x++, var=CDR(var)) *(REAL(CAR(var))) = *x;
209 for (v = 0; v <
NPAR; v++, p++, var=CDR(var)) *(REAL(CAR(var))) = *p;
210 for (v = 0; v <
NCOV; v++, c++, var=CDR(var)) *(REAL(CAR(var))) = *c;
213 PROTECT(ans = eval(ob,CLOENV(
RATEFN)));
216 if (LENGTH(ans) != 1)
217 err(
"'rate.fun' must return a single numeric rate.");
221 rate = *(REAL(AS_NUMERIC(ans)));
227 SEXP vmatrix, SEXP covar, SEXP accumvars, SEXP hmax, SEXP
args, SEXP gnsi)
231 int nvar, nvarv, nevent, npar, nrep, ntimes;
232 SEXP statenames, paramnames, covarnames;
234 int nzeros = LENGTH(accumvars);
236 SEXP
X, zindex, vindex;
237 int *sidx, *pidx, *cidx, *zidx, *vidx;
238 SEXP
fn,
Snames, Pnames, Cnames, Vnames;
243 dim = INTEGER(GET_DIM(xstart)); nvar = dim[0]; nrep = dim[1];
244 dim = INTEGER(GET_DIM(
params)); npar = dim[0];
245 dim = INTEGER(GET_DIM(vmatrix)); nvarv = dim[0]; nevent = dim[1];
248 err(
"number of state variables must equal the number of rows in 'v'.");
250 ntimes = LENGTH(times);
252 PROTECT(tstart = AS_NUMERIC(tstart));
253 PROTECT(times = AS_NUMERIC(times));
254 t0 = *(REAL(tstart));
255 if (t0 > *(REAL(times)))
err(
"'t0' must be no later than 'times[1]'.");
257 PROTECT(
Snames = GET_ROWNAMES(GET_DIMNAMES(xstart)));
258 PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(
params)));
260 PROTECT(Vnames = GET_ROWNAMES(GET_DIMNAMES(vmatrix)));
264 PROTECT(statenames = GET_SLOT(func,install(
"statenames")));
265 PROTECT(paramnames = GET_SLOT(func,install(
"paramnames")));
266 PROTECT(covarnames = GET_SLOT(func,install(
"covarnames")));
269 hasvnames = !isNull(Vnames);
271 PROTECT(hmax = AS_NUMERIC(hmax));
281 err(
"unrecognized 'mode' %d",
mode);
285 *((
void **) (&(
RXR))) = R_ExternalPtrAddr(
fn);
304 xdim[0] = nvar; xdim[1] = nrep; xdim[2] = ntimes;
308 sidx = INTEGER(GET_SLOT(func,install(
"stateindex")));
309 pidx = INTEGER(GET_SLOT(func,install(
"paramindex")));
310 cidx = INTEGER(GET_SLOT(func,install(
"covarindex")));
313 PROTECT(zindex =
MATCHROWNAMES(xstart,accumvars,
"state variables")); nprotect++;
314 zidx = INTEGER(zindex);
320 PROTECT(vindex =
MATCHROWNAMES(xstart,Vnames,
"state variables")); nprotect++;
321 vidx = INTEGER(vindex);
329 for (i = 0; i < nrep; i++) {
330 SSA(
RXR,i,nvar,nevent,npar,nrep,ntimes,
331 REAL(xstart),t0,REAL(times),REAL(
params),
332 REAL(
X),REAL(vmatrix),
333 nzeros,zidx,sidx,pidx,
ncovars,cidx,hasvnames,vidx,
334 covdim,&covariate_table,REAL(hmax));
lookup_table_t make_covariate_table(SEXP, int *)
SEXP pomp_fun_handler(SEXP, SEXP, pompfunmode *, SEXP, SEXP, SEXP, SEXP)
void table_lookup(lookup_table_t *, double, double *)
SEXP get_covariate_names(SEXP)
double pomp_ssa_rate_fn(int event, double t, const double *x, const double *p, const int *stateindex, const int *parindex, const int *covindex, const double *covars)
#define MATCHROWNAMES(X, N, W)
static R_INLINE void setrownames(SEXP x, SEXP names, int rank)
static R_INLINE SEXP makearray(int rank, const int *dim)
static void SSA(pomp_ssa_rate_fn *ratefun, int irep, int nvar, int nevent, int npar, int nrep, int ntimes, double *xstart, double t, const double *times, const double *params, double *xout, const double *v, int nzero, const int *izero, const int *istate, const int *ipar, int ncovar, const int *icovar, Rboolean hasvname, const int *ivmat, int mcov, lookup_table_t *tab, const double *hmax)
static pomp_ssa_rate_fn * __ssa_rxrate
SEXP SSA_simulator(SEXP func, SEXP xstart, SEXP tstart, SEXP times, SEXP params, SEXP vmatrix, SEXP covar, SEXP accumvars, SEXP hmax, SEXP args, SEXP gnsi)
static double __pomp_Rfun_ssa_ratefn(int j, double t, const double *x, const double *p, int *stateindex, int *parindex, int *covindex, double *c)
static void gillespie(double *t, double tmax, double *f, double *y, const double *v, int nvar, int nevent, Rboolean hasvname, const int *ivmat)
static R_INLINE SEXP add_args(SEXP args, SEXP Snames, SEXP Pnames, SEXP Cnames)