adaptive_time_iterator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/adaptive_time_iterator.cpp
  4. [begin_description]
  5. This file tests the adaptive time iterator.
  6. [end_description]
  7. Copyright 2012-2013 Karsten Ahnert
  8. Copyright 2012-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_adaptive_time_iterator
  14. #include <iterator>
  15. #include <algorithm>
  16. #include <vector>
  17. #include <boost/numeric/odeint/config.hpp>
  18. #include <boost/array.hpp>
  19. #include <boost/range/algorithm/copy.hpp>
  20. #include <boost/range/algorithm/for_each.hpp>
  21. #include <boost/mpl/vector.hpp>
  22. #include <boost/test/unit_test.hpp>
  23. #include <boost/test/floating_point_comparison.hpp>
  24. #include <boost/numeric/odeint/iterator/adaptive_time_iterator.hpp>
  25. #include "dummy_steppers.hpp"
  26. #include "dummy_odes.hpp"
  27. #include "dummy_observers.hpp"
  28. namespace mpl = boost::mpl;
  29. using namespace boost::numeric::odeint;
  30. typedef dummy_stepper::state_type state_type;
  31. typedef dummy_stepper::value_type value_type;
  32. typedef dummy_stepper::time_type time_type;
  33. typedef std::vector< std::pair< state_type , time_type > > result_vector;
  34. BOOST_AUTO_TEST_SUITE( adaptive_time_iterator_test )
  35. typedef mpl::vector<
  36. dummy_controlled_stepper
  37. , dummy_dense_output_stepper
  38. > dummy_steppers;
  39. BOOST_AUTO_TEST_CASE( copy_stepper_iterator )
  40. {
  41. typedef adaptive_time_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
  42. state_type x = {{ 1.0 }};
  43. iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
  44. iterator_type iter2 = iter1;
  45. BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
  46. BOOST_CHECK_EQUAL( &( iter1->first ) , &x );
  47. BOOST_CHECK( iter1.same( iter2 ) );
  48. }
  49. BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator )
  50. {
  51. typedef adaptive_time_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
  52. state_type x = {{ 1.0 }};
  53. iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
  54. iterator_type iter2 = iter1;
  55. BOOST_CHECK_NE( &( iter1->first ) , &( iter2->first ) );
  56. BOOST_CHECK( iter1.same( iter2 ) );
  57. }
  58. BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator_with_reference_wrapper )
  59. {
  60. typedef adaptive_time_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
  61. state_type x = {{ 1.0 }};
  62. dummy_dense_output_stepper stepper;
  63. iterator_type iter1 = iterator_type( boost::ref( stepper ) , empty_system() , x , 0.0 , 1.0 , 0.1 );
  64. iterator_type iter2 = iter1;
  65. BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
  66. BOOST_CHECK( iter1.same( iter2 ) );
  67. }
  68. BOOST_AUTO_TEST_CASE( assignment_stepper_iterator )
  69. {
  70. typedef adaptive_time_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
  71. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  72. iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  73. iterator_type iter2 = iterator_type( dummy_controlled_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
  74. BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
  75. BOOST_CHECK_EQUAL( &( iter2->first ) , &x2 );
  76. BOOST_CHECK( !iter1.same( iter2 ) );
  77. iter2 = iter1;
  78. BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
  79. BOOST_CHECK_EQUAL( &( iter2->first ) , &x1 );
  80. BOOST_CHECK( iter1.same( iter2 ) );
  81. }
  82. BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator )
  83. {
  84. typedef adaptive_time_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
  85. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  86. iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  87. iterator_type iter2 = iterator_type( dummy_dense_output_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
  88. BOOST_CHECK_NE( &( iter1->first ) , &x1 );
  89. BOOST_CHECK_NE( &( iter2->first ) , &x2 );
  90. BOOST_CHECK( !iter1.same( iter2 ) );
  91. iter2 = iter1;
  92. BOOST_CHECK_NE( &( iter1->first ) , &x1 );
  93. BOOST_CHECK_NE( &( iter2->first ) , &x1 );
  94. BOOST_CHECK( iter1.same( iter2 ) );
  95. BOOST_CHECK_EQUAL( (iter1->first)[0] , (iter1->first)[0] );
  96. }
  97. BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator_with_reference_wrapper )
  98. {
  99. typedef adaptive_time_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
  100. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  101. dummy_dense_output_stepper stepper;
  102. iterator_type iter1 = iterator_type( boost::ref( stepper ) , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  103. iterator_type iter2 = iterator_type( boost::ref( stepper ) , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
  104. BOOST_CHECK_NE( &( iter1->first ) , &x1 );
  105. BOOST_CHECK_NE( &( iter2->first ) , &x2 );
  106. // same stepper instance -> same internal state
  107. BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
  108. BOOST_CHECK( !iter1.same( iter2 ) );
  109. iter2 = iter1;
  110. BOOST_CHECK_NE( &( iter1->first ) , &x1 );
  111. BOOST_CHECK_NE( &( iter2->first ) , &x1 );
  112. BOOST_CHECK( iter1.same( iter2 ) );
  113. BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
  114. }
  115. BOOST_AUTO_TEST_CASE( stepper_iterator_factory )
  116. {
  117. dummy_controlled_stepper stepper;
  118. empty_system system;
  119. state_type x = {{ 1.0 }};
  120. std::for_each(
  121. make_adaptive_time_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  122. make_adaptive_time_iterator_end( stepper , boost::ref( system ) , x ) ,
  123. dummy_observer() );
  124. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  125. }
  126. // just test if it compiles
  127. BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_factory )
  128. {
  129. dummy_dense_output_stepper stepper;
  130. empty_system system;
  131. state_type x = {{ 1.0 }};
  132. std::for_each(
  133. make_adaptive_time_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  134. make_adaptive_time_iterator_end( stepper , boost::ref( system ) , x ) ,
  135. dummy_observer() );
  136. }
  137. BOOST_AUTO_TEST_CASE( stepper_range )
  138. {
  139. dummy_controlled_stepper stepper;
  140. empty_system system;
  141. state_type x = {{ 1.0 }};
  142. boost::for_each( make_adaptive_time_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  143. dummy_observer() );
  144. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  145. }
  146. // just test if it compiles
  147. BOOST_AUTO_TEST_CASE( dense_output_stepper_range )
  148. {
  149. dummy_dense_output_stepper stepper;
  150. empty_system system;
  151. state_type x = {{ 1.0 }};
  152. boost::for_each( make_adaptive_time_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  153. dummy_observer() );
  154. }
  155. BOOST_AUTO_TEST_CASE( stepper_iterator_with_reference_wrapper_factory )
  156. {
  157. dummy_controlled_stepper stepper;
  158. empty_system system;
  159. state_type x = {{ 1.0 }};
  160. std::for_each(
  161. make_adaptive_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  162. make_adaptive_time_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  163. dummy_observer() );
  164. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  165. }
  166. // just test if it compiles
  167. BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_with_reference_wrapper_factory )
  168. {
  169. dummy_dense_output_stepper stepper;
  170. empty_system system;
  171. state_type x = {{ 1.0 }};
  172. std::for_each(
  173. make_adaptive_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  174. make_adaptive_time_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  175. dummy_observer() );
  176. }
  177. BOOST_AUTO_TEST_CASE( stepper_range_with_reference_wrapper )
  178. {
  179. dummy_controlled_stepper stepper;
  180. empty_system system;
  181. state_type x = {{ 1.0 }};
  182. boost::for_each( make_adaptive_time_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  183. dummy_observer() );
  184. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  185. }
  186. // just test if it compiles
  187. BOOST_AUTO_TEST_CASE( dense_output_stepper_range_with_reference_wrapper )
  188. {
  189. dummy_dense_output_stepper stepper;
  190. empty_system system;
  191. state_type x = {{ 1.0 }};
  192. boost::for_each( make_adaptive_time_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  193. dummy_observer() );
  194. }
  195. BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
  196. {
  197. typedef adaptive_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
  198. state_type x = {{ 1.0 }};
  199. stepper_iterator first1( Stepper() , empty_system() , x , 1.5 , 1.0 , 0.1 );
  200. stepper_iterator last1( Stepper() , empty_system() , x );
  201. stepper_iterator last2( Stepper() , empty_system() , x );
  202. BOOST_CHECK( first1 == last1 );
  203. BOOST_CHECK( first1 == last2 );
  204. BOOST_CHECK( last1 == last2 );
  205. }
  206. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
  207. {
  208. typedef adaptive_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
  209. state_type x = {{ 1.0 }};
  210. result_vector res;
  211. stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 );
  212. stepper_iterator last( Stepper() , empty_system() , x );
  213. std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
  214. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  215. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  216. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  217. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  218. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  219. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  220. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  221. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  222. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  223. BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
  224. BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
  225. }
  226. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
  227. {
  228. state_type x = {{ 1.0 }};
  229. result_vector res;
  230. std::copy( make_adaptive_time_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
  231. make_adaptive_time_iterator_end( Stepper() , empty_system() , x ) ,
  232. std::back_insert_iterator< result_vector >( res ) );
  233. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  234. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  235. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  236. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  237. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  238. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  239. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  240. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  241. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  242. BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
  243. BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
  244. }
  245. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
  246. {
  247. state_type x = {{ 1.0 }};
  248. result_vector res;
  249. boost::range::copy( make_adaptive_time_range( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
  250. std::back_insert_iterator< result_vector >( res ) );
  251. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  252. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  253. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  254. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  255. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  256. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  257. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  258. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  259. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  260. BOOST_CHECK_CLOSE( res[4].first[0] , 2.0 , 1.0e-13 );
  261. BOOST_CHECK_CLOSE( res[4].second , 0.35 , 1.0e-13 );
  262. }
  263. BOOST_AUTO_TEST_SUITE_END()