Skip to content

Commit

Permalink
Merge pull request #92 from lanl/jmm/backwards-compatible-grids
Browse files Browse the repository at this point in the history
make spiner piecewise grids backwards compatible
  • Loading branch information
Yurlungur authored Aug 12, 2024
2 parents 131d902 + 01664cb commit 0d0d747
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
48 changes: 31 additions & 17 deletions spiner/piecewise_grid_1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include <utility>
#include <vector>

#ifdef SPINER_USE_HDF
#include "hdf5.h"
#include "hdf5_hl.h"
#include <string>
#endif

#include <ports-of-call/portability.hpp>
#include <ports-of-call/portable_errors.hpp>

Expand Down Expand Up @@ -169,26 +175,34 @@ class PiecewiseGrid1D {
std::is_same<T, double>::value || std::is_same<T, float>::value,
"Spiner HDF5 only defined for these data types: float, double");
herr_t status = 0;
hid_t group = H5Gopen(loc, name.c_str(), H5P_DEFAULT);
int ngrids;
H5LTget_attribute_int(loc, name.c_str(), SP5::H1D::NGRIDS, &ngrids);
NGRIDS_ = ngrids;
PORTABLE_ALWAYS_REQUIRE(
NGRIDS_ <= NGRIDSMAX,
"Total number of grids must be within maximum allowed");
int point_tot = 0;
for (int i = 0; i < NGRIDS_; ++i) {
status += grids_[i].loadHDF(group, gridname_(i).c_str());
pointTotals_[i] = point_tot;
point_tot += grids_[i].nPoints();
if ((i > 0) &&
ratio_(2 * std::abs(grids_[i].min() - grids_[i - 1].max()),
std::abs(grids_[i].min() + grids_[i - 1].max()) >= EPS_())) {
PORTABLE_ALWAYS_THROW_OR_ABORT(
"Grids must be ordered and intersect at exactly one point.");
int is_piecewise_grid =
H5Aexists_by_name(loc, name.c_str(), SP5::H1D::NGRIDS, H5P_DEFAULT);
if (is_piecewise_grid) {
hid_t group = H5Gopen(loc, name.c_str(), H5P_DEFAULT);
H5LTget_attribute_int(loc, name.c_str(), SP5::H1D::NGRIDS, &ngrids);
NGRIDS_ = ngrids;
PORTABLE_ALWAYS_REQUIRE(
NGRIDS_ <= NGRIDSMAX,
"Total number of grids must be within maximum allowed");
int point_tot = 0;
for (int i = 0; i < NGRIDS_; ++i) {
status += grids_[i].loadHDF(group, gridname_(i).c_str());
pointTotals_[i] = point_tot;
point_tot += grids_[i].nPoints();
if ((i > 0) &&
ratio_(2 * std::abs(grids_[i].min() - grids_[i - 1].max()),
std::abs(grids_[i].min() + grids_[i - 1].max()) >= EPS_())) {
PORTABLE_ALWAYS_THROW_OR_ABORT(
"Grids must be ordered and intersect at exactly one point.");
}
}
status += H5Gclose(group);
} else {
NGRIDS_ = 1;
pointTotals_[0] = 0;
status += grids_[0].loadHDF(loc, name);
}
status += H5Gclose(group);
return status;
}
#endif
Expand Down
28 changes: 28 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,34 @@ TEST_CASE("PiecewiseGrid HDF5", "[PiecewiseGrid1D][HDF5]") {
REQUIRE(loaded_grid == piecewise_grid);
}
}
GIVEN("A single regular grid") {
RegularGrid1D g1(0, 0.25, 3);
WHEN("We save it to file") {
const std::string filename = "backwards_compatibility_test.h5";
const std::string grid_name = "grid";
herr_t status;
hid_t file;
file = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT);
status = g1.saveHDF(file, grid_name.c_str());
status += H5Fclose(file);
REQUIRE(status == H5_SUCCESS);

THEN("We can read the file back out with a piecewise grid") {
PiecewiseGrid1D<3> loaded_grid;
herr_t status;
hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
status = loaded_grid.loadHDF(file, grid_name.c_str());
status += H5Fclose(file);
REQUIRE(status == H5_SUCCESS);

REQUIRE(loaded_grid.min() == g1.min());
REQUIRE(loaded_grid.max() == g1.max());
REQUIRE(loaded_grid.nPoints() == g1.nPoints());
REQUIRE(loaded_grid.nGrids() == 1);
}
}
}
}
}

Expand Down

0 comments on commit 0d0d747

Please sign in to comment.