Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: split out sesame2spiner lib #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions sesame2spiner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
129 changes: 69 additions & 60 deletions sesame2spiner/generate_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ herr_t saveAllMaterials(const std::string &savename,
Verbosity eospacWarn) {
std::vector<Params> params;
std::vector<int> matids;
std::unordered_map<std::string, int> used_names;
std::unordered_set<int> used_matids;
SesameMetadata metadata;
hid_t file;
herr_t status = H5_SUCCESS;

for (auto const &filename : filenames) {
Params p(filename);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<int> matids,
const std::vector<Params> &params, bool printMetadata,
Verbosity eospacWarn) {
std::unordered_map<std::string, int> used_names;
std::unordered_set<int> 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) {
Expand Down
6 changes: 6 additions & 0 deletions sesame2spiner/generate_files.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ herr_t saveAllMaterials(const std::string &savename,
const std::vector<std::string> &filenames, bool printMetadata,
Verbosity eospacWarn);

herr_t saveAllMaterials(const std::string &savename,
const std::vector<int> matids,
const std::vector<Params> &params,
bool printMetadata = false,
Verbosity eospacWarn = Verbosity::Quiet);

void getMatBounds(int i, int matid, const SesameMetadata &metadata, const Params &params,
Bounds &lRhoBounds, Bounds &lTBounds, Bounds &leBounds);

Expand Down
4 changes: 4 additions & 0 deletions sesame2spiner/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Params {
T Get(const std::string &key, T default_value) const {
return Contains(key) ? Get<T>(key) : default_value;
}
template<typename T>
void Add(const std::string &key, const T &val) {
params[key] = std::to_string(val);
}

private:
void Parse(std::istream &s);
Expand Down