Added __eq__ and __ne__ method to Cell, Universe, Lattice and Material classes in Python API

This commit is contained in:
wbinventor@gmail.com 2015-11-25 12:45:57 -05:00
parent aee8970c69
commit fca533e3d1
3 changed files with 133 additions and 10 deletions

View file

@ -83,6 +83,29 @@ class Material(object):
# If specified, this file will be used instead of composition values
self._distrib_otf_file = None
def __eq__(self, other):
if not isinstance(other, Material):
return False
elif self.id != other.id:
return False
elif self.name != other.name:
return False
elif self.density != other.density:
return False
elif self.density_units != other.density_units:
return False
elif self._nuclides != other.nuclides:
return False
elif self._elements != other._elements:
return False
elif self._sab != other._sab:
return False
else:
return True
def __ne__(self, other):
return not self == other
def __repr__(self):
string = 'Material\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)

View file

@ -29,6 +29,17 @@ class Region(object):
def __str__(self):
return ''
def __eq__(self, other):
if not isinstance(other, type(self)):
return False
elif str(self) != str(other):
return False
else:
return True
def __ne__(self, other):
return not self == other
@staticmethod
def from_expression(expression, surfaces):
"""Generate a region given an infix expression.

View file

@ -73,6 +73,29 @@ class Cell(object):
self._translation = None
self._offsets = None
def __eq__(self, other):
if not isinstance(other, Cell):
return False
elif self.id != other.id:
return False
elif self.name != other.name:
return False
# FIXME: This won't work for materials fills since OpenMC only outputs
# nuclide densities in units of atom/b-cm irregardless of input units
# elif self.fill != other.fill:
# return False
elif self.region != other.region:
return False
elif self.rotation != other.rotation:
return False
elif self.translation != other.translation:
return False
else:
return True
def __ne__(self, other):
return not self == other
def __repr__(self):
string = 'Cell\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)
@ -443,6 +466,31 @@ class Universe(object):
self._cell_offsets = OrderedDict()
self._num_regions = 0
def __eq__(self, other):
if not isinstance(other, Universe):
return False
elif self.id != other.id:
return False
elif self.name != other.name:
return False
elif self.cells != other.cells:
return False
else:
return True
def __ne__(self, other):
return not self == other
def __repr__(self):
string = 'Universe\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)
string += '{0: <16}{1}{2}\n'.format('\tName', '=\t', self._name)
string += '{0: <16}{1}{2}\n'.format('\tCells', '=\t',
list(self._cells.keys()))
string += '{0: <16}{1}{2}\n'.format('\t# Regions', '=\t',
self._num_regions)
return string
@property
def id(self):
return self._id
@ -630,16 +678,6 @@ class Universe(object):
return universes
def __repr__(self):
string = 'Universe\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)
string += '{0: <16}{1}{2}\n'.format('\tName', '=\t', self._name)
string += '{0: <16}{1}{2}\n'.format('\tCells', '=\t',
list(self._cells.keys()))
string += '{0: <16}{1}{2}\n'.format('\t# Regions', '=\t',
self._num_regions)
return string
def create_xml_subelement(self, xml_element):
# Iterate over all Cells
@ -695,6 +733,25 @@ class Lattice(object):
self._outer = None
self._universes = None
def __eq__(self, other):
if not isinstance(other, Lattice):
return False
elif self.id != other.id:
return False
elif self.name != other.name:
return False
elif self.pitch != other.pitch:
return False
elif self.outer != other.outer:
return False
elif self.universes != other.universes:
return False
else:
return True
def __ne__(self, other):
return not self == other
@property
def id(self):
return self._id
@ -894,6 +951,21 @@ class RectLattice(Lattice):
self._lower_left = None
self._offsets = None
def __eq__(self, other):
if not isinstance(other, RectLattice):
return False
elif not super(RectLattice, self).__eq__(other):
return False
elif self.dimension != other.dimension:
return False
elif self.lower_left != other.lower_left:
return False
else:
return True
def __ne__(self, other):
return not self == other
def __repr__(self):
string = 'RectLattice\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)
@ -1111,6 +1183,23 @@ class HexLattice(Lattice):
self._num_axial = None
self._center = None
def __eq__(self, other):
if not isinstance(other, HexLattice):
return False
elif not super(HexLattice, self).__eq__(other):
return False
elif self.num_rings != other.num_rings:
return False
elif self.num_axial != other.num_axial:
return False
elif self.center != other.center:
return False
else:
return True
def __ne__(self, other):
return not self == other
def __repr__(self):
string = 'HexLattice\n'
string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)