library(tools)library(gen3sis)# if from comand line, read options# optparse is not installed on hyperion# try loading it, if it fails retry with a local library folderif(!library(optparse,logical.return=T)){#library was not found, try local copylibrary(optparse,lib.loc="~/lib/R")}option_list=list(make_option(c("-c","--config_file"),action="store",default=NA,type='character',help="configuration file for experiment run"),make_option(c("-i","--input_directory"),action="store",default=NA,type='character',help="input directory for simulation data \tnot set: derive input folder from config file path \t\"path\": use provided path (absolute or relative)"),make_option(c("-o","--output_directory"),action="store",default=NA,type='character',help="output directory for simulation results \tnot set: derive output folder from config file path \t\"path\": use provided path (absolute or relative)"),make_option(c("-m","--move_output_to_target"),action="store",default=NA,type='character',help="directory where final simulation data is moved to \tnot set: don't move \t\"path\": prepend provided path to [output_directory]"),make_option(c("-t","--timestep_restart"),action="store",default=NA,type='character',help="Set the start time timestep: \tnot set: start at the beginning, \t\"ti\" \tto start from the last available timestep, \t\"x\" \tto start from timestep x "),make_option(c("-s","--save_intermediate_results"),action="store",default=NA,type='character',help="save intermediate species and landscapes: \tnot set: save only initial (t_start) and final timesteps (t_end), \t\"x\" \t save x timesteps between t_start and t_end, \t\"all\" \t save all intermediate timesteps"),make_option(c("-f","--save_state"),action="store",default=NA,type='character',help="save intermediate simulation state for restore points: \tnot set: do not save any state, \t\"last\" \t save only the last timestep, \t\"all\" \t save all intermediate timesteps"),make_option(c("-r","--redirect_output"),action="store_true",default=FALSE,help="print the output to the file [gasm.out] in the output directory"),make_option(c("-p","--enable_profiling"),action="store_true",default=FALSE,help="enable profiling [default %default]"),make_option(c("-g","--enable_gc"),action="store_true",default=FALSE,help="enable extra garbage colletion steps on memory critical operations"),make_option(c("-v","--verbose"),action="store_true",default=FALSE,help="Print extra stuff out [default %default]"))parser<-OptionParser(option_list=option_list,add_help_option=TRUE)opt=parse_args(parser)if(opt$verbose){print("parsed option arguments:")print(opt)}if(opt$enable_profiling){options("keep.source"=TRUE)}dir<-gen3sis::prepare_directories(opt$config_file,opt$input_directory,opt$output_directory)if(opt$redirect_output){sink(file=file(file.path(dir$output,"gasm.out"),open="w"))}if(opt$enable_profiling){Rprof(filename=file.path(dir$output,"profiling.out"),memory.profiling=TRUE,gc.profiling=TRUE,line.profiling=TRUE)}sim<-run_simulation(config=opt$config_file,landscape=opt$input_directory,output_directory=opt$output_directory,save_state=NA,call_observer="all",enable_gc=opt$enable_gc,verbose=1)if(opt$enable_profiling){Rprof(NULL)}if(opt$redirect_output){sink()# close redirectcloseAllConnections()# force close, bug fix for win/Rstudion}# move output folderif(!is.na(opt$move_output_to_target)){target=file.path(opt$move_output_to_target,dir$output)dir.create(target,recursive=TRUE,showWarnings=FALSE)files<-list.files(dir$output,full.names=T)if(all(file.copy(files,target,recursive=T,copy.date=T))){unlink(dir$output,recursive=T)}}