QMCPACK
test_hdf_reshape.cpp
Go to the documentation of this file.
1 
2 //////////////////////////////////////////////////////////////////////////////////////
3 // This file is distributed under the University of Illinois/NCSA Open Source License.
4 // See LICENSE file in top directory for details.
5 //
6 // Copyright (c) 2018 Jeongnim Kim and QMCPACK developers.
7 //
8 // File developed by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
9 //
10 // File created by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 #include <iostream>
16 #include <vector>
17 #include <complex>
18 #include "OhmmsPETE/OhmmsVector.h"
19 #include "OhmmsPETE/OhmmsMatrix.h"
20 #include "OhmmsPETE/OhmmsArray.h"
22 #include "hdf/hdf_archive.h"
23 
24 using namespace qmcplusplus;
25 
26 TEST_CASE("hdf_write_reshape_with_matrix", "[hdf]")
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
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
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
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
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