phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
master.h
Go to the documentation of this file.
1 // -*- C++ -*-
8 
9 #ifndef _MASTER_H_
10 #define _MASTER_H_
11 
12 #include <string>
13 #include <cstring>
14 #include "popul_proc.h"
15 #include "genealogy.h"
16 #include "internal.h"
17 
19 
21 template <class POPN, size_t NDEME = 1>
22 class master_t : public POPN {
23 
24 public:
25 
26  typedef POPN popul_t;
27  const static size_t ndeme = NDEME;
28 
29 public:
30  // DATA MEMBERS
33 
34 public:
36  size_t bytesize (void) const {
37  return popul_t::bytesize() + geneal.bytesize();
38  };
40  friend raw_t* operator>> (const master_t& A, raw_t* o) {
41  o = (reinterpret_cast<const popul_t&>(A) >> o);
42  o = (A.geneal >> o);
43  return o;
44  }
46  friend raw_t* operator>> (raw_t* o, master_t& A) {
47  A.clean();
48  o = (o >> reinterpret_cast<popul_t&>(A));
49  o = (o >> A.geneal);
50  A.inventory = A.geneal.extant();
51  return o;
52  }
53 
54 private:
55 
56  void clean (void) { };
57 
58 public:
59  // CONSTRUCTORS, ETC.
62  master_t (double t0 = 0) : popul_t(t0), geneal(t0,0,ndeme) {};
64  master_t (raw_t *o) {
65  o >> *this;
66  };
68  master_t (SEXP o) {
69  if (LENGTH(o)==0)
70  err("in %s (%s line %d): cannot deserialize a NULL.",
71  __func__,__FILE__,__LINE__);
72  PROTECT(o = AS_RAW(o));
73  RAW(o) >> *this;
74  UNPROTECT(1);
75  };
77  master_t (const master_t& A) {
78  raw_t *o = new raw_t[A.bytesize()];
79  A >> o;
80  o >> *this;
81  delete[] o;
82  };
85  clean();
86  raw_t *o = new raw_t[A.bytesize()];
87  A >> o;
88  o >> *this;
89  delete[] o;
90  return *this;
91  };
93  master_t (master_t &&) = default;
95  master_t & operator= (master_t &&) = default;
97  ~master_t (void) {
98  clean();
99  };
101  slate_t timezero (void) const {
102  return geneal.timezero();
103  };
105  int play (double tfin) {
106  int count = popul_t::play(tfin);
107  geneal.time() = tfin;
108  return count;
109  };
110 
111 public:
113  slate_t time (void) const {
114  return popul_t::time();
115  };
117  std::string describe (void) const {
118  return geneal.describe();
119  };
121  std::string yaml (std::string tab = "") const {
122  std::string t = tab + " ";
123  std::string s = popul_t::yaml(tab)
124  + "genealogy:\n" + geneal.yaml(t);
125  return s;
126  };
128  std::string newick (void) const {
129  return geneal.newick();
130  };
132  SEXP lineage_count (void) const {
133  return geneal.lineage_count();
134  };
136  SEXP structure (void) const {
137  return geneal.structure();
138  };
139 
140 public:
142  void birth (name_t i = 0, name_t j = 0, int n = 1) {
143  ball_t *a = inventory.random_ball(i);
144  ball_t *b = geneal.birth(a,time(),j);
145  inventory.insert(b);
146  while (n > 1) {
147  b = geneal.birth(b->holder(),j);
148  inventory.insert(b);
149  n--;
150  }
151  };
153  void death (name_t i = 0) {
154  ball_t *a = inventory.random_ball(i);
155  inventory.erase(a);
156  geneal.death(a,time());
157  };
159  void graft (name_t i = 0, int m = 1) {
160  for (int j = 0; j < m; j++) {
161  ball_t *a = geneal.graft(time(),i);
162  inventory.insert(a);
163  }
164  };
166  void sample (name_t i = 0, int n = 1) {
168  for (ball_it a = p->begin(); a != p->end(); a++) {
169  geneal.sample(*a,time());
170  }
171  p->clear();
172  delete p;
173  };
175  void sample_death (name_t i = 0, int n = 1) {
177  for (ball_it a = p->begin(); a != p->end(); a++) {
178  inventory.erase(*a);
179  geneal.sample_death(*a,time());
180  }
181  p->clear();
182  delete p;
183  };
185  void migrate (name_t i = 0, name_t j = 0) {
186  ball_t *a = inventory.random_ball(i);
187  inventory.erase(a);
188  geneal.migrate(a,time(),j);
189  a->deme() = j;
190  inventory.insert(a);
191  };
193  void rinit (void);
195  void jump (int e);
196 };
197 
198 #endif
SEXP yaml(SEXP State)
extract a YAML description
Definition: bare.cc:21
Balls function as pointers.
Definition: ball.h:29
name_t deme(void) const
view deme
Definition: ball.h:86
node_t * holder(void) const
in whose pocket do I lie?
Definition: ball.h:109
Encodes a genealogy.
Definition: genealogy.h:22
ball_t * birth(ball_t *a, slate_t t, name_t d)
birth into deme d
Definition: genealogy.h:401
SEXP structure(void) const
R list description.
Definition: genealogy.h:322
std::string newick(void) const
put genealogy at current time into Newick format.
Definition: genealogy.h:364
virtual std::string yaml(std::string tab="") const
machine-readable info
Definition: genealogy.h:352
void death(ball_t *a, slate_t t)
death
Definition: genealogy.h:417
size_t bytesize(void) const
size of serialized binary form
Definition: genealogy.h:73
std::string describe(void) const
human-readable info
Definition: genealogy.h:342
std::pair< node_it, node_it > extant(void) const
Definition: genealogy.h:461
slate_t timezero(void) const
get zero time.
Definition: genealogy.h:158
slate_t & time(void)
view/set current time.
Definition: genealogy.h:150
ball_t * migrate(ball_t *a, slate_t t, name_t d=0)
movement into deme d
Definition: genealogy.h:451
void sample_death(ball_t *a, slate_t t)
insert a sample node and simultaneously terminate the lineage
Definition: genealogy.h:441
ball_t * graft(slate_t t, name_t d)
graft a new lineage into deme d
Definition: genealogy.h:422
void sample(ball_t *a, slate_t t)
insert a sample node
Definition: genealogy.h:432
void lineage_count(double *tout, int *deme, int *ell, int *sat, int *etype) const
Definition: genealogy.h:171
void insert(ball_t *b)
Definition: inventory.h:160
pocket_t * random_balls(name_t i=0, int n=1) const
choose a random set of n balls from deme i
Definition: inventory.h:133
ball_t * random_ball(name_t i=0) const
choose one random ball from deme i
Definition: inventory.h:124
void erase(ball_t *b)
Definition: inventory.h:168
Encodes the master process.
Definition: master.h:22
std::string yaml(std::string tab="") const
machine/human readable info
Definition: master.h:121
slate_t time(void) const
current time
Definition: master.h:113
void clean(void)
Definition: master.h:56
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
int play(double tfin)
runs the process to time tfin
Definition: master.h:105
master_t & operator=(const master_t &A)
copy assignment operator
Definition: master.h:84
friend raw_t * operator>>(const master_t &A, raw_t *o)
binary serialization
Definition: master.h:40
std::string describe(void) const
human-readable info
Definition: master.h:117
void sample(name_t i=0, int n=1)
sample in deme i
Definition: master.h:166
master_t(raw_t *o)
constructor from serialized binary form
Definition: master.h:64
void death(name_t i=0)
death in deme i
Definition: master.h:153
master_t(master_t &&)=default
move constructor
genealogy_t geneal
Definition: master.h:31
void jump(int e)
makes a jump
Definition: lbdp.cc:72
void rinit(void)
initialize the state
Definition: lbdp.cc:66
master_t(double t0=0)
Definition: master.h:62
slate_t timezero(void) const
get zero-time
Definition: master.h:101
void graft(name_t i=0, int m=1)
new root in deme i
Definition: master.h:159
POPN popul_t
Definition: master.h:26
std::string newick(void) const
tree in Newick format
Definition: master.h:128
master_t(const master_t &A)
copy constructor
Definition: master.h:77
static const size_t ndeme
Definition: master.h:27
master_t(SEXP o)
constructor from RAW SEXP (containing binary serialization)
Definition: master.h:68
inventory_t< ndeme > inventory
Definition: master.h:32
~master_t(void)
destructor
Definition: master.h:97
size_t bytesize(void) const
size of serialized binary form
Definition: master.h:36
SEXP lineage_count(void) const
lineage count table
Definition: master.h:132
void migrate(name_t i=0, name_t j=0)
migration from deme i to deme j
Definition: master.h:185
void sample_death(name_t i=0, int n=1)
sample_death in deme i
Definition: master.h:175
SEXP structure(void) const
structure in R list format
Definition: master.h:136
A pocket is a set of balls.
Definition: pocket.h:32
SEXP time(TYPE &X)
Definition: generics.h:22
Rbyte raw_t
Definition: internal.h:43
size_t name_t
Definition: internal.h:45
#define err(...)
Definition: internal.h:18
double slate_t
Definition: internal.h:44
#define n
Definition: lbdp_pomp.c:8
std::set< ball_t *, ball_order >::const_iterator ball_it
Definition: pocket.h:26