#' Function to execute Comsol Modell with defined parameters
#'
#' @param modelname name of the Comsol Modell without .mph ending
#' @param outfile_new name that the COMSOL outputfile is changed to from this function
#' @param outfile_raw name that COMSOL gives the outputfile
#' @param COMSOL_progammpath path to COMSOL project file
#' @param COMSOL_exepath path to comsolbatch.exe
#' @param job name of the job that is defined in the COMSOL project file
#' @param overwrite should existing modellresults be overwritten or not
#' @param input_pars named vector or dataframe where names represent Parameter names as defined in Comsol
#'
#' @return
#' @export
#'
#' @examples comsol_exe(model="Produktionseimer",input_pars=input_pars_9,outfile="CO2_flux_prod_9.txt")
comsol_exe <- function(modelname,
input_pars = NULL,
outfile_new = NULL,
outfile_raw = "CO2_flux_prod.txt",
COMSOL_progammpath = COMSOL_progammpfad,
COMSOL_exepath = COMSOL_exepfad,
job = NULL,
study =NULL,
overwrite_model = F,
overwrite = F) {
#new name for outputfile with path
outfile_new_full <- paste0(comsolpfad,outfile_new)
#check if outpufile already exists
if(overwrite == F & !is.null(outfile_new) & file.exists(outfile_new_full)){
print(paste(outfile_new,"already exists set overwrite = T to replace it"))
}else{
job_str <- ifelse(is.null(job),"",paste("-job",job))
study_str <- ifelse(is.null(study),"",paste("-study",study))
#string that is parsed to commandline
if(overwrite_model){
cmd <- paste0("cd ",COMSOL_exepath,"&& comsolbatch.exe -inputfile ",COMSOL_progammpath,modelname,".mph -outputfile ",COMSOL_progammpath,modelname," ",job_str,study_str)
}else{
cmd <- paste0("cd ",COMSOL_exepath,"&& comsolbatch.exe -inputfile ",COMSOL_progammpath,modelname,".mph -outputfile ",COMSOL_progammpath,modelname,"_solved.mph ",job_str,study_str)
}
#if input pars exists save them to .txt
if(!is.null(input_pars)){
par_file <- paste0(comsolpfad,"input_pars.txt")
if(!is.data.frame(input_pars)){
input_pars <- t(input_pars)
}
write.table(input_pars,file=par_file,row.names = F,quote = F,sep = " ")
#add -paramfile path/input_pars.txt to the commandline string so that comsol knows where to look for the parameters
cmd <- paste0(cmd," -paramfile ",par_file)
}
#commandline befehl ausführen
shell(cmd,translate=T,intern=F)
#name of the outpufile as it is generated by COMSOL
outfile_raw_full <- paste0(comsolpfad,outfile_raw)
if(!is.null(outfile_new)){
if(file.exists(outfile_raw_full)){
#change the name of the Outputfile to outfile_new
file.rename(outfile_raw_full,outfile_new_full)
}else{
print("no outfile found")
}
}
}
}
# input_pars <- c("prod_1"=1.2,"prod_2"=1.3,"prod_3"=0)
# modelname <- "Produktionseimer"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.