QMCPACK
test_EstimatorManagerNew.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) 2022 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File refactored from: Refactored from test_manager.cpp
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "Message/Communicate.h"
16 #include "OhmmsData/Libxml2Doc.h"
26 #include "Utilities/ProjectData.h"
27 #include <stdio.h>
28 #include <sstream>
29 
30 namespace qmcplusplus
31 {
32 
33 TEST_CASE("EstimatorManagerNew::EstimatorManager(comm)", "[estimators]")
34 {
36  using namespace testing;
37 
40 
41  // Just checking here that we have an empty estimator manager in embt.
42 
43  CHECK(embt.em.getNumEstimators() == 0);
45 }
46 
47 TEST_CASE("EstimatorManagerNew::EstimatorManagerNew(EstimatorManagerInput,...)", "[estimators]")
48 {
51 
52  using namespace testing;
55 
56  CHECK(emi.get_estimator_inputs().size() == 2);
57  CHECK(emi.get_scalar_estimator_inputs().size() == 1);
58 
62  auto& pset = *(particle_pool.getParticleSet("e"));
64  auto& twf = *(wavefunction_pool.getWaveFunction("wavefunction"));
65  auto& ham = *(hamiltonian_pool.getPrimary());
66  EstimatorManagerNew emn(comm, std::move(emi), ham, pset, twf);
67 
68  CHECK(emn.getNumEstimators() == 2);
69  // Because the only scalar estimator becomes the main estimator.
72  CHECK(emnta.getMainEstimator().getName() == "LocalEnergyEstimator");
73 
74 
75  // Check behavior when multiple "main" estimators are in the input
76  // Each should override the previous main input as this is the behavior of legacy.
79 
80  CHECK(emi2.get_estimator_inputs().size() == 2);
81  CHECK(emi2.get_scalar_estimator_inputs().size() == 5);
82 
83  EstimatorManagerNew emn2(comm, std::move(emi2), ham, pset, twf);
84 
85  CHECK(emn2.getNumEstimators() == 2);
86  // Because the only scalar estimator becomes the main estimator.
89  CHECK(emnta2.getMainEstimator().getName() == "RMCLocalEnergyEstimator");
90 }
91 
92 TEST_CASE("EstimatorManagerNew::collectMainEstimators", "[estimators]")
93 {
95 
98  // by design we have done no averaging here
99  // the division by total weight happens only when a block is over and the
100  // accumulated data has been reduced down. So here there should just be simple sums.
101 
103 
106  double correct_value = 5.0;
107  CHECK(embt.em.get_AverageCache()[0] == Approx(correct_value));
108  correct_value = 8.0;
109  CHECK(embt.em.get_AverageCache()[1] == Approx(correct_value));
110  correct_value = 11.0;
111  CHECK(embt.em.get_AverageCache()[2] == Approx(correct_value));
112  correct_value = 14.0;
113  CHECK(embt.em.get_AverageCache()[3] == Approx(correct_value));
114 }
115 
116 TEST_CASE("EstimatorManagerNew::collectScalarEstimators", "[estimators]")
117 {
119 
122  // by design we have done no averaging here
123  // the division by total weight happens only when a block is over and the
124  // accumulated data has been reduced down. So here there should just be simple sums.
125 
127  double correct_value = 11.0;
128  CHECK(embt.em.get_AverageCache()[0] == Approx(correct_value));
129  correct_value = 20.0;
130  CHECK(embt.em.get_AverageCache()[1] == Approx(correct_value));
131  correct_value = 29.0;
132  CHECK(embt.em.get_AverageCache()[2] == Approx(correct_value));
133  correct_value = 38.0;
134  CHECK(embt.em.get_AverageCache()[3] == Approx(correct_value));
135 }
136 
137 TEST_CASE("EstimatorManagerNew adhoc addVector operator", "[estimators]")
138 {
139  int num_scalars = 3;
140  std::vector<double> vec_a{1.0, 2.0, 3.0};
141  std::vector<double> vec_b{2.0, 3.0, 4.0};
142  std::vector<std::vector<double>> est{vec_a, vec_b};
143  auto addVectors = [](const auto& vec_a, const auto& vec_b) {
144  std::vector<ScalarEstimatorBase::RealType> result_vector(vec_a.size(), 0.0);
145  for (int i = 0; i < vec_a.size(); ++i)
146  result_vector[i] = vec_a[i] + vec_b[i];
147  return result_vector;
148  };
149 
150  std::vector<double> reduced_scalars(num_scalars);
151  reduced_scalars = std::accumulate(est.begin(), est.end(), std::vector<double>(num_scalars, 0.0), addVectors);
152  std::vector<double> correct{3.0, 5.0, 7.0};
153  REQUIRE(reduced_scalars == correct);
154 }
155 
156 } // namespace qmcplusplus
class that handles xmlDoc
Definition: Libxml2Doc.h:76
ScalarEstimatorInputs & get_scalar_estimator_inputs()
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Libxml2Document createEstimatorManagerNewInputXML()
Class to manage a set of ScalarEstimators As a manager, this class handles the aggregation of data fr...
static ParticleSetPool make_diamondC_1x1x1(Communicate *c)
bool testReplaceMainEstimator()
test replacing the main estimator
EstimatorManagerNew emn2(comm, std::move(emi2), ham, pset, twf)
TEST_CASE("complex_helper", "[type_traits]")
static WaveFunctionPool make_diamondC_1x1x1(const RuntimeOptions &runtime_options, Communicate *comm, ParticleSetPool &particle_pool)
Libxml2Document createEstimatorManagerNewVMCInputXML()
virtual std::string getName() const =0
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
Collection of Local Energy Operators.
void collectMainEstimators()
call private EMB method and collect EMBTs estimators_ as main_estimators
ProjectData test_project("test", ProjectData::DriverVersion::BATCH)
Testing class breaking EstimatorManagerNew encapsultation.
void fakeMainScalarSamples()
Quickly add main scalar samples using FakeEstimator mock estimator.
EstimatorManagerInput emi(estimators_doc.getRoot())
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
const RuntimeOptions & getRuntimeOptions() const noexcept
Wrapping information on parallelism.
Definition: Communicate.h:68
REQUIRE(std::filesystem::exists(filename))
Compilation units that construct QMCDriverInput need visibility to the actual input classes types in ...
EstimatorManagerNewTestAccess emnta(emn)
testing::EstimatorManagerNewTest embt(ham, c, 1)
Libxml2Document estimators_doc2
EstimatorManagerInput emi2(estimators_doc2.getRoot())
static HamiltonianPool make_hamWithEE(Communicate *comm, ParticleSetPool &particle_pool, WaveFunctionPool &wavefunction_pool)
Libxml2Document estimators_doc
Input type for EstimatorManagerNew Parses Estimators level of input and and delegates child estimator...
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void fakeScalarSamplesAndCollect()
Quickly add scalar samples using FakeEstimator mock estimator.
EstimatorManagerNewTestAccess emnta2(emn2)
EstimatorManagerNew emn(comm, std::move(emi), ham, pset, twf)
Declaration of QMCHamiltonian.