From 53a84514db09f4030fccb89128d4d60498f7012c Mon Sep 17 00:00:00 2001 From: Kevin Rajan Date: Tue, 24 Nov 2026 19:15:00 -0600 Subject: [PATCH] Apply cell density multiplier to MG macroscopic tally scores In multigroup mode, Mgxs::calculate_xs scales the particle's macroscopic total, absorption and nu-fission cross sections by the cell density multiplier, so transport and the total/absorption scores see the scaled density. The non-analog macroscopic scores that read Mgxs::get_xs directly (scatter, nu-scatter, fission, nu-fission, prompt-nu-fission, delayed-nu-fission, decay-rate and kappa-fission) used the nominal material density instead. Apply p.density_mult() in those branches of score_general_mg. Nuclide-wise scores already include it via atom_density, and the analog branches use ratios of macroscopic cross sections in which the multiplier cancels. Add a unit test that checks, on the same particle histories, that absorption + scatter = total and that every reaction rate over total equals the input cross section ratio, for density multipliers 0.5, 1 and 2 with tracklength and collision estimators. Fixes #4070 --- src/tallies/tally_scoring.cpp | 46 ++++++---- .../test_mg_cell_density_tallies.py | 91 +++++++++++++++++++ 2 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 tests/unit_tests/test_mg_cell_density_tallies.py diff --git a/src/tallies/tally_scoring.cpp b/src/tallies/tally_scoring.cpp index ac442848492..fc6c9d8fba2 100644 --- a/src/tallies/tally_scoring.cpp +++ b/src/tallies/tally_scoring.cpp @@ -1667,6 +1667,11 @@ void score_general_mg(Particle& p, int i_tally, int start_index, : data::mg.macro_xs_[p.material()]; auto& macro_xs = data::mg.macro_xs_[p.material()]; + // Macroscopic cross sections returned by get_xs are at the nominal material + // density, so reaction rates computed directly from them must include the + // cell density multiplier (as p.macro_xs() and atom_density already do) + double density_mult = p.density_mult(); + // Find the temperature and angle indices of interest int macro_t = p.mg_xs_cache().t; int macro_a = macro_xs.get_angle_index(p_u); @@ -1764,8 +1769,9 @@ void score_general_mg(Particle& p, int i_tally, int start_index, nuc_xs.get_xs(MgxsType::SCATTER, p_g, nullptr, &p.mu(), nullptr, nuc_t, nuc_a); } else { - score = flux * macro_xs.get_xs(MgxsType::SCATTER, p_g, nullptr, - &p.mu(), nullptr, macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs(MgxsType::SCATTER, p_g, nullptr, &p.mu(), + nullptr, macro_t, macro_a); } } break; @@ -1794,8 +1800,8 @@ void score_general_mg(Particle& p, int i_tally, int start_index, score = atom_density * flux * nuc_xs.get_xs(MgxsType::NU_SCATTER, p_g, nuc_t, nuc_a); } else { - score = - flux * macro_xs.get_xs(MgxsType::NU_SCATTER, p_g, macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs(MgxsType::NU_SCATTER, p_g, macro_t, macro_a); } } break; @@ -1858,8 +1864,8 @@ void score_general_mg(Particle& p, int i_tally, int start_index, score = atom_density * flux * nuc_xs.get_xs(MgxsType::FISSION, p_g, nuc_t, nuc_a); } else { - score = - flux * macro_xs.get_xs(MgxsType::FISSION, p_g, macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs(MgxsType::FISSION, p_g, macro_t, macro_a); } } break; @@ -1910,8 +1916,8 @@ void score_general_mg(Particle& p, int i_tally, int start_index, score = atom_density * flux * nuc_xs.get_xs(MgxsType::NU_FISSION, p_g, nuc_t, nuc_a); } else { - score = - flux * macro_xs.get_xs(MgxsType::NU_FISSION, p_g, macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs(MgxsType::NU_FISSION, p_g, macro_t, macro_a); } } break; @@ -1966,8 +1972,9 @@ void score_general_mg(Particle& p, int i_tally, int start_index, score = atom_density * flux * nuc_xs.get_xs(MgxsType::PROMPT_NU_FISSION, p_g, nuc_t, nuc_a); } else { - score = flux * macro_xs.get_xs( - MgxsType::PROMPT_NU_FISSION, p_g, macro_t, macro_a); + score = + density_mult * flux * + macro_xs.get_xs(MgxsType::PROMPT_NU_FISSION, p_g, macro_t, macro_a); } } break; @@ -2084,8 +2091,9 @@ void score_general_mg(Particle& p, int i_tally, int start_index, nuc_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, nullptr, nullptr, &d, nuc_t, nuc_a); } else { - score = flux * macro_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, - nullptr, nullptr, &d, macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, + nullptr, nullptr, &d, macro_t, macro_a); } score_fission_delayed_dg( i_tally, d_bin, score, score_index, p.filter_matches()); @@ -2097,8 +2105,9 @@ void score_general_mg(Particle& p, int i_tally, int start_index, flux * atom_density * nuc_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, nuc_t, nuc_a); } else { - score = flux * macro_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, - macro_t, macro_a); + score = density_mult * flux * + macro_xs.get_xs( + MgxsType::DELAYED_NU_FISSION, p_g, macro_t, macro_a); } } } @@ -2226,7 +2235,7 @@ void score_general_mg(Particle& p, int i_tally, int start_index, nuc_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, nullptr, nullptr, &d, nuc_t, nuc_a); } else { - score = flux * + score = density_mult * flux * macro_xs.get_xs(MgxsType::DECAY_RATE, p_g, nullptr, nullptr, &d, macro_t, macro_a) * macro_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, @@ -2246,7 +2255,7 @@ void score_general_mg(Particle& p, int i_tally, int start_index, nuc_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, nullptr, nullptr, &d, nuc_t, nuc_a); } else { - score += flux * + score += density_mult * flux * macro_xs.get_xs(MgxsType::DECAY_RATE, p_g, nullptr, nullptr, &d, macro_t, macro_a) * macro_xs.get_xs(MgxsType::DELAYED_NU_FISSION, p_g, @@ -2287,8 +2296,9 @@ void score_general_mg(Particle& p, int i_tally, int start_index, score = atom_density * flux * nuc_xs.get_xs(MgxsType::KAPPA_FISSION, p_g, nuc_t, nuc_a); } else { - score = flux * macro_xs.get_xs( - MgxsType::KAPPA_FISSION, p_g, macro_t, macro_a); + score = + density_mult * flux * + macro_xs.get_xs(MgxsType::KAPPA_FISSION, p_g, macro_t, macro_a); } } break; diff --git a/tests/unit_tests/test_mg_cell_density_tallies.py b/tests/unit_tests/test_mg_cell_density_tallies.py new file mode 100644 index 00000000000..9931eef1101 --- /dev/null +++ b/tests/unit_tests/test_mg_cell_density_tallies.py @@ -0,0 +1,91 @@ +"""Multigroup reaction-rate tallies must honor cell density multipliers. + +In MG mode, transport uses macroscopic cross sections scaled by the cell +density multiplier. Every macroscopic reaction-rate score in a tally must be +evaluated at that same density, so ratios between scores on the same tally +(e.g. scatter/total) are independent of the multiplier and equal to the ratios +of the input cross sections, history by history. +""" +import openmc +import pytest + + +TOTAL = 1.0 +ABSORPTION = 0.5 +SCATTER = 0.5 +FISSION = 0.2 +NU_FISSION = 0.25 +KAPPA_FISSION = 200.0e6 * FISSION + + +def _model(density_mult, estimator): + groups = openmc.mgxs.EnergyGroups([0.0, 20.0e6]) + xs = openmc.XSdata('mat', groups, representation='isotropic') + xs.order = 0 + xs.set_total([TOTAL]) + xs.set_absorption([ABSORPTION]) + xs.set_scatter_matrix([[[SCATTER]]]) + xs.set_fission([FISSION]) + xs.set_nu_fission([NU_FISSION]) + xs.set_kappa_fission([KAPPA_FISSION]) + xs.set_chi([1.0]) + library = openmc.MGXSLibrary(groups) + library.add_xsdata(xs) + library.export_to_hdf5('mgxs.h5') + + mat = openmc.Material() + mat.add_macroscopic('mat') + mat.set_density('macro', 1.0) + materials = openmc.Materials([mat]) + materials.cross_sections = 'mgxs.h5' + + sphere = openmc.Sphere(r=5.0, boundary_type='vacuum') + cell = openmc.Cell(fill=mat, region=-sphere) + cell.density = density_mult + + settings = openmc.Settings() + settings.energy_mode = 'multi-group' + settings.run_mode = 'fixed source' + settings.batches = 3 + settings.particles = 200 + settings.source = openmc.IndependentSource( + space=openmc.stats.Point(), + energy=openmc.stats.Discrete([1.0e6], [1.0]), + ) + + tally = openmc.Tally() + tally.estimator = estimator + tally.scores = ['flux', 'total', 'absorption', 'scatter', 'nu-scatter', + 'fission', 'nu-fission', 'kappa-fission'] + return openmc.Model(openmc.Geometry([cell]), materials, settings, + openmc.Tallies([tally])), tally.id + + +@pytest.mark.parametrize('estimator', ['tracklength', 'collision']) +@pytest.mark.parametrize('density_mult', [0.5, 1.0, 2.0]) +def test_mg_scores_scale_with_cell_density(run_in_tmpdir, density_mult, + estimator): + model, tally_id = _model(density_mult, estimator) + sp_path = model.run() + with openmc.StatePoint(sp_path) as sp: + tally = sp.tallies[tally_id] + mean = {s: tally.get_values(scores=[s]).sum() for s in tally.scores} + + assert mean['total'] > 0.0 + + # Closure on the same particle histories + assert mean['absorption'] + mean['scatter'] == pytest.approx( + mean['total'], rel=1e-10) + + # Every macroscopic reaction rate is consistent with the total + for score, xs in [('absorption', ABSORPTION), ('scatter', SCATTER), + ('nu-scatter', SCATTER), ('fission', FISSION), + ('nu-fission', NU_FISSION), + ('kappa-fission', KAPPA_FISSION)]: + assert mean[score] / mean['total'] == pytest.approx( + xs / TOTAL, rel=1e-10), score + + # The total rate is the flux times the density-scaled total cross section + if estimator == 'tracklength': + assert mean['total'] / mean['flux'] == pytest.approx( + density_mult * TOTAL, rel=1e-10)