From 6390360dfa959f94a2281ce2eb4ccf70749863c4 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 14 May 2020 13:44:12 -0500 Subject: [PATCH 01/12] Initial layout of get_contained_cells. --- include/openmc/cell.h | 12 +++++++++++ include/openmc/lattice.h | 11 ++++++++++ src/cell.cpp | 43 +++++++++++++++++++++++++++++++++++++++- src/lattice.cpp | 17 ++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index a5e530d7dc..2faa6bdbfd 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -5,6 +5,7 @@ #include // for hash #include #include // for unique_ptr +#include #include #include #include @@ -43,6 +44,7 @@ constexpr int32_t OP_UNION {std::numeric_limits::max() - 4}; //============================================================================== class Cell; +class CellInstanceItem; class Universe; class UniversePartitioner; @@ -145,6 +147,10 @@ public: //! \param[in] name Cell name void set_name(const std::string& name) { name_ = name; }; + //! Get all cell instances contained by this cell + void get_contained_cells(std::unordered_map>& contained_cells, + std::vector& parent_cells); + //---------------------------------------------------------------------------- // Data members @@ -191,6 +197,12 @@ public: std::vector offset_; //!< Distribcell offset table }; +struct CellInstanceItem { + int32_t index {-1}; //! Index into global cells array + int32_t lattice {-1}; //! Lattice value if part of a lattice + int lattice_indx{-1}; //! Flat index value of the lattice cell +}; + //============================================================================== class CSGCell : public Cell diff --git a/include/openmc/lattice.h b/include/openmc/lattice.h index 95d30aed5a..4fc28b76b4 100644 --- a/include/openmc/lattice.h +++ b/include/openmc/lattice.h @@ -129,6 +129,13 @@ public: //! cell found in the geometry tree under this lattice tile. virtual int32_t& offset(int map, const int i_xyz[3]) = 0; + //! \brief Get the distribcell offset for a lattice tile. + //! \param The map index for the target cell. + //! \param indx The index for a lattice tile. + //! \return Distribcell offset i.e. the largest instance number for the target + //! cell found in the geometry tree for this lattice index. + virtual int32_t& offset(int map, int indx) = 0; + //! \brief Convert an array index to a useful human-readable string. //! \param indx The index for a lattice tile. //! \return A string representing the lattice tile. @@ -220,6 +227,8 @@ public: int32_t& offset(int map, const int i_xyz[3]); + int32_t& offset(int map, int indx); + std::string index_to_string(int indx) const; void to_hdf5_inner(hid_t group_id) const; @@ -262,6 +271,8 @@ public: int32_t& offset(int map, const int i_xyz[3]); + int32_t& offset(int map, int indx); + std::string index_to_string(int indx) const; void to_hdf5_inner(hid_t group_id) const; diff --git a/src/cell.cpp b/src/cell.cpp index 324257022d..f9b5fb4a94 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -1158,6 +1157,48 @@ openmc_cell_set_name(int32_t index, const char* name) { return 0; } +//! Get all cells within this cell +void +Cell::get_contained_cells(std::unordered_map>& contained_cells, + std::vector& parent_cells) +{ + + if (this->type_ == Fill::MATERIAL) { + int instance = 0; + if (this->distribcell_index_ >= 0) { + for (int i = 0; i < parent_cells.size(); i++) { + auto& cell = model::cells[parent_cells[i].index]; + if (cell->type_ == Fill::UNIVERSE) { + instance += cell->offset_[this->distribcell_index_]; + } else if (cell->type_ == Fill::LATTICE) { + auto& lattice = model::lattices[cell->fill_]; + instance += lattice->offset(this->distribcell_index_, parent_cells[i].lattice_indx); + } + } + } + // add entry to contained cells + contained_cells[model::cell_map[this->id_]].insert(instance); + } else if (this->type_ == Fill::UNIVERSE) { + parent_cells.push_back({model::cell_map[this->id_], -1, -1}); + auto& univ = model::universes[fill_]; + for(auto cell_index : univ->cells_) { + auto& cell = model::cells[cell_index]; + cell->get_contained_cells(contained_cells, parent_cells); + } + parent_cells.pop_back(); + } else if (this->type_ == Fill::LATTICE) { + auto& lattice = model::lattices[this->fill_]; + for (auto i = lattice->begin(); i != lattice->end(); ++i) { + auto& univ = model::universes[i.indx_]; + parent_cells.push_back({model::cell_map[this->id_], this->fill_, i.indx_}); + for (auto cell_index : univ->cells_) { + auto& cell = model::cells[cell_index]; + cell->get_contained_cells(contained_cells, parent_cells); + } + parent_cells.pop_back(); + } + } +} //! Return the index in the cells array of a cell with a given ID extern "C" int diff --git a/src/lattice.cpp b/src/lattice.cpp index 2a2e8b79bd..a3287982a3 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -342,6 +342,14 @@ RectLattice::offset(int map, const int i_xyz[3]) //============================================================================== +int32_t& +RectLattice::offset(int map, int indx) +{ + return offsets_[nx*ny*nz*map + indx]; +} + +//============================================================================== + std::string RectLattice::index_to_string(int indx) const { @@ -973,6 +981,15 @@ HexLattice::offset(int map, const int i_xyz[3]) return offsets_[nx*ny*nz*map + nx*ny*i_xyz[2] + nx*i_xyz[1] + i_xyz[0]]; } +int32_t& +HexLattice::offset(int map, int indx) +{ + int nx {2*n_rings_ - 1}; + int ny {2*n_rings_ - 1}; + int nz {n_axial_}; + return offsets_[nx*ny*nz*map + indx]; +} + //============================================================================== std::string From c77eb718320c429aa7dc96c5f4c21ada3e4c7ff8 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 14 May 2020 22:49:13 -0500 Subject: [PATCH 02/12] Fix to the cell gathering algorithm --- include/openmc/cell.h | 7 +++---- src/cell.cpp | 48 ++++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 2faa6bdbfd..5c8a30f344 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -44,7 +44,7 @@ constexpr int32_t OP_UNION {std::numeric_limits::max() - 4}; //============================================================================== class Cell; -class CellInstanceItem; +class CellInstance; class Universe; class UniversePartitioner; @@ -137,7 +137,7 @@ public: //! \param[in] T Temperature in [K] //! \param[in] instance Instance index. If -1 is given, the temperature for //! all instances is set. - void set_temperature(double T, int32_t instance = -1); + void set_temperature(double T, int32_t instance = -1, bool set_contained_cells=false); //! Get the name of a cell //! \return Cell name @@ -149,7 +149,7 @@ public: //! Get all cell instances contained by this cell void get_contained_cells(std::unordered_map>& contained_cells, - std::vector& parent_cells); + std::vector& parent_cells); //---------------------------------------------------------------------------- // Data members @@ -199,7 +199,6 @@ public: struct CellInstanceItem { int32_t index {-1}; //! Index into global cells array - int32_t lattice {-1}; //! Lattice value if part of a lattice int lattice_indx{-1}; //! Flat index value of the lattice cell }; diff --git a/src/cell.cpp b/src/cell.cpp index f9b5fb4a94..0757e699d0 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -239,7 +239,7 @@ Cell::temperature(int32_t instance) const } void -Cell::set_temperature(double T, int32_t instance) +Cell::set_temperature(double T, int32_t instance, bool set_contained_cells) { if (settings::temperature_method == TemperatureMethod::INTERPOLATION) { if (T < data::temperature_min) { @@ -251,17 +251,29 @@ Cell::set_temperature(double T, int32_t instance) } } - if (instance >= 0) { - // If temperature vector is not big enough, resize it first - if (sqrtkT_.size() != n_instances_) sqrtkT_.resize(n_instances_, sqrtkT_[0]); + if (this->type_ == Fill::MATERIAL) { + if (instance >= 0) { + // If temperature vector is not big enough, resize it first + if (sqrtkT_.size() != n_instances_) sqrtkT_.resize(n_instances_, sqrtkT_[0]); - // Set temperature for the corresponding instance - sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T); - } else { - // Set temperature for all instances - for (auto& T_ : sqrtkT_) { - T_ = std::sqrt(K_BOLTZMANN * T); + // Set temperature for the corresponding instance + sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T); + } else { + // Set temperature for all instances + for (auto& T_ : sqrtkT_) { + T_ = std::sqrt(K_BOLTZMANN * T); + } } + } else { + if (!set_contained_cells) { + throw std::runtime_error{fmt::format("Attempted to set the temperature of cell {} " + "which is not filled by a material.", id_)}; + } + std::unordered_map> contained_cells; + std::vector parent_cells; + this->get_contained_cells(contained_cells, parent_cells); + + std::cout << fmt::format("Found {} contained cells.", contained_cells.size()) << std::endl; } } @@ -1160,37 +1172,41 @@ openmc_cell_set_name(int32_t index, const char* name) { //! Get all cells within this cell void Cell::get_contained_cells(std::unordered_map>& contained_cells, - std::vector& parent_cells) + std::vector& parent_cells) { + // filled by material, determine instance based on parent cells if (this->type_ == Fill::MATERIAL) { int instance = 0; if (this->distribcell_index_ >= 0) { for (int i = 0; i < parent_cells.size(); i++) { - auto& cell = model::cells[parent_cells[i].index]; + auto& cell = model::cells[parent_cells[i].index_cell]; if (cell->type_ == Fill::UNIVERSE) { instance += cell->offset_[this->distribcell_index_]; } else if (cell->type_ == Fill::LATTICE) { auto& lattice = model::lattices[cell->fill_]; - instance += lattice->offset(this->distribcell_index_, parent_cells[i].lattice_indx); + instance += lattice->offset(this->distribcell_index_, parent_cells[i].instance); } } } // add entry to contained cells contained_cells[model::cell_map[this->id_]].insert(instance); + // filled with universe, add the containing cell and recurse } else if (this->type_ == Fill::UNIVERSE) { - parent_cells.push_back({model::cell_map[this->id_], -1, -1}); + parent_cells.push_back({model::cell_map[this->id_], -1}); auto& univ = model::universes[fill_]; for(auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; cell->get_contained_cells(contained_cells, parent_cells); } parent_cells.pop_back(); + // filled with a lattice, visit each universe in the lattice + // with a recursive call } else if (this->type_ == Fill::LATTICE) { auto& lattice = model::lattices[this->fill_]; for (auto i = lattice->begin(); i != lattice->end(); ++i) { - auto& univ = model::universes[i.indx_]; - parent_cells.push_back({model::cell_map[this->id_], this->fill_, i.indx_}); + auto& univ = model::universes[*i]; + parent_cells.push_back({model::cell_map[this->id_], i.indx_}); for (auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; cell->get_contained_cells(contained_cells, parent_cells); From c3cb0fca2cb17642ba0bc8afaaab1115181d45c5 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 14 May 2020 22:59:59 -0500 Subject: [PATCH 03/12] Some final changes to the recursive version. Time for testing. --- src/cell.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cell.cpp b/src/cell.cpp index 0757e699d0..10f90fdbe7 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -273,7 +273,14 @@ Cell::set_temperature(double T, int32_t instance, bool set_contained_cells) std::vector parent_cells; this->get_contained_cells(contained_cells, parent_cells); - std::cout << fmt::format("Found {} contained cells.", contained_cells.size()) << std::endl; + for (const auto& entry : contained_cells) { + auto& cell = model::cells[entry.first]; + auto& instances = entry.second; + for (auto instance = instances.begin(); instance != instances.end(); ++instance) { + Expects(cell->type_ == Fill::MATERIAL); + cell->set_temperature(T, *instance); + } + } } } From ad2aa90879affd54df2a493162dc2f07d6527169 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 15 May 2020 20:55:01 -0500 Subject: [PATCH 04/12] Making an inner function for the recursion to clean up the exerior API. --- include/openmc/cell.h | 10 +++++++--- src/cell.cpp | 25 +++++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 5c8a30f344..352750cd06 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -76,7 +76,6 @@ public: }; //============================================================================== -//! A geometry primitive that links surfaces, universes, and materials //============================================================================== class Cell { @@ -148,8 +147,13 @@ public: void set_name(const std::string& name) { name_ = name; }; //! Get all cell instances contained by this cell - void get_contained_cells(std::unordered_map>& contained_cells, - std::vector& parent_cells); + //! \return Map with cell indexes as keys and instances as values + std::unordered_map> + get_contained_cells(); + + void + get_contained_cells_inner(std::unordered_map>& contained_cells, + std::vector& parent_cells); //---------------------------------------------------------------------------- // Data members diff --git a/src/cell.cpp b/src/cell.cpp index 10f90fdbe7..6364a375b7 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -269,15 +269,13 @@ Cell::set_temperature(double T, int32_t instance, bool set_contained_cells) throw std::runtime_error{fmt::format("Attempted to set the temperature of cell {} " "which is not filled by a material.", id_)}; } - std::unordered_map> contained_cells; - std::vector parent_cells; - this->get_contained_cells(contained_cells, parent_cells); + auto contained_cells = this->get_contained_cells(); for (const auto& entry : contained_cells) { auto& cell = model::cells[entry.first]; + Expects(cell->type_ == Fill::MATERIAL); auto& instances = entry.second; for (auto instance = instances.begin(); instance != instances.end(); ++instance) { - Expects(cell->type_ == Fill::MATERIAL); cell->set_temperature(T, *instance); } } @@ -1176,10 +1174,21 @@ openmc_cell_set_name(int32_t index, const char* name) { return 0; } + +std::unordered_map> +Cell::get_contained_cells() { + std::unordered_map> contained_cells; + std::vector parent_cells; + + this->get_contained_cells_inner(contained_cells, parent_cells); + + return contained_cells; +} + //! Get all cells within this cell void -Cell::get_contained_cells(std::unordered_map>& contained_cells, - std::vector& parent_cells) +Cell::get_contained_cells_inner(std::unordered_map>& contained_cells, + std::vector& parent_cells) { // filled by material, determine instance based on parent cells @@ -1204,7 +1213,7 @@ Cell::get_contained_cells(std::unordered_map>& contai auto& univ = model::universes[fill_]; for(auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; - cell->get_contained_cells(contained_cells, parent_cells); + cell->get_contained_cells_inner(contained_cells, parent_cells); } parent_cells.pop_back(); // filled with a lattice, visit each universe in the lattice @@ -1216,7 +1225,7 @@ Cell::get_contained_cells(std::unordered_map>& contai parent_cells.push_back({model::cell_map[this->id_], i.indx_}); for (auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; - cell->get_contained_cells(contained_cells, parent_cells); + cell->get_contained_cells_inner(contained_cells, parent_cells); } parent_cells.pop_back(); } From c7fe3ab9a3f9c4024bb15ed17479f6a5b7330ad7 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 May 2020 13:51:38 -0500 Subject: [PATCH 05/12] Renaming parameter to set_contained. --- include/openmc/cell.h | 5 ++++- src/cell.cpp | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 352750cd06..201c1e5bc0 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -136,7 +136,10 @@ public: //! \param[in] T Temperature in [K] //! \param[in] instance Instance index. If -1 is given, the temperature for //! all instances is set. - void set_temperature(double T, int32_t instance = -1, bool set_contained_cells=false); + //! \param[in] set_contained If this cell is not filled with a material, + //! collect all contained cells with material fills and set their + //! temperatures. + void set_temperature(double T, int32_t instance = -1, bool set_contained=false); //! Get the name of a cell //! \return Cell name diff --git a/src/cell.cpp b/src/cell.cpp index 6364a375b7..3217f2031c 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -239,7 +239,7 @@ Cell::temperature(int32_t instance) const } void -Cell::set_temperature(double T, int32_t instance, bool set_contained_cells) +Cell::set_temperature(double T, int32_t instance, bool set_contained) { if (settings::temperature_method == TemperatureMethod::INTERPOLATION) { if (T < data::temperature_min) { @@ -265,7 +265,7 @@ Cell::set_temperature(double T, int32_t instance, bool set_contained_cells) } } } else { - if (!set_contained_cells) { + if (!set_contained) { throw std::runtime_error{fmt::format("Attempted to set the temperature of cell {} " "which is not filled by a material.", id_)}; } From 00fffc85907da1c056b3943300bc1a71a5884ec1 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 May 2020 13:52:31 -0500 Subject: [PATCH 06/12] Updating the cpp driver test to include testing of the new set_temperature behavior. Includes a fix to the dynamic source test to remove the CMake file as well. --- tests/regression_tests/cpp_driver/driver.cpp | 13 ++++++ .../cpp_driver/inputs_true.dat | 22 +++++++++- .../cpp_driver/results_true.dat | 14 +++--- tests/regression_tests/cpp_driver/test.py | 43 +++++++++++++++++-- tests/regression_tests/source_dlopen/test.py | 1 + 5 files changed, 82 insertions(+), 11 deletions(-) diff --git a/tests/regression_tests/cpp_driver/driver.cpp b/tests/regression_tests/cpp_driver/driver.cpp index 2bc771b247..87d5ca9f59 100644 --- a/tests/regression_tests/cpp_driver/driver.cpp +++ b/tests/regression_tests/cpp_driver/driver.cpp @@ -1,6 +1,8 @@ #include "openmc/capi.h" #include "openmc/cell.h" +#include "openmc/geometry.h" +#include "openmc/summary.h" #include "openmc/tallies/filter.h" #include "openmc/tallies/filter_cell.h" #include "openmc/tallies/tally.h" @@ -29,6 +31,17 @@ int main(int argc, char** argv) { tally->set_filters(filters); tally->set_scores({"flux"}); + // set the temperature of the cell containing + // the lattice + auto& root_univ = openmc::model::universes[openmc::model::root_universe]; + auto& lattice_cell = openmc::model::cells[root_univ->cells_[0]]; + lattice_cell->set_temperature(300, 1, true); + + // the summary file will be used to check that + // temperatures were set correctly so clear + // error output can be provided + openmc::write_summary(); + openmc_run(); openmc_finalize(); return 0; diff --git a/tests/regression_tests/cpp_driver/inputs_true.dat b/tests/regression_tests/cpp_driver/inputs_true.dat index 4d0e90d8f1..84efa64672 100644 --- a/tests/regression_tests/cpp_driver/inputs_true.dat +++ b/tests/regression_tests/cpp_driver/inputs_true.dat @@ -2,8 +2,22 @@ + + + + 4.0 4.0 + 2 2 + -4.0 -4.0 + +1 1 +1 1 + - + + + + + @@ -11,7 +25,11 @@ - + + + + + diff --git a/tests/regression_tests/cpp_driver/results_true.dat b/tests/regression_tests/cpp_driver/results_true.dat index 7748cf5c05..77de290ce4 100644 --- a/tests/regression_tests/cpp_driver/results_true.dat +++ b/tests/regression_tests/cpp_driver/results_true.dat @@ -1,7 +1,11 @@ k-combined: -1.857752E+00 2.922425E-02 +1.934690E+00 2.593158E-02 tally 1: -5.337194E+01 -3.209877E+02 -1.621671E+02 -2.939588E+03 +8.771138E+01 +8.618680E+02 +2.643695E+01 +7.811923E+01 +8.917373E+01 +8.880318E+02 +2.033221E+02 +4.619301E+03 diff --git a/tests/regression_tests/cpp_driver/test.py b/tests/regression_tests/cpp_driver/test.py index a4dd8785b2..42f0c2e581 100644 --- a/tests/regression_tests/cpp_driver/test.py +++ b/tests/regression_tests/cpp_driver/test.py @@ -4,6 +4,7 @@ import shutil import subprocess import textwrap +from numpy.testing import assert_allclose import openmc import pytest @@ -46,6 +47,7 @@ def cpp_driver(request): finally: # Remove local build directory when test is complete shutil.rmtree('build') + os.remove('CMakeLists.txt') @pytest.fixture @@ -57,22 +59,39 @@ def model(): u235.add_nuclide('U235', 1.0, 'ao') u235.set_density('g/cc', 11) + zirc = openmc.Material(name='cladding') + zirc.add_nuclide('Zr90', 1.0) + zirc.set_density('g/cc', 6.44) + water = openmc.Material(name="water") water.add_nuclide('H1', 2.0, 'ao') water.add_nuclide('O16', 1.0, 'ao') water.set_density('g/cc', 1.0) - mats = openmc.Materials([u235, water]) + mats = openmc.Materials([u235, zirc, water]) model.materials = mats # geometry fuel_or = openmc.ZCylinder(r=1.5) - coolant_or = openmc.ZCylinder(r=3.0, boundary_type='reflective') + cladding_or = openmc.ZCylinder(r=1.7) fuel = openmc.Cell(fill=u235, region=-fuel_or) - coolant = openmc.Cell(fill=water, region=+fuel_or & -coolant_or) + cladding = openmc.Cell(fill=zirc, region=+fuel_or & -cladding_or) + moderator = openmc.Cell(fill=water, region=+cladding_or) - model.geometry = openmc.Geometry([fuel, coolant]) + pincell_univ = openmc.Universe(cells=[fuel, cladding, moderator]) + + # lattice + lattice = openmc.RectLattice() + lattice.pitch = (4.0, 4.0) + lattice.lower_left = (-4.0, -4.0) + lattice.universes = [[pincell_univ, pincell_univ], [pincell_univ, pincell_univ]] + lattice_region = openmc.model.rectangular_prism(8.0, + 8.0, + boundary_type='reflective') + lattice_cell = openmc.Cell(fill=lattice, region=lattice_region) + + model.geometry = openmc.Geometry([lattice_cell]) model.settings.particles = 100 model.settings.batches = 10 @@ -97,6 +116,22 @@ class ExternalDriverTestHarness(PyAPITestHarness): openmc.run(openmc_exec=self.executable, event_based=config['event']) + def _compare_results(self): + super()._compare_results() + + # load the summary file + summary = openmc.Summary('summary.h5') + + # get the summary cells + cells = summary.geometry.get_all_cells() + + # for the 2 by 2 lattice, each cell should have 4 + # temperature values set to 300 K + for cell in cells.values(): + if isinstance(cell.fill, openmc.Material): + assert len(cell.temperature) == 4 + assert_allclose(cell.temperature, 300.0) + def test_cpp_driver(cpp_driver, model): harness = ExternalDriverTestHarness(cpp_driver, 'statepoint.10.h5', model) diff --git a/tests/regression_tests/source_dlopen/test.py b/tests/regression_tests/source_dlopen/test.py index 12969cf29d..41690224df 100644 --- a/tests/regression_tests/source_dlopen/test.py +++ b/tests/regression_tests/source_dlopen/test.py @@ -39,6 +39,7 @@ def compile_source(request): # Remove local build directory when test is complete shutil.rmtree('build') + os.remove('CMakeLists.txt') @pytest.fixture From 29cfec5a208ed50573c6cdb174ee751dcaf99e0a Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 May 2020 21:58:18 -0500 Subject: [PATCH 07/12] Creating a separate struct for the parent cell concept. --- include/openmc/cell.h | 19 ++++++++++++++++--- src/cell.cpp | 21 +++++++++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 201c1e5bc0..ef454c6e88 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -44,6 +44,7 @@ constexpr int32_t OP_UNION {std::numeric_limits::max() - 4}; //============================================================================== class Cell; +class ParentCell; class CellInstance; class Universe; class UniversePartitioner; @@ -151,13 +152,15 @@ public: //! Get all cell instances contained by this cell //! \return Map with cell indexes as keys and instances as values - std::unordered_map> + std::unordered_map> get_contained_cells(); +protected: void - get_contained_cells_inner(std::unordered_map>& contained_cells, - std::vector& parent_cells); + get_contained_cells_inner(std::unordered_map>& contained_cells, + std::vector& parent_cells); +public: //---------------------------------------------------------------------------- // Data members @@ -307,6 +310,16 @@ private: std::vector> partitions_; }; + +//============================================================================== +//! Define a containing (parent) cell +//============================================================================== + +struct ParentCell { + gsl::index cell_index; + gsl::index lattice_index; +}; + //============================================================================== //! Define an instance of a particular cell //============================================================================== diff --git a/src/cell.cpp b/src/cell.cpp index 3217f2031c..08476d31f7 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1175,10 +1175,10 @@ openmc_cell_set_name(int32_t index, const char* name) { } -std::unordered_map> +std::unordered_map> Cell::get_contained_cells() { - std::unordered_map> contained_cells; - std::vector parent_cells; + std::unordered_map> contained_cells; + std::vector parent_cells; this->get_contained_cells_inner(contained_cells, parent_cells); @@ -1187,8 +1187,8 @@ Cell::get_contained_cells() { //! Get all cells within this cell void -Cell::get_contained_cells_inner(std::unordered_map>& contained_cells, - std::vector& parent_cells) +Cell::get_contained_cells_inner(std::unordered_map>& contained_cells, + std::vector& parent_cells) { // filled by material, determine instance based on parent cells @@ -1196,18 +1196,19 @@ Cell::get_contained_cells_inner(std::unordered_map>& int instance = 0; if (this->distribcell_index_ >= 0) { for (int i = 0; i < parent_cells.size(); i++) { - auto& cell = model::cells[parent_cells[i].index_cell]; + auto& cell = model::cells[parent_cells[i].cell_index]; if (cell->type_ == Fill::UNIVERSE) { instance += cell->offset_[this->distribcell_index_]; } else if (cell->type_ == Fill::LATTICE) { auto& lattice = model::lattices[cell->fill_]; - instance += lattice->offset(this->distribcell_index_, parent_cells[i].instance); + instance += lattice->offset(this->distribcell_index_, parent_cells[i].lattice_index); } } } // add entry to contained cells - contained_cells[model::cell_map[this->id_]].insert(instance); - // filled with universe, add the containing cell and recurse + contained_cells[model::cell_map[this->id_]].push_back(instance); + // filled with universe, add the containing cell to the parent cells + // and recurse } else if (this->type_ == Fill::UNIVERSE) { parent_cells.push_back({model::cell_map[this->id_], -1}); auto& univ = model::universes[fill_]; @@ -1217,7 +1218,7 @@ Cell::get_contained_cells_inner(std::unordered_map>& } parent_cells.pop_back(); // filled with a lattice, visit each universe in the lattice - // with a recursive call + // with a recursive call to collect the cell instances } else if (this->type_ == Fill::LATTICE) { auto& lattice = model::lattices[this->fill_]; for (auto i = lattice->begin(); i != lattice->end(); ++i) { From 6cbf98962d4f5b515da993908a6046f9a3ac5b6e Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 20 May 2020 12:39:42 -0500 Subject: [PATCH 08/12] Correction to formatting of default param. --- include/openmc/cell.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index ef454c6e88..ba905207ee 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -140,7 +140,7 @@ public: //! \param[in] set_contained If this cell is not filled with a material, //! collect all contained cells with material fills and set their //! temperatures. - void set_temperature(double T, int32_t instance = -1, bool set_contained=false); + void set_temperature(double T, int32_t instance = -1, bool set_contained = false); //! Get the name of a cell //! \return Cell name From db991482813df3db5620da530d66cc6f40d568ff Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 20 May 2020 12:40:00 -0500 Subject: [PATCH 09/12] Adding early return if the cell is filled with a material. --- src/cell.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cell.cpp b/src/cell.cpp index 08476d31f7..431d40b322 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1180,7 +1180,10 @@ Cell::get_contained_cells() { std::unordered_map> contained_cells; std::vector parent_cells; - this->get_contained_cells_inner(contained_cells, parent_cells); + // if this cell is filled w/ a material, it contains no other cells + if (type_ != Fill::MATERIAL) { + this->get_contained_cells_inner(contained_cells, parent_cells); + } return contained_cells; } From 59552944f563c2fc3703f1cb6887ede326434329 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 20 May 2020 12:46:33 -0500 Subject: [PATCH 10/12] Adding a check that material filled cells return an empty result. --- tests/regression_tests/cpp_driver/driver.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/regression_tests/cpp_driver/driver.cpp b/tests/regression_tests/cpp_driver/driver.cpp index 87d5ca9f59..f6fde30fd3 100644 --- a/tests/regression_tests/cpp_driver/driver.cpp +++ b/tests/regression_tests/cpp_driver/driver.cpp @@ -37,6 +37,14 @@ int main(int argc, char** argv) { auto& lattice_cell = openmc::model::cells[root_univ->cells_[0]]; lattice_cell->set_temperature(300, 1, true); + // check that material-filled cells return no contained cells + for (auto& cell : openmc::model::cells) { + if (cell->type_ == Fill::MATERIAL) { + auto contained_cells = cell->get_contained_cells(); + assert(contained_cells.empty()); + } + } + // the summary file will be used to check that // temperatures were set correctly so clear // error output can be provided From 775cc1ae19a80242aceed019e7af624a803ab3ad Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 2 Jun 2020 09:10:13 -0500 Subject: [PATCH 11/12] Incorporating @paulromano's comments --- include/openmc/cell.h | 5 ++--- include/openmc/lattice.h | 6 +++--- src/cell.cpp | 15 ++++++++------- src/lattice.cpp | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index ba905207ee..5efbdd99ef 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -5,7 +5,6 @@ #include // for hash #include #include // for unique_ptr -#include #include #include #include @@ -153,12 +152,12 @@ public: //! Get all cell instances contained by this cell //! \return Map with cell indexes as keys and instances as values std::unordered_map> - get_contained_cells(); + get_contained_cells() const; protected: void get_contained_cells_inner(std::unordered_map>& contained_cells, - std::vector& parent_cells); + std::vector& parent_cells) const; public: //---------------------------------------------------------------------------- diff --git a/include/openmc/lattice.h b/include/openmc/lattice.h index 4fc28b76b4..bf18d92e4b 100644 --- a/include/openmc/lattice.h +++ b/include/openmc/lattice.h @@ -134,7 +134,7 @@ public: //! \param indx The index for a lattice tile. //! \return Distribcell offset i.e. the largest instance number for the target //! cell found in the geometry tree for this lattice index. - virtual int32_t& offset(int map, int indx) = 0; + virtual int32_t offset(int map, int indx) const = 0; //! \brief Convert an array index to a useful human-readable string. //! \param indx The index for a lattice tile. @@ -227,7 +227,7 @@ public: int32_t& offset(int map, const int i_xyz[3]); - int32_t& offset(int map, int indx); + int32_t offset(int map, int indx) const; std::string index_to_string(int indx) const; @@ -271,7 +271,7 @@ public: int32_t& offset(int map, const int i_xyz[3]); - int32_t& offset(int map, int indx); + int32_t offset(int map, int indx) const; std::string index_to_string(int indx) const; diff --git a/src/cell.cpp b/src/cell.cpp index 431d40b322..2761baa44f 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -275,8 +276,8 @@ Cell::set_temperature(double T, int32_t instance, bool set_contained) auto& cell = model::cells[entry.first]; Expects(cell->type_ == Fill::MATERIAL); auto& instances = entry.second; - for (auto instance = instances.begin(); instance != instances.end(); ++instance) { - cell->set_temperature(T, *instance); + for (auto instance : instances) { + cell->set_temperature(T, instance); } } } @@ -1176,7 +1177,7 @@ openmc_cell_set_name(int32_t index, const char* name) { std::unordered_map> -Cell::get_contained_cells() { +Cell::get_contained_cells() const { std::unordered_map> contained_cells; std::vector parent_cells; @@ -1191,20 +1192,20 @@ Cell::get_contained_cells() { //! Get all cells within this cell void Cell::get_contained_cells_inner(std::unordered_map>& contained_cells, - std::vector& parent_cells) + std::vector& parent_cells) const { // filled by material, determine instance based on parent cells if (this->type_ == Fill::MATERIAL) { int instance = 0; if (this->distribcell_index_ >= 0) { - for (int i = 0; i < parent_cells.size(); i++) { - auto& cell = model::cells[parent_cells[i].cell_index]; + for (auto& parent_cell : parent_cells) { + auto& cell = openmc::model::cells[parent_cell.cell_index]; if (cell->type_ == Fill::UNIVERSE) { instance += cell->offset_[this->distribcell_index_]; } else if (cell->type_ == Fill::LATTICE) { auto& lattice = model::lattices[cell->fill_]; - instance += lattice->offset(this->distribcell_index_, parent_cells[i].lattice_index); + instance += lattice->offset(this->distribcell_index_, parent_cell.lattice_index); } } } diff --git a/src/lattice.cpp b/src/lattice.cpp index a3287982a3..f0c3dc979f 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -342,8 +342,8 @@ RectLattice::offset(int map, const int i_xyz[3]) //============================================================================== -int32_t& -RectLattice::offset(int map, int indx) +int32_t +RectLattice::offset(int map, int indx) const { return offsets_[nx*ny*nz*map + indx]; } @@ -981,8 +981,8 @@ HexLattice::offset(int map, const int i_xyz[3]) return offsets_[nx*ny*nz*map + nx*ny*i_xyz[2] + nx*i_xyz[1] + i_xyz[0]]; } -int32_t& -HexLattice::offset(int map, int indx) +int32_t +HexLattice::offset(int map, int indx) const { int nx {2*n_rings_ - 1}; int ny {2*n_rings_ - 1}; From fde485d06a524abf484464509c559f8c2e353316 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 2 Jun 2020 09:11:42 -0500 Subject: [PATCH 12/12] Apply suggestions from code review Co-authored-by: Paul Romano --- src/cell.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cell.cpp b/src/cell.cpp index 2761baa44f..fd488f3604 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -252,7 +252,7 @@ Cell::set_temperature(double T, int32_t instance, bool set_contained) } } - if (this->type_ == Fill::MATERIAL) { + if (type_ == Fill::MATERIAL) { if (instance >= 0) { // If temperature vector is not big enough, resize it first if (sqrtkT_.size() != n_instances_) sqrtkT_.resize(n_instances_, sqrtkT_[0]); @@ -1196,13 +1196,13 @@ Cell::get_contained_cells_inner(std::unordered_map { // filled by material, determine instance based on parent cells - if (this->type_ == Fill::MATERIAL) { + if (type_ == Fill::MATERIAL) { int instance = 0; if (this->distribcell_index_ >= 0) { for (auto& parent_cell : parent_cells) { auto& cell = openmc::model::cells[parent_cell.cell_index]; if (cell->type_ == Fill::UNIVERSE) { - instance += cell->offset_[this->distribcell_index_]; + instance += cell->offset_[distribcell_index_]; } else if (cell->type_ == Fill::LATTICE) { auto& lattice = model::lattices[cell->fill_]; instance += lattice->offset(this->distribcell_index_, parent_cell.lattice_index); @@ -1210,11 +1210,11 @@ Cell::get_contained_cells_inner(std::unordered_map } } // add entry to contained cells - contained_cells[model::cell_map[this->id_]].push_back(instance); + contained_cells[model::cell_map[id_]].push_back(instance); // filled with universe, add the containing cell to the parent cells // and recurse - } else if (this->type_ == Fill::UNIVERSE) { - parent_cells.push_back({model::cell_map[this->id_], -1}); + } else if (type_ == Fill::UNIVERSE) { + parent_cells.push_back({model::cell_map[id_], -1}); auto& univ = model::universes[fill_]; for(auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; @@ -1223,11 +1223,11 @@ Cell::get_contained_cells_inner(std::unordered_map parent_cells.pop_back(); // filled with a lattice, visit each universe in the lattice // with a recursive call to collect the cell instances - } else if (this->type_ == Fill::LATTICE) { - auto& lattice = model::lattices[this->fill_]; + } else if (type_ == Fill::LATTICE) { + auto& lattice = model::lattices[fill_]; for (auto i = lattice->begin(); i != lattice->end(); ++i) { auto& univ = model::universes[*i]; - parent_cells.push_back({model::cell_map[this->id_], i.indx_}); + parent_cells.push_back({model::cell_map[id_], i.indx_}); for (auto cell_index : univ->cells_) { auto& cell = model::cells[cell_index]; cell->get_contained_cells_inner(contained_cells, parent_cells);