• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [auto_generated]
3  libs/numeric/odeint/examples/bind_member_functions.hpp
4 
5  [begin_description]
6  tba.
7  [end_description]
8 
9  Copyright 2012 Karsten Ahnert
10  Copyright 2012 Mario Mulansky
11 
12  Distributed under the Boost Software License, Version 1.0.
13  (See accompanying file LICENSE_1_0.txt or
14  copy at http://www.boost.org/LICENSE_1_0.txt)
15  */
16 
17 #include <iostream>
18 
19 #include <boost/numeric/odeint.hpp>
20 
21 namespace odeint = boost::numeric::odeint;
22 
23 typedef boost::array< double , 3 > state_type;
24 
25 //[ ode_wrapper
26 template< class Obj , class Mem >
27 class ode_wrapper
28 {
29     Obj m_obj;
30     Mem m_mem;
31 
32 public:
33 
ode_wrapper(Obj obj,Mem mem)34     ode_wrapper( Obj obj , Mem mem ) : m_obj( obj ) , m_mem( mem ) { }
35 
36     template< class State , class Deriv , class Time >
operator ()(const State & x,Deriv & dxdt,Time t)37     void operator()( const State &x , Deriv &dxdt , Time t )
38     {
39         (m_obj.*m_mem)( x , dxdt , t );
40     }
41 };
42 
43 template< class Obj , class Mem >
make_ode_wrapper(Obj obj,Mem mem)44 ode_wrapper< Obj , Mem > make_ode_wrapper( Obj obj , Mem mem )
45 {
46     return ode_wrapper< Obj , Mem >( obj , mem );
47 }
48 //]
49 
50 
51 template< class Obj , class Mem >
52 class observer_wrapper
53 {
54     Obj m_obj;
55     Mem m_mem;
56 
57 public:
58 
observer_wrapper(Obj obj,Mem mem)59     observer_wrapper( Obj obj , Mem mem ) : m_obj( obj ) , m_mem( mem ) { }
60 
61     template< class State , class Time >
operator ()(const State & x,Time t)62     void operator()( const State &x , Time t )
63     {
64         (m_obj.*m_mem)( x , t );
65     }
66 };
67 
68 template< class Obj , class Mem >
make_observer_wrapper(Obj obj,Mem mem)69 observer_wrapper< Obj , Mem > make_observer_wrapper( Obj obj , Mem mem )
70 {
71     return observer_wrapper< Obj , Mem >( obj , mem );
72 }
73 
74 
75 
76 //[ bind_member_function
77 struct lorenz
78 {
odelorenz79     void ode( const state_type &x , state_type &dxdt , double t ) const
80     {
81         dxdt[0] = 10.0 * ( x[1] - x[0] );
82         dxdt[1] = 28.0 * x[0] - x[1] - x[0] * x[2];
83         dxdt[2] = -8.0 / 3.0 * x[2] + x[0] * x[1];
84     }
85 };
86 
main(int argc,char * argv[])87 int main( int argc , char *argv[] )
88 {
89     using namespace boost::numeric::odeint;
90     state_type x = {{ 10.0 , 10.0 , 10.0 }};
91     integrate_const( runge_kutta4< state_type >() , make_ode_wrapper( lorenz() , &lorenz::ode ) ,
92                      x , 0.0 , 10.0 , 0.01 );
93     return 0;
94 }
95 //]
96 
97 
98 /*
99 struct lorenz
100 {
101     void ode( const state_type &x , state_type &dxdt , double t ) const
102     {
103         dxdt[0] = 10.0 * ( x[1] - x[0] );
104         dxdt[1] = 28.0 * x[0] - x[1] - x[0] * x[2];
105         dxdt[2] = -8.0 / 3.0 * x[2] + x[0] * x[1];
106     }
107 
108     void obs( const state_type &x , double t ) const
109     {
110         std::cout << t << " " << x[0] << " " << x[1] << " " << x[2] << "\n";
111     }
112 };
113 
114 int main( int argc , char *argv[] )
115 {
116     using namespace boost::numeric::odeint;
117 
118     state_type x = {{ 10.0 , 10.0 , 10.0 }};
119     integrate_const( runge_kutta4< state_type >() ,
120                      make_ode_wrapper( lorenz() , &lorenz::ode ) ,
121                      x , 0.0 , 10.0 , 0.01 ,
122                      make_observer_wrapper( lorenz() , &lorenz::obs ) );
123 
124     return 0;
125 }
126 */
127