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
|
/*************************************************************************
* Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com> *
* *
* This file is part of The vZ Library. *
* *
* The vZ Library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* The vZ Library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
*************************************************************************/
#ifndef VZ_RK_HPP
#define VZ_RK_HPP
#include <vector>
namespace vZ
{
// Base class for Runge-Kutta type algorithms
template <typename Y>
class GenericRKIntegrator : public GenericIntegrator<Y>
{
public:
typedef typename GenericIntegrator<Y>::Scalar Scalar;
typedef typename GenericIntegrator<Y>::Function Function;
protected:
// Coefficients in the tableau representation of the RK algorithm
typedef std::vector<std::vector<Scalar> > ACoefficients;
typedef std::vector<Scalar> BCoefficients;
// Result vectors
typedef std::vector<Y> KVector;
GenericRKIntegrator(Function f) : GenericIntegrator<Y>(f) { }
virtual ~GenericRKIntegrator() { }
// Perform the stages of an RK integration
KVector calculateK(const ACoefficients& a) const;
KVector calculateK(Y& y, const ACoefficients& a) const;
KVector calculateK(Y k1, Y& y, const ACoefficients& a) const;
Y calculateY(const KVector& k, const BCoefficients& b) const;
};
// Type alias
typedef GenericRKIntegrator<double> RKIntegrator;
// Implementation
template <typename Y>
typename GenericRKIntegrator<Y>::KVector
GenericRKIntegrator<Y>::calculateK(const ACoefficients& a) const
{
Y ytemp; // Ignored
return calculateK(ytemp, a);
}
template <typename Y>
typename GenericRKIntegrator<Y>::KVector
GenericRKIntegrator<Y>::calculateK(Y& y, const ACoefficients& a) const
{
return calculateK(this->f()(this->x(), this->y()), y, a);
}
template <typename Y>
typename GenericRKIntegrator<Y>::KVector
GenericRKIntegrator<Y>::calculateK(Y k1, Y& y, const ACoefficients& a) const
{
KVector k;
k.reserve(a.size() + 1);
// k1
k.push_back(k1);
// k2..n
Scalar h(this->h());
for (typename ACoefficients::const_iterator i = a.begin();
i != a.end();
++i)
{
Scalar c(0);
y = this->y();
for (typename std::vector<Scalar>::size_type j = 0; j < i->size(); ++j) {
Scalar aij = i->at(j);
c += aij;
y += h*aij*k.at(j);
}
k.push_back(this->f()(this->x() + h*c, y));
}
return k;
}
template <typename Y>
Y
GenericRKIntegrator<Y>::calculateY(const KVector& k, const BCoefficients& b)
const
{
Y y(this->y());
Scalar h(this->h());
for (typename std::vector<Scalar>::size_type i = 0; i < k.size(); ++i) {
y += h*b.at(i)*k.at(i);
}
return y;
}
}
#endif // VZ_RK_HPP
|