diff --git a/R/sim_utils.R b/R/sim_utils.R index 8588a27e608222e92cffb11ae2c9f26f7fba1a40..3bf35e44121eaa82d8e738b93a502615a10a1b8e 100644 --- a/R/sim_utils.R +++ b/R/sim_utils.R @@ -6,12 +6,12 @@ #'@description Calls OGS6 object validator functions, exports all necessary #' files and starts OpenGeoSys6 #'@param ogs6_obj OGS6: Simulation object -#'@param output_to_log_file flag: Should output be written to a log file? +#'@param write_logfile flag: Should output be written to a logfile? #'@export -run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { +run_simulation <- function(ogs6_obj, write_logfile = TRUE) { - assertthat::assert_that(inherits(ogs6_obj), "OGS6") - assertthat::assert_that(assertthat::is.flag(output_to_log_file)) + assertthat::assert_that(inherits(ogs6_obj, "OGS6")) + assertthat::assert_that(assertthat::is.flag(write_logfile)) # Call all validators validate_all(ogs6_obj) @@ -20,7 +20,7 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { 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) { + if (length(list.files(ogs6_obj$sim_path, all.files = TRUE)) != 0) { warning( paste0( "The defined sim_path directory '", @@ -36,71 +36,44 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { export_gml(ogs6_obj) export_prj(ogs6_obj) - # Construct the system call - ogs6_call <- paste0( - ogs6_obj$ogs_bin_path, - "ogs.exe ", - ogs6_obj$sim_path, - ogs6_obj$sim_name, - ".prj -o ", - ogs6_obj$sim_path - ) + # Construct the call + ogs6_command_str <- paste0(ogs6_obj$ogs_bin_path, "ogs.exe") + sim_path_full <- paste0(ogs6_obj$sim_path, + ogs6_obj$sim_name, + ".prj") + ogs6_args <- c(sim_path_full, "-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) - } - - closeAllConnections() -} - - -#===== LOGGING UTILITY ===== + exit_code <- 0 + # Finally, make the system call to start the simulation + if (write_logfile) { -#'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){ - - assertthat::assert_that(assertthat::is.string(sim_name)) - assertthat::assert_that(assertthat::is.string(sim_path)) - sim_path <- validate_is_dir_path(sim_path) - - assertthat::assert_that(assertthat::is.string(ogs6_call)) + # Create logfile directory + logfile_dir <- paste0(ogs6_obj$sim_path, "logfiles/") - # Create logfile directory - logfile_dir <- paste0(sim_path, "logfiles/") + # Set logfile parameter of simulation object + ogs6_obj$logfile <- paste0(logfile_dir, ogs6_obj$sim_name, "_log.txt") - if(!dir.exists(logfile_dir)){ - dir.create(logfile_dir) - } + if(!dir.exists(logfile_dir)){ + dir.create(logfile_dir) + } - # Create initialization script - script_path <- paste0(logfile_dir, "sim_init.R") + assertthat::assert_that(!file.exists(ogs6_obj$logfile)) + file.create(ogs6_obj$logfile) - if(!file.exists(script_path)){ - file.create(script_path) + exit_code <- system2(command = ogs6_command_str, + args = ogs6_args, + stdout = ogs6_obj$logfile) + } else{ + exit_code <- system2(command = ogs6_command_str, + args = ogs6_args) } - cmd_str <- paste0("system(command = \"", ogs6_call, "\")") - - file_conn <- file(script_path) - writeLines(c(cmd_str), file_conn) - close(file_conn) + closeAllConnections() - # Return string for calling R CMD BATCH - batch_call <- paste0("R CMD BATCH --no-echo ", - script_path, " ", - sim_name, "_log.txt") + cat("\nCaught exit code", exit_code, "\n") - return(invisible(batch_call)) + return(invisible(exit_code)) }