runge_kutta_concepts.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/runge_kutta_concepts.cpp
  4. [begin_description]
  5. This file tests the Stepper concepts of odeint with all Runge-Kutta steppers. It's one of the main tests
  6. 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_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/euler.hpp>
  38. #include <boost/numeric/odeint/stepper/modified_midpoint.hpp>
  39. #include <boost/numeric/odeint/stepper/runge_kutta4_classic.hpp>
  40. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  41. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54_classic.hpp>
  42. #include <boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp>
  43. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  44. #include <boost/numeric/odeint/stepper/runge_kutta_fehlberg78.hpp>
  45. #include <boost/numeric/odeint/algebra/detail/extract_value_type.hpp>
  46. #include "prepare_stepper_testing.hpp"
  47. #include "dummy_odes.hpp"
  48. using std::vector;
  49. using namespace boost::unit_test;
  50. using namespace boost::numeric::odeint;
  51. namespace mpl = boost::mpl;
  52. const double result = 2.2;
  53. const double eps = 1.0e-14;
  54. template< class Stepper , class System >
  55. void check_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x )
  56. {
  57. typedef Stepper stepper_type;
  58. typedef typename stepper_type::deriv_type container_type;
  59. typedef typename stepper_type::order_type order_type;
  60. typedef typename stepper_type::time_type time_type;
  61. stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) );
  62. }
  63. // default case is used for vector space types like plain double
  64. template< class Stepper , typename T >
  65. struct perform_stepper_test
  66. {
  67. typedef T vector_space_type;
  68. void operator()( void ) const
  69. {
  70. using std::abs;
  71. vector_space_type x;
  72. x = 2.0;
  73. Stepper stepper;
  74. constant_system_functor_vector_space sys;
  75. #ifndef _MSC_VER
  76. // dont run this for MSVC due to compiler bug 697006
  77. check_stepper_concept( stepper , constant_system_vector_space< vector_space_type , vector_space_type , typename Stepper::time_type > , x );
  78. #else
  79. check_stepper_concept( stepper , boost::cref( sys ) , x );
  80. #endif
  81. check_stepper_concept( stepper , boost::cref( sys ) , x );
  82. std::cout << x << " ?= " << result << std::endl;
  83. BOOST_CHECK( (abs( x - result )) < eps );
  84. }
  85. };
  86. template< class Stepper , typename T >
  87. struct perform_stepper_test< Stepper , std::vector<T> >
  88. {
  89. typedef std::vector<T> vector_type;
  90. void operator()( void )
  91. {
  92. using std::abs;
  93. vector_type x( 1 , static_cast<T>(2.0) );
  94. Stepper stepper;
  95. constant_system_functor_standard sys;
  96. #ifndef _MSC_VER
  97. // dont run this for MSVC due to compiler bug 697006
  98. check_stepper_concept( stepper , constant_system_standard< vector_type , vector_type , typename Stepper::time_type > , x );
  99. #else
  100. check_stepper_concept( stepper , boost::cref( sys ) , x );
  101. #endif
  102. check_stepper_concept( stepper , boost::cref( sys ) , x );
  103. std::cout << x[0] << " ?= " << result << std::endl;
  104. BOOST_CHECK( (abs( x[0] - result )) < eps );
  105. }
  106. };
  107. template< class Stepper , typename T >
  108. struct perform_stepper_test< Stepper , boost::array<T,1> >
  109. {
  110. typedef boost::array<T,1> array_type;
  111. void operator()( void )
  112. {
  113. using std::abs;
  114. array_type x;
  115. x[0] = static_cast<T>(2.0);
  116. Stepper stepper;
  117. constant_system_functor_standard sys;
  118. #ifndef _MSC_VER
  119. // dont run this for MSVC due to compiler bug 697006
  120. check_stepper_concept( stepper , constant_system_standard< array_type , array_type , typename Stepper::time_type > , x );
  121. #else
  122. check_stepper_concept( stepper , boost::cref( sys ) , x );
  123. #endif
  124. check_stepper_concept( stepper , boost::cref( sys ) , x );
  125. std::cout << x[0] << " ?= " << result << std::endl;
  126. BOOST_CHECK( (abs( x[0] - result )) < eps );
  127. }
  128. };
  129. // split stepper methods to ensure the final vector has less than 30(?) elements
  130. // (stepper_methods*container_types) < 30(?)
  131. template< class State > class stepper_methods1 : public mpl::vector<
  132. euler< State , typename detail::extract_value_type<State>::type > ,
  133. modified_midpoint< State , typename detail::extract_value_type<State>::type > ,
  134. runge_kutta4< State , typename detail::extract_value_type<State>::type > ,
  135. runge_kutta4_classic< State , typename detail::extract_value_type<State>::type >
  136. > { };
  137. template< class State > class stepper_methods2 : public mpl::vector<
  138. runge_kutta_cash_karp54_classic< State , typename detail::extract_value_type<State>::type > ,
  139. runge_kutta_cash_karp54< State , typename detail::extract_value_type<State>::type > ,
  140. runge_kutta_dopri5< State , typename detail::extract_value_type<State>::type > ,
  141. runge_kutta_fehlberg78< State , typename detail::extract_value_type<State>::type >
  142. > { };
  143. typedef mpl::copy
  144. <
  145. container_types ,
  146. mpl::inserter
  147. <
  148. mpl::vector0<> ,
  149. mpl::insert_range
  150. <
  151. mpl::_1 ,
  152. mpl::end< mpl::_1 > ,
  153. stepper_methods1< mpl::_2 >
  154. >
  155. >
  156. >::type stepper_combinations1;
  157. typedef mpl::copy
  158. <
  159. container_types ,
  160. mpl::inserter
  161. <
  162. mpl::vector0<> ,
  163. mpl::insert_range
  164. <
  165. mpl::_1 ,
  166. mpl::end< mpl::_1 > ,
  167. stepper_methods2< mpl::_2 >
  168. >
  169. >
  170. >::type stepper_combinations2;
  171. BOOST_AUTO_TEST_SUITE( runge_kutta_concept_test )
  172. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_test1 , Stepper, stepper_combinations1 )
  173. {
  174. perform_stepper_test< Stepper , typename Stepper::deriv_type > tester;
  175. tester();
  176. }
  177. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_test2 , Stepper, stepper_combinations2 )
  178. {
  179. perform_stepper_test< Stepper , typename Stepper::deriv_type > tester;
  180. tester();
  181. }
  182. BOOST_AUTO_TEST_SUITE_END()