Replaced prior __eq__ classes with the equality mixin

This commit is contained in:
Adam Nelson 2016-08-17 05:15:57 -04:00
parent f886d2849e
commit f2fc06d960
17 changed files with 33 additions and 293 deletions

View file

@ -22,6 +22,8 @@ import sys
import numpy as np
from openmc.mixin import Equality
if sys.version_info[0] >= 3:
basestring = str
@ -131,7 +133,7 @@ def get_table(filename, name=None):
.format(name))
class Library(object):
class Library(Equality):
"""A Library objects represents an ACE-formatted file which may contain
multiple tables with data.
@ -353,7 +355,7 @@ class Library(object):
lines = [ace_file.readline() for i in range(13)]
class Table(object):
class Table(Equality):
"""ACE cross section table
Parameters

View file

@ -4,11 +4,12 @@ from numbers import Real
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
from openmc.stats import Univariate, Tabular, Uniform
from .function import INTERPOLATION_SCHEME
class AngleDistribution(object):
class AngleDistribution(Equality):
"""Angle distribution as a function of incoming energy
Parameters
@ -32,16 +33,6 @@ class AngleDistribution(object):
self.energy = energy
self.mu = mu
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.energy, other.energy) or
not np.array_equal(self.mu, other.mu)):
eqval = False
return eqval
@property
def energy(self):
return self._energy

View file

@ -1,9 +1,10 @@
from abc import ABCMeta, abstractmethod
import openmc.data
from openmc.mixin import Equality
class AngleEnergy(object):
class AngleEnergy(Equality):
"""Distribution in angle and energy of a secondary particle."""
__metaclass = ABCMeta

View file

@ -49,19 +49,6 @@ class CorrelatedAngleEnergy(AngleEnergy):
self.energy_out = energy_out
self.mu = mu
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.breakpoints, other.breakpoints) or
not np.array_equal(self.interpolation, other.interpolation) or
not np.array_equal(self.energy, other.energy) or
not np.array_equal(self.energy_out, other.energy_out) or
not np.array_equal(self.mu, other.mu)):
eqval = False
return eqval
@property
def breakpoints(self):
return self._breakpoints
@ -69,6 +56,7 @@ class CorrelatedAngleEnergy(AngleEnergy):
@property
def interpolation(self):
return self._interpolation
@property
def energy(self):
return self._energy

View file

@ -8,9 +8,10 @@ import numpy as np
from .function import Tabulated1D, INTERPOLATION_SCHEME
from openmc.stats.univariate import Univariate, Tabular, Discrete, Mixture
import openmc.checkvalue as cv
from openmc.mixin import Equality
class EnergyDistribution(object):
class EnergyDistribution(Equality):
"""Abstract superclass for all energy distributions."""
__metaclass__ = ABCMeta
@ -84,16 +85,6 @@ class ArbitraryTabulated(EnergyDistribution):
self.energy = energy
self.pdf = pdf
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.energy, other.energy) or
not np.array_equal(self.pdf, other.pdf)):
eqval = False
return eqval
def to_hdf5(self, group):
raise NotImplementedError
@ -132,17 +123,6 @@ class GeneralEvaporation(EnergyDistribution):
self.g = g
self.u = u
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.theta != other.theta or
self.g != other.g or
self.u != other.u):
eqval = False
return eqval
def to_hdf5(self, group):
raise NotImplementedError
@ -180,16 +160,6 @@ class MaxwellEnergy(EnergyDistribution):
self.theta = theta
self.u = u
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.theta != other.theta or
self.u != other.u):
eqval = False
return eqval
@property
def theta(self):
return self._theta
@ -298,16 +268,6 @@ class Evaporation(EnergyDistribution):
self.theta = theta
self.u = u
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.theta != other.theta or
self.u != other.u):
eqval = False
return eqval
@property
def theta(self):
return self._theta
@ -420,17 +380,6 @@ class WattEnergy(EnergyDistribution):
self.b = b
self.u = u
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.a != other.a or
self.b != other.b or
self.u != other.u):
eqval = False
return eqval
@property
def a(self):
return self._a
@ -572,17 +521,6 @@ class MadlandNix(EnergyDistribution):
self.efh = efh
self.tm = tm
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.efl != other.efl or
self.efh != other.efh or
self.tm != other.tm):
eqval = False
return eqval
@property
def efl(self):
return self._efl
@ -681,17 +619,6 @@ class DiscretePhoton(EnergyDistribution):
self.energy = energy
self.atomic_weight_ratio = atomic_weight_ratio
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.primary_flag != other.primary_flag or
self.energy != other.energy or
self.atomic_weight_ratio != other.atomic_weight_ratio):
eqval = False
return eqval
@property
def primary_flag(self):
return self._primary_flag
@ -800,16 +727,6 @@ class LevelInelastic(EnergyDistribution):
self.threshold = threshold
self.mass_ratio = mass_ratio
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.threshold != other.threshold or
self.mass_ratio != other.mass_ratio):
eqval = False
return eqval
@property
def threshold(self):
return self._threshold
@ -916,18 +833,6 @@ class ContinuousTabular(EnergyDistribution):
self.energy = energy
self.energy_out = energy_out
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.breakpoints, other.breakpoints) or
not np.array_equal(self.interpolation, other.interpolation) or
not np.array_equal(self.energy, other.energy) or
not np.array_equal(self.energy_out, other.energy_out)):
eqval = False
return eqval
@property
def breakpoints(self):
return self._breakpoints

View file

@ -9,6 +9,7 @@ from .data import ATOMIC_SYMBOL
from .endf_utils import read_float, read_CONT_line, identify_nuclide
from .function import Function1D, Tabulated1D, Polynomial, Sum
import openmc.checkvalue as cv
from openmc.mixin import Equality
if sys.version_info[0] >= 3:
basestring = str
@ -189,7 +190,7 @@ def write_compact_458_library(endf_files, output_name='fission_Q_data.h5',
out.close()
class FissionEnergyRelease(object):
class FissionEnergyRelease(Equality):
"""Energy relased by fission reactions.
Energy is carried away from fission reactions by many different particles.
@ -560,7 +561,7 @@ class FissionEnergyRelease(object):
self.delayed_photons.to_hdf5(group, 'delayed_photons')
self.betas.to_hdf5(group, 'betas')
self.neutrinos.to_hdf5(group, 'neutrinos')
if isinstance(self.prompt_neutrons, Polynomial):
# Add the polynomials for the relevant components together. Use a
# Polynomial((0.0, -1.0)) to subtract incident energy.

View file

@ -5,12 +5,13 @@ from numbers import Real, Integral
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
INTERPOLATION_SCHEME = {1: 'histogram', 2: 'linear-linear', 3: 'linear-log',
4: 'log-linear', 5: 'log-log'}
class Function1D(object):
class Function1D(Equality):
"""A function of one independent variable with HDF5 support."""
__metaclass__ = ABCMeta
@ -110,18 +111,6 @@ class Tabulated1D(Function1D):
self.x = np.asarray(x)
self.y = np.asarray(y)
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.x, other.x) or
not np.array_equal(self.y, other.y) or
not np.array_equal(self.breakpoints, other.breakpoints) or
not np.array_equal(self.interpolation, other.interpolation)):
eqval = False
return eqval
def __call__(self, x):
# Check if input is array or scalar
if isinstance(x, Iterable):
@ -401,7 +390,7 @@ class Polynomial(np.polynomial.Polynomial, Function1D):
return cls(dataset.value)
class Sum(object):
class Sum(Equality):
"""Sum of multiple functions.
This class allows you to create a callable object which represents the sum

View file

@ -5,6 +5,7 @@ from warnings import warn
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
from openmc.stats import Tabular, Univariate, Discrete, Mixture
from .function import Tabulated1D, INTERPOLATION_SCHEME
from .angle_energy import AngleEnergy
@ -59,20 +60,6 @@ class KalbachMann(AngleEnergy):
self.precompound = precompound
self.slope = slope
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.breakpoints, other.breakpoints) or
not np.array_equal(self.interpolation, other.interpolation) or
not np.array_equal(self.energy, other.energy) or
not np.array_equal(self.energy_out, other.energy_out) or
not np.array_equal(self.precompound, other.precompound) or
not np.array_equal(self.slope, other.slope)):
eqval = False
return eqval
@property
def breakpoints(self):
return self._breakpoints

View file

@ -5,7 +5,7 @@ import h5py
from openmc.clean_xml import clean_xml_indentation
class DataLibrary(object):
class DataLibrary(Equality):
def __init__(self):
self.libraries = []

View file

@ -38,18 +38,6 @@ class NBodyPhaseSpace(AngleEnergy):
self.atomic_weight_ratio = atomic_weight_ratio
self.q_value = q_value
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.total_mass != other.total_mass or
self.n_particles != other.n_particles or
self.atomic_weight_ratio != other.atomic_weight_ratio or
self.q_value != other.q_value):
eqval = False
return eqval
@property
def total_mass(self):
return self._total_mass

View file

@ -15,12 +15,13 @@ from .product import Product
from .reaction import Reaction, _get_photon_products
from .urr import ProbabilityTables
import openmc.checkvalue as cv
from openmc.mixin import Equality
if sys.version_info[0] >= 3:
basestring = str
class IncidentNeutron(object):
class IncidentNeutron(Equality):
"""Continuous-energy neutron interaction data.
Instances of this class are not normally instantiated by the user but rather

View file

@ -5,6 +5,7 @@ import sys
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
from .function import Tabulated1D, Polynomial, Function1D
from .angle_energy import AngleEnergy
@ -12,7 +13,7 @@ if sys.version_info[0] >= 3:
basestring = str
class Product(object):
class Product(Equality):
"""Secondary particle emitted in a nuclear reaction
Parameters
@ -48,20 +49,6 @@ class Product(object):
self.applicability = []
self.yield_ = Polynomial((1,)) # 0-order polynomial i.e. a constant
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.particle != other.particle or
self.decay_rate != other.decay_rate or
self.emission_mode != other.emission_mode or
not np.narray_equal(self.distribution, other.distribution) or
not np.narray_equal(self.applicability, other.applicability) or
self.yield_ != other.yield_):
eqval = False
return eqval
def __repr__(self):
if isinstance(self.yield_, Real):
return "<Product: {}, emission={}, yield={}>".format(

View file

@ -7,6 +7,7 @@ from warnings import warn
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
from openmc.stats import Uniform
from .angle_distribution import AngleDistribution
from .angle_energy import AngleEnergy
@ -250,7 +251,7 @@ def _get_photon_products(ace, rx):
return photons
class Reaction(object):
class Reaction(Equality):
"""A nuclear reaction
A Reaction object represents a single reaction channel for a nuclide with

View file

@ -7,6 +7,7 @@ import numpy as np
import h5py
import openmc.checkvalue as cv
from openmc.mixin import Equality
from .ace import Table, get_table
from .angle_energy import AngleEnergy
from .function import Tabulated1D
@ -60,7 +61,7 @@ def get_thermal_name(name):
return 'c_' + name
class CoherentElastic(object):
class CoherentElastic(Equality):
r"""Coherent elastic scattering data from a crystalline material
Parameters
@ -89,15 +90,6 @@ class CoherentElastic(object):
idx = np.searchsorted(self.bragg_edges, E)
return self.factors[idx]/E
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.bragg_edges, other.bragg_edges) or
not np.array_equal(self.factors, other.factors)):
eqval = False
return eqval
def __len__(self):
return len(self.bragg_edges)
@ -156,7 +148,7 @@ class CoherentElastic(object):
return cls(bragg_edges, factors)
class ThermalScattering(object):
class ThermalScattering(Equality):
"""A ThermalScattering object contains thermal scattering data as represented by
an S(alpha, beta) table.
@ -200,23 +192,6 @@ class ThermalScattering(object):
self.secondary_mode = None
self.zaids = []
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.name != other.name or
self.atomic_weight_ratio != other.atomic_weight_ratio or
self.elastic_xs != other.elastic_xs or
self.elastic_mu_out != other.elastic_mu_out or
self.inelastic_xs != other.inelastic_xs or
self.inelastic_mu_out != other.inelastic_mu_out or
self.temperature != other.temperature or
not np.array_equal(self.zaids, other.zaids) or
self.secondary_mode != other.secondary_mode):
eqval = False
return eqval
def __repr__(self):
if hasattr(self, 'name'):
return "<Thermal Scattering Data: {0}>".format(self.name)

View file

@ -1,6 +1,7 @@
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
from .angle_energy import AngleEnergy
from .energy_distribution import EnergyDistribution
from .angle_distribution import AngleDistribution
@ -34,15 +35,6 @@ class UncorrelatedAngleEnergy(AngleEnergy):
if energy is not None:
self.energy = energy
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
if self.__dict__ != other.__dict__:
return False
else:
return True
@property
def angle(self):
return self._angle

View file

@ -4,9 +4,10 @@ from numbers import Integral, Real
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
class ProbabilityTables(object):
class ProbabilityTables(Equality):
r"""Unresolved resonance region probability tables.
Parameters

View file

@ -7,6 +7,7 @@ from xml.etree import ElementTree as ET
import numpy as np
import openmc.checkvalue as cv
from openmc.mixin import Equality
if sys.version_info[0] >= 3:
basestring = str
@ -15,7 +16,7 @@ _INTERPOLATION_SCHEMES = ['histogram', 'linear-linear', 'linear-log',
'log-linear', 'log-log']
class Univariate(object):
class Univariate(Equality):
"""Probability distribution of a single random variable.
The Univariate class is an abstract class that can be derived to implement a
@ -65,16 +66,6 @@ class Discrete(Univariate):
self.x = x
self.p = p
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.x, other.x) or
not np.array_equal(self.p, other.p)):
eqval = False
return eqval
def __len__(self):
return len(self.x)
@ -149,16 +140,6 @@ class Uniform(Univariate):
self.a = a
self.b = b
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.a != other.a or
self.b != other.b):
eqval = False
return eqval
def __len__(self):
return 2
@ -229,15 +210,6 @@ class Maxwell(Univariate):
super(Maxwell, self).__init__()
self.theta = theta
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.theta != other.theta):
eqval = False
return eqval
def __len__(self):
return 1
@ -299,16 +271,6 @@ class Watt(Univariate):
self.a = a
self.b = b
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self.a != other.a or
self.b != other.b):
eqval = False
return eqval
def __len__(self):
return 2
@ -391,18 +353,6 @@ class Tabular(Univariate):
self.p = p
self.interpolation = interpolation
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (self._ignore_negative != other._ignore_negative or
not np.array_equal(self.x, other.x) or
not np.array_equal(self.p, other.p) or
self.interpolation != other.interpolation):
eqval = False
return eqval
def __len__(self):
return len(self.x)
@ -484,15 +434,6 @@ class Legendre(Univariate):
def __call__(self, x):
return self._legendre_polynomial(x)
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if not np.array_equal(self.coefficients, other.coefficients):
eqval = False
return eqval
def __len__(self):
return len(self._legendre_polynomial.coef)
@ -539,16 +480,6 @@ class Mixture(Univariate):
self.probability = probability
self.distribution = distribution
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
else:
eqval = True
if (not np.array_equal(self.probability, other.probability) or
not np.array_equal(self.distribution, other.distribution)):
eqval = False
return eqval
def __len__(self):
return sum(len(d) for d in self.distribution)