From 7fc5c7e5479429815b8f3f7ff350715c730a5e46 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sat, 27 Sep 2025 20:51:09 -0500 Subject: [PATCH 1/4] use new API for adaptive mesh --- include/openmc/mesh.h | 19 +++++++++++++++++-- src/mesh.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index 2936d3ab1bd..a5f942548a4 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -429,7 +429,6 @@ class PeriodicStructuredMesh : public StructuredMesh { public: PeriodicStructuredMesh() = default; PeriodicStructuredMesh(pugi::xml_node node) : StructuredMesh {node} {}; - PeriodicStructuredMesh(hid_t group) : StructuredMesh {group} {}; Position local_coords(const Position& r) const override { @@ -788,7 +787,6 @@ class MOABMesh : public UnstructuredMesh { // Constructors MOABMesh() = default; MOABMesh(pugi::xml_node); - MOABMesh(hid_t group); MOABMesh(const std::string& filename, double length_multiplier = 1.0); MOABMesh(std::shared_ptr external_mbi); @@ -1062,6 +1060,9 @@ class AdaptiveLibMesh : public LibMesh { int get_bin(Position r) const override; + //! setter for mesh tally amalgamtion + void set_mesh_tally_amalgamation(std::string cluster_element_integer_name); + protected: // Overridden methods int get_bin_from_element(const libMesh::Elem* elem) const override; @@ -1080,6 +1081,20 @@ class AdaptiveLibMesh : public LibMesh { //!< elements std::vector elem_to_bin_map_; //!< mapping dof indices to bin indices for //!< active elements + + bool amalgamation_ = false; //!< whether we are doing mesh and tally + //!< amalgamation by default it's turned off. + + int clustering_element_integer_index_ = -1; //!< extra element integer index for + // element clustering + + /*create a hash map where every element in a cluster would map to the first + * element of in that cluster if the element isn't part of a cluster then it + * will point to it self + */ + std::unordered_map + clustering_element_mapping_; }; #endif diff --git a/src/mesh.cpp b/src/mesh.cpp index 6ee15b9c18b..e506bdbc610 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3763,6 +3763,44 @@ AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh, } } +void AdaptiveLibMesh::set_mesh_tally_amalgamation( + std::string cluster_element_integer_name) +{ + + clustering_element_integer_index_ = + m_->has_elem_integer(cluster_element_integer_name) + ? m_->get_elem_integer_index(cluster_element_integer_name) + : -1; + amalgamation_ = (clustering_element_integer_index_ != -1); + + if (amalgamation_) { + // reseve the hash map for cluster elements + clustering_element_mapping_.reserve(m_->n_active_elem()); + + // adding clustering map + for (auto it = m_->active_elements_begin(); it != m_->active_elements_end(); + it++) { + + auto elem = *it; + auto cluster_elem = elem; + unsigned int cluster_id = + elem->get_extra_integer(clustering_element_integer_index_); + + if (cluster_id != -1) { + auto first_element_in_a_cluster = m_->elem_ptr(cluster_id); + + if (first_element_in_a_cluster and first_element_in_a_cluster->active()) + cluster_elem = first_element_in_a_cluster; + } + clustering_element_mapping_.insert(std::make_pair(elem, cluster_elem)); + } + } + else{ + fatal_error(fmt::format("No extra element integer named: {} found in the " + "mesh", cluster_element_integer_name)); + } +} + int AdaptiveLibMesh::n_bins() const { return num_active_; @@ -3813,7 +3851,9 @@ int AdaptiveLibMesh::get_bin(Position r) const int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const { - int bin = elem_to_bin_map_[elem->id()]; + auto tally_elem = amalgamation_ ? clustering_element_mapping_.at(elem) : elem; + int bin = adaptive_ ? elem_to_bin_map_[tally_elem->id()] + : tally_elem->id() - first_element_id_; if (bin >= n_bins() || bin < 0) { fatal_error(fmt::format("Invalid bin: {}", bin)); } From 4ff4d4cdfd7aecdb4c6667c78855b906915c9c38 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sat, 27 Sep 2025 21:43:06 -0500 Subject: [PATCH 2/4] I don't need adaptive_ check for an Adaptive class --- include/openmc/mesh.h | 5 +++-- src/mesh.cpp | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index a5f942548a4..a605d59812b 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -1085,8 +1085,9 @@ class AdaptiveLibMesh : public LibMesh { bool amalgamation_ = false; //!< whether we are doing mesh and tally //!< amalgamation by default it's turned off. - int clustering_element_integer_index_ = -1; //!< extra element integer index for - // element clustering + int clustering_element_integer_index_ = + -1; //!< extra element integer index for + // element clustering /*create a hash map where every element in a cluster would map to the first * element of in that cluster if the element isn't part of a cluster then it diff --git a/src/mesh.cpp b/src/mesh.cpp index e506bdbc610..a3783dbdc89 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3766,20 +3766,20 @@ AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh, void AdaptiveLibMesh::set_mesh_tally_amalgamation( std::string cluster_element_integer_name) { - + clustering_element_integer_index_ = m_->has_elem_integer(cluster_element_integer_name) ? m_->get_elem_integer_index(cluster_element_integer_name) : -1; amalgamation_ = (clustering_element_integer_index_ != -1); - + if (amalgamation_) { // reseve the hash map for cluster elements clustering_element_mapping_.reserve(m_->n_active_elem()); // adding clustering map for (auto it = m_->active_elements_begin(); it != m_->active_elements_end(); - it++) { + it++) { auto elem = *it; auto cluster_elem = elem; @@ -3794,10 +3794,10 @@ void AdaptiveLibMesh::set_mesh_tally_amalgamation( } clustering_element_mapping_.insert(std::make_pair(elem, cluster_elem)); } - } - else{ + } else { fatal_error(fmt::format("No extra element integer named: {} found in the " - "mesh", cluster_element_integer_name)); + "mesh", + cluster_element_integer_name)); } } @@ -3852,9 +3852,10 @@ int AdaptiveLibMesh::get_bin(Position r) const int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const { auto tally_elem = amalgamation_ ? clustering_element_mapping_.at(elem) : elem; - int bin = adaptive_ ? elem_to_bin_map_[tally_elem->id()] - : tally_elem->id() - first_element_id_; - if (bin >= n_bins() || bin < 0) { + int bin = elem_to_bin_map_[tally_elem->id()]; + + if (bin >= n_bins() || bin < 0) + { fatal_error(fmt::format("Invalid bin: {}", bin)); } return bin; From 5c167568f059f2611dd3ed2371690d14463a1349 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sun, 28 Sep 2025 08:59:15 -0500 Subject: [PATCH 3/4] apply clang format --- src/mesh.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mesh.cpp b/src/mesh.cpp index a3783dbdc89..c909993b76e 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3854,8 +3854,7 @@ int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const auto tally_elem = amalgamation_ ? clustering_element_mapping_.at(elem) : elem; int bin = elem_to_bin_map_[tally_elem->id()]; - if (bin >= n_bins() || bin < 0) - { + if (bin >= n_bins() || bin < 0) { fatal_error(fmt::format("Invalid bin: {}", bin)); } return bin; From 752e80b2e8eaa1abe313c2309343af63ee3e4c00 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sat, 17 Jan 2026 11:10:04 -0600 Subject: [PATCH 4/4] applying clang format --- include/openmc/mesh.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index a605d59812b..2ab8c412871 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -429,6 +429,7 @@ class PeriodicStructuredMesh : public StructuredMesh { public: PeriodicStructuredMesh() = default; PeriodicStructuredMesh(pugi::xml_node node) : StructuredMesh {node} {}; + PeriodicStructuredMesh(hid_t group) : StructuredMesh {group} {}; Position local_coords(const Position& r) const override { @@ -787,6 +788,7 @@ class MOABMesh : public UnstructuredMesh { // Constructors MOABMesh() = default; MOABMesh(pugi::xml_node); + MOABMesh(hid_t group); MOABMesh(const std::string& filename, double length_multiplier = 1.0); MOABMesh(std::shared_ptr external_mbi);