Skip to content

Commit

Permalink
Make Nuclide, Element, and Macroscopic subclasses of str
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Dec 17, 2017
1 parent 9744d17 commit ee2f7e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 83 deletions.
31 changes: 10 additions & 21 deletions openmc/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from openmc.data import NATURAL_ABUNDANCE, atomic_mass


class Element(object):
class Element(str):
"""A natural element that auto-expands to add the isotopes of an element to
a material in their natural abundance. Internally, the OpenMC Python API
expands the natural element into isotopes only when the materials.xml file
Expand All @@ -28,25 +28,14 @@ class Element(object):
"""

def __init__(self, name=''):
# Initialize class attributes
self._name = ''

# Set class attributes
self.name = name

def __repr__(self):
return 'Element - {0}\n'.format(self._name)
def __new__(cls, name):
cv.check_type('element name', name, string_types)
cv.check_length('element name', name, 1, 2)
return super(Element, cls).__new__(cls, name)

@property
def name(self):
return self._name

@name.setter
def name(self, name):
cv.check_type('element name', name, string_types)
cv.check_length('element name', name, 1, 2)
self._name = name
return self

def expand(self, percent, percent_type, enrichment=None,
cross_sections=None):
Expand Down Expand Up @@ -93,7 +82,7 @@ def expand(self, percent, percent_type, enrichment=None,
# Get the nuclides present in nature
natural_nuclides = set()
for nuclide in sorted(NATURAL_ABUNDANCE.keys()):
if re.match(r'{}\d+'.format(self.name), nuclide):
if re.match(r'{}\d+'.format(self), nuclide):
natural_nuclides.add(nuclide)

# Create dict to store the expanded nuclides and abundances
Expand All @@ -113,7 +102,7 @@ def expand(self, percent, percent_type, enrichment=None,
root = tree.getroot()
for child in root:
nuclide = child.attrib['materials']
if re.match(r'{}\d+'.format(self.name), nuclide) and \
if re.match(r'{}\d+'.format(self), nuclide) and \
'_m' not in nuclide:
library_nuclides.add(nuclide)

Expand All @@ -134,14 +123,14 @@ def expand(self, percent, percent_type, enrichment=None,
# 0 nuclide is present. If so, set the abundance to 1 for this
# nuclide. Else, raise an error.
elif len(mutual_nuclides) == 0:
nuclide_0 = self.name + '0'
nuclide_0 = self + '0'
if nuclide_0 in library_nuclides:
abundances[nuclide_0] = 1.0
else:
msg = 'Unable to expand element {0} because the cross '\
'section library provided does not contain any of '\
'the natural isotopes for that element.'\
.format(self.name)
.format(self)
raise ValueError(msg)

# If some, but not all, natural nuclides are in the library, add
Expand Down
22 changes: 5 additions & 17 deletions openmc/macroscopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from openmc.checkvalue import check_type


class Macroscopic(object):
class Macroscopic(str):
"""A Macroscopic object that can be used in a material.
Parameters
Expand All @@ -18,22 +18,10 @@ class Macroscopic(object):
"""

def __init__(self, name=''):
# Initialize class attributes
self._name = ''

# Set the Macroscopic class attributes
self.name = name

def __repr__(self):
string = 'Macroscopic - {0}\n'.format(self._name)
return string
def __new__(cls, name):
check_type('name', name, string_types)
return super(Macroscopic, cls).__new__(cls, name)

@property
def name(self):
return self._name

@name.setter
def name(self, name):
check_type('name', name, string_types)
self._name = name
return self
58 changes: 13 additions & 45 deletions openmc/nuclide.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import openmc.checkvalue as cv


class Nuclide(object):
class Nuclide(str):
"""A nuclide that can be used in a material.
Parameters
Expand All @@ -20,54 +20,22 @@ class Nuclide(object):
"""

def __init__(self, name=''):
def __new__(cls, name):
# Initialize class attributes
self._name = ''
orig_name = name

# Set the Material class attributes
self.name = name

def __eq__(self, other):
if isinstance(other, Nuclide):
if self.name != other.name:
return False
else:
return True
elif isinstance(other, string_types) and other == self.name:
return True
else:
return False

def __ne__(self, other):
return not self == other

def __gt__(self, other):
return repr(self) > repr(other)

def __lt__(self, other):
return not self > other
if '-' in name:
name = name.replace('-', '')
name = name.replace('Nat', '0')
if name.endswith('m'):
name = name[:-1] + '_m1'

def __hash__(self):
return hash(repr(self))
msg = 'OpenMC nuclides follow the GND naming convention. Nuclide ' \
'"{}" is being renamed as "{}".'.format(orig_name, name)
warnings.warn(msg)

def __repr__(self):
return 'Nuclide - {0}\n'.format(self._name)
return super(Nuclide, cls).__new__(cls, name)

@property
def name(self):
return self._name

@name.setter
def name(self, name):
cv.check_type('name', name, string_types)
self._name = name

if '-' in name:
self._name = name.replace('-', '')
self._name = self._name.replace('Nat', '0')
if self._name.endswith('m'):
self._name = self._name[:-1] + '_m1'

msg = 'OpenMC nuclides follow the GND naming convention. Nuclide ' \
'"{}" is being renamed as "{}".'.format(name, self._name)
warnings.warn(msg)
return self

0 comments on commit ee2f7e9

Please sign in to comment.