-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDESolver.h
90 lines (79 loc) · 2.75 KB
/
DESolver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef DESOLVER_H
#define DESOLVER_H
#include <Cell.h>
#include <GnuplotPiper.h>
#include <stdio.h>
using namespace std;
/** @brief This is a general differential equations solver class
*/
class DESolver{
private:
GnuplotPiper *Gpp;
FILE *ofs;
public:
DESolver(GnuplotPiper *GPP = NULL){
Gpp = GPP;
ofs = fopen("rst.m","w");
fprintf(ofs,"clear all;\nx=[\n");
}
/** public constructor
@param GPP - pointer to gnuplot piper, in vheart it is NULL by default
*/
//DESolver(){
//}
/** integrate a cell over single "small" time step
@param cell - pointer to the cell to be integrated
@param dt - time step
@param time - current time
*/
virtual void IntegrateOverDt(Cell *cell, double dt, double time = 0, bool inSplitPhase = false) = 0;
/** solves dynamical equations over given time period
@param cell - pointer to the cell to be integrated
@param MaxTime - time period in milliseconds
@param dt - time step
*/
void Solve(Cell *cell, double MaxTime = 1000, double dt = 0.005){
unsigned long int MT = (unsigned long int)MaxTime/dt;
for (unsigned long int i = 1; i <= MT; i++){
double time = i*dt;
unsigned long int step = MT/1000;
this->IntegrateOverDt(cell,dt,time);
if (Gpp) Gpp->pipe1D(i,cell->getV());
if (i/step*step == i) {
fprintf(ofs,"%g\t%g\t%g\n",time,cell->getV(),cell->getTension());
}
}
fprintf(ofs,"];\nplot(x(:,1),x(:,2));\n");
fprintf(ofs,"hold;\nplot(x(:,1),x(:,3),'r');\n");
fprintf(stderr,"Solve finished\n");
}
};
/** @brief class implementing forward Euler integration method
*/
class ForwardEulerSolver:public DESolver{
public:
long count;
//ForwardEulerSolver():DESolver(){}
ForwardEulerSolver(GnuplotPiper *GPP = NULL):DESolver(GPP){count = 0;}
/** integrate a cell over single "small" time step
@param cell - pointer to the cell to be integrated
@param dt - time step
@param time - current time
*/
void IntegrateOverDt(Cell *cell, double dt, double time = 0, bool inSplitPhase = false){
cell->computeRightHandSide(time);
double dV = cell->getDV();
if (fabs(dV) > cell->splitVoltageDerivativeThreshold && !inSplitPhase){
double split_dt = dt/cell->splitStepFactor;
for (int i=0; i<int(cell->splitStepFactor); i++){
this->IntegrateOverDt(cell,split_dt,time+split_dt*i,true);
}
}else{
for (int i=0; i<cell->systemSize; i++){
cell->Vars[i] += dt*cell->dVars[i];
count++;
}
}
}
};
#endif // DESOLVER_H