Some improvements to readability and moving to removal of all scores from mesh instances in one step.

This commit is contained in:
Patrick Shriwise 2021-01-06 21:50:28 -06:00
parent 33158e7815
commit 9fec4b4b3e
3 changed files with 59 additions and 65 deletions

View file

@ -293,8 +293,8 @@ public:
//! Add a variable to the mesh instance
virtual void add_score(const std::string& var_name) = 0;
//! Remove variable from the mesh instance
virtual void remove_score(const std::string& var_name) = 0;
//! Remove tally data from the instance
virtual void remove_scores() = 0;
//! Set the value of a bin for a variable on the internal
// mesh instance
@ -304,8 +304,8 @@ public:
//! Write the unstructured mesh to file
//
//! \param[in] filename Name of the file to write
virtual void write(std::string filename) const = 0;
//! \param[in] filename Base of the file to write
virtual void write(const std::string& base_filename) const = 0;
//! Retrieve a centroid for the mesh cell
//
@ -369,7 +369,7 @@ public:
void add_score(const std::string& score) override;
//! Remove a score from the mesh instance
void remove_score(const std::string& score) override;
void remove_scores() override;
//! Set data for a score
void set_score_data(const std::string& score,
@ -377,7 +377,7 @@ public:
std::vector<double> std_dev) override;
//! Write the mesh with any current tally data
void write(std::string base_filename) const;
void write(const std::string& base_filename) const;
Position centroid(int bin) const override;
@ -484,6 +484,7 @@ private:
std::unique_ptr<moab::Interface> mbi_; //!< MOAB instance
std::unique_ptr<moab::AdaptiveKDTree> kdtree_; //!< MOAB KDTree instance
std::vector<moab::Matrix3> baryc_data_; //!< Barycentric data for tetrahedra
std::vector<std::string> tag_names_; //!< Names of score tags added to the mesh
};
#endif
@ -512,13 +513,13 @@ public:
void add_score(const std::string& var_name) override;
void remove_score(const std::string& var_name) override;
void remove_scores() override;
void set_score_data(const std::string& var_name,
std::vector<double> values,
std::vector<double> std_dev) override;
void write(std::string filename) const override;
void write(const std::string& base_filename) const override;
Position centroid(int bin) const override;

View file

@ -25,6 +25,7 @@
#include "openmc/message_passing.h"
#include "openmc/search.h"
#include "openmc/settings.h"
#include "openmc/tallies/tally.h"
#include "openmc/tallies/filter.h"
#include "openmc/xml_interface.h"
@ -2050,35 +2051,39 @@ void
MOABMesh::add_score(const std::string& score)
{
auto score_tags = get_score_tags(score);
tag_names_.push_back(score);
}
void MOABMesh::remove_score(const std::string& score)
void MOABMesh::remove_scores()
{
auto value_name = score + "_mean";
moab::Tag tag;
moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
if (rval != moab::MB_SUCCESS) return;
for (const auto& name : tag_names_) {
auto value_name = name + "_mean";
moab::Tag tag;
moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
if (rval != moab::MB_SUCCESS) return;
rval = mbi_->tag_delete(tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Failed to delete mesh tag for the score {}"
" on unstructured mesh {}", score, id_);
fatal_error(msg);
}
rval = mbi_->tag_delete(tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Failed to delete mesh tag for the score {}"
" on unstructured mesh {}", name, id_);
fatal_error(msg);
}
auto std_dev_name = score + "_std_dev";
rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Std. Dev. mesh tag does not exist for the score {}"
" on unstructured mesh {}", score, id_);
}
auto std_dev_name = name + "_std_dev";
rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Std. Dev. mesh tag does not exist for the score {}"
" on unstructured mesh {}", name, id_);
}
rval = mbi_->tag_delete(tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Failed to delete mesh tag for the score {}"
" on unstructured mesh {}", score, id_);
fatal_error(msg);
rval = mbi_->tag_delete(tag);
if (rval != moab::MB_SUCCESS) {
auto msg = fmt::format("Failed to delete mesh tag for the score {}"
" on unstructured mesh {}", name, id_);
fatal_error(msg);
}
}
tag_names_.clear();
}
void
@ -2115,7 +2120,7 @@ MOABMesh::set_score_data(const std::string& score,
}
void
MOABMesh::write(std::string base_filename) const
MOABMesh::write(const std::string& base_filename) const
{
// add extension to the base name
auto filename = base_filename + ".vtk";
@ -2241,12 +2246,12 @@ LibMesh::add_score(const std::string& var_name)
}
void
LibMesh::remove_score(const std::string& var_name)
LibMesh::remove_scores()
{
return;
equation_systems_->clear();
equation_systems_->init();
}
void
LibMesh::set_score_data(const std::string& var_name,
std::vector<double> values,
@ -2283,7 +2288,7 @@ LibMesh::set_score_data(const std::string& var_name,
}
}
void LibMesh::write(std::string filename) const
void LibMesh::write(const std::string& filename) const
{
write_message(fmt::format("Writing file: {}.e for unstructured mesh {}", filename, this->id_));
libMesh::ExodusII_IO exo(*m_);

View file

@ -805,52 +805,40 @@ void write_unstructured_mesh_results() {
int n_realizations = tally->n_realizations_;
for (int i_score = 0; i_score < tally->scores_.size(); i_score++) {
for (int score_idx = 0; score_idx < tally->scores_.size(); score_idx++) {
for (auto nuc_idx : tally->nuclides_) {
// generate a name for the value
// get the name for this nuclide
std::string nuclide_name = "total"; // start with total by default
if (nuc_idx > -1) {
nuclide_name = data::nuclides[nuc_idx]->name_;
}
if (nuc_idx > -1) nuclide_name = data::nuclides[nuc_idx]->name_;
std::string score_name = tally->score_name(i_score);
// 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);
// add this score to the mesh
umesh->add_score(score_str);
}
}
// write each score/nuclide combination for this tally
for (int i_score = 0; i_score < tally->scores_.size(); i_score++) {
for (int i_nuc = 0; i_nuc < tally->nuclides_.size(); i_nuc++) {
// index for this nuclide and score
int nuc_score_idx = i_score + i_nuc*tally->scores_.size();
int nuc_score_idx = score_idx + nuc_idx*tally->scores_.size();
// construct result vectors
std::vector<double> mean_vec, std_dev_vec;
for (int j = 0; j < tally->results_.shape()[0]; j++) {
// mean
// compute the mean
double mean = tally->results_(j, nuc_score_idx, TallyResult::SUM) / n_realizations;
mean_vec.push_back(mean);
// std. dev.
// compute the standard deviation
double sum_sq = tally->results_(j , nuc_score_idx, TallyResult::SUM_SQ);
double std_dev {0.0};
if (n_realizations > 1) {
double std_dev = sum_sq/n_realizations - mean*mean;
std_dev = sum_sq/n_realizations - mean*mean;
std_dev = std::sqrt(std_dev / (n_realizations - 1));
std_dev_vec.push_back(std_dev);
} else {
std_dev_vec.push_back(0.0);
}
std_dev_vec.push_back(std_dev);
}
// generate a name for the value
std::string nuclide_name = "total"; // start with total by default
if (tally->nuclides_[i_nuc] > -1) {
nuclide_name = data::nuclides[tally->nuclides_[i_nuc]]->name_;
}
std::string score_name = tally->score_name(i_score);
auto score_str = fmt::format("{}_{}", score_name, nuclide_name);
tally_scores.push_back(score_str);
// set the data for this score
umesh->set_score_data(score_str, mean_vec, std_dev_vec);
}
@ -858,18 +846,18 @@ void write_unstructured_mesh_results() {
// Generate a file name based on the tally id
// and the current batch number
int w = std::to_string(settings::n_max_batches).size();
size_t batch_width {std::to_string(settings::n_max_batches).size()};
std::string filename = fmt::format("tally_{0}.{1:0{2}}",
tally->id_,
simulation::current_batch,
w);
batch_width);
if (umesh->library() == "moab" && !mpi::master) continue;
// Write the unstructured mesh and data to file
umesh->write(filename);
for (const auto& score : tally_scores) { umesh->remove_score(score); }
umesh->remove_scores();
}
}
#endif