Commit 938a29f9 authored by Forquet Nicolas's avatar Forquet Nicolas
Browse files

* re-organizing code (turn it into function)

* add HP1 support
parent 06699cb1
No related merge requests found
Showing with 118 additions and 39 deletions
+118 -39
# Nicolas Forquet, 2015 # Nicolas Forquet, 2015
new.hydrus.subfolder = function(new.name,reference.folder,hydrus.folder.path){ # new.hydrus.subfolder = function(new.name,reference.folder,hydrus.folder.path){
# new.name : name of the new hydrus subfolder # # new.name : name of the new hydrus subfolder
# reference.folder : folder containing all new hydrus subfolder # # reference.folder : folder containing all new hydrus subfolder
# hydrus.folder : folder containing the reference model obtained with the GUI # # 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 # # 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 # # get the content list from the reference folder
files2copy <- list.files(path = hydrus.folder.path) # files2copy <- list.files(path = hydrus.folder.path)
# create a new folder into the reference folder # # create a new folder into the reference folder
setwd(reference.folder) # setwd(reference.folder)
dir.create(new.name) # dir.create(new.name)
# copy all input files # # copy all input files
file.copy(list.files(pattern = ".exe", ignore.case = TRUE),paste0(new.name,"/",list.files(pattern = ".exe", ignore.case = TRUE))) # 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)){ # for (i in 1:length(files2copy)){
file.copy(paste(hydrus.folder,files2copy[i],sep = "/"),paste(new.name,files2copy[i],sep = "/")) # file.copy(paste(hydrus.folder,files2copy[i],sep = "/"),paste(new.name,files2copy[i],sep = "/"))
} # }
} # }
dummy.white.space = function(char.line){ dummy.white.space = function(char.line){
temp = unlist(strsplit(char.line," ",fixed = TRUE)) temp = unlist(strsplit(char.line," ",fixed = TRUE))
...@@ -109,13 +109,32 @@ write_domain = function(reference.folder, file.name.out = "empty", Axz = "empty" ...@@ -109,13 +109,32 @@ write_domain = function(reference.folder, file.name.out = "empty", Axz = "empty"
return(new_linn) return(new_linn)
} }
level_01.creator = function(new.subfolder.path){ level_01.creator = function(new.subfolder.path, HP1 = FALSE){
# new.name : name of the new hydrus subfolder # new.subfolder.path : the path to which create the level_01.dir file (and the path.dat file if HP1 is in use)
# reference.folder : folder containing all new hydrus subfolder file.create(paste(new.subfolder.path,"/Level_01.dir",sep=""))
level01.file = file(paste(new.subfolder.path,"/Level_01.dir",sep=""),"w") 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) writeLines(new.subfolder.path,level01.file)
close(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){ modify.selector = function(new.name,hydrus.folder,data,header.size = 24, end.size = 65){
......
# specify the location of the rhydrus folder: hydrus.batch <- function(parameter.file, HP1 = FALSE){
rhydrus.folder <- "C:/Users/forquet/Documents/script_R/rhydrus" #--------- load necessary libraries ----------#
# source the file with functions dedicated to file manipulation library(fs)
filename <- paste0(rhydrus.folder,"/file_manip.R") library(rjson)
source(filename) library(tidyverse)
# This function process the output of the script .bat that estimates processor usage. #---- check if the parameter file exists ----#
output.bat <- function(output){ if (file.exists(path = parameter.file) == FALSE){
usage <- rep(0,5) stop("invalid parameter file path")
for (i in 1:5){ }
usage[i] <- as.numeric(strsplit(output[4+i],split='"',fixed=TRUE)[[1]][4])
#----------- 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 # set the working directory to main.directory
setwd(main.directory) setwd(main.directory)
......
# This function process the output of the script .bat that estimates processor 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)
#}
# This function process the output of the script .bat that estimates processor usage.
output.bat <- function(output){ output.bat <- function(output){
usage <- as.numeric(trimws(strsplit(output[2],"\r"))) usage <- rep(0,5)
return(usage) 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 # This function creates the file tree necessary to run simulation
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment