Guest User

Untitled

a guest
Apr 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. modelsir_cpp =function(t,x){
  2. S = x[1]
  3. I1 = x[2]
  4. I2 = x[3]
  5. N=S+I1+I2
  6. with(as.list(parm), {
  7. dS=B*I1-mu*S-beta*(S*(I1+I2)/N)
  8. dI1=beta*(S*(I1+I2)/N)-B*I1-lambda12*I1
  9. dI2=lambda12*I1
  10. res=c(dS,dI1,dI2)
  11. return(res)
  12. })
  13. }
  14.  
  15. times = seq(0, 10, by = 1/52)
  16. parm=c(B=0.01,mu=0.008,beta=10,lambda12=1)
  17. xstart=c(S=900,I1=100,I2=0)
  18. out = as.data.frame(lsoda(xstart, times, modelsir, parm))
  19.  
  20. #include <Rcpp.h>
  21. #include <boost/array.hpp>
  22.  
  23. // include Boost's odeint
  24. #include <boost/numeric/odeint.hpp>
  25.  
  26. // [[Rcpp::depends(BH)]]
  27.  
  28. using namespace Rcpp;
  29. using namespace std;
  30. using namespace boost::numeric::odeint;
  31.  
  32. typedef boost::array< double ,3 > state_type;
  33.  
  34. void eqsir(const state_type &x, state_type &dxdt, const double t){
  35. Rcpp::NumericVector nvec=boost_array_to_nvec(x);
  36. Rcpp::NumericVector nvec2(3);
  37. nvec2=my_fun2(nvec,t);
  38. dxdt=nvec_to_boost_array(nvec2);
  39. }
  40.  
  41. void write_cout_2( const state_type &x , const double t ) {
  42. // use Rcpp's stream
  43. Rcpp::Rcout << t << 't' << x[0] << 't' << x[1] << 't' << x[2] << endl;
  44. }
  45. typedef runge_kutta_dopri5< state_type > stepper_type;
  46.  
  47. // [[Rcpp::export]]
  48. bool my_fun10_solver() {
  49. state_type x = { 900 , 100, 0 }; // initial conditions
  50. integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
  51. eqsir , x , 1.0 , 2 , 1/52 , write_cout_2 );
  52. return true;
  53. }
  54.  
  55. #include <Rcpp.h>
  56. #include <boost/array.hpp>
  57.  
  58. // include Boost's odeint
  59. #include <boost/numeric/odeint.hpp>
  60.  
  61. // [[Rcpp::depends(BH)]]
  62.  
  63. using namespace Rcpp;
  64. using namespace std;
  65. using namespace boost::numeric::odeint;
  66.  
  67.  
  68. typedef boost::array< double ,3 > state_type;
  69.  
  70. void rhs( const state_type &x , state_type &dxdt , const double t ) {
  71.  
  72. dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
  73. dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
  74. dxdt[2] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
  75. }
  76.  
  77. void write_cout( const state_type &x , const double t ) {
  78. // use Rcpp's stream
  79. Rcpp::Rcout << t << 't' << x[0] << 't' << x[1] << 't' << x[2] << endl;
  80. }
  81.  
  82. typedef runge_kutta_dopri5< state_type > stepper_type;
  83.  
  84. // [[Rcpp::export]]
  85. bool boostExample() {
  86. state_type x = { 1.0 , 1.0, 1.0 }; // initial conditions
  87. integrate_adaptive(make_controlled( 1E-12 , 1E-12 , stepper_type () ) ,
  88. rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
  89. return true;
  90. }
Add Comment
Please, Sign In to add comment