QMCPACK
test_Listener.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 Laboratory
8 //
9 // Some code refactored from: DensityMatrices1b.h
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "catch.hpp"
13 
14 #include "Listener.hpp"
15 #include <string>
16 #include <vector>
17 #include "Configuration.h"
18 
19 namespace qmcplusplus
20 {
21 
22 using QMCT = QMCTraits;
23 using Real = QMCT::RealType;
24 
25 namespace testing
26 {
27 
28 /** Mock class that collects ListnerVectors as QMCHamiltonian does
29  * and reports ListenerVectors Hamiltonian operators do when they report per particle values.
30  */
32 {
33 private:
34  std::vector<ListenerVector<Real>> listener_vectors_;
35  const std::string name_{"Talker"};
36 
37 public:
38  /** why move or not move */
39  void registerVector(ListenerVector<Real>&& listener_vector) { listener_vectors_.push_back(std::move(listener_vector)); }
40  void reportVector()
41  {
42  Vector<Real> vec_part(4);
43  std::iota(vec_part.begin(), vec_part.end(), 0);
44  for (auto& listener : listener_vectors_)
45  listener.report(0, name_, vec_part);
46  }
47 };
48 
50 {
51 public:
52  /** Return listener frunction that has captured an object data member.
53  * returning a lambda allows access to the listening object controlled but allows a great deal of flexibility
54  * in dealing with the vector report. In this case the receiver just copies the vector it is called with to local storage
55  * which the lambda has captured.
56  */
57  auto getParticularListener(Vector<Real>& local_vector)
58  {
59  return [&local_vector](const int walker_index, const std::string& name, const Vector<Real>& values) -> void {
60  local_vector = values;
61  };
62  }
64 
65  // For purposes of testing this is public.
67 };
68 
69 TEST_CASE("ListenerVector", "[hamiltonian]")
70 {
71  MockQMCHamiltonianAndReporter mock_ham_report;
72  MockPerParticleEstimator mock_estimator;
73 
74  mock_ham_report.registerVector(mock_estimator.makeListener());
75 
76  mock_ham_report.reportVector();
77  CHECK(mock_estimator.receiver_vector_[0] == 0);
78  CHECK(mock_estimator.receiver_vector_[3] == 3);
79 }
80 
81 } // namespace testing
82 } // namespace qmcplusplus
Mock class that collects ListnerVectors as QMCHamiltonian does and reports ListenerVectors Hamiltonia...
TEST_CASE("ListenerVector", "[hamiltonian]")
auto getParticularListener(Vector< Real > &local_vector)
Return listener frunction that has captured an object data member.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::RealType RealType
Definition: Configuration.h:58
An object of this type is a listener expecting a callback to the report function with a vector of val...
Definition: Listener.hpp:38
std::vector< ListenerVector< Real > > listener_vectors_
ForceBase::Real Real
Definition: ForceBase.cpp:26
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void registerVector(ListenerVector< Real > &&listener_vector)
why move or not move
Listener types that allow Estimators to register with QMCHamiltonian to have "trace" values from oper...