I am solving ODE's in R
using the deSolve
package. In order to speed up the calculations I want to use compiled code, using the instructions here
I am showing below an example of ODE system - which I am coding using Rcpp
below. The details of the ODE system are taken from an example MATLAB
code (can be found here). I wanted to simulate a non-trivial set of ODE in order to see a difference in only R
and compile code. The following is my drive file, where I calculated the mass-balances in two different ways (only R
and compiled code)
library(deSolve)
library(ggplot2)
library(microbenchmark)
source('parameters_gprotein.R')
p <- parameters()
source('IC_gprotein.R')
IC <- Initial_conditions()
TIME = seq(from = 0, to = 600)
source('odes_gprotein.R')
sim.data.df <- as.data.frame(vode(IC,TIME,ODE_gprotein,p,
mf = 22, rtol=1e-3,atol=1e-6,maxord = 5,
verbose = F))
Rcpp::sourceCpp("odes_gprotein.cpp")
sim.data.df <- as.data.frame(vode(IC,TIME,odes_gprotein,p,
mf = 22, rtol = 1e-3, atol = 1e-6, maxord = 5,
verbose = F))
My question is since vode
call is made in R
. Does that mean the equations are solved in compiled code if the mass balances are formed in cpp
and the speed gains are realized, or do I have to also make the vode
call in cpp
file.
Certainly, the microbenchmarking results show that there is a speed gain when using odes_gprotein.cpp
Unit: milliseconds
expr
sim.data.df1 <- as.data.frame(vode(IC, TIME, ODE_gprotein, p, mf = 22, rtol = 0.001, atol = 1e-06, maxord = 5, verbose = F))
sim.data.df2 <- as.data.frame(vode(IC, TIME, odes_gprotein, p, mf = 22, rtol = 0.001, atol = 1e-06, maxord = 5, verbose = F))
min lq mean median uq max neval
27.801954 29.543624 31.213758 30.565434 31.399140 86.28537 100
8.188846 8.577824 9.177491 8.817025 9.437214 18.94304 100
Thanks