phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
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
7static const int Infected = 1;
8
10typedef struct {
11 int S;
12 int I;
13 int R;
15
17typedef struct {
18 double Beta;
19 double gamma;
20 double psi;
21 double chi;
22 double omega;
23 double pop;
24 double S0;
25 double I0;
26 double R0;
28
31
32template<>
33std::string sir_proc_t::yaml (std::string tab) const {
34 std::string t = tab + " ";
35 std::string p = tab + "parameter:\n"
41 + YAML_PARAM(pop)
42 + YAML_PARAM(S0)
43 + YAML_PARAM(I0)
44 + YAML_PARAM(R0);
45 std::string s = tab + "state:\n"
46 + YAML_STATE(S)
47 + YAML_STATE(I)
48 + YAML_STATE(R);
49 return p+s;
50}
51
52template<>
53void sir_proc_t::update_params (double *p, int n) {
54 int m = 0;
60 if (m != n) err("wrong number of parameters!");
61}
62
63template<>
64void sir_proc_t::update_IVPs (double *p, int n) {
65 int m = 0;
66 PARAM_SET(pop);
70 if (m != n) err("wrong number of initial-value parameters!");
71}
72
73template<>
74double sir_proc_t::event_rates (double *rate, int n) const {
75 int m = 0;
76 double total = 0;
77 RATE_CALC(params.Beta * state.S * state.I / params.pop);
78 RATE_CALC(params.gamma * state.I);
79 RATE_CALC(params.psi * state.I);
80 RATE_CALC(params.chi * state.I);
81 RATE_CALC(params.omega * state.R);
82 if (m != n) err("wrong number of events!");
83 return total;
84}
85
86template<>
88 double f = params.pop/(params.S0+params.I0+params.R0);
89 state.S = nearbyint(f*params.S0);
90 state.I = nearbyint(f*params.I0);
91 state.R = nearbyint(f*params.R0);
93}
94
95template<>
97 switch (event) {
98 case 0:
99 state.S -= 1; state.I += 1; birth();
100 break;
101 case 1:
102 state.I -= 1; state.R += 1; death();
103 break;
104 case 2:
105 sample();
106 break;
107 case 3:
108 state.I -= 1; state.R += 1; sample_death();
109 break;
110 case 4:
111 state.R -= 1; state.S += 1;
112 break;
113 default: // #nocov
114 assert(0); // #nocov
115 break; // #nocov
116 }
117}
118
Encodes the master process.
Definition master.h:21
void jump(int e)
Definition sir.cc:96
void graft(name_t i=1, int m=1)
new root in deme i
Definition master.h:153
void sample(name_t i=1, int n=1)
sample in deme i
Definition master.h:160
void birth(name_t i=1, name_t j=1, int n=1)
n births into deme j with parent in deme i
Definition master.h:136
void sample_death(name_t i=1, int n=1)
sample_death in deme i
Definition master.h:169
void death(name_t i=1)
death in deme i
Definition master.h:147
Population process class.
Definition popul_proc.h:16
double event_rates(double *rate, int n) const
Definition sir.cc:74
#define GENERICS(X, TYPE)
Definition generics.h:133
#define err(...)
Definition internal.h:18
#define chi
Definition lbdp_pomp.c:7
#define n
Definition lbdp_pomp.c:9
#define psi
Definition lbdp_pomp.c:6
#define YAML_PARAM(X)
Definition popul_proc.h:136
#define RATE_CALC(X)
Definition popul_proc.h:135
#define YAML_STATE(X)
Definition popul_proc.h:137
#define PARAM_SET(X)
Definition popul_proc.h:134
#define R0
Definition seirs_pomp.c:36
#define gamma
Definition seirs_pomp.c:29
#define R
Definition seirs_pomp.c:41
#define S0
Definition seirs_pomp.c:33
#define I
Definition seirs_pomp.c:40
#define Beta
Definition seirs_pomp.c:27
#define I0
Definition seirs_pomp.c:35
#define Infected
Definition seirs_pomp.c:5
#define omega
Definition seirs_pomp.c:32
#define S
Definition seirs_pomp.c:38
master_t< sir_proc_t, 1 > sir_genealogy_t
Definition sir.cc:30
popul_proc_t< sir_state_t, sir_parameters_t, 5 > sir_proc_t
Definition sir.cc:29
SIR process parameters.
Definition sir.cc:17
double chi
Definition sir.cc:21
double R0
Definition sir.cc:26
double omega
Definition sir.cc:22
double I0
Definition sir.cc:25
double Beta
Definition sir.cc:18
double gamma
Definition sir.cc:19
double pop
Definition sir.cc:23
double S0
Definition sir.cc:24
double psi
Definition sir.cc:20
SIR process state.
Definition sir.cc:10
int S
Definition sir.cc:11
int R
Definition sir.cc:13
int I
Definition sir.cc:12