Skip to content

Commit 2332dd2

Browse files
committed
Adding support for a position hint to get_contained_cells for acceleration
1 parent f8488e5 commit 2332dd2

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

include/openmc/cell.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ class Cell {
172172
//! \return Map with cell indexes as keys and
173173
//! instances as values
174174
std::unordered_map<int32_t, vector<int32_t>> get_contained_cells(
175-
int32_t instance = 0) const;
175+
int32_t instance = 0, Position* hint = nullptr) const;
176176

177177
protected:
178178
//! Determine the path to this cell instance in the geometry hierarchy
179-
vector<ParentCell> find_parent_cells(int32_t instance) const;
179+
vector<ParentCell> find_parent_cells(
180+
int32_t instance, Position* hint = nullptr) const;
180181

181182
//! Inner function for retrieving contained cells
182183
void get_contained_cells_inner(

include/openmc/lattice.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class Lattice {
9999
virtual void get_indices(
100100
Position r, Direction u, array<int, 3>& result) const = 0;
101101

102+
//! \brief Compute the the flat index for a set of lattice cell indices
103+
//! \param i_xyz The indices for a lattice cell
104+
//! \return Flat index into the universes vector
105+
virtual int get_flat_index(const array<int, 3>& i_xyz) const = 0;
106+
102107
//! \brief Get coordinates local to a lattice tile.
103108
//! \param r A 3D Cartesian coordinate.
104109
//! \param i_xyz The indices for a lattice tile.
@@ -210,6 +215,8 @@ class RectLattice : public Lattice {
210215

211216
void get_indices(Position r, Direction u, array<int, 3>& result) const;
212217

218+
int get_flat_index(const array<int, 3>& i_xyz) const;
219+
213220
Position get_local_position(Position r, const array<int, 3>& i_xyz) const;
214221

215222
int32_t& offset(int map, array<int, 3> const& i_xyz);
@@ -245,6 +252,8 @@ class HexLattice : public Lattice {
245252

246253
void get_indices(Position r, Direction u, array<int, 3>& result) const;
247254

255+
int get_flat_index(const array<int, 3>& i_xyz) const;
256+
248257
Position get_local_position(Position r, const array<int, 3>& i_xyz) const;
249258

250259
bool is_valid_index(int indx) const;

src/cell.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,12 +1316,14 @@ struct ParentCellStack {
13161316
visited_cells_;
13171317
};
13181318

1319-
vector<ParentCell> Cell::find_parent_cells(int32_t instance) const
1319+
vector<ParentCell> Cell::find_parent_cells(
1320+
int32_t instance, Position* hint) const
13201321
{
13211322
ParentCellStack stack;
13221323
// start with this cell's universe
13231324
int32_t prev_univ_idx;
13241325
int32_t univ_idx = this->universe_;
1326+
bool hint_used = false;
13251327

13261328
while (true) {
13271329
const auto& univ = model::universes[univ_idx];
@@ -1350,26 +1352,38 @@ vector<ParentCell> Cell::find_parent_cells(int32_t instance) const
13501352
const auto& lattice = model::lattices[cell->fill_];
13511353
const auto& lattice_univs = lattice->universes_;
13521354

1353-
// start search for universe
1354-
auto lat_it = lattice_univs.begin();
1355-
while (true) {
1356-
// find the next lattice cell with this universe
1357-
lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
1358-
if (lat_it == lattice_univs.end())
1355+
// if a hint position is provided, look up the lattice cell
1356+
// for that position
1357+
if (hint && !hint_used) {
1358+
std::array<int, 3> hint_idx;
1359+
lattice->get_indices(*hint, Direction(0.0, 0.0, 0.0), hint_idx);
1360+
int hint_offset = lattice->get_flat_index(hint_idx);
1361+
if (univ_idx == lattice_univs[hint_offset]) {
1362+
hint_used = true;
1363+
stack.push(univ_idx, {model::cell_map[cell->id_], hint_offset});
1364+
}
1365+
} else {
1366+
// start search for universe
1367+
auto lat_it = lattice_univs.begin();
1368+
while (true) {
1369+
// find the next lattice cell with this universe
1370+
lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
1371+
if (lat_it == lattice_univs.end())
1372+
break;
1373+
1374+
int lattice_idx = lat_it - lattice_univs.begin();
1375+
1376+
// move iterator forward one to avoid finding the same entry
1377+
lat_it++;
1378+
if (stack.visited(
1379+
univ_idx, {model::cell_map[cell->id_], lattice_idx}))
1380+
continue;
1381+
1382+
// add this cell and lattice index to the stack and exit loop
1383+
stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
1384+
univ_idx = cell->universe_;
13591385
break;
1360-
1361-
int lattice_idx = lat_it - lattice_univs.begin();
1362-
1363-
// move iterator forward one to avoid finding the same entry
1364-
lat_it++;
1365-
if (stack.visited(
1366-
univ_idx, {model::cell_map[cell->id_], lattice_idx}))
1367-
continue;
1368-
1369-
// add this cell and lattice index to the stack and exit loop
1370-
stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
1371-
univ_idx = cell->universe_;
1372-
break;
1386+
}
13731387
}
13741388
}
13751389
// if we've updated the universe, break
@@ -1403,7 +1417,7 @@ vector<ParentCell> Cell::find_parent_cells(int32_t instance) const
14031417
}
14041418

14051419
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
1406-
int32_t instance) const
1420+
int32_t instance, Position* hint) const
14071421
{
14081422
std::unordered_map<int32_t, vector<int32_t>> contained_cells;
14091423

@@ -1412,7 +1426,7 @@ std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
14121426
return contained_cells;
14131427

14141428
// find the pathway through the geometry to this cell
1415-
vector<ParentCell> parent_cells = this->find_parent_cells(instance);
1429+
vector<ParentCell> parent_cells = this->find_parent_cells(instance, hint);
14161430

14171431
// if this cell is filled w/ a material, it contains no other cells
14181432
if (type_ != Fill::MATERIAL) {

src/lattice.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ RectLattice::RectLattice(pugi::xml_node lat_node) : Lattice {lat_node}
215215

216216
int32_t const& RectLattice::operator[](array<int, 3> const& i_xyz)
217217
{
218-
int indx =
219-
n_cells_[0] * n_cells_[1] * i_xyz[2] + n_cells_[0] * i_xyz[1] + i_xyz[0];
220-
return universes_[indx];
218+
return universes_[get_flat_index(i_xyz)];
221219
}
222220

223221
//==============================================================================
@@ -323,6 +321,12 @@ void RectLattice::get_indices(
323321
}
324322
}
325323

324+
int RectLattice::get_flat_index(const array<int, 3>& i_xyz) const
325+
{
326+
return n_cells_[0] * n_cells_[1] * i_xyz[2] + n_cells_[0] * i_xyz[1] +
327+
i_xyz[0];
328+
}
329+
326330
//==============================================================================
327331

328332
Position RectLattice::get_local_position(
@@ -662,9 +666,7 @@ void HexLattice::fill_lattice_y(const vector<std::string>& univ_words)
662666

663667
int32_t const& HexLattice::operator[](array<int, 3> const& i_xyz)
664668
{
665-
int indx = (2 * n_rings_ - 1) * (2 * n_rings_ - 1) * i_xyz[2] +
666-
(2 * n_rings_ - 1) * i_xyz[1] + i_xyz[0];
667-
return universes_[indx];
669+
return universes_[get_flat_index(i_xyz)];
668670
}
669671

670672
//==============================================================================
@@ -929,6 +931,12 @@ void HexLattice::get_indices(
929931
result[1] += i2_chg;
930932
}
931933

934+
int HexLattice::get_flat_index(const array<int, 3>& i_xyz) const
935+
{
936+
return (2 * n_rings_ - 1) * (2 * n_rings_ - 1) * i_xyz[2] +
937+
(2 * n_rings_ - 1) * i_xyz[1] + i_xyz[0];
938+
}
939+
932940
//==============================================================================
933941

934942
Position HexLattice::get_local_position(

0 commit comments

Comments
 (0)