From 400b5e2c1df2a00f6d5da39353466bb16316dae0 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Mon, 10 Apr 2017 17:17:17 -0400 Subject: [PATCH] Added __hash__ method to Surface class; revised clone methods memo param to use objects as keys --- openmc/cell.py | 12 ++++++------ openmc/lattice.py | 14 +++++++------- openmc/material.py | 12 ++++++------ openmc/region.py | 16 ++++++++-------- openmc/surface.py | 19 +++++++++++-------- openmc/universe.py | 12 ++++++------ 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/openmc/cell.py b/openmc/cell.py index b0847fc047..4496062f7f 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -1,4 +1,4 @@ -from collections import OrderedDict, Iterable, defaultdict +from collections import OrderedDict, Iterable from copy import deepcopy from math import cos, sin, pi from numbers import Real, Integral @@ -511,7 +511,7 @@ class Cell(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -523,10 +523,10 @@ class Cell(object): """ if memo is None: - memo = defaultdict(dict) + memo = {} # If no nemoize'd clone exists, instantiate one - if self.id not in memo['cells']: + if self not in memo: clone = deepcopy(self) clone.id = None if self.region is not None: @@ -539,9 +539,9 @@ class Cell(object): clone.fill = self.fill.clone(memo) # Memoize the clone - memo['cells'][self.id] = clone + memo[self] = clone - return memo['cells'][self.id] + return memo[self] def create_xml_subelement(self, xml_element): element = ET.Element("cell") diff --git a/openmc/lattice.py b/openmc/lattice.py index f9fa63331a..0fbd74f28e 100644 --- a/openmc/lattice.py +++ b/openmc/lattice.py @@ -1,7 +1,7 @@ from __future__ import division from abc import ABCMeta -from collections import OrderedDict, Iterable, defaultdict +from collections import OrderedDict, Iterable from copy import deepcopy from math import sqrt, floor from numbers import Real, Integral @@ -421,7 +421,7 @@ class Lattice(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -433,15 +433,15 @@ class Lattice(object): """ if memo is None: - memo = defaultdict(dict) + memo = {} # If no nemoize'd clone exists, instantiate one - if self.id not in memo['lattices']: + if self not in memo: clone = deepcopy(self) clone.id = None if self.outer is not None: - clone.outer = self.outer.clone() + clone.outer = self.outer.clone(memo) # Clone all unique universes in the lattice univ_clones = self.get_unique_universes() @@ -462,9 +462,9 @@ class Lattice(object): clone.universes[i[0]][i[1]][i[2]] = univ_clones[univ_id] # Memoize the clone - memo['lattices'][self.id] = clone + memo[self] = clone - return memo['lattices'][self.id] + return memo[self] class RectLattice(Lattice): diff --git a/openmc/material.py b/openmc/material.py index cdb82da03d..762337659a 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -1,4 +1,4 @@ -from collections import OrderedDict, defaultdict +from collections import OrderedDict from copy import deepcopy from numbers import Real, Integral import warnings @@ -804,7 +804,7 @@ class Material(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -816,17 +816,17 @@ class Material(object): """ if memo is None: - memo = defaultdict(dict) + memo = dict # If no nemoize'd clone exists, instantiate one - if self.id not in memo['materials']: + if self not in memo: clone = deepcopy(self) clone.id = None # Memoize the clone - memo['materials'][self.id] = clone + memo[self] = clone - return memo['materials'][self.id] + return memo[self] def _get_nuclide_xml(self, nuclide, distrib=False): xml_element = ET.Element("nuclide") diff --git a/openmc/region.py b/openmc/region.py index 0b1f970840..854852ba41 100644 --- a/openmc/region.py +++ b/openmc/region.py @@ -1,5 +1,5 @@ from abc import ABCMeta, abstractmethod -from collections import Iterable, OrderedDict, defaultdict +from collections import Iterable, OrderedDict from copy import deepcopy from six import add_metaclass @@ -229,7 +229,7 @@ class Region(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -332,7 +332,7 @@ class Intersection(Region): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -344,7 +344,7 @@ class Intersection(Region): """ if memo is None: - memo = defaultdict(dict) + memo = {} clone = deepcopy(self) clone.nodes = [n.clone(memo) for n in self.nodes] @@ -433,7 +433,7 @@ class Union(Region): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -445,7 +445,7 @@ class Union(Region): """ if memo is None: - memo = defaultdict(dict) + memo = {} clone = copy.deepcopy(self) clone.nodes = [n.clone(memo) for n in self.nodes] @@ -554,7 +554,7 @@ class Complement(Region): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -566,7 +566,7 @@ class Complement(Region): """ if memo is None: - memo = defaultdict(dict) + memo = {} clone = copy.deepcopy(self) clone.node = self.node.clone(memo) diff --git a/openmc/surface.py b/openmc/surface.py index 1edae2cae7..5db15138d4 100644 --- a/openmc/surface.py +++ b/openmc/surface.py @@ -1,5 +1,5 @@ from abc import ABCMeta -from collections import Iterable, OrderedDict, defaultdict +from collections import Iterable, OrderedDict from copy import deepcopy from numbers import Real, Integral from xml.etree import ElementTree as ET @@ -83,6 +83,9 @@ class Surface(object): def __pos__(self): return Halfspace(self, '+') + def __hash__(self): + return hash(repr(self)) + def __repr__(self): string = 'Surface\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) @@ -177,7 +180,7 @@ class Surface(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -189,17 +192,17 @@ class Surface(object): """ if memo is None: - memo = defaultdict(dict) + memo = {} # If no nemoize'd clone exists, instantiate one - if self.id not in memo['surfaces']: + if self not in memo: clone = deepcopy(self) clone.id = None # Memoize the clone - memo['surfaces'][self.id] = clone + memo[self] = clone - return memo['surfaces'][self.id] + return memo[self] def to_xml_element(self): """Return XML representation of the surface @@ -1938,7 +1941,7 @@ class Halfspace(Region): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -1950,7 +1953,7 @@ class Halfspace(Region): """ if memo is None: - memo = defaultdict(dict) + memo = dict clone = deepcopy(self) clone.surface = self.surface.clone(memo) diff --git a/openmc/universe.py b/openmc/universe.py index 1fefa2372c..c955934f2a 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -1,5 +1,5 @@ from __future__ import division -from collections import OrderedDict, Iterable, defaultdict +from collections import OrderedDict, Iterable from copy import copy, deepcopy from numbers import Integral, Real import random @@ -523,7 +523,7 @@ class Universe(object): Parameters ---------- - memo : defaultdict(dict) or None + memo : dict or None A nested dictionary of previously cloned objects. This parameter is used internally and should not be specified by the user. @@ -535,10 +535,10 @@ class Universe(object): """ if memo is None: - memo = defaultdict(dict) + memo = {} # If no nemoize'd clone exists, instantiate one - if self.id not in memo['universes']: + if self not in memo: clone = deepcopy(self) clone.id = None @@ -548,9 +548,9 @@ class Universe(object): clone.add_cell(cell.clone(memo)) # Memoize the clone - memo['universes'][self.id] = clone + memo[self] = clone - return memo['universes'][self.id] + return memo[self] def create_xml_subelement(self, xml_element): # Iterate over all Cells