Can you provide me with a simple example of performing a numeric integration with odeint in C++?
I would like to use the convenient integrate function, documented as:
integrate( system , x0 , t0 , t1 , dt )
Also I'm not sure, how to pass it instead of a function or a functor, a class method, if that's possible.
In C++11 you can use a simple lambda function wrapping the call to your member method
Class c;
auto f = [&c]( const state_type & x , state_type &dxdt , double t ) {
c.system_func( x , dxdt , t ); };
integrate( f , x0 , t0 , t1 , dt );
std::bind might also work, but then you have to take care if values are passed by reference of by value.
In C++03 you need to write a simple wrapper around your class method
struct wrapper
{
Class &c;
wrapper( Class &c_ ) : c( c_ ) { }
template< typename State , typename Time >
void operator()( State const &x , State &dxdt , Time t ) const
{
c.system_func( x , dxdt , t );
}
};
// ...
integrate( wrapper( c ) , x0 , t0 , t1 , dt );
(Boost.Bind will not work correctly with more then two arguments).
You mean examples in addition to the ones provided online?
#include <iostream>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
const double sigma = 10.0;
const double R = 28.0;
const double b = 8.0 / 3.0;
typedef boost::array< double , 3 > state_type;
void lorenz( const state_type &x , state_type &dxdt , double t )
{
dxdt[0] = sigma * ( x[1] - x[0] );
dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
dxdt[2] = -b * x[2] + x[0] * x[1];
}
void write_lorenz( const state_type &x , const double t )
{
cout << t << '\t' << x[0] << '\t' << x[1] << '\t' << x[2] << endl;
}
int main(int argc, char **argv)
{
state_type x = { 10.0 , 1.0 , 1.0 }; // initial conditions
integrate( lorenz , x , 0.0 , 25.0 , 0.1 , write_lorenz );
}
And regarding the system, you can provide anything where the following expression is valid:
sys( x , dxdt , t ) // returning void
Check the user's guide (and more examples) online.
Related
I try to run this example from the ODEINT library to solve ODE. It just runs fine, but instead of cout the results to screen, I want to write them to a file. I add this ofstream to the code under write_cout function but it only writes the last line of result to the file and not all.
Do you have any idea about this? Thanks
#include <iostream>
#include <boost/numeric/odeint.hpp>
#include <fstream>
using namespace std;
using namespace boost::numeric::odeint;
void rhs( const double x , double &dxdt , const double t )
{
dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}
void write_cout( const double &x , const double t )
{
cout << t << '\t' << x << endl;
cout<<"alo"<<endl;
ofstream buckyFile ("tuna.txt");
buckyFile<<t <<'\t'<<x<<endl;
}
// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;
int main()
{
double x = 0.0;
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
}
Or even better
struct stream_writer
{
std::ostream& m_out;
stream_writer( std::ostream& out ) : m_out( out ) {}
void operator()( const double &x , const double t )
{
m_out << t << "\t" << x << "\n";
}
};
int main()
{
double x = 0.0;
ofstream fout( "tuna.txt" );
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , stream_writer( fout ) );
}
ofstream buckyFile ("tuna.txt");
opens a new file tuna.txt each time the function is entered, overriding what ever was there before.
A quick fix would be to use a
static ofstream buckyFile ("tuna.txt");
instead.
Is there a way to write the outputs of t and x of this example to a txt file instead of the console. Thanks!
This is the example I copied from Odeint website.
#include <iostream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/
void rhs( const double x , double &dxdt , const double t )
{
dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}
void write_cout( const double &x , const double t )
{
cout << t << '\t' << x << endl;
}
// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;
int main()
{
double x = 0.0;
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
}
you can simply pipe the output of this example into a text file
$ ./lorenz > data.txt
Otherwise you can use a C++ ofstreams to write the output directly into a file, e.g. described there: http://www.cplusplus.com/doc/tutorial/files/
just replace cout with object of ofstream.
#include <iostream>
#include <fstream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
ofstream data("data.txt");
/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/
void rhs(const double x, double &dxdt, const double t)
{
dxdt = 3.0 / (2.0*t*t) + x / (2.0*t);
}
void write_cout(const double &x, const double t)
{
data << t << '\t' << x << endl;
}
// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;
int main()
{
double x = 0.0;
integrate_adaptive(make_controlled(1E-12, 1E-12, stepper_type()),
rhs, x, 1.0, 10.0, 0.1, write_cout);
}
I have been extending the simple Runge Kutta example (example 2, 1D ODE) from ODEINT .
The code itself works without trouble.
But then I tried to put everything into a class called runge_kutta and now the recursive inegration is not working anymore, the orginal line is
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
I thought that just adding "runge_kutta::"
return integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , runge_kutta::rhs, x , 1.0 , 10.0 , timestep , runge_kutta::write_cout );
would fix the issue , but
the error message is
error: no matching function for call to 'integrate_adaptive(boost::numeric::odeint::result_of::make_controlled<boost::numeric::odeint::runge_kutta_dopri5<double> >::type, <unresolved overloaded function type>, double&, double, double, double&, <unresolved overloaded function type>)'
The complete class is defined as follows:
rk.h
#ifndef RK_H_
#define RK_H_
#include <iostream>
#include <cmath>
#include <odeint.hpp>
using namespace boost::numeric::odeint;
class runge_kutta{
public:
runge_kutta(double beta1, double gamma1);
void setparameter(double dist1,double grad);
double solve(double x,double t);
int main();
void write_cout( const double &x , const double t );
private:
void rhs( const double x , double &dxdt , const double t );
double smoothen(double x);
double beta, gamma;
double distance, gradient;
typedef runge_kutta_dopri5< double > stepper_type;
};
#endif /* RK_H_ */
rk.cpp:
#include "rk.h"
double beta, gamma;
double dist, gradient;
void runge_kutta::rhs( const double x , double &dxdt , const double t )
{
dxdt = smoothen(x)*dist*abs(gradient);
}
void runge_kutta::write_cout( const double &x , const double t )
{
std::cout << t << '\t' << x << std::endl;
}
// state_type = double
runge_kutta::runge_kutta(double beta1, double gamma1){
beta = beta1;
gamma = gamma1;
}
void runge_kutta::setparameter(double dist1,double grad){
dist = dist1;
gradient = grad;
}
double runge_kutta::solve(double x,double t){
beta = 0.4;
gamma = 0.8;
typedef runge_kutta_dopri5< double > stepper_type;
return integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , runge_kutta::rhs, x , 1.0 , 10.0 , timestep , runge_kutta::write_cout );
}
double runge_kutta::smoothen(double phix){
if (abs(phix)<= beta)
return 1;
else if (abs(phix)> gamma)
return 0;
else
return ((abs(phix)-gamma)*(abs(phix)-gamma)*(2*abs(phix)+gamma-3*beta)/((gamma-beta)*(gamma-beta)*(gamma-beta)));
}
You've replaced functions like write_cout with member functions, but they are not the same thing. You can't just use free functions and member functions interchangeably.
Also, to take the address of a member function you must use &runge_kutta::write_cout not just runge_kutta::write_cout (but that probably won't fix the error, for the reason above).
I try to run [odeint complex state type example code in boost_1_55_0 on Mac OS X 10.9.2 g++ 5.1.
The code below is the copy on the website which solves the Stuart-Landau oscillator
#include <iostream>
#include <complex>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
//[ stuart_landau_system_function
typedef complex< double > state_type;
struct stuart_landau
{
double m_eta;
double m_alpha;
stuart_landau( double eta = 1.0 , double alpha = 1.0 )
: m_eta( eta ) , m_alpha( alpha ) { }
void operator()( const state_type &x , state_type &dxdt , double t ) const
{
const complex< double > I( 0.0 , 1.0 );
dxdt = ( 1.0 + m_eta * I ) * x - ( 1.0 + m_alpha * I ) * norm( x ) * x;
}
};
//]
/*
//[ stuart_landau_system_function_alternative
double eta = 1.0;
double alpha = 1.0;
void stuart_landau( const state_type &x , state_type &dxdt , double t )
{
const complex< double > I( 0.0 , 1.0 );
dxdt = ( 1.0 + m_eta * I ) * x - ( 1.0 + m_alpha * I ) * norm( x ) * x;
}
//]
*/
struct streaming_observer
{
std::ostream& m_out;
streaming_observer( std::ostream &out ) : m_out( out ) { }
template< class State >
void operator()( const State &x , double t ) const
{
m_out << t;
m_out << "\t" << x.real() << "\t" << x.imag() ;
m_out << "\n";
}
};
int main( int argc , char **argv )
{
//[ stuart_landau_integration
state_type x = complex< double >( 1.0 , 0.0 );
const double dt = 0.1;
typedef runge_kutta4< state_type > stepper_type;
integrate_const( stepper_type() , stuart_landau( 2.0 , 1.0 ) , x , 0.0 , 10.0 , dt , streaming_observer( cout ) );
//]
return 0;
}
The sample code above doesn't compile. 9 errors generates and ends with:
'boost::numeric::odeint::explicit_stepper_base<boost::numeric::odeint::explicit_generic_rk<4, 4, std::__1::complex<double>, double, std::__1::complex<double>, double,
boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, 4, std::__1::complex<double>, double, std::__1::complex<double>,
double, boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>::do_step_v1<stuart_landau, std::__1::complex<double> >'
requested here
do_step_v1( system , x , t , dt );
^
/usr/include/boost/numeric/odeint/integrate/detail/integrate_adaptive.hpp:62:17: note: in instantiation of function template specialization
'boost::numeric::odeint::explicit_stepper_base<boost::numeric::odeint::explicit_generic_rk<4, 4, std::__1::complex<double>, double, std::__1::complex<double>, double,
boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, 4, std::__1::complex<double>, double, std::__1::complex<double>,
double, boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>::do_step<stuart_landau, std::__1::complex<double> >' requested
here
stepper.do_step( system , start_state , end , end_time - end );
^
/usr/include/boost/numeric/odeint/integrate/integrate_const.hpp:50:24: note: in instantiation of function template specialization
'boost::numeric::odeint::detail::integrate_adaptive<boost::numeric::odeint::runge_kutta4<std::__1::complex<double>, double, std::__1::complex<double>, double, boost::numeric::odeint::range_algebra,
boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, stuart_landau, std::__1::complex<double>, double, streaming_observer>' requested here
return detail::integrate_adaptive(
^
main.cpp:84:5: note: in instantiation of function template specialization 'boost::numeric::odeint::integrate_const<boost::numeric::odeint::runge_kutta4<std::__1::complex<double>, double,
std::__1::complex<double>, double, boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, stuart_landau,
std::__1::complex<double>, double, streaming_observer>' requested here
integrate_const( stepper_type() , stuart_landau( 2.0 , 1.0 ) , x , 0.0 , 10.0 , dt , streaming_observer( cout ) );
^
What is the bug?
You use a feature which only exist in the github version of odeint. Replace the typedef for the stepper with
typedef runge_kutta4< state_type , double ,
state_type , double ,
vector_space_algebra > stepper_type;
Automatic algebra detection currently works only in the github version of odeint. We try to get this feature into the next official boost version.
I intend to use the Boost odeint library in an MCMC routine to estimate parameters in an ODE model. Since these ODEs may be stiff, I need to be able to pass the jacobian into the solver with the derivative. I would like to make a class which has the parameters and initial values as private members and then the derivative, jacobian, and methods to change the parameters as public methods. I tried to modify the stiff example from the odeint website to use one class containing both, but am receiving the error 'error: no matching function for call to 'Fitzhugh::deriv()' when comiling. I am not an experienced C++ programmer, so this is likely a conceptual mistake. Here is the code.
/* Fitzhugh Nagumo Equation in odeint */
#include <iostream>
#include <fstream>
#include <utility>
#include <cmath>
#include <boost/numeric/odeint.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
using namespace std;
using namespace boost::numeric::odeint;
namespace phoenix = boost::phoenix;
//[ stiff_system_definition
typedef boost::numeric::ublas::vector< double > vector_type;
typedef boost::numeric::ublas::matrix< double > matrix_type;
class Fitzhugh
{
private:
double a;
double b;
double c;
public:
Fitzhugh( double a_, double b_, double c_ ) : a(a_), b(b_), c(c_) { }
void deriv ( const vector_type &x , vector_type &dxdt , double )
{
dxdt[ 0 ] = c*(x[0] - pow(x[0], 3.0)/3.0 + x[1]);
dxdt[ 1 ] = -(x[0] - a + b*x[1])/c;
}
void jac ( const vector_type &x, matrix_type &J , const double & , vector_type &dfdt )
{
J( 0 , 0 ) = c*(1 - pow(x[0], 2.0));
J( 0 , 1 ) = c;
J( 1 , 0 ) = -1/c;
J( 1 , 1 ) = -b/c;
dfdt[0] = 0.0;
dfdt[1] = 0.0;
}
};
int main( int argc , char **argv )
{
// typedef rosenbrock4< double > stepper_type;
// typedef rosenbrock4_controller< stepper_type > controlled_stepper_type;
// typedef rosenbrock4_dense_output< controlled_stepper_type > dense_output_type;
//[ integrate_stiff_system
vector_type x( 2 , 1.0 );
Fitzhugh fitzhugh(0.2, 0.2, 3.0);
size_t num_of_steps = integrate_const( make_dense_output< rosenbrock4< double > >( 1.0e-6 , 1.0e-6 ) ,
make_pair( fitzhugh.deriv() , fitzhugh.jac() ) ,
x , 0.0 , 50.0 , 0.01 ,
cout << phoenix::arg_names::arg2 << " " << phoenix::arg_names::arg1[0] << "\n" );
//]
clog << num_of_steps << endl;
return 0;
}
Here is the full output
/home/chris/C++/examples/fitzhugh/main.cpp: In function ‘int main(int, char**)’:
/home/chris/C++/examples/fitzhugh/main.cpp:60:39: error: no matching function for call to ‘Fitzhugh::deriv()’
/home/chris/C++/examples/fitzhugh/main.cpp:60:39: note: candidate is:
/home/chris/C++/examples/fitzhugh/main.cpp:30:14: note: void Fitzhugh::deriv(const vector_type&, vector_type&, double)
/home/chris/C++/examples/fitzhugh/main.cpp:30:14: note: candidate expects 3 arguments, 0 provided
/home/chris/C++/examples/fitzhugh/main.cpp:60:56: error: no matching function for call to ‘Fitzhugh::jac()’
/home/chris/C++/examples/fitzhugh/main.cpp:60:56: note: candidate is:
/home/chris/C++/examples/fitzhugh/main.cpp:36:14: note: void Fitzhugh::jac(const vector_type&, matrix_type&, const double&, vector_type&)
/home/chris/C++/examples/fitzhugh/main.cpp:36:14: note: candidate expects 4 arguments, 0 provided
to pass member function you need to use a bind mechanism. if you have a c++11 compiler you can use std::bind; include the std::placeholders namespace: using namespace std::placeholders; then use std::bind from <functional>:
make_pair( bind( &Fitzhugh::deriv , &fitzhugh , _1 , _2 , _3 ) ,
bind( &Fitzhugh::jac , &fitzhugh , _1 , _2 , _3, _4 ) )