Skip to content

Fix type hinting and simplify implementation of combine_distributions - #3445

Merged
paulromano merged 16 commits into
openmc-dev:developfrom
GuySten:combine
Jan 21, 2026
Merged

paulromano merged 16 commits into
openmc-dev:developfrom
GuySten:combine

Conversation

@GuySten

@GuySten GuySten commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

Description

This pr change the implementation of combine_distributions to support combination of any Univariate distributions with specified probabilities.

The new implementation knows to combine analytically discrete and normal distributions.

Fixes #3105

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

@GuySten
GuySten marked this pull request as ready for review June 16, 2025 16:17
@GuySten
GuySten requested review from pshriwise and shimwell August 12, 2025 03:52
@GuySten GuySten added the Bugs label Aug 14, 2025

@paulromano paulromano left a comment

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.

There are a few problems with this PR in my opinion. First, multiple normal distributions cannot be merged together as the combined distribution would not be normally distributed. The more fundamental issue is that for combine_distributions, it was never intended to handle distributions other than Discrete/Tabular as those are the only ones currently that are allowed have a non-unity integral. I think for now, the proper solution to #3105 is to simply check that the distributions passed to combine_distributions are either Discrete or Tabular and update the type hint.

@GuySten

GuySten commented Jan 16, 2026

Copy link
Copy Markdown
Contributor Author

I've fixed the type hinting, added type checking and simplified the implementation a bit.

@GuySten
GuySten requested a review from paulromano January 16, 2026 21:37
@paulromano paulromano changed the title Fix a bug in openmc.data.combine_distributions Fix type hinting and simplify implementation of combine_distributions Jan 21, 2026
@paulromano
paulromano enabled auto-merge (squash) January 21, 2026 05:06
@paulromano
paulromano merged commit 2691ff8 into openmc-dev:develop Jan 21, 2026
22 of 31 checks passed
@GuySten
GuySten deleted the combine branch January 21, 2026 14:44
@yrrepy

yrrepy commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Howdy,
so I'm reasonably sure that this broke my R2S during get_photon_energy.

I am using a JEFF-3.3 chain, there are photon mixture distributions for:
Th230, Th232, Pa231, U232, U234, U235, U236, U238, Pu239

I also see many of these are on the official JEFF-4.0 chains (https://openmc.org/data/#jeff-4-0-1)

I guess previously it was silently failing?

Where it fails:

  File "/home/etc/OMC-mR2S_v6.py", line 918, in <module>
    generate_photon_sources_mpi(args.model_photon, args.campaign, args.dose_function, args.directory)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/etc/OMC-mR2S_v6.py", line 588, in generate_photon_sources_mpi
    photon_energy = material.get_decay_photon_energy(
                                                     clip_tolerance=1e-4,
                                                     units='Bq',
                                                     exclude_nuclides=exclude_nuc
                                                     )
  File "/home/pe427y/Codes/py313/lib64/python3.13/site-packages/openmc/material.py", line 401, in get_decay_photon_energy
    combined = openmc.data.combine_distributions(dists, probs)
  File "/home/pe427y/Codes/py313/lib64/python3.13/site-packages/openmc/stats/univariate.py", line 1922, in combine_distributions
    cv.check_type(f'dists[{i}]', dist, (Discrete, Tabular))
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pe427y/Codes/py313/lib64/python3.13/site-packages/openmc/checkvalue.py", line 41, in check_type
    raise TypeError(msg)
TypeError: Unable to set "dists[809]" to "<openmc.stats.univariate.Mixture object at 0x7f5d6d108850>" which is not one of the following types: "Discrete, Tabular"
Traceback (most recent call last):

The problematic photon source mixture on a JEFF-3.3 chain file:

  <nuclide name="U235" half_life="2.22102e+16" decay_modes="2" decay_energy="4678887.7" reactions="12">
    <decay type="alpha" target="Th231" branching_ratio="1.0"/>
    <decay type="sf" target="U235" branching_ratio="7.2e-11"/>
    <source type="mixture" particle="photon">
      <pair probability="1.0">
        <dist type="tabular" interpolation="histogram">
          <parameters>0.0 140000.0 300000.0 500000.0 700000.0 1000000.0 1500000.0 2000000.0 2500000.0 3000000.0 4000000.0 5000000.0 6000000.0 7000000.0 10000000.0 5.189951678644752e-33 1.6796690775253955e-32 1.8548490651832704e-32 1.603596506230337e-32 9.033942537964795e-33 4.713505853171821e-33 2.182070655791734e-33 1.3136836990653461e-33 8.28217209559098e-34 3.8235372393008833e-34 1.4399330328638888e-34 5.368003528935868e-35 2.111046096044921e-35 1.2821985662241311e-36 0.0</parameters>
        </dist>
      </pair>
      <pair probability="1.0">
        <dist type="discrete">
          <parameters>4176.0 13409.0 19595.0 31580.0 34550.0 40900.0 41950.0 51180.0 54030.0 54180.0 64350.0 72500.0 74950.0 89953.0 93350.0 96130.0 104831.0 105610.0 108600.0 109180.0 115900.0 119990.0 136700.0 140760.0 142600.0 143760.0 150940.0 163360.0 172270.0 182520.0 185714.0 194940.0 198930.0 202110.0 205310.0 215290.0 221400.0 228780.0 233470.0 240880.0 246870.0 266470.0 275130.0 275430.0 281450.0 282960.0 289560.0 290200.0 291700.0 301740.0 317080.0 343740.0 345880.0 356050.0 387830.0 410230.0 448330.0 2.413878143615725e-18 7.226080110994538e-18 1.9602063201128385e-21 5.346017236671377e-21 7.662624705895642e-22 8.91002872778563e-21 1.6038051710014132e-20 8.019025855007066e-21 5.346017236671378e-22 8.91002872778563e-21 5.346017236671377e-21 3.564011491114252e-20 1.782005745557126e-20 1.1301130903045706e-18 1.8435770088742827e-18 2.851209192891402e-20 2.1385504538004174e-19 4.07430178788491e-19 2.212293034819278e-19 5.025256202471095e-19 1.782005745557126e-20 8.019025855007066e-21 3.564011491114252e-21 6.237020109449941e-20 1.6038051710014134e-21 3.4161050142330107e-18 2.3166074692242635e-20 1.582421102054728e-18 1.9602063201128385e-21 1.229583964434417e-19 1.782005745557126e-17 1.995846435023981e-19 1.1404836771565607e-20 3.3679908591029683e-19 1.5628190388535994e-18 8.91002872778563e-21 3.7065719507588217e-20 2.3166074692242635e-21 1.1226636197009894e-20 2.3166074692242635e-20 1.7998258030126973e-20 1.9602063201128385e-21 1.2474040218899881e-20 5.346017236671378e-22 1.9602063201128385e-21 1.6038051710014134e-21 1.0692034473342756e-21 1.2474040218899881e-21 1.2474040218899881e-20 1.6038051710014134e-21 3.564011491114252e-22 1.0692034473342756e-21 1.2474040218899881e-20 1.6038051710014134e-21 1.2474040218899881e-20 1.0692034473342756e-21 3.564011491114252e-22</parameters>
        </dist>
      </pair>
    </source>

jon-proximafusion pushed a commit to shimwell/openmc that referenced this pull request Apr 13, 2026
apingegno pushed a commit to apingegno/openmc that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

probs arg ignored if openmc.stats.Normal passed to openmc.stats.combine_distributions

3 participants