diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index c28b7f0..2f40b95 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -114,7 +114,7 @@ auto main(int argc, char** argv) -> int { auto const sim = pffdtd::loadSimulation2D(simDir); auto const out = engine(sim); - auto results = pffdtd::H5FWriter{simDir / args.sim2d.out}; + auto results = pffdtd::HDF5Writer{simDir / args.sim2d.out}; results.write("out", out); auto const stop = pffdtd::getTime(); diff --git a/src/cpp/pffdtd/hdf.hpp b/src/cpp/pffdtd/hdf.hpp index caaeb55..9811e14 100644 --- a/src/cpp/pffdtd/hdf.hpp +++ b/src/cpp/pffdtd/hdf.hpp @@ -32,11 +32,16 @@ inline constexpr auto isStdVector = false; template inline constexpr auto isStdVector> = true; -struct H5FReader { - explicit H5FReader(std::filesystem::path const& path) - : _handle{H5Fopen(path.string().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT)} {} +struct HDF5Reader { + explicit HDF5Reader(std::filesystem::path const& path) + : _handle{[&] { + if (not std::filesystem::exists(path)) { + raisef("file '{}' does not exist", path.string()); + } + return H5Fopen(path.string().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); + }()} {} - ~H5FReader() { H5Fclose(_handle); } + ~HDF5Reader() { H5Fclose(_handle); } [[nodiscard]] auto handle() const noexcept -> hid_t { return _handle; } @@ -131,11 +136,11 @@ struct H5FReader { hid_t _handle; }; -struct H5FWriter { - explicit H5FWriter(std::filesystem::path const& path) +struct HDF5Writer { + explicit HDF5Writer(std::filesystem::path const& path) : _handle{H5Fcreate(path.string().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)} {} - ~H5FWriter() { H5Fclose(_handle); } + ~HDF5Writer() { H5Fclose(_handle); } auto write(char const* name, stdex::mdspan> buf) -> void { hsize_t dims[2]{ @@ -170,7 +175,7 @@ struct H5FWriter { }; template -[[nodiscard]] auto read(H5FReader& reader, char const* dset_str, int ndims, hsize_t* dims) -> std::unique_ptr { +[[nodiscard]] auto read(HDF5Reader& reader, char const* dset_str, int ndims, hsize_t* dims) -> std::unique_ptr { auto dset = H5Dopen(reader.handle(), dset_str, H5P_DEFAULT); auto dspace = H5Dget_space(dset); PFFDTD_ASSERT(H5Sget_simple_extent_ndims(dspace) == ndims); diff --git a/src/cpp/pffdtd/simulation_2d.cpp b/src/cpp/pffdtd/simulation_2d.cpp index 1953c0e..f9a19a7 100644 --- a/src/cpp/pffdtd/simulation_2d.cpp +++ b/src/cpp/pffdtd/simulation_2d.cpp @@ -10,7 +10,7 @@ namespace pffdtd { auto loadSimulation2D(std::filesystem::path const& dir) -> Simulation2D { - auto sim = H5FReader{dir / "sim.h5"}; + auto sim = HDF5Reader{dir / "sim.h5"}; auto const Nx = sim.read("Nx"); auto const Ny = sim.read("Ny"); diff --git a/src/cpp/pffdtd/simulation_3d.cpp b/src/cpp/pffdtd/simulation_3d.cpp index 241b4b0..b739320 100644 --- a/src/cpp/pffdtd/simulation_3d.cpp +++ b/src/cpp/pffdtd/simulation_3d.cpp @@ -90,12 +90,7 @@ template //////////////////////////////////////////////////////////////////////// // Read constants HDF5 dataset //////////////////////////////////////////////////////////////////////// - auto filename = simDir / "constants.h5"; - if (not std::filesystem::exists(filename)) { - raisef("file '{}' does not exist", filename.string()); - } - - auto constants = H5FReader{filename}; + auto constants = HDF5Reader{simDir / "constants.h5"}; ////////////////// // constants @@ -147,12 +142,7 @@ template //////////////////////////////////////////////////////////////////////// // Read vox HDF5 dataset //////////////////////////////////////////////////////////////////////// - filename = simDir / "vox_out.h5"; - if (not std::filesystem::exists(filename)) { - raisef("file '{}' does not exist", filename.string()); - } - - auto vox_out = H5FReader{filename}; + auto vox_out = HDF5Reader{simDir / "vox_out.h5"}; ////////////////// // integers @@ -206,12 +196,7 @@ template //////////////////////////////////////////////////////////////////////// // Read signals HDF5 dataset //////////////////////////////////////////////////////////////////////// - filename = simDir / "signals.h5"; - if (not std::filesystem::exists(filename)) { - raisef("file '{}' does not exist", filename.string()); - } - - auto signals = H5FReader{filename}; + auto signals = HDF5Reader{simDir / "signals.h5"}; ////////////////// // integers @@ -254,12 +239,7 @@ template //////////////////////////////////////////////////////////////////////// // Read materials HDF5 dataset //////////////////////////////////////////////////////////////////////// - filename = simDir / "materials.h5"; - if (not std::filesystem::exists(filename)) { - raisef("file '{}' does not exist", filename.string()); - } - - auto materials = H5FReader{filename}; + auto materials = HDF5Reader{simDir / "materials.h5"}; ////////////////// // integers @@ -539,7 +519,7 @@ void writeOutputs_impl(Simulation3D const& sim, std::filesystem::path cons } } - auto writer = H5FWriter{simDir / "sim_outs.h5"}; + auto writer = HDF5Writer{simDir / "sim_outs.h5"}; writer.write("u_out", u_out); std::puts("wrote output dataset"); }