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

Better error messages in interpolate_for_nondim_position #657

Merged

Conversation

Hallberg-NOAA
Copy link
Member

Cleaned up the error messages when there are fatal problems in interpolate_for_nondim_position() in the MOM_neutral_diffusion module to explicitly give an indications of all the problems. These had previously triggered fatal errors (or should have), but had less explicit or incorrect error messages in some cases, so all solutions and behavior are identical in any cases that worked previously.

@Hallberg-NOAA Hallberg-NOAA added the documentation Improvements or additions to documentation label May 29, 2024
Copy link
Member

@marshallward marshallward left a comment

Choose a reason for hiding this comment

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

I believe that the conditionals can be simplified due to the boundedness of interpolate_for_nondim_position, and the error message can be consolidated into a single block.

While this does not need to be addressed now, we should explore if this function might be written without branching, and without MOM_error calls. Both coule be making vectorization and GPU migration more difficult.


! Error handling for problematic cases. It is expected that this should never occur.
if ( (Ppos < Pneg) .or. (dRhoNeg > dRhoPos) .or. &
(interpolate_for_nondim_position < 0.) .or. (interpolate_for_nondim_position > 1.) ) then
Copy link
Member

@marshallward marshallward Jun 24, 2024

Choose a reason for hiding this comment

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

This could be written with any(...).

However, I think the interpolate_for_nondim_position checks for > 1. and < 0. are too conservative. I don't see any way that these conditions could ever be true.

  • Ppos < Pneg is an error, Ppos == Pneg is 0.5, Ppos > Pneg is primarily a flag to inspect dRhoPos and dRhoNeg.
  • dRhoPos < dRhoNeg is an error, dRhoPos > dRhoNeg is a mathematical function that must be between 0 and 1 (inclusve). dRhoPos == dRhoNeg is explicitly either 0, 1, or 0.5 (the final case being double-expressed for Ppos > Pneg and Ppos == Pneg.)

So I don't believe this needs to be checked.

An alternative form is something like

if (Ppos - Pneg > 0. .and. dRhoPos - dRhoNeg >= 0.) then
  if (dRhoPos - dRhoNeg > 0.) then
    r = min(1., max(0., ...)
  else ! dRhoPos == dRhoNeg
    if (dRhoNeg > 0.) then
      r = 0.
    elseif (dRhoNeg < 0.) then
      r = 1.
    else ! dRhoPos == dRhoNeg == 0
      r = 0.5
    endif
  endif
elseif (Ppos - Pneg == 0.) then
  r = 0.5
else ! Ppos < Pneg .or. dRpos < dRhoNeg
  call MOM_error()
endif

BTW I am not sure if Ppos - Pneg == 0 (for example) was deliberately chosen rather than Ppos == Pneg. (I find the latter more readable, but the results could be different for Pneg / Ppos < 10**(-16)). (Nevermind, I don't think this is an issue.) If the exisiting forms were deliberate, maybe this should be noted in a comment. Either way, it should not change the bounds of interpolate_for_nondim_position.

Copy link
Member Author

Choose a reason for hiding this comment

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

These are all good questions. Probably the right person to answer them would be @ashao, who wrote the original version of this code.

I agree with your analysis of how the logic could be simplified. Everything in this commit is retaining the original logic, but just making the messages more explicit. I would be happy to modify this as suggested, but before doing so, I would prefer to hear what @ashao thought about this.

  Cleaned up the error messages when there are fatal problems in
interpolate_for_nondim_position() in the MOM_neutral_diffusion module to
explicitly give an indications of all the problems.  These had previously
triggered fatal errors (or should have), but had less explicit or incorrect
error messages in some cases, so all solutions and behavior are identical in any
cases that worked previously.
@Hallberg-NOAA Hallberg-NOAA force-pushed the inter_for_pos_error_handling branch from fa61ad8 to f85f797 Compare July 11, 2024 17:00
@Hallberg-NOAA
Copy link
Member Author

This PR has been revised as suggested.

@marshallward
Copy link
Member

@Hallberg-NOAA Hallberg-NOAA merged commit c5e5e30 into NOAA-GFDL:dev/gfdl Jul 13, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants