Put library and library_map in data namespace

This commit is contained in:
Paul Romano 2018-11-06 10:11:48 -06:00
parent de666e3291
commit 7d6ab8d72d
3 changed files with 36 additions and 20 deletions

View file

@ -30,24 +30,36 @@ public:
}
// Data members
Type type_;
std::vector<std::string> materials_;
std::string path_;
Type type_; //!< Type of data library
std::vector<std::string> materials_; //!< Materials contained in library
std::string path_; //!< File path to library
};
using LibraryKey = std::pair<Library::Type, std::string>;
//==============================================================================
// Global variable declarations
//==============================================================================
namespace data {
//!< Data libraries
extern std::vector<Library> libraries;
using LibraryKey = std::pair<Library::Type, std::string>;
extern std::map<LibraryKey, std::size_t> library_dict;
//! Maps (type, name) to index in libraries
extern std::map<LibraryKey, std::size_t> library_map;
} // namespace data
//==============================================================================
// Non-member functions
//==============================================================================
//! Read cross sections file (either XML or multigroup H5) and populate data
//! libraries
extern "C" void read_cross_sections_xml();
//! Read cross_sections.xml and populate data libraries
void read_ce_cross_sections_xml();
} // namespace openmc

View file

@ -19,10 +19,12 @@ namespace openmc {
// Global variable declarations
//==============================================================================
std::vector<Library> libraries;
std::map<LibraryKey, std::size_t> library_dict;
namespace data {
extern "C" void read_mg_cross_sections_header();
std::vector<Library> libraries;
std::map<LibraryKey, std::size_t> library_map;
}
//==============================================================================
// Library methods
@ -76,6 +78,8 @@ Library::Library(pugi::xml_node node, const std::string& directory)
// Non-member functions
//==============================================================================
extern "C" void read_mg_cross_sections_header();
void read_cross_sections_xml()
{
// Check if materials.xml exists
@ -146,12 +150,12 @@ void read_cross_sections_xml()
// Establish mapping between (type, material) and index in libraries
int i = 0;
for (const auto& lib : libraries) {
for (const auto& lib : data::libraries) {
for (const auto& name : lib.materials_) {
std::string lower_name = name;
to_lower(lower_name);
LibraryKey key {lib.type_, lower_name};
library_dict.insert({key, i});
data::library_map.insert({key, i});
}
++i;
}
@ -161,7 +165,7 @@ void read_cross_sections_xml()
std::string lower_name = name;
to_lower(lower_name);
LibraryKey key {Library::Type::neutron, lower_name};
if (library_dict.find(key) == library_dict.end()) {
if (data::library_map.find(key) == data::library_map.end()) {
fatal_error("Could not find resonant scatterer " +
name + " in cross_sections.xml file!");
}
@ -200,11 +204,11 @@ void read_ce_cross_sections_xml()
}
for (const auto& node_library : root.children("library")) {
libraries.emplace_back(node_library, directory);
data::libraries.emplace_back(node_library, directory);
}
// Make sure file was not empty
if (libraries.empty()) {
if (data::libraries.empty()) {
fatal_error("No cross section libraries present in cross_sections.xml file.");
}
}
@ -214,25 +218,25 @@ void read_ce_cross_sections_xml()
//==============================================================================
extern "C" void library_clear() {
libraries.clear();
library_dict.clear();
data::libraries.clear();
data::library_map.clear();
}
extern "C" const char* library_path(int type, const char* name) {
auto lib_type = static_cast<Library::Type>(type);
LibraryKey key {lib_type, name};
if (library_dict.find(key) == library_dict.end()) {
if (data::library_map.find(key) == data::library_map.end()) {
return nullptr;
} else {
auto idx = library_dict[key];
return libraries[idx].path_.c_str();
auto idx = data::library_map[key];
return data::libraries[idx].path_.c_str();
}
}
extern "C" bool library_present(int type, const char* name) {
auto lib_type = static_cast<Library::Type>(type);
LibraryKey key {lib_type, name};
return library_dict.find(key) != library_dict.end();
return data::library_map.find(key) != data::library_map.end();
}
} // namespace openmc

View file

@ -122,7 +122,7 @@ void read_mg_cross_sections_header_c(hid_t file_id)
Library lib {};
lib.type_ = Library::Type::neutron;
lib.materials_.push_back(name);
libraries.push_back(lib);
data::libraries.push_back(lib);
}
}