QMCPACK
ScalarEstimatorBase.h
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 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
11 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
12 //
13 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 
17 #ifndef QMCPLUSPLUS_SCALAR_ESTIMATORBASE_H
18 #define QMCPLUSPLUS_SCALAR_ESTIMATORBASE_H
22 #include "Particle/Walker.h"
23 #if !defined(REMOVE_TRACEMANAGER)
25 #endif
26 
27 namespace qmcplusplus
28 {
29 class ObservableHelper;
30 
31 /** Abstract class for an estimator of a scalar operator.
32  *
33  * ScalarEstimators derived from ScalarEstimatorBase implement three main functions
34  * - accumulate : measure and accumulate its value and the square of the value
35  * - add2Record : \todo document this
36  * - registerObservables : \todo document this
37  * - clone : because all types must be erased
38  * ScalarEstimatorBase and its derived classes do not perform any I/O function.
39  */
41 {
48 
49  ///first index within an record of the first element handled by an object
51  ///last index within an record of the first element handled by an object
52  int LastIndex;
53  ///scalars to be measured
54  std::vector<accumulator_type> scalars;
55  ///scalars saved
56  std::vector<accumulator_type> scalars_saved;
57  // RealType NSTEPS;
58 
60 
61  virtual ~ScalarEstimatorBase() {}
62 
63  /// Is this estimator a main estimator i.e. the estimator required for a particular driver.
64  virtual bool isMainEstimator() const { return false; }
65  virtual std::string getName() const = 0;
66 
67  ///return average of the
68  inline RealType average(int i = 0) const { return scalars_saved[i].mean(); }
69  ///return a variance
70  inline RealType variance(int i = 0) const { return scalars_saved[i].variance(); }
71  ///retrun mean and variance
72  inline std::pair<RealType, RealType> operator[](int i) const { return scalars[i].mean_and_variance(); }
73 
74  ///return the size of scalars it manages
75  virtual inline int size() const { return scalars.size(); }
76 
77  ///clear the scalars to collect
78  inline void clear()
79  {
80  for (int i = 0; i < scalars.size(); i++)
81  scalars[i].clear();
82  }
83 
84  /** take block average and write to a common container */
85  template<typename IT>
86  inline void takeBlockAverage(IT first)
87  {
88  first += FirstIndex;
89  for (int i = 0; i < scalars.size(); i++)
90  {
91  *first++ = scalars[i].mean();
92  scalars_saved[i] = scalars[i]; //save current block
93  scalars[i].clear();
94  }
95  }
96 
97  /** take block average and write to common containers for values and squared values
98  * @param first starting iterator of values
99  * @param first_sq starting iterator of squared values
100  */
101  template<typename IT>
102  inline void takeBlockAverage(IT first, IT first_sq)
103  {
104  first += FirstIndex;
105  first_sq += FirstIndex;
106  for (int i = 0; i < scalars.size(); i++)
107  {
108  *first++ = scalars[i].mean();
109  *first_sq++ = scalars[i].mean2();
110  // For mixed precision this is where data goes from Actual QMCT::RealType
111  // to the local RealType which is hard coded to QMCTFullPrecRealType.
112  // I think it is a bad idea to have RealType to have a changing meaning,
113  // I also feel like having the floating point expension needed to write always to
114  // double in hdf5 is poor form especially since I had to figure this out in many
115  // years later.
116  scalars_saved[i] = scalars[i]; //save current block
117  scalars[i].clear();
118  }
119  }
120 
121  /** add the block accumulated scalars
122  * @param first starting iterator of values
123  */
124  template<typename IT>
125  inline void addAccumulated(IT first)
126  {
127  first += FirstIndex;
128  for (int i = 0; i < scalars.size(); i++)
129  {
130  *first++ += scalars[i].result();
131  scalars[i].clear();
132  }
133  }
134 
135  /** a virtual function to accumulate observables or collectables
136  * @param W const MCWalkerConfiguration
137  * @param first const_iterator for the first walker
138  * @param last const_iterator for the last walker
139  * @param wgt weight
140  *
141  * Pass W along with the iterators so that the properties of W can be utilized.
142  */
143  virtual void accumulate(const MCWalkerConfiguration& W, WalkerIterator first, WalkerIterator last, RealType wgt) = 0;
144 
145  /** a virtual function to accumulate observables or collectables
146  * @param global_walkers_ walkers per ranks or walkers total?
147  * @param RefVector of MCPWalkers
148  * @param wgt weight or maybe norm
149  *
150  */
151  virtual void accumulate(const RefVector<MCPWalker>&) = 0;
152 
153  /** add the content of the scalar estimator to the record
154  * @param record scalar data list
155  *
156  * Each ScalarEstimatorBase object adds 1 to many accumulator_type
157  */
158  virtual void add2Record(RecordNamedProperty<RealType>& record) = 0;
159 
160  /** add descriptors of observables to utilize hdf5
161  * @param h5desc descriptor of a data stored in a h5 group
162  * @param file file to which each statistical data will be stored
163  */
164  virtual void registerObservables(std::vector<ObservableHelper>& h5dec, hdf_archive& file) = 0;
165 
166  ///clone the object
167  virtual ScalarEstimatorBase* clone() = 0;
168 
169  /// String representation of the derived type of the ScalarEstimator
170  virtual const std::string& getSubTypeStr() const = 0;
171 };
172 } // namespace qmcplusplus
173 
174 #endif
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::pair< RealType, RealType > operator[](int i) const
retrun mean and variance
void takeBlockAverage(IT first, IT first_sq)
take block average and write to common containers for values and squared values
virtual bool isMainEstimator() const
Is this estimator a main estimator i.e. the estimator required for a particular driver.
virtual std::string getName() const =0
virtual const std::string & getSubTypeStr() const =0
String representation of the derived type of the ScalarEstimator.
class to handle hdf file
Definition: hdf_archive.h:51
int FirstIndex
first index within an record of the first element handled by an object
QMCTraits::FullPrecRealType RealType
RealType average(int i=0) const
return average of the
std::vector< accumulator_type > scalars
scalars to be measured
virtual void registerObservables(std::vector< ObservableHelper > &h5dec, hdf_archive &file)=0
add descriptors of observables to utilize hdf5
virtual int size() const
return the size of scalars it manages
void takeBlockAverage(IT first)
take block average and write to a common container
void addAccumulated(IT first)
add the block accumulated scalars
WalkerList_t::const_iterator const_iterator
const_iterator of Walker container
RealType variance(int i=0) const
return a variance
virtual void accumulate(const MCWalkerConfiguration &W, WalkerIterator first, WalkerIterator last, RealType wgt)=0
a virtual function to accumulate observables or collectables
std::vector< accumulator_type > scalars_saved
scalars saved
void clear()
clear the scalars to collect
WalkerConfigurations::Walker_t Walker_t
std::vector< std::reference_wrapper< T > > RefVector
virtual void add2Record(RecordNamedProperty< RealType > &record)=0
add the content of the scalar estimator to the record
MCWalkerConfiguration::const_iterator WalkerIterator
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
Abstract class for an estimator of a scalar operator.
virtual ScalarEstimatorBase * clone()=0
clone the object
int LastIndex
last index within an record of the first element handled by an object
Declaration of a MCWalkerConfiguration.
A container class to represent a walker.
Definition: Walker.h:49
Define and declare accumulator_set.