-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFate Analysis SB4solar-III-V.Rmd
303 lines (226 loc) · 10.4 KB
/
Fate Analysis SB4solar-III-V.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
title: "Calculating Probabilistic Dynamic Exposure Concentrations"
author: "Joris T.K. Quik and Carlos Blanco"
date: "20/10/2021"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
ptm <- proc.time()
```
## Introduction
This document describes the code and functions to produce quasi-dynamic ('levelIV') solutions of the [SimpleBox](https://www.rivm.nl/simplebox) multimedia fate model in comvbination with dynamic emission rates.
This script uses a adaptation of SimpleBox v4 to which a local scale with air, water, sediment and soil compartments. The SimnpleBox model is run probabilistically seperately to produce a series of K matrices which are dynamically analysed using this script in combination with probabilistic dynamic emission data.
```{r requirements}
library(openxlsx)
library(deSolve)
library(ggplot2)
library(reshape2)
library(devtools)
library(foreach)
library(doParallel)
# adjust to correct location of SimpleBox xls file
# sb4n.loc <- paste("data/SimpleBox4.0_web_PBT1.xlsm",sep="")
#For installing correct version of openxlsx:
#install_version("openxlsx", version = "4.2.3", repos = "https://cran.us.r-project.org")
```
## required functions
```{r}
# ODE function of SimpleBox:
SimpleBoxODE <- function(t, m, parms) {
with(as.list(c(parms, m)), {
e <- c(rep(0,length(SB.names)))
e[grep("aL",SB.names)] <- ef.a_L(t)
e[grep("wL",SB.names)] <- ef.fw_L(t)
e[grep("sL",SB.names)] <- ef.s_L(t)
e[grep("aR",SB.names)] <- ef.a_R(t)
e[grep("w1R",SB.names)] <- ef.fw_R(t)
e[grep("s2R",SB.names)] <- ef.s_R(t) # soil emission goes to agricultural soil
e[grep("aC",SB.names)] <- ef.a_C(t)
e[grep("w1C",SB.names)] <- ef.fw_C(t)
e[grep("s2C",SB.names)] <- ef.s_C(t) # soil emission goes to agricultural soil
dm <- K %*% m + e
res <- c(dm)
list(res, signal = e)
})
}
f_Soil.wetweight <- function(Conc.soil, # in kg/m3 soil or sediment
Fracw,
Fraca,
RHOsolid){
Conc.soil*1000/(Fracw*1000+(1-Fracw-Fraca)*RHOsolid) # in g/kg (wet) soil
}
f_Soil.dryweight <- function(Conc.soil, # in kg/m3 soil
Fracs,
RHOsolid){
Conc.soil*1000/(Fracs*RHOsolid) # in g/kg (dry) soil
}
f_wet2dryweight <- function(Conc.soil, # in g/kg (wet) soil/sediment
Fracs,
RHOsolid,
Fracw,
RHOw){
Conc.soil*(Fracw*RHOw/(Fracs*RHOsolid)+1) # in g/kg (dry) soil
}
# check and of not available create figures output folder
if (file.exists("figures")){ print("output folder figures exists") } else {
dir.create("figures")
}
```
## Probabilistic Dynamic analysis
```{r main script}
# base SimpleBox file
# sb4n.loc <- "data/SimpleBox4.0_20210402_easylocal_As.xlsm"
sb4n.loc <- "data/@RISK_SimpleBox4DIRECT 21062022.xlsm"
# SB.K <- as.matrix(read.xlsx(sb4n.loc,colNames=FALSE, namedRegion ="K")) # matrix of rate constants "k"
#SB.m0 <- read.xlsx(sb4n.loc,colNames=FALSE, namedRegion ="m0") # Initial mass of each compartment
SB.names <- read.xlsx(sb4n.loc,colNames=FALSE, namedRegion ="box_names") #Names for each compartment
# SB.v <- read.xlsx(sb4n.loc,colNames=FALSE, namedRegion ="v") #Volumes
# start with 0 mass of substance x
SB.m0 <- rep(0,length(SB.names)) # Initial mass of each compartment in kg
# Read in and prepare emission data per compartment and scale
Analysis_name <- "v1.8b"
# Scenario_name <- "III-V_Si_As_NoR"
detectCores()
ncores <- 10 #take care not to set to more than amount of cores available.
# read in emission data
cases <- expand.grid(Recycling=c("NR", "R"),substance=c("As","In", "Ga"),paneltype=c("III-V"))
cases$casename <- paste(cases$substance,cases$Recycling,cases$paneltype,sep="_")
out.data2 <- as.list(NULL)
for(i in 1:length(cases[,1])){
# foreach::foreach()
Emis.Loc <- paste0("data/Emis PV data III-V/PV_emissions_v1.8_LOC_",cases$Recycling[i],"_",cases$substance[i],"_kg.xlsm")
Emis.Reg <- paste0("data/Emis PV data III-V/PV_emissions_v1.8_AM_",cases$Recycling[i],"_",cases$substance[i],"_kg.xlsm")
Emis.Cont <-paste0("data/Emis PV data III-V/PV_emissions_v1.8_EU_",cases$Recycling[i],"_",cases$substance[i],"_kg.xlsm")
prep.emis.v1 <- function(emis.data){ # input is format of xls emission data
emis.data[,1] <- paste0(emis.data[,1], Analysis_name)
row.names(emis.data) <- emis.data[,1]
emis.data[,1 ] <- 0
colnames(emis.data)[1] <- 0
emis.data <- emis.data/(365.25*24*3600)
emis.data # output is formatted emission data for aproxfun + desolve
}
# emission rate is read in and organised
Emis_a_L <- head(prep.emis.v1(read.xlsx(Emis.Loc,colNames=TRUE,sheet = "PROB_Y_a")),1000)
Emis_fw_L <- head(prep.emis.v1(read.xlsx(Emis.Loc,colNames=TRUE,sheet = "PROB_Y_tw")), 1000)
Emis_s_L <- head(prep.emis.v1(read.xlsx(Emis.Loc,colNames=TRUE,sheet = "PROB_Y_s")), 1000)
Emis_a_R <- head(prep.emis.v1(read.xlsx(Emis.Reg,colNames=TRUE,sheet = "PROB_Y_a")), 1000)
Emis_fw_R <- head(prep.emis.v1(read.xlsx(Emis.Reg,colNames=TRUE,sheet = "PROB_Y_tw")), 1000)
Emis_s_R <- head(prep.emis.v1(read.xlsx(Emis.Reg,colNames=TRUE,sheet = "PROB_Y_s")), 1000)
Emis_a_C <- head(prep.emis.v1(read.xlsx(Emis.Cont,colNames=TRUE,sheet = "PROB_Y_a")), 1000)
Emis_fw_C <- head(prep.emis.v1(read.xlsx(Emis.Cont,colNames=TRUE,sheet = "PROB_Y_tw")), 1000)
Emis_s_C <- head(prep.emis.v1(read.xlsx(Emis.Cont,colNames=TRUE,sheet = "PROB_Y_s")), 1000)
# Function for converting emission data to linear interpolation functions for Solver
f.emisfun <- function(Y){
f.emisfun.a <- function(X){
approxfun(data.frame(year = as.numeric(names(X))*(60*60*24*365.25),
emis_kg = as.numeric(X)),rule = 1:2)
}
Y.list <- setNames(split(Y, # Modify names of list elements
seq(nrow(Y))),
rownames(Y))
emisfun.Y <- lapply(Y.list,f.emisfun.a)
emisfun.Y
}
# creates a list of emission data:
Emisdata <- list( Emis_a_L=Emis_a_L,
Emis_fw_L=Emis_fw_L,
Emis_s_L=Emis_s_L,
Emis_a_R=Emis_a_R,
Emis_fw_R=Emis_fw_R,
Emis_s_R=Emis_s_R,
Emis_a_C=Emis_a_C,
Emis_fw_C=Emis_fw_C,
Emis_s_C=Emis_s_C)
Emisfunctions <- lapply(Emisdata,f.emisfun) # converts elements in list to emission functions
ADDRISKdata <- paste0("data/SBMatrix III-V/",cases$substance[i],"_SB4DIRECT @RISK output to K-matrix_21062022.xlsx")
###
f.emisfun <- function(Y){
f.emisfun.a <- function(X){
approxfun(data.frame(year = as.numeric(names(X))*(60*60*24*365.25),
emis_kg = as.numeric(X)),rule = 1:2)
}
Y.list <- setNames(split(Y, # Modify names of list elements
seq(nrow(Y))),
rownames(Y))
emisfun.Y <- lapply(Y.list,f.emisfun.a)
emisfun.Y
}
###
Emisdata <- list( Emis_a_L=Emis_a_L,
Emis_fw_L=Emis_fw_L,
Emis_s_L=Emis_s_L,
Emis_a_R=Emis_a_R,
Emis_fw_R=Emis_fw_R,
Emis_s_R=Emis_s_R,
Emis_a_C=Emis_a_C,
Emis_fw_C=Emis_fw_C,
Emis_s_C=Emis_s_C)
Emisfunctions <- lapply(Emisdata,f.emisfun)
# out.data <- as.list(NULL)
registerDoParallel(ncores) # use multicore, set to the number of our cores
out.data <-
foreach(k=c(1:1000), .inorder=FALSE, .errorhandling = "pass") %dopar% {
library(openxlsx)
Run <- paste0(k, Analysis_name)
## to read SB.K ##
n <- 0:(999)
matrixK.rijstart = 2
matrixK.rij <- matrixK.rijstart+n*39
matrixK.kollom <- 3
SB.K = as.matrix(read.xlsx(ADDRISKdata,
colNames=TRUE,
sheet="Matrixes",
rows = c(matrixK.rij[k]:(matrixK.rij[k]+37)),
cols = c(matrixK.kollom:(matrixK.kollom+37))) )
# testing
# sb4n.loc
# SB.K <- as.matrix(read.xlsx(sb4n.loc,colNames=FALSE, namedRegion ="K"))
parms <- list(K=as.matrix(SB.K),SB.names,Run,
ef.a_L=Emisfunctions[["Emis_a_L"]][[Run]],
ef.fw_L=Emisfunctions[["Emis_fw_L"]][[Run]],
ef.s_L=Emisfunctions[["Emis_s_L"]][[Run]],
ef.a_R=Emisfunctions[["Emis_a_R"]][[Run]],
ef.fw_R=Emisfunctions[["Emis_fw_R"]][[Run]],
ef.s_R=Emisfunctions[["Emis_s_R"]][[Run]],
ef.a_C=Emisfunctions[["Emis_a_C"]][[Run]],
ef.fw_C=Emisfunctions[["Emis_fw_C"]][[Run]],
ef.s_C=Emisfunctions[["Emis_s_C"]][[Run]])
etimes <- c(0:100)*(60*60*24*365.25) #manually set
out <- ode(y=as.numeric(SB.m0),times=c(etimes),func=SimpleBoxODE,parms,rtol=1e-30,atol=1e-7)
colnames(out)[2:38] <- SB.names
list(SBout_kg=out,Run=Run)
# output to out.data list
# list of all the runs. Each out object contains time step, the mass in each compartment (kg) and a set of signals, which are the emission rates for each compartment.
}
stopImplicitCluster()
out.data2[[cases$casename[i]]] <- out.data
addriskinputs <- read.xlsx(ADDRISKdata,
colNames=TRUE,
rowNames = FALSE,
sheet="Raw data output",
rows = c(2:1005),
cols = c( 1369:1526))
out.data2[[cases$casename[i]]]$addriskinput <- addriskinputs
}
# head(out.data2)
# save data for use in R later
save(out.data2, file=paste0("data/20220317RprobDynSB4_III-V_analysis.RData"))
# Stop the clock
runtime <- proc.time() - ptm
print(paste(round(runtime[["elapsed"]]/(60),0),"minutes runtime"))
# load(file=paste0("20220128RprobDynSB4_III-V_analysis.RData"))
# out.data2[["Ga_R_III-V"]]
# length(out.data2)
# names(out.data2)
# for testing
# plot(out)
#head(out.data2)
#str(out.data2)
# str(out.data2[[1]][[1]]$SBout_kg)
#
# plot(out.data2[[1]][[1]]$SBout_kg)
# out.data2$Ga_R[[1]]$SBout_kg
```