thk686 / odeintr

An R package for integrating differential equations in C++
18 stars 2 forks source link

"Invalid initial state" when using more than 10 equations #6

Closed sdotsam closed 6 years ago

sdotsam commented 6 years ago

There seems to be a problem with using odeintr for more than 10 equations.

When I use odeintr for up to 10 equations it works perfectly fine, as soon es I add another one it returns:

Error in runtime_test(Nstart, timelength, 0.1) : Invalid initial state
Called from: runtime_test(Nstart, timelength, 0.1)

Is there any way to solve this issue, while still using odeintr?

This is a simple version of the code I'm using:

runtime_test_model = '
dxdt[0] = r*x[0];
dxdt[1] = r*x[1];
dxdt[2] = r*x[2];
dxdt[3] = y*x[3];
dxdt[4] = y*x[4];
dxdt[5] = y*x[5];
dxdt[6] = r*x[6];
dxdt[7] = r*x[7];
dxdt[8] = r*x[8];
dxdt[9] = r*x[9];
dxdt[10]= y*x[10]
'

compile_sys("runtime_test", runtime_test_model, pars = c("r", "y"
), method = "rk54_a")

timelength = 10000

runtime_testR = function(Nstart, parms){
  runtime_test_set_params(r=parms[1],
                       y=parms[2])
  runtime_test(Nstart,timelength,.1)
}

set.seed(667)

out = runtime_testR(Nstart = c(x1 = 1, x2 = 1, x3 = 1, x4 = 1, x5 = 1, x6 = 1, x7 = 1, x8 = 1, x9 = 1, x10 = 1, x11 = 1),
                 parms = c(r=0.1, y=0.2)
)
thk686 commented 6 years ago

You have to use the "sys_dim" option to compile_sys.

compile_sys("runtime_test", runtime_test_model, pars = c("r", "y"
), method = "rk54_a", sys_dim = 11)
sdotsam commented 6 years ago

Perfect, thank you!