Skip to content

SIP400 Address harvest and leaf off issues - #401

Open
Alomir wants to merge 15 commits into
masterfrom
SIP400-Address-harvest-and-leaf-off-issues
Open

Alomir wants to merge 15 commits into
masterfrom
SIP400-Address-harvest-and-leaf-off-issues

Conversation

@Alomir

@Alomir Alomir commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • What: Fix discovered issues with harvest and leaf-on mechanics
  • Motivation: Non-negative pool sizes and mass imbalances occur without these changes

How was this change tested?

  • Current tests all pass (after updates for slightly altered flow)
  • Extensive new test passes
  • Smoke tests pass without any modifications to sipnet.out files (one minor changes to russell_2/events.out)

Related issues

Checklist

  • Related issues are listed above. PRs without an approved, related issue may not get reviewed.
  • PR title has the issue number in it ("[#] <concise description of proposed change>")
  • Tests added/updated for new features (if applicable)
  • Documentation updated (if applicable)
  • docs/CHANGELOG.md updated with noteworthy changes
  • Code formatted with clang-format (run git clang-format if needed)

@dlebauer

dlebauer commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

When I asked Chad to "just make it work, please" (referring to the entire MAGic pipeline) Chad did this (among other things):

master...dlebauer:sipnet:fix/zero-carbon-nitrogen-transfer

And now that this is up he said:

In calcNPoolFluxes(), there are three remaining cases where a zero C flux can be divided by an undefined C:N ratio when the corresponding C pool is empty: rLitter / litterCN, rSoil / soilCN, and litterToSoil / litterCN. This produces NaN nitrogen fluxes.

Could we guard each calculation so that a zero carbon flux carries zero nitrogen? I have a small patch and regression test for this and can update them to preserve the new getLeafLitterFlux() logic in this PR.

@Alomir

Alomir commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator Author

@dlebauer I like that! Go Chad!

@Alomir

Alomir commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator Author

I'm going to go ahead and make a PR for Chad's changes, rather than make this one bigger

@Alomir

Alomir commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

Moving to ready for review - I suspect copilot will point out some doc updates I missed.

@Alomir
Alomir marked this pull request as ready for review October 2, 2026 17:48
Copilot AI balanced review requested due to automatic review settings October 2, 2026 17:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Harvest deltas remain absent from pool look-ahead checks, and leaf-off logs can report pre-limitation values.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Fixes harvest and leaf-off mass conservation by restructuring event processing, adding harvest termination handling, and expanding regression coverage.

Changes:

  • Splits event processing into carbon and nitrogen passes.
  • Tracks harvest routing and leaf-off fluxes more precisely.
  • Adds comprehensive harvest, balance, restart, and event-output tests.
File Description
src/​common/​exitCodes.h Adds allocation-failure exit code.
src/​common/​util.c Implements dynamic strings.
src/​common/​util.h Declares dynamic-string API.
src/​sipnet/​balance.c Improves imbalance diagnostics.
src/​sipnet/​debug_log.c Logs new flux and event fields.
src/​sipnet/​events.c Splits event passes and revises harvest handling.
src/​sipnet/​events.h Adds event logs and harvest trackers.
src/​sipnet/​limitations.c Extends negative-pool limitation logic.
src/​sipnet/​nitrogen.c Separates leaf-off nitrogen effects.
src/​sipnet/​nitrogen.h Exposes leaf-off nitrogen helper.
src/​sipnet/​restart.c Serializes expanded event trackers.
src/​sipnet/​sipnet.c Integrates event passes and mortality correction.
src/​sipnet/​state.c Adds combined leaf-litter accessor.
src/​sipnet/​state.h Adds new event and leaf-off fluxes.
tests/​sipnet/​test_events_infrastructure/​events_output_header.out Updates expected event output.
tests/​sipnet/​test_events_infrastructure/​events_output_no_header.out Updates headerless event output.
tests/​sipnet/​test_events_types/​testEventHarvest.c Tests accounting-carbon harvests.
tests/​sipnet/​test_modeling/​Makefile Registers the harvest regression suite.
tests/​sipnet/​test_modeling/​termination.clim Adds regression climate fixture.
tests/​sipnet/​test_modeling/​termination.param Adds regression parameter fixture.
tests/​sipnet/​test_modeling/​testCompleteHarvest.c Adds comprehensive harvest regressions.
tests/​sipnet/​test_modeling/​testNitrogenCycle.c Updates event/N-cycle sequencing tests.
tests/​sipnet/​test_modeling/​testPlantMortality.c Uses expanded harvest trackers.
tests/​sipnet/​test_sipnet_infrastructure/​testDebugLogFiles.c Validates expanded debug output.
tests/​smoke/​russell_2/​events.out Updates smoke-test field ordering.
tests/​utils/​helpers.c Updates test event processing flow.
docs/​CHANGELOG.md Documents harvest and leaf-off fixes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/sipnet/events.c
Comment on lines +716 to +719
double leafOff = envi.plantLeafC * params.fracLeafFall;
fluxes.eventLeafOffLitterC += leafOff / climLen;

appendLog(event, 1, "eventLeafOffLitter", leafOff);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fine, I was clearly ignoring this as these outputs are really just for debugging. However, we're in a decent place for doing this in a centralized/extendable way, so why not...

Comment thread src/sipnet/limitations.c
Comment on lines +91 to +92
double deficit = fluxes.leafOffLitter + fluxes.eventLeafOffLitterC -
fluxes.leafCreation - availableLeafRate;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is harder, as I don't think this is the best place to handle eventLeafC overdraws.

Hmm, maybe as a step between leaf-off litter and transfer to woodC... that might work. This will mess up the test, so more work there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Address harvest and leaf off mass conservation issues Consider leaf-off litter and event fluxes in negative-growth limitation check

3 participants