diff --git a/sesame2spiner/CMakeLists.txt b/sesame2spiner/CMakeLists.txt index fd211ab86a..5eccca3ecf 100644 --- a/sesame2spiner/CMakeLists.txt +++ b/sesame2spiner/CMakeLists.txt @@ -12,27 +12,32 @@ # publicly and display publicly, and to permit others to do so. #------------------------------------------------------------------------------ -add_executable(sesame2spiner - io_eospac.cpp - io_eospac.hpp - - generate_files.cpp - generate_files.hpp - - parse_cli.cpp - parse_cli.hpp - - parser.cpp - parser.hpp - - main.cpp - ) - -target_include_directories(sesame2spiner PUBLIC +add_library(sesame2spiner-lib + io_eospac.cpp + io_eospac.hpp + + generate_files.cpp + generate_files.hpp + + parse_cli.cpp + parse_cli.hpp + + parser.cpp + parser.hpp + ) + +target_include_directories(sesame2spiner-lib PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ) -target_link_libraries(sesame2spiner singularity-eos::libs singularity-eos::flags) +target_link_libraries(sesame2spiner-lib + singularity-eos::libs singularity-eos::flags) + +add_executable(sesame2spiner + main.cpp + ) -# TODO: Add tests for sesame2spiner here. +target_link_libraries(sesame2spiner + sesame2spiner-lib + singularity-eos::libs singularity-eos::flags) diff --git a/sesame2spiner/generate_files.cpp b/sesame2spiner/generate_files.cpp index e809472ca6..90f6eb861a 100644 --- a/sesame2spiner/generate_files.cpp +++ b/sesame2spiner/generate_files.cpp @@ -151,11 +151,6 @@ herr_t saveAllMaterials(const std::string &savename, Verbosity eospacWarn) { std::vector params; std::vector matids; - std::unordered_map used_names; - std::unordered_set used_matids; - SesameMetadata metadata; - hid_t file; - herr_t status = H5_SUCCESS; for (auto const &filename : filenames) { Params p(filename); @@ -169,61 +164,7 @@ herr_t saveAllMaterials(const std::string &savename, params.push_back(p); } - std::cout << "Saving to file " << savename << std::endl; - file = H5Fcreate(savename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); - - std::cout << "Processing " << matids.size() << " materials..." << std::endl; - - for (size_t i = 0; i < matids.size(); i++) { - int matid = matids[i]; - if (used_matids.count(matid) > 0) { - std::cerr << "...Duplicate matid " << matid << " detected. Skipping." << std::endl; - continue; - } - used_matids.insert(matid); - - std::cout << "..." << matid << std::endl; - - eosGetMetadata(matid, metadata, Verbosity::Debug); - if (printMetadata) std::cout << metadata << std::endl; - - std::string name = params[i].Get("name", metadata.name); - if (name == "-1" || name == "") { - std::string new_name = "material_" + std::to_string(i); - std::cerr << "...WARNING: no reasonable name found. " - << "Using a default name: " << new_name << std::endl; - name = new_name; - } - if (used_names.count(name) > 0) { - used_names[name] += 1; - std::string new_name = name + "_" + std::to_string(used_names[name]); - std::cerr << "...WARNING: Name " << name << " already used. " - << "Using name: " << new_name << std::endl; - name = new_name; - } else { - used_names[name] = 1; - } - - Bounds lRhoBounds, lTBounds, leBounds; - getMatBounds(i, matid, metadata, params[i], lRhoBounds, lTBounds, leBounds); - - if (eospacWarn == Verbosity::Debug) { - std::cout << "bounds for log(rho), log(T), log(sie) are:\n" - << lRhoBounds << lTBounds << leBounds << std::endl; - } - - status += - saveMaterial(file, metadata, lRhoBounds, lTBounds, leBounds, name, eospacWarn); - if (status != H5_SUCCESS) { - std::cerr << "WARNING: problem with HDf5" << std::endl; - } - } - - std::cout << "Cleaning up." << std::endl; - status += H5Fclose(file); - if (status != H5_SUCCESS) { - std::cerr << "WARNING: problem with HDf5" << std::endl; - } + herr_t status = saveAllMaterials(savename, matids, params, printMetadata, eospacWarn); return status; } @@ -305,6 +246,74 @@ void getMatBounds(int i, int matid, const SesameMetadata &metadata, const Params return; } +herr_t saveAllMaterials(const std::string &savename, const std::vector matids, + const std::vector ¶ms, bool printMetadata, + Verbosity eospacWarn) { + std::unordered_map used_names; + std::unordered_set used_matids; + SesameMetadata metadata; + hid_t file; + herr_t status = H5_SUCCESS; + + std::cout << "Saving to file " << savename << std::endl; + file = H5Fcreate(savename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + std::cout << "Processing " << matids.size() << " materials..." << std::endl; + + for (size_t i = 0; i < matids.size(); i++) { + int matid = matids[i]; + if (used_matids.count(matid) > 0) { + std::cerr << "...Duplicate matid " << matid << " detected. Skipping." << std::endl; + continue; + } + used_matids.insert(matid); + + std::cout << "..." << matid << std::endl; + + eosGetMetadata(matid, metadata, Verbosity::Debug); + if (printMetadata) std::cout << metadata << std::endl; + + std::string name = params[i].Get("name", metadata.name); + if (name == "-1" || name == "") { + std::string new_name = "material_" + std::to_string(i); + std::cerr << "...WARNING: no reasonable name found. " + << "Using a default name: " << new_name << std::endl; + name = new_name; + } + if (used_names.count(name) > 0) { + used_names[name] += 1; + std::string new_name = name + "_" + std::to_string(used_names[name]); + std::cerr << "...WARNING: Name " << name << " already used. " + << "Using name: " << new_name << std::endl; + name = new_name; + } else { + used_names[name] = 1; + } + + Bounds lRhoBounds, lTBounds, leBounds; + getMatBounds(i, matid, metadata, params[i], lRhoBounds, lTBounds, leBounds); + + if (eospacWarn == Verbosity::Debug) { + std::cout << "bounds for log(rho), log(T), log(sie) are:\n" + << lRhoBounds << lTBounds << leBounds << std::endl; + } + + status += + saveMaterial(file, metadata, lRhoBounds, lTBounds, leBounds, name, eospacWarn); + if (status != H5_SUCCESS) { + std::cerr << "WARNING: problem with HDf5" << std::endl; + } + } + + std::cout << "Cleaning up." << std::endl; + status += H5Fclose(file); + if (status != H5_SUCCESS) { + std::cerr << "WARNING: problem with HDf5" << std::endl; + } + + return status; +} + bool checkValInMatBounds(int matid, const std::string &name, Real val, Real vmin, Real vmax) { if (val < vmin || val > vmax) { diff --git a/sesame2spiner/generate_files.hpp b/sesame2spiner/generate_files.hpp index 5659e867dc..98aa43ee0d 100644 --- a/sesame2spiner/generate_files.hpp +++ b/sesame2spiner/generate_files.hpp @@ -41,6 +41,12 @@ herr_t saveAllMaterials(const std::string &savename, const std::vector &filenames, bool printMetadata, Verbosity eospacWarn); +herr_t saveAllMaterials(const std::string &savename, + const std::vector matids, + const std::vector ¶ms, + bool printMetadata = false, + Verbosity eospacWarn = Verbosity::Quiet); + void getMatBounds(int i, int matid, const SesameMetadata &metadata, const Params ¶ms, Bounds &lRhoBounds, Bounds &lTBounds, Bounds &leBounds); diff --git a/sesame2spiner/parser.hpp b/sesame2spiner/parser.hpp index 88e507f838..d68a3e6d36 100644 --- a/sesame2spiner/parser.hpp +++ b/sesame2spiner/parser.hpp @@ -34,6 +34,10 @@ class Params { T Get(const std::string &key, T default_value) const { return Contains(key) ? Get(key) : default_value; } + template + void Add(const std::string &key, const T &val) { + params[key] = std::to_string(val); + } private: void Parse(std::istream &s);