integrate_stepper_refs.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/integrate_stepper_refs.cpp
  4. [begin_description]
  5. Tests the integrate functions with boost::ref( stepper)
  6. [end_description]
  7. Copyright 2009-2013 Karsten Ahnert
  8. Copyright 2009-2013 Mario Mulansky
  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_integrate_stepper_refs
  14. #include <vector>
  15. #include <cmath>
  16. #include <iostream>
  17. #include <boost/numeric/odeint/config.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. #include <boost/iterator/counting_iterator.hpp>
  21. #include <boost/mpl/vector.hpp>
  22. #include <boost/numeric/odeint/integrate/integrate_const.hpp>
  23. #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
  24. #include <boost/numeric/odeint/integrate/integrate_times.hpp>
  25. #include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
  26. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  27. using namespace boost::unit_test;
  28. using namespace boost::numeric::odeint;
  29. namespace mpl = boost::mpl;
  30. typedef double value_type;
  31. typedef std::vector< value_type > state_type;
  32. // minimal non-copyable basic stepper
  33. template<
  34. class State
  35. >
  36. class simple_stepper_nc : boost::noncopyable
  37. {
  38. public :
  39. typedef State state_type;
  40. typedef double value_type;
  41. typedef state_type deriv_type;
  42. typedef double time_type;
  43. typedef size_t order_type;
  44. typedef stepper_tag stepper_category;
  45. template< class System >
  46. void do_step( System system , state_type &in , time_type t , time_type dt )
  47. {
  48. // empty
  49. }
  50. };
  51. // minimal non-copyable controlled stepper
  52. template<
  53. class State
  54. >
  55. class controlled_stepper_nc : boost::noncopyable
  56. {
  57. public :
  58. typedef State state_type;
  59. typedef double value_type;
  60. typedef state_type deriv_type;
  61. typedef double time_type;
  62. typedef size_t order_type;
  63. typedef controlled_stepper_tag stepper_category;
  64. template< class System >
  65. controlled_step_result try_step( System system , state_type &in , time_type &t , time_type &dt )
  66. {
  67. std::cout << "dense out stepper: " << t << " , " << dt << std::endl;
  68. t += dt;
  69. return success;
  70. }
  71. };
  72. // minimal non-copyable dense_output stepper
  73. template<
  74. class State
  75. >
  76. class dense_out_stepper_nc : boost::noncopyable
  77. {
  78. public :
  79. typedef State state_type;
  80. typedef double value_type;
  81. typedef state_type deriv_type;
  82. typedef double time_type;
  83. typedef size_t order_type;
  84. typedef dense_output_stepper_tag stepper_category;
  85. void initialize( const state_type &x0 , const time_type t0 , const time_type dt0 )
  86. {
  87. m_x = x0;
  88. m_t = t0;
  89. m_dt = dt0;
  90. std::cout << "initialize: " << m_t << " , " << m_dt << std::endl;
  91. }
  92. template< class System >
  93. void do_step( System system )
  94. {
  95. std::cout << "dense out stepper: " << m_t << " , " << m_dt << std::endl;
  96. m_t += m_dt;
  97. }
  98. void calc_state( const time_type t_inter , state_type &x )
  99. {
  100. x = m_x;
  101. }
  102. const state_type& current_state() const
  103. { return m_x; }
  104. time_type current_time() const
  105. { return m_t; }
  106. time_type current_time_step() const
  107. { return m_dt; }
  108. private :
  109. time_type m_t;
  110. time_type m_dt;
  111. state_type m_x;
  112. };
  113. void lorenz( const state_type &x , state_type &dxdt , const value_type t )
  114. {
  115. //const value_type sigma( 10.0 );
  116. const value_type R( 28.0 );
  117. const value_type b( value_type( 8.0 ) / value_type( 3.0 ) );
  118. // first component trivial
  119. dxdt[0] = 1.0; //sigma * ( x[1] - x[0] );
  120. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  121. dxdt[2] = -b * x[2] + x[0] * x[1];
  122. }
  123. struct push_back_time
  124. {
  125. std::vector< double >& m_times;
  126. state_type& m_x;
  127. push_back_time( std::vector< double > &times , state_type &x )
  128. : m_times( times ) , m_x( x ) { }
  129. void operator()( const state_type &x , double t )
  130. {
  131. m_times.push_back( t );
  132. boost::numeric::odeint::copy( x , m_x );
  133. }
  134. };
  135. template< class Stepper >
  136. struct perform_integrate_const_test
  137. {
  138. void operator()()
  139. {
  140. state_type x( 3 , 10.0 ) , x_end( 3 );
  141. std::vector< value_type > times;
  142. Stepper stepper;
  143. integrate_const( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
  144. 0.1, push_back_time( times , x_end ) );
  145. }
  146. };
  147. template< class Stepper >
  148. struct perform_integrate_adaptive_test
  149. {
  150. void operator()()
  151. {
  152. state_type x( 3 , 10.0 ) , x_end( 3 );
  153. std::vector< value_type > times;
  154. Stepper stepper;
  155. integrate_adaptive( boost::ref(stepper) , lorenz , x , 0.0 , 1.0 ,
  156. 0.1, push_back_time( times , x_end ) );
  157. }
  158. };
  159. template< class Stepper >
  160. struct perform_integrate_n_steps_test
  161. {
  162. void operator()()
  163. {
  164. state_type x( 3 , 10.0 ) , x_end( 3 );
  165. std::vector< value_type > times;
  166. Stepper stepper;
  167. integrate_n_steps( boost::ref(stepper) , lorenz , x , 0.0 , 0.1 ,
  168. 10 , push_back_time( times , x_end ) );
  169. }
  170. };
  171. template< class Stepper >
  172. struct perform_integrate_times_test
  173. {
  174. void operator()()
  175. {
  176. state_type x( 3 , 10.0 ) , x_end( 3 );
  177. std::vector< value_type > times;
  178. Stepper stepper;
  179. integrate_times( boost::ref(stepper) , lorenz , x ,
  180. boost::counting_iterator<int>(0) , boost::counting_iterator<int>(10) , 0.1 ,
  181. push_back_time( times , x_end ) );
  182. }
  183. };
  184. class stepper_methods : public mpl::vector<
  185. simple_stepper_nc< state_type > ,
  186. controlled_stepper_nc< state_type >,
  187. dense_out_stepper_nc< state_type > > { };
  188. BOOST_AUTO_TEST_SUITE( integrate_stepper_refs )
  189. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case , Stepper, stepper_methods )
  190. {
  191. std::cout << "integrate const" << std::endl;
  192. perform_integrate_const_test< Stepper > tester;
  193. tester();
  194. }
  195. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case , Stepper, stepper_methods )
  196. {
  197. std::cout << "integrate adaptive" << std::endl;
  198. perform_integrate_adaptive_test< Stepper > tester;
  199. tester();
  200. }
  201. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case , Stepper, stepper_methods )
  202. {
  203. std::cout << "integrate n steps" << std::endl;
  204. perform_integrate_n_steps_test< Stepper > tester;
  205. tester();
  206. }
  207. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case , Stepper, stepper_methods )
  208. {
  209. std::cout << "integrate times" << std::endl;
  210. perform_integrate_times_test< Stepper > tester;
  211. tester();
  212. }
  213. BOOST_AUTO_TEST_SUITE_END()