turning a simple c++ code into a class (runge kutta method) - c++

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).

Related

Boost giving strange result for ode integration

I ran the following code which solves a simple differential equation. The result seems to depend on tsteps. The result I get for tsteps = 100 is 9.688438503116524e-15, but for tsteps = 1000 the answer is 7.124585369895499e-218 much closer to the expected result.
#include <iostream>
#include <iomanip>
#include <boost/numeric/odeint.hpp> // odeint function definitions
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector< double> state_type;
int tsize = 1;
void my_observer( const state_type &x, const double t );
void initarrays(state_type &x)
{
x[0]=1.0e0;
}
void my_system( const state_type &x , state_type &dxdt , const double t )
{
dxdt[0]=-x[0];
}
void my_observer( const state_type &x, const double t )
{
std::cout<<t<<" ";
for(int i=0;i<tsize;i++)
{
std::cout<<x[i]<<" ";
}
std::cout<<std::endl;
}
int main()
{
std::cout.setf ( std::ios::scientific, std::ios::floatfield );
std::cout.precision(15);
int size=tsize;
state_type x0(size);
double err_abs = 1.0e-12;
double err_rel = 1.0e-12;
double a_x = 1.0;
double a_dxdt = 1.0;
initarrays(x0);
double t0 = 0.0e0;
int tsteps = 1000;
double t1 = 500.0e0;
double dt = (t1-t0)/((double)tsteps);
typedef runge_kutta_fehlberg78< state_type > solver;
typedef controlled_runge_kutta< solver > controller;
my_observer(x0,t0);
for(int ts=0;ts<tsteps;ts++)
{
integrate_adaptive( make_controlled( err_abs , err_rel , solver() ), my_system, x0 , t0+ts*dt , t0+(1+ts)*dt , dt);
my_observer(x0,t0+(1+ts)*dt);
}
}

ofstream Odeint output to txt file

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.

ODEINT output to txt file instead of console

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);
}

odeint complex state type example does not compile

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.

How to perform simple numeric integration with odeint in C++

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.