Skip to content
Snippets Groups Projects
Commit e8a681b2 authored by Ruben Heinrich's avatar Ruben Heinrich
Browse files

[base] Modified run_simulation so it can be used by ensembles later

parent 31f9a628
No related branches found
No related tags found
1 merge request!6Merge branch 7 fixed functionality into master
...@@ -6,76 +6,105 @@ ...@@ -6,76 +6,105 @@
#'@description Calls OGS6 object validator functions, exports all necessary #'@description Calls OGS6 object validator functions, exports all necessary
#' files and starts OpenGeoSys6 #' files and starts OpenGeoSys6
#'@param ogs6_obj OGS6: Simulation object #'@param ogs6_obj OGS6: Simulation object
#'@param iter_n number: Number of iterations (for simulation chains)
#'@param output_to_log_file flag: Should output be written to a log file? #'@param output_to_log_file flag: Should output be written to a log file?
#'@export #'@export
run_simulation <- function(ogs6_obj, iter_n = 1, output_to_log_file = TRUE) { run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) {
assertthat::assert_that(inherits(ogs6_obj, "OGS6")) assertthat::assert_that(inherits(ogs6_obj), "OGS6")
assertthat::assert_that(assertthat::is.number(iter_n),
iter_n > 0, iter_n < 500)
assertthat::assert_that(assertthat::is.flag(output_to_log_file)) assertthat::assert_that(assertthat::is.flag(output_to_log_file))
#Call all validators # Call all validators
validate_all(ogs6_obj) validate_all(ogs6_obj)
#Export the simulation files # Create the simulation folder
if (!dir.exists(ogs6_obj$sim_path)) {
dir.create(ogs6_obj$sim_path)
} else{
if (length(dir(ogs6_obj$sim_path, all.files = TRUE)) != 0) {
warning(
paste0(
"The defined sim_path directory '",
ogs6_obj$sim_path,
"' is not empty. Files may be overwritten."
),
call. = FALSE
)
}
}
# Export the simulation files
export_gml(ogs6_obj) export_gml(ogs6_obj)
export_prj(ogs6_obj) export_prj(ogs6_obj)
#Construct the system call # Construct the system call
ogs6_call <- paste0(ogs6_obj$ogs_bin_path, ogs6_call <- paste0(
"ogs.exe ", ogs6_obj$ogs_bin_path,
ogs6_obj$sim_path, "ogs.exe ",
ogs6_obj$sim_name, ogs6_obj$sim_path,
".prj -o ", ogs6_obj$sim_name,
ogs6_obj$sim_path) ".prj -o ",
ogs6_obj$sim_path
)
# Finally, make the system call to start the simulation
if (output_to_log_file) {
system(command = setup_logging(ogs6_obj$sim_name,
ogs6_obj$sim_path,
ogs6_call))
} else{
system(command = ogs6_call)
}
system_call <- ogs6_call closeAllConnections()
}
#Direct simulation output to log file
if(output_to_log_file){
# Create logfile directory #===== LOGGING UTILITY =====
logfile_dir <- paste0(ogs6$sim_path, "logfiles/")
if(!dir.exists(logfile_dir)){
dir.create(logfile_dir)
}else{
warning("Logfile directory already exists", call. = FALSE)
}
for(i in seq_len(iter_n)){ #'setup_logging
#'@description Sets up logging.
#'@param sim_name string: Simulation name
#'@param sim_path string: Simulation path
#'@param ogs6_call string: Corresponding OGS6 call
setup_logging <- function(sim_name, sim_path, ogs6_call){
logfile_path <- paste0(logfile_dir, assertthat::assert_that(assertthat::is.string(sim_name))
ogs6_obj$sim_name, assertthat::assert_that(assertthat::is.string(sim_path))
"_log_", iter_n, ".txt") sim_path <- validate_is_dir_path(sim_path)
#Call OGS6 assertthat::assert_that(assertthat::is.string(ogs6_call))
system(command = paste("R CMD BATCH --no-echo",
script_path,
logfile_path))
# read_in_output(ogs6_obj) # Create logfile directory
} logfile_dir <- paste0(sim_path, "logfiles/")
#Write to file... if(!dir.exists(logfile_dir)){
dir.create(logfile_dir)
} }
#Run simulations (and read in output as input) # Create initialization script
for(i in seq_len(iter_n)){ script_path <- paste0(logfile_dir, "sim_init.R")
#Call OGS6
system(command = ogs6_call)
# read_in_output(ogs6_obj) if(!file.exists(script_path)){
file.create(script_path)
} }
closeAllConnections() cmd_str <- paste0("system(command = \"", ogs6_call, "\")")
file_conn <- file(script_path)
writeLines(c(cmd_str), file_conn)
close(file_conn)
# Return string for calling R CMD BATCH
batch_call <- paste0("R CMD BATCH --no-echo ",
script_path, " ",
sim_name, "_log.txt")
return(invisible(batch_call))
} }
#===== VALIDATION UTILITY ===== #===== Validation utility =====
#'validate_all #'validate_all
...@@ -103,7 +132,7 @@ validate_all <- function(ogs6_obj) { ...@@ -103,7 +132,7 @@ validate_all <- function(ogs6_obj) {
} }
#===== CHAINING UTILITY (WIP) ===== #===== Chaining utility (WIP) =====
#'read_in_output #'read_in_output
...@@ -118,3 +147,4 @@ read_in_output <- function(ogs6_obj) { ...@@ -118,3 +147,4 @@ read_in_output <- function(ogs6_obj) {
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