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{
}}\preformatted{ModvegeSite$plot(smooth_interval = 28)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{ModvegeSite$plot(smooth_interval = 28, ...)}\if{html}{\out{
}} } \subsection{Arguments}{ @@ -440,11 +442,13 @@ if applicable, target biomass, dBM, cBM and hvBM. \describe{ \item{\code{smooth_interval}}{Int. Number of days over which the variable \code{dBM} is smoothened.} + +\item{\code{...}}{Further arguments are discarded.} } \if{html}{\out{}} } \subsection{Returns}{ -None Creates a plot of the result. +NULL Creates a plot of the result in the active device. } } \if{html}{\out{
}} diff --git a/man/growR_run_loop.Rd b/man/growR_run_loop.Rd index 2b8d098..893817c 100644 --- a/man/growR_run_loop.Rd +++ b/man/growR_run_loop.Rd @@ -4,22 +4,18 @@ \alias{growR_run_loop} \title{Run growR simulations} \usage{ -growR_run_loop(modvege_environments, output_dir = "", store_results = FALSE) +growR_run_loop(modvege_environments, output_dir = "") } \arguments{ \item{modvege_environments}{A list of \link{ModvegeEnvironment} instances.} \item{output_dir}{string; name of directory to which output files are to be written. If \code{output_dir == ""} (default), no files are written.} - -\item{store_results}{boolean; If TRUE, return a list of the \link{ModvegeSite} -objects which were run.} } \value{ -If \code{store_results == TRUE}, a list of the format -\verb{[[run]][[year]]} containing clones of the \link{ModvegeSite} instances that -were run. Otherwise an empty list. Defaults to -getOption("growR.output_dir"). +A list of the format \verb{[[run]][[year]]} containing clones of +the \link{ModvegeSite} instances that were run. Also write to files, if +\emph{output_dir} is nonempty. } \description{ Start the loop over runs specified in the config file. @@ -34,7 +30,6 @@ as specified in the \emph{site_name} and \emph{run_name} fields of the supplied 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 = "") } diff --git a/man/plot.ModvegeSite.Rd b/man/plot.ModvegeSite.Rd new file mode 100644 index 0000000..76b95b5 --- /dev/null +++ b/man/plot.ModvegeSite.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/modvegesite.R +\name{plot.ModvegeSite} +\alias{plot.ModvegeSite} +\title{Plot ModVege simulation result overview} +\usage{ +\method{plot}{ModvegeSite}(x, ...) +} +\arguments{ +\item{x}{A \link{ModvegeSite} instance.} + +\item{...}{Arguments are passed on to \link{ModvegeSite}\verb{$plot()}.} +} +\value{ +NULL, but plots to the active device. +} +\description{ +This wraps the \code{ModvegeSite} instance's \code{plot()} method. +} diff --git a/tests/testthat/test-run.R b/tests/testthat/test-run.R index e2bcbc2..23e1f72 100644 --- a/tests/testthat/test-run.R +++ b/tests/testthat/test-run.R @@ -7,7 +7,6 @@ test_that("growR_run_loop with example config", { for (env in envs) { env$years = env$years[[1]] } - expect_no_error(growR_run_loop(envs, - store_results = FALSE)) + expect_no_error(growR_run_loop(envs)) unlink(.tmpdir(), recursive = TRUE) }) diff --git a/vignettes/growR.Rmd b/vignettes/growR.Rmd index 33677e4..e478016 100644 --- a/vignettes/growR.Rmd +++ b/vignettes/growR.Rmd @@ -138,8 +138,7 @@ information needed in order to run a ModVege simulation. We are now ready to do so: ```{r run_simulation} results = growR_run_loop(environments, - output_dir = file.path(working_dir, "output"), - store_results = TRUE) + output_dir = file.path(working_dir, "output")) ``` This will simulate grass growth for every year in every environment present in `environments`. After some console output, we now have the results of @@ -230,7 +229,7 @@ new_envs = c(env0, env1, env2) And we're ready to run and inspect our next run of simulations: ```{r ni_screening} -new_results = growR_run_loop(new_envs, store_results = TRUE) +new_results = growR_run_loop(new_envs) # Plot all results for (run in new_results) { print(run[[1]]$parameters$NI)