Skip to content

Commit c2cccd3

Browse files
committed
fix pulse-height
1 parent e783e01 commit c2cccd3

3 files changed

Lines changed: 77 additions & 16 deletions

File tree

src/tallies/tally_scoring.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,9 @@ void score_pulse_height_tally(Particle& p, const vector<int>& tallies)
27302730
int orig_cell = p.coord(0).cell();
27312731
double orig_E_last = p.E_last();
27322732

2733+
// Set particle in top level
2734+
p.n_coord() = 1;
2735+
27332736
for (auto i_tally : tallies) {
27342737
auto& tally {*model::tallies[i_tally]};
27352738

@@ -2740,7 +2743,6 @@ void score_pulse_height_tally(Particle& p, const vector<int>& tallies)
27402743

27412744
for (auto cell_id : cells) {
27422745
// Temporarily change cell of particle
2743-
p.n_coord() = 1;
27442746
p.coord(0).cell() = cell_id;
27452747

27462748
// Determine index of cell in model::pulse_height_cells
@@ -2756,31 +2758,31 @@ void score_pulse_height_tally(Particle& p, const vector<int>& tallies)
27562758
// we skip the assume_separate break below.
27572759
auto filter_iter = FilterBinIter(tally, p);
27582760
auto end = FilterBinIter(tally, true, &p.filter_matches());
2759-
if (filter_iter == end)
2760-
continue;
2761+
if (filter_iter != end) {
27612762

2762-
// Loop over filter bins.
2763-
for (; filter_iter != end; ++filter_iter) {
2764-
auto filter_index = filter_iter.index_;
2765-
auto filter_weight = filter_iter.weight_;
2763+
// Loop over filter bins.
2764+
for (; filter_iter != end; ++filter_iter) {
2765+
auto filter_index = filter_iter.index_;
2766+
auto filter_weight = filter_iter.weight_;
27662767

2767-
// Loop over scores.
2768-
for (auto score_index = 0; score_index < tally.scores_.size();
2769-
++score_index) {
2768+
// Loop over scores.
2769+
for (auto score_index = 0; score_index < tally.scores_.size();
2770+
++score_index) {
27702771
#pragma omp atomic
2771-
tally.results_(filter_index, score_index, TallyResult::VALUE) +=
2772-
filter_weight;
2772+
tally.results_(filter_index, score_index, TallyResult::VALUE) +=
2773+
filter_weight;
2774+
}
27732775
}
27742776
}
27752777

27762778
// Reset all the filter matches for the next tally event.
27772779
for (auto& match : p.filter_matches())
27782780
match.bins_present_ = false;
27792781
}
2780-
// Restore cell/energy
2781-
p.n_coord() = orig_n_coord;
2782-
p.coord(0).cell() = orig_cell;
2783-
p.E_last() = orig_E_last;
27842782
}
2783+
// Restore cell/energy
2784+
p.n_coord() = orig_n_coord;
2785+
p.coord(0).cell() = orig_cell;
2786+
p.E_last() = orig_E_last;
27852787
}
27862788
} // namespace openmc

tests/unit_tests/pulseheight/__init__.py

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
import pytest
3+
import openmc
4+
5+
6+
@pytest.fixture
7+
def model():
8+
openmc.reset_auto_ids()
9+
model = openmc.Model()
10+
11+
# Define materials
12+
NaI = openmc.Material()
13+
NaI.set_density('g/cm3', 3.7)
14+
NaI.add_element('Na', 1.0)
15+
NaI.add_element('I', 1.0)
16+
17+
# Define geometry: two spheres in each other
18+
s1 = openmc.Sphere(r=1)
19+
s2 = openmc.Sphere(r=2, boundary_type='vacuum')
20+
inner_sphere = openmc.Cell(name='inner sphere', fill=NaI, region=-s1)
21+
outer_sphere = openmc.Cell(name='outer sphere', fill=NaI, region=+s1 & -s2)
22+
model.geometry = openmc.Geometry([inner_sphere, outer_sphere])
23+
24+
# Define settings
25+
model.settings.run_mode = 'fixed source'
26+
model.settings.batches = 1
27+
model.settings.particles = 10000
28+
model.settings.photon_transport = True
29+
model.settings.source = openmc.IndependentSource(
30+
energy=openmc.stats.delta_function(1e6),
31+
particle='photon'
32+
)
33+
34+
# Define tallies
35+
energy_filter = openmc.EnergyFilter([1e3, 1e7])
36+
37+
tally1 = openmc.Tally()
38+
tally1.scores = ['pulse-height']
39+
cell_filter1 = openmc.CellFilter([inner_sphere, outer_sphere])
40+
tally1.filters = [cell_filter1, energy_filter]
41+
42+
tally2 = openmc.Tally()
43+
tally2.scores = ['pulse-height']
44+
cell_filter2 = openmc.CellFilter([outer_sphere, inner_sphere])
45+
tally2.filters = [cell_filter2, energy_filter]
46+
47+
model.tallies = [tally1, tally2]
48+
return model
49+
50+
51+
def test_pulse_height(model, run_in_tmpdir):
52+
sp_path = model.run()
53+
sp = openmc.StatePoint(sp_path)
54+
t1 = sp.tallies[1].mean.squeeze()
55+
t2 = sp.tallies[2].mean.squeeze()
56+
57+
np.testing.assert_array_equal(t1, t2[::-1])
58+
59+

0 commit comments

Comments
 (0)