Skip to content

Commit 48f4b5f

Browse files
authored
Release 2026.3.1 (#259)
Bug fixes: ChemClass variable was missing as input in two functions, and Fakelib was not read in correctly in parallel functions Enhancement: Rescale FRACinf in k_Leaching if inputs are very high
2 parents af12586 + 6077b0c commit 48f4b5f

5 files changed

Lines changed: 55 additions & 13 deletions

File tree

R/fParallel.R

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,29 @@ solveInParallelSteadyState <- function(max_runs_per_slice,
7676
registerDoParallel(cl)
7777

7878
# Define the worker function for each slice
79-
processSlice <- function(i) {
79+
processSlice <- function(i, SBooDataLocation) {
8080
# Load fakelib
8181
if (!is.null(SBooDataLocation) && !is.na(SBooDataLocation) && SBooDataLocation != "") {
82-
source(file.path(SBooDataLocation, "baseScripts/fakeLib.R"))
82+
# code take from fakeLib.R in SBooScripts/basescripts
83+
library(tidyverse)
84+
library(ggdag) #for plotting DAG graphs
85+
library(R6)
86+
library(rlang)
87+
#path to the SBoo package
88+
Path2PackageSource <- file.path(SBooDataLocation,"../SBoo")
89+
#source all R files and load data from the package
90+
Dfiles <- list.files(paste(Path2PackageSource, "data", sep = "/"), pattern = "\\.rda$")
91+
Rded <- lapply(Dfiles, function(x) {
92+
Dfilename <- paste(Path2PackageSource, "data", x, sep = "/")
93+
if (exists("verbose") && verbose) cat(Dfilename, "\n")
94+
load(Dfilename, envir = global_env())
95+
})
96+
Rfiles <- list.files(paste(Path2PackageSource, "R", sep = "/"), pattern = "\\.R$")
97+
sourced <- lapply(Rfiles, function(x) {
98+
Rfilename <- paste(Path2PackageSource, "R", x, sep = "/")
99+
if (exists("verbose") && verbose) cat(Rfilename, "\n")
100+
source(Rfilename)
101+
})
83102
} else {
84103
source("baseScripts/fakeLib.R")}
85104

@@ -113,7 +132,7 @@ solveInParallelSteadyState <- function(max_runs_per_slice,
113132

114133
# Execute in parallel using foreach
115134
combinedResults <- foreach(i = seq_len(nSlices)) %dopar% {
116-
processSlice(i)
135+
processSlice(i, SBooDataLocation)
117136
}
118137

119138
# Stop the cluster
@@ -222,10 +241,29 @@ solveInParallelDynamic <- function(max_runs_per_slice,
222241
cl <- parallel::makeCluster(nCores)
223242
doParallel::registerDoParallel(cl)
224243

225-
processSlice <- function(i) {
244+
processSlice <- function(i, SBooDataLocation) {
226245
# Load fakelib
227246
if (!is.null(SBooDataLocation) && !is.na(SBooDataLocation) && SBooDataLocation != "") {
228-
source(file.path(SBooDataLocation, "baseScripts/fakeLib.R"))
247+
# code take from fakeLib.R in SBooScripts/basescripts
248+
library(tidyverse)
249+
library(ggdag) #for plotting DAG graphs
250+
library(R6)
251+
library(rlang)
252+
#path to the SBoo package
253+
Path2PackageSource <- file.path(SBooDataLocation,"../SBoo")
254+
#source all R files and load data from the package
255+
Dfiles <- list.files(paste(Path2PackageSource, "data", sep = "/"), pattern = "\\.rda$")
256+
Rded <- lapply(Dfiles, function(x) {
257+
Dfilename <- paste(Path2PackageSource, "data", x, sep = "/")
258+
if (exists("verbose") && verbose) cat(Dfilename, "\n")
259+
load(Dfilename, envir = global_env())
260+
})
261+
Rfiles <- list.files(paste(Path2PackageSource, "R", sep = "/"), pattern = "\\.R$")
262+
sourced <- lapply(Rfiles, function(x) {
263+
Rfilename <- paste(Path2PackageSource, "R", x, sep = "/")
264+
if (exists("verbose") && verbose) cat(Rfilename, "\n")
265+
source(Rfilename)
266+
})
229267
} else {
230268
source("baseScripts/fakeLib.R")}
231269

@@ -266,7 +304,7 @@ solveInParallelDynamic <- function(max_runs_per_slice,
266304
# browser()
267305
# Define parallel execution and combine results with `foreach`
268306
combinedResults <- foreach(i = seq_len(nSlices)) %dopar% {
269-
processSlice(i)
307+
processSlice(i, SBooDataLocation)
270308
}
271309

272310
stopCluster(cl)

R/k_Leaching.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
#'@param SpeciesName considered species
77
#'@param penetration_depth_s penetration depth for correction factor, based on Hollander et al. (2007) https://doi.org/10.1016/j.envpol.2006.09.018 [m]
88
#'@param Kscompw dimensionless partitioning coefficient soil-water [-]
9-
#'@return Leaching of aggregated (A) or free (S) enp species from natural soil #[s-1]
9+
#'@description Note: The fraction infiltration (FRACinf) is rescaled to the rest of inflow from rain based on the fraction rain going to runoff (FRACrun).
10+
#'@return Leaching rate constant of compound from soil out of the system (e.g. no deep(er) soil layer considered). [s-1]
1011
#'@export
11-
k_Leaching <- function(FRACinf, RAINrate, VertDistance, SpeciesName,
12+
k_Leaching <- function(FRACinf,FRACrun, RAINrate, VertDistance, SpeciesName,
1213
penetration_depth_s, Kscompw){ #k_ Leaching
1314

1415
if(SpeciesName %in% c("Aggregated", "Nanoparticle") ){
1516
#Correction factor depth dependent soil concentration
1617
CORRleach <- f_CORRsoil(VertDistance, relevant_depth_s=0.5, penetration_depth_s)
17-
18+
#If due to extreme inputs, FRACinf and FRACrun are both too large, scale FRACinf to mass conserve rain
19+
if((FRACinf + FRACrun) > 1) {FRACinf = 1-FRACrun}
1820
#Leaching of aggregated (A) and free (S) enp species from natural soil [s-1]
1921
return( (FRACinf * RAINrate * CORRleach) / VertDistance )
2022
} else if( SpeciesName %in% c("Molecular")) {
21-
CORRleach <- f_CORRsoil(VertDistance, relevant_depth_s=0.5, penetration_depth_s=0.1)
23+
CORRleach <- f_CORRsoil(VertDistance, relevant_depth_s=0.5, penetration_depth_s=0.1)
24+
#If due to extreme inputs, FRACinf and FRACrun are both too large, scale FRACinf to conserve rain
25+
if((FRACinf + FRACrun) > 1) {FRACinf = 1-FRACrun}
2226
return(FRACinf*RAINrate/Kscompw*CORRleach/VertDistance)
2327
} else return(NA)
2428

R/v_KocAltDorC.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @param pKa Dissociation constant of (conjugated) acid (default = 7)
1010

1111
#' @export
12-
KocAltDorC <- function (Kow, a, b, pKa, KocAlt){
12+
KocAltDorC <- function (Kow, a, b, pKa, KocAlt, ChemClass){
1313

1414
if (is.na(KocAlt) || KocAlt == "NA") {
1515
if (is.na(pKa) || pKa == "NA"){

R/v_KocDorC.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param Koc Organic Carbon partitioning coefficient in
99
#' @param pKa Dissociation constant of (conjugated) acid (default = 7)
1010
#' @export
11-
KocDorC <- function (Kow, a, b, Koc){
11+
KocDorC <- function (Kow, a, b, Koc, ChemClass){
1212

1313
if (is.na(Koc) || Koc == "NA") {
1414
switch(ChemClass,

R/v_KswDorC.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param pKa Dissociation constant of (conjugated) acid (default = 70
1111
#' @param Ksw soil water partitioning coefficient in data
1212
#' @export
13-
KswDorC <- function (KocDorC, CorgStandard, all.rhoMatrix, Ksw){
13+
KswDorC <- function (KocDorC, CorgStandard, all.rhoMatrix, Ksw, ChemClass){
1414
RHOsolid <- all.rhoMatrix$rhoMatrix[all.rhoMatrix$SubCompart == "naturalsoil"]
1515

1616
if (is.na(Ksw) || Ksw == "NA") {

0 commit comments

Comments
 (0)