integrate_adaptive.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 2011-2013 Karsten Ahnert
  8. Copyright 2011-2015 Mario Mulansky
  9. Copyright 2012 Christoph Koke
  10. Distributed under the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or
  12. copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED
  16. #include <stdexcept>
  17. #include <boost/throw_exception.hpp>
  18. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  19. #include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
  20. #include <boost/numeric/odeint/integrate/max_step_checker.hpp>
  21. #include <boost/numeric/odeint/integrate/detail/integrate_const.hpp>
  22. #include <boost/numeric/odeint/util/bind.hpp>
  23. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  24. #include <boost/numeric/odeint/util/copy.hpp>
  25. #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
  26. #include <iostream>
  27. namespace boost {
  28. namespace numeric {
  29. namespace odeint {
  30. namespace detail {
  31. // forward declaration
  32. template< class Stepper , class System , class State , class Time , class Observer >
  33. size_t integrate_const(
  34. Stepper stepper , System system , State &start_state ,
  35. Time start_time , Time end_time , Time dt ,
  36. Observer observer , stepper_tag );
  37. /*
  38. * integrate_adaptive for simple stepper is basically an integrate_const + some last step
  39. */
  40. template< class Stepper , class System , class State , class Time , class Observer >
  41. size_t integrate_adaptive(
  42. Stepper stepper , System system , State &start_state ,
  43. Time start_time , Time end_time , Time dt ,
  44. Observer observer , stepper_tag
  45. )
  46. {
  47. size_t steps = detail::integrate_const( stepper , system , start_state , start_time ,
  48. end_time , dt , observer , stepper_tag() );
  49. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  50. typename odeint::unwrap_reference< Stepper >::type &st = stepper;
  51. Time end = start_time + dt*steps;
  52. if( less_with_sign( end , end_time , dt ) )
  53. { //make a last step to end exactly at end_time
  54. st.do_step( system , start_state , end , end_time - end );
  55. steps++;
  56. obs( start_state , end_time );
  57. }
  58. return steps;
  59. }
  60. /*
  61. * integrate adaptive for controlled stepper
  62. */
  63. template< class Stepper , class System , class State , class Time , class Observer >
  64. size_t integrate_adaptive(
  65. Stepper stepper , System system , State &start_state ,
  66. Time &start_time , Time end_time , Time &dt ,
  67. Observer observer , controlled_stepper_tag
  68. )
  69. {
  70. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  71. typename odeint::unwrap_reference< Stepper >::type &st = stepper;
  72. failed_step_checker fail_checker; // to throw a runtime_error if step size adjustment fails
  73. size_t count = 0;
  74. while( less_with_sign( start_time , end_time , dt ) )
  75. {
  76. obs( start_state , start_time );
  77. if( less_with_sign( end_time , static_cast<Time>(start_time + dt) , dt ) )
  78. {
  79. dt = end_time - start_time;
  80. }
  81. controlled_step_result res;
  82. do
  83. {
  84. res = st.try_step( system , start_state , start_time , dt );
  85. fail_checker(); // check number of failed steps
  86. }
  87. while( res == fail );
  88. fail_checker.reset(); // if we reach here, the step was successful -> reset fail checker
  89. ++count;
  90. }
  91. obs( start_state , start_time );
  92. return count;
  93. }
  94. /*
  95. * integrate adaptive for dense output steppers
  96. *
  97. * step size control is used if the stepper supports it
  98. */
  99. template< class Stepper , class System , class State , class Time , class Observer >
  100. size_t integrate_adaptive(
  101. Stepper stepper , System system , State &start_state ,
  102. Time start_time , Time end_time , Time dt ,
  103. Observer observer , dense_output_stepper_tag )
  104. {
  105. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  106. typename odeint::unwrap_reference< Stepper >::type &st = stepper;
  107. size_t count = 0;
  108. st.initialize( start_state , start_time , dt );
  109. while( less_with_sign( st.current_time() , end_time , st.current_time_step() ) )
  110. {
  111. while( less_eq_with_sign( static_cast<Time>(st.current_time() + st.current_time_step()) ,
  112. end_time ,
  113. st.current_time_step() ) )
  114. { //make sure we don't go beyond the end_time
  115. obs( st.current_state() , st.current_time() );
  116. st.do_step( system );
  117. ++count;
  118. }
  119. // calculate time step to arrive exactly at end time
  120. st.initialize( st.current_state() , st.current_time() , static_cast<Time>(end_time - st.current_time()) );
  121. }
  122. obs( st.current_state() , st.current_time() );
  123. // overwrite start_state with the final point
  124. boost::numeric::odeint::copy( st.current_state() , start_state );
  125. return count;
  126. }
  127. } // namespace detail
  128. } // namespace odeint
  129. } // namespace numeric
  130. } // namespace boost
  131. #endif // BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_ADAPTIVE_HPP_INCLUDED