Skip to content

Incorrect (or confusing?) indexing in "if ( any(fert_cft(begg:endg,cft_ub+1:)" in surfrdUtilsMod.F90 #3032

Open

Description

@slevis-lmwg

Brief summary of bug

@swensosc brought to my attention the following incorrect use of cft_ub in surfrdUtilsMod.F90

if ( any(wt_cft(begg:endg,cft_ub+1:)
...
if ( any(fert_cft(begg:endg,cft_ub+1:)

These variables are dimensioned and later asserted as

real(r8), intent(inout) :: wt_cft(begg:, cft_lb:)
real(r8), intent(inout) :: fert_cft(begg:, cft_lb:)
...
SHR_ASSERT_ALL_FL((ubound(wt_cft)   == (/endg, cft_lb+cftsize-1/)), sourcefile, __LINE__)
SHR_ASSERT_ALL_FL((ubound(fert_cft) == (/endg, cft_lb+cftsize-1/)), sourcefile, __LINE__)

so in the if statements we likely need to replace cft_ub+1 with npcropmin, though testing will tell us more.

General bug information

CTSM version you are using: ctsm5.3 but this goes back to ctsm5.1

Does this bug cause significantly incorrect results in the model's science?
This bug makes the above if statements useless and their error checks null. This means that users could have been running with .not. use_crop and non-zero prognostic crop amounts inadvertently.

Configurations affected: .not. use_crop

Activity

  1. self-assigned this
    on Mar 25, 2025
  2. added theissue type on Mar 25, 2025
  3. moved this from Todo to No status in LMWG: Sprint Planning Boardon Mar 25, 2025
  4. added
    nextthis should get some attention in the next week or two. Normally each Thursday SE meeting.
    on Mar 25, 2025
  5. removed
    nextthis should get some attention in the next week or two. Normally each Thursday SE meeting.
    on Mar 27, 2025
  6. wwieder commented on Mar 27, 2025

    @wwieder
    Contributor

    In the SE meeting there was some uncertainty if this is actually a bug, although correcting to npcropmin could be more clear?

  7. added
    code healthimproving internal code structure to make easier to maintain (sustainability)
    on Mar 27, 2025
  8. removed theissue type on Mar 27, 2025
  9. added
    priority: lowBackground task that doesn't need to be done right away.
    and removed
    bugsomething is working incorrectly
    on Mar 27, 2025
  10. added this to the Eventually milestone on Mar 27, 2025
  11. swensosc commented on Mar 27, 2025

    @swensosc
    Contributor
  12. samsrabin commented on Mar 27, 2025

    @samsrabin
    Member

    Here is the context for those checks:

    if ( .not. use_crop )then
    if ( any(wt_cft(begg:endg,cft_ub+1:) /= 0.0_r8) )then
    call endrun(msg = subname//' without prognostic crops (use_crop=F) and weight of CFT of prognostic crop'//&
    ' is not zero as expected' // errMsg(sourcefile, __LINE__))
    end if
    if ( any(fert_cft(begg:endg,cft_ub+1:) /= 0.0_r8) )then
    call endrun(msg = subname//' without prognostic crops (use_crop=F) and fertilizer of prognostic crop'// &
    ' is not zero as expected' // errMsg(sourcefile, __LINE__))
    end if
    end if

    So they're checking that no PFTs above the crop PFTs have area or fertilizer read in. This is unnecessary because we have assertions at the top ensuring that there are no such PFTs:

    SHR_ASSERT_ALL_FL((ubound(wt_cft) == (/endg, cft_lb+cftsize-1/)), sourcefile, __LINE__)
    SHR_ASSERT_ALL_FL((ubound(fert_cft) == (/endg, cft_lb+cftsize-1/)), sourcefile, __LINE__)

    And also, as @swensosc pointed out, the allocations of those variables stop at cft_ub:

    allocate (wt_cft (begg:endg, cft_lb:cft_ub ))
    allocate (fert_cft (begg:endg, cft_lb:cft_ub ))

    The checks in question presumably don't segfault because the compilers see those allocations and/or assertions and thus optimize these checks away. There's thus an argument that we should just delete these checks. However, we might want to leave them in so that, if those protections go away, these checks kick in.

    I think it'd be best to leave them in but add an explanatory comment.

  13. changed the title [-]Incorrect indexing in "if ( any(fert_cft(begg:endg,cft_ub+1:)" in surfrdUtilsMod.F90[/-] [+]Incorrect (or confusing?) indexing in "if ( any(fert_cft(begg:endg,cft_ub+1:)" in surfrdUtilsMod.F90[/+] on Mar 27, 2025
  14. swensosc commented on Mar 27, 2025

    @swensosc
    Contributor
  15. swensosc commented on Mar 27, 2025

    @swensosc
    Contributor
  16. slevis-lmwg commented on Mar 27, 2025

    @slevis-lmwg
    ContributorAuthor

    I'm open to additional feedback here, and this is my plan currently:
    (When I get to this issue) I will run various scenarios (mainly I'm thinking Sp vs. BgcCrop) where I output the index values and the contents of these variables to see what's really happening. I hope this helps me determine any necessary changes to the code plus comments that I should add that explain the code's behavior.

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

Metadata

Metadata

Assignees

Labels

code healthimproving internal code structure to make easier to maintain (sustainability)priority: lowBackground task that doesn't need to be done right away.

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions