From bfaa3d2fccd44252867fa612ef6ca792e57595d2 Mon Sep 17 00:00:00 2001 From: madhavzanwar Date: Mon, 2 Nov 2026 09:37:12 +0000 Subject: [PATCH] Read opt_SMDbare in write.config.RothC The bare-soil option was read from opt_RMmoist, so opt_SMDbare was ignored. Fixes #4217 --- CHANGELOG.md | 1 + models/rothc/NEWS.md | 4 +- models/rothc/R/write.config.RothC.R | 2 +- .../tests/testthat/test-write.config.RothC.R | 62 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 models/rothc/tests/testthat/test-write.config.RothC.R diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e14e5b8155..b2886e44dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha - Added statewide synthetic fertilization and compost amendment event workflows for CA ag parcels. Outputs share an ensemble naming so a downstream cleaner unions them into one fertilization event type for SIPNET. ### Fixed +- `PEcAn.RothC::write.config.RothC()` ignored the `opt_SMDbare` setting and used `opt_RMmoist` for both options (#4217). - `PEcAn.utils::match_file()` anchors file prefixes to the beginning of the filename, so a request for `foo/site1` no longer matches `foo/old-site1` (but still finds `foo/site1-old` as previously) (#4196, @Shubham-Padkonde). - PEcAnAssimSequential: `simple.local()` and `piecew.poly.local()` now scale distances by their `scalef` argument instead of the undefined `rloc`; `GEF()`, `GEF.MultiSite()` and `EnKF.MultiSite()` read their extra arguments from `...` explicitly instead of assigning them into the global environment; `SDA_remote_launcher()` no longer references an undefined `stop.on.error`. Together these clear most of the package's "no visible binding for global variable" check notes (#2758). - `PEcAn.SIPNET::write.config.SIPNET()` wrote `stem_respiration_rate` and `root_respiration_rate` as per day rates into SIPNET's per year `baseVegResp` and `baseFineRootResp`, 365x too low. diff --git a/models/rothc/NEWS.md b/models/rothc/NEWS.md index cc686419188..5367331c482 100644 --- a/models/rothc/NEWS.md +++ b/models/rothc/NEWS.md @@ -1,3 +1,5 @@ # PEcAn.RothC 0.0.0.9000 -Initial development version \ No newline at end of file +Initial development version + +* `write.config.RothC()` now reads the `opt_SMDbare` setting for the bare-soil moisture deficit option. It previously used `opt_RMmoist` for both options, so `opt_SMDbare` was ignored (#4217). \ No newline at end of file diff --git a/models/rothc/R/write.config.RothC.R b/models/rothc/R/write.config.RothC.R index d645ecb012b..4a85741dd75 100644 --- a/models/rothc/R/write.config.RothC.R +++ b/models/rothc/R/write.config.RothC.R @@ -137,7 +137,7 @@ write.config.RothC <- function(defaults, trait.values, settings, run.id) { # Bare SMD: wilting point configuration # 1: Standard RothC bareSMD # 2: bareSMD is set to wilting point -15bar (could be better for dry soils) - smdbare <- settings$model$opt_RMmoist %||% 1 + smdbare <- settings$model$opt_SMDbare %||% 1 config.text <- gsub("@OPT_SMDBARE@", smdbare, config.text) # min_RM_moist: Lowest value taken by the soil moisture rate modifier, diff --git a/models/rothc/tests/testthat/test-write.config.RothC.R b/models/rothc/tests/testthat/test-write.config.RothC.R new file mode 100644 index 00000000000..5f5906ba177 --- /dev/null +++ b/models/rothc/tests/testthat/test-write.config.RothC.R @@ -0,0 +1,62 @@ +test_that("write.config.RothC writes opt_RMmoist and opt_SMDbare as separate flags", { + pth <- withr::local_tempdir() + dir.create(file.path(pth, "run", "r1"), recursive = TRUE) + dir.create(file.path(pth, "out", "r1"), recursive = TRUE) + met <- file.path(pth, "met.dat") + data.frame( + year = 2020, month = 1:12, Tmp_C = 10, Rain_mm = 50, Evap_mm = 0 + ) |> + utils::write.table(met, row.names = FALSE, quote = FALSE) + + fake_soil <- function(...) { + dat <- data.frame( + depth = c(0.1, 0.2, 0.3, 0.4), # m + fraction_of_clay_in_soil = 0.3, + fraction_of_silt_in_soil = 0.3, + soil_bulk_density = 1350, + soil_organic_carbon_stock = 2 + ) + attr(dat, "units") <- c( + depth = "meters", + fraction_of_clay_in_soil = "1", + fraction_of_silt_in_soil = "1", + soil_bulk_density = "kg m-3", + soil_organic_carbon_stock = "kg m-2" + ) + dat + } + + run_config <- function(model_opts) { + s <- list( + rundir = file.path(pth, "run"), + host = list(rundir = file.path(pth, "run"), outdir = file.path(pth, "out")), + model = c(list(binary = "rothc"), model_opts), + run = list( + start.date = "2020-01-01", + end.date = "2020-12-31", + site = list( + lat = 1, lon = 1, met.start = "2020-01-01", met.end = "2020-12-31" + ), + inputs = list( + met = list(path = met), + soil_physics = list(path = "ignored") + ) + ) + ) + with_mocked_bindings( + write.config.RothC(NULL, NULL, s, "r1"), + netcdf2df = fake_soil + ) + lines <- readLines(file.path(pth, "run", "r1", "RothC_input.dat")) + lines <- lines[!grepl("^#", lines)] + list( + flags = scan(text = lines[[1]], quiet = TRUE), + soil = scan(text = lines[[2]], quiet = TRUE) + ) + } + + res <- run_config(list(opt_RMmoist = 2, opt_SMDbare = 1)) + expect_equal(res$flags, c(2, 1)) + res <- run_config(list(opt_RMmoist = 1, opt_SMDbare = 2)) + expect_equal(res$flags, c(1, 2)) +})