Skip to content

Commit 3ba8a9f

Browse files
GuyStenpaulromano
andauthored
Correctly score pulse height tally when no cell filter is present (#3821)
Co-authored-by: Paul Romano <[email protected]>
1 parent 8081815 commit 3ba8a9f

File tree

3 files changed

+52
-63
lines changed

3 files changed

+52
-63
lines changed

include/openmc/tallies/tally.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ extern vector<int> active_collision_tallies;
212212
extern vector<int> active_meshsurf_tallies;
213213
extern vector<int> active_surface_tallies;
214214
extern vector<int> active_pulse_height_tallies;
215-
extern vector<int> pulse_height_cells;
215+
extern vector<int32_t> pulse_height_cells;
216216
extern vector<double> time_grid;
217217

218218
} // namespace model

src/tallies/tally.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "openmc/array.h"
44
#include "openmc/capi.h"
5+
#include "openmc/cell.h"
56
#include "openmc/constants.h"
67
#include "openmc/container_util.h"
78
#include "openmc/error.h"
@@ -62,7 +63,7 @@ vector<int> active_collision_tallies;
6263
vector<int> active_meshsurf_tallies;
6364
vector<int> active_surface_tallies;
6465
vector<int> active_pulse_height_tallies;
65-
vector<int> pulse_height_cells;
66+
vector<int32_t> pulse_height_cells;
6667
vector<double> time_grid;
6768
} // namespace model
6869

@@ -632,29 +633,24 @@ void Tally::set_scores(const vector<std::string>& scores)
632633
estimator_ = TallyEstimator::COLLISION;
633634
break;
634635

635-
case SCORE_PULSE_HEIGHT:
636+
case SCORE_PULSE_HEIGHT: {
636637
if (non_cell_energy_present) {
637638
fatal_error("Pulse-height tallies are not compatible with filters "
638639
"other than CellFilter and EnergyFilter");
639640
}
640641
type_ = TallyType::PULSE_HEIGHT;
641-
642-
// Collecting indices of all cells covered by the filters in the pulse
643-
// height tally in global variable pulse_height_cells
644-
for (const auto& i_filt : filters_) {
645-
auto cell_filter =
646-
dynamic_cast<CellFilter*>(model::tally_filters[i_filt].get());
647-
if (cell_filter) {
648-
const auto& cells = cell_filter->cells();
649-
for (int i = 0; i < cell_filter->n_bins(); i++) {
650-
int cell_index = cells[i];
651-
if (!contains(model::pulse_height_cells, cell_index)) {
652-
model::pulse_height_cells.push_back(cell_index);
653-
}
654-
}
655-
}
642+
// Collect all unique cell indices covered by this tally.
643+
// If no CellFilter is present, all cells in the geometry are scored.
644+
const auto* cell_filter_ptr = get_filter<CellFilter>();
645+
int n = cell_filter_ptr ? cell_filter_ptr->n_bins()
646+
: static_cast<int>(model::cells.size());
647+
for (int i = 0; i < n; ++i) {
648+
int32_t cell_index = cell_filter_ptr ? cell_filter_ptr->cells()[i] : i;
649+
if (!contains(model::pulse_height_cells, cell_index))
650+
model::pulse_height_cells.push_back(cell_index);
656651
}
657652
break;
653+
}
658654

659655
case SCORE_IFP_TIME_NUM:
660656
case SCORE_IFP_BETA_NUM:

src/tallies/tally_scoring.cpp

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,56 +2672,49 @@ void score_pulse_height_tally(Particle& p, const vector<int>& tallies)
26722672
for (auto i_tally : tallies) {
26732673
auto& tally {*model::tallies[i_tally]};
26742674

2675-
// Determine all CellFilter in the tally
2676-
for (const auto& filter : tally.filters()) {
2677-
auto cell_filter =
2678-
dynamic_cast<CellFilter*>(model::tally_filters[filter].get());
2679-
if (cell_filter != nullptr) {
2680-
2681-
const auto& cells = cell_filter->cells();
2682-
// Loop over all cells in the CellFilter
2683-
for (auto cell_index = 0; cell_index < cells.size(); ++cell_index) {
2684-
int cell_id = cells[cell_index];
2685-
2686-
// Temporarily change cell of particle
2687-
p.n_coord() = 1;
2688-
p.coord(0).cell() = cell_id;
2689-
2690-
// Determine index of cell in model::pulse_height_cells
2691-
auto it = std::find(model::pulse_height_cells.begin(),
2692-
model::pulse_height_cells.end(), cell_id);
2693-
int index = std::distance(model::pulse_height_cells.begin(), it);
2694-
2695-
// Temporarily change energy of particle to pulse-height value
2696-
p.E_last() = p.pht_storage()[index];
2697-
2698-
// Initialize an iterator over valid filter bin combinations. If
2699-
// there are no valid combinations, use a continue statement to ensure
2700-
// we skip the assume_separate break below.
2701-
auto filter_iter = FilterBinIter(tally, p);
2702-
auto end = FilterBinIter(tally, true, &p.filter_matches());
2703-
if (filter_iter == end)
2704-
continue;
2675+
// Find CellFilter in the tally (if any) to determine cells to loop over
2676+
const auto* cell_filter = tally.get_filter<CellFilter>();
2677+
const auto& cells =
2678+
cell_filter ? cell_filter->cells() : model::pulse_height_cells;
2679+
2680+
for (auto cell_id : cells) {
2681+
// Temporarily change cell of particle
2682+
p.n_coord() = 1;
2683+
p.coord(0).cell() = cell_id;
2684+
2685+
// Determine index of cell in model::pulse_height_cells
2686+
auto it = std::find(model::pulse_height_cells.begin(),
2687+
model::pulse_height_cells.end(), cell_id);
2688+
int index = std::distance(model::pulse_height_cells.begin(), it);
2689+
2690+
// Temporarily change energy of particle to pulse-height value
2691+
p.E_last() = p.pht_storage()[index];
2692+
2693+
// Initialize an iterator over valid filter bin combinations. If
2694+
// there are no valid combinations, use a continue statement to ensure
2695+
// we skip the assume_separate break below.
2696+
auto filter_iter = FilterBinIter(tally, p);
2697+
auto end = FilterBinIter(tally, true, &p.filter_matches());
2698+
if (filter_iter == end)
2699+
continue;
27052700

2706-
// Loop over filter bins.
2707-
for (; filter_iter != end; ++filter_iter) {
2708-
auto filter_index = filter_iter.index_;
2709-
auto filter_weight = filter_iter.weight_;
2701+
// Loop over filter bins.
2702+
for (; filter_iter != end; ++filter_iter) {
2703+
auto filter_index = filter_iter.index_;
2704+
auto filter_weight = filter_iter.weight_;
27102705

2711-
// Loop over scores.
2712-
for (auto score_index = 0; score_index < tally.scores_.size();
2713-
++score_index) {
2706+
// Loop over scores.
2707+
for (auto score_index = 0; score_index < tally.scores_.size();
2708+
++score_index) {
27142709
#pragma omp atomic
2715-
tally.results_(filter_index, score_index, TallyResult::VALUE) +=
2716-
filter_weight;
2717-
}
2718-
}
2719-
2720-
// Reset all the filter matches for the next tally event.
2721-
for (auto& match : p.filter_matches())
2722-
match.bins_present_ = false;
2710+
tally.results_(filter_index, score_index, TallyResult::VALUE) +=
2711+
filter_weight;
27232712
}
27242713
}
2714+
2715+
// Reset all the filter matches for the next tally event.
2716+
for (auto& match : p.filter_matches())
2717+
match.bins_present_ = false;
27252718
}
27262719
// Restore cell/energy
27272720
p.n_coord() = orig_n_coord;

0 commit comments

Comments
 (0)