OpenMC/openmc/trigger.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.3 KiB
Python
Raw Permalink Normal View History

from collections.abc import Iterable
from numbers import Real
from xml.etree import ElementTree as ET
import openmc.checkvalue as cv
from .mixin import EqualityMixin
class Trigger(EqualityMixin):
"""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.
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
"""
def __init__(self, trigger_type, threshold):
self.trigger_type = trigger_type
self.threshold = threshold
self._scores = []
def __repr__(self):
string = 'Trigger\n'
string += '{: <16}=\t{}\n'.format('\tType', self._trigger_type)
string += '{: <16}=\t{}\n'.format('\tThreshold', self._threshold)
string += '{: <16}=\t{}\n'.format('\tScores', self._scores)
return string
@property
def trigger_type(self):
return self._trigger_type
@property
def threshold(self):
return self._threshold
@property
def scores(self):
return self._scores
@trigger_type.setter
def trigger_type(self, trigger_type):
cv.check_value('tally trigger type', trigger_type,
['variance', 'std_dev', 'rel_err'])
self._trigger_type = trigger_type
@threshold.setter
def threshold(self, threshold):
cv.check_type('tally trigger threshold', threshold, Real)
self._threshold = threshold
@scores.setter
def scores(self, scores):
2017-12-24 16:06:05 +07:00
cv.check_type('trigger scores', scores, Iterable, str)
# 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):
"""Return XML representation of the trigger
Returns
-------
element : xml.etree.ElementTree.Element
XML element containing trigger data
"""
2022-01-20 17:51:16 -06:00
element = ET.Element("trigger")
element.set("type", self._trigger_type)
element.set("threshold", str(self._threshold))
if len(self._scores) != 0:
element.set("scores", ' '.join(self._scores))
2022-01-20 17:51:16 -06:00
return element
@classmethod
def from_xml_element(cls, elem):
"""Generate trigger object from an XML element
Parameters
----------
elem : xml.etree.ElementTree.Element
XML element
Returns
-------
openmc.Trigger
Trigger object
"""
# Generate trigger object
trigger_type = elem.get("type")
threshold = float(elem.get("threshold"))
trigger = cls(trigger_type, threshold)
# Add scores if present
scores = elem.get("scores")
if scores is not None:
trigger.scores = scores.split()
return trigger