integrate_overflow.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/integrate_overflow.cpp
  4. [begin_description]
  5. This file tests the overflow exception of the integrate functions.
  6. [end_description]
  7. Copyright 2015 Mario Mulansky
  8. Distributed under the Boost Software License, Version 1.0.
  9. (See accompanying file LICENSE_1_0.txt or
  10. copy at http://www.boost.org/LICENSE_1_0.txt)
  11. */
  12. #define BOOST_TEST_MODULE odeint_integrate_overflow
  13. #include <boost/test/unit_test.hpp>
  14. #include <utility>
  15. #include <iostream>
  16. #include <vector>
  17. #include <vector>
  18. #include <cmath>
  19. #include <iostream>
  20. #include <boost/iterator/counting_iterator.hpp>
  21. #include <boost/numeric/odeint.hpp>
  22. using namespace boost::numeric::odeint;
  23. typedef double value_type;
  24. typedef std::vector< value_type > state_type;
  25. // the famous lorenz system as usual
  26. void lorenz( const state_type &x , state_type &dxdt , const value_type t )
  27. {
  28. //const value_type sigma( 10.0 );
  29. const value_type R( 28.0 );
  30. const value_type b( value_type( 8.0 ) / value_type( 3.0 ) );
  31. // first component trivial
  32. dxdt[0] = 1.0; //sigma * ( x[1] - x[0] );
  33. dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
  34. dxdt[2] = -b * x[2] + x[0] * x[1];
  35. }
  36. struct push_back_time
  37. {
  38. std::vector< double >& m_times;
  39. push_back_time( std::vector< double > &times )
  40. : m_times( times ) { }
  41. void operator()( const state_type &x , double t )
  42. {
  43. m_times.push_back( t );
  44. }
  45. };
  46. typedef runge_kutta_dopri5<state_type> stepper_type;
  47. typedef runge_kutta_cash_karp54<state_type> cash_karp_type;
  48. BOOST_AUTO_TEST_SUITE( integrate_overflow )
  49. BOOST_AUTO_TEST_CASE( test_integrate_const )
  50. {
  51. state_type x(3, 5.0);
  52. std::vector<double> times;
  53. // check the function signatures with normal stepper
  54. integrate_const(stepper_type(), lorenz, x, 0.0, 10.0, 1.0);
  55. integrate_const(stepper_type(), lorenz, x, 0.0, 10.0, 1.0, null_observer());
  56. // no exceptions expected for normal steppers
  57. integrate_const(stepper_type(), lorenz, x, 0.0, 10.0, 1.0, null_observer(), max_step_checker(10));
  58. // check exceptions for controlled stepper
  59. integrate_const(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 10.0, 1.0);
  60. // very small error terms -> standard overflow threshold of 500 should fire an exception
  61. BOOST_CHECK_THROW(integrate_const(make_controlled<stepper_type>(1E-15, 1E-15), lorenz, x,
  62. 0.0, 10.0, 1.0, push_back_time(times), max_step_checker()),
  63. std::runtime_error);
  64. // small threshold of 10 -> larger error still gives an exception
  65. BOOST_CHECK_THROW(integrate_const(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x,
  66. 0.0, 10.0, 1.0, push_back_time(times), max_step_checker(10)),
  67. std::runtime_error);
  68. // check exceptions for dense output stepper
  69. integrate_const(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 10.0, 1.0);
  70. integrate_const(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 10.0, 1.0);
  71. // very small error terms -> standard overflow threshold of 500 should fire an exception
  72. BOOST_CHECK_THROW(integrate_const(make_dense_output<stepper_type>(1E-15, 1E-15), lorenz, x,
  73. 0.0, 10.0, 1.0, push_back_time(times), max_step_checker()),
  74. std::runtime_error);
  75. // small threshold of 10 -> larger error still gives an exception
  76. BOOST_CHECK_THROW(integrate_const(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x,
  77. 0.0, 10.0, 1.0, push_back_time(times), max_step_checker(10)),
  78. std::runtime_error);
  79. }
  80. BOOST_AUTO_TEST_CASE( test_integrate_n_steps )
  81. {
  82. state_type x(3, 5.0);
  83. std::vector<double> times;
  84. // check the function signatures with normal stepper
  85. integrate_n_steps(stepper_type(), lorenz, x, 0.0, 1.0, 10);
  86. integrate_n_steps(stepper_type(), lorenz, x, 0.0, 1.0, 10, null_observer());
  87. // no exceptions expected for normal steppers
  88. integrate_n_steps(stepper_type(), lorenz, x, 0.0, 1.0, 10, null_observer(), max_step_checker(10));
  89. // check exceptions for controlled stepper
  90. integrate_n_steps(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 1.0, 10);
  91. // very small error terms -> standard overflow threshold of 500 should fire an exception
  92. BOOST_CHECK_THROW(integrate_n_steps(make_controlled<stepper_type>(1E-15, 1E-15), lorenz, x,
  93. 0.0, 1.0, 10, push_back_time(times), max_step_checker()),
  94. std::runtime_error);
  95. // small threshold of 10 -> larger error still gives an exception
  96. BOOST_CHECK_THROW(integrate_n_steps(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x,
  97. 0.0, 1.0, 10, push_back_time(times), max_step_checker(10)),
  98. std::runtime_error);
  99. // check exceptions for dense output stepper
  100. integrate_n_steps(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 1.0, 10);
  101. integrate_n_steps(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x, 0.0, 1.0, 10, push_back_time(times));
  102. // very small error terms -> standard overflow threshold of 500 should fire an exception
  103. BOOST_CHECK_THROW(integrate_n_steps(make_dense_output<stepper_type>(1E-15, 1E-15), lorenz, x,
  104. 0.0, 1.0, 10, push_back_time(times), max_step_checker()),
  105. std::runtime_error);
  106. // small threshold of 10 -> larger error still gives an exception
  107. BOOST_CHECK_THROW(integrate_n_steps(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x,
  108. 0.0, 1.0, 10, push_back_time(times), max_step_checker(10)),
  109. std::runtime_error);
  110. }
  111. BOOST_AUTO_TEST_CASE( test_integrate_times )
  112. {
  113. state_type x(3, 5.0);
  114. std::vector<double> times;
  115. boost::counting_iterator<int> t0(0);
  116. boost::counting_iterator<int> t1(10);
  117. // check the function signatures with normal stepper
  118. integrate_times(stepper_type(), lorenz, x, t0, t1, 1.0 , push_back_time(times));
  119. // no exceptions expected for big enough step size
  120. integrate_times(stepper_type(), lorenz, x, t0, t1, 1.0 , push_back_time(times), max_step_checker(10));
  121. // if dt*max_steps < observer time difference we expect an exception
  122. BOOST_CHECK_THROW(integrate_times(stepper_type(), lorenz, x, t0, t1, 0.01, push_back_time(times),
  123. max_step_checker(10)),
  124. std::runtime_error);
  125. // check exceptions for controlled stepper
  126. // no exception if no checker is provided
  127. integrate_times(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x, t0, t1, 1.0 , push_back_time(times));
  128. // very small error terms -> standard overflow threshold of 500 should fire an exception
  129. BOOST_CHECK_THROW(integrate_times(make_controlled<stepper_type>(1E-15, 1E-15), lorenz, x,
  130. t0, t1, 1.0 , push_back_time(times), max_step_checker()),
  131. std::runtime_error);
  132. // small threshold of 10 -> larger error still gives an exception
  133. BOOST_CHECK_THROW(integrate_times(make_controlled<stepper_type>(1E-5, 1E-5), lorenz, x,
  134. t0, t1, 1.0 , push_back_time(times), max_step_checker(10)),
  135. std::runtime_error);
  136. // check cash karp controlled stepper
  137. // no exception if no checker is provided
  138. integrate_times(make_controlled<cash_karp_type>(1E-5, 1E-5), lorenz, x, t0, t1, 1.0 , push_back_time(times));
  139. // very small error terms -> standard overflow threshold of 500 should fire an exception
  140. BOOST_CHECK_THROW(integrate_times(make_controlled<cash_karp_type>(1E-15, 1E-15), lorenz, x,
  141. t0, t1, 1.0 , push_back_time(times), max_step_checker()),
  142. std::runtime_error);
  143. // small threshold of 10 -> larger error still gives an exception
  144. BOOST_CHECK_THROW(integrate_times(make_controlled<cash_karp_type>(1E-5, 1E-5), lorenz, x,
  145. t0, t1, 1.0 , push_back_time(times), max_step_checker(10)),
  146. std::runtime_error);
  147. // check exceptions for dense output stepper
  148. integrate_times(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x, t0, t1, 1.0 ,
  149. push_back_time(times));
  150. // very small error terms -> standard overflow threshold of 500 should fire an exception
  151. BOOST_CHECK_THROW(integrate_times(make_dense_output<stepper_type>(1E-15, 1E-15), lorenz, x,
  152. t0, t1, 1.0 , push_back_time(times), max_step_checker()),
  153. std::runtime_error);
  154. // small threshold of 10 -> larger error still gives an exception
  155. BOOST_CHECK_THROW(integrate_times(make_dense_output<stepper_type>(1E-5, 1E-5), lorenz, x,
  156. t0, t1, 1.0 , push_back_time(times), max_step_checker(10)),
  157. std::runtime_error);
  158. }
  159. BOOST_AUTO_TEST_SUITE_END()