Binding member functions to a function objects suitable for odeint system
function is not easy, at least in C++03. The usual way of using __boost_bind
does not work because of the forwarding problem. odeint provides two do_step
method which only differ in the
const specifiers of the arguments and __boost_bind binders only provide the
specializations up to two argument which is not enough for odeint.
But one can easily implement the according binders themself:
template< class Obj , class Mem > class ode_wrapper { Obj m_obj; Mem m_mem; public: ode_wrapper( Obj obj , Mem mem ) : m_obj( obj ) , m_mem( mem ) { } template< class State , class Deriv , class Time > void operator()( const State &x , Deriv &dxdt , Time t ) { (m_obj.*m_mem)( x , dxdt , t ); } }; template< class Obj , class Mem > ode_wrapper< Obj , Mem > make_ode_wrapper( Obj obj , Mem mem ) { return ode_wrapper< Obj , Mem >( obj , mem ); }
One can use this binder as follows
struct lorenz { void ode( const state_type &x , state_type &dxdt , double t ) const { dxdt[0] = 10.0 * ( x[1] - x[0] ); dxdt[1] = 28.0 * x[0] - x[1] - x[0] * x[2]; dxdt[2] = -8.0 / 3.0 * x[2] + x[0] * x[1]; } }; int main( int argc , char *argv[] ) { using namespace boost::numeric::odeint; state_type x = {{ 10.0 , 10.0 , 10.0 }}; integrate_const( runge_kutta4< state_type >() , make_ode_wrapper( lorenz() , &lorenz::ode ) , x , 0.0 , 10.0 , 0.01 ); return 0; }
In C++11 one can use std::bind
and one does not need to implement the bind themself:
namespace pl = std::placeholders; state_type x = {{ 10.0 , 10.0 , 10.0 }}; integrate_const( runge_kutta4< state_type >() , std::bind( &lorenz::ode , lorenz() , pl::_1 , pl::_2 , pl::_3 ) , x , 0.0 , 10.0 , 0.01 );