libpspm
cohort.h
Go to the documentation of this file.
1 #ifndef PSPM_PSPM_COHORT_H_
2 #define PSPM_PSPM_COHORT_H_
3 
4 #include <iostream>
5 
6 template<class Ind>
7 class Cohort : public Ind {
8  public:
9  static int np, ng, nm, nf; // number of evaluations of demographic functions
10 
11  double x = -999;
12  double u = -999;
13  int id = 0;
14 
15  double birth_time = 0;
16  bool remove = false;
17 
18  bool need_precompute = true;
19 
20  // Default constructor simply calls Individual's default ctor
21  // NOTE: This means user's Ind class will need ctor with no arguments
22  Cohort() : Ind() {
23  }
24 
25  // Construct a cohort from Individual using copy constructor of Individual
26  Cohort(const Ind& _ind) : Ind(_ind){
27  }
28 
29  void print_xu(std::ostream &out = std::cout){
30  out << birth_time << "\t" << x << "\t" << u << "\t";
31  }
32 
33  void print(std::ostream &out = std::cout){
34  print_xu(out);
35  Ind::print(out);
36  }
37 
38  void set_size(double _x){
39  x = _x;
40  need_precompute = true; // when size is updated, next rate calc will need precompute
41  Ind::set_size(x);
42  }
43 
44 
45  // These are defined here so that precompute trigger can be
46  // checked before calling user-defined function
47  void preCompute(double x, double t, void * _env){
48  ++np;
49  //std::cout << "cohort precompute: "; print(); std::cout << "\n";
50  Ind::preCompute(x,t,_env);
51  need_precompute = false; // Once precompute is called, no need to further precompute until necessary
52  }
53 
54  double growthRate(double x, double t, void * _env){
55  ++ng;
56  if (need_precompute) preCompute(x,t,_env);
57  //std::cout << "cohort growthRate(): "; print(); std::cout << "\n";
58  return Ind::growthRate(x,t,_env);
59  }
60 
61  double mortalityRate(double x, double t, void * _env){
62  ++nm;
63  if (need_precompute) preCompute(x,t,_env);
64  //std::cout << "cohort mortRate(): "; print(); std::cout << "\n";
65  return Ind::mortalityRate(x,t,_env);
66  }
67 
68  double birthRate(double x, double t, void * _env){
69  ++nf;
70  if (need_precompute) preCompute(x,t,_env);
71  //std::cout << x << " cohort birthRate: "; print(); std::cout << "\n";
72  return Ind::birthRate(x,t,_env);
73  }
74 
75  // FIXME other env dependent rates should also check for precompute
76 };
77 
78 template<class Model>
79 int Cohort<Model>::np = 0;
80 
81 template<class Model>
82 int Cohort<Model>::ng = 0;
83 
84 template<class Model>
85 int Cohort<Model>::nm = 0;
86 
87 template<class Model>
88 int Cohort<Model>::nf = 0;
89 
90 #endif
91 
static int nm
Definition: cohort.h:9
static int np
Definition: cohort.h:9
Cohort()
Definition: cohort.h:22
Cohort(const Ind &_ind)
Definition: cohort.h:26
double x
Definition: cohort.h:11
Definition: cohort.h:7
void set_size(double _x)
Definition: cohort.h:38
double birth_time
Definition: cohort.h:15
bool need_precompute
Definition: cohort.h:18
void print_xu(std::ostream &out=std::cout)
Definition: cohort.h:29
double birthRate(double x, double t, void *_env)
Definition: cohort.h:68
double growthRate(double x, double t, void *_env)
Definition: cohort.h:54
static int nf
Definition: cohort.h:9
void preCompute(double x, double t, void *_env)
Definition: cohort.h:47
void print(std::ostream &out=std::cout)
Definition: cohort.h:33
static int ng
Definition: cohort.h:9
double mortalityRate(double x, double t, void *_env)
Definition: cohort.h:61
double u
Definition: cohort.h:12