summaryrefslogtreecommitdiffstats
path: root/src/vZ/RK.hpp
blob: 97ab487812858f1f5e7c08ede555e5d11ffb9828 (plain)
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
/*************************************************************************
 * 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;
    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
  {
    KVector k;
    k.reserve(a.size() + 1);

    // k1
    k.push_back(this->f()(this->x(), this->y()));

    // k2..n
    for (typename ACoefficients::const_iterator i = a.begin();
         i != a.end();
         ++i)
    {
      Scalar c(0);
      Y y = this->y();
      for (typename std::vector<Scalar>::size_type j = 0; j < i->size(); ++j) {
        Scalar aij = i->at(j);
        c += aij;

        y += aij*this->h()*k.at(j);
      }

      k.push_back(this->f()(this->x() + c, y));
    }

    return k;
  }

  template <typename Y>
  Y
  GenericRKIntegrator<Y>::calculateY(const KVector& k, const BCoefficients& b)
    const
  {
    Y y = this->y();

    for (typename std::vector<Scalar>::size_type i = 0; i < k.size(); ++i) {
      y += this->h()*b.at(i)*k.at(i);
    }

    return y;
  }
}

#endif // VZ_RK_HPP