Skip to content
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

Fix C balance and FPE errors #6235

Conversation

peterdschwartz
Copy link
Contributor

@peterdschwartz peterdschwartz commented Feb 13, 2024

Carbon balance was never checked correctly and let through negative values.
Initialization of Ecosystem variables needed to be re-adjusted, and balance checking for FATES needed to be changed, which causes round-off differences in CMASS_BALANCE_ERROR for fate_cold_allvars test.

Also included divide-by-zero check in PhenologyMod.F90 to fix fpe in debug mode.

[non-BFB] for one FATES test.
Fixes #6120
Fixes #6203
Fixes #6177

@peterdschwartz
Copy link
Contributor Author

@glemieux Here is a PR to fix the balance checking issues. I did get DIFF for fates_cold_allvars but only for CMASS_BALANCE_CHECK but the values were still ~1E-16.

Let me know if how I implemented the new balance checking makes sense to you.

Copy link
Contributor

@glemieux glemieux left a comment

Choose a reason for hiding this comment

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

The implementation of the fates gridcell balance checks looks good to me. Thanks!

@peterdschwartz
Copy link
Contributor Author

After doing a test merge I obtained FILLDIFF for the fates_cold_allvars. That I need to look into today

@peterdschwartz peterdschwartz mentioned this pull request Feb 21, 2024
@peterdschwartz
Copy link
Contributor Author

Quick update: fates_cold_allvars FAILs at the COMP_base_rest stage. The cprnc file just shows CMASS_BALANCE_ERROR with very small differences. Since there are no other diffs (i.e., in variables used to calculate carbon balance), I wonder if it's just due to floating point rounding issues. But, if that is the case, I'm not sure what the solution should be.

 CMASS_BALANCE_ERROR   (lndgrid,time)  t_index =     10    10
        127      384  (    25,     1) (    93,     1) (   148,     1) (   148,     1)
                 211   0.000000000000000E+00  -2.810582373931857E-16 2.3E-21 -1.292301226966941E-16 2.0E-06 -1.292301226966941E-16
                 211   0.000000000000000E+00  -2.810570991926628E-16         -1.292278595305381E-16         -1.292278595305381E-16
                 384  (    25,     1) (    93,     1)
          avg abs field values:    1.589865621873051E-16    rms diff: 0.0E+00   avg rel diff(npos):  2.0E-06
                                   1.589865886570847E-16                        avg decimal digits(ndif):  5.7 worst:  4.8
 RMS CMASS_BALANCE_ERROR              0.0000E+00            NORMALIZED  0.0000E+00

@rljacob
Copy link
Member

rljacob commented Feb 21, 2024

What is the diff with? A baseline from the previous code or is it a diff within a test like an ERS test?

@peterdschwartz
Copy link
Contributor Author

Not baseline diff (though that is also there but makes sense to me). It fails the COMP_base_rest portion of ERP test (same thing with the ERS version I made for debugging).

@rljacob
Copy link
Member

rljacob commented Feb 22, 2024

Try a REP test.

@peterdschwartz
Copy link
Contributor Author

REP passes just fine. I'm going to double check that each variable for carbon balance is read/written to the restart file, and maybe a longer simulation will reveal diff's accruing in other fields.

Copy link
Contributor

@bishtgautam bishtgautam left a comment

Choose a reason for hiding this comment

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

I have minor stylistic code change requests for this PR.

@@ -624,7 +621,7 @@ subroutine ColPBalanceCheck(bounds, &
! set time steps
dt = dtime_mod
kyr = year_curr; kmo = mon_curr; kda = day_curr; mcsec = secs_curr;

!nstep = nstep_mod
Copy link
Contributor

Choose a reason for hiding this comment

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

Please delete this line.

Comment on lines 901 to 908
grc_coutputs => grc_cf%coutputs , & ! Output: [real(r8) (:) ] (gC/m2/s) column-level C outputs
beg_totpftc => grc_cs%beg_totpftc , & ! Input: [real(r8) (:)] (gC/m2) patch-level carbon aggregated to column level, incl veg and cpool
beg_cwdc => grc_cs%beg_cwdc , & ! Input: [real(r8) (:)] (gC/m2) total column coarse woody debris carbon
beg_totsomc => grc_cs%beg_totsomc , & ! Input: [real(r8) (:)] (gC/m2) total column soil organic matter carbon
beg_totlitc => grc_cs%beg_totlitc , & ! Input: [real(r8) (:)] (gC/m2) total column litter carbon
beg_totprodc => grc_cs%beg_totprodc , & ! Input: [real(r8) (:)] (gC/m2) total column wood product carbon
beg_ctrunc => grc_cs%beg_ctrunc , & ! Input: [real(r8) (:)] (gC/m2) total column truncation carbon sink
beg_cropseedc_deficit => grc_cs%beg_cropseedc_deficit & ! Input: [real(r8) (:)] (gC/m2) column carbon pool for seeding new growth
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove an extra whitespace after => grc_cs% to be => grc_cs%


if(use_fates) then
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a whitespace if (use_fates)

dt = real( get_step_size(), r8 )
nstep = get_nstep()

do g = bounds%begg, bounds%endg
grc_cinputs(g) = grc_gpp(g) + grc_dwt_seedc_to_leaf(g) + grc_dwt_seedc_to_deadstem(g)

if(.not. use_fates) then
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly add a whitespace.

! calculate the P:PET for each month
p2ETo(p,kmo) = xp(p,kmo)/ETo(p,kmo)
if( abs(ETo(p,kmo)) > 0._r8) then
Copy link
Contributor

Choose a reason for hiding this comment

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

  • please add whitespace
  • Shouldn't there also be code corresponding to an else condition?

Comment on lines 7769 to 7770
this%er(i) = value_column !! Grid Balance Error
this%som_c_leached(i) = value_column !! Grid Balance Error
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please remove the !! Grid Balance Error

@peterdschwartz
Copy link
Contributor Author

peterdschwartz commented Mar 21, 2024

status:
fates_cold_allvars still fails restart test. Tried adding every gridcell and column variable related to CMASS_BALANCE to history and restart files but no other differences. Tried running test for 2 years to accumulate more carbon, but there was no other diffs detected.

Adjusting the history output interval did allow the test to pass the restart comparison but that's not a real solution.

Scheduled meeting with FATES devs to go over the logic for balance checking with FATES enabled.

@rljacob
Copy link
Member

rljacob commented Apr 12, 2024

@peterdschwartz what is the status of this PR?

@peterdschwartz
Copy link
Contributor Author

@rljacob I met with @glemieux a couple of weeks ago. He mentioned a possible time-step dependent part of FATES that could be the cause, but I think he's been busy with the FATES api update and our follow up meeting with @thorntonpe was cancelled due to lab holiday.

Today, I'll try increasing the history output to be every timestep and see if that provides any useful information.

@peterdschwartz
Copy link
Contributor Author

peterdschwartz commented Apr 12, 2024

Update:

Increasing output freq finally showed a new diff in a field. Looking into it further, it appears that the vertical transport tendency terms for carbon are not added to the corresponding leaching term due to some issue 5 years ago (according to git blame atleast), but it is for nitrogen and phosphorus.

Hopefully the straightforward solution works here.

 LITR2C_TNDNCY_VERT_TRANS   (lndgrid,levdcmp,time)  t_index =    194   194
          3     5760  (   153,     2,     1) (   153,     1,     1) (   237,     3,     1) (   247,    10,     1)
                3165   1.043116371057806E-09  -1.643725044964128E-09 2.2E-19  2.944499461962624E-12 8.1E-11  1.293930177203629E-16
                3165   1.043116371057806E-09  -1.643725044964128E-09          2.944499678803059E-12          1.293930309552527E-16
                5760  (   153,     2,     1) (   153,     1,     1)
          avg abs field values:    4.034842251976478E-11    rms diff: 0.0E+00   avg rel diff(npos):  8.1E-11
                                   4.034842251976478E-11                        avg decimal digits(ndif):  7.1 worst:  7.0
 RMS LITR2C_TNDNCY_VERT_TRANS         0.0000E+00            NORMALIZED  0.0000E+00

@glemieux
Copy link
Contributor

@peterdschwartz can you point me to the line of code with the git blame that you noted earlier, please?

@peterdschwartz
Copy link
Contributor Author

@glemieux This Code block is what I was referring to. The transport tendency is not added to carbon leached pool even though the corresponding terms are added for Nitrogen and Phosphorus.

More experimenting showed that bypassing the SoilLittVertTransp calculation, the restart failure goes away. For FATES, the UpdateLitterFluxes is done just prior that adjusts the inputs to the SoilLittVertTransp that could be the cause. Line

@rljacob
Copy link
Member

rljacob commented May 2, 2024

Waiting to see about FATES side of this PR.

@peterdschwartz
Copy link
Contributor Author

Since there's a previous fates related issue linked above with the same errors, I'm going to just clean the branch up for the other issues and follow the review comments. The other issue can be used to the issue.

@glemieux
Copy link
Contributor

Since there's a previous fates related issue linked above with the same errors, I'm going to just clean the branch up for the other issues and follow the review comments. The other issue can be used to the issue.

Sorry for the delay in trying to address the fates-side of this. Just to clarify, you're saying your going to move forwards towards integration @peterdschwartz? If so, that sounds good to me.

Corrected GridCBalanceCheck to use absolute value for error.
Adjusted col_cf_setvalues to fix undetected C imbalances -- Still error with FATES
…n input to col_cf%litfall

Added more output when an carbon imbalance is detected.

Column Carbon balance is now checked with FATES active.
Adjusted whitespaces and comments.
@peterdschwartz peterdschwartz force-pushed the peterdschwartz/lnd/fix-balance-errors branch from cac574e to e6097d2 Compare May 23, 2024 15:41
@peterdschwartz
Copy link
Contributor Author

Since there's a previous fates related issue linked above with the same errors, I'm going to just clean the branch up for the other issues and follow the review comments. The other issue can be used to the issue.

Sorry for the delay in trying to address the fates-side of this. Just to clarify, you're saying your going to move forwards towards integration @peterdschwartz? If so, that sounds good to me.

@glemieux Yes, and now I know how to how to reliably reproduce the issue, we can use #6125 to track progress. I will share a branch with the changes to do it.

@bishtgautam I have rebased the branch and addressed your comments. There's potentially a couple of different ways to handle the else condition in the PhenologyMod, so I went with what I believe to be the simplest and added a comment explaining why. Let me know what you think

e3sm_developer came back [BfB] with the exception of one FATES test that will FAIL restart comparison for the time being.

Comment on lines +2564 to +2572
if ( abs(ETo(p,kmo)) > 0._r8) then
p2ETo(p,kmo) = xp(p,kmo)/ETo(p,kmo)
else ! P:PET is undefined.
! Setting to a fill value ( 'spval' ) would
! require nested if statements due to
! the weighting of previous years (i.e., p2ETo and prev_p2ETo_bar )
! So, set to zero for simplicity.
p2ETo(p,kmo) = 0._r8
end if
Copy link
Contributor

Choose a reason for hiding this comment

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

@peterdschwartz This looks good to me. Thanks for the comment explaining the logic.

@rljacob
Copy link
Member

rljacob commented May 30, 2024

This is ready to merge.

@bishtgautam bishtgautam self-requested a review May 30, 2024 17:37
bishtgautam added a commit that referenced this pull request May 30, 2024
)

Carbon balance was never checked correctly and let through negative values.
Initialization of Ecosystem variables needed to be re-adjusted, and balance
checking for FATES needed to be changed, which causes round-off differences
in CMASS_BALANCE_ERROR for fate_cold_allvars test.

Also included divide-by-zero check in PhenologyMod.F90 to fix fpe in debug mode.

[non-BFB] for one FATES test.
Fixes #6120
Fixes #6203
Fixes #6177
bishtgautam added a commit that referenced this pull request Jun 4, 2024
)

Re-merging the PR as a FATES test has been moved to a different test suite.
@bishtgautam bishtgautam merged commit db11f6c into E3SM-Project:master Jun 5, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants