Skip to content
Snippets Groups Projects
Commit f6a9623d authored by phit0's avatar phit0
Browse files

[base] update_component() method for OGS6

Tests that make sure the function only accepts valid r2ogs6 components in the right format
parent f55a1fb6
No related branches found
No related tags found
1 merge request!28update_component() method for OGS6
...@@ -124,6 +124,39 @@ OGS6 <- R6::R6Class("OGS6", ...@@ -124,6 +124,39 @@ OGS6 <- R6::R6Class("OGS6",
invisible(self) invisible(self)
}, },
#' @description
#' Update a component of the \code{OGS6} object.
#' @param cmpts list(sublist, length(sublist) == 2): The first element
#' of a sublist is a character that calls an \code{OGS6} component, the
#' second one is the corresponding value.
#' @examples
#' \dontrun{ogs6_obj$update_component(list(
#' list("ogs6_obj$parameters[[1]]$value", 2.3),
#' list("ogs6_obj$media[[1]]$properties[[2]]$value",
#' 1.0e-3)))}
update_component = function(cmpts){#cmpts=list(list(call_str, value))
assertthat::assert_that(is.list(cmpts))
for (i in seq_along(cmpts)){
# check sublists
assertthat::assert_that(is.list(cmpts[[i]]))
assertthat::assert_that(length(cmpts[[i]])==2)
assertthat::assert_that(is.character(cmpts[[i]][[1]]))
# update component via call
call_str <- cmpts[[i]][[1]]
value <- cmpts[[i]][[2]]
call_str <- gsub("^[A-Za-z_0-9]*\\$", "self$", call_str)
assertthat::assert_that(!is.null(eval(parse(text = call_str))),
msg = paste(call_str,
"not found in ogs6_obj",
self$sim_name))
set_call <- paste0(call_str, " <- ", value)
eval(parse(text = set_call))
}
invisible(self)
},
#===== Utility ===== #===== Utility =====
......
ogs6_obj <- OGS6$new(sim_name = "theis", tmp_dir)
prj_path <- system.file("extdata/benchmarks/AxiSymTheis",
"axisym_theis.prj", package = "r2ogs6")
read_in_prj(ogs6_obj, prj_path)
test_that("Only existing components can be changed", {
expect_error(ogs6_obj$update_component(
list(list("ogs6_obj$media[[1]]$materials", 0.003))
))
expect_error(ogs6_obj$update_component(
list(list("ogs6_obj$media[[1]$properties", 0.003))
))
})
test_that("Only accepts parameters in right format", {
expect_error(ogs6_obj$update_component(
list("ogs6_obj$media[[1]]$properties", 0.003)
))
expect_error(ogs6_obj$update_component(
list(c("ogs6_obj$media[[1]]$properties", 0.003),
c("ogs6_obj$media[[1]]$phases[[1]]$properties[[1]]$value", 0.03))
))
expect_silent(ogs6_obj$update_component(
list(list("ogs6_obj$media[[1]]$properties", 0.003),
list("ogs6_obj$media[[1]]$phases[[1]]$properties[[1]]$value", 0.03))
))
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment