Skip to content

Commit bd76fc0

Browse files
authored
Fix bug in normalization of tally results with no_reduce (#3619)
1 parent 2d8e006 commit bd76fc0

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/tallies/tally.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,14 @@ void Tally::accumulate()
825825
total_source = 1.0;
826826
}
827827

828+
// Determine number of particles contributing to tally
829+
double contributing_particles = settings::reduce_tallies
830+
? settings::n_particles
831+
: simulation::work_per_rank;
832+
828833
// Account for number of source particles in normalization
829834
double norm =
830-
total_source / (settings::n_particles * settings::gen_per_batch);
835+
total_source / (contributing_particles * settings::gen_per_batch);
831836

832837
if (settings::solver_type == SolverType::RANDOM_RAY) {
833838
norm = 1.0;

tests/unit_tests/test_no_reduce.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Test the settings.no_reduce feature to ensure tallies are correctly
2+
reduced across MPI processes."""
3+
4+
import openmc
5+
import pytest
6+
7+
from tests.testing_harness import config
8+
9+
10+
@pytest.mark.parametrize('no_reduce', [True, False])
11+
def test_no_reduce(no_reduce, run_in_tmpdir):
12+
"""Test that tally results are correct with and without no_reduce."""
13+
14+
# Create simple sphere model with vacuum
15+
model = openmc.Model()
16+
sphere = openmc.Sphere(r=1.0, boundary_type='vacuum')
17+
cell = openmc.Cell(region=-sphere)
18+
model.geometry = openmc.Geometry([cell])
19+
model.settings.run_mode = 'fixed source'
20+
model.settings.batches = 10
21+
model.settings.particles = 100
22+
model.settings.source = openmc.IndependentSource(space=openmc.stats.Point())
23+
model.settings.no_reduce = no_reduce
24+
25+
# Tally: surface current on vacuum boundary
26+
surf_filter = openmc.SurfaceFilter(sphere)
27+
tally = openmc.Tally()
28+
tally.filters = [surf_filter]
29+
tally.scores = ['current']
30+
model.tallies = [tally]
31+
32+
# Run OpenMC with proper MPI arguments if needed
33+
kwargs = {'apply_tally_results': True, 'openmc_exec': config['exe']}
34+
if config['mpi']:
35+
kwargs['mpi_args'] = [config['mpiexec'], '-n', config['mpi_np']]
36+
model.run(**kwargs)
37+
38+
# The tally should be ~1.0 (every particle crosses the surface once)
39+
tally_mean = tally.mean.flatten()[0]
40+
assert tally_mean == pytest.approx(1.0)

0 commit comments

Comments
 (0)