libpspm
ebt.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cassert>
3 
4 #include <solver.h>
5 using namespace std;
6 
7 
8 void Solver::calcRates_EBT(double t, vector<double>::iterator S, vector<double>::iterator dSdt){
9 
10  vector<double>::iterator its = S + n_statevars_system; // Skip system variables
11  vector<double>::iterator itr = dSdt + n_statevars_system;
12 
13  for (int s = 0; s < species_vec.size(); ++s){
14  Species_Base * spp = species_vec[s];
15 
16  double pi0 = spp->getX(spp->J-1); // last cohort is pi0, N0
17  double N0 = spp->getU(spp->J-1);
18  //std::cout << "pi = " << pi0 << ", N0 = " << N0 << "\n";
19 
20  std::vector<double> g_gx = spp->growthRateGradient(-1, spp->xb, t, env, control.ebt_grad_dx);
21  std::vector<double> m_mx = spp->mortalityRateGradient(-1, spp->xb, t, env, control.ebt_grad_dx);
22  //std::cout << "g = " << g_gx[0] << ", gx = " << g_gx[1] << "\n";
23  //std::cout << "m = " << m_mx[0] << ", mx = " << m_mx[1] << "\n";
24 
25  double mb = m_mx[0], mortGrad = m_mx[1], gb = g_gx[0], growthGrad = g_gx[1];
26 
27  double birthFlux;
28  double pe = spp->establishmentProbability(t, env);
29  if (spp->birth_flux_in < 0){
30  birthFlux = calcSpeciesBirthFlux(s,t) * pe;
31  }
32  else{
33  double u0 = spp->calc_boundary_u(gb, pe);
34  birthFlux = u0*gb;
35  }
36 
37  for (int i=0; i<spp->J-1; ++i){ // go down to the second last cohort (exclude boundary cohort)
38  double dx = spp->growthRate(i, spp->getX(i), t, env); // dx/dt
39  double du = -spp->mortalityRate(i, spp->getX(i), t, env) * spp->getU(i); // du/dt
40  //std::cout << "S/C = " << s << "/" << i << " " << spp->getX(i) << " " << dx << " " << du << "\n";
41  *itr++ = dx;
42  *itr++ = du;
43  its += 2;
44  }
45 
46  // dpi0/dt and dN0/dt
47  //std::cout << "S/C = " << s << "/" << "b" << " | pi0/N0 = " << pi0 << " " << N0;
48  //if (pi0 <= 0) pi0 = 1e-40;
49  //if (N0 <= 0) N0 = 1e-40;
50  double dN0 = -mb*N0 - mortGrad*pi0 + birthFlux;
51  double dpi0 = gb*N0 + growthGrad*pi0 - mb*pi0;
52  //std::cout << " | dpi0/dN0 = " << dpi0 << " " << dN0 << " | mx/gx/mb/gb = " << m_mx[1] << " " << g_gx[1] << " " << m_mx[0] << " " << g_gx[0] << "\n";
53  *itr++ = dpi0;
54  *itr++ = dN0;
55  its += 2;
56 
57  if (spp->n_extra_statevars > 0){
58  auto itr_prev = itr;
59  spp->getExtraRates(itr);
60  assert(distance(itr_prev, itr) == spp->n_extra_statevars*spp->J);
61  its += spp->n_extra_statevars*spp->J;
62  }
63 
64  }
65 
66 }
67 
68 
69 
71 
72  for (auto spp : species_vec){
73  // 1. internalize the pi0-cohort (this cohort's birth time would have been already set when it was inserted)
74  // - get pi0, N0 from last cohort
75  double pi0 = spp->getX(spp->J-1);
76  double N0 = spp->getU(spp->J-1);
77 
78  // - update the recently internalized pi0-cohort with actual x0 value
79  double x0 = spp->xb + pi0/(N0+1e-12);
80  spp->setX(spp->J-1, x0);
81  spp->setU(spp->J-1, N0);
82 
83  // 2. insert a new cohort (copy of boundary cohort, to be the new pi0-cohort)
84  spp->initBoundaryCohort(current_time, env); // update initial extra state and birth-time of boundary cohort
85  spp->addCohort(); // introduce copy of boundary cohort into species
86 
87  // 3. set x,u of the new cohort to 0,0, thus marking it as the new pi0-cohort
88  spp->setX(spp->J-1, 0);
89  spp->setU(spp->J-1, 0);
90  }
91 
92  // 4. reset state from cohorts
93  resizeStateFromSpecies();
94  copyCohortsToState();
95 
96 }
97 
98 
100 
101  for (auto spp : species_vec){
102  spp->removeDeadCohorts(control.ebt_ucut);
103  }
104 
105  resizeStateFromSpecies();
106  copyCohortsToState();
107 
108 }
109 
110 
virtual double growthRate(int i, double x, double t, void *env)=0
virtual double getU(int i)=0
STL namespace.
void removeDeadCohorts_EBT()
Definition: ebt.cpp:99
int n_extra_statevars
Definition: species.h:21
virtual double getX(int i)=0
virtual std::vector< double > mortalityRateGradient(int i, double x, double t, void *env, double grad_dx)=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_EBT(double t, std::vector< double >::iterator S, std::vector< double >::iterator dSdt)
Definition: ebt.cpp:8
void addCohort_EBT()
Definition: ebt.cpp:70