pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rprocess.c
Go to the documentation of this file.
1// dear emacs, please treat this as -*- C++ -*-
2
3#include <R.h>
4#include <Rmath.h>
5#include <Rdefines.h>
6
7
8#include "internal.h"
9
10static SEXP pomp_default_rprocess (SEXP xstart, int nvars, int nreps, int ntimes)
11{
12 SEXP Snames, X;
13 int dim[3] = {nvars, nreps, ntimes};
14 int i, n = nvars*nreps*ntimes;
15 double *xp = 0;
16 PROTECT(Snames = GET_ROWNAMES(GET_DIMNAMES(xstart)));
17 PROTECT(X = makearray(3,dim));
19 for (i= 0, xp = REAL(X); i < n; i++, xp++) *xp = R_NaReal;
20 warn("'rprocess' unspecified: NAs generated.");
21 UNPROTECT(2);
22 return X;
23}
24
25SEXP do_rprocess (SEXP object, SEXP xstart, SEXP tstart, SEXP times, SEXP params, SEXP gnsi)
26{
27
28 int *xdim, type, nvars, npars, nreps, nrepsx, ntimes;
29 SEXP X, copy, rproc, args, accumvars, covar;
30 SEXP dimXstart, dimP;
31 const char *dimnm[3] = {"name",".id","time"};
32
33 PROTECT(gnsi = duplicate(gnsi));
34
35 PROTECT(tstart = AS_NUMERIC(tstart));
36
37 PROTECT(times = AS_NUMERIC(times));
38 ntimes = length(times);
39 if (ntimes < 1) {
40 err("length(times) < 1: no work to do.");
41 }
42
43 PROTECT(xstart = as_matrix(xstart));
44 PROTECT(dimXstart = GET_DIM(xstart));
45 xdim = INTEGER(dimXstart);
46 nvars = xdim[0]; nrepsx = xdim[1];
47
48 PROTECT(params = as_matrix(params));
49 PROTECT(dimP = GET_DIM(params));
50 xdim = INTEGER(dimP);
51 npars = xdim[0]; nreps = xdim[1];
52
53 int nprotect = 7;
54
55 if (nrepsx > nreps) { // more ICs than parameters
56 if (nrepsx % nreps != 0) {
57 err("the larger number of replicates is not a multiple of smaller.");
58 } else {
59 double *src, *tgt;
60 int dims[2];
61 int j, k;
62 dims[0] = npars; dims[1] = nrepsx;
63 PROTECT(copy = duplicate(params));
64 PROTECT(params = makearray(2,dims));
65 nprotect += 2;
66 setrownames(params,GET_ROWNAMES(GET_DIMNAMES(copy)),2);
67 src = REAL(copy);
68 tgt = REAL(params);
69 for (j = 0; j < nrepsx; j++) {
70 for (k = 0; k < npars; k++, tgt++) {
71 *tgt = src[k+npars*(j%nreps)];
72 }
73 }
74 }
75 nreps = nrepsx;
76 } else if (nrepsx < nreps) { // more parameters than ICs
77 if (nreps % nrepsx != 0) {
78 err("the larger number of replicates is not a multiple of smaller.");
79 } else {
80 double *src, *tgt;
81 int dims[2];
82 int j, k;
83 dims[0] = nvars; dims[1] = nreps;
84 PROTECT(copy = duplicate(xstart));
85 PROTECT(xstart = makearray(2,dims));
86 nprotect += 2;
87 setrownames(xstart,GET_ROWNAMES(GET_DIMNAMES(copy)),2);
88 src = REAL(copy);
89 tgt = REAL(xstart);
90 for (j = 0; j < nreps; j++) {
91 for (k = 0; k < nvars; k++, tgt++) {
92 *tgt = src[k+nvars*(j%nrepsx)];
93 }
94 }
95 }
96 }
97
98 PROTECT(rproc = GET_SLOT(object,install("rprocess")));
99 PROTECT(args = GET_SLOT(object,install("userdata")));
100 PROTECT(accumvars = GET_SLOT(object,install("accumvars")));
101 PROTECT(covar = GET_SLOT(object,install("covar")));
102
103 nprotect += 4;
104
105 // extract the process function
106 type = *(INTEGER(GET_SLOT(rproc,install("type"))));
107 switch (type) {
108 case onestep: // one-step simulator
109 {
110 SEXP fn;
111 double deltat = 1.0;
112 PROTECT(fn = GET_SLOT(rproc,install("step.fn")));
113 PROTECT(X = euler_simulator(fn,xstart,tstart,times,params,deltat,type,
114 accumvars,covar,args,gnsi));
115 nprotect += 2;
116 }
117 break;
118 case discrete: case euler: // discrete-time and Euler
119 {
120 SEXP fn;
121 double deltat;
122 PROTECT(fn = GET_SLOT(rproc,install("step.fn")));
123 deltat = *(REAL(AS_NUMERIC(GET_SLOT(rproc,install("delta.t")))));
124 PROTECT(X = euler_simulator(fn,xstart,tstart,times,params,deltat,type,
125 accumvars,covar,args,gnsi));
126 nprotect += 2;
127 }
128 break;
129 case gill: // Gillespie's method
130 {
131 SEXP fn, vmatrix, hmax;
132 PROTECT(fn = GET_SLOT(rproc,install("rate.fn")));
133 PROTECT(vmatrix = GET_SLOT(rproc,install("v")));
134 PROTECT(hmax = GET_SLOT(rproc,install("hmax")));
135 PROTECT(X = SSA_simulator(fn,xstart,tstart,times,params,vmatrix,covar,
136 accumvars,hmax,args,gnsi));
137 nprotect += 4;
138 }
139 break;
140 case dflt: default:
141 PROTECT(X = pomp_default_rprocess(xstart,nvars,nreps,ntimes));
142 nprotect++;
143 break;
144 }
145
146 fixdimnames(X,dimnm,3);
147 UNPROTECT(nprotect);
148 return X;
149
150}
SEXP euler_simulator(SEXP, SEXP, SEXP, SEXP, SEXP, double, rprocmode, SEXP, SEXP, SEXP, SEXP)
Definition euler.c:108
SEXP SSA_simulator(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP)
Definition ssa.c:226
#define X
Definition gompertz.c:14
#define warn(...)
Definition pomp.h:22
#define err(...)
Definition pomp.h:21
@ gill
@ discrete
@ dflt
@ onestep
@ euler
static R_INLINE void fixdimnames(SEXP x, const char **names, int n)
static R_INLINE void setrownames(SEXP x, SEXP names, int rank)
static R_INLINE SEXP makearray(int rank, const int *dim)
static R_INLINE SEXP as_matrix(SEXP x)
static SEXP pomp_default_rprocess(SEXP xstart, int nvars, int nreps, int ntimes)
Definition rprocess.c:10
SEXP do_rprocess(SEXP object, SEXP xstart, SEXP tstart, SEXP times, SEXP params, SEXP gnsi)
Definition rprocess.c:25
int npars
Definition trajectory.c:132
SEXP Snames
Definition trajectory.c:140
SEXP params
Definition trajectory.c:128
int nreps
Definition trajectory.c:134
SEXP args
Definition trajectory.c:139
SEXP fn
Definition trajectory.c:138
int nvars
Definition trajectory.c:131