bind_member_functions.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/examples/bind_member_functions.hpp
  4. [begin_description]
  5. tba.
  6. [end_description]
  7. Copyright 2012 Karsten Ahnert
  8. Copyright 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. #include <iostream>
  14. #include <boost/numeric/odeint.hpp>
  15. namespace odeint = boost::numeric::odeint;
  16. typedef boost::array< double , 3 > state_type;
  17. //[ ode_wrapper
  18. template< class Obj , class Mem >
  19. class ode_wrapper
  20. {
  21. Obj m_obj;
  22. Mem m_mem;
  23. public:
  24. ode_wrapper( Obj obj , Mem mem ) : m_obj( obj ) , m_mem( mem ) { }
  25. template< class State , class Deriv , class Time >
  26. void operator()( const State &x , Deriv &dxdt , Time t )
  27. {
  28. (m_obj.*m_mem)( x , dxdt , t );
  29. }
  30. };
  31. template< class Obj , class Mem >
  32. ode_wrapper< Obj , Mem > make_ode_wrapper( Obj obj , Mem mem )
  33. {
  34. return ode_wrapper< Obj , Mem >( obj , mem );
  35. }
  36. //]
  37. template< class Obj , class Mem >
  38. class observer_wrapper
  39. {
  40. Obj m_obj;
  41. Mem m_mem;
  42. public:
  43. observer_wrapper( Obj obj , Mem mem ) : m_obj( obj ) , m_mem( mem ) { }
  44. template< class State , class Time >
  45. void operator()( const State &x , Time t )
  46. {
  47. (m_obj.*m_mem)( x , t );
  48. }
  49. };
  50. template< class Obj , class Mem >
  51. observer_wrapper< Obj , Mem > make_observer_wrapper( Obj obj , Mem mem )
  52. {
  53. return observer_wrapper< Obj , Mem >( obj , mem );
  54. }
  55. //[ bind_member_function
  56. struct lorenz
  57. {
  58. void ode( const state_type &x , state_type &dxdt , double t ) const
  59. {
  60. dxdt[0] = 10.0 * ( x[1] - x[0] );
  61. dxdt[1] = 28.0 * x[0] - x[1] - x[0] * x[2];
  62. dxdt[2] = -8.0 / 3.0 * x[2] + x[0] * x[1];
  63. }
  64. };
  65. int main( int argc , char *argv[] )
  66. {
  67. using namespace boost::numeric::odeint;
  68. state_type x = {{ 10.0 , 10.0 , 10.0 }};
  69. integrate_const( runge_kutta4< state_type >() , make_ode_wrapper( lorenz() , &lorenz::ode ) ,
  70. x , 0.0 , 10.0 , 0.01 );
  71. return 0;
  72. }
  73. //]
  74. /*
  75. struct lorenz
  76. {
  77. void ode( const state_type &x , state_type &dxdt , double t ) const
  78. {
  79. dxdt[0] = 10.0 * ( x[1] - x[0] );
  80. dxdt[1] = 28.0 * x[0] - x[1] - x[0] * x[2];
  81. dxdt[2] = -8.0 / 3.0 * x[2] + x[0] * x[1];
  82. }
  83. void obs( const state_type &x , double t ) const
  84. {
  85. std::cout << t << " " << x[0] << " " << x[1] << " " << x[2] << "\n";
  86. }
  87. };
  88. int main( int argc , char *argv[] )
  89. {
  90. using namespace boost::numeric::odeint;
  91. state_type x = {{ 10.0 , 10.0 , 10.0 }};
  92. integrate_const( runge_kutta4< state_type >() ,
  93. make_ode_wrapper( lorenz() , &lorenz::ode ) ,
  94. x , 0.0 , 10.0 , 0.01 ,
  95. make_observer_wrapper( lorenz() , &lorenz::obs ) );
  96. return 0;
  97. }
  98. */