我正在尝试使用purrr解决ODE的许多置换初始条件。
我在数据框中存储了以下信息。
library(tidyverse)
library(deSolve)
flow.data <- tibble(Unit=c(rep("MSS",times=11),rep("CS",times=3)),
Size=c(400,750,1200,1600,2400,3000,3600,4500,
6000,7200,10000,(1700*60/500),(4000*60/500),
(6600*60/500)),
Product=Size/60*3.786,
Feed=Product*.85,
Blowdown=Feed*.15,
Vol=c(74,106,163,275,353,492,603,
772,1340,1417,1800,7,18,26),
Ca=c(rep(10000,n=14))
)
我已经按照以下步骤设置了ODE解决方案,并且当我插入变量时它可以工作:
odefunction <- function(h,q,ca,v){
g <- .5 #L/min
h <- h*3.785 #Feed gal/min converted to L/min
q <- q*3.785 #Blowdown gal/min converted to L/min
v <- v*3.785 #Vol gal converted to L
parameters <- c(h=h,q=q,v=v,f=ca)
state <- c(ca=0)
times <- seq(0,50,by=1)
funs <- function(t,state,parameters) {
with(as.list(c(state,parameters)),{
dca <- (h*f/v)-(q*ca/v)
list(c(dca))
})
}
out <- ode(y=state,times=times,func=funs,parms=parameters)
}
例如,
res <- odefunction(118,18,10000,1370)
产生:
> head(res)
time ca
[1,] 0 0.0000
[2,] 1 855.6803
[3,] 2 1700.1916
[4,] 3 2533.6797
[5,] 4 3356.2886
[6,] 5 4168.1601
我正在尝试使用pmap(来自purrr)获取结果,以将列表映射到odefunction。
test <- pmap(list(flow.data$Feed,flow.data$Blowdown,flow.data$Ca,flow.data$Vol),odefunction(h,q,ca,v))
我希望使用flow.data来获得针对该行的ODE ala解决方案的嵌套标题,
MSS 400 25.2 21.5 3.22 74 10000 tibble...
我得到的是:
Error: Can't convert a `deSolve` object to function
Call `rlang::last_error()` to see a backtrace
<error>
message: Can't convert a `deSolve` object to function
class: `rlang_error`
backtrace:
-purrr::pmap(...)
-purrr:::as_mapper.default(.f, ...)
-rlang::as_closure(.f)
-rlang::as_function(x, env = env)
-rlang::coerce_type(...)
-rlang:::abort_coercion(.x, .to)
Call `summary(rlang::last_error())` to see the full backtrace
对我在做什么错有任何想法吗?