我正在尝试使用日晷用于求解常微分方程组 (ODE) 的包,使用SUNDIALS。首先,我尝试运行手册中的示例。
当 ODE 的右侧在 C 文件中描述时,Rsundials 可用于求解 ODE。
首先,ODE 在 C 文件中进行描述(包中的示例程序)
#include "include/nvector/nvector_serial.h"
#include "include/sundials/sundials_dense.h"
#define Ith(v,i) ( NV_DATA_S(v)[i - 1] )
int rhs(realtype t, N_Vector y, N_Vector ydot, void *f_data){
realtype y1, y2, y3;
y1 = Ith(y,1); y2 = Ith(y,2); y3 = Ith(y,3);
/* Change values of ydot here using Ith(ydot,i) */
double L = 49.3;
double a21 = 0.011; double a12 = 0.012;
double a31 = 0.0039; double a13 = 0.000035;
double a01 = 0.021; double a02 = 0.016;
Ith(ydot, 1) = -(a01 + a21 + a31)*y1 + a12*y2 + a13*y3 + L;
Ith(ydot, 2) = a21*y1 - (a02 + a12)*y2;
Ith(ydot, 3) = a31*y1 - a13*y3;
return(0);
}
保存在文件中test_rsundials.c
。然后在终端中使用以下命令编译该文件
R CMD SHLIB ./test_rsundials.c
该文件编译时没有任何错误消息我看到test_rsundials.so
and test_rsundials.o
在我正在生成的目录中。
然后我用dyn.load
加载包 -
dyn.load("/ .. path .. /test_rsundials.so")
上面的步骤中也没有错误消息,最后我使用以下命令调用cvode求解器(如包手册中所示)
library(Rsundials)
vals <- cvodes(c(0.0,0.0,0.0), seq(0,400,20),"test_rsundials","rhs",rtol=1e-4,atol=c(1e-8,1e-14,1e-6),verbose=T)
这会产生错误 -
Error in .Call("cvodes", PACKAGE = "Rsundials", as.double(y), as.double(times), :
"cvodes" not available for .Call() for package "Rsundials"
我也尝试了以下命令
yvals <- .Call("cvodes",package="Rsundials",c(0.0,0.0,0.0), seq(0,400,20),"test_rsundials","rhs",rtol=1e-4,atol=c(1e-8,1e-14,1e-6),verbose=T)
这给了我错误
Error in .Call("cvodes", package = "Rsundials", c(0, 0, 0), seq(0, 400, :
C symbol name "cvodes" not in load table
我不确定如何解决它,或者它意味着什么。我粘贴代码cvodes
以下供参考
function (y, times, package, rhs, fndata = NULL, jacfunc = NULL,
rootfunc = NULL, numroots = 0, rtol = 1e-06, atol = 1e-06,
maxnumsteps = 500, maxstep = 0, verbose = FALSE, lasttime = FALSE)
{
if (!is.numeric(y))
stop("Error: 'y' must be numeric")
if (!is.numeric(times))
stop("Error: 'times' must be numeric")
if (!is.character(package))
stop("Error: 'package' must be a character vector")
if (!is.character(rhs))
stop("Error: 'Right Hand Side function must be a character vector")
if (!is.null(jacfunc) && !is.character(jacfunc))
stop("Error: 'jacfunc' must be a character vector")
if (!is.null(rootfunc) && !is.character(rootfunc))
stop("Error: 'rootfunc' must be a character vector")
if (!is.null(rootfunc) && numroots <= 0)
stop("Error: numroots must be greater than 0")
if (!is.numeric(numroots))
stop("Error: 'numroots' must be numeric")
if (!is.numeric(rtol))
stop("Error: 'rtol' must be numeric")
if (!is.numeric(atol))
stop("Error: 'atol' must be numeric")
if (!is.numeric(maxstep))
stop("Error: 'maxsteps' must be numeric")
if (!is.null(fndata) && !is.numeric(fndata))
stop("Error: Data arguments must be numeric")
s = 1
rhs = getNativeSymbolInfo(rhs, PACKAGE = package)$address
jfunc = NULL
if (!is.null(jacfunc))
jfunc = getNativeSymbolInfo(jacfunc, PACKAGE = package)$address
rofunc = NULL
if (!is.null(rootfunc))
rofunc = getNativeSymbolInfo(rootfunc, PACKAGE = package)$address
solutions = .Call("cvodes", PACKAGE = "Rsundials", as.double(y),
as.double(times), rhs, as.double(fndata), jfunc, rofunc,
as.integer(numroots), as.integer(s), as.double(rtol),
as.double(atol), as.integer(maxnumsteps), as.integer(maxstep),
as.integer(verbose), as.integer(lasttime))
if (lasttime == TRUE)
rows = 1
else rows = length(times)
solutions <- matrix(solutions, rows)
col <- c()
for (i in 1:length(y)) col[i] <- paste("y", i, sep = "")
if (lasttime == TRUE)
dimnames(solutions) <- list(times[length(times)], col)
else dimnames(solutions) <- list(times, col)
solutions
}
<environment: namespace:Rsundials>
下面还粘贴了有关我的 R 的详细信息
> version
_
platform x86_64-apple-darwin13.1.0
arch x86_64
os darwin13.1.0
system x86_64, darwin13.1.0
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
我读到这可能是 64 位与 32 位编译问题,我不确定如何在 64 位标志中指定编译。我是 R 和 C 的新手,所以任何帮助将不胜感激!
发布更新——我读到此错误可能是由于链接库问题造成的。我看了看src这个包的文件夹下,有一个文件Makevars.old
这意味着我可能需要链接到库
PKG_CFLAGS=-w
PKG_LIBS=-L. -lsundials_cvodes -lsundials_ida -lsundials_fida -lsundials_nvecserial
注意 - 软件包手册说“安装 SUNDIALS 不是该软件包的先决条件”。所以我把所有的文件都放进去了src
该包的文件夹位于我的项目文件夹中(因此希望没有文件未找到错误)。
现在我尝试在终端中使用以下命令进行编译
export PKG_CFLAGS="-w"
export PKG_LIBS="-L. -lsundials_cvodes -lsundials_nvecserial"
R CMD SHLIB test_rsundials.c sundials_dense.c nvector_serial.c
我现在收到以下错误
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -w -fPIC -Wall -mtune=core2 -g -O2 -c nvector_serial.c -o nvector_serial.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o nvector_serial.so nvector_serial.o sundials_dense.o test_rsundials.o -L. -lsundials_cvodes -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lsundials_cvodes
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nvector_serial.so] Error 1
虽然,我确实看到了三个新的.o
files (test_rsundials.o
, nvector_serial.o
and sundials_dense.o
)生成
任何帮助都感激不尽!!