From f1983adb487bb531c1c68226596b26eebf99876a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 6 Oct 2010 21:50:48 -0400 Subject: Add Heun-Euler adaptive method. --- src/vZ/Adaptive.hpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/vZ/Adaptive.hpp (limited to 'src/vZ/Adaptive.hpp') diff --git a/src/vZ/Adaptive.hpp b/src/vZ/Adaptive.hpp new file mode 100644 index 0000000..864b038 --- /dev/null +++ b/src/vZ/Adaptive.hpp @@ -0,0 +1,109 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes * + * * + * 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 * + * . * + *************************************************************************/ + +#ifndef VZ_ADAPTIVE_HPP +#define VZ_ADAPTIVE_HPP + +#include + +namespace vZ +{ + // Base class for adaptive RK-style algorithms + template + class GenericAdaptiveIntegrator : public GenericRKIntegrator + { + public: + typedef typename GenericRKIntegrator::Scalar Scalar; + typedef typename GenericRKIntegrator::Function Function; + + GenericAdaptiveIntegrator& atol(Scalar tol) { m_atol = tol; return *this; } + GenericAdaptiveIntegrator& rtol(Scalar tol) { m_rtol = tol; return *this; } + + Scalar atol() const { return m_atol; } + Scalar rtol() const { return m_rtol; } + + protected: + typedef typename GenericRKIntegrator::ACoefficients ACoefficients; + typedef typename GenericRKIntegrator::BCoefficients BCoefficients; + typedef typename GenericRKIntegrator::KVector KVector; + + GenericAdaptiveIntegrator(Function f, unsigned int order, + ACoefficients a, BCoefficients b, + BCoefficients bStar) + : GenericRKIntegrator(f), m_order(order), + m_a(a), m_b(b), m_bStar(bStar) + { } + virtual ~GenericAdaptiveIntegrator() { } + + void step(); + + private: + Scalar m_atol, m_rtol; + unsigned int m_order; + ACoefficients m_a; + BCoefficients m_b, m_bStar; + }; + + // Type alias + typedef GenericAdaptiveIntegrator AdaptiveIntegrator; + + // Implementations + + template + void + GenericAdaptiveIntegrator::step() + { + static const Scalar S = Scalar(19)/Scalar(20); // Arbitrary saftey factor + Scalar newH; + Y y; + + // Attempt the integration step in a loop + bool rejected = true; + while (rejected) { + KVector k = calculateK(m_a); + y = calculateY(k, m_b); + Y yStar = calculateY(k, m_bStar); + + // Get an error estimate + using std::abs; + using std::pow; + Scalar delta = abs(y - yStar); + Scalar scale = m_atol + std::max(abs(y), abs(this->y()))*m_rtol; + + newH = S*this->h()*pow(scale/delta, Scalar(1)/m_order); + + if (delta > scale) { + // Reject the step + this->h(newH); + } else { + rejected = false; + } + } + + // Update x and y + this->y(y); + this->x(this->x() + this->h()); + + // Adjust the stepsize for the next iteration + this->h(newH); + } +} + +#endif // VZ_ADAPTIVE_HPP -- cgit v1.2.3