3838# ' @param output_path Argument is deprecated as of version 1.0; use sim_path
3939# ' instead.
4040# '
41+ # ' @param debug Logical. If `TRUE`, only the command to run LPJmL is printed
42+ # ' and parallelization is switched off.
43+ # '
4144# ' @return See `x`, extended by columns `"type"`, `"job_id"` and `"status"`.
4245# '
4346# ' @details
164167# '
165168# ' @md
166169# ' @export
167- run_lpjml <- function (x ,
170+ run_lpjml <- function (x , # nolint:cyclocomp_linter.
168171 model_path = " ." ,
169172 sim_path = NULL ,
170- run_cmd = " srun --propagate " ,
173+ run_cmd = " mpirun " ,
171174 parallel_cores = 1 ,
172175 write_stdout = FALSE ,
173176 raise_error = TRUE ,
174- output_path = NULL ) {
177+ output_path = NULL ,
178+ debug = FALSE ) {
175179
176180 warn_runner_os(" run_lpjml" )
177181
182+ if (run_cmd != " " && ! grepl(" $" , run_cmd )) {
183+ run_cmd <- paste0(run_cmd , " " )
184+ message(" Note: Added trailing space to run_cmd" )
185+ }
186+
178187 # Check if model_path is set or unit test flag provided
179188 if (! dir.exists(model_path )) {
180189 stop(" Folder of model_path \" " , model_path , " \" does not exist." )
@@ -212,9 +221,10 @@ run_lpjml <- function(x,
212221 for (order in unique(sort(x $ order ))) {
213222 sim_names <- x $ sim_name [which(x $ order == order )]
214223
215- if (parallel_cores == 1 ) {
224+ if (parallel_cores == 1 || debug ) { # nolint:undesirable_function_linter.
216225 do_sequential(
217- sim_names , model_path , sim_path , run_cmd , write_stdout , raise_error
226+ sim_names , model_path , sim_path , run_cmd , write_stdout , raise_error ,
227+ debug = debug # nolint:undesirable_function_linter.
218228 )
219229 } else if (parallel_cores > 1 && Sys.getenv(" SLURM_JOB_ID" ) != " " ) {
220230 do_parallel(
@@ -230,9 +240,10 @@ run_lpjml <- function(x,
230240 }
231241
232242 } else {
233- if (parallel_cores == 1 ) {
243+ if (parallel_cores == 1 || debug ) { # nolint:undesirable_function_linter.
234244 do_sequential(
235- x $ sim_name , model_path , sim_path , run_cmd , write_stdout , raise_error
245+ x $ sim_name , model_path , sim_path , run_cmd , write_stdout , raise_error ,
246+ debug = debug # nolint:undesirable_function_linter.
236247 )
237248 } else if (parallel_cores > 1 && Sys.getenv(" SLURM_JOB_ID" ) != " " ) {
238249 do_parallel(
@@ -258,7 +269,8 @@ do_run <- function(sim_name,
258269 sim_path ,
259270 run_cmd ,
260271 write_stdout ,
261- raise_error ) {
272+ raise_error ,
273+ debug ) {
262274
263275 config_file <- paste0(" config_" ,
264276 sim_name ,
@@ -279,6 +291,9 @@ do_run <- function(sim_name,
279291 sim_path ,
280292 " /configurations/" ,
281293 config_file )
294+ if (debug ) { # nolint:undesirable_function_linter.
295+ message(" Debug mode is on, only printing command:\n " , inner_command )
296+ }
282297
283298 stdout_file <- ifelse(write_stdout ,
284299 paste0(sim_path ,
@@ -328,7 +343,8 @@ do_sequential <- function(sim_names,
328343 sim_path ,
329344 run_cmd ,
330345 write_stdout ,
331- raise_error ) {
346+ raise_error ,
347+ debug ) {
332348
333349 # tryCatch to unset and set MPI for function call outside of slurm job on
334350 # an HPC cluster even when function call is interrupted or has thrown
@@ -343,12 +359,13 @@ do_sequential <- function(sim_names,
343359 mpi_var <- NULL
344360 # If not specified by the user set number of processes to 1 to run lpjml
345361 # in interactive mode
346- if (run_cmd == " mpirun" ) {
362+ if (run_cmd == " mpirun " && Sys.getenv( " SLURM_JOB_ID " ) == " " ) {
347363 run_cmd <- paste0(run_cmd , " -np 1 " )
348364 }
349365 }
350366 for (sim_name in sim_names ) {
351- do_run(sim_name , model_path , sim_path , run_cmd , write_stdout , raise_error )
367+ do_run(sim_name , model_path , sim_path , run_cmd , write_stdout , raise_error ,
368+ debug = debug ) # nolint:undesirable_function_linter.
352369 }
353370 }, finally = {
354371 # Check if slurm is available
0 commit comments