phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
popul_proc.h
Go to the documentation of this file.
1// -*- C++ -*-
2// POPUL_PROC class
3
4#ifndef _POPUL_PROC_H_
5#define _POPUL_PROC_H_
6
7#include "internal.h"
8
10
15template <class STATE, class PARAMETERS, size_t NEVENT>
17
18protected:
19 // TYPE DEFINITIONS
20 typedef STATE state_t;
21 typedef PARAMETERS parameters_t;
22 static const size_t nevent = NEVENT;
23
24protected:
25 // MEMBER DATA
26 slate_t next; // time of next event
27 size_t event; // mark of next event
28 slate_t current; // current time
29 state_t state; // current state
30 parameters_t params; // model parameters
31
32private:
33
34 void clean (void) {}; // memory cleanup
35
36public:
37
38 // SERIALIZATION
40 size_t bytesize (void) const {
41 return 2*sizeof(slate_t) + sizeof(size_t)
42 + sizeof(state_t) + sizeof(parameters_t);
43 };
44
45 friend raw_t* operator>> (const popul_proc_t &X, raw_t *o) {
46 slate_t A[2]; A[0] = X.current; A[1] = X.next;
47 memcpy(o,A,sizeof(A)); o += sizeof(A);
48 memcpy(o,&X.event,sizeof(size_t)); o += sizeof(size_t);
49 memcpy(o,&X.state,sizeof(state_t)); o += sizeof(state_t);
50 memcpy(o,&X.params,sizeof(parameters_t)); o += sizeof(parameters_t);
51 return o;
52 };
53
55 X.clean();
56 slate_t A[2];
57 memcpy(A,o,sizeof(A)); o += sizeof(A);
58 X.current = A[0]; X.next = A[1];
59 memcpy(&X.event,o,sizeof(size_t)); o += sizeof(size_t);
60 memcpy(&X.state,o,sizeof(state_t)); o += sizeof(state_t);
61 memcpy(&X.params,o,sizeof(parameters_t)); o += sizeof(parameters_t);
62 return o;
63 };
64
65public:
66 // CONSTRUCTORS, ETC.
69 popul_proc_t (double t0 = 0) {
70 clean();
71 next = current = slate_t(t0);
72 event = 0;
73 };
74
76 o >> *this;
77 };
78
80 raw_t *o = new raw_t[X.bytesize()];
81 X >> o;
82 o >> *this;
83 delete[] o;
84 };
85
87 clean();
88 raw_t *o = new raw_t[X.bytesize()];
89 X >> o;
90 o >> *this;
91 delete[] o;
92 return *this;
93 };
94
100 clean();
101 };
102
103public:
104
105 // INFORMATION EXTRACTORS
107 slate_t time (void) const {
108 return current;
109 };
110
111 virtual void valid (void) const {};
112
114 virtual void rinit (void) = 0;
116 virtual void jump (int e) = 0;
118 void update_params (double*, int);
120 void update_IVPs (double*, int);
122 double event_rates (double *rate, int n) const;
124 string_t yaml (string_t tab) const;
125
127 void update_clocks (void);
130 int play (double tfin);
131
132};
133
134#define PARAM_SET(X) if (!ISNA(p[m])) params.X = p[m]; m++;
135#define RATE_CALC(X) total += rate[m++] = (X);
136#define YAML_PARAM(X) (t + #X + ": " + std::to_string(params.X) + "\n")
137#define YAML_STATE(X) (t + #X + ": " + std::to_string(state.X) + "\n")
138
139template <class STATE, class PARAMETERS, size_t NEVENT>
140void
142(void)
143{
144 double rate[nevent];
145 double total_rate = event_rates(rate,nevent);
146 if (R_FINITE(total_rate)) {
147 if (total_rate > 0) {
148 next = current+rexp(1/total_rate);
149 } else {
150 next = R_PosInf;
151 }
152 } else {
153 for (event = 0; event < nevent; event++) {
154 if (!R_FINITE(rate[event]))
155 Rprintf("in '%s': invalid event rate[%zd]=%lg\n",__func__,event,rate[event]);
156 }
157 err("in '%s': invalid total event rate=%lg", __func__,total_rate);
158 }
159 double u = runif(0,total_rate);
160 event = 0;
161 while (u > rate[event] && event < nevent) {
162 if (rate[event] < 0)
163 err("in '%s': negative rate[%zd]=%lg",__func__,event,rate[event]); // #nocov
164 u -= rate[event];
165 event++;
166 }
167 assert(event < nevent);
168}
169
170template <class STATE, class PARAMETERS, size_t NEVENT>
171int
173(double tfin)
174{
175 int count = 0;
176 if (current > tfin)
177 err("cannot simulate backward! (current t=%lg, requested t=%lg)",current,tfin);
178 while (next < tfin) {
179 current = next;
180 jump(event);
182 count++;
183 R_CheckUserInterrupt();
184 }
185 if (next > tfin) current = tfin; // relies on Markov property
186 return count;
187}
188
189#endif
void clean(void)
Definition popul_proc.h:34
virtual void jump(int e)=0
makes a jump
popul_proc_t(const popul_proc_t &X)
copy constructor
Definition popul_proc.h:79
friend raw_t * operator>>(const popul_proc_t &X, raw_t *o)
binary serialization
Definition popul_proc.h:45
virtual void valid(void) const
Definition popul_proc.h:111
popul_proc_t & operator=(const popul_proc_t &X)
copy assignment operator
Definition popul_proc.h:86
virtual void rinit(void)=0
initialize the state
~popul_proc_t(void)
destructor
Definition popul_proc.h:99
popul_proc_t(popul_proc_t &&)=delete
move constructor
void update_params(double *, int)
set parameters
slate_t time(void) const
get current time.
Definition popul_proc.h:107
popul_proc_t(raw_t *o)
constructor from serialized binary form
Definition popul_proc.h:75
popul_proc_t(double t0=0)
Definition popul_proc.h:69
size_t bytesize(void) const
size of serialized binary form
Definition popul_proc.h:40
double event_rates(double *rate, int n) const
compute event rates
int play(double tfin)
Definition popul_proc.h:173
void update_IVPs(double *, int)
set initial-value parameters
string_t yaml(string_t tab) const
machine/human readable info
void update_clocks(void)
updates clock and next event
Definition popul_proc.h:142
Rbyte raw_t
Definition internal.h:52
#define err(...)
Definition internal.h:18
double slate_t
Definition internal.h:53
static double event_rates(double *__x, const double *__p, double t, const int *__stateindex, const int *__parindex, double *rate, double *penalty)
Definition lbdp_pomp.c:19
#define n
Definition lbdp_pomp.c:9