Skip to content

Commit feefcc6

Browse files
shimwelljon-proximafusionGuySten
authored
Adding tally filter type option to statepoint get_tally (#3584)
Co-authored-by: Jon Shimwell <[email protected]> Co-authored-by: GuySten <[email protected]>
1 parent 4011b7a commit feefcc6

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

openmc/statepoint.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def add_volume_information(self, volume_calc):
536536
def get_tally(self, scores=[], filters=[], nuclides=[],
537537
name=None, id=None, estimator=None, exact_filters=False,
538538
exact_nuclides=False, exact_scores=False,
539-
multiply_density=None, derivative=None):
539+
multiply_density=None, derivative=None, filter_type=None):
540540
"""Finds and returns a Tally object with certain properties.
541541
542542
This routine searches the list of Tallies and returns the first Tally
@@ -580,6 +580,9 @@ def get_tally(self, scores=[], filters=[], nuclides=[],
580580
to the same value as this parameter.
581581
derivative : openmc.TallyDerivative, optional
582582
TallyDerivative object to match.
583+
filter_type : type, optional
584+
If not None, the Tally must have at least one Filter that is an
585+
instance of this type. For example `openmc.MeshFilter`.
583586
584587
Returns
585588
-------
@@ -653,6 +656,10 @@ def get_tally(self, scores=[], filters=[], nuclides=[],
653656
if not contains_filters:
654657
continue
655658

659+
if filter_type is not None:
660+
if not any(isinstance(f, filter_type) for f in test_tally.filters):
661+
continue
662+
656663
# Determine if Tally has the queried Nuclide(s)
657664
if nuclides:
658665
if not all(nuclide in test_tally.nuclides for nuclide in nuclides):
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import openmc
2+
3+
4+
def test_get_tally_filter_type(run_in_tmpdir):
5+
"""Test various ways of retrieving tallies from a StatePoint object."""
6+
7+
mat = openmc.Material()
8+
mat.add_nuclide("H1", 1.0)
9+
mat.set_density("g/cm3", 10.0)
10+
11+
sphere = openmc.Sphere(r=10.0, boundary_type="vacuum")
12+
cell = openmc.Cell(fill=mat, region=-sphere)
13+
geometry = openmc.Geometry([cell])
14+
15+
settings = openmc.Settings()
16+
settings.particles = 10
17+
settings.batches = 2
18+
settings.run_mode = "fixed source"
19+
20+
reg_mesh = openmc.RegularMesh().from_domain(cell)
21+
tally1 = openmc.Tally(tally_id=1)
22+
mesh_filter = openmc.MeshFilter(reg_mesh)
23+
tally1.filters = [mesh_filter]
24+
tally1.scores = ["flux"]
25+
26+
tally2 = openmc.Tally(tally_id=2, name="heating tally")
27+
cell_filter = openmc.CellFilter(cell)
28+
tally2.filters = [cell_filter]
29+
tally2.scores = ["heating"]
30+
31+
tallies = openmc.Tallies([tally1, tally2])
32+
model = openmc.Model(
33+
geometry=geometry, materials=[mat], settings=settings, tallies=tallies
34+
)
35+
36+
sp_filename = model.run()
37+
38+
sp = openmc.StatePoint(sp_filename)
39+
40+
tally_found = sp.get_tally(filter_type=openmc.MeshFilter)
41+
assert tally_found.id == 1
42+
43+
tally_found = sp.get_tally(filter_type=openmc.CellFilter)
44+
assert tally_found.id == 2
45+
46+
tally_found = sp.get_tally(filters=[mesh_filter])
47+
assert tally_found.id == 1
48+
49+
tally_found = sp.get_tally(filters=[cell_filter])
50+
assert tally_found.id == 2
51+
52+
tally_found = sp.get_tally(scores=["heating"])
53+
assert tally_found.id == 2
54+
55+
tally_found = sp.get_tally(name="heating tally")
56+
assert tally_found.id == 2
57+
58+
tally_found = sp.get_tally(name=None)
59+
assert tally_found.id == 1
60+
61+
tally_found = sp.get_tally(id=1)
62+
assert tally_found.id == 1
63+
64+
tally_found = sp.get_tally(id=2)
65+
assert tally_found.id == 2

0 commit comments

Comments
 (0)