libpspm
ifmu.cpp
Go to the documentation of this file.
1 #include <cassert>
2 #include "solver.h"
3 using namespace std;
4 
5 void Solver::calcRates_iFMU(double t, vector<double>::iterator S, vector<double>::iterator dSdt){
6  vector<double>::iterator its = S + n_statevars_system; // Skip system variables
7  vector<double>::iterator itr = dSdt + n_statevars_system;
8 
9  for (int s = 0; s<species_vec.size(); ++s){
10  auto spp = species_vec[s];
11 
12  its += spp->J;// skip u
13  for (int i=0; i<spp->J; ++i) *itr++ = 0; // set du/dt to 0
14 
15  if (spp->n_extra_statevars > 0){
16  auto itr_prev = itr;
17  spp->getExtraRates(itr); // TODO/FIXME: Does calc of extra rates need t and env?
18  assert(distance(itr_prev, itr) == spp->n_extra_statevars*spp->J);
19  its += spp->n_extra_statevars*spp->J;
20  }
21  }
22 
23 }
24 
25 void Solver::stepU_iFMU(double t, vector<double> &S, vector<double> &dSdt, double dt){
26 
27  vector<double>::iterator its = S.begin() + n_statevars_system; // Skip system variables
28 
29  // 1. Take implicit step for U
30  for (int s = 0; s<species_vec.size(); ++s){
31  Species_Base* spp = species_vec[s];
32 
33  // [S S S u u u u u a b c a b c a b c a b c a b c] <--- full SV for species is this
34  // ^ its, itr
35  double *U = &(*its); // Since FMU only has U in state, start of species is actually U
36  int J = spp->J; // xsize of species.
37  vector<double> &h = spp->h;
38 
39  vector <double> growthArray(J);
40  for (int i=0; i<J; ++i) growthArray[i] = spp->growthRate(i, spp->getX(i), t, env);
41 
42  double birthFlux;
43  double pe = spp->establishmentProbability(t, env);
44  if (spp->birth_flux_in < 0){
45  birthFlux = calcSpeciesBirthFlux(s,t) * pe;
46  }
47  else{
48  birthFlux = spp->calc_boundary_u(growthArray[0], pe)*growthArray[0];
49  }
50 
51  //cout << t << "\t" << birthFlux/growthArray[0] << " -> " << calcSpeciesBirthFlux(s,t)/growthArray[0] << "\n";
52  double B0 = 1 + dt/h[0]*growthArray[0] + dt*spp->mortalityRate(0, spp->getX(0), t, env);
53 // std::cout << "B0 = " << B0 << endl;
54  double C0 = spp->getU(0) + dt/h[0]*birthFlux;
55  U[0] = C0/B0;
56 // std::cout << "U0 = " << U[0] << endl;
57 
58  for (int w = 1; w < J; ++w){
59  double Aw = -growthArray[w-1]*dt/h[w];
60  double Bw = 1 + dt/h[w]*growthArray[w] + dt*spp->mortalityRate(w, spp->getX(w), t, env);
61  double Cw = spp->getU(w);
62 
63  U[w] = (Cw - Aw*U[w-1])/Bw;
64  }
65 
66  its += J*(1+spp->n_extra_statevars);
67 
68  }
69 
70 }
71 
72 
virtual double growthRate(int i, double x, double t, void *env)=0
std::vector< double > h
Definition: species.h:30
virtual double getU(int i)=0
STL namespace.
int n_extra_statevars
Definition: species.h:21
virtual double getX(int i)=0
void stepU_iFMU(double t, std::vector< double > &S, std::vector< double > &dSdt, double dt)
Definition: ifmu.cpp:25
void calcRates_iFMU(double t, std::vector< double >::iterator S, std::vector< double >::iterator dSdt)
Definition: ifmu.cpp:5
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
virtual double calc_boundary_u(double gb, double pe)=0