Skip to content

Commit

Permalink
[OneD] Check groups and data size in HDF import file
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jan 12, 2023
1 parent 6550238 commit f51fb65
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions src/oneD/Sim1D.cpp
Expand Up @@ -18,6 +18,14 @@

using namespace std;

#if CT_USE_HIGHFIVE_HDF
#include <highfive/H5File.hpp>
#include <highfive/H5Group.hpp>
#include <highfive/H5DataSet.hpp>

namespace h5 = HighFive;
#endif

namespace Cantera
{

Expand Down Expand Up @@ -161,34 +169,62 @@ void Sim1D::restore(const std::string& fname, const std::string& id,
"Restoring from XML is no longer supported.");
} else if (extension == "h5" || extension == "hdf") {
#if CT_USE_HIGHFIVE_HDF
throw CanteraError("Sim1D::restore",
"Not yet implemented.");
h5::File file(fname, h5::File::ReadOnly);
if (!file.exist(id) || file.getObjectType(id) != h5::ObjectType::Group) {
throw CanteraError("Sim1D::restore",
"No solution with id '{}'", id);
}
h5::Group grp = file.getGroup(id);
for (auto dom : m_dom) {
std::string dn = dom->id();
if (!grp.exist(dn)|| grp.getObjectType(dn) != h5::ObjectType::Group) {
throw CanteraError("Sim1D::restore",
"Saved state '{}' does not contain a domain named '{}'.", id, dn);
}
size_t points;
try {
// determine size based on stored temperature
points = grp.getGroup(dn).getDataSet("T").getElementCount();
} catch (exception& err) {
throw CanteraError("Sim1D::restore",
"Unable to determine domain size:\n{}", err.what());
}
dom->resize(dom->nComponents(), points);
}
resize();
m_xlast_ts.clear();

throw CanteraError("Sim1D::restore", "Work in progress.");
#else
throw CanteraError("Sim1D::restore",
"Restoring from HDF requires HighFive installation.");
#endif
}
AnyMap root = AnyMap::fromYamlFile(fname);
if (!root.hasKey(id)) {
throw InputFileError("Sim1D::restore", root,
"No solution with id '{}'", id);
}
const auto& state = root[id];
for (auto dom : m_dom) {
if (!state.hasKey(dom->id())) {
throw InputFileError("Sim1D::restore", state,
"Saved state '{}' does not contain a domain named '{}'.",
id, dom->id());
} else if (extension == "yaml" || extension == "yml") {
AnyMap root = AnyMap::fromYamlFile(fname);
if (!root.hasKey(id)) {
throw InputFileError("Sim1D::restore", root,
"No solution with id '{}'", id);
}
dom->resize(dom->nComponents(), state[dom->id()]["points"].asInt());
}
resize();
m_xlast_ts.clear();
for (auto dom : m_dom) {
dom->restore(state[dom->id()].as<AnyMap>(), m_x.data() + dom->loc(),
loglevel);
const auto& state = root[id];
for (auto dom : m_dom) {
if (!state.hasKey(dom->id())) {
throw InputFileError("Sim1D::restore", state,
"Saved state '{}' does not contain a domain named '{}'.",
id, dom->id());
}
dom->resize(dom->nComponents(), state[dom->id()]["points"].asInt());
}
resize();
m_xlast_ts.clear();
for (auto dom : m_dom) {
dom->restore(state[dom->id()].as<AnyMap>(), m_x.data() + dom->loc(),
loglevel);
}
finalize();
} else {
throw CanteraError("Sim1D::restore",
"Unknown file extension '{}'", extension);
}
finalize();
}

void Sim1D::setFlatProfile(size_t dom, size_t comp, doublereal v)
Expand Down

0 comments on commit f51fb65

Please sign in to comment.