From 2c2b69e1df183f118c45d74c18c7e84934aaff1f Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 11 Oct 2010 02:38:46 -0400 Subject: Support systems of ODEs. --- src/Makefile.am | 1 + src/vZ.hpp | 1 + src/vZ/EquationSystem.hpp | 154 ++++++++++++++++++++++++++++++++++++++++++++++ src/vZ/Integrator.hpp | 4 +- 4 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/vZ/EquationSystem.hpp (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index bf2b10c..7db29dc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,6 +23,7 @@ nobase_include_HEADERS = vZ.hpp \ vZ/CK45.hpp \ vZ/DP45.hpp \ vZ/Euler.hpp \ + vZ/EquationSystem.hpp \ vZ/HE12.hpp \ vZ/Heun.hpp \ vZ/Integrator.hpp \ diff --git a/src/vZ.hpp b/src/vZ.hpp index ab7b3c2..c6efc80 100644 --- a/src/vZ.hpp +++ b/src/vZ.hpp @@ -24,6 +24,7 @@ #define VZ_HPP #include +#include #include #include #include diff --git a/src/vZ/EquationSystem.hpp b/src/vZ/EquationSystem.hpp new file mode 100644 index 0000000..0963baf --- /dev/null +++ b/src/vZ/EquationSystem.hpp @@ -0,0 +1,154 @@ +/************************************************************************* + * 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_EQUATIONSYSTEM_HPP +#define VZ_EQUATIONSYSTEM_HPP + +#include + +namespace vZ +{ + // A class to easily represent a system of ODEs + template + class EquationSystem + { + public: + typedef typename Traits::Scalar Scalar; + + // EquationSystem(); + // ~EquationSystem(); + + T& operator[](std::size_t i) { return m_values[0]; } + const T& operator[](std::size_t i) const { return m_values[0]; } + + EquationSystem& operator+=(const EquationSystem& rhs); + EquationSystem& operator-=(const EquationSystem& rhs); + EquationSystem& operator*=(Scalar rhs); + EquationSystem& operator/=(Scalar rhs); + + private: + T m_values[N]; + }; + + // Traits specialization + template + class Traits > + { + public: + typedef typename Traits::Scalar Scalar; + + private: + Traits(); + }; + + // Binary operators + + template + inline EquationSystem + operator+(const EquationSystem& lhs, const EquationSystem& rhs) + { + EquationSystem res = lhs; + res += rhs; + return res; + } + + template + inline EquationSystem + operator-(const EquationSystem& lhs, const EquationSystem& rhs) + { + EquationSystem res = lhs; + res -= rhs; + return res; + } + + template + inline EquationSystem + operator*(typename EquationSystem::Scalar lhs, + const EquationSystem& rhs) + { + EquationSystem res = rhs; + res *= lhs; + return res; + } + + template + inline EquationSystem + operator*(const EquationSystem& lhs, + typename EquationSystem::Scalar rhs) + { + EquationSystem res = lhs; + res *= rhs; + return res; + } + + template + inline EquationSystem + operator/(const EquationSystem& lhs, + typename EquationSystem::Scalar rhs) + { + EquationSystem res = lhs; + res /= rhs; + return res; + } + + // Implementation + + template + EquationSystem& + EquationSystem::operator+=(const EquationSystem& rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] += rhs.m_values[i]; + } + return *this; + } + + template + EquationSystem& + EquationSystem::operator-=(const EquationSystem& rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] -= rhs.m_values[i]; + } + return *this; + } + + template + EquationSystem& + EquationSystem::operator*=(typename EquationSystem::Scalar rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] *= rhs; + } + return *this; + } + + template + EquationSystem& + EquationSystem::operator/=(typename EquationSystem::Scalar rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] /= rhs; + } + return *this; + } +} + +#endif // VZ_EQUATIONSYSTEM_HPP diff --git a/src/vZ/Integrator.hpp b/src/vZ/Integrator.hpp index c6d6fbe..cb9649a 100644 --- a/src/vZ/Integrator.hpp +++ b/src/vZ/Integrator.hpp @@ -39,9 +39,9 @@ namespace vZ typedef typename Traits::Scalar Scalar; typedef std::tr1::function Function; - // By default, y and t start at zero, h starts UNDEFINED + // By default, y, t, and h start UNDEFINED GenericIntegrator(Function f) - : m_f(f), m_y(0), m_x(0), m_iterations(0) { } + : m_f(f),m_iterations(0) { } virtual ~GenericIntegrator() { } GenericIntegrator& y(Y y) { m_y = y; return *this; } -- cgit v1.2.3