From 4cd527cb167bd4909404170180c16b3c18397d3a Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 6 Mar 2017 12:04:05 -0600 Subject: [PATCH] Rewrite Geometry.get_cell_instance -> Geometry.get_instance --- openmc/cell.py | 17 -------------- openmc/geometry.py | 56 +++++++++++++++++++--------------------------- openmc/lattice.py | 28 ----------------------- openmc/universe.py | 23 +------------------ 4 files changed, 24 insertions(+), 100 deletions(-) diff --git a/openmc/cell.py b/openmc/cell.py index 7948ea912a..4dfed3cad8 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -422,23 +422,6 @@ class Cell(object): else: raise ValueError('No volume information found for this cell.') - def get_cell_instance(self, path, distribcell_index): - - # If the Cell is filled by a Material - if self.fill_type in ('material', 'distribmat', 'void'): - offset = 0 - - # If the Cell is filled by a Universe - elif self.fill_type == 'universe': - offset = self.offsets[distribcell_index-1] - offset += self.fill.get_cell_instance(path, distribcell_index) - - # If the Cell is filled by a Lattice - else: - offset = self.fill.get_cell_instance(path, distribcell_index) - - return offset - def get_nuclides(self): """Returns all nuclides in the cell diff --git a/openmc/geometry.py b/openmc/geometry.py index d9a844896b..a25a7ace64 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -105,53 +105,43 @@ class Geometry(object): """ return self.root_universe.find(point) - def get_cell_instance(self, path): - """Return the instance number for the final cell in a geometry path. + def get_instance(self, path): + """Return the instance number for a cell/material in a geometry path. - The instance is an index into tally distribcell filter arrays. + The instance number is used as an index into distributed + material/temperature arrays and tally distribcell filter arrays. Parameters ---------- - path : list - A list of IDs that form the path to the target. It should begin with - 0 for the base universe, and should cover every universe, cell, and - lattice passed through. For the case of the lattice, a tuple should - be provided to indicate which coordinates in the lattice should be - entered. This should be in the form: (lat_id, i_x, i_y, i_z) + path : str + + The path traversed through the CSG tree to reach a cell or material + instance. For example, 'u0->c10->l20(2,2,1)->u5->c5' would indicate + the cell instance whose first level is universe 0 and cell 10, + second level is lattice 20 position (2,2,1), and third level is + universe 5 and cell 5. Returns ------- - instance : int - Index in tally results array for distribcell filters + int + Instance number for the given path """ # Extract the cell id from the path last_index = path.rfind('>') - cell_id = int(path[last_index+1:]) + last_path = path[last_index+1:] + uid = int(last_path[1:]) - # Find the distribcell index of the cell. - cells = self.get_all_cells() - for cell in cells: - if cell.id == cell_id: - distribcell_index = cell.distribcell_index - break - else: - raise RuntimeError('Could not find cell {} specified in a \ - distribcell filter'.format(cell_id)) + if last_path[0] == 'c': + obj = self.get_all_cells()[uid] + elif last_path[0] == 'm': + obj = self.get_all_materials()[uid] - # Return memoize'd offset if possible - if (path, distribcell_index) in self._offsets: - offset = self._offsets[(path, distribcell_index)] - - # Begin recursive call to compute offset starting with the base Universe - else: - offset = self._root_universe.get_cell_instance(path, - distribcell_index) - self._offsets[(path, distribcell_index)] = offset - - # Return the final offset - return offset + try: + return obj.paths.index(path) + except ValueError: + return None def get_all_cells(self): """Return all cells in the geometry. diff --git a/openmc/lattice.py b/openmc/lattice.py index 7994048b17..c59a3c76c3 100644 --- a/openmc/lattice.py +++ b/openmc/lattice.py @@ -611,34 +611,6 @@ class RectLattice(Lattice): min_depth=2, max_depth=3) self._universes = np.asarray(universes) - def get_cell_instance(self, path, distribcell_index): - # Extract the lattice element from the path - next_index = path.index('-') - lat_id_indices = path[:next_index] - path = path[next_index+2:] - - # Extract the lattice cell indices from the path - i1 = lat_id_indices.index('(') - i2 = lat_id_indices.index(')') - i = lat_id_indices[i1+1:i2] - lat_x = int(i.split(',')[0]) - 1 - lat_y = int(i.split(',')[1]) - 1 - lat_z = int(i.split(',')[2]) - 1 - - # For 2D Lattices - if self.ndim == 2: - offset = self._offsets[lat_z, lat_y, lat_x, distribcell_index-1] - offset += self._universes[lat_x][lat_y].get_cell_instance( - path, distribcell_index) - - # For 3D Lattices - else: - offset = self._offsets[lat_z, lat_y, lat_x, distribcell_index-1] - offset += self._universes[lat_z][lat_y][lat_x].get_cell_instance( - path, distribcell_index) - - return offset - def find_element(self, point): """Determine index of lattice element and local coordinates for a point diff --git a/openmc/universe.py b/openmc/universe.py index 1733a5428b..b8ee823225 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -392,27 +392,6 @@ class Universe(object): self._cells.clear() - def get_cell_instance(self, path, distribcell_index): - - # Pop off the root Universe ID from the path - next_index = path.index('-') - path = path[next_index+2:] - - # Extract the Cell ID from the path - if '-' in path: - next_index = path.index('-') - cell_id = int(path[:next_index]) - path = path[next_index+2:] - else: - cell_id = int(path) - path = '' - - # Make a recursive call to the Cell within this Universe - offset = self.cells[cell_id].get_cell_instance(path, distribcell_index) - - # Return the offset computed at all nested Universe levels - return offset - def get_nuclides(self): """Returns all nuclides in the universe @@ -553,7 +532,7 @@ class Universe(object): mat = None if mat is not None: - mat._paths.append(cell_path) + mat._paths.append('{}->m{}'.format(cell_path, mat.id)) # Append current path cell._paths.append(cell_path)