Refactoring all of the openmc namespace functions into the DAGUniverse class.

This commit is contained in:
Patrick Shriwise 2021-03-15 14:28:44 -05:00
parent 4819e02f22
commit da6a369d1f
2 changed files with 76 additions and 67 deletions

View file

@ -26,12 +26,23 @@ public:
void initialize(); //!< Sets up the DAGMC instance and OpenMC internals
std::shared_ptr<UWUW>
read_uwuw_materials(); //!< Reads UWUW materials and returns an ID map
void read_uwuw_materials(); //!< Reads UWUW materials and returns an ID map
bool uses_uwuw() const;
std::string get_uwuw_materials_xml() const;
void write_uwuw_materials_xml(const std::string& outfile = "uwuw_materials.xml") const;
void legacy_assign_material(std::string mat_string,
std::unique_ptr<DAGCell>& c) const;
std::string dagmc_ids_for_dim(int dim) const;
// Data Members
std::string filename_;
std::shared_ptr<moab::DagMC> dagmc_instance_; //! DAGMC Instance for this universe
std::shared_ptr<UWUW> uwuw_;
int32_t cell_idx_offset_;
int32_t surf_idx_offset_;
bool adjust_geometry_ids_;

View file

@ -47,14 +47,14 @@ std::string dagmc_file() {
return filename;
}
std::string dagmc_ids_for_dim(std::shared_ptr<moab::DagMC>& dagmc_instance,
int dim)
std::string
DAGUniverse::dagmc_ids_for_dim(int dim) const
{
// generate a vector of ids
std::vector<int> id_vec;
int n_cells = dagmc_instance->num_entities(dim);
int n_cells = dagmc_instance_->num_entities(dim);
for (int i = 1; i <= n_cells; i++) {
id_vec.push_back(dagmc_instance->id_by_index(dim, i));
id_vec.push_back(dagmc_instance_->id_by_index(dim, i));
}
// sort the vector of ids
@ -88,55 +88,50 @@ std::string dagmc_ids_for_dim(std::shared_ptr<moab::DagMC>& dagmc_instance,
return out.str();
}
bool get_uwuw_materials_xml(std::string& s) {
std::string filename = dagmc_file();
UWUW uwuw(filename.c_str());
bool
DAGUniverse::uses_uwuw() const
{
return uwuw_ && !uwuw_->material_library.empty();
}
std::string
DAGUniverse::get_uwuw_materials_xml() const
{
if (!uses_uwuw()) {
throw std::runtime_error("This DAGMC Universe does not use UWUW materials");
}
std::stringstream ss;
bool uwuw_mats_present = false;
if (uwuw.material_library.size() != 0) {
uwuw_mats_present = true;
// write header
ss << "<?xml version=\"1.0\"?>\n";
ss << "<materials>\n";
const auto& mat_lib = uwuw.material_library;
// write materials
for (auto mat : mat_lib) { ss << mat.second->openmc("atom"); }
// write footer
ss << "</materials>";
s = ss.str();
}
// write header
ss << "<?xml version=\"1.0\"?>\n";
ss << "<materials>\n";
const auto& mat_lib = uwuw_->material_library;
// write materials
for (auto mat : mat_lib) { ss << mat.second->openmc("atom"); }
// write footer
ss << "</materials>";
return uwuw_mats_present;
return ss.str();
}
bool read_uwuw_materials(pugi::xml_document& doc) {
std::string s;
bool found_uwuw_mats = get_uwuw_materials_xml(s);
if (found_uwuw_mats) {
pugi::xml_parse_result result = doc.load_string(s.c_str());
if (!result) {
throw std::runtime_error{"Error reading UWUW materials"};
}
}
return found_uwuw_mats;
}
bool write_uwuw_materials_xml() {
std::string s;
bool found_uwuw_mats = get_uwuw_materials_xml(s);
// if there is a material library in the file
if (found_uwuw_mats) {
// write a material.xml file
std::ofstream mats_xml("materials.xml");
mats_xml << s;
mats_xml.close();
void
DAGUniverse::write_uwuw_materials_xml(const std::string& outfile) const
{
if (!uses_uwuw()) {
throw std::runtime_error("This DAGMC universe does not use UWUW materials.");
}
return found_uwuw_mats;
std::string xml_str = get_uwuw_materials_xml();
// if there is a material library in the file
std::ofstream mats_xml(outfile);
mats_xml << xml_str;
mats_xml.close();
}
void legacy_assign_material(std::string mat_string, DAGCell* c)
void
DAGUniverse::legacy_assign_material(std::string mat_string,
std::unique_ptr<DAGCell>& c) const
{
bool mat_found_by_name = false;
// attempt to find a material with a matching name
@ -152,7 +147,7 @@ void legacy_assign_material(std::string mat_string, DAGCell* c)
// report error if more than one material is found
} else {
fatal_error(fmt::format(
"More than one material found with name {}. Please ensure materials "
"More than one material found with name '{}'. Please ensure materials "
"have unique names if using this property to assign materials.",
mat_string));
}
@ -166,7 +161,7 @@ void legacy_assign_material(std::string mat_string, DAGCell* c)
c->material_.emplace_back(id);
} catch (const std::invalid_argument&) {
fatal_error(fmt::format(
"No material {} found for volume (cell) {}", mat_string, c->id_));
"No material '{}' found for volume (cell) {}", mat_string, c->id_));
}
}
@ -256,10 +251,10 @@ void DAGUniverse::initialize() {
// --- Materials ---
// read any UWUW materials from the file
std::shared_ptr<UWUW> uwuw = read_uwuw_materials();
read_uwuw_materials();
// check for uwuw material definitions
bool using_uwuw = !uwuw->material_library.empty();
bool using_uwuw = uses_uwuw();
// notify user if UWUW materials are going to be used
if (using_uwuw) {
@ -293,7 +288,7 @@ void DAGUniverse::initialize() {
moab::EntityHandle vol_handle = dagmc_instance_->entity_by_index(3, i + 1);
// set cell ids using global IDs
DAGCell* c = new DAGCell();
auto c = std::make_unique<DAGCell>();
c->dag_index_ = i + 1;
c->id_ = adjust_geometry_ids_ ? next_cell_id++ : dagmc_instance_->id_by_index(3, c->dag_index_);
c->dagmc_ptr_ = dagmc_instance_;
@ -304,13 +299,11 @@ void DAGUniverse::initialize() {
if (in_map == model::cell_map.end()) {
model::cell_map[c->id_] = model::cells.size();
} else {
warning(fmt::format("DAGMC Cell IDs: {}", dagmc_ids_for_dim(dagmc_instance_, 3)));
warning(fmt::format("DAGMC Cell IDs: {}", dagmc_ids_for_dim(3)));
fatal_error(fmt::format("Cell ID {} exists in both DAGMC Universe {} "
"and the CSG geometry.", c->id_, this->id_));
}
model::cells.emplace_back(c);
// MATERIALS
// determine volume material assignment
@ -333,12 +326,12 @@ void DAGUniverse::initialize() {
if (using_uwuw) {
// lookup material in uwuw if present
std::string uwuw_mat = DMD.volume_material_property_data_eh[vol_handle];
if (uwuw->material_library.count(uwuw_mat) != 0) {
if (uwuw_->material_library.count(uwuw_mat) != 0) {
// Note: material numbers are set by UWUW
int mat_number = uwuw->material_library.get_material(uwuw_mat).metadata["mat_number"].asInt();
int mat_number = uwuw_->material_library.get_material(uwuw_mat).metadata["mat_number"].asInt();
c->material_.push_back(mat_number);
} else {
fatal_error(fmt::format("Material with value {} not found in the "
fatal_error(fmt::format("Material with value '{}' not found in the "
"UWUW material library", mat_str));
}
} else {
@ -350,7 +343,10 @@ void DAGUniverse::initialize() {
std::string temp_value;
// no temperature if void
if (c->material_[0] == MATERIAL_VOID) continue;
if (c->material_[0] == MATERIAL_VOID) {
model::cells.emplace_back(std::move(c));
continue;
}
// assign cell temperature
const auto& mat = model::materials[model::material_map.at(c->material_[0])];
@ -364,6 +360,8 @@ void DAGUniverse::initialize() {
} else {
c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * settings::temperature_default));
}
model::cells.emplace_back(std::move(c));
}
// allocate the cell overlap count if necessary
@ -384,7 +382,7 @@ void DAGUniverse::initialize() {
moab::EntityHandle surf_handle = dagmc_instance_->entity_by_index(2, i+1);
// set cell ids using global IDs
DAGSurface* s = new DAGSurface();
auto s = std::make_unique<DAGSurface>();
s->dag_index_ = i+1;
s->id_ = adjust_geometry_ids_ ? next_surf_id++ : dagmc_instance_->id_by_index(2, i+1);
s->dagmc_ptr_ = dagmc_instance_;
@ -422,16 +420,17 @@ void DAGUniverse::initialize() {
if (in_map == model::surface_map.end()) {
model::surface_map[s->id_] = model::surfaces.size();
} else {
warning(fmt::format("DAGMC Surface IDs: {}", dagmc_ids_for_dim(dagmc_instance_, 2)));
warning(fmt::format("DAGMC Surface IDs: {}", dagmc_ids_for_dim(2)));
fatal_error(fmt::format("Surface ID {} exists in both Universe {} "
"and the CSG geometry.", s->id_, this->id_));
}
model::surfaces.emplace_back(s);
model::surfaces.emplace_back(std::move(s));
} // end surface loop
}
std::shared_ptr<UWUW>
void
DAGUniverse::read_uwuw_materials() {
int32_t next_material_id = 0;
@ -440,13 +439,13 @@ DAGUniverse::read_uwuw_materials() {
}
next_material_id++;
auto uwuw = std::make_shared<UWUW>(filename_.c_str());
const auto& mat_lib = uwuw->material_library;
if (mat_lib.size() == 0) return uwuw;
uwuw_ = std::make_shared<UWUW>(filename_.c_str());
const auto& mat_lib = uwuw_->material_library;
if (mat_lib.size() == 0) return;
// if we're using automatic IDs, update the UWUW material metadata
if (adjust_material_ids_) {
for (auto& mat : uwuw->material_library) {
for (auto& mat : uwuw_->material_library) {
mat.second->metadata["mat_number"] = std::to_string(next_material_id++);
}
}
@ -468,7 +467,6 @@ DAGUniverse::read_uwuw_materials() {
for (pugi::xml_node material_node : root.children("material")) {
model::materials.push_back(std::make_unique<Material>(material_node));
}
return uwuw;
}
}