libpspm
Species< Model > Class Template Reference

#include <species.h>

Inheritance diagram for Species< Model >:
[legend]
Collaboration diagram for Species< Model >:
[legend]

Public Member Functions

 Species (std::vector< double > breaks=std::vector< double >())
 
 Species (Model M)
 
void resize (int _J)
 
double get_maxSize ()
 
void print ()
 
void set_xb (double _xb)
 
void set_ub (double _ub)
 
void set_birthTime (int i, double t0)
 
void setX (int i, double _x)
 
void setU (int i, double _u)
 
double getX (int i)
 
double getU (int i)
 
double init_density (int i, double x, void *env)
 
void initAndCopyExtraState (double t, void *env, std::vector< double >::iterator &it)
 
void initBoundaryCohort (double t, void *env)
 
void copyExtraStateToCohorts (std::vector< double >::iterator &it)
 
void copyCohortsExtraToState (std::vector< double >::iterator &it)
 
double establishmentProbability (double t, void *env)
 
double calc_boundary_u (double gb, double pe)
 
double get_boundary_u ()
 
void triggerPreCompute ()
 
double growthRate (int i, double x, double t, void *env)
 
double growthRateOffset (int i, double x, double t, void *env)
 
std::vector< double > growthRateGradient (int i, double x, double t, void *env, double grad_dx)
 
std::vector< double > growthRateGradientCentered (int i, double xplus, double xminus, double t, void *env)
 
double mortalityRate (int i, double x, double t, void *env)
 
std::vector< double > mortalityRateGradient (int i, double x, double t, void *env, double grad_dx)
 
double birthRate (int i, double x, double t, void *env)
 
void getExtraRates (std::vector< double >::iterator &it)
 
void addCohort ()
 
void addCohort (Cohort< Model > bc)
 
void removeDensestCohort ()
 
void removeDenseCohorts (double dxcut)
 
void removeDeadCohorts (double ucut)
 
Cohort< Model > & getCohort (int i)
 
- Public Member Functions inherited from Species_Base
virtual ~Species_Base ()=0
 
int xsize ()
 
int size ()
 
void set_inputBirthFlux (double b)
 
void set_bfin_is_u0in (bool flag)
 
template<class T >
void addCohort (T bc)
 

Protected Attributes

std::vector< Cohort< Model > > cohorts
 
Cohort< Model > boundaryCohort
 
Cohort< Model > savedCohort
 
- Protected Attributes inherited from Species_Base
int J
 
int n_extra_statevars = 0
 
std::list< double > birth_flux_out_history
 
std::vector< double > X
 
std::vector< double > x
 
std::vector< double > h
 
std::vector< double > schedule
 

Additional Inherited Members

- Public Attributes inherited from Species_Base
double birth_flux_in
 
bool bfin_is_u0in = false
 
double xb
 
bool is_resident
 

Detailed Description

template<class Model>
class Species< Model >

Definition at line 107 of file species.h.

Constructor & Destructor Documentation

◆ Species() [1/2]

template<class Model >
Species< Model >::Species ( std::vector< double >  breaks = std::vector<double>())

Definition at line 36 of file species.tpp.

36  {
37  J = breaks.size(); // changes with
38  cohorts.resize(J, boundaryCohort);
39  for (int i=0; i<J; ++i) cohorts[i].x = breaks[i];
40 }
Cohort< Model > boundaryCohort
Definition: species.h:110
std::vector< double > x
Definition: species.h:29
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ Species() [2/2]

template<class Model >
Species< Model >::Species ( Model  M)

Definition at line 44 of file species.tpp.

44  {
46 }
Cohort< Model > savedCohort
Definition: species.h:112
Cohort< Model > boundaryCohort
Definition: species.h:110

Member Function Documentation

◆ addCohort() [1/2]

template<class Model >
void Species< Model >::addCohort ( )
virtual

Implements Species_Base.

Definition at line 305 of file species.tpp.

305  {
306  cohorts.push_back(boundaryCohort);
307  ++J;
308 }
Cohort< Model > boundaryCohort
Definition: species.h:110
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ addCohort() [2/2]

template<class Model >
void Species< Model >::addCohort ( Cohort< Model >  bc)

Definition at line 315 of file species.tpp.

315  {
316  cohorts.push_back(bc);
317  ++J;
318  // FIXME: add option to sort cohorts here.
319 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ birthRate()

template<class Model >
double Species< Model >::birthRate ( int  i,
double  x,
double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 292 of file species.tpp.

292  {
293  Cohort<Model> &c = (i<0)? boundaryCohort : cohorts[i];
294  return c.birthRate(c.x,t,env);
295 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
double birthRate(double x, double t, void *_env)
Definition: cohort.h:68
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ calc_boundary_u()

template<class Model >
double Species< Model >::calc_boundary_u ( double  gb,
double  pe 
)
virtual

Implements Species_Base.

Definition at line 205 of file species.tpp.

205  {
206  std::cout << "calc_boundary_u\n";
207  if (bfin_is_u0in){
209  }
210  else {
211  boundaryCohort.u = (gb>0)? birth_flux_in * pe/gb : 0;
212  }
213  return boundaryCohort.u;
214 }
Cohort< Model > boundaryCohort
Definition: species.h:110
bool bfin_is_u0in
Definition: species.h:37
double birth_flux_in
Definition: species.h:34
double u
Definition: cohort.h:12

◆ copyCohortsExtraToState()

template<class Model >
void Species< Model >::copyCohortsExtraToState ( std::vector< double >::iterator &  it)
virtual

Implements Species_Base.

Definition at line 185 of file species.tpp.

185  {
186  for (auto& c : cohorts) c.get_state(it);
187 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ copyExtraStateToCohorts()

template<class Model >
void Species< Model >::copyExtraStateToCohorts ( std::vector< double >::iterator &  it)
virtual

Implements Species_Base.

Definition at line 179 of file species.tpp.

179  {
180  for (auto& c : cohorts) c.set_state(it);
181 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ establishmentProbability()

template<class Model >
double Species< Model >::establishmentProbability ( double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 198 of file species.tpp.

198  {
199  return boundaryCohort.establishmentProbability(t, env);
200 }
Cohort< Model > boundaryCohort
Definition: species.h:110

◆ get_boundary_u()

template<class Model >
double Species< Model >::get_boundary_u ( )
virtual

Implements Species_Base.

Definition at line 113 of file species.tpp.

113  {
114  return boundaryCohort.u;
115 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double u
Definition: cohort.h:12

◆ get_maxSize()

template<class Model >
double Species< Model >::get_maxSize ( )
virtual

Implements Species_Base.

Definition at line 25 of file species.tpp.

25  { // TODO ALERT: make sure this sees the latest state
26  if (!X.empty()) return *x.rbegin(); // for FMU, get this from X
27  else if (cohorts.empty()) return 0;
28  else { // else get from state vector
29  return cohorts[0].x; // cohorts are sorted descending
30  }
31 }
std::vector< double > x
Definition: species.h:29
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
std::vector< double > X
Definition: species.h:28

◆ getCohort()

template<class Model >
Cohort< Model > & Species< Model >::getCohort ( int  i)

Definition at line 94 of file species.tpp.

94  {
95  if (i == -1) return boundaryCohort;
96  else return cohorts[i];
97 }
Cohort< Model > boundaryCohort
Definition: species.h:110
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ getExtraRates()

template<class Model >
void Species< Model >::getExtraRates ( std::vector< double >::iterator &  it)
virtual

Implements Species_Base.

Definition at line 299 of file species.tpp.

299  {
300  for (auto& c : cohorts) c.get_rates(it);
301 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ getU()

template<class Model >
double Species< Model >::getU ( int  i)
virtual

Implements Species_Base.

Definition at line 107 of file species.tpp.

107  {
108  return cohorts[i].u;
109 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ getX()

template<class Model >
double Species< Model >::getX ( int  i)
virtual

Implements Species_Base.

Definition at line 100 of file species.tpp.

100  {
101  if (i == -1) return boundaryCohort.x;
102  else return cohorts[i].x;
103 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ growthRate()

template<class Model >
double Species< Model >::growthRate ( int  i,
double  x,
double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 218 of file species.tpp.

218  {
219  Cohort<Model> &c = (i<0)? boundaryCohort : cohorts[i];
220  return c.growthRate(c.x,t,env);
221 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
double growthRate(double x, double t, void *_env)
Definition: cohort.h:54
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ growthRateGradient()

template<class Model >
std::vector< double > Species< Model >::growthRateGradient ( int  i,
double  x,
double  t,
void *  env,
double  grad_dx 
)
virtual

Implements Species_Base.

Definition at line 235 of file species.tpp.

235  {
236  Cohort<Model> &c = (i<0)? boundaryCohort : cohorts[i];
237 
238  Cohort<Model> cplus = c;
239  cplus.set_size(c.x + grad_dx);
240  //cplus.preCompute(cplus.x,t,env);
241 
242  double g = c.growthRate(c.x,t,env);
243  double gplus = cplus.growthRate(cplus.x, t, env);
244 
245  return {g, (gplus-g)/grad_dx};
246 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
void set_size(double _x)
Definition: cohort.h:38
double growthRate(double x, double t, void *_env)
Definition: cohort.h:54
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ growthRateGradientCentered()

template<class Model >
std::vector< double > Species< Model >::growthRateGradientCentered ( int  i,
double  xplus,
double  xminus,
double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 250 of file species.tpp.

250  {
251  assert(i>=0);
252 
253  Cohort<Model> cplus = cohorts[i];
254  cplus.set_size(xplus);
255  //cplus.preCompute(cplus.x,t,env);
256 
257  Cohort<Model> cminus = cohorts[i];
258  cminus.set_size(xminus);
259  //cminus.preCompute(cminus.x,t,env);
260 
261  double gplus = cplus.growthRate(cplus.x, t, env);
262  double gminus = cminus.growthRate(cminus.x, t, env);
263 
264  return {gplus, gminus};
265 }
double x
Definition: cohort.h:11
void set_size(double _x)
Definition: cohort.h:38
double growthRate(double x, double t, void *_env)
Definition: cohort.h:54
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ growthRateOffset()

template<class Model >
double Species< Model >::growthRateOffset ( int  i,
double  x,
double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 225 of file species.tpp.

225  {
226  Cohort<Model> coff = (i<0)? boundaryCohort : cohorts[i];
227  coff.set_size(x);
228  //coff.preCompute(coff.x,t,env);
229 
230  return coff.growthRate(coff.x,t,env);
231 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
void set_size(double _x)
Definition: cohort.h:38
std::vector< double > x
Definition: species.h:29
double growthRate(double x, double t, void *_env)
Definition: cohort.h:54
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ init_density()

template<class Model >
double Species< Model >::init_density ( int  i,
double  x,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 173 of file species.tpp.

173  {
174  return cohorts[i].init_density(_x, _env, birth_flux_in);
175 }
double birth_flux_in
Definition: species.h:34
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ initAndCopyExtraState()

template<class Model >
void Species< Model >::initAndCopyExtraState ( double  t,
void *  env,
std::vector< double >::iterator &  it 
)
virtual

Implements Species_Base.

Definition at line 152 of file species.tpp.

152  {
153  // init boundary cohort (no copy required)
154  boundaryCohort.init_state(t, env);
155  // init internal cohorts and copy to state vector
156  for (auto& c : cohorts){
157  c.init_state(t, env); // init state
158  auto it_prev = it;
159  it = c.get_state(it); // copy the initialized state into state vector
160  assert(distance(it_prev, it) == n_extra_statevars);
161  }
162 }
Cohort< Model > boundaryCohort
Definition: species.h:110
int n_extra_statevars
Definition: species.h:21
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ initBoundaryCohort()

template<class Model >
void Species< Model >::initBoundaryCohort ( double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 166 of file species.tpp.

166  {
168  boundaryCohort.init_state(t, env);
169 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double birth_time
Definition: cohort.h:15

◆ mortalityRate()

template<class Model >
double Species< Model >::mortalityRate ( int  i,
double  x,
double  t,
void *  env 
)
virtual

Implements Species_Base.

Definition at line 269 of file species.tpp.

269  {
270  assert(i>=0);
271  Cohort<Model> &c = cohorts[i];
272  return c.mortalityRate(c.x,t,env);
273 }
double x
Definition: cohort.h:11
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
double mortalityRate(double x, double t, void *_env)
Definition: cohort.h:61
Here is the call graph for this function:

◆ mortalityRateGradient()

template<class Model >
std::vector< double > Species< Model >::mortalityRateGradient ( int  i,
double  x,
double  t,
void *  env,
double  grad_dx 
)
virtual

Implements Species_Base.

Definition at line 277 of file species.tpp.

277  {
278  Cohort<Model> &c = (i<0)? boundaryCohort : cohorts[i];
279 
280  Cohort<Model> cplus = c;
281  cplus.set_size(x + grad_dx);
282  //cplus.preCompute(cplus.x,t,env);
283 
284  double g = c.mortalityRate(c.x,t,env);
285  double gplus = cplus.mortalityRate(cplus.x, t, env);
286 
287  return {g, (gplus-g)/grad_dx};
288 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
void set_size(double _x)
Definition: cohort.h:38
std::vector< double > x
Definition: species.h:29
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
double mortalityRate(double x, double t, void *_env)
Definition: cohort.h:61
Here is the call graph for this function:

◆ print()

template<class Model >
void Species< Model >::print ( )
virtual

Implements Species_Base.

Definition at line 50 of file species.tpp.

50  {
51  std::cout << "~~~~~ Species ~~~~~\n";
52  //auto iset = get_iterators(sv);
53  //std::cout << "start index = " << start_index <<"\n";
54  //std::cout << "Model = " << mod << "\n";
55  std::cout << "xb = " << boundaryCohort.x << " / " << xb << "\n";
56  std::cout << "xsize = " << J << "\n";
57  std::cout << "Extra state variables: " << n_extra_statevars << "\n";
58  std::cout << "Input birth flux = " << birth_flux_in << "\n";
59  //if (!X.empty()){
60  // iset.push_back("_X", X.begin(),1);
61  // iset.push_back("_h", h.begin(),1);
62  std::cout << "x (" << x.size() << "): ";
63  for (auto xx : x) std::cout << xx << " ";
64  std::cout << "\n";
65  //}
66 
67  std::cout << "Cohorts: (" << cohorts.size() << ")\n";
68  std::cout << "t0" << "\tx" << "\t" << "u" << "\t";
69  if (!cohorts.empty()){
70  for (auto s : cohorts[0].varnames) std::cout << s << "\t";
71  }
72  std::cout << "\n";
73  for (auto& c : cohorts) {
74  //c.print_xu(); //std::cout << c.x << "\t" << c.u << "\t";
75  c.print();
76  std::cout << "\n";
77  }
78  std::cout << "- - - - - - - - - - - - - - - - - - - - - - - \n";
79  //boundaryCohort.print_xu(); //std::cout << c.x << "\t" << c.u << "\t";
81  std::cout << "\n";
82  std::cout << "Max size = " << get_maxSize() << "\n";
83  //std::cout << "State (" << size() << "):\n";
84  //iset.print();
85 
86  //std::cout << "Rates (" << size() << "):\n";
87  //auto irates = get_iterators(rv);
88  //irates.print();
89  std::cout << "-------\n\n"; std::cout.flush();
90 
91 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
int n_extra_statevars
Definition: species.h:21
std::vector< double > x
Definition: species.h:29
double get_maxSize()
Definition: species.tpp:25
double birth_flux_in
Definition: species.h:34
double xb
Definition: species.h:41
void print(std::ostream &out=std::cout)
Definition: cohort.h:33
std::vector< Cohort< Model > > cohorts
Definition: species.h:109
Here is the call graph for this function:

◆ removeDeadCohorts()

template<class Model >
void Species< Model >::removeDeadCohorts ( double  ucut)
virtual

Implements Species_Base.

Definition at line 360 of file species.tpp.

360  {
361  // mark cohorts to remove; skip pi0-cohort (index J-1)
362  for (int i=0; i<J-1; ++i){
363  if (cohorts[i].u < ucut) cohorts[i].remove = true;
364  }
365 
366  // remove marked cohorts
367  auto it_end = std::remove_if(cohorts.begin(), cohorts.end(), [](Cohort<Model> &c){return c.remove;});
368  cohorts.erase(it_end, cohorts.end());
369 
370  // reset size
371  J = cohorts.size();
372 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ removeDenseCohorts()

template<class Model >
void Species< Model >::removeDenseCohorts ( double  dxcut)
virtual

Implements Species_Base.

Definition at line 341 of file species.tpp.

341  {
342  // mark cohorts to remove; skip 1st and last cohort
343  for (int i=1; i<J-1; i+=2){
344  double dx_lo = cohorts[i-1].x-cohorts[i].x;
345  double dx_hi = cohorts[i].x-cohorts[i+1].x;
346 
347  if (dx_lo < dxcut || dx_hi < dxcut) cohorts[i].remove = true;
348  }
349 
350  // remove marked cohorts
351  auto it_end = std::remove_if(cohorts.begin(), cohorts.end(), [](Cohort<Model> &c){return c.remove;});
352  cohorts.erase(it_end, cohorts.end());
353 
354  // reset size
355  J = cohorts.size();
356 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ removeDensestCohort()

template<class Model >
void Species< Model >::removeDensestCohort ( )
virtual

Implements Species_Base.

Definition at line 323 of file species.tpp.

323  {
324  if (cohorts.size() < 3) return; // do nothing if there are 2 or fewer cohorts
325  int i_min = 1;
326  double dx_min = cohorts[0].x - cohorts[2].x;
327  for (int i=1; i<J-1; ++i){ // skip first and last cohorts
328  double dx = cohorts[i-1].x - cohorts[i+1].x;
329  if (dx < dx_min){
330  dx_min = dx;
331  i_min = i;
332  }
333  }
334 
335  cohorts.erase(cohorts.begin()+i_min);
336  --J;
337 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ resize()

template<class Model >
void Species< Model >::resize ( int  _J)
virtual

Implements Species_Base.

Definition at line 18 of file species.tpp.

18  {
19  J = _J;
20  cohorts.resize(J, boundaryCohort); // when resizing, always insert copies of boundary cohort
21 }
Cohort< Model > boundaryCohort
Definition: species.h:110
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ set_birthTime()

template<class Model >
void Species< Model >::set_birthTime ( int  i,
double  t0 
)
virtual

Implements Species_Base.

Definition at line 133 of file species.tpp.

133  {
134  cohorts[i].birth_time = t0;
135 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ set_ub()

template<class Model >
void Species< Model >::set_ub ( double  _ub)
virtual

Implements Species_Base.

Definition at line 127 of file species.tpp.

127  {
128  boundaryCohort.u = _ub;
129 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double u
Definition: cohort.h:12

◆ set_xb()

template<class Model >
void Species< Model >::set_xb ( double  _xb)
virtual

Implements Species_Base.

Definition at line 119 of file species.tpp.

119  {
120  xb = _xb;
121  boundaryCohort.x = _xb;
123 }
Cohort< Model > boundaryCohort
Definition: species.h:110
double x
Definition: cohort.h:11
void set_size(double _x)
Definition: cohort.h:38
double xb
Definition: species.h:41

◆ setU()

template<class Model >
void Species< Model >::setU ( int  i,
double  _u 
)
virtual

Implements Species_Base.

Definition at line 146 of file species.tpp.

146  {
147  cohorts[i].u = _u;
148 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ setX()

template<class Model >
void Species< Model >::setX ( int  i,
double  _x 
)
virtual

Implements Species_Base.

Definition at line 139 of file species.tpp.

139  {
140  cohorts[i].x = _x;
141  cohorts[i].set_size(_x);
142 }
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

◆ triggerPreCompute()

template<class Model >
void Species< Model >::triggerPreCompute ( )
virtual

Implements Species_Base.

Definition at line 191 of file species.tpp.

191  {
192  for (auto& c : cohorts) c.need_precompute = true;
194 }
Cohort< Model > boundaryCohort
Definition: species.h:110
bool need_precompute
Definition: cohort.h:18
std::vector< Cohort< Model > > cohorts
Definition: species.h:109

Member Data Documentation

◆ boundaryCohort

template<class Model >
Cohort<Model> Species< Model >::boundaryCohort
protected

Definition at line 110 of file species.h.

◆ cohorts

template<class Model >
std::vector<Cohort<Model> > Species< Model >::cohorts
protected

Definition at line 109 of file species.h.

◆ savedCohort

template<class Model >
Cohort<Model> Species< Model >::savedCohort
protected

Definition at line 112 of file species.h.


The documentation for this class was generated from the following files: