r - 使用 purrr 在 R 中向量化 ode
问题描述
我正在尝试使用 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
关于我做错了什么有什么想法吗?
解决方案
推荐阅读
- java - 为什么 JProfiler 在 Java 中向我显示每个 char[] 200+ 字节的巨大开销?
- swift - SwiftUI 如何从另一个类访问 ContentView 变量
- python - 你能帮我理解这个熊猫代码吗
- javascript - Chrome 扩展:我需要开发工具网络选项卡日志。那可能吗?
- visual-studio-code - VSCode 中的自动缩进
- javascript - Material-ui:找不到模块:无法解析“material-ui/Toolbar”
- javascript - 我的导航栏的样式在移动模式下完全改变
- r - 如何将数据框的两行合并为一行?
- java - 无法解决 Android 中的 POST 请求
- mysql - 如何计算两个直接行之间的最大差值?