0

Here is the sample R code:

library(deSolve)
d = c(5.29,4.16,2.49,1.53,0.7,0.41,0.21)*10^-4
rho = rep(1.27,7)
dg = d * sqrt(1/rho)
r0 = dg/2
Fr = c(0.188,0.297,0.274,0.181,0.032,0.013,0.015)
X0 = Fr*200*10^-6
N0 = X0*(3/(4*3.14*r0^3*rho))


state <- c(X1=X0[1],X2=X0[2],X3=X0[3],X4=X0[4],X5=X0[5],
       X6=X0[6],X7=X0[7],S=0)
parameters <- c(D=6.19*60*10^-6,rho=rho,N=N0,Cs=17*10^-6,V=1000)
times <- seq(0,16,by=0.0005)

func2 <- function(t,state,parameters){ 
n <- length(state)
v <- 1:(n-1)
grad <- rep(NA,n)
tmp1 <- (4*3.14*rho[v]*N0[v])
tmp2 <- 3*state[v]/tmp1
tmp3 <- tmp2^(1/3)
grad[v] <- with(as.list(c(state,parameters)),{
-D*(N0[v]*4*3.14*tmp3)*(Cs-S/V)
})

grad[n] <- -sum(grad[v], na.rm = T)
return(list(grad))
}

sfunc <- function(t,state,parameters){
with(as.list(c(state,parameters)),{

S = S - (S/1000)*10
res <- c(X1,X2,X3,X4,X5,X6,X7,S)
return(res)

})
}

spt <- c(0,10/60,20/60,30/60,45/60,1,1.5,2,3,4,6,8,12,24)

out <- ode(y = state, times = times,func = func2, 
       parms = parameters, method = 'rk4',
       events=list(func=sfunc,time=spt))
plot(out)

I encounter the following error:

Error in rk(y, times, func, parms, method = "rk4", ...) : 'rho' cannot be C NULL: detected in C-level eval

and the following warning messages:

In addition: Warning messages:

1: In checkevents(events, times, Ynames, dllname) : Not all event times 'events$time' are in output 'times' so they are automatically included.

2: In checkevents(events, times, Ynames, dllname) : Some time steps were very close to events - only the event times are used in these cases.

It runs fine if I remove the events.

Ruser
  • 23
  • 4
  • How can we reproduce the error? Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Oct 16 '18 at 10:07
  • @jogo This question is in continuation to: https://stackoverflow.com/questions/52576686/nans-produced-in-desolve-package – Ruser Oct 16 '18 at 10:49
  • @jogo I have now edited my post to provide the sample R code and the error should be easily reproducible. – Ruser Oct 16 '18 at 11:10

0 Answers0