integrate_implicit.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/integrate_implicit.cpp
  4. [begin_description]
  5. This file tests the integrate function and its variants with the implicit steppers.
  6. [end_description]
  7. Copyright 2011 Mario Mulansky
  8. Copyright 2011-2013 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_integrate_functions_implicit
  14. #include <vector>
  15. #include <cmath>
  16. #include <iostream>
  17. #include <boost/numeric/odeint/config.hpp>
  18. #include <boost/array.hpp>
  19. #include <boost/ref.hpp>
  20. #include <boost/iterator/counting_iterator.hpp>
  21. #include <boost/numeric/ublas/vector.hpp>
  22. #include <boost/numeric/ublas/matrix.hpp>
  23. #include <boost/test/unit_test.hpp>
  24. #include <boost/mpl/vector.hpp>
  25. #ifndef ODEINT_INTEGRATE_ITERATOR
  26. #include <boost/numeric/odeint/integrate/integrate_const.hpp>
  27. #include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
  28. #include <boost/numeric/odeint/integrate/integrate_times.hpp>
  29. #include <boost/numeric/odeint/integrate/integrate_n_steps.hpp>
  30. #else
  31. #include <boost/numeric/odeint/iterator/integrate/integrate_const.hpp>
  32. #include <boost/numeric/odeint/iterator/integrate/integrate_adaptive.hpp>
  33. #include <boost/numeric/odeint/iterator/integrate/integrate_times.hpp>
  34. #include <boost/numeric/odeint/iterator/integrate/integrate_n_steps.hpp>
  35. #endif
  36. #include <boost/numeric/odeint/stepper/rosenbrock4.hpp>
  37. #include <boost/numeric/odeint/stepper/rosenbrock4_controller.hpp>
  38. #include <boost/numeric/odeint/stepper/rosenbrock4_dense_output.hpp>
  39. using namespace boost::unit_test;
  40. using namespace boost::numeric::odeint;
  41. namespace mpl = boost::mpl;
  42. namespace ublas = boost::numeric::ublas;
  43. typedef double value_type;
  44. typedef ublas::vector< value_type > state_type;
  45. typedef boost::numeric::ublas::matrix< value_type > matrix_type;
  46. struct sys
  47. {
  48. void operator()( const state_type &x , state_type &dxdt , const value_type &t ) const
  49. {
  50. dxdt( 0 ) = x( 0 ) + 2 * x( 1 );
  51. dxdt( 1 ) = x( 1 );
  52. }
  53. };
  54. struct jacobi
  55. {
  56. void operator()( const state_type &x , matrix_type &jacobi , const value_type &t , state_type &dfdt ) const
  57. {
  58. jacobi( 0 , 0 ) = 1;
  59. jacobi( 0 , 1 ) = 2;
  60. jacobi( 1 , 0 ) = 0;
  61. jacobi( 1 , 1 ) = 1;
  62. dfdt( 0 ) = 0.0;
  63. dfdt( 1 ) = 0.0;
  64. }
  65. };
  66. struct push_back_time
  67. {
  68. std::vector< double >& m_times;
  69. push_back_time( std::vector< double > &times )
  70. : m_times( times ) { }
  71. void operator()( const state_type &x , double t )
  72. {
  73. m_times.push_back( t );
  74. }
  75. };
  76. template< class Stepper >
  77. struct perform_integrate_const_test
  78. {
  79. void operator()( void )
  80. {
  81. state_type x( 2 , 1.0 );
  82. const value_type dt = 0.03;
  83. const value_type t_end = 10.0;
  84. std::vector< value_type > times;
  85. integrate_const( Stepper() , std::make_pair( sys() , jacobi() ) , x , 0.0 , t_end ,
  86. dt , push_back_time( times ) );
  87. BOOST_CHECK_EQUAL( static_cast<int>(times.size()) , static_cast<int>(std::floor(t_end/dt))+1 );
  88. for( size_t i=0 ; i<times.size() ; ++i )
  89. {
  90. //std::cout << i << std::endl;
  91. // check if observer was called at times 0,1,2,...
  92. BOOST_CHECK_SMALL( times[i] - static_cast< value_type >(i)*dt , (i+1) * 2.0e-16 );
  93. }
  94. }
  95. };
  96. template< class Stepper >
  97. struct perform_integrate_adaptive_test
  98. {
  99. void operator()( void )
  100. {
  101. state_type x( 2 , 1.0 );
  102. const value_type dt = 0.03;
  103. const value_type t_end = 10.0;
  104. std::vector< value_type > times;
  105. size_t steps = integrate_adaptive( Stepper() , std::make_pair( sys() , jacobi() ) , x , 0.0 , t_end ,
  106. dt , push_back_time( times ) );
  107. BOOST_CHECK_EQUAL( times.size() , steps+1 );
  108. BOOST_CHECK_SMALL( times[0] - 0.0 , 2.0e-16 );
  109. BOOST_CHECK_SMALL( times[times.size()-1] - t_end , times.size() * 3.0e-16 );
  110. }
  111. };
  112. template< class Stepper >
  113. struct perform_integrate_times_test
  114. {
  115. void operator()( void )
  116. {
  117. state_type x( 2 , 1.0 );
  118. const value_type dt = 0.03;
  119. std::vector< double > times;
  120. // simple stepper
  121. integrate_times( Stepper() , std::make_pair( sys() , jacobi() ) , x ,
  122. boost::counting_iterator<int>(0) , boost::counting_iterator<int>(10) ,
  123. dt , push_back_time( times ) );
  124. BOOST_CHECK_EQUAL( static_cast<int>(times.size()) , 10 );
  125. for( size_t i=0 ; i<times.size() ; ++i )
  126. // check if observer was called at times 0,1,2,...
  127. BOOST_CHECK_EQUAL( times[i] , static_cast<double>(i) );
  128. }
  129. };
  130. template< class Stepper >
  131. struct perform_integrate_n_steps_test
  132. {
  133. void operator()( void )
  134. {
  135. state_type x( 2 , 1.0 );
  136. const value_type dt = 0.03;
  137. const int n = 200;
  138. std::vector< double > times;
  139. // simple stepper
  140. value_type end_time = integrate_n_steps( Stepper() , std::make_pair( sys() , jacobi() ) , x , 0.0 , dt , n , push_back_time( times ) );
  141. BOOST_CHECK_SMALL( end_time - n*dt , 3.0e-16 );
  142. BOOST_CHECK_EQUAL( static_cast<int>(times.size()) , n+1 );
  143. for( size_t i=0 ; i<times.size() ; ++i )
  144. // check if observer was called at times 0,1,2,...
  145. BOOST_CHECK_SMALL( times[i] - static_cast< value_type >(i)*dt , (i+1) * 2.0e-16 );
  146. }
  147. };
  148. class stepper_methods : public mpl::vector<
  149. rosenbrock4< value_type > ,
  150. rosenbrock4_controller< rosenbrock4< value_type > > ,
  151. rosenbrock4_dense_output< rosenbrock4_controller< rosenbrock4< value_type > > >
  152. > { };
  153. BOOST_AUTO_TEST_SUITE( integrate_test )
  154. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_const_test_case , Stepper, stepper_methods )
  155. {
  156. perform_integrate_const_test< Stepper > tester;
  157. tester();
  158. }
  159. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_adaptive_test_case , Stepper, stepper_methods )
  160. {
  161. perform_integrate_adaptive_test< Stepper > tester;
  162. tester();
  163. }
  164. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_times_test_case , Stepper, stepper_methods )
  165. {
  166. perform_integrate_times_test< Stepper > tester;
  167. tester();
  168. }
  169. class simple_stepper_methods : public mpl::vector<
  170. rosenbrock4< value_type >
  171. > { };
  172. BOOST_AUTO_TEST_CASE_TEMPLATE( integrate_n_steps_test_case , Stepper, simple_stepper_methods )
  173. {
  174. perform_integrate_n_steps_test< Stepper > tester;
  175. tester();
  176. }
  177. BOOST_AUTO_TEST_SUITE_END()