QMCPACK
test_OptimizableObject.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: 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 #include "catch.hpp"
14 
15 #include "OptimizableObject.h"
16 #include "VariableSet.h"
17 #include "io/hdf/hdf_archive.h"
18 
19 namespace qmcplusplus
20 {
21 
23 {
24 public:
25  FakeOptimizableObject(const std::string& my_name, double var1 = 0.0, double var2 = 0.0) : OptimizableObject(my_name)
26  {
27  myVars.insert("var1", var1);
28  myVars.insert("var2", var2);
29  }
30  void checkInVariablesExclusive(opt_variables_type& active) override { active.insertFrom(myVars); }
31 
32  void resetParametersExclusive(const opt_variables_type& active) override {}
33 
35  {
36  hout.push("FakeOptimizableObject");
37  // Convert to double before writing to HDF.
38  // If these get written as complex, but read as double, it can cause memory overwrites.
39  // The HDF read call uses the file's type information to determine the amount data read
40  // (2 doubles for complex), but the variable size is just one double.
41  double var1 = std::real(myVars["var1"]);
42  double var2 = std::real(myVars["var2"]);
43  hout.write(var1, "var1");
44  hout.write(var2, "var2");
45  hout.write(extra_data, "extra_data");
46  hout.pop();
47  }
48 
50  {
51  hin.push("FakeOptimizableObject", false);
52 
53  myVars.clear();
54  double tmp_var1;
55  double tmp_var2;
56  // The HDF read call uses the file's type information to determine the amount data read into the variable.
57  hin.read(tmp_var1, "var1");
58  hin.read(tmp_var2, "var2");
59  hin.read(extra_data, "extra_data");
60  myVars.insert("var1", tmp_var1);
61  myVars.insert("var2", tmp_var2);
62  hin.pop();
63  }
64 
65  double extra_data;
67 };
68 
69 TEST_CASE("Test OptimizableObject", "[wavefunction]")
70 {
71  FakeOptimizableObject fake_a("functor_a");
72  FakeOptimizableObject fake_b("functor_b");
73 
74  UniqueOptObjRefs opt_obj_refs;
75  opt_obj_refs.push_back(fake_a);
76  opt_obj_refs.push_back(fake_b);
77 
78  CHECK(opt_obj_refs.size() == 2);
79 
80  FakeOptimizableObject fake_c("functor_b");
81  opt_obj_refs.push_back(fake_c);
82  CHECK(opt_obj_refs.size() == 2);
83  CHECK(opt_obj_refs[0].getName() == "functor_a");
84  CHECK(opt_obj_refs[1].getName() == "functor_b");
85 }
86 
87 TEST_CASE("OptimizableObject HDF output and input", "[wavefunction]")
88 {
89  FakeOptimizableObject fake_a("functor_a", 1.1, 2.3);
90  fake_a.extra_data = 3.4;
91 
92  hdf_archive hout;
93  opt_variables_type opt_vars;
94  fake_a.checkInVariablesExclusive(opt_vars);
95  opt_vars.writeToHDF("opt_obj.h5", hout);
96 
97  fake_a.writeVariationalParameters(hout);
98 
99  FakeOptimizableObject fake_2a("functor_a");
100  hdf_archive hin;
101  opt_variables_type opt_vars2;
102  fake_2a.checkInVariablesExclusive(opt_vars2);
103 
104  opt_vars2.readFromHDF("opt_obj.h5", hin);
105  CHECK(std::real(opt_vars2["var1"]) == Approx(1.1));
106  CHECK(std::real(opt_vars2["var2"]) == Approx(2.3));
107 
108  fake_2a.readVariationalParameters(hin);
109 
110  opt_variables_type opt_vars3;
111  fake_2a.checkInVariablesExclusive(opt_vars3);
112  CHECK(std::real(opt_vars3["var1"]) == Approx(1.1));
113  CHECK(std::real(opt_vars3["var2"]) == Approx(2.3));
114  CHECK(fake_2a.extra_data == Approx(3.4));
115 }
116 } // namespace qmcplusplus
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
Declaration of OptimizableObject.
void writeToHDF(const std::string &filename, qmcplusplus::hdf_archive &hout) const
void write(T &data, const std::string &aname)
write the data to the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:259
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
void readFromHDF(const std::string &filename, qmcplusplus::hdf_archive &hin)
Read variational parameters from an HDF file.
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
class to handle hdf file
Definition: hdf_archive.h:51
void writeVariationalParameters(hdf_archive &hout) override
Write the variational parameters for this object to the VP HDF file.
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Definition: VariableSet.h:133
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
void clear()
clear the variable set
Definition: VariableSet.cpp:28
void readVariationalParameters(hdf_archive &hin) override
Read the variational parameters for this object from the VP HDF file.
void push(const std::string &gname, bool createit=true)
push a group to the group stack
FakeOptimizableObject(const std::string &my_name, double var1=0.0, double var2=0.0)
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void read(T &data, const std::string &aname)
read the data from the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:306
void insertFrom(const VariableSet &input)
insert a VariableSet to the list
Definition: VariableSet.cpp:37
void push_back(OptimizableObject &obj)