-
Notifications
You must be signed in to change notification settings - Fork 20
Address PR #248 review feedback: document wood carbon storage pool #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
32
commits into
master
Choose a base branch
from
copilot/sub-pr-248-again
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9f91be1
Add balance.c|h
Alomir 7b182f7
Improve do-single-output help
Alomir 0ec16f7
Carbon and nitrogen balance checks
Alomir 2bfd757
Add event fluxes to track system inputs and outputs; add nppStorage p…
Alomir f6f89d8
Add external writeEvent function, refactor original
Alomir 9aa91fb
Add event for leaf on/off; split woodC into two pools; add balance ch…
Alomir 0c454bf
Updates for balance checks and leaf on/off events
Alomir ec9b469
Merge branch 'master' into add_carbon_balance_check
Alomir 8ba00eb
Add ability to compare to base
Alomir b2e26c4
Remove tolerance from balance tracker
Alomir 67028b1
Update col list for niwot
Alomir 13faa6d
Avoid negative zero
Alomir 9211161
Remove negative zeros
Alomir fee673b
Add test for balance check
Alomir 5b5c64a
Merge branch 'master' into add_carbon_balance_check
Alomir 17da4b6
PR feedback
Alomir bae820e
Turn off microbes, as it is now broken
Alomir 83afb27
Test updates after balance check updates
Alomir 1aacd4c
Split leaf-on carbon creation into own flux
Alomir 14f6d4a
Fix leafOn flux
Alomir 2a8abae
Updates for leafOn split
Alomir b38d604
Force skipping russell_4
Alomir 75a44c3
Update for new event fluxes
Alomir 649b35b
Initial plan
Copilot 02f68a4
Fix typos and clarify plantWoodCStorageDelta comment
Copilot df0257d
Add documentation for carbon balance checks and update CHANGELOG
Copilot 63c6504
Address code review feedback: clarify nppStorage comment and fix bala…
Copilot f48c49c
Documentation and feedback complete
Copilot a248978
Add CodeQL build artifacts to .gitignore
Copilot 1a071ee
Update documentation for storage pool, remove Mass Balance section pe…
Copilot 305769d
Remove test output files and fix model-structure.md per review feedback
Copilot f127f8b
Merge branch 'master' into copilot/sub-pr-248-again
Alomir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #include "balance.h" | ||
|
|
||
| #include <math.h> | ||
|
|
||
| #include "common/context.h" | ||
| #include "common/exitCodes.h" | ||
| #include "common/logging.h" | ||
| #include "state.h" | ||
|
|
||
| // Definition of global balance tracker struct | ||
| BalanceTracker balanceTracker; | ||
|
|
||
| void getMassTotals(double *carbon, double *nitrogen) { | ||
| *carbon = (envi.plantWoodC + envi.plantWoodCStorageDelta) + envi.plantLeafC + | ||
| envi.fineRootC + envi.coarseRootC + envi.soilC; | ||
| if (ctx.litterPool) { | ||
| *carbon += envi.litterC; | ||
| } | ||
|
|
||
| if (ctx.nitrogenCycle) { | ||
| // Note: this is the one place where we use plantWoodC by itself; it's the | ||
| // reason plantWoodCStorageDelta was created, so that we can ignore it here. | ||
| *nitrogen = | ||
| envi.plantWoodC / params.woodCN + envi.plantLeafC / params.leafCN + | ||
| envi.fineRootC / params.fineRootCN + envi.coarseRootC / params.woodCN + | ||
| envi.soilOrgN + envi.litterN + envi.minN; | ||
| } else { | ||
| *nitrogen = 0.0; | ||
| } | ||
| } | ||
|
|
||
| void updateBalanceTrackerPreUpdate(void) { | ||
| // Set the pre-update pool totals | ||
| getMassTotals(&balanceTracker.preTotalC, &balanceTracker.preTotalN); | ||
| } | ||
|
|
||
| void updateBalanceTrackerPostUpdate(void) { | ||
| // Set the post-update pool totals | ||
| getMassTotals(&balanceTracker.postTotalC, &balanceTracker.postTotalN); | ||
|
|
||
| // Calculate the system inputs and outputs | ||
| // CARBON | ||
| balanceTracker.inputsC = fluxes.photosynthesis + // GPP | ||
| fluxes.eventInputC; // agro event additions | ||
| balanceTracker.outputsC = fluxes.rVeg + fluxes.rFineRoot + | ||
| fluxes.rCoarseRoot + // R_a | ||
| fluxes.rSoil + // R_h | ||
| fluxes.eventOutputC; // agro event removals | ||
| if (ctx.litterPool) { | ||
| balanceTracker.outputsC += fluxes.rLitter; | ||
| } | ||
| // Account for climate length | ||
| balanceTracker.inputsC *= climate->length; | ||
| balanceTracker.outputsC *= climate->length; | ||
|
|
||
| // NITROGEN | ||
| if (ctx.nitrogenCycle) { | ||
| balanceTracker.inputsN = | ||
| // TODO: fluxes.fixation + | ||
| fluxes.eventInputN; | ||
| balanceTracker.outputsN = | ||
| fluxes.nLeaching + fluxes.nVolatilization + fluxes.eventOutputN; | ||
|
|
||
| // Account for climate length | ||
| balanceTracker.inputsN *= climate->length; | ||
| balanceTracker.outputsN *= climate->length; | ||
| } | ||
| } | ||
|
|
||
| void initBalanceTracker(void) { | ||
| // Initialize all to zero | ||
| balanceTracker.preTotalC = 0.0; | ||
| balanceTracker.postTotalC = 0.0; | ||
| balanceTracker.inputsC = 0.0; | ||
| balanceTracker.outputsC = 0.0; | ||
| balanceTracker.preTotalN = 0.0; | ||
| balanceTracker.postTotalN = 0.0; | ||
| balanceTracker.inputsN = 0.0; | ||
| balanceTracker.outputsN = 0.0; | ||
| balanceTracker.deltaC = 0.0; | ||
| balanceTracker.deltaN = 0.0; | ||
| } | ||
|
|
||
| void checkBalance(void) { | ||
| // CARBON | ||
| // Pool delta | ||
| double poolCDelta = balanceTracker.postTotalC - balanceTracker.preTotalC; | ||
| // System delta | ||
| double systemCDelta = balanceTracker.inputsC - balanceTracker.outputsC; | ||
| balanceTracker.deltaC = poolCDelta - systemCDelta; | ||
|
|
||
| // NITROGEN | ||
| // Pool delta | ||
| double poolNDelta = balanceTracker.postTotalN - balanceTracker.preTotalN; | ||
| // System delta | ||
| double systemNDelta = balanceTracker.inputsN - balanceTracker.outputsN; | ||
| balanceTracker.deltaN = poolNDelta - systemNDelta; | ||
|
|
||
| // To avoid weird negative-zero issues... | ||
| if (fabs(balanceTracker.deltaC) < 1e-8) { | ||
| balanceTracker.deltaC = 0.0; | ||
| } | ||
| if (fabs(balanceTracker.deltaN) < 1e-8) { | ||
| balanceTracker.deltaN = 0.0; | ||
| } | ||
|
|
||
| int err = 0; | ||
| if (fabs(balanceTracker.deltaC) > 0.0) { | ||
| err = 1; | ||
| logInternalError( | ||
| "Carbon balance check failed (delta=%8.4f, Y: %d D: %d T: %4.2f)\n", | ||
| balanceTracker.deltaC, climate->year, climate->day, climate->time); | ||
| } | ||
| // RE-ENABLE WHEN N IS FIXED | ||
| // if (fabs(balanceTracker.deltaN) > 0.0) { | ||
| // err = 1; | ||
| // logInternalError("Nitrogen balance check failed (delta=%8.4f)\n", | ||
| // balanceTracker.deltaN); | ||
| //} | ||
| if (err) { | ||
| logInternalError("Exiting\n"); | ||
| // exit(EXIT_CODE_INTERNAL_ERROR); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #ifndef BALANCE_H | ||
| #define BALANCE_H | ||
|
|
||
| typedef struct BalanceTrackerStruct { | ||
| // Mass balance checks: | ||
| // X_t - X_(t-1) = inputs - outputs + tolerance | ||
| // or, we check that | ||
| // (pool delta) + (system delta) < tolerance | ||
| // where | ||
| // pool delta = X_t - X_(t-1) | ||
| // system delta = outputs - inputs | ||
|
|
||
| // Carbon balance | ||
| double preTotalC; | ||
| double postTotalC; | ||
| double inputsC; | ||
| double outputsC; | ||
|
|
||
| // Nitrogen balance | ||
| double preTotalN; | ||
| double postTotalN; | ||
| double inputsN; | ||
| double outputsN; | ||
|
|
||
| // Checks | ||
| double deltaC; | ||
| double deltaN; | ||
| } BalanceTracker; | ||
|
|
||
| // Global var | ||
| extern BalanceTracker balanceTracker; | ||
|
|
||
| void updateBalanceTrackerPreUpdate(void); | ||
|
|
||
| void updateBalanceTrackerPostUpdate(void); | ||
|
|
||
| void initBalanceTracker(void); | ||
|
|
||
| void checkBalance(void); | ||
|
|
||
| #endif // BALANCE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required for model structure doc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 1a071ee. The Mass Balance Checks section has been removed from model-structure.md.