diff --git a/hydrus/file_manip.R b/hydrus/file_manip_fcn.R similarity index 78% rename from hydrus/file_manip.R rename to hydrus/file_manip_fcn.R index c26d65585e8f52ed2cbefb796078392500f5d56d..e76c162d60be5de91afdcca2827de4f994382c9d 100644 --- a/hydrus/file_manip.R +++ b/hydrus/file_manip_fcn.R @@ -1,22 +1,22 @@ # Nicolas Forquet, 2015 -new.hydrus.subfolder = function(new.name,reference.folder,hydrus.folder.path){ - # new.name : name of the new hydrus subfolder - # reference.folder : folder containing all new hydrus subfolder - # hydrus.folder : folder containing the reference model obtained with the GUI - # NOTE: the reference folder must contain a copy of the Hydrus batch executable file : H2D_Calc.exe - - # get the content list from the reference folder - files2copy <- list.files(path = hydrus.folder.path) - # create a new folder into the reference folder - setwd(reference.folder) - dir.create(new.name) - # copy all input files - file.copy(list.files(pattern = ".exe", ignore.case = TRUE),paste0(new.name,"/",list.files(pattern = ".exe", ignore.case = TRUE))) - for (i in 1:length(files2copy)){ - file.copy(paste(hydrus.folder,files2copy[i],sep = "/"),paste(new.name,files2copy[i],sep = "/")) - } -} +# new.hydrus.subfolder = function(new.name,reference.folder,hydrus.folder.path){ +# # new.name : name of the new hydrus subfolder +# # reference.folder : folder containing all new hydrus subfolder +# # hydrus.folder : folder containing the reference model obtained with the GUI +# # NOTE: the reference folder must contain a copy of the Hydrus batch executable file : H2D_Calc.exe +# +# # get the content list from the reference folder +# files2copy <- list.files(path = hydrus.folder.path) +# # create a new folder into the reference folder +# setwd(reference.folder) +# dir.create(new.name) +# # copy all input files +# file.copy(list.files(pattern = ".exe", ignore.case = TRUE),paste0(new.name,"/",list.files(pattern = ".exe", ignore.case = TRUE))) +# for (i in 1:length(files2copy)){ +# file.copy(paste(hydrus.folder,files2copy[i],sep = "/"),paste(new.name,files2copy[i],sep = "/")) +# } +# } dummy.white.space = function(char.line){ temp = unlist(strsplit(char.line," ",fixed = TRUE)) @@ -109,13 +109,32 @@ write_domain = function(reference.folder, file.name.out = "empty", Axz = "empty" return(new_linn) } -level_01.creator = function(new.subfolder.path){ - # new.name : name of the new hydrus subfolder - # reference.folder : folder containing all new hydrus subfolder +level_01.creator = function(new.subfolder.path, HP1 = FALSE){ + # new.subfolder.path : the path to which create the level_01.dir file (and the path.dat file if HP1 is in use) + file.create(paste(new.subfolder.path,"/Level_01.dir",sep="")) level01.file = file(paste(new.subfolder.path,"/Level_01.dir",sep=""),"w") - #working.directory = paste(reference.folder,"/",new.name,sep="") writeLines(new.subfolder.path,level01.file) close(level01.file) + if (HP1 == TRUE){ + file.create(paste(new.subfolder.path,"/path.dat",sep="")) + path.file = file(paste(new.subfolder.path,"/path.dat",sep=""),"w") + writeLines(new.subfolder.path,path.file) + close(path.file) + } +} + +exe_copy = function(hydrus.path, new.subfolder.path, HP1 = FALSE){ + path <- paste0(hydrus.path, "/H1D_CALC.EXE") + new.path <- paste0(new.subfolder.path, "/H1D_CALC.EXE") + file_copy(path, new.path, overwrite = TRUE) + if (HP1 == TRUE){ + path <- paste0(hydrus.path, "/HP1.exe") + new.path <- paste0(new.subfolder.path, "/HP1.exe") + file_copy(path, new.path, overwrite = TRUE) + path <- paste0(hydrus.path, "/Phreeqc.dll") + new.path <- paste0(new.subfolder.path, "/Phreeqc.dll") + file_copy(path, new.path, overwrite = TRUE) + } } modify.selector = function(new.name,hydrus.folder,data,header.size = 24, end.size = 65){ diff --git a/hydrus/main_script.R b/hydrus/main_script.R index d37eea0cfa4b83b44251661a3091a9ae55a74a08..9d012df1c57929b97fdefe9b695a67105e5cfd33 100644 --- a/hydrus/main_script.R +++ b/hydrus/main_script.R @@ -1,22 +1,73 @@ -# specify the location of the rhydrus folder: -rhydrus.folder <- "C:/Users/forquet/Documents/script_R/rhydrus" -# source the file with functions dedicated to file manipulation -filename <- paste0(rhydrus.folder,"/file_manip.R") -source(filename) - -# This function process the output of the script .bat that estimates processor usage. -output.bat <- function(output){ - usage <- rep(0,5) - for (i in 1:5){ - usage[i] <- as.numeric(strsplit(output[4+i],split='"',fixed=TRUE)[[1]][4]) +hydrus.batch <- function(parameter.file, HP1 = FALSE){ + #--------- load necessary libraries ----------# + library(fs) + library(rjson) + library(tidyverse) + + #---- check if the parameter file exists ----# + if (file.exists(path = parameter.file) == FALSE){ + stop("invalid parameter file path") + } + + #----------- import parameters -------------# + parameter <- fromJSON(file = parameter.file) + + #------------ Check parameter file ------------# + + # check if the working folder is valid + if (dir.exists(path = parameter$working.folder) == FALSE){ + stop("invalid working folder path") + } + + # check if the rhydrus folder is valid + if (dir.exists(path = parameter$rhydrus.folder) == FALSE){ + stop("invalid rhydrus folder path") + } + + # check if the hydrus folder is valid + if (dir.exists(path = parameter$hydrus.folder) == FALSE){ + stop("invalid hydrus folder path") + } + + # check the reference model folder is valid: + if (dir.exists(path = parameter$reference.model.folder) == FALSE){ + stop("invalid reference model path") + } + + # convert simulation number to integer and check for valid output + parameter$simulation.number <- parse_integer(parameter$simulation.number) + if(is.na(parameter$simulation.number) == TRUE){ + stop("invalid format for simulation.number") + } + + + #--------- load rhydrus functions --------# + + # source the file with the functions dedicated to file manipulation + filename <- paste0(parameter$rhydrus.folder,"/file_manip_fcn.R") + source(filename) + + # source the file with the functions dedicated to run simulations + filename <- paste0(parameter$rhydrus.folder,"/simulation_fcn.R") + source(filename) + + #-------- create file tree --------# + + for (i in 1:parameter$simulation.number){ + # copy the reference model folder + dir_copy(path = parameter$reference.model.folder, + new_path = paste0(parameter$working.folder,"/","simulation","_",as.character(i)), overwrite = TRUE) + + # modify input files + + # create level_01.dir (and path.dat) + level_01.creator(paste0(parameter$working.folder,"/","simulation","_",as.character(i)), HP1 = HP1) + # and copy executable file(s) + exe_copy(parameter$hydrus.folder, paste0(parameter$working.folder,"/","simulation","_",as.character(i)), HP1 = HP1) + } - mean_usage <- mean(usage) - return(mean_usage) } -# repository that contains all Hydrus folders -main.directory <- "C:/Users/forquet/Documents/script_R/rhydrus_demo/PF_1pulse" - # set the working directory to main.directory setwd(main.directory) diff --git a/hydrus/simulation_fcn.R b/hydrus/simulation_fcn.R index 3b0863d5cd9a3350e0b5b66cf4b91f62424b7cca..066b936d3053fc008a88d8b9e1541757586cd746 100644 --- a/hydrus/simulation_fcn.R +++ b/hydrus/simulation_fcn.R @@ -1,7 +1,16 @@ # This function process the output of the script .bat that estimates processor usage. +#output.bat <- function(output){ +# usage <- as.numeric(trimws(strsplit(output[2],"\r"))) +# return(usage) +#} +# This function process the output of the script .bat that estimates processor usage. output.bat <- function(output){ - usage <- as.numeric(trimws(strsplit(output[2],"\r"))) - return(usage) + usage <- rep(0,5) + for (i in 1:5){ + usage[i] <- as.numeric(strsplit(output[4+i],split='"',fixed=TRUE)[[1]][4]) + } + mean_usage <- mean(usage) + return(mean_usage) } # This function creates the file tree necessary to run simulation