QMCPACK
test_hdf_reshape.cpp File Reference
+ Include dependency graph for test_hdf_reshape.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("hdf_write_reshape_with_matrix", "[hdf]")
 

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "hdf_write_reshape_with_matrix"  ,
""  [hdf] 
)

Definition at line 26 of file test_hdf_reshape.cpp.

References qmcplusplus::CHECK(), hdf_archive::close(), hdf_archive::create(), qmcplusplus::Units::distance::m, qmcplusplus::okay, hdf_archive::open(), hdf_archive::read(), hdf_archive::readSlabReshaped(), hdf_archive::readSlabSelection(), qmcplusplus::REQUIRE(), Vector< T, Alloc >::size(), and hdf_archive::writeSlabReshaped().

27 {
28  hdf_archive hd;
29  bool okay = hd.create("test_write_matrix_reshape.hdf");
30  REQUIRE(okay);
31 
32  Vector<double> v(6);
33  v[0] = 0.0;
34  v[1] = 0.1;
35  v[2] = 0.2;
36  v[3] = 1.0;
37  v[4] = 1.1;
38  v[5] = 1.2;
39 
40  std::array<int, 2> shape{2, 3};
41  hd.writeSlabReshaped(v, shape, "matrix_from_vector");
42 
43  std::vector<std::complex<float>> v_cplx(6);
44  v_cplx[0] = {0.0, 0.2};
45  v_cplx[1] = {0.1, 0.3};
46  v_cplx[2] = {0.2, 0.4};
47  v_cplx[3] = {1.0, 1.2};
48  v_cplx[4] = {1.1, 1.3};
49  v_cplx[5] = {1.2, 1.4};
50  hd.writeSlabReshaped(v_cplx, shape, "matrix_from_vector_cplx");
51 
52  hd.close();
53 
54  hdf_archive hd2;
55  hd2.open("test_write_matrix_reshape.hdf");
56 
57  Matrix<double> m; // resized by read
58  hd2.read(m, "matrix_from_vector");
59 
60  REQUIRE(m.rows() == 2);
61  REQUIRE(m.cols() == 3);
62  CHECK(m(0, 0) == Approx(v[0]));
63  CHECK(m(0, 1) == Approx(v[1]));
64  CHECK(m(0, 2) == Approx(v[2]));
65  CHECK(m(1, 0) == Approx(v[3]));
66  CHECK(m(1, 1) == Approx(v[4]));
67  CHECK(m(1, 2) == Approx(v[5]));
68 
69  std::vector<double> vec; // reshaped and resized by read
70  hd2.readSlabReshaped(vec, shape, "matrix_from_vector");
71 
72  REQUIRE(vec.size() == 6);
73  CHECK(vec[0] == Approx(v[0]));
74  CHECK(vec[1] == Approx(v[1]));
75  CHECK(vec[2] == Approx(v[2]));
76  CHECK(vec[3] == Approx(v[3]));
77  CHECK(vec[4] == Approx(v[4]));
78  CHECK(vec[5] == Approx(v[5]));
79 
80  // using hyperslab selection
81  Vector<std::complex<float>> vec_cplx; // reshaped and resized by read
82  std::array<int, 2> spec{-1, -1};
83  hd2.readSlabSelection(vec_cplx, spec, "matrix_from_vector_cplx");
84 
85  REQUIRE(vec_cplx.size() == 6);
86  CHECK(vec_cplx[0] == ComplexApprox(v_cplx[0]));
87  CHECK(vec_cplx[1] == ComplexApprox(v_cplx[1]));
88  CHECK(vec_cplx[2] == ComplexApprox(v_cplx[2]));
89  CHECK(vec_cplx[3] == ComplexApprox(v_cplx[3]));
90  CHECK(vec_cplx[4] == ComplexApprox(v_cplx[4]));
91  CHECK(vec_cplx[5] == ComplexApprox(v_cplx[5]));
92 }
bool open(const std::filesystem::path &fname, unsigned flags=H5F_ACC_RDWR)
open a file
void close()
close all the open groups and file
Definition: hdf_archive.cpp:38
class to handle hdf file
Definition: hdf_archive.h:51
void readSlabReshaped(T &data, const std::array< IT, RANK > &shape, const std::string &aname)
read file dataset with a specific shape into a container and check status
Definition: hdf_archive.h:321
void readSlabSelection(T &data, const std::array< IT, RANK > &readSpec, const std::string &aname)
read a portion of the data from the group aname and check status runtime error is issued on I/O error...
Definition: hdf_archive.h:345
size_type size() const
return the current size
Definition: OhmmsVector.h:162
REQUIRE(std::filesystem::exists(filename))
void writeSlabReshaped(T &data, const std::array< IT, RANK > &shape, const std::string &aname)
write the container data with a specific shape and check status
Definition: hdf_archive.h:274
bool create(const std::filesystem::path &fname, unsigned flags=H5F_ACC_TRUNC)
create a file
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void read(T &data, const std::string &aname)
read the data from the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:306