Skip to content

Fix possible divide-by-zero error in MoL code#381

Open
Thomas Bendall (tommbendall) wants to merge 4 commits intoMetOffice:mainfrom
tommbendall:TBendall/mol_division_fix
Open

Fix possible divide-by-zero error in MoL code#381
Thomas Bendall (tommbendall) wants to merge 4 commits intoMetOffice:mainfrom
tommbendall:TBendall/mol_division_fix

Conversation

@tommbendall
Copy link
Copy Markdown
Contributor

@tommbendall Thomas Bendall (tommbendall) commented Mar 18, 2026

PR Summary

Sci/Tech Reviewer: James Kent (@jameskent-metoffice)
Code Reviewer: Benjamin Went (@MetBenjaminWent)

A calculation in the Koren limiter for the MoL scheme involves the calculation r = y / x, which has been written as:

r = (y + SMALL_R_TRAN) / (x + SMALL_R_TRAN)

to avoid a potential divide-by-zero error. However, this can still lead to a divide-by-zero error in the case that x = -SMALL_R_TRAN. This PR fixes that by changing the calculation to:

r = y / (MAX(ABS(x), SMALL_R_TRAN)*SIGN(1.0_r_tran, x))

This cannot be zero in the denominator, and is generally equal to y / x since ABS(x)*sign(1.0, x) = x.

The other improvement is that now r = 0 if y = 0.

Results

Plots showing the change to the lfric_atm MoL test can be found in this PDF:
results_mol_division_fix_lfric_atm.pdf

Code Quality Checklist

  • I have performed a self-review of my own code
  • My code follows the project's style guidelines
  • Comments have been included that aid understanding and enhance the readability of the code
  • My changes generate no new warnings
  • All automated checks in the CI pipeline have completed successfully

Testing

  • I have tested this change locally, using the LFRic Apps rose-stem suite
  • If any tests fail (rose-stem or CI) the reason is understood and acceptable (e.g. kgo changes)
  • I have added tests to cover new functionality as appropriate (e.g. system tests, unit tests, etc.)
  • Any new tests have been assigned an appropriate amount of compute resource and have been allocated to an appropriate testing group (i.e. the developer tests are for jobs which use a small amount of compute resource and complete in a matter of minutes)

trac.log

Test Suite Results - lfric_apps - mol_division_fix/run2

Suite Information

Item Value
Suite Name mol_division_fix/run2
Suite User thomas.bendall
Workflow Start 2026-03-17T14:16:22
Groups Run all
Dependency Reference Main Like
casim MetOffice/casim@2026.03.2 True
jules MetOffice/jules@2026.03.2 True
lfric_apps tommbendall/lfric_apps@TBendall/mol_division_fix False
lfric_core MetOffice/lfric_core@2026.03.2 True
moci MetOffice/moci@2026.03.2 True
SimSys_Scripts MetOffice/SimSys_Scripts@2026.03.2 True
socrates MetOffice/socrates@2026.03.2 True
socrates-spectral MetOffice/socrates-spectral@2026.03.2 True
ukca MetOffice/ukca@2026.03.2 True

Task Information

✅ succeeded tasks - 1511

Security Considerations

  • I have reviewed my changes for potential security issues
  • Sensitive data is properly handled (if applicable)
  • Authentication and authorisation are properly implemented (if applicable)

Performance Impact

  • Performance of the code has been considered and, if applicable, suitable performance measurements have been conducted

AI Assistance and Attribution

  • Some of the content of this change has been produced with the assistance of Generative AI tool name (e.g., Met Office Github Copilot Enterprise, Github Copilot Personal, ChatGPT GPT-4, etc) and I have followed the Simulation Systems AI policy (including attribution labels)

Documentation

  • Where appropriate I have updated documentation related to this change and confirmed that it builds correctly

PSyclone Approval

  • If you have edited any PSyclone-related code (e.g. PSyKAl-lite, Kernel interface, optimisation scripts, LFRic data structure code) then please contact the TCD Team

Sci/Tech Review

  • I understand this area of code and the changes being added
  • The proposed changes correspond to the pull request description
  • Documentation is sufficient (do documentation papers need updating)
  • Sufficient testing has been completed

(Please alert the code reviewer via a tag when you have approved the SR)

Code Review

  • All dependencies have been resolved
  • Related Issues have been properly linked and addressed
  • CLA compliance has been confirmed
  • Code quality standards have been met
  • Tests are adequate and have passed
  • Documentation is complete and accurate
  • Security considerations have been addressed
  • Performance impact is acceptable

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.

This is a very small change to fix a possible divide by zero in the Koren kernels. The method chosen is sensible. There are kgo changes in tests that run with MoL and Koren, but all the plots look good and I am happy they are correct. Approved for science review.

@iboutle
Copy link
Copy Markdown
Contributor

iboutle commented Apr 7, 2026

Benjamin Went (@MetBenjaminWent) this PR it appears is blocking #326, and I think any KGO changing PR is likely to encounter the issues that this PR fixes - therefore it would be good if we could prioritise getting this on to help other PRs along.

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

Labels

bug Something isn't working KGO This PR contains changes to KGO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants