trivial_state.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/trivial_state.cpp
  4. [begin_description]
  5. This file tests if the steppers can integrate the trivial state consisting of a single double.
  6. [end_description]
  7. Copyright 2012-2013 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. #include <limits>
  14. // disable checked iterator warning for msvc
  15. #include <boost/config.hpp>
  16. #ifdef BOOST_MSVC
  17. #pragma warning(disable:4996)
  18. #endif
  19. #define BOOST_TEST_MODULE odeint_trivial_state
  20. #include <boost/test/unit_test.hpp>
  21. #include <boost/mpl/vector.hpp>
  22. #include <boost/mpl/int.hpp>
  23. #include <boost/mpl/at.hpp>
  24. #include <boost/numeric/odeint/stepper/euler.hpp>
  25. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  26. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
  27. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  28. #include <boost/numeric/odeint/stepper/generation.hpp>
  29. #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
  30. #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
  31. using namespace boost::unit_test;
  32. using namespace boost::numeric::odeint;
  33. namespace mpl = boost::mpl;
  34. struct constant_system
  35. {
  36. template< typename T >
  37. void operator()( const T &x , T &dxdt , const T t ) const
  38. { dxdt = 1.0; }
  39. };
  40. BOOST_AUTO_TEST_SUITE( trivial_state_test )
  41. /* test different do_step methods of simple steppers */
  42. typedef mpl::vector<
  43. euler< double > ,
  44. runge_kutta4< double > ,
  45. // with floats all four parameters have to be given explicitly to override default double
  46. euler< float , float , float , float > ,
  47. runge_kutta4< float , float , float , float >
  48. >::type stepper_types;
  49. BOOST_AUTO_TEST_CASE_TEMPLATE( test_do_step , T, stepper_types )
  50. {
  51. typedef T stepper_type;
  52. stepper_type stepper;
  53. typename stepper_type::state_type x = 0.0;
  54. typename stepper_type::time_type t = 0.0;
  55. typename stepper_type::time_type dt = 0.1;
  56. stepper.do_step( constant_system() , x , t , dt );
  57. BOOST_CHECK_CLOSE( x , 0.1 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
  58. // this overload is not allowed if the types of dxdt and dt are the same
  59. // deriv_type dxdt = 1.0;
  60. // stepper.do_step( constant_system , x , dxdt , t , dt );
  61. typename stepper_type::state_type x_out;
  62. stepper.do_step( constant_system() , x , t , x_out , dt );
  63. BOOST_CHECK_CLOSE( x , 0.1 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
  64. BOOST_CHECK_CLOSE( x_out , 0.2 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
  65. }
  66. /* test integrate_adaptive with controlled steppers */
  67. typedef mpl::vector<
  68. runge_kutta_cash_karp54< double > ,
  69. runge_kutta_dopri5< double > ,
  70. // with floats all four parameters have to be given explicitly to override default double
  71. runge_kutta_cash_karp54< float , float , float , float > ,
  72. runge_kutta_dopri5< float , float , float , float >
  73. > error_stepper_types;
  74. BOOST_AUTO_TEST_CASE_TEMPLATE( test_integrate , T , error_stepper_types )
  75. {
  76. typedef T stepper_type;
  77. typename stepper_type::state_type x = 0.0;
  78. typename stepper_type::time_type t0 = 0.0;
  79. typename stepper_type::time_type t1 = 1.0;
  80. typename stepper_type::time_type dt = 0.1;
  81. integrate_adaptive( make_controlled< stepper_type >( 1e-6 , 1e-6 ) , constant_system() , x , t0 , t1 , dt );
  82. BOOST_CHECK_CLOSE( x , 1.0 , 100*std::numeric_limits< typename stepper_type::state_type >::epsilon() );
  83. }
  84. BOOST_AUTO_TEST_SUITE_END()