Anthony Hauser
Anthony Hauser

Reputation: 719

How to use C++ ODE solver with Rcpp in R?

To assess the difference in speed to solve ODEs between R and C++, I build the following ODE system in R:

modelsir_cpp =function(t,x){
S = x[1]
I1 = x[2]
I2 = x[3]
N=S+I1+I2
with(as.list(parm), {
   dS=B*I1-mu*S-beta*(S*(I1+I2)/N)
   dI1=beta*(S*(I1+I2)/N)-B*I1-lambda12*I1
   dI2=lambda12*I1
   res=c(dS,dI1,dI2)
   return(res)
 })
}

To solve it, I used the deSolve package.

times = seq(0, 10, by = 1/52)
parm=c(B=0.01,mu=0.008,beta=10,lambda12=1)
xstart=c(S=900,I1=100,I2=0)
out = as.data.frame(lsoda(xstart, times, modelsir, parm))

This works. I tried to solve the same system with C++ solver, by using Rcpp library in R. Here is what I add:

#include <Rcpp.h>
#include <boost/array.hpp>

// include Boost's odeint
#include <boost/numeric/odeint.hpp>

// [[Rcpp::depends(BH)]]

using namespace Rcpp;
using namespace std;
using namespace boost::numeric::odeint;

typedef boost::array< double ,3 > state_type;

// [[Rcpp::export]]
 Rcpp::NumericVector my_fun2(const Rcpp::NumericVector &x, const double t){
 Function f("modelsir_cpp");
    return f(_["t"]=t,_["x"]=x);
}
    void eqsir(const state_type &x, state_type &dxdt, const double t){
      Rcpp::NumericVector nvec=boost_array_to_nvec(x);
      Rcpp::NumericVector nvec2(3);
      nvec2=my_fun2(nvec,t);
      dxdt=nvec_to_boost_array(nvec2);
        }


void write_cout_2( const state_type &x , const double t ) {
  // use Rcpp's stream
  Rcpp::Rcout << t << '\t' << x[0] << '\t' << x[1] << '\t' << x[2] <<  endl;
}
typedef runge_kutta_dopri5< state_type > stepper_type;

// [[Rcpp::export]]
bool my_fun10_solver() {
  state_type x = { 900 , 100, 0 }; // initial conditions
  integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
                     eqsir , x , 1.0 , 2 , 1/52 , write_cout_2 );
  return true;
}

But an error message appeared :

In function 'bool my_fun10_solver()': ex3.cpp:114:64: error: no matching function for call to 'integrate_adaptive(boost::numeric::odeint::result_of::make_controlled > >::type, void (&)(const state_type&, state_type&, double), state_type&, double, int, int, void (&)(const state_type&, double))' eqsir , x , 1.0 , 2 , 1/52 , write_cout_2 );

What is wrong with my code?

Below is the script I took and adapted to my problem. This scripts works well.


#include <Rcpp.h>
#include <boost/array.hpp>

// include Boost's odeint
#include <boost/numeric/odeint.hpp>

// [[Rcpp::depends(BH)]]

using namespace Rcpp;
using namespace std;
using namespace boost::numeric::odeint;


typedef boost::array< double ,3 > state_type;

void rhs( const state_type &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);
  dxdt[2] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}

void write_cout( const state_type &x , const double t ) {
  // use Rcpp's stream
  Rcpp::Rcout << t << '\t' << x[0] << '\t' << x[1] << '\t' << x[2] <<  endl;
}

 typedef runge_kutta_dopri5< state_type > stepper_type;

// [[Rcpp::export]]
bool boostExample() {
  state_type x = { 1.0 , 1.0, 1.0 }; // initial conditions
  integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
                     rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
  return true;
}

Upvotes: 1

Views: 743

Answers (1)

Kaveh Vahedipour
Kaveh Vahedipour

Reputation: 3477

Could you try the following please?

integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
                 eqsir , x , 1.0 , 2.0 , 1.0/52.0 , write_cout_2 );

A couple of optimisation suggestions though. You are solving an ODE, which suggests that you are a physicist or engineer, which is awesome, I am a physicist too, and physicists just rock.

But so do computer scientists, and they have tried their best to do thing as fast as possible. They build compilers, which take away a lot of the thinking involved. Try to help them where ever you can.

Why am I telling you this? Back to the ODE thing. boost's adaptive intergrator is maybe calling eqsir() 1e9 times. Try to make that function as performant as possible. Consider rewriting my_fun2 so that x gets overwritten rather than create a new one and return it. x is the last state. You wouldn't care less about it unless you wanted to plot the dynamics.

void my_fun2(Rcpp::NumericVector &x, const double t);

Also

Rcpp::NumericVector nvec=boost_array_to_nvec(x);
Rcpp::NumericVector nvec2(3);

have to allocate new memory on every call. Finally, consider using the 2 converters for nvec and state_type with the references I wrote as second option. Your new eqsir would look like this and run possibly a lot faster.

Rcpp::NumericVector nvec(3); // declared outside
void eqsir(const state_type &x, state_type &dxdt, const double t){
  boost_array_to_nvec(x, nvec);
  my_fun2(nvec,t);
  nvec_to_boost_array(nvec, dxdt);
}

Upvotes: 1

Related Questions