libpspm
cm.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cassert>
3 
4 #include "solver.h"
5 //#include "vector_insert.h"
6 using namespace std;
7 
8 // state must be copied to cohorts before calling this function
9 void Solver::calcRates_CM(double t, vector<double>::iterator S, vector<double>::iterator dSdt){
10 
11  //auto ss = species_vec[0];
12  //cout << "svec: " << t << " " << S[0] << " " << S[1] << " " << S[2] << " " << S[3] << "\n";
13  //cout << "coho: " << t << " " << ss->getX(0) << " " << ss->getU(0) << " " << ss->getX(1) << " " << ss->getU(1) << "\n";
14 
15  vector<double>::iterator its = S + n_statevars_system; // Skip system variables
16  vector<double>::iterator itr = dSdt + n_statevars_system;
17 
18  for (int s = 0; s<species_vec.size(); ++s){
19  Species_Base* spp = species_vec[s];
20  cout << "calcRates(species = " << s << ")\n";
21 
22  double pe = spp->establishmentProbability(t, env);
23  double gb = spp->growthRate(-1, spp->xb, t, env);
24  if (spp->birth_flux_in < 0){
25  double birthFlux = calcSpeciesBirthFlux(s,t) * pe;
26  spp->set_ub(birthFlux/(gb+1e-20));
27  }
28  else{
29  spp->calc_boundary_u(gb, pe); // this will set u in boundary cohort
30  }
31 
32 
33  for (int i=0; i<spp->J; ++i){
34  double x = spp->getX(i);
35  vector<double> g_gx = spp->growthRateGradient(i, x, t, env, control.cm_grad_dx); // FIXME: x can go
36  double dx = g_gx[0];
37  double du = -(spp->mortalityRate(i, x, t, env) + g_gx[1]);
38  if (!use_log_densities) du *= spp->getU(i);
39 
40  *itr++ = dx;
41  *itr++ = du;
42 
43  its+=2;
44  }
45 
46 
47  if (spp->n_extra_statevars > 0){
48  auto itr_prev = itr;
49  spp->getExtraRates(itr);
50  assert(distance(itr_prev, itr) == spp->n_extra_statevars*spp->J);
51  its += spp->n_extra_statevars*spp->J;
52  }
53  //cout << "---\n";
54  }
55 }
56 
57 
59  cout << ".......... add cohorts ...............\n";
60  updateEnv(current_time, state.begin(), rates.begin());
61 
62  for (int s = 0; s< species_vec.size(); ++s){
63  auto spp = species_vec[s];
64 
65  // calculate u for boundary cohort
66  double gb = spp->growthRate(-1, spp->xb, current_time, env);
67  double pe = spp->establishmentProbability(current_time, env);
68  if (spp->birth_flux_in < 0){
69  double birthFlux = calcSpeciesBirthFlux(s,current_time) * pe;
70  spp->set_ub(birthFlux/(gb+1e-20));
71  }
72  else{
73  spp->calc_boundary_u(gb, pe); // init density of boundary cohort
74  }
75 
76  spp->initBoundaryCohort(current_time, env); // init extra state variables and birth time of the boundary cohort
77  spp->addCohort(); // introduce copy of boundary cohort in system
78  }
79 
80  resizeStateFromSpecies();
81  copyCohortsToState();
82 }
83 
84 
86 
87  for (auto& spp : species_vec){
88  if (spp->J > control.max_cohorts) // remove a cohort if number of cohorts in the species exceeds a threshold
89  spp->removeDensestCohort();
90  }
91 
92  resizeStateFromSpecies();
93  copyCohortsToState();
94  cout << "........................................\n";
95 
96 }
97 
98 
99 
100 //template<class Model, class Environment>
101 //double Solver<Model,Environment>::calc_u0_CM(){
110 
114 
126  //return 0;
127 //}
128 
virtual double growthRate(int i, double x, double t, void *env)=0
void removeCohort_CM()
Definition: cm.cpp:85
virtual double getU(int i)=0
STL namespace.
virtual void set_ub(double _ub)=0
int n_extra_statevars
Definition: species.h:21
virtual double getX(int i)=0
virtual void getExtraRates(std::vector< double >::iterator &it)=0
virtual double establishmentProbability(double t, void *env)=0
virtual double mortalityRate(int i, double x, double t, void *env)=0
double birth_flux_in
Definition: species.h:34
double xb
Definition: species.h:41
virtual double calc_boundary_u(double gb, double pe)=0
virtual std::vector< double > growthRateGradient(int i, double x, double t, void *env, double grad_dx)=0
void calcRates_CM(double t, std::vector< double >::iterator S, std::vector< double >::iterator dSdt)
Definition: cm.cpp:9
void addCohort_CM()
Definition: cm.cpp:58