Skip to content

SIP217 Update harvest and fert event handling#270

Merged
Alomir merged 34 commits into
masterfrom
SIP217-Update-harvest-event-handling
Mar 6, 2026
Merged

SIP217 Update harvest and fert event handling#270
Alomir merged 34 commits into
masterfrom
SIP217-Update-harvest-event-handling

Conversation

@Alomir

@Alomir Alomir commented Feb 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • What: Update event handling to address two issues:
    • Incorrect handling of litter v. soil pools for harvest
    • Handle litter-pool=off for harvest and fert
  • Motivation: Event handling is not correct without this.

How was this change tested?

Unit tests updated, including adding N handling for affected tests.

Smoke tests run; no updates needed, as we do not (yet) have harvest or fert events there.

Reproduction steps

N/A

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)

Note: See CONTRIBUTING.md for additional guidance. This repository uses automated formatting checks; if the pre-commit hook blocks your commit, run git clang-format to format staged changes.

@Alomir
Alomir marked this pull request as ready for review March 4, 2026 17:10

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.

Pull request overview

This PR updates SIPNET’s agronomic event handling to correctly route harvest and fertilization transfers between litter vs soil pools, including the special case where litter_pool=off (issue #217).

Changes:

  • Split harvest transfers into separate above-ground (litter) vs below-ground (soil) additions, with fallback routing to soil when litter_pool=off.
  • Extend event flux tracking to include soil carbon and soil organic nitrogen event fluxes.
  • Update tests and event-output fixtures to reflect the new event flux fields and behaviors; add a manual debug workflow.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/sipnet/events.c Implements new harvest/fert routing logic and emits additional event-output fields.
src/sipnet/state.h Adds new event flux fields (eventSoilC, eventSoilOrgN) to the global flux struct.
tests/sipnet/test_events_types/testEventHarvest.c Updates harvest tests to validate soil vs litter pool behavior and adds N-related expectations.
tests/sipnet/test_events_types/testEventFertilization.c Ensures event output file is closed between runs (avoids leaking open handles).
tests/sipnet/test_events_types/testEventPlanting.c Closes event output file after each run.
tests/sipnet/test_events_types/testEventIrrigation.c Closes event output file after each run.
tests/sipnet/test_events_types/testEventTillage.c Closes event output file after each run.
tests/sipnet/test_events_types/events_two_harvest.in Adjusts the second harvest event fractions used by the harvest arithmetic test.
tests/sipnet/test_events_infrastructure/events_output_no_header.out Updates expected event output to include soil C / soil org N fields for harvest.
tests/sipnet/test_events_infrastructure/events_output_header.out Updates expected event output to include soil C / soil org N fields for harvest.
.github/workflows/debug.yml Adds an opt-in workflow-dispatch tmate-based debug workflow on Linux.
.github/workflows/ci.yml Minor whitespace-only change.
Comments suppressed due to low confidence (2)

src/sipnet/events.c:575

  • In FERTILIZATION, the code/comment say N is ignored when nitrogenCycle is off, but orgN/minN are no longer zeroed in that case and writeEventOut() still prints non-zero eventOrgN, eventMinN, and eventInputN. This makes the event output misleading/inconsistent with the actual model state (fluxes.eventMinN/eventLitterN/eventInputN are not updated when nitrogenCycle is off). Consider explicitly setting orgN=minN=0 when !ctx.nitrogenCycle and ensuring the values written to events.out match what is actually applied.
        const FertilizationParams *fertParams = gEvent->eventParams;
        const double orgC = fertParams->orgC;
        double orgN = fertParams->orgN;
        double minN = fertParams->minN;
        if (ctx.litterPool) {
          fluxes.eventLitterC += orgC / climLen;
        } else {
          fluxes.eventSoilC += orgC / climLen;
        }

        if (ctx.nitrogenCycle) {
          // As the warning says in readEventData(), we ignore N when the
          // nitrogen cycle model is off
          // Implies ctx.litterPool
          fluxes.eventLitterN += orgN / climLen;
          fluxes.eventMinN += minN / climLen;
        }

        // MASS BALANCE: this is a system input
        fluxes.eventInputC += orgC / climLen;
        if (ctx.nitrogenCycle) {
          fluxes.eventInputN += (orgN + minN) / climLen;
        }

        writeEventOut(gEvent, 5, "fluxes.eventOrgN", orgN / climLen,
                      "fluxes.eventLitterC", orgC / climLen, "fluxes.eventMinN",
                      minN / climLen, "fluxes.eventInputC", orgC / climLen,
                      "fluxes.eventInputN", (orgN + minN) / climLen);

src/sipnet/events.c:575

  • For FERTILIZATION when ctx.litterPool is off, carbon is now routed into fluxes.eventSoilC, but the output still labels the carbon term as fluxes.eventLitterC in writeEventOut(). This contradicts the meaning of eventLitterC in state.h and can confuse debugging / downstream parsing of events.out. Consider writing fluxes.eventSoilC when litterPool is off (and fluxes.eventLitterC when on), or otherwise aligning the field names with the actual destination pool.
        if (ctx.litterPool) {
          fluxes.eventLitterC += orgC / climLen;
        } else {
          fluxes.eventSoilC += orgC / climLen;
        }

        if (ctx.nitrogenCycle) {
          // As the warning says in readEventData(), we ignore N when the
          // nitrogen cycle model is off
          // Implies ctx.litterPool
          fluxes.eventLitterN += orgN / climLen;
          fluxes.eventMinN += minN / climLen;
        }

        // MASS BALANCE: this is a system input
        fluxes.eventInputC += orgC / climLen;
        if (ctx.nitrogenCycle) {
          fluxes.eventInputN += (orgN + minN) / climLen;
        }

        writeEventOut(gEvent, 5, "fluxes.eventOrgN", orgN / climLen,
                      "fluxes.eventLitterC", orgC / climLen, "fluxes.eventMinN",
                      minN / climLen, "fluxes.eventInputC", orgC / climLen,
                      "fluxes.eventInputN", (orgN + minN) / climLen);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/sipnet/test_events_types/testEventHarvest.c Outdated
Comment thread tests/sipnet/test_events_types/testEventHarvest.c Outdated
Comment thread tests/sipnet/test_events_types/testEventHarvest.c Outdated
Comment thread .github/workflows/debug.yml
Comment thread src/sipnet/events.c Outdated
Comment thread src/sipnet/events.c Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

@dlebauer dlebauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

I'm assuming the debugger action was intentionally checked in, wouldn't warrant a separate pr.

@Alomir

Alomir commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator Author

Moving to draft to address copilot's comments

@Alomir
Alomir marked this pull request as draft March 6, 2026 15:15
@Alomir
Alomir marked this pull request as ready for review March 6, 2026 15:58
@Alomir
Alomir merged commit 469b8b8 into master Mar 6, 2026
12 checks passed
@Alomir
Alomir deleted the SIP217-Update-harvest-event-handling branch March 6, 2026 15:59
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.

Update handling of harvest and fert events

3 participants