From 3ba8a9f078896ebab9db184d4e2b63ea127f24c0 Mon Sep 17 00:00:00 2001 From: GuySten <62616591+GuySten@users.noreply.github.com> Date: Thu, 26 Feb 2026 04:44:17 +0200 Subject: [PATCH] Correctly score pulse height tally when no cell filter is present (#3821) Co-authored-by: Paul Romano --- include/openmc/tallies/tally.h | 2 +- src/tallies/tally.cpp | 30 ++++++-------- src/tallies/tally_scoring.cpp | 75 +++++++++++++++------------------- 3 files changed, 48 insertions(+), 59 deletions(-) diff --git a/include/openmc/tallies/tally.h b/include/openmc/tallies/tally.h index 6161f823b..66e6ff764 100644 --- a/include/openmc/tallies/tally.h +++ b/include/openmc/tallies/tally.h @@ -212,7 +212,7 @@ extern vector active_collision_tallies; extern vector active_meshsurf_tallies; extern vector active_surface_tallies; extern vector active_pulse_height_tallies; -extern vector pulse_height_cells; +extern vector pulse_height_cells; extern vector time_grid; } // namespace model diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index 32566721f..13225adca 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -2,6 +2,7 @@ #include "openmc/array.h" #include "openmc/capi.h" +#include "openmc/cell.h" #include "openmc/constants.h" #include "openmc/container_util.h" #include "openmc/error.h" @@ -62,7 +63,7 @@ vector active_collision_tallies; vector active_meshsurf_tallies; vector active_surface_tallies; vector active_pulse_height_tallies; -vector pulse_height_cells; +vector pulse_height_cells; vector time_grid; } // namespace model @@ -632,29 +633,24 @@ void Tally::set_scores(const vector& scores) estimator_ = TallyEstimator::COLLISION; break; - case SCORE_PULSE_HEIGHT: + case SCORE_PULSE_HEIGHT: { if (non_cell_energy_present) { fatal_error("Pulse-height tallies are not compatible with filters " "other than CellFilter and EnergyFilter"); } type_ = TallyType::PULSE_HEIGHT; - - // Collecting indices of all cells covered by the filters in the pulse - // height tally in global variable pulse_height_cells - for (const auto& i_filt : filters_) { - auto cell_filter = - dynamic_cast(model::tally_filters[i_filt].get()); - if (cell_filter) { - const auto& cells = cell_filter->cells(); - for (int i = 0; i < cell_filter->n_bins(); i++) { - int cell_index = cells[i]; - if (!contains(model::pulse_height_cells, cell_index)) { - model::pulse_height_cells.push_back(cell_index); - } - } - } + // Collect all unique cell indices covered by this tally. + // If no CellFilter is present, all cells in the geometry are scored. + const auto* cell_filter_ptr = get_filter(); + int n = cell_filter_ptr ? cell_filter_ptr->n_bins() + : static_cast(model::cells.size()); + for (int i = 0; i < n; ++i) { + int32_t cell_index = cell_filter_ptr ? cell_filter_ptr->cells()[i] : i; + if (!contains(model::pulse_height_cells, cell_index)) + model::pulse_height_cells.push_back(cell_index); } break; + } case SCORE_IFP_TIME_NUM: case SCORE_IFP_BETA_NUM: diff --git a/src/tallies/tally_scoring.cpp b/src/tallies/tally_scoring.cpp index fb851f783..cc7b16f44 100644 --- a/src/tallies/tally_scoring.cpp +++ b/src/tallies/tally_scoring.cpp @@ -2672,56 +2672,49 @@ void score_pulse_height_tally(Particle& p, const vector& tallies) for (auto i_tally : tallies) { auto& tally {*model::tallies[i_tally]}; - // Determine all CellFilter in the tally - for (const auto& filter : tally.filters()) { - auto cell_filter = - dynamic_cast(model::tally_filters[filter].get()); - if (cell_filter != nullptr) { + // Find CellFilter in the tally (if any) to determine cells to loop over + const auto* cell_filter = tally.get_filter(); + const auto& cells = + cell_filter ? cell_filter->cells() : model::pulse_height_cells; - const auto& cells = cell_filter->cells(); - // Loop over all cells in the CellFilter - for (auto cell_index = 0; cell_index < cells.size(); ++cell_index) { - int cell_id = cells[cell_index]; + for (auto cell_id : cells) { + // Temporarily change cell of particle + p.n_coord() = 1; + p.coord(0).cell() = cell_id; - // Temporarily change cell of particle - p.n_coord() = 1; - p.coord(0).cell() = cell_id; + // Determine index of cell in model::pulse_height_cells + auto it = std::find(model::pulse_height_cells.begin(), + model::pulse_height_cells.end(), cell_id); + int index = std::distance(model::pulse_height_cells.begin(), it); - // Determine index of cell in model::pulse_height_cells - auto it = std::find(model::pulse_height_cells.begin(), - model::pulse_height_cells.end(), cell_id); - int index = std::distance(model::pulse_height_cells.begin(), it); + // Temporarily change energy of particle to pulse-height value + p.E_last() = p.pht_storage()[index]; - // Temporarily change energy of particle to pulse-height value - p.E_last() = p.pht_storage()[index]; + // Initialize an iterator over valid filter bin combinations. If + // there are no valid combinations, use a continue statement to ensure + // we skip the assume_separate break below. + auto filter_iter = FilterBinIter(tally, p); + auto end = FilterBinIter(tally, true, &p.filter_matches()); + if (filter_iter == end) + continue; - // Initialize an iterator over valid filter bin combinations. If - // there are no valid combinations, use a continue statement to ensure - // we skip the assume_separate break below. - auto filter_iter = FilterBinIter(tally, p); - auto end = FilterBinIter(tally, true, &p.filter_matches()); - if (filter_iter == end) - continue; + // Loop over filter bins. + for (; filter_iter != end; ++filter_iter) { + auto filter_index = filter_iter.index_; + auto filter_weight = filter_iter.weight_; - // Loop over filter bins. - for (; filter_iter != end; ++filter_iter) { - auto filter_index = filter_iter.index_; - auto filter_weight = filter_iter.weight_; - - // Loop over scores. - for (auto score_index = 0; score_index < tally.scores_.size(); - ++score_index) { + // Loop over scores. + for (auto score_index = 0; score_index < tally.scores_.size(); + ++score_index) { #pragma omp atomic - tally.results_(filter_index, score_index, TallyResult::VALUE) += - filter_weight; - } - } - - // Reset all the filter matches for the next tally event. - for (auto& match : p.filter_matches()) - match.bins_present_ = false; + tally.results_(filter_index, score_index, TallyResult::VALUE) += + filter_weight; } } + + // Reset all the filter matches for the next tally event. + for (auto& match : p.filter_matches()) + match.bins_present_ = false; } // Restore cell/energy p.n_coord() = orig_n_coord;