integrate_adaptive.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/integrate/detail/integrate_adaptive.hpp
  4. [begin_description]
  5. Default Integrate adaptive implementation.
  6. [end_description]
  7. Copyright 2009-2011 Karsten Ahnert
  8. Copyright 2009-2011 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. #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED
  14. #define BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED
  15. #include <stdexcept>
  16. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  17. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  18. #include <boost/numeric/odeint/integrate/detail/integrate_n_steps.hpp>
  19. #include <boost/numeric/odeint/util/bind.hpp>
  20. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  21. #include <boost/numeric/odeint/util/copy.hpp>
  22. #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
  23. #include <iostream>
  24. namespace boost {
  25. namespace numeric {
  26. namespace odeint {
  27. namespace detail {
  28. // forward declaration
  29. template< class Stepper , class System , class State , class Time , class Observer>
  30. Time integrate_n_steps(
  31. Stepper stepper , System system , State &start_state ,
  32. Time start_time , Time dt , size_t num_of_steps ,
  33. Observer observer , stepper_tag );
  34. /*
  35. * integrate_adaptive for simple stepper is basically an integrate_const + some last step
  36. */
  37. template< class Stepper , class System , class State , class Time , class Observer >
  38. size_t integrate_adaptive(
  39. Stepper stepper , System system , State &start_state ,
  40. Time start_time , Time end_time , Time dt ,
  41. Observer observer , stepper_tag
  42. )
  43. {
  44. size_t steps = static_cast< size_t >( (end_time-start_time)/dt );
  45. Time end = detail::integrate_n_steps( stepper , system , start_state , start_time ,
  46. dt , steps , observer , stepper_tag() );
  47. if( less_with_sign( end , end_time , dt ) )
  48. { //make a last step to end exactly at end_time
  49. stepper.do_step( system , start_state , end , end_time - end );
  50. steps++;
  51. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  52. obs( start_state , end_time );
  53. }
  54. return steps;
  55. }
  56. /*
  57. * classical integrate adaptive
  58. */
  59. template< class Stepper , class System , class State , class Time , class Observer >
  60. size_t integrate_adaptive(
  61. Stepper stepper , System system , State &start_state ,
  62. Time &start_time , Time end_time , Time &dt ,
  63. Observer observer , controlled_stepper_tag
  64. )
  65. {
  66. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  67. const size_t max_attempts = 1000;
  68. const char *error_string = "Integrate adaptive : Maximal number of iterations reached. A step size could not be found.";
  69. size_t count = 0;
  70. while( less_with_sign( start_time , end_time , dt ) )
  71. {
  72. obs( start_state , start_time );
  73. if( less_with_sign( end_time , start_time + dt , dt ) )
  74. {
  75. dt = end_time - start_time;
  76. }
  77. size_t trials = 0;
  78. controlled_step_result res = success;
  79. do
  80. {
  81. res = stepper.try_step( system , start_state , start_time , dt );
  82. ++trials;
  83. }
  84. while( ( res == fail ) && ( trials < max_attempts ) );
  85. if( trials == max_attempts ) throw std::overflow_error( error_string );
  86. ++count;
  87. }
  88. obs( start_state , start_time );
  89. return count;
  90. }
  91. /*
  92. * integrate adaptive for dense output steppers
  93. *
  94. * step size control is used if the stepper supports it
  95. */
  96. template< class Stepper , class System , class State , class Time , class Observer >
  97. size_t integrate_adaptive(
  98. Stepper stepper , System system , State &start_state ,
  99. Time start_time , Time end_time , Time dt ,
  100. Observer observer , dense_output_stepper_tag )
  101. {
  102. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  103. size_t count = 0;
  104. stepper.initialize( start_state , start_time , dt );
  105. while( less_with_sign( stepper.current_time() , end_time , stepper.current_time_step() ) )
  106. {
  107. while( less_eq_with_sign( stepper.current_time() + stepper.current_time_step() ,
  108. end_time ,
  109. stepper.current_time_step() ) )
  110. { //make sure we don't go beyond the end_time
  111. obs( stepper.current_state() , stepper.current_time() );
  112. stepper.do_step( system );
  113. ++count;
  114. }
  115. stepper.initialize( stepper.current_state() , stepper.current_time() , end_time - stepper.current_time() );
  116. }
  117. obs( stepper.current_state() , stepper.current_time() );
  118. // overwrite start_state with the final point
  119. boost::numeric::odeint::copy( stepper.current_state() , start_state );
  120. return count;
  121. }
  122. } // namespace detail
  123. } // namespace odeint
  124. } // namespace numeric
  125. } // namespace boost
  126. #endif // BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED