mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-26 13:15:39 -04:00
Applying a new stack to simplify parent cell traversal alg.
This commit is contained in:
parent
683a671d11
commit
0817a792d9
2 changed files with 38 additions and 13 deletions
|
|
@ -6,6 +6,7 @@
|
|||
#include <limits>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <set>
|
||||
|
||||
#include "hdf5.h"
|
||||
#include "pugixml.hpp"
|
||||
|
|
@ -319,6 +320,33 @@ struct ParentCell {
|
|||
gsl::index lattice_index;
|
||||
};
|
||||
|
||||
struct ParentCellStack {
|
||||
|
||||
void push_back(int32_t search_universe, const ParentCell& pc) {
|
||||
parent_cells_.push_back(pc);
|
||||
// add parent cell to the set of cells we've visited for this search universe
|
||||
visited_cells_[search_universe].insert(pc);
|
||||
}
|
||||
|
||||
void pop_back() {
|
||||
visited_cells_[this->current_univ()].clear();
|
||||
parent_cells_.pop_back();
|
||||
}
|
||||
|
||||
bool visited(int32_t univ_idx, const ParentCell& parent_cell) {
|
||||
return visited_cells_[univ_idx].count(parent_cell) != 0;
|
||||
}
|
||||
|
||||
int32_t current_univ() const { return model::cells[parent_cells_.back().cell_index]->universe_; }
|
||||
|
||||
bool empty() const { return parent_cells_.empty(); }
|
||||
|
||||
std::vector<ParentCell> parent_cells() { return parent_cells_; }
|
||||
|
||||
std::vector<ParentCell> parent_cells_;
|
||||
std::unordered_map<int32_t, std::set<ParentCell>> visited_cells_;
|
||||
};
|
||||
|
||||
struct ParentCellHash {
|
||||
std::size_t operator()(const ParentCell& p) const
|
||||
{
|
||||
|
|
|
|||
23
src/cell.cpp
23
src/cell.cpp
|
|
@ -5,7 +5,6 @@
|
|||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
|
|
@ -1223,26 +1222,21 @@ extern "C" int openmc_cell_set_name(int32_t index, const char* name)
|
|||
void
|
||||
Cell::find_parent_cells(vector<ParentCell>& parent_cells, int32_t instance) const
|
||||
{
|
||||
// clear out the list of parent cells
|
||||
parent_cells.clear();
|
||||
ParentCellStack stack;
|
||||
// start with this cell's universe
|
||||
int32_t visited_univ = -1;
|
||||
int32_t prev_univ_idx = -1;
|
||||
int32_t prev_univ_idx = C_NONE;
|
||||
int32_t univ_idx = this->universe_;
|
||||
int32_t search_instance = -1;
|
||||
|
||||
std::set<ParentCell> visited_cells;
|
||||
|
||||
while (true) {
|
||||
const auto& univ = model::universes[univ_idx];
|
||||
prev_univ_idx = univ_idx;
|
||||
for (const auto& cell : model::cells) {
|
||||
// if this is in our current set of visited cells, move on
|
||||
if (visited_cells.count({model::cell_map[cell->id_], -1})) continue;
|
||||
if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE})) continue;
|
||||
|
||||
if (cell->type_ == Fill::UNIVERSE) {
|
||||
if(cell->fill_ == univ_idx) {
|
||||
parent_cells.push_back({model::cell_map[cell->id_], -1});
|
||||
stack.push_back(univ_idx, {model::cell_map[cell->id_], C_NONE});
|
||||
univ_idx = cell->universe_;
|
||||
}
|
||||
} else if (cell->type_ == Fill::LATTICE) {
|
||||
|
|
@ -1258,9 +1252,9 @@ Cell::find_parent_cells(vector<ParentCell>& parent_cells, int32_t instance) cons
|
|||
|
||||
lat_it++;
|
||||
|
||||
if (visited_cells.count({model::cell_map[cell->id_], lattice_idx})) continue;
|
||||
if (stack.visited(univ_idx, {model::cell_map[cell->id_], lattice_idx})) continue;
|
||||
|
||||
parent_cells.push_back({model::cell_map[cell->id_], lattice_idx});
|
||||
stack.push_back(univ_idx, {model::cell_map[cell->id_], lattice_idx});
|
||||
univ_idx = cell->universe_;
|
||||
}
|
||||
}
|
||||
|
|
@ -1279,10 +1273,13 @@ Cell::find_parent_cells(vector<ParentCell>& parent_cells, int32_t instance) cons
|
|||
|
||||
// if we don't find a suitable update, adjust the stack and continue
|
||||
if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
|
||||
}
|
||||
stack.pop_back();
|
||||
univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
|
||||
}
|
||||
|
||||
} // end while
|
||||
|
||||
parent_cells = stack.parent_cells();
|
||||
std::reverse(parent_cells.begin(), parent_cells.end());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue