Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cause of "Error in .C("unlock solver")" Error in deSolve Package?

Tags:

r

I have been using the deSolve package in a MCMC algorithm to estimate parameters in an ODE and wrote the functions used in the solver in C to speed up the algorithm. Sometimes, but not always I get the error Error in .C("unlock solver") when running the ode function. I am able to successfully compile and link the C files using the commands

system("R CMD SHLIB [insert-file-path]")
dyn.load("[dll-file-path]")

but when I try to solve the ODE using the dll file, the error is thrown. Then, even when running a simple script like the one below, I get the same error. I think the issue is related to using the compiled code, but I don't know how and cannot find any references on this error.

> require(deSolve)
> initVal <- c(y=1)
> times <- seq(0, 1, 0.001)
> parms <- c(k=1)
> model1 <- function(t, y, parms){
+   with(as.list(c(y, parms)),{
+     dy <- -k*y;
+     list(c(dy))
+   })
+ }
> out <- ode(y=initVal, times=times, parms=parms, func=model1)
Error in .C("unlock_solver") : 
  "unlock_solver" not resolved from current namespace (deSolve)

Partial Solution If I restart R and only load the DLL using the dyn.load function, but don't compile the code, the ode function runs without an error. This fixes my problem, but I still have no idea why.

like image 323
caburke Avatar asked Mar 23 '23 19:03

caburke


1 Answers

Edit: REAL solution from Thomas Petzoldt on the R help list:

[The error] occurs, if package deSolve is loaded after the compiled model... The solution is to load deSolve before [before loading DLL's], ideally at the very beginning of your script, and at least before loading/unloading the DLL/.so

If that doesn't work, the below might as well (old answer):

I have found a somewhat inelegant solution.

The problem seems to be that the "unlock_solver" function within deSolve is somehow not being accessed correctly. You can unload and reload the entire deSolve.so file, rather than restarting R.

To do this you can use something like:

require(deSolve)

# encounter error

library.dynam.unload("deSolve", libpath=paste(.libPaths()[1], "//deSolve", sep=""))
library.dynam("deSolve", package="deSolve", lib.loc=.libPaths()[1])

You'll need to replace ".libPaths()[1]" with wherever you have installed deSolve, if it isn't in the first element of your .libPaths variable.

This is something of a sledge hammer, though. I've sent out a request to the r-help list asking if there is some way to either change where R looks for "unlock_solver", or to just unload/reload a portion of deSolve.

like image 64
Adam Clark Avatar answered Mar 25 '23 20:03

Adam Clark