euler_stepper.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/euler_stepper.cpp
  4. [begin_description]
  5. This file tests explicit Euler stepper.
  6. [end_description]
  7. Copyright 2011 Mario Mulansky
  8. Copyright 2012 Karsten Ahnert
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #define BOOST_TEST_MODULE odeint_explicit_euler
  14. #include <boost/test/unit_test.hpp>
  15. #include <utility>
  16. #include <iostream>
  17. #include <vector>
  18. #include <boost/numeric/odeint/stepper/euler.hpp>
  19. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  20. using namespace boost::unit_test;
  21. using namespace boost::numeric::odeint;
  22. // test with own vector implementation
  23. class my_vec : public std::vector< double > {
  24. public:
  25. my_vec() : std::vector< double >()
  26. { }
  27. my_vec( const my_vec &x ) : std::vector< double >( x )
  28. { }
  29. my_vec( size_t dim )
  30. : std::vector< double >( dim )
  31. { }
  32. };
  33. namespace boost {
  34. namespace numeric {
  35. namespace odeint {
  36. template<>
  37. struct is_resizeable< my_vec >
  38. {
  39. //struct type : public boost::true_type { };
  40. typedef boost::true_type type;
  41. const static bool value = type::value;
  42. };
  43. } } }
  44. typedef double value_type;
  45. //typedef std::vector< value_type > state_type;
  46. typedef my_vec state_type;
  47. /* use functors, because functions don't work with msvc 10, I guess this is a bug */
  48. struct sys
  49. {
  50. void operator()( const state_type &x , state_type &dxdt , const value_type t ) const
  51. {
  52. std::cout << "sys start " << dxdt.size() << std::endl;
  53. dxdt[0] = x[0] + 2 * x[1];
  54. dxdt[1] = x[1];
  55. std::cout << "sys done" << std::endl;
  56. }
  57. };
  58. BOOST_AUTO_TEST_SUITE( explicit_euler_test )
  59. BOOST_AUTO_TEST_CASE( test_euler )
  60. {
  61. range_algebra algebra;
  62. euler< state_type > stepper( algebra );
  63. state_type x( 2 );
  64. x[0] = 0.0; x[1] = 1.0;
  65. std::cout << "initialized" << std::endl;
  66. const value_type eps = 1E-12;
  67. const value_type dt = 0.1;
  68. stepper.do_step( sys() , x , 0.0 , dt );
  69. using std::abs;
  70. // compare with analytic solution of above system
  71. BOOST_CHECK_MESSAGE( abs( x[0] - 2.0*1.0*dt ) < eps , x[0] - 2.0*1.0*dt );
  72. BOOST_CHECK_MESSAGE( abs( x[1] - (1.0 + dt) ) < eps , x[1] - (1.0+dt) );
  73. }
  74. BOOST_AUTO_TEST_SUITE_END()