pid_step_adjuster.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. boost/numeric/odeint/stepper/detail/pid_step_adjuster.hpp
  3. [begin_description]
  4. Implementation of the stepsize controller for the controlled adams bashforth moulton stepper.
  5. [end_description]
  6. Copyright 2017 Valentin Noah Hartmann
  7. Distributed under the Boost Software License, Version 1.0.
  8. (See accompanying file LICENSE_1_0.txt or
  9. copy at http://www.boost.org/LICENSE_1_0.txt)
  10. */
  11. #ifndef BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_HPP_INCLUDED
  12. #define BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_HPP_INCLUDED
  13. #include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
  14. #include <boost/numeric/odeint/stepper/detail/pid_step_adjuster_coefficients.hpp>
  15. #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
  16. #include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
  17. #include <math.h>
  18. namespace boost {
  19. namespace numeric {
  20. namespace odeint {
  21. namespace detail {
  22. template<
  23. class Value = double,
  24. class Time = double
  25. >
  26. struct pid_op
  27. {
  28. public:
  29. typedef Value value_type;
  30. typedef Time time_type;
  31. const double beta1;
  32. const double beta2;
  33. const double beta3;
  34. const double alpha1;
  35. const double alpha2;
  36. const time_type dt1;
  37. const time_type dt2;
  38. const time_type dt3;
  39. const size_t m_steps;
  40. pid_op(const size_t steps, const double _dt1, const double _dt2, const double _dt3,
  41. const double b1 = 1, const double b2 = 0, const double b3 = 0, const double a1 = 0, const double a2 = 0)
  42. :beta1(b1), beta2(b2), beta3(b3), alpha1(a1), alpha2(a2),
  43. dt1(_dt1), dt2(_dt2), dt3(_dt3),
  44. m_steps(steps)
  45. {};
  46. template<class T1, class T2, class T3, class T4>
  47. void operator()(T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
  48. {
  49. using std::abs;
  50. t1 = adapted_pow(abs(t2), -beta1/(m_steps + 1)) *
  51. adapted_pow(abs(t3), -beta2/(m_steps + 1)) *
  52. adapted_pow(abs(t4), -beta3/(m_steps + 1)) *
  53. adapted_pow(abs(dt1/dt2), -alpha1/(m_steps + 1))*
  54. adapted_pow(abs(dt2/dt3), -alpha2/(m_steps + 1));
  55. t1 = 1/t1;
  56. };
  57. template<class T1, class T2>
  58. void operator()(T1 &t1, const T2 &t2)
  59. {
  60. using std::abs;
  61. t1 = adapted_pow(abs(t2), -beta1/(m_steps + 1));
  62. t1 = 1/t1;
  63. };
  64. private:
  65. template<class T>
  66. inline value_type adapted_pow(T base, double exp)
  67. {
  68. if(exp == 0)
  69. {
  70. return 1;
  71. }
  72. else if (exp > 0)
  73. {
  74. return pow(base, exp);
  75. }
  76. else
  77. {
  78. return 1/pow(base, -exp);
  79. }
  80. };
  81. };
  82. template<
  83. class State,
  84. class Value = double,
  85. class Deriv = State,
  86. class Time = double,
  87. class Algebra = typename algebra_dispatcher< State >::algebra_type,
  88. class Operations = typename operations_dispatcher< Deriv >::operations_type,
  89. size_t Type = BASIC
  90. >
  91. struct pid_step_adjuster
  92. {
  93. public:
  94. static double threshold() { return 0.9; };
  95. typedef State state_type;
  96. typedef Value value_type;
  97. typedef Deriv deriv_type;
  98. typedef Time time_type;
  99. typedef Algebra algebra_type;
  100. typedef Operations operations_type;
  101. typedef rotating_buffer<state_type, 3> error_storage_type;
  102. typedef rotating_buffer<time_type, 3> time_storage_type;
  103. typedef pid_step_adjuster_coefficients<Type> coeff_type;
  104. pid_step_adjuster(double abs_tol = 1e-6, double rel_tol = 1e-6, time_type dtmax = 1.0)
  105. :m_dtmax(dtmax), m_error_storage(), m_dt_storage(), m_init(0),
  106. m_abs_tol(abs_tol), m_rel_tol(rel_tol)
  107. {};
  108. time_type adjust_stepsize(const size_t steps, time_type dt, state_type &err, const state_type &x, const deriv_type &dxdt)
  109. {
  110. using std::abs;
  111. m_algebra.for_each3( err , x , dxdt ,
  112. typename operations_type::template rel_error< value_type >( m_abs_tol , m_rel_tol , 1.0 , 1.0 * abs(get_unit_value( dt )) ) );
  113. m_error_storage[0] = err;
  114. m_dt_storage[0] = dt;
  115. if(m_init >= 2)
  116. {
  117. m_algebra.for_each4(err, m_error_storage[0], m_error_storage[1], m_error_storage[2],
  118. pid_op<>(steps, m_dt_storage[0], m_dt_storage[1], m_dt_storage[2],
  119. m_coeff[0], m_coeff[1], m_coeff[2], m_coeff[3], m_coeff[4]));
  120. }
  121. else
  122. {
  123. m_algebra.for_each2(err, m_error_storage[0],
  124. pid_op<>(steps, m_dt_storage[0], m_dt_storage[1], m_dt_storage[2], 0.7));
  125. }
  126. value_type ratio = 1 / m_algebra.norm_inf(err);
  127. value_type kappa = 1.0;
  128. ratio = 1.0 + kappa*atan((ratio - 1) / kappa);
  129. if(ratio*dt >= m_dtmax)
  130. {
  131. ratio = m_dtmax / dt;
  132. }
  133. if(ratio >= threshold())
  134. {
  135. m_error_storage.rotate();
  136. m_dt_storage.rotate();
  137. ++m_init;
  138. }
  139. else
  140. {
  141. m_init = 0;
  142. }
  143. return dt * static_cast<time_type>(ratio);
  144. };
  145. private:
  146. algebra_type m_algebra;
  147. time_type m_dtmax;
  148. error_storage_type m_error_storage;
  149. time_storage_type m_dt_storage;
  150. size_t m_init;
  151. double m_abs_tol;
  152. double m_rel_tol;
  153. coeff_type m_coeff;
  154. };
  155. } // detail
  156. } // odeint
  157. } // numeric
  158. } // boost
  159. #endif