integrate_n_steps.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/integrate/detail/integrate_n_steps.hpp
  4. [begin_description]
  5. integrate steps implementation
  6. [end_description]
  7. Copyright 2009-2012 Karsten Ahnert
  8. Copyright 2009-2012 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_N_STEPS_HPP_INCLUDED
  14. #define BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_N_STEPS_HPP_INCLUDED
  15. #include <boost/numeric/odeint/util/unwrap_reference.hpp>
  16. #include <boost/numeric/odeint/stepper/stepper_categories.hpp>
  17. #include <boost/numeric/odeint/iterator/integrate/detail/integrate_adaptive.hpp>
  18. #include <boost/numeric/odeint/iterator/integrate/detail/functors.hpp>
  19. #include <boost/numeric/odeint/iterator/n_step_time_iterator.hpp>
  20. #include <boost/numeric/odeint/util/unit_helper.hpp>
  21. #include <boost/numeric/odeint/util/detail/less_with_sign.hpp>
  22. namespace boost {
  23. namespace numeric {
  24. namespace odeint {
  25. namespace detail {
  26. // forward declaration
  27. template< class Stepper , class System , class State , class Time , class Observer >
  28. size_t integrate_adaptive(
  29. Stepper stepper , System system , State &start_state ,
  30. Time &start_time , Time end_time , Time &dt ,
  31. Observer observer , controlled_stepper_tag
  32. );
  33. /* basic version */
  34. template< class Stepper , class System , class State , class Time , class Observer>
  35. Time integrate_n_steps(
  36. Stepper stepper , System system , State &start_state ,
  37. Time start_time , Time dt , size_t num_of_steps ,
  38. Observer observer , stepper_tag )
  39. {
  40. // ToDo: is there a better way to extract the final time?
  41. Time t = start_time; // Assignment is only here to avoid warnings.
  42. boost::for_each( make_n_step_time_range( stepper , system , start_state ,
  43. start_time , dt , num_of_steps ) ,
  44. obs_caller_time< Observer , Time >( t , observer ) );
  45. return t;
  46. }
  47. /* controlled version */
  48. template< class Stepper , class System , class State , class Time , class Observer>
  49. Time integrate_n_steps(
  50. Stepper stepper , System system , State &start_state ,
  51. Time start_time , Time dt , size_t num_of_steps ,
  52. Observer observer , controlled_stepper_tag )
  53. {
  54. typename odeint::unwrap_reference< Observer >::type &obs = observer;
  55. Time time = start_time;
  56. Time time_step = dt;
  57. for( size_t step = 0; step < num_of_steps ; ++step )
  58. {
  59. obs( start_state , time );
  60. detail::integrate_adaptive( stepper , system , start_state , time , static_cast<Time>(time+time_step) , dt ,
  61. null_observer() , controlled_stepper_tag() );
  62. // direct computation of the time avoids error propagation happening when using time += dt
  63. // we need clumsy type analysis to get boost units working here
  64. time = start_time + static_cast< typename unit_value_type<Time>::type >(step+1) * time_step;
  65. }
  66. obs( start_state , time );
  67. return time;
  68. }
  69. /* dense output version */
  70. template< class Stepper , class System , class State , class Time , class Observer>
  71. Time integrate_n_steps(
  72. Stepper stepper , System system , State &start_state ,
  73. Time start_time , Time dt , size_t num_of_steps ,
  74. Observer observer , dense_output_stepper_tag )
  75. {
  76. // ToDo: is there a better way to extract the final time?
  77. Time t = start_time; // Assignment is only here to avoid warnings.
  78. boost::for_each( make_n_step_time_range( stepper , system , start_state ,
  79. start_time , dt , num_of_steps ) ,
  80. obs_caller_time< Observer , Time >( t , observer ) );
  81. return t;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. #endif /* BOOST_NUMERIC_ODEINT_INTEGRATE_DETAIL_INTEGRATE_N_STEPS_HPP_INCLUDED */