Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.SIPNET::write.config.SIPNET()` writes `sipnet.param` values at 17 significant digits instead of 15, so allocations that sum to 1 no longer reach SIPNET summing past 1.
- `PEcAn.uncertainty::read.ensemble.output()` uses `NA` for missing variables, so each member’s result uses only its own output (#4204).
- Fixed broken meteorological data, flux network, and API documentation links in the PEcAn book and data.atmosphere package (#4071).
- `PEcAn.uncertainty::generate_joint_ensemble_design(sobol = TRUE)` now errors on looped inputs and quasi-random parameter methods, which split into dependent `X1` and `X2` halves and gave wrong Sobol indices (#4134).
Expand Down
1 change: 1 addition & 0 deletions models/sipnet/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# PEcAn.SIPNET 1.10.0.9000

* `write.config.SIPNET` writes `sipnet.param` values at 17 significant digits instead of 15, so allocations that sum to 1 no longer sum past 1 in the file. Param files change in the last digits.
* `write_segmented_configs.SIPNET` now merges consecutive segments with the same PFT to avoid unnecessary SIPNET restarts (#4129).
* `write.config.SIPNET`: Fixed initial LAI being too low when `leaf_carbon_content` is read from an initial condition file (#4205).
* `write.config.SIPNET` converted `stem_respiration_rate` and
Expand Down
9 changes: 8 additions & 1 deletion models/sipnet/R/write.configs.SIPNET.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ write.config.SIPNET <- function(defaults, trait.values, settings, run.id, inputs
pft.trait.names <- names(pft.traits)

## Append/replace params specified as constants
constant.traits <- unlist(defaults[[1]]$constants)
# xml constants are strings, and c() below would make every value 15 digit
# text. convert one at a time, since type.convert() rounds numbers too
constant.traits <- unlist(lapply(defaults[[1]]$constants, function(x) {
if (is.character(x)) utils::type.convert(x, as.is = TRUE) else x
}))
constant.names <- names(constant.traits)

# Replace matches
Expand Down Expand Up @@ -1080,6 +1084,9 @@ write.config.SIPNET <- function(defaults, trait.values, settings, run.id, inputs
)
}

# write.table keeps 15 significant digits, which can push allocations that
# sum to exactly 1 past sipnet's check. 17 digits always read back exactly
param[[2]] <- sprintf("%.17g", param[[2]])

utils::write.table(
param,
Expand Down
60 changes: 39 additions & 21 deletions models/sipnet/tests/testthat/test-write.config.SIPNET.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,11 @@ test_that("write.config.SIPNET", {
)

# (at least some) parameters updated
param_result <- readLines(file.path(pth, "run", "run1", "sipnet.param"))
expect_match(
param_result,
"aMax 10", # this is Amax * SLA,
fixed = TRUE,
all = FALSE
)
expect_match(
param_result,
"aMaxFrac 0.99", # raw template had 0.76
fixed = TRUE,
all = FALSE
)
p <- utils::read.table(file.path(pth, "run", "run1", "sipnet.param"))
expect_equal(p[p[[1]] == "aMax", 2], 10) # this is Amax * SLA
expect_equal(p[p[[1]] == "aMaxFrac", 2], 0.99) # raw template had 0.76
# leaf C specific weight is leafC / SLA, with units converted to g C/m2 leaf
expect_match(
param_result,
"leafCSpWt 235 ", # space at end to catch unit errors (eg fail on 23500)
fixed = TRUE,
all = FALSE
)
expect_equal(p[p[[1]] == "leafCSpWt", 2], 235)

})

Expand Down Expand Up @@ -130,8 +115,8 @@ test_that("leafNResorptionFrac trait reaches the v2 param file", {
run.id = "run1"
)

param_result <- readLines(file.path(pth, "run", "run1", "sipnet.param"))
expect_match(param_result, "leafNResorptionFrac 0.6", fixed = TRUE, all = FALSE)
p <- utils::read.table(file.path(pth, "run", "run1", "sipnet.param"))
expect_equal(p[p[[1]] == "leafNResorptionFrac", 2], 0.6)
})

test_that("stem and root respiration are written as per year rates", {
Expand Down Expand Up @@ -286,3 +271,36 @@ test_that("leaf carbon from a poolinitcond file sets the initial leaf pool", {
# tolerance covers the rounding when sipnet.param is written
expect_equal(pval("laiInit") * pval("leafCSpWt"), 100, tolerance = 1e-6)
})

test_that("allocations that sum to 1 reach sipnet.param exactly", {
# these sum to exactly 1 in double, but at 15 digits they sum past 1
# and sipnet exits
leaf <- 0.25758166726134457
wood <- 0.4742485584847288
root <- 0.26816977425392663
pth <- withr::local_tempdir()
dir.create(file.path(pth, "run", "run1"), recursive = TRUE)
s <- PEcAn.settings::as.Settings(list(
outdir = file.path(pth, "out"), rundir = file.path(pth, "run"),
pfts = list(pft1 = list()),
model = list(binary = "", revision = "v2.2.0"),
run = list(site = list(name = "site1", lat = 40, lon = -88),
inputs = list(met = list(path = "")),
start.date = "2025-01-01", end.date = "2025-01-02"),
host = list(name = "", outdir = file.path(pth, "out"), rundir = file.path(pth, "run"))
))
# one value each way it can arrive: a trait, an xml constant (a string),
# and a constant set from R
write.config.SIPNET(
defaults = list(pft1 = list(constants = list(
root_allocation_fraction = "0.26816977425392663",
leaf_allocation_fraction = leaf
))),
trait.values = list(pft1 = list(wood_allocation_fraction = wood)),
settings = s, run.id = "run1"
)
p <- utils::read.table(file.path(pth, "run", "run1", "sipnet.param"))
alloc <- p[match(c("leafAllocation", "woodAllocation", "fineRootAllocation"), p[[1]]), 2]
# not expect_equal, whose tolerance would pass the 15 digit values
expect_identical(alloc, c(leaf, wood, root))
})
10 changes: 4 additions & 6 deletions models/sipnet/tests/testthat/test-write_segmented_configs.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,15 @@ test_that("write_segmented_configs", {

# (at least one) parameter updated
for (seg in 1:3) {
param_result <- readLines(
param_result <- utils::read.table(
file.path(run_path, "segments", paste0("segment_00", seg),
"run", "1", "sipnet.param")
)
seg_samp <- ens.samples[[paste0("pft", seg)]][c("Amax", "SLA")]
expected_amax <- seg_samp$Amax * seg_samp$SLA
expect_match(
param_result,
paste0("aMax ", expected_amax),
fixed = TRUE,
all = FALSE
expect_equal(
param_result[param_result[[1]] == "aMax", 2],
expected_amax
)
}

Expand Down
Loading