QMCPACK
OptimizableObject.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: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #ifndef QMCPLUSPLUS_OPTIMIZABLEOBJECT_H
14 #define QMCPLUSPLUS_OPTIMIZABLEOBJECT_H
15 
16 #include "VariableSet.h"
18 
19 /**@file OptimizableObject.h
20  *@brief Declaration of OptimizableObject
21  */
22 namespace qmcplusplus
23 {
25 
27 {
28 public:
29  OptimizableObject(const std::string& name) : name_(name) {}
30 
31  const std::string& getName() const { return name_; }
32  bool isOptimized() const { return is_optimized_; }
33 
34 private:
35  /** Name of the optimizable object
36  */
37  const std::string name_;
38  /** If true, this object is actively modified during WFOpt
39  */
40  bool is_optimized_ = false;
41 
42 public:
43  /** check in variational parameters to the global list of parameters used by the optimizer.
44  * @param active a super set of optimizable variables
45  *
46  * The existing checkInVariables implementation in WFC/SPO/.. are inclusive and it calls checkInVariables of its members
47  * class A: public SPOSet {}
48  * class B: public WFC
49  * {
50  * A objA;
51  * checkInVariables() { objA.checkInVariables(); }
52  * };
53  *
54  * With OptimizableObject,
55  * class A: public OptimizableObject {}
56  * class B: public OptimizableObject
57  * {
58  * A objA;
59  * checkInVariablesExclusive() { // should not call objA.checkInVariablesExclusive() if objA has been extracted; }
60  * };
61  * A vector of OptimizableObject, will be created by calling extractOptimizableObjects().
62  * All the checkInVariablesExclusive() will be called through this vector and thus
63  * checkInVariablesExclusive implementation should only handle non-OptimizableObject members.
64  */
65  virtual void checkInVariablesExclusive(opt_variables_type& active) = 0;
66 
67  /** reset the parameters during optimizations. Exclusive, see checkInVariablesExclusive
68  */
69  virtual void resetParametersExclusive(const opt_variables_type& active) = 0;
70 
71  /** print the state, e.g., optimizables */
72  virtual void reportStatus(std::ostream& os) {}
73 
74  void setOptimization(bool state) { is_optimized_ = state; }
75 
76  /** Write the variational parameters for this object to the VP HDF file
77  *
78  * The hout parameter should come from VariableSet::writeToHDF
79  *
80  * Objects can use this function to store additional information to the file.
81  *
82  * By default the parameters are saved in VariableSet::writeToHDF, and objects
83  * do not need to implement this function (yet).
84  *
85  */
86  virtual void writeVariationalParameters(hdf_archive& hout){};
87 
88  /** Read the variational parameters for this object from the VP HDF file
89  *
90  * The hin parameter should come from VariableSet::readFromHDF
91  *
92  * By default the parameters are read in VariableSet::readFromHDF, and objects
93  * do not need to implement this function (yet).
94  */
95  virtual void readVariationalParameters(hdf_archive& hin){};
96 };
97 
98 class UniqueOptObjRefs : public RefVector<OptimizableObject>
99 {
100 public:
102 
104  {
105  if (obj.getName().empty())
106  throw std::logic_error("BUG!! Only named OptimizableObject object can be added to UniqueOptObjRefs!");
107  auto result =
108  std::find_if(begin(), end(), [&](OptimizableObject& element) { return element.getName() == obj.getName(); });
109  if (result == end())
111  }
112 };
113 
114 } // namespace qmcplusplus
115 #endif
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
OptimizableObject & operator[](size_t i) const
virtual void reportStatus(std::ostream &os)
print the state, e.g., optimizables
virtual void writeVariationalParameters(hdf_archive &hout)
Write the variational parameters for this object to the VP HDF file.
class to handle hdf file
Definition: hdf_archive.h:51
virtual void checkInVariablesExclusive(opt_variables_type &active)=0
check in variational parameters to the global list of parameters used by the optimizer.
const std::string & getName() const
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
OptimizableObject(const std::string &name)
const std::string name_
Name of the optimizable object.
std::vector< std::reference_wrapper< T > > RefVector
bool is_optimized_
If true, this object is actively modified during WFOpt.
virtual void readVariationalParameters(hdf_archive &hin)
Read the variational parameters for this object from the VP HDF file.
void push_back(OptimizableObject &obj)
virtual void resetParametersExclusive(const opt_variables_type &active)=0
reset the parameters during optimizations.