phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
master.h
Go to the documentation of this file.
1// -*- C++ -*-
8
9#ifndef _MASTER_H_
10#define _MASTER_H_
11
12#include "popul_proc.h"
13#include "genealogy.h"
14#include "inventory.h"
15#include "internal.h"
16
18
20template <class POPN, size_t NDEME>
21class master_t : public POPN {
22
23public:
24
25 typedef POPN popul_t;
26 const static size_t ndeme = NDEME;
27
28public:
29 // DATA MEMBERS
32
33public:
35 size_t bytesize (void) const {
36 return geneal.bytesize() + popul_t::bytesize();
37 };
38
39 friend raw_t* operator>> (const master_t& A, raw_t* o) {
40 o = (A.geneal >> o);
41 o = (reinterpret_cast<const popul_t&>(A) >> o);
42 return o;
43 }
44
45 friend raw_t* operator>> (raw_t* o, master_t& A) {
46 A.clean();
47 o = (o >> A.geneal);
48 o = (o >> reinterpret_cast<popul_t&>(A));
49 A.inventory = A.geneal;
50 return o;
51 }
52
53private:
54
55 void clean (void) { };
56
57public:
58 // CONSTRUCTORS, ETC.
61 master_t (double t0 = 0) : popul_t(t0), geneal(t0,ndeme) {};
64 o >> *this;
65 };
66
67 master_t (SEXP o) {
68 if (LENGTH(o)==0)
69 err("in %s: cannot deserialize a NULL.",__func__);
70 PROTECT(o = AS_RAW(o));
71 RAW(o) >> *this;
72 UNPROTECT(1);
73 };
74
75 master_t (const master_t& A) {
76 raw_t *o = new raw_t[A.bytesize()];
77 A >> o;
78 o >> *this;
79 delete[] o;
80 };
81
83 clean();
84 raw_t *o = new raw_t[A.bytesize()];
85 A >> o;
86 o >> *this;
87 delete[] o;
88 return *this;
89 };
90
91 master_t (master_t &&) = default;
93 master_t & operator= (master_t &&) = default;
95 ~master_t (void) {
96 clean();
97 };
98
99 slate_t timezero (void) const {
100 return geneal.timezero();
101 };
102
103 int play (double tfin) {
104 int count = popul_t::play(tfin);
105 geneal.time() = tfin;
106 return count;
107 };
108
109public:
111 slate_t time (void) const {
112 return popul_t::time();
113 };
114
115 string_t yaml (string_t tab = "") const {
116 string_t t = tab + " ";
117 string_t s = popul_t::yaml(tab)
118 + "genealogy:\n" + geneal.yaml(t);
119 return s;
120 };
121
122 string_t newick (void) const {
123 return geneal.newick();
124 };
125
126 SEXP lineage_count (void) const {
127 return geneal.lineage_count();
128 };
129
130 SEXP structure (void) const {
131 return geneal.structure();
132 };
133
134public:
136 void birth (name_t i = 1, name_t j = 1, int n = 1) {
137 ball_t *a = inventory.random_ball(i);
138 ball_t *b = geneal.birth(a,time(),j);
139 inventory.insert(b);
140 while (n > 1) {
141 b = geneal.birth(b->holder(),j);
142 inventory.insert(b);
143 n--;
144 }
145 };
146
147 void death (name_t i = 1) {
148 ball_t *a = inventory.random_ball(i);
149 inventory.erase(a);
150 geneal.death(a,time());
151 };
152
153 void graft (name_t i = 1, int m = 1) {
154 for (int j = 0; j < m; j++) {
155 ball_t *a = geneal.graft(time(),i);
156 inventory.insert(a);
157 }
158 };
159
160 void sample (name_t i = 1, int n = 1) {
161 pocket_t *p = inventory.random_balls(i,n);
162 for (ball_t *a : *p) {
163 geneal.sample(a,time());
164 }
165 p->clear();
166 delete p;
167 };
168
169 void sample_death (name_t i = 1, int n = 1) {
170 pocket_t *p = inventory.random_balls(i,n);
171 for (ball_t *a : *p) {
172 inventory.erase(a);
173 geneal.sample_death(a,time());
174 }
175 p->clear();
176 delete p;
177 };
178
179 void migrate (name_t i = 1, name_t j = 1) {
180 ball_t *a = inventory.random_ball(i);
181 inventory.erase(a);
182 geneal.migrate(a,time(),j);
183 inventory.insert(a);
184 };
185
186 void sample_migrate (name_t i = 1, name_t j = 1) {
187 ball_t *a = inventory.random_ball(i);
188 inventory.erase(a);
189 geneal.sample_migrate(a,time(),j);
190 inventory.insert(a);
191 };
192
193 void rinit (void);
195 void jump (int e);
196};
197
198#endif
Balls function as pointers.
Definition ball.h:27
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
Encodes a genealogy.
Definition genealogy.h:19
Implementation of the inventory process.
Definition inventory.h:18
slate_t time(void) const
current time
Definition master.h:111
void clean(void)
Definition master.h:55
string_t newick(void) const
tree in Newick format
Definition master.h:122
int play(double tfin)
runs the process to time tfin
Definition master.h:103
void jump(int e)
makes a jump
master_t(raw_t *o)
constructor from serialized binary form
Definition master.h:63
master_t(master_t &&)=default
move constructor
string_t yaml(string_t tab="") const
human/machine readable info
Definition master.h:115
master_t(double t0=0)
Definition master.h:61
slate_t timezero(void) const
get zero-time
Definition master.h:99
master_t(const master_t &A)
copy constructor
Definition master.h:75
static const size_t ndeme
Definition master.h:26
inventory_t< NDEME > inventory
Definition master.h:31
master_t(SEXP o)
constructor from RAW SEXP (containing binary serialization)
Definition master.h:67
void rinit(void)
initialize the state
void graft(name_t i=1, int m=1)
new root in deme i
Definition master.h:153
~master_t(void)
destructor
Definition master.h:95
master_t & operator=(const master_t &A)
copy assignment operator
Definition master.h:82
size_t bytesize(void) const
size of serialized binary form
Definition master.h:35
void sample_migrate(name_t i=1, name_t j=1)
sample_migrate in deme i to deme j
Definition master.h:186
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
SEXP lineage_count(void) const
lineage count table
Definition master.h:126
friend raw_t * operator>>(const master_t &A, raw_t *o)
binary serialization
Definition master.h:39
void death(name_t i=1)
death in deme i
Definition master.h:147
SEXP structure(void) const
structure in R list format
Definition master.h:130
void migrate(name_t i=1, name_t j=1)
migration from deme i to deme j
Definition master.h:179
A pocket is a set of balls.
Definition pocket.h:30
Rbyte raw_t
Definition internal.h:52
size_t name_t
Definition internal.h:54
#define err(...)
Definition internal.h:18
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9