forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost_solvers.h
More file actions
149 lines (111 loc) · 4.13 KB
/
boost_solvers.h
File metadata and controls
149 lines (111 loc) · 4.13 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//#define MOBIUS_SOLVER_FUNCTION(Name) void Name(double h, size_t n, double* x0, double* wk, const mobius_solver_equation_function &EquationFunction, const mobius_solver_equation_function &JacobiFunction, double AbsErr, double RelErr)
/*
There are a lot of different solvers int the boost::numeric::odeint package. We have not wrapped all of them here. If you want one you could ask for it to be included here, or just do it yourself, which should be pretty easy.
*/
#if !defined(BOOST_SOLVERS_H)
#include <boost/numeric/odeint.hpp>
typedef boost::numeric::ublas::vector< double > vec_boost;
typedef boost::numeric::ublas::matrix< double > mat_boost;
struct boost_ode_system
{
const mobius_solver_equation_function &EquationFunction;
boost_ode_system(const mobius_solver_equation_function &EquationFunction) : EquationFunction(EquationFunction)
{
}
void operator()( const vec_boost &X , vec_boost &DXDT , double /* t */ )
{
EquationFunction((double *)X.data().begin(), (double *)DXDT.data().begin());
}
};
struct boost_ode_system_jacobi
{
const mobius_solver_jacobi_function &JacobiFunction;
boost_ode_system_jacobi(const mobius_solver_jacobi_function &JacobiFunction) : JacobiFunction(JacobiFunction)
{
}
void operator()( const vec_boost &X, mat_boost &J , const double & /* t */ , vec_boost /* &DFDT */ )
{
//NOTE: We are banking on not having to clear DFDT each time. We assume it is inputed as 0 from the solver.. However I don't know if this is documented functionality
J.clear(); //NOTE: Unfortunately it seems like J contains garbage values at the start of each run unless we clear it. And we have to clear it since we only set the nonzero values in the JacobiEstimation.
mobius_matrix_insertion_function MatrixInserter = [&](size_t Row, size_t Col, double Value){ J(Row, Col) = Value; };
JacobiFunction((double *)X.data().begin(), MatrixInserter);
}
};
MOBIUS_SOLVER_FUNCTION(BoostRosenbrock4Impl_)
{
using namespace boost::numeric::odeint;
//It is a little stupid that we have to copy the state back and forth, but it seems like we can't create a ublas vector that has an existing pointer as its data (or correct me if I'm wrong!)
vec_boost X(n);
for(size_t Idx = 0; Idx < n; ++Idx)
{
X[Idx] = x0[Idx];
}
size_t NSteps = integrate_adaptive(
make_controlled< rosenbrock4< double > >( AbsErr, RelErr ),
std::make_pair( boost_ode_system(EquationFunction), boost_ode_system_jacobi(JacobiFunction) ),
X, 0.0 , 1.0 , h
/*TODO: add an observer to handle errors? */);
//std::cout << "N steps : " << NSteps << std::endl;
for(size_t Idx = 0; Idx < n; ++Idx)
{
x0[Idx] = X[Idx];
}
}
MOBIUS_SOLVER_SETUP_FUNCTION(BoostRosenbrock4)
{
SolverSpec->SolverFunction = BoostRosenbrock4Impl_;
SolverSpec->UsesJacobian = true;
SolverSpec->UsesErrorControl = true;
}
MOBIUS_SOLVER_FUNCTION(BoostRK4Impl_)
{
using namespace boost::numeric::odeint;
vec_boost X(n);
for(size_t Idx = 0; Idx < n; ++Idx)
{
X[Idx] = x0[Idx];
}
size_t NSteps = integrate_adaptive(
runge_kutta4<vec_boost>(),
boost_ode_system(EquationFunction),
X, 0.0 , 1.0 , h
/*TODO: add an observer to handle errors? */);
//std::cout << "N steps : " << NSteps << std::endl;
for(size_t Idx = 0; Idx < n; ++Idx)
{
x0[Idx] = X[Idx];
}
}
MOBIUS_SOLVER_SETUP_FUNCTION(BoostRK4)
{
SolverSpec->SolverFunction = BoostRK4Impl_;
SolverSpec->UsesJacobian = false;
SolverSpec->UsesErrorControl = false;
}
MOBIUS_SOLVER_FUNCTION(BoostCashCarp54Impl_)
{
using namespace boost::numeric::odeint;
vec_boost X(n);
for(size_t Idx = 0; Idx < n; ++Idx)
{
X[Idx] = x0[Idx];
}
size_t NSteps = integrate_adaptive(
controlled_runge_kutta<runge_kutta_cash_karp54<vec_boost>>(AbsErr, RelErr),
boost_ode_system(EquationFunction),
X, 0.0 , 1.0 , h
/*TODO: add an observer to handle errors? */);
//std::cout << "N steps : " << NSteps << std::endl;
for(size_t Idx = 0; Idx < n; ++Idx)
{
x0[Idx] = X[Idx];
}
}
MOBIUS_SOLVER_SETUP_FUNCTION(BoostCashCarp54)
{
SolverSpec->SolverFunction = BoostCashCarp54Impl_;
SolverSpec->UsesJacobian = false;
SolverSpec->UsesErrorControl = true;
}
#define BOOST_SOLVERS_H
#endif