Discussion:
[Rcpp-devel] Using Rcpp to solve ODEs: LSODE (deSolve) says "Confusion over the length of parms"
Gibbons, Frank
2017-01-20 19:29:27 UTC
Permalink
Hi,

Just joined the list. I've searched online for help, and could find little mention of the problem I'm having, and zero solutions. I'm getting a strange error message when using Rcpp to combine a C-implementation of some simple linear ODEs with deSolve in package "popED". (It's a package for optimal design of experiments. Because it relies on repeated solution of the ODEs at each iterative step, a pure-R implementation is too slow for me on non-trivial problems.)
tic(); output <- poped_optim(poped.db.compiled, opt_xt =TRUE, parallel=F, method = c("LS")); toc()
Error in lsoda(y, times, func, parms, ...) :
Confusion over the length of parms
In addition: Warning message:
In lsoda(y, times, func, parms, ...) :
Number of parameters passed to solver, 3; number in DLL, 5
Called from: lsoda(y, times, func, parms, ...)

How can it run to evaluate the model, then fail when it comes to optimization? Since the error is coming from LSODA (the solver that is wrapped by deSolve), I'm guessing the problem lies in interface between deSolve and the code I've got. I'm no stranger to language extension (years ago, I used SWIG to extend some Python libraries with C code), but I'm struggling to understand where to look for the source of this problem. In particular, it's telling me the DLL has 5 parameters, but I just don't see where that could be coming from: I explicitly pass in three parameters (the KA, V, and CL from above). Does Rcpp inject extras that are somehow being exposed inappropriately?

One last thing: you may look at this model and ask why I'm not using the obvious closed-form solution for the concentration profile - that's the fastest way to the answer. The answer is that this isn't really the problem I'm interested in, and those don't have an easy closed-form solution.

Would really appreciate any pointers you can give. Many thanks and kind regards,

-Frank







P.S.
R.Version()
$platform
[1] "x86_64-w64-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "3"

$minor
[1] "3.2"

$year
[1] "2016"

$month
[1] "10"

$day
[1] "31"

$`svn rev`
[1] "71607"

$language
[1] "R"

$version.string
[1] "R version 3.3.2 (2016-10-31)"

$nickname
[1] "Sincere Pumpkin Patch"
Frank Gibbons, PhD
Principal Scientist
_____________________________________________________________________________________________
DMPK Modeling & Simulation | IMED Oncology | AstraZeneca
35 Gatehouse Lane, Waltham MA 02451, USA
+1-781-839-4032 ***@astrazeneca.com<mailto:***@astrazeneca.com>


________________________________

Confidentiality Notice: This message is private and may contain confidential and proprietary information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorized use or disclosure of the contents of this message is not permitted and may be unlawful.
Dirk Eddelbuettel
2017-01-23 01:50:50 UTC
Permalink
Frank,

That's a pretty long message and a lot of code. Can you try to distill it
down to a particular (smaller?) issue?

And FWIW I cannot run. After saving your attachment, and installing PopED, I
get

R> source("/tmp/myExampleODE.compiled.R")

Attaching package: ‘deSolve’

The following object is masked from ‘package:graphics’:

matplot

Elapsed time: 10.425 seconds.
Error in poped.db$settings$optsw[2] <- opt_xt :
object 'poped.db.compiled' not found
R>

I also recommend the rbenchmark and microbenchmark packages over tic() and toc().

Sorry that I have no quick fix.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Kyle Baron
2017-01-23 18:34:57 UTC
Permalink
Frank -

In the code you provided, it looks like it is "poped.db.compiled.rcpp", not
"poped.db.compiled".

I found that, after making the change to fix the error that Dirk pointed
out, the code appears to work with no error (attached). I don't know if
the results are what you were intending, however.

Also: I would not set up the optimization like this. When you create
the one_comp_oral_ode function with cppFunction and hand that off to
deSolve, you are still passing along an R function to get called. Even if
the calculations are accomplished with Rcpp, it still an R function. So
every step the solver takes, it will have to call an R function. I don't
think this is what you want. When you write the ode function as suggested
by the deSolve package vignette, you pass off the name of a C function and
the solver calls that C function directly as the system advances. It will
be faster than what you are doing with cppFunction.

Finally, I'll ask if you have looked at one of the more-modern simulation
packages for R that are designed for PK/PD work?
I maintain mrgsolve: https://github.com/metrumresearchgroup/mrgsolve
You could also look at RxODE: https://cran.r-project.
org/web/packages/RxODE/index.html

Hope that helps ...
Kyle
Post by Dirk Eddelbuettel
Frank,
That's a pretty long message and a lot of code. Can you try to distill it
down to a particular (smaller?) issue?
And FWIW I cannot run. After saving your attachment, and installing PopED, I
get
R> source("/tmp/myExampleODE.compiled.R")
Attaching package: ‘deSolve’
matplot
Elapsed time: 10.425 seconds.
object 'poped.db.compiled' not found
R>
I also recommend the rbenchmark and microbenchmark packages over tic() and toc().
Sorry that I have no quick fix.
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron
Metrum Research Group
860-735-7043, Ext. 202
***@metrumrg.com
Gibbons, Frank
2017-01-25 17:33:43 UTC
Permalink
Kyle,

D’oh! You’re absolutely right. Embarrassing. :-}
(I’m told that’s the emoticon for ‘sheepish grin’.)

I did try going the way suggested by deSolve, but find that (for reasons unknown to me), “R CMD SHLIB” compiles the object file but not the DLL (error about not having permission).

I wasn’t aware of RxODE at all (and only vaguely familiar with mrgsolve), but installed it, read through the tutorial (from the journal CPT-PSP), and within a few hours was in business. It won’t be for everyone of course, since it’s specialized for PK/PD modeling, but it enabled me to get a 15x performance improvement in my application (which requires repeated model evaluation to optimize a study design). This is huge: over-lunchbreak vs over-night. If the speedup reported by the authors in their tutorial applies to my situation, this improvement factor will improve as the model grows (currently just 2 ODEs).

For those with similar goals as mine (PK/PD models), highly recommend it. Much like Rcpp, it enables specification of both the compiled and non-compiled parts of the code in a single file. But like SHLIB, it compiles behind the scenes (few seconds), and while I don’t fully understand what’s going on under the hood, it performs as if there’s a minimum of back-and-forth between R and C.

Many thanks to you both

-Frank

From: Kyle Baron [mailto:***@metrumrg.com]
Sent: Monday, January 23, 2017 1:35 PM
To: Dirk Eddelbuettel <***@debian.org>
Cc: Gibbons, Frank <***@astrazeneca.com>; rcpp-***@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Using Rcpp to solve ODEs: LSODE (deSolve) says "Confusion over the length of parms"

Frank -

In the code you provided, it looks like it is "poped.db.compiled.rcpp", not "poped.db.compiled".

I found that, after making the change to fix the error that Dirk pointed out, the code appears to work with no error (attached). I don't know if the results are what you were intending, however.

Also: I would not set up the optimization like this. When you create the one_comp_oral_ode function with cppFunction and hand that off to deSolve, you are still passing along an R function to get called. Even if the calculations are accomplished with Rcpp, it still an R function. So every step the solver takes, it will have to call an R function. I don't think this is what you want. When you write the ode function as suggested by the deSolve package vignette, you pass off the name of a C function and the solver calls that C function directly as the system advances. It will be faster than what you are doing with cppFunction.

Finally, I'll ask if you have looked at one of the more-modern simulation packages for R that are designed for PK/PD work?
I maintain mrgsolve: https://github.com/metrumresearchgroup/mrgsolve
You could also look at RxODE: https://cran.r-project.org/web/packages/RxODE/index.html

Hope that helps ...
Kyle



On Sun, Jan 22, 2017 at 7:50 PM, Dirk Eddelbuettel <***@debian.org<mailto:***@debian.org>> wrote:

Frank,

That's a pretty long message and a lot of code. Can you try to distill it
down to a particular (smaller?) issue?

And FWIW I cannot run. After saving your attachment, and installing PopED, I
get

R> source("/tmp/myExampleODE.compiled.R")

Attaching package: ‘deSolve’

The following object is masked from ‘package:graphics’:

matplot

Elapsed time: 10.425 seconds.
Error in poped.db$settings$optsw[2] <- opt_xt :
object 'poped.db.compiled' not found
R>

I also recommend the rbenchmark and microbenchmark packages over tic() and toc().

Sorry that I have no quick fix.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org<mailto:***@debian.org>
_______________________________________________
Rcpp-devel mailing list
Rcpp-***@lists.r-forge.r-project.org<mailto:Rcpp-***@lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron
Metrum Research Group
860-735-7043, Ext. 202
***@metrumrg.com<mailto:***@metrumrg.com>

________________________________

Confidentiality Notice: This message is private and may contain confidential and proprietary information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorized use or disclosure of the contents of this message is not permitted and may be unlawful.
Tim Keitt
2017-01-25 18:13:52 UTC
Permalink
There's also https://github.com/thk686/odeintr (use the devel version, not
CRAN -- need to upload a bug fix).

THK

http://www.keittlab.org/

On Wed, Jan 25, 2017 at 11:33 AM, Gibbons, Frank <
Post by Gibbons, Frank
Kyle,
D’oh! You’re absolutely right. Embarrassing. :-}
(I’m told that’s the emoticon for ‘sheepish grin’.)
I did try going the way suggested by deSolve, but find that (for reasons
unknown to me), “R CMD SHLIB” compiles the object file but not the DLL
(error about not having permission).
I wasn’t aware of RxODE at all (and only vaguely familiar with mrgsolve),
but installed it, read through the tutorial (from the journal CPT-PSP), and
within a few hours was in business. It won’t be for everyone of course,
since it’s specialized for PK/PD modeling, but it enabled me to get a 15x
performance improvement in my application (which requires repeated model
evaluation to optimize a study design). This is huge: over-lunchbreak vs
over-night. If the speedup reported by the authors in their tutorial
applies to my situation, this improvement factor will improve as the model
grows (currently just 2 ODEs).
For those with similar goals as mine (PK/PD models), highly recommend it.
Much like Rcpp, it enables specification of both the compiled and
non-compiled parts of the code in a single file. But like SHLIB, it
compiles behind the scenes (few seconds), and while I don’t fully
understand what’s going on under the hood, it performs as if there’s a
minimum of back-and-forth between R and C.
Many thanks to you both
-Frank
*Sent:* Monday, January 23, 2017 1:35 PM
*Subject:* Re: [Rcpp-devel] Using Rcpp to solve ODEs: LSODE (deSolve)
says "Confusion over the length of parms"
Frank -
In the code you provided, it looks like it is "poped.db.compiled.rcpp",
not "poped.db.compiled".
I found that, after making the change to fix the error that Dirk pointed
out, the code appears to work with no error (attached). I don't know if
the results are what you were intending, however.
Also: I would not set up the optimization like this. When you create
the one_comp_oral_ode function with cppFunction and hand that off to
deSolve, you are still passing along an R function to get called. Even if
the calculations are accomplished with Rcpp, it still an R function. So
every step the solver takes, it will have to call an R function. I don't
think this is what you want. When you write the ode function as suggested
by the deSolve package vignette, you pass off the name of a C function and
the solver calls that C function directly as the system advances. It will
be faster than what you are doing with cppFunction.
Finally, I'll ask if you have looked at one of the more-modern simulation
packages for R that are designed for PK/PD work?
I maintain mrgsolve: https://github.com/metrumresearchgroup/mrgsolve
You could also look at RxODE: https://cran.r-project.
org/web/packages/RxODE/index.html
Hope that helps ...
Kyle
Frank,
That's a pretty long message and a lot of code. Can you try to distill it
down to a particular (smaller?) issue?
And FWIW I cannot run. After saving your attachment, and installing PopED, I
get
R> source("/tmp/myExampleODE.compiled.R")
Attaching package: ‘deSolve’
matplot
Elapsed time: 10.425 seconds.
object 'poped.db.compiled' not found
R>
I also recommend the rbenchmark and microbenchmark packages over tic() and toc().
Sorry that I have no quick fix.
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron
Metrum Research Group
860-735-7043, Ext. 202 <(860)%20735-7043>
------------------------------
*Confidentiality Notice: *This message is private and may contain
confidential and proprietary information. If you have received this message
in error, please notify us and remove it from your system and note that you
must not copy, distribute or take any action in reliance on it. Any
unauthorized use or disclosure of the contents of this message is not
permitted and may be unlawful.
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Loading...