QMCPACK
test_checkMatrix.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "catch.hpp"
13 #include "checkMatrix.hpp"
14 #include "MatrixAccessor.hpp"
15 #include <iostream>
16 #include <string>
17 #include "OhmmsPETE/OhmmsMatrix.h"
18 
19 /** \file
20  * Tests checkMatrix and MatrixAccessor.
21  * Separate unit tests are likely a waste of effort since these are complementary
22  * and in the same module.
23  */
24 namespace qmcplusplus
25 {
26 // When linking containers does cause asan failure this should return
27 TEST_CASE("checkMatrix_OhmmsMatrix_real", "[utilities][for_testing]")
28 {
29  Matrix<double> a_mat;
30  a_mat.resize(3, 3);
31  a_mat(0, 0) = 2.3;
32  a_mat(0, 1) = 4.5;
33  a_mat(0, 2) = 2.6;
34  a_mat(1, 0) = 0.5;
35  a_mat(1, 1) = 8.5;
36  a_mat(1, 2) = 3.3;
37  a_mat(2, 0) = 1.8;
38  a_mat(2, 1) = 4.4;
39  a_mat(2, 2) = 4.9;
40 
41  Matrix<double> b_mat;
42  b_mat.resize(3, 3);
43  b_mat(0, 0) = 2.3;
44  b_mat(0, 1) = 4.5;
45  b_mat(0, 2) = 2.6;
46  b_mat(1, 0) = 0.5;
47  b_mat(1, 1) = 8.5;
48  b_mat(1, 2) = 3.3;
49  b_mat(2, 0) = 1.8;
50  b_mat(2, 1) = 4.4;
51  b_mat(2, 2) = 4.9;
52 
53  auto check_matrix_result = checkMatrix(a_mat, b_mat);
54  // This would be how you would fail and print the information about what element failed.
55  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
56 
57  //check with epsilon
58  b_mat(0,2) = 2.6005;
59  check_matrix_result = checkMatrix(a_mat, b_mat, false, 1e-4);
60  REQUIRE(check_matrix_result.result == false);
61  check_matrix_result = checkMatrix(a_mat, b_mat, false, 1e-3);
62  REQUIRE(check_matrix_result.result == true);
63 
64  b_mat.resize(4, 4);
65  b_mat(0, 0) = 2.3;
66  b_mat(0, 1) = 4.5;
67  b_mat(0, 2) = 2.6;
68  b_mat(1, 0) = 0.5;
69  b_mat(1, 1) = 8.5;
70  b_mat(1, 2) = 3.3;
71  b_mat(2, 0) = 1.8;
72  b_mat(2, 1) = 4.4;
73  b_mat(2, 2) = 4.9;
74 
75  check_matrix_result = checkMatrix(a_mat, b_mat);
76  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
77 
78  b_mat(0, 0) = 1.0;
79  b_mat(1,1) = 3.6;
80  check_matrix_result = checkMatrix(a_mat, b_mat, true);
81  std::string::size_type pos = 0;
82  int lines = 0;
83  while (true)
84  {
85  pos = check_matrix_result.result_message.find("\n", pos);
86  if (pos == std::string::npos)
87  break;
88  ++pos;
89  ++lines;
90  }
91  CHECK(lines == 2);
92  REQUIRE(check_matrix_result.result == false);
93 }
94 
95 using testing::MatrixAccessor;
96 
97 TEST_CASE("checkMatrix_real", "[utilities][for_testing]")
98 {
99  // clang-format off
100  std::vector<double> a_vec{2.3, 4.5, 2.6,
101  0.5, 8.5, 3.3,
102  1.8, 4.4, 4.9};
103  MatrixAccessor<double> a_mat(a_vec.data(), 3, 3);
104  std::vector<double> b_vec{2.3, 4.5, 2.6,
105  0.5, 8.5, 3.3,
106  1.8, 4.4, 4.9};
107  MatrixAccessor<double> b_mat(b_vec.data(), 3, 3);
108 
109  auto check_matrix_result = checkMatrix(a_mat, b_mat);
110  // This would be how you would fail and print the information about what element failed.
111  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
112 
113  std::vector<double> c_vec{2.3, 4.5, 2.6, 0.0,
114  0.5, 8.5, 3.3, 0.0,
115  1.8, 4.4, 4.9, 0,0,
116  0.0, 0.0, 0.0, 0.0};
117  MatrixAccessor<double> c_mat(c_vec.data(), 4, 4);
118  // clang-format on
119 
120  check_matrix_result = checkMatrix(a_mat, c_mat);
121  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
122 
123  b_vec[0] = 1.0;
124  b_vec[5] = 3.6;
125  check_matrix_result = checkMatrix(a_mat, b_mat, true);
126  std::string::size_type pos = 0;
127  int lines = 0;
128  while (true)
129  {
130  pos = check_matrix_result.result_message.find("\n", pos);
131  if (pos == std::string::npos)
132  break;
133  ++pos;
134  ++lines;
135  }
136  CHECK(lines == 2);
137  REQUIRE(check_matrix_result.result == false);
138 }
139 
140 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
CHECKED_ELSE(check_matrix_result.result)
auto check_matrix_result
void resize(size_type n, size_type m)
Resize the container.
Definition: OhmmsMatrix.h:99
REQUIRE(std::filesystem::exists(filename))
CheckMatrixResult checkMatrix(M1 &a_mat, M2 &b_mat, const bool check_all=false, std::optional< const double > eps=std::nullopt)
This function checks equality a_mat and b_mat elements M1, M2 need to have their element type declare...
Definition: checkMatrix.hpp:63
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
Read only access, for testing!