From 90f6cedb9eae73fdf0c44c34874c7e67f39e02c2 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 7 Oct 2010 00:02:53 -0400 Subject: Add iteration count to Integrator. --- src/vZ/Integrator.hpp | 6 +++++- tests/BS23.cpp | 1 + tests/Euler.cpp | 9 +++++---- tests/HE12.cpp | 1 + tests/Heun.cpp | 9 +++++---- tests/Midpoint.cpp | 9 +++++---- tests/RK4.cpp | 9 +++++---- tests/RKF45.cpp | 1 + 8 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/vZ/Integrator.hpp b/src/vZ/Integrator.hpp index 9346eb8..c6d6fbe 100644 --- a/src/vZ/Integrator.hpp +++ b/src/vZ/Integrator.hpp @@ -41,7 +41,7 @@ namespace vZ // By default, y and t start at zero, h starts UNDEFINED GenericIntegrator(Function f) - : m_f(f), m_y(0), m_x(0), m_h() { } + : m_f(f), m_y(0), m_x(0), m_iterations(0) { } virtual ~GenericIntegrator() { } GenericIntegrator& y(Y y) { m_y = y; return *this; } @@ -52,6 +52,8 @@ namespace vZ Scalar x() const { return m_x; } Scalar h() const { return m_h; } + unsigned int iterations() const { return m_iterations; } + // Integrate until x == x_final void integrate(Scalar x_final); @@ -64,6 +66,7 @@ namespace vZ Function m_f; Y m_y; Scalar m_x, m_h; + unsigned int m_iterations; }; // Type alias @@ -78,6 +81,7 @@ namespace vZ while (m_x < x_final) { m_h = std::min(m_h, x_final - m_x); step(); + ++m_iterations; } } } diff --git a/tests/BS23.cpp b/tests/BS23.cpp index 15abd0c..7493345 100644 --- a/tests/BS23.cpp +++ b/tests/BS23.cpp @@ -26,6 +26,7 @@ main() << "Numerical: " << actual << std::endl << "Expected: " << expected << std::endl << "h: " << integrator.h() << std::endl + << "iterations: " << integrator.iterations() << std::endl << "rejections: " << integrator.rejections() << std::endl; double error = std::fabs(expected - actual)/expected; diff --git a/tests/Euler.cpp b/tests/Euler.cpp index 0a6e28e..62ecee7 100644 --- a/tests/Euler.cpp +++ b/tests/Euler.cpp @@ -23,15 +23,16 @@ main() double expected = std::exp(2.0); std::cout << std::setprecision(10) - << "Numerical: " << actual << std::endl - << "Expected: " << expected << std::endl; + << "Numerical: " << actual << std::endl + << "Expected: " << expected << std::endl + << "iterations: " << integrator.iterations() << std::endl; double error = std::fabs(expected - actual)/expected; if (error > 0.01) { - std::cerr << "Error: " << 100.0*error << "%" << std::endl; + std::cerr << "Error: " << 100.0*error << "%" << std::endl; return EXIT_FAILURE; } else { - std::cout << "Error: " << 100.0*error << "%" << std::endl; + std::cout << "Error: " << 100.0*error << "%" << std::endl; return EXIT_SUCCESS; } } diff --git a/tests/HE12.cpp b/tests/HE12.cpp index 511a8e0..9a22d20 100644 --- a/tests/HE12.cpp +++ b/tests/HE12.cpp @@ -26,6 +26,7 @@ main() << "Numerical: " << actual << std::endl << "Expected: " << expected << std::endl << "h: " << integrator.h() << std::endl + << "iterations: " << integrator.iterations() << std::endl << "rejections: " << integrator.rejections() << std::endl; double error = std::fabs(expected - actual)/expected; diff --git a/tests/Heun.cpp b/tests/Heun.cpp index 758524b..e8b4e6c 100644 --- a/tests/Heun.cpp +++ b/tests/Heun.cpp @@ -23,15 +23,16 @@ main() double expected = std::exp(2.0); std::cout << std::setprecision(10) - << "Numerical: " << actual << std::endl - << "Expected: " << expected << std::endl; + << "Numerical: " << actual << std::endl + << "Expected: " << expected << std::endl + << "iterations: " << integrator.iterations() << std::endl; double error = std::fabs(expected - actual)/expected; if (error > 1.4e-4) { - std::cerr << "Error: " << 100.0*error << "%" << std::endl; + std::cerr << "Error: " << 100.0*error << "%" << std::endl; return EXIT_FAILURE; } else { - std::cout << "Error: " << 100.0*error << "%" << std::endl; + std::cout << "Error: " << 100.0*error << "%" << std::endl; return EXIT_SUCCESS; } } diff --git a/tests/Midpoint.cpp b/tests/Midpoint.cpp index c16fb76..b544ed1 100644 --- a/tests/Midpoint.cpp +++ b/tests/Midpoint.cpp @@ -23,15 +23,16 @@ main() double expected = std::exp(2.0); std::cout << std::setprecision(10) - << "Numerical: " << actual << std::endl - << "Expected: " << expected << std::endl; + << "Numerical: " << actual << std::endl + << "Expected: " << expected << std::endl + << "iterations: " << integrator.iterations() << std::endl; double error = std::fabs(expected - actual)/expected; if (error > 1.4e-4) { - std::cerr << "Error: " << 100.0*error << "%" << std::endl; + std::cerr << "Error: " << 100.0*error << "%" << std::endl; return EXIT_FAILURE; } else { - std::cout << "Error: " << 100.0*error << "%" << std::endl; + std::cout << "Error: " << 100.0*error << "%" << std::endl; return EXIT_SUCCESS; } } diff --git a/tests/RK4.cpp b/tests/RK4.cpp index 2090a1e..4b667d4 100644 --- a/tests/RK4.cpp +++ b/tests/RK4.cpp @@ -23,15 +23,16 @@ main() double expected = std::exp(2.0); std::cout << std::setprecision(10) - << "Numerical: " << actual << std::endl - << "Expected: " << expected << std::endl; + << "Numerical: " << actual << std::endl + << "Expected: " << expected << std::endl + << "iterations: " << integrator.iterations() << std::endl; double error = std::fabs(expected - actual)/expected; if (error > 4.2e-8) { - std::cerr << "Error: " << 100.0*error << "%" << std::endl; + std::cerr << "Error: " << 100.0*error << "%" << std::endl; return EXIT_FAILURE; } else { - std::cout << "Error: " << 100.0*error << "%" << std::endl; + std::cout << "Error: " << 100.0*error << "%" << std::endl; return EXIT_SUCCESS; } } diff --git a/tests/RKF45.cpp b/tests/RKF45.cpp index 8b87216..1ac3e4f 100644 --- a/tests/RKF45.cpp +++ b/tests/RKF45.cpp @@ -26,6 +26,7 @@ main() << "Numerical: " << actual << std::endl << "Expected: " << expected << std::endl << "h: " << integrator.h() << std::endl + << "iterations: " << integrator.iterations() << std::endl << "rejections: " << integrator.rejections() << std::endl; double error = std::fabs(expected - actual)/expected; -- cgit v1.2.3