openmc.Tally creation from constructor (#3777)

This commit is contained in:
Jonathan Shimwell 2026-02-06 17:23:11 +01:00 committed by GitHub
parent 6f625f3dea
commit 8a62a97115
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View file

@ -50,6 +50,18 @@ class Tally(IDManagerMixin):
will automatically be assigned
name : str, optional
Name of the tally. If not specified, the name is the empty string.
scores : list of str, optional
List of scores, e.g. ['flux', 'fission']
filters : list of openmc.Filter, optional
List of filters for the tally
nuclides : list of str, optional
List of nuclides to score results for
estimator : {'analog', 'tracklength', 'collision'}, optional
Type of estimator for the tally
triggers : list of openmc.Trigger, optional
List of tally triggers
derivative : openmc.TallyDerivative, optional
A material perturbation derivative to apply to all scores in the tally
Attributes
----------
@ -124,7 +136,9 @@ class Tally(IDManagerMixin):
next_id = 1
used_ids = set()
def __init__(self, tally_id=None, name=''):
def __init__(self, tally_id=None, name='', scores=None, filters=None,
nuclides=None, estimator=None, triggers=None,
derivative=None):
# Initialize Tally class attributes
self.id = tally_id
self.name = name
@ -155,6 +169,19 @@ class Tally(IDManagerMixin):
self._sp_filename = None
self._results_read = False
if filters is not None:
self.filters = filters
if nuclides is not None:
self.nuclides = nuclides
if scores is not None:
self.scores = scores
if estimator is not None:
self.estimator = estimator
if triggers is not None:
self.triggers = triggers
if derivative is not None:
self.derivative = derivative
def __eq__(self, other):
if other.id != self.id:
return False

View file

@ -0,0 +1,19 @@
import openmc
def test_tally_init_args():
"""Test that Tally constructor kwargs are applied correctly."""
filter = openmc.EnergyFilter([0.0, 1.0, 20.0e6])
tally = openmc.Tally(
name='my tally',
scores=['flux', 'fission'],
filters=[filter],
nuclides=['U235'],
estimator='tracklength',
)
assert tally.name == 'my tally'
assert tally.scores == ['flux', 'fission']
assert tally.filters == [filter]
assert tally.nuclides == ['U235']
assert tally.estimator == 'tracklength'