From 161bfd064a0a6a8d8e68979c4742e801df9d9983 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sat, 27 Sep 2025 20:51:09 -0500 Subject: [PATCH 1/3] use new API for adaptive mesh --- include/openmc/mesh.h | 17 +++++++++++++++++ src/mesh.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index a56705c9ec5..3a8e13ee6cc 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -1041,6 +1041,9 @@ class AdaptiveLibMesh : public LibMesh { void write(const std::string& filename) 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; @@ -1056,6 +1059,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 58d218b9cad..5b4fc947e87 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3537,6 +3537,44 @@ AdaptiveLibMesh::AdaptiveLibMesh( } } +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_; @@ -3566,7 +3604,9 @@ void AdaptiveLibMesh::write(const std::string& filename) 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 9f7a9605934754602350fc3586d881567f54af57 Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sat, 27 Sep 2025 21:43:06 -0500 Subject: [PATCH 2/3] 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 3a8e13ee6cc..b5e67f52b9b 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -1063,8 +1063,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 5b4fc947e87..be45049af8d 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3540,20 +3540,20 @@ AdaptiveLibMesh::AdaptiveLibMesh( 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; @@ -3568,10 +3568,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)); } } @@ -3605,9 +3605,10 @@ void AdaptiveLibMesh::write(const std::string& filename) 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 3291dfd3d1eb9b9297163609b1e89a598e57bedc Mon Sep 17 00:00:00 2001 From: magnoxemo Date: Sun, 28 Sep 2025 08:59:15 -0500 Subject: [PATCH 3/3] 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 be45049af8d..ca85646584a 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3607,8 +3607,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;