Skip to content

Avoid very rare NaN in set_cld - #51

Open
Thomas Bendall (tommbendall) wants to merge 15 commits into
MetOffice:mainfrom
tommbendall:set_cld_nan_bug
Open

Thomas Bendall (tommbendall) wants to merge 15 commits into
MetOffice:mainfrom
tommbendall:set_cld_nan_bug

Conversation

@tommbendall

@tommbendall Thomas Bendall (tommbendall) commented Nov 23, 2026 •

Copy link
Copy Markdown

PR Summary

This PR fixes an issue that can cause a NaN in an incredibly rare scenario.

Sci/Tech Reviewer: James Manners (@mo-jmanners)
Code Reviewer:

I saw a Cloud fraction greater than 1 failure from socrates_set_cld.F90 in a very high-resolution LFRic AMIP run. This was triggered by the frac_cloud arrays containing a NaN at a single point, but none of the inputs to Socrates contained a NaN.

Why this issue is rare

All of these must hold at once:

  1. cca is exactly 1.0 (or area_fraction exactly 0), so frac is exactly 0.
  2. The liquid and ice fractions are both non-zero (mixed-phase cloud).
  3. The total liquid and ice mixing ratios are equal to the last digit. If they
    differ, one fraction stays positive after the call to split_mixed_phase. Then the denominator is
    positive, and the result is finite.

Cause

For the failure I saw, the inputs entering Socrates at that cell were:

  • stratiform frac = 0, from area_fraction*(1-cca) with area_fraction = 1
    and cca = 1
  • liq_frac = 0.66457766, ice_frac = 1.0
  • liquid mixing ratio, mr_cl + conv_liquid_mmr
    = 3.32288822E-4 + 7.11565488E-4 = 1.0438543104100972E-3
  • ice mixing ratio, mr_ice + conv_frozen_mmr
    = 8.8357972E-4 + 1.60274591E-4 = 1.0438543104100972E-3

The sums are equal to 17 digits.

The code tests frac_liq + frac_ice > eps before splitting the overlapping
ice and liquid fraction. Here it is 1.66, so the splitting code runs. split_mix_phase then removes the overlap
frac_mix = liq + ice - frac = 1.66. With equal mixing ratios and
frac = 0, both frac_liq and frac_ice come out exactly 0.

The next lines compute frac*frac_liq/(frac_liq+frac_ice), which is
0*0/(0+0) = NaN, and this goes into both cloud types.

Fix

In the stratiform and convective blocks of the two-type ice/liquid
representation, and the stratiform and convective blocks of the four-type
representation, we can test frac_liq + frac_ice > 0 after split_mix_phase. If it is false, both types are set to 0.
Otherwise the existing code runs unchanged.

Code Quality Checklist

(Some checks are automatically carried out via the CI pipeline)

  • 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

Testing

  • If shared files have been modified, I have run the UM and LFRic Apps rose stem suites
  • If any tests fail (rose-stem or CI) the reason is understood and acceptable (eg. kgo changes)
  • I have added tests to cover new functionality as appropriate (eg. system tests, unit tests, etc.)

trac.log

Output from Socrates quick_tests

Testing code compiled in /home/users/thomas.bendall/socrates/set_cld_nan_bug/bin
---
Testing simple calls to the runes interface:
Matched gfortran output
OK
---
Testing the runes_nc driver using LFRic diagnostic input:
Matched SW output
Matched LW output
OK
---
Testing raw_input to convert column data into input files:
OK
---
Testing Cl_run_cdl for ICRCCM case 27 (LW) on AER profiles:
OK
---
Testing Cl_run_cdf on multiple profiles:
OK
---
Test Cl_run_cdf for CIRC case 6
(HadGEM, GA7, SES and 300/260 band spectral files):
OK
---
Testing pseudo-spherical geometry code:
Calculating zenith angles:  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
OK
---
Testing photolysis and non-LTE code:
Mapping sub-bands to channels.
Matched gfortran output
OK
---
Test McICA code (create mcica_data file and run on CRM profile):
OK
---
Testing creation of aerosol properties for a spectral file:
Running scatter_90...
Running scatter_average_90...
Running prep_spec...
Matched gfortran12 output
OK
---
Testing generating and running with prescribed optical properties:
Running raw_input...
Running Cscatter... please wait...
Running Cscatter... please wait...
Running Cscatter_average...
Running Cscatter_average...
Running prep_opt_profile...
Running Cl_run_cdl...
All done.
OK
---
Testing Crun_mono to calculate radiances:
Matched ref output
OK
---
Testing Ccorr_k to generate correlated-k coefficients:
Matched gfortran12 output
OK
---
Testing flexchem chemistry:
Matched KGO output
OK
---
Testing CDL scripts:
OK
---
All passed.

Test Suite Results - lfric_apps - socrates_set_cld_nan/run4

Suite Information

Item Value
Suite Name socrates_set_cld_nan/run4
Suite User thomas.bendall
Workflow Start 2026-12-02T08:47:38
Groups Run lfric_atm
Dependency Reference Main Like
casim MetOffice/casim@2026.07.1 True
jules MetOffice/jules@2026.07.1 True
lfric_apps tommbendall/lfric_apps@socrates_set_cld_nan False
lfric_core MetOffice/lfric_core@2026.07.1 True
moci MetOffice/moci@2026.07.1 True
SimSys_Scripts MetOffice/SimSys_Scripts@2026.07.1 True
socrates tommbendall/socrates@fef5a6a True
socrates-spectral MetOffice/socrates-spectral@2026.07.1 True
ukca MetOffice/ukca@2026.07.1 True

Task Information

✅ succeeded tasks - 442

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)

Copilot helped me debug and understand this problem and possible fixes

Documentation

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

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

@github-actions github-actions Bot added the cla-required The CLA has not yet been signed by the author of this PR - added by GA label Nov 23, 2026
@github-actions github-actions Bot added cla-signed The CLA has been signed as part of this PR - added by GA and removed cla-required The CLA has not yet been signed by the author of this PR - added by GA labels Dec 1, 2026
@mo-jmanners

Copy link
Copy Markdown
Member

Hi Tom,

I think this failure can only happen if frac=0, in which case I think it would be better to avoid calling split_mix_phase entirely. i.e. replace the if test before the call:
if (frac_liq(l, k) + frac_ice(l, k) > eps) then
with
if (frac(l, k) > 0.0_RealK .and. frac_liq(l, k) + frac_ice(l, k) > eps) then

You'd still need an else clause in the l_combine case to set frac_liq and frac_ice to zero.
I think this would be cleaner overall.

James

@tommbendall

Copy link
Copy Markdown
Author

Hi Tom,

I think this failure can only happen if frac=0, in which case I think it would be better to avoid calling split_mix_phase entirely. i.e. replace the if test before the call: if (frac_liq(l, k) + frac_ice(l, k) > eps) then with if (frac(l, k) > 0.0_RealK .and. frac_liq(l, k) + frac_ice(l, k) > eps) then

You'd still need an else clause in the l_combine case to set frac_liq and frac_ice to zero. I think this would be cleaner overall.

James

Thanks, your suggestion is much tidier. I've done that, and checked this against the LFRic-Apps tests. There don't seem to be any KGO changes so this won't require a linked PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code looks fine.

I don't think any of the code changes are now from AI so I think the comment at the top of socrates_set_cld can be removed.

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

Labels

cla-signed The CLA has been signed as part of this PR - added by GA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants