max_step_checker.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/integrate/max_step_checker.hpp
  4. [begin_description]
  5. Throws exception if too many steps are performed.
  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. #ifndef BOOST_NUMERIC_ODEINT_INTEGRATE_MAX_STEP_CHECKER_HPP_INCLUDED
  13. #define BOOST_NUMERIC_ODEINT_INTEGRATE_MAX_STEP_CHECKER_HPP_INCLUDED
  14. #include <stdexcept>
  15. #include <cstdio>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/numeric/odeint/util/odeint_error.hpp>
  18. namespace boost {
  19. namespace numeric {
  20. namespace odeint {
  21. /**
  22. * \brief A class for performing overflow checks on the step count in integrate functions.
  23. *
  24. * Provide an instance of this class to integrate functions if you want to throw a runtime error if
  25. * too many steps are performed without progress during the integrate routine.
  26. */
  27. class max_step_checker
  28. {
  29. public:
  30. protected:
  31. const int m_max_steps;
  32. int m_steps;
  33. public:
  34. /**
  35. * \brief Construct the max_step_checker.
  36. * max_steps is the maximal number of iterations allowed before runtime_error is thrown.
  37. */
  38. max_step_checker(const int max_steps = 500)
  39. : m_max_steps(max_steps)
  40. {
  41. reset();
  42. }
  43. /**
  44. * \brief Resets the max_step_checker by setting the internal counter to 0.
  45. */
  46. void reset()
  47. {
  48. m_steps = 0;
  49. }
  50. /**
  51. * \brief Increases the counter and performs the iteration check
  52. */
  53. void operator()(void)
  54. {
  55. if( m_steps++ >= m_max_steps )
  56. {
  57. char error_msg[200];
  58. std::sprintf(error_msg, "Max number of iterations exceeded (%d).", m_max_steps);
  59. BOOST_THROW_EXCEPTION( no_progress_error(error_msg) );
  60. }
  61. }
  62. };
  63. /**
  64. * \brief A class for performing overflow checks on the failed step count in step size adjustments.
  65. *
  66. * Used internally within the dense output stepper and integrate routines.
  67. */
  68. class failed_step_checker : public max_step_checker
  69. {
  70. public:
  71. /**
  72. * \brief Construct the failed_step_checker.
  73. * max_steps is the maximal number of iterations allowed before runtime_error is thrown.
  74. */
  75. failed_step_checker(const int max_steps = 500)
  76. : max_step_checker(max_steps)
  77. {}
  78. /**
  79. * \brief Increases the counter and performs the iteration check
  80. */
  81. void operator()(void)
  82. {
  83. if( m_steps++ >= m_max_steps )
  84. {
  85. char error_msg[200];
  86. std::sprintf(error_msg, "Max number of iterations exceeded (%d). A new step size was not found.", m_max_steps);
  87. BOOST_THROW_EXCEPTION( step_adjustment_error(error_msg) );
  88. }
  89. }
  90. };
  91. } // namespace odeint
  92. } // namespace numeric
  93. } // namespace boost
  94. #endif