From e8ebc74b4f4c33da3edd381d45127df610863da8 Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Thu, 11 Aug 2016 19:11:10 -0400 Subject: [PATCH 1/8] added __eq__ to the nuclear data classes and also enabled openmc.data.ThermalScattering.from_hdf5 to use either a group or filename like Neutron can. --- openmc/data/angle_distribution.py | 10 ++++ openmc/data/correlated.py | 13 ++++ openmc/data/energy_distribution.py | 96 ++++++++++++++++++++++++++++++ openmc/data/function.py | 12 ++++ openmc/data/kalbach_mann.py | 14 +++++ openmc/data/nbody.py | 12 ++++ openmc/data/product.py | 14 +++++ openmc/data/thermal.py | 41 ++++++++++++- openmc/data/uncorrelated.py | 10 ++++ openmc/stats/univariate.py | 70 ++++++++++++++++++++++ 10 files changed, 289 insertions(+), 3 deletions(-) diff --git a/openmc/data/angle_distribution.py b/openmc/data/angle_distribution.py index 316559c50..afb9a0545 100644 --- a/openmc/data/angle_distribution.py +++ b/openmc/data/angle_distribution.py @@ -32,6 +32,16 @@ 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 diff --git a/openmc/data/correlated.py b/openmc/data/correlated.py index 97a96c17c..2415aaab3 100644 --- a/openmc/data/correlated.py +++ b/openmc/data/correlated.py @@ -49,6 +49,19 @@ 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 diff --git a/openmc/data/energy_distribution.py b/openmc/data/energy_distribution.py index 2300081c1..4299fc166 100644 --- a/openmc/data/energy_distribution.py +++ b/openmc/data/energy_distribution.py @@ -84,6 +84,16 @@ 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 @@ -122,6 +132,17 @@ 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 @@ -159,6 +180,16 @@ 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 @@ -267,6 +298,16 @@ 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 @@ -379,6 +420,17 @@ 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 @@ -520,6 +572,17 @@ 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 @@ -618,6 +681,17 @@ 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 @@ -726,6 +800,16 @@ 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 @@ -832,6 +916,18 @@ 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 diff --git a/openmc/data/function.py b/openmc/data/function.py index 56a3d4a45..e0acf9f29 100644 --- a/openmc/data/function.py +++ b/openmc/data/function.py @@ -65,6 +65,18 @@ class Tabulated1D(object): 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): diff --git a/openmc/data/kalbach_mann.py b/openmc/data/kalbach_mann.py index 5aca17ba9..519796ad4 100644 --- a/openmc/data/kalbach_mann.py +++ b/openmc/data/kalbach_mann.py @@ -59,6 +59,20 @@ 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 diff --git a/openmc/data/nbody.py b/openmc/data/nbody.py index a34380dc9..274db48f9 100644 --- a/openmc/data/nbody.py +++ b/openmc/data/nbody.py @@ -38,6 +38,18 @@ 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 diff --git a/openmc/data/product.py b/openmc/data/product.py index dd276daac..08529f90a 100644 --- a/openmc/data/product.py +++ b/openmc/data/product.py @@ -49,6 +49,20 @@ class Product(object): self.applicability = [] self.yield_ = 1 + 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 + self.distribution.all() != other.distribution.all() or + self.applicability.all() != other.applicability.all() or + self.yield_ != other.yield_): + eqval = False + return eqval + def __repr__(self): if isinstance(self.yield_, Real): return "".format( diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 9de39f518..cb61aa730 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -69,6 +69,16 @@ 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) @@ -170,6 +180,23 @@ 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 "".format(self.name) @@ -216,13 +243,15 @@ class ThermalScattering(object): self.inelastic_dist.to_hdf5(inelastic_group) @classmethod - def from_hdf5(cls, group): + def from_hdf5(cls, group_or_filename): """Generate thermal scattering data from HDF5 group Parameters ---------- - group : h5py.Group - HDF5 group to read from + group_or_filename : h5py.Group or str + HDF5 group containing interaction data. If given as a string, it is + assumed to be the filename for the HDF5 file, and the first group + is used to read from. Returns ------- @@ -230,6 +259,12 @@ class ThermalScattering(object): Neutron thermal scattering data """ + if isinstance(group_or_filename, h5py.Group): + group = group_or_filename + else: + h5file = h5py.File(group_or_filename, 'r') + group = list(h5file.values())[0] + name = group.name[1:] atomic_weight_ratio = group.attrs['atomic_weight_ratio'] temperature = group.attrs['temperature'] diff --git a/openmc/data/uncorrelated.py b/openmc/data/uncorrelated.py index 0361c9b37..babe003c0 100644 --- a/openmc/data/uncorrelated.py +++ b/openmc/data/uncorrelated.py @@ -34,6 +34,16 @@ class UncorrelatedAngleEnergy(AngleEnergy): if energy is not None: self.energy = energy + def __eq__(self, other): + if not isinstance(other, type(self)): + return NotImplemented + else: + eqval = True + if (self.angle != other.angle or + self.energy != other.energy): + eqval = False + return eqval + @property def angle(self): return self._angle diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index 24ab98895..d7ba94520 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -65,6 +65,16 @@ 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) @@ -126,6 +136,16 @@ 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 @@ -183,6 +203,15 @@ 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 @@ -231,6 +260,16 @@ 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 @@ -300,6 +339,18 @@ 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) @@ -368,6 +419,15 @@ 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) @@ -414,6 +474,16 @@ 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) From c91744d2d54adac36beb57074bb8e7bb2463360d Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Sun, 14 Aug 2016 12:09:59 -0400 Subject: [PATCH 2/8] testing run to use the dict comparison approach --- openmc/data/product.py | 4 ++-- openmc/data/uncorrelated.py | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/openmc/data/product.py b/openmc/data/product.py index 08529f90a..a33ee9dfb 100644 --- a/openmc/data/product.py +++ b/openmc/data/product.py @@ -57,8 +57,8 @@ class Product(object): if (self.particle != other.particle or self.decay_rate != other.decay_rate or self.emission_mode != other.emission_mode or - self.distribution.all() != other.distribution.all() or - self.applicability.all() != other.applicability.all() 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 diff --git a/openmc/data/uncorrelated.py b/openmc/data/uncorrelated.py index babe003c0..19945205e 100644 --- a/openmc/data/uncorrelated.py +++ b/openmc/data/uncorrelated.py @@ -38,11 +38,10 @@ class UncorrelatedAngleEnergy(AngleEnergy): if not isinstance(other, type(self)): return NotImplemented else: - eqval = True - if (self.angle != other.angle or - self.energy != other.energy): - eqval = False - return eqval + if self.__dict__ != other.__dict__: + return False + else: + return True @property def angle(self): From f886d2849e664c9e742dc84fa4656e2d46e1d72e Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Wed, 17 Aug 2016 05:07:29 -0400 Subject: [PATCH 3/8] Added in a mixin module with an Equality class for mixin use --- openmc/mixin.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 openmc/mixin.py diff --git a/openmc/mixin.py b/openmc/mixin.py new file mode 100644 index 000000000..f4d5f4c8d --- /dev/null +++ b/openmc/mixin.py @@ -0,0 +1,24 @@ +import numpy as np + + +class Equality(object): + """A Class which provides generic __eq__ and __ne__ functionality which + can easily be inherited by downstream classes. + """ + + def __eq__(self, other): + eqval = True + if isinstance(other, type(self)): + for key, value in self.__dict__.items(): + if key in other.__dict__: + if not np.array_equal(value, other.__dict__.get(key)): + eqval = False + else: + eqval = False + else: + eqval = False + + return eqval + + def __ne__(self, other): + return not self.__eq__(other) From f2fc06d960fc78b2e4a967265143d810631546ea Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Wed, 17 Aug 2016 05:15:57 -0400 Subject: [PATCH 4/8] Replaced prior __eq__ classes with the equality mixin --- openmc/data/ace.py | 6 +- openmc/data/angle_distribution.py | 13 +--- openmc/data/angle_energy.py | 3 +- openmc/data/correlated.py | 14 +---- openmc/data/energy_distribution.py | 99 +----------------------------- openmc/data/fission_energy.py | 5 +- openmc/data/function.py | 17 +---- openmc/data/kalbach_mann.py | 15 +---- openmc/data/library.py | 2 +- openmc/data/nbody.py | 12 ---- openmc/data/neutron.py | 3 +- openmc/data/product.py | 17 +---- openmc/data/reaction.py | 3 +- openmc/data/thermal.py | 31 +--------- openmc/data/uncorrelated.py | 10 +-- openmc/data/urr.py | 3 +- openmc/stats/univariate.py | 73 +--------------------- 17 files changed, 33 insertions(+), 293 deletions(-) diff --git a/openmc/data/ace.py b/openmc/data/ace.py index 4085b4222..867e3767a 100644 --- a/openmc/data/ace.py +++ b/openmc/data/ace.py @@ -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 diff --git a/openmc/data/angle_distribution.py b/openmc/data/angle_distribution.py index afb9a0545..81cc06830 100644 --- a/openmc/data/angle_distribution.py +++ b/openmc/data/angle_distribution.py @@ -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 diff --git a/openmc/data/angle_energy.py b/openmc/data/angle_energy.py index 00e049ebc..21748ab8a 100644 --- a/openmc/data/angle_energy.py +++ b/openmc/data/angle_energy.py @@ -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 diff --git a/openmc/data/correlated.py b/openmc/data/correlated.py index 2415aaab3..d6ce8ab47 100644 --- a/openmc/data/correlated.py +++ b/openmc/data/correlated.py @@ -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 diff --git a/openmc/data/energy_distribution.py b/openmc/data/energy_distribution.py index 4299fc166..ba34abb4f 100644 --- a/openmc/data/energy_distribution.py +++ b/openmc/data/energy_distribution.py @@ -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 diff --git a/openmc/data/fission_energy.py b/openmc/data/fission_energy.py index 03d3ed84e..d1023a01a 100644 --- a/openmc/data/fission_energy.py +++ b/openmc/data/fission_energy.py @@ -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. diff --git a/openmc/data/function.py b/openmc/data/function.py index 98a7f2c5e..7a4a938d1 100644 --- a/openmc/data/function.py +++ b/openmc/data/function.py @@ -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 diff --git a/openmc/data/kalbach_mann.py b/openmc/data/kalbach_mann.py index 519796ad4..c44c46b65 100644 --- a/openmc/data/kalbach_mann.py +++ b/openmc/data/kalbach_mann.py @@ -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 diff --git a/openmc/data/library.py b/openmc/data/library.py index dba5eabc1..3fed7598d 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -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 = [] diff --git a/openmc/data/nbody.py b/openmc/data/nbody.py index 274db48f9..a34380dc9 100644 --- a/openmc/data/nbody.py +++ b/openmc/data/nbody.py @@ -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 diff --git a/openmc/data/neutron.py b/openmc/data/neutron.py index 95f840d64..66f4b2797 100644 --- a/openmc/data/neutron.py +++ b/openmc/data/neutron.py @@ -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 diff --git a/openmc/data/product.py b/openmc/data/product.py index 06875556f..1f07076d9 100644 --- a/openmc/data/product.py +++ b/openmc/data/product.py @@ -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 "".format( diff --git a/openmc/data/reaction.py b/openmc/data/reaction.py index d35599579..0255a3097 100644 --- a/openmc/data/reaction.py +++ b/openmc/data/reaction.py @@ -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 diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 2232274f4..d729e41ec 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -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 "".format(self.name) diff --git a/openmc/data/uncorrelated.py b/openmc/data/uncorrelated.py index 19945205e..399db3e7c 100644 --- a/openmc/data/uncorrelated.py +++ b/openmc/data/uncorrelated.py @@ -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 diff --git a/openmc/data/urr.py b/openmc/data/urr.py index 052da6612..2e6ea355f 100644 --- a/openmc/data/urr.py +++ b/openmc/data/urr.py @@ -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 diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index c29386046..2f5b30464 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -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) From 89778d422e966692aaef5fe9c6c9e4322c094f05 Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Wed, 17 Aug 2016 18:55:08 -0400 Subject: [PATCH 5/8] Missed an import of mixin --- openmc/__init__.py | 1 + openmc/data/kalbach_mann.py | 1 - openmc/data/library.py | 2 ++ openmc/data/uncorrelated.py | 1 - 4 files changed, 3 insertions(+), 2 deletions(-) diff --git a/openmc/__init__.py b/openmc/__init__.py index a0492ee40..2aeacd689 100644 --- a/openmc/__init__.py +++ b/openmc/__init__.py @@ -22,6 +22,7 @@ from openmc.executor import * from openmc.statepoint import * from openmc.summary import * from openmc.particle_restart import * +from openmc.mixin import * try: from openmc.opencg_compatible import * diff --git a/openmc/data/kalbach_mann.py b/openmc/data/kalbach_mann.py index c44c46b65..5aca17ba9 100644 --- a/openmc/data/kalbach_mann.py +++ b/openmc/data/kalbach_mann.py @@ -5,7 +5,6 @@ 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 diff --git a/openmc/data/library.py b/openmc/data/library.py index 3fed7598d..6c2600f32 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -3,8 +3,10 @@ import xml.etree.ElementTree as ET import h5py +from openmc.mixin import Equality from openmc.clean_xml import clean_xml_indentation + class DataLibrary(Equality): def __init__(self): self.libraries = [] diff --git a/openmc/data/uncorrelated.py b/openmc/data/uncorrelated.py index 399db3e7c..0361c9b37 100644 --- a/openmc/data/uncorrelated.py +++ b/openmc/data/uncorrelated.py @@ -1,7 +1,6 @@ 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 From 64032169117fc57a48ae706855aa38a43ecdb4db Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Fri, 19 Aug 2016 06:58:17 -0400 Subject: [PATCH 6/8] Renamed Equality to EqualityMixin --- openmc/data/ace.py | 6 +++--- openmc/data/angle_distribution.py | 4 ++-- openmc/data/angle_energy.py | 4 ++-- openmc/data/energy_distribution.py | 4 ++-- openmc/data/fission_energy.py | 4 ++-- openmc/data/function.py | 6 +++--- openmc/data/library.py | 4 ++-- openmc/data/neutron.py | 4 ++-- openmc/data/product.py | 4 ++-- openmc/data/reaction.py | 4 ++-- openmc/data/thermal.py | 6 +++--- openmc/data/urr.py | 4 ++-- openmc/mixin.py | 2 +- 13 files changed, 28 insertions(+), 28 deletions(-) diff --git a/openmc/data/ace.py b/openmc/data/ace.py index 867e3767a..831e40fc8 100644 --- a/openmc/data/ace.py +++ b/openmc/data/ace.py @@ -22,7 +22,7 @@ import sys import numpy as np -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin if sys.version_info[0] >= 3: @@ -133,7 +133,7 @@ def get_table(filename, name=None): .format(name)) -class Library(Equality): +class Library(EqualityMixin): """A Library objects represents an ACE-formatted file which may contain multiple tables with data. @@ -355,7 +355,7 @@ class Library(Equality): lines = [ace_file.readline() for i in range(13)] -class Table(Equality): +class Table(EqualityMixin): """ACE cross section table Parameters diff --git a/openmc/data/angle_distribution.py b/openmc/data/angle_distribution.py index 81cc06830..80b6cdbdb 100644 --- a/openmc/data/angle_distribution.py +++ b/openmc/data/angle_distribution.py @@ -4,12 +4,12 @@ from numbers import Real import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin from openmc.stats import Univariate, Tabular, Uniform from .function import INTERPOLATION_SCHEME -class AngleDistribution(Equality): +class AngleDistribution(EqualityMixin): """Angle distribution as a function of incoming energy Parameters diff --git a/openmc/data/angle_energy.py b/openmc/data/angle_energy.py index 21748ab8a..4a5e391e3 100644 --- a/openmc/data/angle_energy.py +++ b/openmc/data/angle_energy.py @@ -1,10 +1,10 @@ from abc import ABCMeta, abstractmethod import openmc.data -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin -class AngleEnergy(Equality): +class AngleEnergy(EqualityMixin): """Distribution in angle and energy of a secondary particle.""" __metaclass = ABCMeta diff --git a/openmc/data/energy_distribution.py b/openmc/data/energy_distribution.py index ba34abb4f..a16090635 100644 --- a/openmc/data/energy_distribution.py +++ b/openmc/data/energy_distribution.py @@ -8,10 +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 +from openmc.mixin import EqualityMixin -class EnergyDistribution(Equality): +class EnergyDistribution(EqualityMixin): """Abstract superclass for all energy distributions.""" __metaclass__ = ABCMeta diff --git a/openmc/data/fission_energy.py b/openmc/data/fission_energy.py index d1023a01a..6352915cc 100644 --- a/openmc/data/fission_energy.py +++ b/openmc/data/fission_energy.py @@ -9,7 +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 +from openmc.mixin import EqualityMixin if sys.version_info[0] >= 3: basestring = str @@ -190,7 +190,7 @@ def write_compact_458_library(endf_files, output_name='fission_Q_data.h5', out.close() -class FissionEnergyRelease(Equality): +class FissionEnergyRelease(EqualityMixin): """Energy relased by fission reactions. Energy is carried away from fission reactions by many different particles. diff --git a/openmc/data/function.py b/openmc/data/function.py index 7a4a938d1..94a2f0911 100644 --- a/openmc/data/function.py +++ b/openmc/data/function.py @@ -5,13 +5,13 @@ from numbers import Real, Integral import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin INTERPOLATION_SCHEME = {1: 'histogram', 2: 'linear-linear', 3: 'linear-log', 4: 'log-linear', 5: 'log-log'} -class Function1D(Equality): +class Function1D(EqualityMixin): """A function of one independent variable with HDF5 support.""" __metaclass__ = ABCMeta @@ -390,7 +390,7 @@ class Polynomial(np.polynomial.Polynomial, Function1D): return cls(dataset.value) -class Sum(Equality): +class Sum(EqualityMixin): """Sum of multiple functions. This class allows you to create a callable object which represents the sum diff --git a/openmc/data/library.py b/openmc/data/library.py index 6c2600f32..49d1c78f6 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -3,11 +3,11 @@ import xml.etree.ElementTree as ET import h5py -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin from openmc.clean_xml import clean_xml_indentation -class DataLibrary(Equality): +class DataLibrary(EqualityMixin): def __init__(self): self.libraries = [] diff --git a/openmc/data/neutron.py b/openmc/data/neutron.py index 66f4b2797..c6621e1b8 100644 --- a/openmc/data/neutron.py +++ b/openmc/data/neutron.py @@ -15,13 +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 +from openmc.mixin import EqualityMixin if sys.version_info[0] >= 3: basestring = str -class IncidentNeutron(Equality): +class IncidentNeutron(EqualityMixin): """Continuous-energy neutron interaction data. Instances of this class are not normally instantiated by the user but rather diff --git a/openmc/data/product.py b/openmc/data/product.py index 1f07076d9..753888f6d 100644 --- a/openmc/data/product.py +++ b/openmc/data/product.py @@ -5,7 +5,7 @@ import sys import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin from .function import Tabulated1D, Polynomial, Function1D from .angle_energy import AngleEnergy @@ -13,7 +13,7 @@ if sys.version_info[0] >= 3: basestring = str -class Product(Equality): +class Product(EqualityMixin): """Secondary particle emitted in a nuclear reaction Parameters diff --git a/openmc/data/reaction.py b/openmc/data/reaction.py index 0255a3097..e7580ec47 100644 --- a/openmc/data/reaction.py +++ b/openmc/data/reaction.py @@ -7,7 +7,7 @@ from warnings import warn import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin from openmc.stats import Uniform from .angle_distribution import AngleDistribution from .angle_energy import AngleEnergy @@ -251,7 +251,7 @@ def _get_photon_products(ace, rx): return photons -class Reaction(Equality): +class Reaction(EqualityMixin): """A nuclear reaction A Reaction object represents a single reaction channel for a nuclide with diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 34981e566..1551e8ac5 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -7,7 +7,7 @@ import numpy as np import h5py import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin from .ace import Table, get_table from .angle_energy import AngleEnergy from .function import Tabulated1D @@ -62,7 +62,7 @@ def get_thermal_name(name): return 'c_' + name -class CoherentElastic(Equality): +class CoherentElastic(EqualityMixin): r"""Coherent elastic scattering data from a crystalline material Parameters @@ -149,7 +149,7 @@ class CoherentElastic(Equality): return cls(bragg_edges, factors) -class ThermalScattering(Equality): +class ThermalScattering(EqualityMixin): """A ThermalScattering object contains thermal scattering data as represented by an S(alpha, beta) table. diff --git a/openmc/data/urr.py b/openmc/data/urr.py index 2e6ea355f..05f64e578 100644 --- a/openmc/data/urr.py +++ b/openmc/data/urr.py @@ -4,10 +4,10 @@ from numbers import Integral, Real import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin -class ProbabilityTables(Equality): +class ProbabilityTables(EqualityMixin): r"""Unresolved resonance region probability tables. Parameters diff --git a/openmc/mixin.py b/openmc/mixin.py index f4d5f4c8d..3495f0f6f 100644 --- a/openmc/mixin.py +++ b/openmc/mixin.py @@ -1,7 +1,7 @@ import numpy as np -class Equality(object): +class EqualityMixin(object): """A Class which provides generic __eq__ and __ne__ functionality which can easily be inherited by downstream classes. """ From e79651c60045379345b1fc8942b0c6b8c66854e8 Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Fri, 19 Aug 2016 06:59:25 -0400 Subject: [PATCH 7/8] Forgot one in stats, all good --- openmc/stats/univariate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index 2f5b30464..ce0f0fae1 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -7,7 +7,7 @@ from xml.etree import ElementTree as ET import numpy as np import openmc.checkvalue as cv -from openmc.mixin import Equality +from openmc.mixin import EqualityMixin if sys.version_info[0] >= 3: basestring = str @@ -16,7 +16,7 @@ _INTERPOLATION_SCHEMES = ['histogram', 'linear-linear', 'linear-log', 'log-linear', 'log-log'] -class Univariate(Equality): +class Univariate(EqualityMixin): """Probability distribution of a single random variable. The Univariate class is an abstract class that can be derived to implement a From d24aaa8626f38bf752d3e775c7956e44d0742f16 Mon Sep 17 00:00:00 2001 From: Adam Nelson Date: Fri, 19 Aug 2016 19:24:51 -0400 Subject: [PATCH 8/8] Final changes --- openmc/mixin.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/openmc/mixin.py b/openmc/mixin.py index 3495f0f6f..dd97e8924 100644 --- a/openmc/mixin.py +++ b/openmc/mixin.py @@ -7,18 +7,14 @@ class EqualityMixin(object): """ def __eq__(self, other): - eqval = True if isinstance(other, type(self)): for key, value in self.__dict__.items(): - if key in other.__dict__: - if not np.array_equal(value, other.__dict__.get(key)): - eqval = False - else: - eqval = False + if not np.array_equal(value, other.__dict__.get(key)): + return False else: - eqval = False + return False - return eqval + return True def __ne__(self, other): return not self.__eq__(other)