OpenMC/openmc/tally_derivative.py

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

131 lines
3.8 KiB
Python
Raw Permalink Normal View History

from numbers import Integral
2023-05-09 11:41:04 -04:00
import lxml.etree as ET
2016-01-30 23:18:08 -05:00
2016-10-30 15:31:19 -04:00
import openmc.checkvalue as cv
from .mixin import EqualityMixin, IDManagerMixin
from ._xml import get_text
2016-01-30 23:43:11 -05:00
class TallyDerivative(EqualityMixin, IDManagerMixin):
"""A material perturbation derivative to apply to a tally.
Parameters
----------
derivative_id : int, optional
Unique identifier for the tally derivative. If none is specified, an
identifier will automatically be assigned
2016-11-13 13:49:30 -05:00
variable : str, optional
Accepted values are 'density', 'nuclide_density', and 'temperature'
material : int, optional
The perturbed material ID
2016-11-13 13:49:30 -05:00
nuclide : str, optional
The perturbed nuclide. Only needed for 'nuclide_density' derivatives.
Ex: 'Xe135'
Attributes
----------
id : int
Unique identifier for the tally derivative
variable : str
Accepted values are 'density', 'nuclide_density', and 'temperature'
material : int
2016-11-13 13:49:30 -05:00
The perturubed material ID
nuclide : str
The perturbed nuclide. Only needed for 'nuclide_density' derivatives.
2016-11-13 13:49:30 -05:00
Ex: 'Xe135'
"""
next_id = 1
used_ids = set()
2016-11-13 13:49:30 -05:00
def __init__(self, derivative_id=None, variable=None, material=None,
nuclide=None):
# Initialize Tally class attributes
self.id = derivative_id
2016-11-13 13:49:30 -05:00
self.variable = variable
self.material = material
self.nuclide = nuclide
def __repr__(self):
string = 'Tally Derivative\n'
2016-11-13 13:49:30 -05:00
string += '{: <16}=\t{}\n'.format('\tID', self.id)
string += '{: <16}=\t{}\n'.format('\tVariable', self.variable)
string += '{: <16}=\t{}\n'.format('\tMaterial', self.material)
if self.variable == 'nuclide_density':
2016-11-13 13:49:30 -05:00
string += '{: <16}=\t{}\n'.format('\tNuclide', self.nuclide)
return string
@property
def variable(self):
return self._variable
@variable.setter
def variable(self, var):
if var is not None:
2017-12-24 16:06:05 +07:00
cv.check_type('derivative variable', var, str)
2016-11-13 13:49:30 -05:00
cv.check_value('derivative variable', var,
('density', 'nuclide_density', 'temperature'))
2016-10-30 15:31:19 -04:00
self._variable = var
@property
def material(self):
return self._material
@material.setter
def material(self, mat):
if mat is not None:
cv.check_type('derivative material', mat, Integral)
2016-10-30 15:31:19 -04:00
self._material = mat
@property
def nuclide(self):
return self._nuclide
@nuclide.setter
def nuclide(self, nuc):
if nuc is not None:
2017-12-24 16:06:05 +07:00
cv.check_type('derivative nuclide', nuc, str)
2016-10-30 15:31:19 -04:00
self._nuclide = nuc
2016-11-13 13:49:30 -05:00
def to_xml_element(self):
"""Return XML representation of the tally derivative
Returns
-------
2023-05-09 11:41:04 -04:00
element : lxml.etree._Element
XML element containing derivative data
"""
element = ET.Element("derivative")
element.set("id", str(self.id))
element.set("variable", self.variable)
element.set("material", str(self.material))
if self.variable == 'nuclide_density':
element.set("nuclide", self.nuclide)
return element
2022-01-20 17:51:16 -06:00
@classmethod
def from_xml_element(cls, elem):
"""Generate tally derivative 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.TallyDerivative
Tally derivative object
"""
derivative_id = int(get_text(elem, "id"))
variable = get_text(elem, "variable")
material = int(get_text(elem, "material"))
nuclide = get_text(elem, "nuclide") if variable == "nuclide_density" else None
2022-01-20 17:51:16 -06:00
return cls(derivative_id, variable, material, nuclide)