QMCPACK
EstimatorManagerBase.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) 2020 QMCPACK developers.
6 //
7 // File developed by: Bryan Clark, bclark@Princeton.edu, Princeton University
8 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
11 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
12 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
13 //
14 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
15 //////////////////////////////////////////////////////////////////////////////////////
16 
17 
18 /** @file EstimatorManagerBase.h
19  * @brief Manager class of scalar estimators
20  */
21 #ifndef QMCPLUSPLUS_ESTIMATORMANAGERBASE_H
22 #define QMCPLUSPLUS_ESTIMATORMANAGERBASE_H
23 
24 #include "Configuration.h"
25 #include "Utilities/Timer.h"
26 #include "Pools/PooledData.h"
27 #include "Message/Communicate.h"
29 #include "Particle/Walker.h"
30 #include "OhmmsPETE/OhmmsVector.h"
31 #include "io/hdf/hdf_archive.h"
32 #include <bitset>
33 
34 namespace qmcplusplus
35 {
36 class MCWalkerConfiguration;
37 class QMCHamiltonian;
38 class CollectablesEstimator;
39 
40 namespace testing
41 {
42 class EstimatorManagerBaseTest;
43 } // namespace testing
44 
45 
46 /** Class to manage a set of ScalarEstimators */
48 {
49 public:
52 
54  using BufferType = std::vector<RealType>;
56 
57  ///default constructor
59  ///copy constructor
61  ///destructor
62  virtual ~EstimatorManagerBase();
63 
64  /** set the communicator */
66 
67  /** return the communicator
68  */
70 
71  /** return true if the rank == 0
72  */
73  inline bool is_manager() const { return !myComm->rank(); }
74 
75  ///return the number of ScalarEstimators
76  inline int size() const { return Estimators.size(); }
77 
78  /** add a property with a name
79  * @param aname name of the column
80  * @return the property index so that its value can be set by setProperty(i)
81  *
82  * Append a named column. BlockProperties do not contain any meaning data
83  * but manages the name to index map for PropertyCache.
84  */
85  inline int addProperty(const char* aname) { return BlockProperties.add(aname); }
86 
87  /** set the value of the i-th column with a value v
88  * @param i column index
89  * @param v value
90  */
91  inline void setProperty(int i, RealType v) { PropertyCache[i] = v; }
92 
93  inline RealType getProperty(int i) const { return PropertyCache[i]; }
94 
95  int addObservable(const char* aname);
96 
97  inline RealType getObservable(int i) const { return TotalAverages[i]; }
98 
99  void getData(int i, std::vector<RealType>& values);
100 
101  /** add an Estimator
102  * @param newestimator New Estimator
103  * @param aname name of the estimator
104  * @return locator of newestimator
105  */
106  int add(std::unique_ptr<EstimatorType> newestimator, const std::string& aname);
107  //int add(CompositeEstimatorBase* newestimator, const std::string& aname);
108 
109  /** add a main estimator
110  * @param newestimator New Estimator
111  * @return locator of newestimator
112  */
113  int add(std::unique_ptr<EstimatorType> newestimator) { return add(std::move(newestimator), main_estimator_name_); }
114 
115  ///return a pointer to the estimator aname
116  EstimatorType* getEstimator(const std::string& a);
117 
118  ///return a pointer to the estimator
120 
121  void setCollectionMode(bool collect);
122  //void setAccumulateMode (bool setAccum) {AccumulateBlocks = setAccum;};
123 
124  ///process xml tag associated with estimators
125  //bool put(xmlNodePtr cur);
126  bool put(QMCHamiltonian& H, xmlNodePtr cur);
127 
129 
130  /** reset the estimator
131  */
132  void reset();
133 
134  /** start a run
135  * @param blocks number of blocks
136  * @param record if true, will write to a file
137  *
138  * Replace reportHeader and reset functon.
139  */
140  void start(int blocks, bool record = true);
141  /** stop a qmc run
142  *
143  * Replace finalize();
144  */
145  void stop();
146  /** stop a qmc run
147  */
148  void stop(const std::vector<EstimatorManagerBase*> m);
149 
150 
151  /** start a block
152  * @param steps number of steps in a block
153  */
154  void startBlock(int steps);
155 
156  /** stop a block
157  * @param accept acceptance rate of this block
158  */
159  void stopBlock(RealType accept, bool collectall = true);
160 
161  /** stop a block
162  * @param m list of estimator which has been collecting data independently
163  */
164  void stopBlock(const std::vector<EstimatorManagerBase*>& m);
165 
166  /** accumulate the measurements
167  * @param W walkers
168  */
170 
171  /** accumulate the measurements for a subset of walkers [it,it_end)
172  * @param W walkers
173  * @param it first walker
174  * @param it_end last walker
175  */
177 
178  /** get the average of per-block energy and variance of all the blocks
179  * Note: this is not weighted average. It can be the same as weighted average only when block weights are identical.
180  */
182 
183  template<class CT>
184  void write(CT& anything, bool doappend)
185  {
186  anything.write(h_file, doappend);
187  }
188 
189  auto& get_AverageCache() { return AverageCache; }
191 
192 protected:
193  // TODO: fix needless use of bitset instead of clearer more visible booleans
194  std::bitset<8> Options;
195  ///size of the message buffer
197  ///number of records in a block
199  ///index for the block weight PropertyCache(weightInd)
201  ///index for the block cpu PropertyCache(cpuInd)
202  int cpuInd;
203  ///index for the acceptance rate PropertyCache(acceptInd)
205  ///hdf5 handler
207  ///total weight accumulated in a block
209  ///file handler to write data
210  std::unique_ptr<std::ofstream> Archive;
211 #if defined(DEBUG_ESTIMATOR_ARCHIVE)
212  ///file handler to write data for debugging
213  std::unique_ptr<std::ofstream> DebugArchive;
214 #endif
215  ///communicator to handle communication
217  /** pointer to the primary ScalarEstimatorBase
218  */
220  /** pointer to the CollectablesEstimator
221  *
222  * Do not need to clone: owned by the master thread
223  */
224  std::unique_ptr<CollectablesEstimator> Collectables;
225  /** accumulator for the energy
226  *
227  * @todo expand it for all the scalar observables to report the final results
228  */
230  /** accumulator for the variance **/
232  ///cached block averages of the values
234  ///cached block averages of the squared values
236  ///cached block averages of properties, e.g. BlockCPU
238  ///manager of scalar data
240  ///manager of property data
242  ///block averages: name to value
244  ///data accumulated over the blocks
246  ///index mapping between BlockAverages and TotalAverages
247  std::vector<int> Block2Total;
248  ///column map
249  std::map<std::string, int> EstimatorMap;
250  ///estimators of simple scalars
252  ///convenient descriptors for hdf5
253  std::vector<ObservableHelper> h5desc;
254  /////estimators of composite data
255  //CompositeEstimatorSet* CompEstimators;
256  ///Timer
258 
259 private:
260  ///name of the primary estimator name
261  std::string main_estimator_name_;
262 
263  ///number of maximum data for a scalar.dat
265 
266  //Data for communication
267  std::vector<std::unique_ptr<BufferType>> RemoteData;
268 
269  ///collect data and write
270  void collectBlockAverages();
271 
272  ///add header to an std::ostream
273  void addHeader(std::ostream& o);
274 
275  ///largest name in BlockAverages adding 2 characters
277 
279 };
280 } // namespace qmcplusplus
281 #endif
bool is_manager() const
return true if the rank == 0
int add(std::unique_ptr< EstimatorType > newestimator, const std::string &aname)
add an Estimator
ScalarEstimatorBase * MainEstimator
pointer to the primary ScalarEstimatorBase
QMCTraits::FullPrecRealType FullPrecRealType
EstimatorType * getEstimator(const std::string &a)
return a pointer to the estimator aname
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void accumulate(MCWalkerConfiguration &W)
accumulate the measurements
int rank() const
return the rank
Definition: Communicate.h:116
RealType BlockWeight
total weight accumulated in a block
int weightInd
index for the block weight PropertyCache(weightInd)
Testing class breaking EstimatorManagerBase encapsultation.
int acceptInd
index for the acceptance rate PropertyCache(acceptInd)
RecordNamedProperty< RealType > BlockProperties
manager of property data
ScalarEstimatorBase::accumulator_type energyAccumulator
accumulator for the energy
QMCTraits::FullPrecRealType RealType
Collection of Local Energy Operators.
void getData(int i, std::vector< RealType > &values)
Vector< RealType > AverageCache
cached block averages of the values
Communicate * getCommunicator()
return the communicator
int max_output_scalar_dat_
number of maximum data for a scalar.dat
class to handle hdf file
Definition: hdf_archive.h:51
Timer class.
std::vector< std::unique_ptr< T > > UPtrVector
int RecordCount
number of records in a block
ScalarEstimatorBase::accumulator_type varAccumulator
accumulator for the variance
std::unique_ptr< std::ofstream > Archive
file handler to write data
int size() const
return the number of ScalarEstimators
void addHeader(std::ostream &o)
add header to an std::ostream
void write(CT &anything, bool doappend)
Wrapping information on parallelism.
Definition: Communicate.h:68
void startBlock(int steps)
start a block
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
std::map< std::string, int > EstimatorMap
column map
WalkerList_t::iterator iterator
FIX: a type alias of iterator for an object should not be for just one of many objects it holds...
virtual ~EstimatorManagerBase()
destructor
void setCollectionMode(bool collect)
set CollectSum
int add(const std::string &aname)
int BufferSize
size of the message buffer
int cpuInd
index for the block cpu PropertyCache(cpuInd)
UPtrVector< EstimatorType > Estimators
estimators of simple scalars
RecordNamedProperty< RealType > TotalAverages
block averages: name to value
bool put(QMCHamiltonian &H, xmlNodePtr cur)
process xml tag associated with estimators
std::string main_estimator_name_
name of the primary estimator name
Class to manage a set of ScalarEstimators.
void setCommunicator(Communicate *c)
set the communicator
std::unique_ptr< CollectablesEstimator > Collectables
pointer to the CollectablesEstimator
void collectBlockAverages()
collect data and write
Matrix< RealType > TotalAveragesData
data accumulated over the blocks
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
EstimatorType * getMainEstimator()
return a pointer to the estimator
EstimatorManagerBase(Communicate *c=0)
default constructor
std::vector< ObservableHelper > h5desc
convenient descriptors for hdf5
Communicate * myComm
communicator to handle communication
std::vector< std::unique_ptr< BufferType > > RemoteData
void stopBlock(RealType accept, bool collectall=true)
stop a block
Vector< RealType > PropertyCache
cached block averages of properties, e.g. BlockCPU
RecordNamedProperty< RealType > BlockAverages
manager of scalar data
int addProperty(const char *aname)
add a property with a name
std::vector< int > Block2Total
index mapping between BlockAverages and TotalAverages
size_t max_block_avg_name_
largest name in BlockAverages adding 2 characters
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
Abstract class for an estimator of a scalar operator.
void start(int blocks, bool record=true)
start a run
Vector< RealType > SquaredAverageCache
cached block averages of the squared values
void getApproximateEnergyVariance(RealType &e, RealType &var)
get the average of per-block energy and variance of all the blocks Note: this is not weighted average...
A container class to represent a walker.
Definition: Walker.h:49
Define a serialized buffer to store anonymous data.
int add(std::unique_ptr< EstimatorType > newestimator)
add a main estimator
void setProperty(int i, RealType v)
set the value of the i-th column with a value v