diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index 8494e8578c..29d1a0e4dd 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -356,6 +356,9 @@ intersect_track(const moab::CartVect& start, //! \param[in] all_tets MOAB Range of tetrahedra for the tree void build_kdtree(const moab::Range& all_tets); + std::pair + get_score_tags(std::string score) const; + public: std::pair, std::vector> @@ -388,6 +391,22 @@ public: std::string bin_label(int bin) const override; + //! Get the tags for a score from the mesh instance + moab::ErrorCode get_score_tags(std::string score, + moab::Tag& val_tag, + moab::Tag& err_tag) const; + + //! Add a score to the mesh instance + void add_score(std::string score) const; + + //! Set data for a score + void set_score(const std::string& score, + int bin, + double val, + double err) const; + + //! Write the mesh with any current tally data + void write(std::string base_filename) const; std::string filename_; //tag_get_handle(score.c_str(), + 1, + moab::MB_TYPE_DOUBLE, + score_val, + moab::MB_TAG_DENSE|moab::MB_TAG_CREAT, + &default_val); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Could not create or retrieve the value tag for the score " << score + << " on unstructured mesh " << id_; + warning(msg); + return rval; + } + + moab::Tag score_err; + rval = mbi_->tag_get_handle(score.c_str(), + 1, + moab::MB_TYPE_DOUBLE, + score_err, + moab::MB_TAG_DENSE|moab::MB_TAG_CREAT, + &default_val); + + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Could not create or retrieve the error tag for the score " << score + << " on unstructured mesh " << id_; + warning(msg); + return rval; + } + return moab::MB_SUCCESS; +} + +void +UnstructuredMesh::add_score(std::string score) const { + moab::Tag score_val, score_err; + moab::ErrorCode rval = get_score_tags(score, score_val, score_err); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Failed to add score '" << score << "' to unstructured mesh" << id_; + fatal_error(msg); + } +} + +void +UnstructuredMesh::set_score(const std::string& score, + int bin, + double val, + double err) const { + moab::Tag score_val, score_err; + moab::ErrorCode rval = get_score_tags(score, score_val, score_err); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Failed to get tags for the score '" << score << "' on " + << "unstructured mesh " << id_; + warning(msg); + } + + moab::EntityHandle eh = get_ent_handle_from_bin(bin); + + // set the score value + rval = mbi_->tag_set_data(score_val, &eh, 1, &val); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Failed to set the tally value for score '" << score << "' " + << " on unstructured mesh " << id_; + warning(msg); + } + // set the error value + rval = mbi_->tag_set_data(score_err, &eh, 1, &err); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Failed to set the tally value for score '" << score << "' " + << " on unstructured mesh " << id_; + warning(msg); + } +} + +void +UnstructuredMesh::write(std::string base_filename) const { + // add extension to the base name + base_filename += ".h5m"; + + moab::ErrorCode rval; + rval = mbi_->write_mesh(base_filename.c_str()); + if (rval != moab::MB_SUCCESS) { + std::stringstream msg; + msg << "Failed to write unstructured mesh " << id_; + warning(msg); + } +} + #endif diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index b63128dc04..6a9c1455fd 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -831,6 +831,31 @@ void Tally::accumulate() } } } + +#ifdef DAGMC + for (auto filter_idx : filters_) { + auto& filter = model::tally_filters[filter_idx]; + if (filter->type() == "mesh") { + auto mesh_filter = dynamic_cast(filter.get()); + auto& mesh = model::meshes[mesh_filter->mesh()]; + auto umesh = dynamic_cast(mesh.get()); + if (umesh) { + for (auto score : scores_) { + umesh->add_score(std::to_string(score)); + for (int i = 0; i < results_.shape()[0]; i ++) { + umesh->set_score(std::to_string(score), + i, + results_(i, 0, RESULT_SUM), + results_(i, 0, RESULT_SUM_SQ)); + } + } + std::stringstream output_filename; + output_filename << "tally_" << id_ << "umesh"; + umesh->write(output_filename.str()); + } + } + } +#endif } //==============================================================================