From e8a681b2dc9bba5ffa0044fd37f3528038f8e383 Mon Sep 17 00:00:00 2001
From: aheinri5 <Anna@netzkritzler.de>
Date: Wed, 16 Dec 2020 17:16:07 +0100
Subject: [PATCH] [base] Modified run_simulation so it can be used by ensembles
 later

---
 R/sim_utils.R | 118 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 74 insertions(+), 44 deletions(-)

diff --git a/R/sim_utils.R b/R/sim_utils.R
index 835a6ae..8588a27 100644
--- a/R/sim_utils.R
+++ b/R/sim_utils.R
@@ -6,76 +6,105 @@
 #'@description Calls OGS6 object validator functions, exports all necessary
 #' files and starts OpenGeoSys6
 #'@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?
 #'@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(assertthat::is.number(iter_n),
-                            iter_n > 0, iter_n < 500)
+    assertthat::assert_that(inherits(ogs6_obj), "OGS6")
     assertthat::assert_that(assertthat::is.flag(output_to_log_file))
 
-    #Call all validators
+    # Call all validators
     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_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 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
+    )
+
+    # 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
-        logfile_dir <- paste0(ogs6$sim_path, "logfiles/")
+#===== LOGGING UTILITY =====
 
-        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,
-                                   ogs6_obj$sim_name,
-                                   "_log_", iter_n, ".txt")
+    assertthat::assert_that(assertthat::is.string(sim_name))
+    assertthat::assert_that(assertthat::is.string(sim_path))
+    sim_path <- validate_is_dir_path(sim_path)
 
-            #Call OGS6
-            system(command = paste("R CMD BATCH --no-echo",
-                                   script_path,
-                                   logfile_path))
+    assertthat::assert_that(assertthat::is.string(ogs6_call))
 
-            # 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)
-    for(i in seq_len(iter_n)){
-
-        #Call OGS6
-        system(command = ogs6_call)
+    # Create initialization script
+    script_path <- paste0(logfile_dir, "sim_init.R")
 
-        # 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
@@ -103,7 +132,7 @@ validate_all <- function(ogs6_obj) {
 }
 
 
-#===== CHAINING UTILITY (WIP) =====
+#===== Chaining utility (WIP) =====
 
 
 #'read_in_output
@@ -118,3 +147,4 @@ read_in_output <- function(ogs6_obj) {
 
 
 
+
-- 
GitLab