QMCPACK
test_variable_set.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) 2019 QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "VariableSet.h"
16 #include "io/hdf/hdf_archive.h"
17 
18 #include <stdio.h>
19 #include <string>
20 
21 using std::string;
22 
23 namespace optimize
24 {
25 TEST_CASE("VariableSet empty", "[optimize]")
26 {
27  VariableSet vs;
28 
29  REQUIRE(vs.is_optimizable() == false);
30  REQUIRE(vs.size_of_active() == 0);
31  REQUIRE(vs.find("something") == vs.NameAndValue.end());
32  REQUIRE(vs.getIndex("something") == -1);
33 }
34 
35 TEST_CASE("VariableSet one", "[optimize]")
36 {
37  VariableSet vs;
38  VariableSet::real_type first_val(1.123456789);
39  vs.insert("first", first_val);
40  std::vector<std::string> names{"first"};
41  vs.activate(names.begin(), names.end(), true);
42 
43  REQUIRE(vs.is_optimizable() == true);
44  REQUIRE(vs.size_of_active() == 1);
45  REQUIRE(vs.getIndex("first") == 0);
46  REQUIRE(vs.name(0) == "first");
47  double first_val_real = 1.123456789;
48  CHECK(vs[0] == Approx(first_val_real));
49 
50  std::ostringstream o;
51  vs.print(o, 0, false);
52  //std::cout << o.str() << std::endl;
53  REQUIRE(o.str() == "first 1.123457e+00 0 1 ON 0\n");
54 
55  std::ostringstream o2;
56  vs.print(o2, 1, true);
57  //std::cout << o2.str() << std::endl;
58 
59  char formatted_output[] = " Name Value Type Recompute Use Index\n"
60  " ----- ---------------------------- ---- --------- --- -----\n"
61  " first 1.123457e+00 0 1 ON 0\n";
62 
63 
64  REQUIRE(o2.str() == formatted_output);
65 }
66 
67 TEST_CASE("VariableSet output", "[optimize]")
68 {
69  VariableSet vs;
70  VariableSet::real_type first_val(11234.56789);
71  VariableSet::real_type second_val(0.000256789);
72  VariableSet::real_type third_val(-1.2);
73  vs.insert("s", first_val);
74  vs.insert("second", second_val);
75  vs.insert("really_long_name", third_val);
76  std::vector<std::string> names{"s", "second", "really_long_name"};
77  vs.activate(names.begin(), names.end(), true);
78 
79  std::ostringstream o;
80  vs.print(o, 0, true);
81  //std::cout << o.str() << std::endl;
82 
83  char formatted_output[] = " Name Value Type Recompute Use Index\n"
84  "---------------- ---------------------------- ---- --------- --- -----\n"
85  " s 1.123457e+04 0 1 ON 0\n"
86  " second 2.567890e-04 0 1 ON 1\n"
87  "really_long_name -1.200000e+00 0 1 ON 2\n";
88 
89  REQUIRE(o.str() == formatted_output);
90 }
91 
92 TEST_CASE("VariableSet HDF output and input", "[optimize]")
93 {
94  VariableSet vs;
95  VariableSet::real_type first_val(11234.56789);
96  VariableSet::real_type second_val(0.000256789);
97  VariableSet::real_type third_val(-1.2);
98  vs.insert("s", first_val);
99  vs.insert("second", second_val);
100  vs.insert("really_really_really_long_name", third_val);
102  vs.writeToHDF("vp.h5", hout);
103 
104  VariableSet vs2;
105  vs2.insert("s", 0.0);
106  vs2.insert("second", 0.0);
108  vs2.readFromHDF("vp.h5", hin);
109  CHECK(vs2.find("s")->second == Approx(first_val));
110  CHECK(vs2.find("second")->second == Approx(second_val));
111  // This value as in the file, but not in the VariableSet that loaded the file,
112  // so the value does not get added.
113  CHECK(vs2.find("really_really_really_long_name") == vs2.end());
114 }
115 
116 
117 } // namespace optimize
iterator find(const std::string &vname)
return the iterator of a named parameter
Definition: VariableSet.h:98
void writeToHDF(const std::string &filename, qmcplusplus::hdf_archive &hout) const
bool is_optimizable() const
if any of Index value is not zero, return true
Definition: VariableSet.h:76
const_iterator end() const
return the last const_iterator
Definition: VariableSet.h:82
void readFromHDF(const std::string &filename, qmcplusplus::hdf_archive &hin)
Read variational parameters from an HDF file.
class to handle hdf file
Definition: hdf_archive.h:51
void activate(ForwardIterator first, ForwardIterator last, bool reindex)
activate variables for optimization
Definition: VariableSet.h:264
TEST_CASE("VariableSet empty", "[optimize]")
REQUIRE(std::filesystem::exists(filename))
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
int size_of_active() const
return the number of active variables
Definition: VariableSet.h:78
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
std::vector< pair_type > NameAndValue
Definition: VariableSet.h:66
qmcplusplus::QMCTraits::RealType real_type
Definition: VariableSet.h:51
const std::string & name(int i) const
return the name of i-th variable
Definition: VariableSet.h:189
void print(std::ostream &os, int leftPadSpaces=0, bool printHeader=false) const
int getIndex(const std::string &vname) const
return the Index vaule for the named parameter