From 0f04e97fa748b6740da4c9512b596d7d3a2788c5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 6 Oct 2010 16:47:04 -0400 Subject: Add the Euler method. --- src/vZ/RK.hpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 6 deletions(-) (limited to 'src/vZ/RK.hpp') diff --git a/src/vZ/RK.hpp b/src/vZ/RK.hpp index 3ba4b8a..df608ff 100644 --- a/src/vZ/RK.hpp +++ b/src/vZ/RK.hpp @@ -26,23 +26,77 @@ namespace vZ { // Base class for Runge-Kutta type algorithms - template - class GenericRKIntegrator : public GenericIntegrator + template + class GenericRKIntegrator : public GenericIntegrator { public: - typedef typename GenericIntegrator::Function Function; + typedef typename GenericIntegrator::Scalar Scalar; + typedef typename GenericIntegrator::Function Function; protected: // Coefficients in the tableau representation of the RK algorithm - typedef std::vector > ACoefficients; - typedef std::vector BCoefficients; + typedef std::vector > ACoefficients; + typedef std::vector BCoefficients; - GenericRKIntegrator(Function f, T dt) : GenericIntegrator(f, dt) { } + // Result vectors + typedef std::vector KVector; + + GenericRKIntegrator(Function f) : GenericIntegrator(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 RKIntegrator; + + // Implementation + + template + typename GenericRKIntegrator::KVector + GenericRKIntegrator::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::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 + Y + GenericRKIntegrator::calculateY(const KVector& k, const BCoefficients& b) + const + { + Y y = this->y(); + + for (typename std::vector::size_type i = 0; i < k.size(); ++i) { + y += this->h()*b.at(i)*k.at(i); + } + + return y; + } } #endif // VZ_RK_HPP -- cgit v1.2.3