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. --- tests/EquationSystem.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 22 ++++++++++++---------- 2 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 tests/EquationSystem.cpp (limited to 'tests') diff --git a/tests/EquationSystem.cpp b/tests/EquationSystem.cpp new file mode 100644 index 0000000..973dd30 --- /dev/null +++ b/tests/EquationSystem.cpp @@ -0,0 +1,46 @@ +#include "vZ.hpp" +#include +#include +#include +#include + +typedef vZ::EquationSystem<2> Y; + +// y'' = y (y == C*exp(t)) +Y +f(double t, Y y) +{ + Y r; + r[0] = y[1]; + r[1] = y[0]; + return r; +} + +int +main() +{ + Y y; + y[0] = 1.0; + y[1] = 1.0; + vZ::GenericEulerIntegrator integrator(f); + integrator.y(y).x(0.0).h(0.01); + + integrator.integrate(2.0); + + double actual = integrator.y()[0]; + double expected = std::exp(2.0); + + std::cout << std::setprecision(10) + << "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; + return EXIT_FAILURE; + } else { + std::cout << "Error: " << 100.0*error << "%" << std::endl; + return EXIT_SUCCESS; + } +} diff --git a/tests/Makefile.am b/tests/Makefile.am index 5874419..1cd184d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -27,15 +27,17 @@ check_PROGRAMS = Euler-test \ BS23-test \ RKF45-test \ CK45-test \ - DP45-test + DP45-test \ + EquationSystem-test TESTS = $(check_PROGRAMS) -Euler_test_SOURCES = Euler.cpp -Midpoint_test_SOURCES = Midpoint.cpp -Heun_test_SOURCES = Heun.cpp -RK4_test_SOURCES = RK4.cpp -HE12_test_SOURCES = HE12.cpp -BS23_test_SOURCES = BS23.cpp -RKF45_test_SOURCES = RKF45.cpp -CK45_test_SOURCES = CK45.cpp -DP45_test_SOURCES = DP45.cpp +Euler_test_SOURCES = Euler.cpp +Midpoint_test_SOURCES = Midpoint.cpp +Heun_test_SOURCES = Heun.cpp +RK4_test_SOURCES = RK4.cpp +HE12_test_SOURCES = HE12.cpp +BS23_test_SOURCES = BS23.cpp +RKF45_test_SOURCES = RKF45.cpp +CK45_test_SOURCES = CK45.cpp +DP45_test_SOURCES = DP45.cpp +EquationSystem_test_SOURCES = EquationSystem.cpp -- cgit v1.2.3