phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sir.cc
Go to the documentation of this file.
1 // SIR: Classical susceptible-infected-recovered model (C++)
2 #include "master.h"
3 #include "popul_proc.h"
4 #include "generics.h"
5 #include "internal.h"
6 
7 static int Infected = 0;
8 
10 typedef struct {
11  int S;
12  int I;
13  int R;
14  double N;
15 } sir_state_t;
16 
18 typedef struct {
19  double Beta;
20  double gamma;
21  double psi;
22  double omega;
23  int S0;
24  int I0;
25  int R0;
27 
30 
31 template<>
32 std::string sir_proc_t::yaml (std::string tab) const {
33  std::string t = tab + " ";
34  std::string p = tab + "parameter:\n"
35  + YAML_PARAM(Beta)
36  + YAML_PARAM(gamma)
37  + YAML_PARAM(psi)
38  + YAML_PARAM(omega)
39  + YAML_PARAM(S0)
40  + YAML_PARAM(I0)
41  + YAML_PARAM(R0);
42  std::string s = tab + "state:\n"
43  + YAML_STATE(S)
44  + YAML_STATE(I)
45  + YAML_STATE(R)
46  + YAML_STATE(N);
47  return p+s;
48 }
49 
50 template<>
51 void sir_proc_t::update_params (double *p, int n) {
52  int m = 0;
53  PARAM_SET(Beta);
55  PARAM_SET(psi);
57  if (m != n) err("wrong number of parameters!");
58 }
59 
60 template<>
61 void sir_proc_t::update_IVPs (double *p, int n) {
62  int m = 0;
63  PARAM_SET(S0);
64  PARAM_SET(I0);
65  PARAM_SET(R0);
66  if (m != n) err("wrong number of initial-value parameters!");
67 }
68 
69 template<>
70 double sir_proc_t::event_rates (double *rate, int n) const {
71  int m = 0;
72  double total = 0;
73  RATE_CALC(params.Beta * state.S * state.I / state.N);
74  RATE_CALC(params.gamma * state.I);
75  RATE_CALC(params.psi * state.I);
76  RATE_CALC(params.omega * state.R);
77  if (m != n) err("wrong number of events!");
78  return total;
79 }
80 
81 template<>
82 void sir_genealogy_t::rinit (void) {
83  state.S = params.S0;
84  state.I = params.I0;
85  state.R = params.R0;
86  state.N = double(params.S0+params.I0+params.R0);
87  graft(Infected,params.I0);
88 }
89 
90 template<>
91 void sir_genealogy_t::jump (int event) {
92  switch (event) {
93  case 0:
94  state.S -= 1; state.I += 1; birth();
95  break;
96  case 1:
97  state.I -= 1; state.R += 1; death();
98  break;
99  case 2:
100  sample();
101  break;
102  case 3:
103  state.R -= 1; state.S += 1;
104  break;
105  default: // #nocov
106  assert(0); // #nocov
107  break; // #nocov
108  }
109 }
110 
Encodes the master process.
Definition: master.h:22
void birth(name_t i=0, name_t j=0, int n=1)
n births into deme j with parent in deme i
Definition: master.h:142
void sample(name_t i=0, int n=1)
sample in deme i
Definition: master.h:166
void death(name_t i=0)
death in deme i
Definition: master.h:153
void jump(int e)
makes a jump
Definition: lbdp.cc:72
void rinit(void)
initialize the state
Definition: lbdp.cc:66
void graft(name_t i=0, int m=1)
new root in deme i
Definition: master.h:159
Population process class.
Definition: popul_proc.h:20
void update_IVPs(double *, int)
set initial-value parameters
Definition: lbdp.cc:48
void update_params(double *, int)
set parameters
Definition: lbdp.cc:39
std::string yaml(std::string tab) const
machine/human readable info
Definition: lbdp.cc:26
parameters_t params
Definition: popul_proc.h:35
double event_rates(double *rate, int n) const
compute event rates
Definition: lbdp.cc:55
state_t state
Definition: popul_proc.h:34
#define GENERICS(X, TYPE)
Definition: generics.h:149
#define err(...)
Definition: internal.h:18
#define n
Definition: lbdp_pomp.c:8
#define psi
Definition: lbdp_pomp.c:6
#define YAML_PARAM(X)
Definition: popul_proc.h:180
#define RATE_CALC(X)
Definition: popul_proc.h:179
#define YAML_STATE(X)
Definition: popul_proc.h:181
#define PARAM_SET(X)
Definition: popul_proc.h:177
#define N
Definition: seirs_pomp.c:32
#define R0
Definition: seirs_pomp.c:31
#define gamma
Definition: seirs_pomp.c:25
#define R
Definition: seirs_pomp.c:36
#define S0
Definition: seirs_pomp.c:28
#define I
Definition: seirs_pomp.c:35
#define Beta
Definition: seirs_pomp.c:23
#define I0
Definition: seirs_pomp.c:30
#define omega
Definition: seirs_pomp.c:27
#define S
Definition: seirs_pomp.c:33
static int Infected
Definition: sir.cc:7
SIR process parameters.
Definition: sir.cc:18
double omega
Definition: sir.cc:22
double Beta
Definition: sir.cc:19
double gamma
Definition: sir.cc:20
double psi
Definition: sir.cc:21
SIR process state.
Definition: sir.cc:10
int S
Definition: sir.cc:11
int R
Definition: sir.cc:13
double N
Definition: sir.cc:14
int I
Definition: sir.cc:12