diff --git a/DESCRIPTION b/DESCRIPTION index 3b39212..b2b5b55 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: growR Type: Package -Version: 1.0.0.9008 +Version: 1.0.9.9000 Date: 2023-09-27 Authors@R: person( given = "Kevin", diff --git a/NAMESPACE b/NAMESPACE index d8ae239..be86731 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ S3method("*",FunctionalGroup) S3method("+",FunctionalGroup) +S3method(plot,ModvegeSite) export(FG_A) export(FG_B) export(FG_C) diff --git a/NEWS.md b/NEWS.md index 41555f8..d692fc7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,9 @@ concerns `ModvegeSite$plot()` and `analyze_parameter_scan()` as well as the `compare.R` script. +* S3 dispatch for plot method of `ModvegeSite` objects -> `plot(mvs)` is now + possible if `mvs` is a `ModvegeSite` instance. + ## Changed * Input data CSV files are now actual CSV files, instead of @@ -22,17 +25,23 @@ `check_parameters` now only throws an error if any of the really *required* parameters are missing. +## Fixed + +* autocut: `get_annual_gross_yield` was incorrectly hardcoded to return 1. + ## Removed * Removed superfluous weather inputs. * Removed automatic temperature *correction*. +* Redundant argument *store_results* in `growR_run_loop`. + # growR 1.0.0 * Initial CRAN submission. -# grower 0.0.1 +# growR 0.0.1 * Renaming from `rmodvege` to `growR`. diff --git a/R/management.R b/R/management.R index 2d81df0..a811b4a 100644 --- a/R/management.R +++ b/R/management.R @@ -85,7 +85,7 @@ ManagementData = R6Class( if (intensity %in% choices) { self$intensity = intensity logger(sprintf("[autocut]Setting management intensity to `%s`.", - intensity)) + intensity), level = INFO) } else { self$intensity = "high" } diff --git a/R/modvegesite.R b/R/modvegesite.R index 209717f..80d5015 100644 --- a/R/modvegesite.R +++ b/R/modvegesite.R @@ -467,12 +467,19 @@ ModvegeSite = R6Class( #' #' Creates a simple base R plot showing the BM with cutting events and, #' if applicable, target biomass, dBM, cBM and hvBM. + #' Can only be sensibly run *after* a simulation has been carried out, + #' i.e. after this instance's `run()` method has been called. #' #' @param smooth_interval Int. Number of days over which the variable #' `dBM` is smoothened. - #' @return None Creates a plot of the result. + #' @param ... Further arguments are discarded. + #' @return NULL Creates a plot of the result in the active device. #' - plot = function(smooth_interval = 28) { + plot = function(smooth_interval = 28, ...) { + if (private$current_DOY == 1) { + warning("Cannot plot results because simulation has not yet been run.") + return() + } oldpar = par(no.readonly = TRUE) on.exit(par(oldpar)) par(mfrow = c(2, 2)) @@ -490,7 +497,6 @@ ModvegeSite = R6Class( plot(self$cBM, type = "l", xlab = xlab, ylab = "cBM (kg / ha)") plot(self$hvBM, type = "l", xlab = xlab, ylab = "hvBM (kg / ha)") } - ) ), # End of public attributes @@ -965,3 +971,20 @@ ModvegeSite = R6Class( ) # End of private attributes ) + +## S3 dispatch methods + +#' Plot ModVege simulation result overview +#' +#' This wraps the `ModvegeSite` instance's `plot()` method. +#' +#' @param x A [ModvegeSite] instance. +#' @param ... Arguments are passed on to [ModvegeSite]`$plot()`. +#' @return NULL, but plots to the active device. +#' +#' @md +#' @export +plot.ModvegeSite = function(x, ...) { + x$plot(...) +} + diff --git a/R/parameter_scan.R b/R/parameter_scan.R index 96db678..77d8247 100644 --- a/R/parameter_scan.R +++ b/R/parameter_scan.R @@ -90,10 +90,7 @@ run_parameter_scan = function(environment, param_values, force = FALSE, level = INFO ) environment$parameters$set_parameters(parameter_sets[[i]]) - mvs = growR_run_loop(c(environment), - output_dir = "", - store_results = TRUE - ) + mvs = growR_run_loop(c(environment), output_dir = "") results[[i]] = list(params = parameter_sets[[i]], data = mvs[[1]]) } diff --git a/R/run.R b/R/run.R index ff8c87c..40e5b3c 100644 --- a/R/run.R +++ b/R/run.R @@ -10,27 +10,21 @@ #' @param modvege_environments A list of [ModvegeEnvironment] instances. #' @param output_dir string; name of directory to which output files are to #' be written. If `output_dir == ""` (default), no files are written. -#' @param store_results boolean; If TRUE, return a list of the [ModvegeSite] -#' objects which were run. #' -#' @return If `store_results == TRUE`, a list of the format -#' `[[run]][[year]]` containing clones of the [ModvegeSite] instances that -#' were run. Otherwise an empty list. Defaults to -#' getOption("growR.output_dir"). +#' @return A list of the format `[[run]][[year]]` containing clones of +#' the [ModvegeSite] instances that were run. Also write to files, if +#' *output_dir* is nonempty. #' #' @examples #' env1 = create_example_environment(site = "posieux") #' env2 = create_example_environment(site = "sorens") #' -#' growR_run_loop(c(env1, env2), output_dir = "", -#' store_results = TRUE) +#' growR_run_loop(c(env1, env2), output_dir = "") #' #' @md #' @export #' -growR_run_loop = function(modvege_environments, - output_dir = "", - store_results = FALSE) { +growR_run_loop = function(modvege_environments, output_dir = "") { # Parse output dir if (output_dir == "") { write_files = FALSE @@ -62,19 +56,17 @@ growR_run_loop = function(modvege_environments, #-Write-output------------------------------------------------------------ if (write_files) { - out_file = sprintf("%s%s%s_%s.dat", - output_dir, + out_file = sprintf("%s%s_%s.dat", run_environment$site_name, run_environment$run_name_in_filename, this_year) + out_path = file.path(output_dir, out_file) logger("Entering `ModvegeSite$write_output`", level = TRACE) - modvege$write_output(out_file, force = TRUE) + modvege$write_output(out_path, force = TRUE) } #-Store-output------------------------------------------------------------ - if (store_results) { - results[[run]][[i_year]] = modvege$clone(deep = TRUE) - } + results[[run]][[i_year]] = modvege$clone(deep = TRUE) } # End of loop over simulation years } # End of loop over runs logger("All runs completed.", level = INFO) diff --git a/R/support_functions.R b/R/support_functions.R index 79225e5..eae85e4 100644 --- a/R/support_functions.R +++ b/R/support_functions.R @@ -299,11 +299,11 @@ fC.ST = function(x){1} # function(x){1 + .1*(x - 360)/(720 - 360)} #' @md #' @export get_annual_gross_yield = function(elevation, intensity = "high") { - #mask = yield_parameters$intensity == intensity - #a = yield_parameters[mask, ]$a - #b = yield_parameters[mask, ]$b - #return(a + b * max(elevation, 500)) - return(1) + mask = yield_parameters$intensity == intensity + a = yield_parameters[mask, ]$a + b = yield_parameters[mask, ]$b + return(a + b * max(elevation, 500)) +# return(1) } #' Get number of expected cuts diff --git a/man/ModvegeSite.Rd b/man/ModvegeSite.Rd index ddb3e2f..5368832 100644 --- a/man/ModvegeSite.Rd +++ b/man/ModvegeSite.Rd @@ -431,8 +431,10 @@ Create an overview plot Creates a simple base R plot showing the BM with cutting events and, if applicable, target biomass, dBM, cBM and hvBM. +Can only be sensibly run \emph{after} a simulation has been carried out, +i.e. after this instance's \code{run()} method has been called. \subsection{Usage}{ -\if{html}{\out{