ofstream Odeint output to txt file - c++

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.

Related

Using dopri5 with odeint boost library

system of equations
Hi. I want to evolve those equations in time from zero to 10^16 and initial condotions x(0)=10^8 and y(0)= 0.5. Because of the dependence of the equations on x in the denominator I think using odeint with runge_kutta_dopri5 is a good choice because of the adaptive step control. The thing is I have little idea how to do this in practice cause i have little experience in c++ and odeint. I searched a lot about using odeint but the examples where not helpful for me. Also i want to stop the calculations when x reaches zero i saw this https://stackoverflow.com/questions/33334073/stop-integration-in-odeint-with-stiff-ode
based on examples i wrote this so far with no luck
#include <iostream>
#include <vector>
#include <cmath>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
const double b = 43.0e17;
typedef boost::array< double , 2 > state_type;
void binary(const state_type &x , state_type &dxdt , double t )
{
dxdt[0] = -b*(64.0/5)*(1 + (73.0/24)*pow(x[1],2)
+ 37.0/96)*pow(x[1],4) )/pow(x[0],3)*pow(1-pow(x[1],2),7.0/2);
dxdt[1] = -b*(304.0/96)*x[1]*(1 + (121.0/304)*pow(x[1],2))
/pow(x[0],4)*pow((1 - pow(x[1],2)),5.0/2);
}
void write_binary( const state_type &x , const double t )
{
cout << t << '\t' << x[0] << '\t' << x[1] << '\t' << x[2] << endl;
}
//I dont know what this does but the examples used it
struct streaming_observer
{
std::ostream& m_out;
streaming_observer( std::ostream &out ) : m_out( out ) { }
template< class State , class Time >
void operator()( const State &x , Time t ) const
{
m_out << t;
for( size_t i=0 ; i<x.size() ; ++i ) m_out << "\t" << x[i] ;
m_out << "\n";
}
};
//This was a first try with a given stepper but i want to replace it
int main(int argc, char **argv)
{
state_type x = { 20.871e8 , 0.5 }; // initial conditions
integrate( binary , x , 0.0 , 1000.0 , 0.1 , write_binary );
}
When I compiled it a run it I got this error
Internal Program Error - assertion (i < N) failed in const T& boost::array::operator[](boost::array::size_type) const [with T = double; long unsigned int N = 2ul; boost::array::const_reference = const double&; boost::array::size_type = long unsigned int]:
/usr/include/boost/array.hpp(129): out of range
Aborted (core dumped)
How can i get this work?
the write_binary function writes over the array bounds and causes the assertion. x[2] is not valid.

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

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

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

Simple 2d system (using array) with odeint does not compile

I'm running g++ 4.7 on Mint 12 with boost 1.55. I'm trying to solve a simple 2d system of ode with odeint -- following the 1d example here: 1d. The 1d example compiles alright in both the original version and the amended version from the answer. Now, if I want a 2d system and I use double[2] things do not work:
#include <iostream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
void rhs( const double *x, double *dxdt, const double t )
{
dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( double *x, const double t )
{
cout << t << '\t' << x[0] << '\t' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< double[2] > stepper_type;
int main()
{
double x[2] = {0.0,0.0};
integrate_adaptive( make_controlled( 1E-12, 1E-12, stepper_type() ), rhs, x, 1.0, 10.0, 0.1, write_cout );
}
The error message is a mess, but ends with:
/usr/include/boost/numeric/odeint/algebra/range_algebra.hpp:129:47: error: function returning an array
Is the array double[2] the problem? And how should I fix it? Perhaps using a vector? By the way, I tried using both
typedef runge_kutta_cash_karp54< double > stepper_type;
typedef runge_kutta_cash_karp54< double , double , double , double , vector_space_algebra > stepper_type;
as suggested in the 1d answer, but to no avail. I should mention also that on an older machine with older boost (don't remember which version) everything compiled without problems. Thanks for any suggestion!
Use std::array< double ,2 >
#include <array>
typedef std::array< double , 2 > state_type;
void rhs( state_type const &x, state_type &dxdt, const double t )
{
dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( state_type const& x, const double t )
{
cout << t << '\t' << x[0] << '\t' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< state_type > stepper_type;

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.