[R-sig-dyn-mod] Problem with loading compiled code into R
Thomas Petzoldt
thomas.petzoldt at tu-dresden.de
Wed Apr 12 13:46:12 CEST 2017
Hi,
many thanks to Tim for the detailed answer and to Mathilde for further
clarification.
Just for the record, in addition to the CRAN tests we tested deSolve
1.14 with CLANG and a "user-compiled" model (see attachment):
- on Ubuntu 16.04 (R-devel) and
- macOS 10.12.4 (R 3.3.2)
(on the laptop of our master student Felix)
The example compiled, loaded and worked flawlessly without any problems.
Thomas
-------------- next part --------------
library(deSolve)
library(scatterplot3d)
parameters <- c(a = -8/3, b = -10, c = 28)
state <- c(X = 1, Y = 1, Z = 1)
times <- seq(0, 100, by = 0.01)
system("R CMD SHLIB lorenzc.c")
dll <- paste0("lorenzc", .Platform$dynlib.ext)
dyn.load(dll)
out <- ode(state, times, func = "derivs", parms = parameters,
dllname = "lorenzc", initfunc = "initmod")
dyn.unload(dll)
plot(out)
library(scatterplot3d)
scatterplot3d(out[,-1], type="l")
-------------- next part --------------
/* file lorenz.c */
#include <R.h>
static double parms[3];
#define a parms[0]
#define b parms[1]
#define c parms[2]
/* initializer */
void initmod(void (* odeparms)(int *, double *))
{
int N = 3;
odeparms(&N, parms);
}
/* Derivatives */
void derivs (int *neq, double *t, double *y, double *ydot,
double *yout, int *ip)
{
ydot[0] = a * y[0] + y[1] * y[2];
ydot[1] = b * (y[1] - y[2]);
ydot[2] = - y[0] * y[1] + c * y[1] - y[2];
}
More information about the R-sig-dynamic-models
mailing list