runge_kutta_error_concepts.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/runge_kutta_error_concepts.cpp
  4. [begin_description]
  5. This file tests the Stepper concepts of odeint with all Runge-Kutta Error steppers.
  6. It's one of the main tests of odeint.
  7. [end_description]
  8. Copyright 2012 Karsten Ahnert
  9. Copyright 2012-2013 Mario Mulansky
  10. Distributed under the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or
  12. copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  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_runge_kutta_error_concepts
  20. #include <vector>
  21. #include <cmath>
  22. #include <iostream>
  23. #include <boost/numeric/odeint/config.hpp>
  24. #include <boost/array.hpp>
  25. #include <boost/test/unit_test.hpp>
  26. #include <boost/ref.hpp>
  27. #include <boost/bind.hpp>
  28. #include <boost/utility.hpp>
  29. #include <boost/type_traits/add_reference.hpp>
  30. #include <boost/mpl/vector.hpp>
  31. #include <boost/mpl/for_each.hpp>
  32. #include <boost/mpl/insert_range.hpp>
  33. #include <boost/mpl/end.hpp>
  34. #include <boost/mpl/copy.hpp>
  35. #include <boost/mpl/placeholders.hpp>
  36. #include <boost/mpl/inserter.hpp>
  37. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
  38. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
  39. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  40. #include <boost/numeric/odeint/stepper/runge_kutta_fehlberg78.hpp>
  41. #include "prepare_stepper_testing.hpp"
  42. #include "dummy_odes.hpp"
  43. using std::vector;
  44. using namespace boost::unit_test;
  45. using namespace boost::numeric::odeint;
  46. namespace mpl = boost::mpl;
  47. const double result = 2.4; // four steps total...
  48. const double eps = 1.0e-14;
  49. template< class Stepper , class System >
  50. void check_error_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x , typename Stepper::state_type &xerr )
  51. {
  52. typedef Stepper stepper_type;
  53. typedef typename stepper_type::deriv_type container_type;
  54. typedef typename stepper_type::order_type order_type;
  55. typedef typename stepper_type::time_type time_type;
  56. stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) );
  57. stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) , xerr );
  58. }
  59. // default case is used for vector space types like plain double
  60. template< class Stepper , typename T >
  61. struct perform_error_stepper_test
  62. {
  63. typedef T vector_space_type;
  64. void operator()( void ) const
  65. {
  66. using std::abs;
  67. vector_space_type x , xerr;
  68. x = 2.0;
  69. Stepper stepper;
  70. constant_system_functor_vector_space sys;
  71. #ifndef _MSC_VER
  72. // dont run this for MSVC due to compiler bug 697006
  73. check_error_stepper_concept( stepper ,
  74. constant_system_vector_space< vector_space_type , vector_space_type , typename Stepper::time_type > ,
  75. x ,
  76. xerr );
  77. #else
  78. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  79. #endif
  80. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  81. BOOST_CHECK_MESSAGE( (abs( x - result )) < eps , x );
  82. }
  83. };
  84. template< class Stepper , typename T >
  85. struct perform_error_stepper_test< Stepper , std::vector<T> >
  86. {
  87. typedef std::vector<T> vector_type;
  88. void operator()( void )
  89. {
  90. using std::abs;
  91. vector_type x( 1 , 2.0 ) , xerr( 1 );
  92. Stepper stepper;
  93. constant_system_functor_standard sys;
  94. #ifndef _MSC_VER
  95. // dont run this for MSVC due to compiler bug 697006
  96. check_error_stepper_concept( stepper , constant_system_standard< vector_type , vector_type , typename Stepper::time_type > , x , xerr );
  97. #else
  98. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  99. #endif
  100. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  101. BOOST_CHECK( (abs( x[0] - result )) < eps );
  102. }
  103. };
  104. template< class Stepper , typename T >
  105. struct perform_error_stepper_test< Stepper , boost::array<T,1> >
  106. {
  107. typedef boost::array<T,1> array_type;
  108. void operator()( void )
  109. {
  110. using std::abs;
  111. array_type x , xerr;
  112. x[0] = 2.0;
  113. Stepper stepper;
  114. constant_system_functor_standard sys;
  115. #ifndef _MSC_VER
  116. // dont run this for MSVC due to compiler bug 697006
  117. check_error_stepper_concept( stepper , constant_system_standard< array_type , array_type , typename Stepper::time_type > , x , xerr );
  118. #else
  119. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  120. #endif
  121. check_error_stepper_concept( stepper , boost::cref( sys ) , x , xerr );
  122. BOOST_CHECK( (abs( x[0] - result )) < eps );
  123. }
  124. };
  125. template< class State > class error_stepper_methods : public mpl::vector<
  126. runge_kutta_cash_karp54_classic< State , typename detail::extract_value_type<State>::type > ,
  127. runge_kutta_cash_karp54< State , typename detail::extract_value_type<State>::type > ,
  128. runge_kutta_dopri5< State , typename detail::extract_value_type<State>::type > ,
  129. runge_kutta_fehlberg78< State , typename detail::extract_value_type<State>::type >
  130. > { };
  131. typedef mpl::copy
  132. <
  133. container_types ,
  134. mpl::inserter
  135. <
  136. mpl::vector0<> ,
  137. mpl::insert_range
  138. <
  139. mpl::_1 ,
  140. mpl::end< mpl::_1 > ,
  141. error_stepper_methods< mpl::_2 >
  142. >
  143. >
  144. >::type all_error_stepper_methods;
  145. BOOST_AUTO_TEST_SUITE( runge_kutta_error_concept_test )
  146. BOOST_AUTO_TEST_CASE_TEMPLATE( error_stepper_test , Stepper , all_error_stepper_methods )
  147. {
  148. perform_error_stepper_test< Stepper , typename Stepper::state_type > tester;
  149. tester();
  150. }
  151. BOOST_AUTO_TEST_SUITE_END()