From 7a2f1d6d0a926c919e52238ff4bd1b8ad1d9cf0d Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sat, 9 Jan 2021 18:38:30 -0600 Subject: [PATCH] Updating unstructured mesh writing to use the nuclide_score tally method and putting variable addition back into its own loop. --- include/openmc/mesh.h | 12 ++++++------ src/mesh.cpp | 23 +++++++---------------- src/state_point.cpp | 33 +++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index ab1c1c406e..6bc0843861 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -299,8 +299,8 @@ public: //! Set the value of a bin for a variable on the internal // mesh instance virtual void set_score_data(const std::string& var_name, - std::vector values, - std::vector std_dev) = 0; + const std::vector& values, + const std::vector& std_dev) = 0; //! Write the unstructured mesh to file // @@ -373,8 +373,8 @@ public: //! Set data for a score void set_score_data(const std::string& score, - std::vector values, - std::vector std_dev) override; + const std::vector& values, + const std::vector& std_dev) override; //! Write the mesh with any current tally data void write(const std::string& base_filename) const; @@ -516,8 +516,8 @@ public: void remove_scores() override; void set_score_data(const std::string& var_name, - std::vector values, - std::vector std_dev) override; + const std::vector& values, + const std::vector& std_dev) override; void write(const std::string& base_filename) const override; diff --git a/src/mesh.cpp b/src/mesh.cpp index 40b8834a2e..5e035b20e6 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -2088,19 +2088,11 @@ void MOABMesh::remove_scores() void MOABMesh::set_score_data(const std::string& score, - std::vector values, - std::vector std_dev) + const std::vector& values, + const std::vector& std_dev) { auto score_tags = this->get_score_tags(score); - // normalize tally values by element volume - for (int i = 0; i < ehs_.size(); i++) { - auto eh = this->get_ent_handle_from_bin(i); - double volume = this->tet_volume(eh); - values[i] /= volume; - std_dev[i] /= volume; - } - moab::ErrorCode rval; // set the score value rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data()); @@ -2250,13 +2242,13 @@ LibMesh::remove_scores() { auto& eqn_sys = equation_systems_->get_system(eq_system_name_); eqn_sys.clear(); - eqn_sys.init(); + variable_map_.clear(); } void LibMesh::set_score_data(const std::string& var_name, - std::vector values, - std::vector std_dev) + const std::vector& values, + const std::vector& std_dev) { auto& eqn_sys = equation_systems_->get_system(eq_system_name_); @@ -2278,14 +2270,13 @@ LibMesh::set_score_data(const std::string& var_name, std::vector value_dof_indices; dof_map.dof_indices(e, value_dof_indices, value_num); Ensures(value_dof_indices.size() == 1); - eqn_sys.solution->set(value_dof_indices[0], values[bin]); + eqn_sys.solution->set(value_dof_indices[0], values.at(bin)); // set std dev std::vector std_dev_dof_indices; dof_map.dof_indices(e, std_dev_dof_indices, std_dev_num); Ensures(std_dev_dof_indices.size() == 1); - eqn_sys.solution->set(std_dev_dof_indices[0], std_dev[bin]); - + eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin)); } } diff --git a/src/state_point.cpp b/src/state_point.cpp index 4e4c3849f3..80349c9f72 100644 --- a/src/state_point.cpp +++ b/src/state_point.cpp @@ -770,8 +770,6 @@ void read_source_bank(hid_t group_id, std::vector& sites, bool d H5Tclose(banktype); } - - void write_unstructured_mesh_results() { #if !defined(LIBMESH) && !defined(DAGMC) @@ -807,18 +805,24 @@ void write_unstructured_mesh_results() { for (int score_idx = 0; score_idx < tally->scores_.size(); score_idx++) { for (int nuc_idx = 0; nuc_idx < tally->nuclides_.size(); nuc_idx++) { - // get the name for this nuclide - int nuclide = tally->nuclides_[nuc_idx]; - std::string nuclide_name = "total"; // start with total by default - if (nuclide > -1) nuclide_name = data::nuclides[nuclide]->name_; - - // get the name for this score - std::string score_name = tally->score_name(score_idx); - // combine the score and nuclide into a name for the value - auto score_str = fmt::format("{}_{}", score_name, nuclide_name); + auto score_str = fmt::format("{}_{}", + tally->score_name(score_idx), + tally->nuclide_name(nuc_idx)); // add this score to the mesh + // (this is in a separate loop because all variables need to be added + // to libMesh's equation system before any are initialized, which + // happens in set_score_data) umesh->add_score(score_str); + } + } + + for (int score_idx = 0; score_idx < tally->scores_.size(); score_idx++) { + for (int nuc_idx = 0; nuc_idx < tally->nuclides_.size(); nuc_idx++) { + // combine the score and nuclide into a name for the value + auto score_str = fmt::format("{}_{}", + tally->score_name(score_idx), + tally->nuclide_name(nuc_idx)); // index for this nuclide and score int nuc_score_idx = score_idx + nuc_idx*tally->scores_.size(); @@ -826,9 +830,11 @@ void write_unstructured_mesh_results() { // construct result vectors std::vector mean_vec, std_dev_vec; for (int j = 0; j < tally->results_.shape()[0]; j++) { + // get the volume for this bin + double volume = umesh->volume(j); // compute the mean double mean = tally->results_(j, nuc_score_idx, TallyResult::SUM) / n_realizations; - mean_vec.push_back(mean); + mean_vec.push_back(mean / volume); // compute the standard deviation double sum_sq = tally->results_(j , nuc_score_idx, TallyResult::SUM_SQ); @@ -837,9 +843,8 @@ void write_unstructured_mesh_results() { std_dev = sum_sq/n_realizations - mean*mean; std_dev = std::sqrt(std_dev / (n_realizations - 1)); } - std_dev_vec.push_back(std_dev); + std_dev_vec.push_back(std_dev / volume); } - // set the data for this score umesh->set_score_data(score_str, mean_vec, std_dev_vec); }