QMCPACK
test_accumulator.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) 2017 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewin@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "Message/Communicate.h"
16 #include "OhmmsData/Libxml2Doc.h"
18 
19 
20 #include <stdio.h>
21 #include <sstream>
22 
23 namespace qmcplusplus
24 {
25 template<typename T>
27 {
28  //std::cout << "int eps = " << std::numeric_limits<T>::epsilon() << std::endl;
29  //std::cout << "int max = " << std::numeric_limits<T::max() << std::endl;
31  REQUIRE(a1.count() == 0);
32  REQUIRE(a1.good() == false);
33  REQUIRE(a1.bad() == true);
34  CHECK(a1.mean() == Approx(0.0));
35  CHECK(a1.mean2() == Approx(0.0));
36  CHECK(a1.variance() == Approx(std::numeric_limits<T>::max()));
37 
38  a1(2.0);
39  REQUIRE(a1.count() == 1);
40  REQUIRE(a1.good() == true);
41  REQUIRE(a1.bad() == false);
42  CHECK(a1.result() == Approx(2.0));
43  CHECK(a1.result2() == Approx(4.0));
44  CHECK(a1.mean() == Approx(2.0));
45  CHECK(a1.mean2() == Approx(4.0));
46  CHECK(a1.variance() == Approx(0.0));
47 
48  a1.clear();
49  REQUIRE(a1.count() == 0);
50  CHECK(a1.result() == Approx(0.0));
51  CHECK(a1.result2() == Approx(0.0));
52 }
53 
54 TEST_CASE("accumulator basic float", "[estimators]") { test_real_accumulator_basic<double>(); }
55 
56 TEST_CASE("accumulator basic double", "[estimators]") { test_real_accumulator_basic<double>(); }
57 
58 template<typename T>
60 {
62  a(2.0);
63  a(3.1);
64  a(-1.1);
65  a(4.8);
66  CHECK(a.count() == Approx(4.0));
67 
68  CHECK(a.result() == Approx(8.8));
69  CHECK(a.result2() == Approx(37.86));
70  CHECK(a.mean() == Approx(2.2));
71  CHECK(a.mean2() == Approx(9.465));
72  CHECK(a.variance() == Approx(4.625));
73 
74  std::pair<T, T> mv = a.mean_and_variance();
75  CHECK(mv.first == Approx(a.mean()));
76  CHECK(mv.second == Approx(a.variance()));
77 
78  CHECK(mean(a) == Approx(a.mean()));
79 
80  // check that this doesn't crash
81  std::stringstream o;
82  o << a;
83 }
84 
85 TEST_CASE("accumulator some values float", "[estimators]") { test_real_accumulator<float>(); }
86 
87 TEST_CASE("accumulator some values double", "[estimators]") { test_real_accumulator<double>(); }
88 
89 template<typename T>
91 {
93  a(2.0, 1.0);
94  a(3.1, 2.4);
95  a(-1.1, 2.1);
96  a(4.8, 3.3);
97  CHECK(a.count() == Approx(8.8));
98 
99  CHECK(a.result() == Approx(22.97));
100  CHECK(a.result2() == Approx(105.637));
101  CHECK(a.mean() == Approx(2.6102272727));
102  CHECK(a.mean2() == Approx(12.004204545));
103  CHECK(a.variance() == Approx(5.1909181302));
104 
105  std::pair<T, T> mv = a.mean_and_variance();
106  CHECK(mv.first == Approx(a.mean()));
107  CHECK(mv.second == Approx(a.variance()));
108 }
109 
110 TEST_CASE("accumulator with weights float", "[estimators]") { test_real_accumulator_weights<float>(); }
111 
112 TEST_CASE("accumulator with weights double", "[estimators]") { test_real_accumulator_weights<double>(); }
113 
114 } // namespace qmcplusplus
std::pair< return_type, return_type > mean_and_variance() const
Definition: accumulators.h:118
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void test_real_accumulator_basic()
TEST_CASE("complex_helper", "[type_traits]")
return_type mean2() const
return the mean of squared values
Definition: accumulators.h:129
void test_real_accumulator_weights()
bool good() const
return true if Weight!= 0
Definition: accumulators.h:103
bool bad() const
return true if Weight== 0
Definition: accumulators.h:105
generic accumulator of a scalar type
Definition: accumulators.h:32
void test_real_accumulator()
REQUIRE(std::filesystem::exists(filename))
return_type mean() const
return the mean
Definition: accumulators.h:126
return_type result() const
return the sum
Definition: accumulators.h:108
return_type result2() const
return the sum of value squared
Definition: accumulators.h:111
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
return_type count() const
return the count
Definition: accumulators.h:116
return_type variance() const
Definition: accumulators.h:131
ACC::value_type mean(const ACC &ac)
Definition: accumulators.h:147
Define and declare accumulator_set.