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

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;

Related

Eigen::VectorXd and Boost::Odeint, not working

I am testing with Eigen::VectorXd as state_type for boost::odeint. I use this code:
#include <Eigen/Eigen>
#include <boost/numeric/odeint.hpp>
#include <boost/numeric/odeint/external/eigen/eigen_algebra.hpp>
#include <iostream>
#include <vector>
template<class T>
struct push_back_state_and_time
{
std::vector<T>& m_states;
std::vector< double >& m_times;
push_back_state_and_time( std::vector<T> &states ,std::vector<double> &times )
: m_states(states) , m_times(times) { }
void operator()(const T &x ,double t )
{
m_states.push_back(x);
m_times.push_back(t);
}
};
template<class T>
struct write_state
{
void operator() ( const T &x, const double t ) {
std::cout << t << "\t";
for(size_t i = 0; i < x.size(); ++i)
std::cout << x[i] << "\t";
std::cout << std::endl;
};
};
template<class T>
class odeClass {
private:
double Ka, Kel, Vd;
public:
odeClass(double ka, double kel, double vd) : Ka(ka), Kel(kel), Vd(vd) {};
void operator() (const T &x, T &dxdt, const double t) {
dxdt[0] = - Ka * x[0];
dxdt[1] = Ka * x[0] - Kel * x[1];
};
};
void testODE_Eigen() {
double Ka = 0.195, Vd = 13.8, Kel = 0.79 / Vd;
Eigen::VectorXd x(2);
x << 40 / Vd, 0.0;
odeClass<Eigen::VectorXd> myClass(Ka, Kel, Vd);
boost::numeric::odeint::runge_kutta4<Eigen::VectorXd, double, Eigen::VectorXd, double, boost::numeric::odeint::vector_space_algebra> stepper;
size_t steps = integrate_adaptive( stepper, myClass, x ,0.0 ,100.0 ,0.5 ,write_state<Eigen::VectorXd>() );
}
void testODE_Vector() {
double Ka = 0.195, Vd = 13.8, Kel = 0.79 / Vd;
std::vector<double> x = { 40 / Vd, 0.0 };
odeClass<std::vector<double>> myClass(Ka, Kel, Vd);
boost::numeric::odeint::runge_kutta4<std::vector<double>> stepper;
size_t steps = integrate_adaptive( stepper, myClass, x ,0.0 ,100.0 ,0.5 ,write_state<std::vector<double>>() );
}
int main()
{
testODE_Eigen();
return 0;
}
When running the function testODE_Vector();, it works perfectly, but when runningtestODE_Eigen();` I get the first integration step, one assertion stop: "Assertion failed: index >= 0 && index < size(), file C:\Eigen-3.3.7\Eigen\src\Core\DenseCoeffsBase.h, line 408.", and a windows message saying that the application has stop working, with no clue, if a use Code::Blocks. If I run the same on Visual Studio 2017 console application, I get one error saying Cannot write on a memory address without printing anything.
Any clues?
Thank you.
It looks like you are failing an assertion inside the Eigen library you are using. With a message like Assertion failed: index >= 0 && index < size() the library is probably trying to iterate over a vector internally and checks that the vector is valid before trying to access it. I would check the objects you are passing in and make sure they are valid.
It looks like one of the main differences in the two function testODE_Vector() and testODE_Eigen() is the way that you create that x. I'm not sure what this line
x << 40 / Vd, 0.0; is intended to do, but I would start there and verify that the value of x is right before it's passed into integrate_adaptive
My answer is a little late but in case someone else runs into this issue, here's what I found.
The issue seems to be that OdeInt can't handle properly the dynamic size with Eigen vectors and matrices. Therefore when creating dxdt, it creates an empty dynamic matrix or vector. This leads to an issue in your operator overload, where you try to access an element of dxdt where it contains none.
A quick fix I found was to use the resize() function (or conservativeResize()) to make sure dxdt has the proper size:
void operator() (const T &x, T &dxdt, const double t) {
dxdt.resize(x.size())
dxdt[0] = - Ka * x[0];
dxdt[1] = Ka * x[0] - Kel * x[1];
};
Note that if you want to use matrices instead of vectors you will have to use x.rows() and x.cols() instead of x.size().

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

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.

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