2020-04-06 15:16:09 -05:00
|
|
|
from collections.abc import Iterable
|
2015-06-28 08:46:50 +07:00
|
|
|
from numbers import Real
|
2023-12-13 08:11:32 -06:00
|
|
|
|
2023-05-09 11:41:04 -04:00
|
|
|
import lxml.etree as ET
|
2015-05-11 17:00:23 -04:00
|
|
|
|
2016-10-06 21:39:57 -05:00
|
|
|
import openmc.checkvalue as cv
|
2020-04-06 15:16:09 -05:00
|
|
|
from .mixin import EqualityMixin
|
2025-08-08 20:47:55 +03:00
|
|
|
from ._xml import get_elem_list, get_text
|
2015-05-11 17:00:23 -04:00
|
|
|
|
|
|
|
|
|
2020-04-01 13:40:17 -05:00
|
|
|
class Trigger(EqualityMixin):
|
2015-06-14 15:39:46 +07:00
|
|
|
"""A criterion for when to finish a simulation based on tally uncertainties.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
trigger_type : {'variance', 'std_dev', 'rel_err'}
|
|
|
|
|
Determine whether to trigger on the variance, standard deviation, or
|
|
|
|
|
relative error of scores.
|
|
|
|
|
threshold : float
|
|
|
|
|
The threshold for the trigger type.
|
2024-05-02 10:00:22 -04:00
|
|
|
ignore_zeros : bool
|
|
|
|
|
Whether to allow zero tally bins to be ignored. Note that this option
|
|
|
|
|
can cause the trigger to fire prematurely if there are zero scores in
|
|
|
|
|
any bin at the first evaluation.
|
|
|
|
|
|
2024-06-21 20:28:56 -05:00
|
|
|
.. versionadded:: 0.15.0
|
2015-06-14 15:39:46 +07:00
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
|
----------
|
|
|
|
|
trigger_type : {'variance', 'std_dev', 'rel_err'}
|
|
|
|
|
Determine whether to trigger on the variance, standard deviation, or
|
|
|
|
|
relative error of scores.
|
|
|
|
|
threshold : float
|
|
|
|
|
The threshold for the trigger type.
|
|
|
|
|
scores : list of str
|
|
|
|
|
Scores which should be checked against the trigger
|
2024-05-02 10:00:22 -04:00
|
|
|
ignore_zeros : bool
|
|
|
|
|
Whether to allow zero tally bins to be ignored.
|
2015-06-14 15:39:46 +07:00
|
|
|
|
|
|
|
|
"""
|
2015-05-11 17:00:23 -04:00
|
|
|
|
2024-05-02 10:00:22 -04:00
|
|
|
def __init__(self, trigger_type: str, threshold: float, ignore_zeros: bool = False):
|
2015-05-11 17:00:23 -04:00
|
|
|
self.trigger_type = trigger_type
|
|
|
|
|
self.threshold = threshold
|
2024-05-02 10:00:22 -04:00
|
|
|
self.ignore_zeros = ignore_zeros
|
2015-05-11 17:00:23 -04:00
|
|
|
self._scores = []
|
|
|
|
|
|
2015-10-08 14:23:19 -04:00
|
|
|
def __repr__(self):
|
|
|
|
|
string = 'Trigger\n'
|
2020-04-01 13:40:17 -05:00
|
|
|
string += '{: <16}=\t{}\n'.format('\tType', self._trigger_type)
|
|
|
|
|
string += '{: <16}=\t{}\n'.format('\tThreshold', self._threshold)
|
2024-05-02 10:00:22 -04:00
|
|
|
string += '{: <16}=\t{}\n'.format('\tIgnore Zeros', self._ignore_zeros)
|
2020-04-01 13:40:17 -05:00
|
|
|
string += '{: <16}=\t{}\n'.format('\tScores', self._scores)
|
2015-10-08 14:23:19 -04:00
|
|
|
return string
|
|
|
|
|
|
2015-05-11 17:00:23 -04:00
|
|
|
@property
|
|
|
|
|
def trigger_type(self):
|
|
|
|
|
return self._trigger_type
|
|
|
|
|
|
|
|
|
|
@trigger_type.setter
|
|
|
|
|
def trigger_type(self, trigger_type):
|
2016-03-23 13:50:27 -04:00
|
|
|
cv.check_value('tally trigger type', trigger_type,
|
2016-07-01 22:57:14 +07:00
|
|
|
['variance', 'std_dev', 'rel_err'])
|
2015-05-11 17:00:23 -04:00
|
|
|
self._trigger_type = trigger_type
|
|
|
|
|
|
2023-06-17 04:34:02 +01:00
|
|
|
@property
|
|
|
|
|
def threshold(self):
|
|
|
|
|
return self._threshold
|
|
|
|
|
|
2015-05-11 17:00:23 -04:00
|
|
|
@threshold.setter
|
|
|
|
|
def threshold(self, threshold):
|
2016-03-23 13:50:27 -04:00
|
|
|
cv.check_type('tally trigger threshold', threshold, Real)
|
2015-05-11 17:00:23 -04:00
|
|
|
self._threshold = threshold
|
|
|
|
|
|
2024-05-02 10:00:22 -04:00
|
|
|
@property
|
|
|
|
|
def ignore_zeros(self):
|
|
|
|
|
return self._ignore_zeros
|
|
|
|
|
|
|
|
|
|
@ignore_zeros.setter
|
|
|
|
|
def ignore_zeros(self, ignore_zeros):
|
|
|
|
|
cv.check_type('tally trigger ignores zeros', ignore_zeros, bool)
|
|
|
|
|
self._ignore_zeros = ignore_zeros
|
|
|
|
|
|
2023-06-17 04:34:02 +01:00
|
|
|
@property
|
|
|
|
|
def scores(self):
|
|
|
|
|
return self._scores
|
|
|
|
|
|
2016-02-02 10:07:08 -06:00
|
|
|
@scores.setter
|
|
|
|
|
def scores(self, scores):
|
2017-12-24 16:06:05 +07:00
|
|
|
cv.check_type('trigger scores', scores, Iterable, str)
|
2016-02-02 10:07:08 -06:00
|
|
|
|
|
|
|
|
# Set scores making sure not to have duplicates
|
|
|
|
|
self._scores = []
|
|
|
|
|
for score in scores:
|
|
|
|
|
if score not in self._scores:
|
|
|
|
|
self._scores.append(score)
|
|
|
|
|
|
2022-01-20 17:51:16 -06:00
|
|
|
def to_xml_element(self):
|
2015-06-14 15:39:46 +07:00
|
|
|
"""Return XML representation of the trigger
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2023-05-09 11:41:04 -04:00
|
|
|
element : lxml.etree._Element
|
2015-06-14 15:39:46 +07:00
|
|
|
XML element containing trigger data
|
|
|
|
|
|
|
|
|
|
"""
|
2015-05-11 17:00:23 -04:00
|
|
|
|
2022-01-20 17:51:16 -06:00
|
|
|
element = ET.Element("trigger")
|
|
|
|
|
element.set("type", self._trigger_type)
|
|
|
|
|
element.set("threshold", str(self._threshold))
|
2024-05-02 10:00:22 -04:00
|
|
|
if self._ignore_zeros:
|
|
|
|
|
element.set("ignore_zeros", "true")
|
2015-05-11 17:00:23 -04:00
|
|
|
if len(self._scores) != 0:
|
2022-01-23 14:03:18 -06:00
|
|
|
element.set("scores", ' '.join(self._scores))
|
2022-01-20 17:51:16 -06:00
|
|
|
return element
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-08-05 21:16:42 +01:00
|
|
|
def from_xml_element(cls, elem: ET.Element):
|
2022-01-20 17:51:16 -06:00
|
|
|
"""Generate trigger object from an XML element
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2023-05-09 11:41:04 -04:00
|
|
|
elem : lxml.etree._Element
|
2022-01-20 17:51:16 -06:00
|
|
|
XML element
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
openmc.Trigger
|
|
|
|
|
Trigger object
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# Generate trigger object
|
2025-08-08 20:47:55 +03:00
|
|
|
trigger_type = get_text(elem, "type")
|
|
|
|
|
threshold = float(get_text(elem, "threshold"))
|
|
|
|
|
ignore_zeros = str(get_text(elem, "ignore_zeros", "false")).lower()
|
2024-05-02 10:00:22 -04:00
|
|
|
# Try to convert to bool. Let Trigger error out on instantiation.
|
|
|
|
|
ignore_zeros = ignore_zeros in ('true', '1')
|
|
|
|
|
trigger = cls(trigger_type, threshold, ignore_zeros)
|
2022-01-20 17:51:16 -06:00
|
|
|
|
|
|
|
|
# Add scores if present
|
2025-08-08 20:47:55 +03:00
|
|
|
scores = get_elem_list(elem, "scores", str)
|
2022-01-20 17:51:16 -06:00
|
|
|
if scores is not None:
|
2025-08-08 20:47:55 +03:00
|
|
|
trigger.scores = scores
|
2022-01-20 17:51:16 -06:00
|
|
|
|
|
|
|
|
return trigger
|