QMCPACK
test_QMCCostFunctionBatched.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) 2020 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 #include "catch.hpp"
14 #include "FillData.h"
15 // Input data and gold data for fillFromText test
16 #include "diamond_fill_data.h"
18 
19 
20 namespace qmcplusplus
21 {
22 void compute_batch_parameters(int sample_size, int batch_size, int& num_batches, int& final_batch_size);
23 
24 TEST_CASE("compute_batch_parameters", "[drivers]")
25 {
26  int sample_size = 1;
27  int batch_size = 1;
28 
29  int num_batches;
30  int final_batch_size;
31 
32  compute_batch_parameters(sample_size, batch_size, num_batches, final_batch_size);
33  CHECK(num_batches == 1);
34  CHECK(final_batch_size == 1);
35 
36 
37  sample_size = 11;
38  batch_size = 4;
39 
40  compute_batch_parameters(sample_size, batch_size, num_batches, final_batch_size);
41  CHECK(num_batches == 3);
42  CHECK(final_batch_size == 3);
43 
44  batch_size = 0;
45  compute_batch_parameters(sample_size, batch_size, num_batches, final_batch_size);
46  CHECK(num_batches == 0);
47  CHECK(final_batch_size == 0);
48 }
49 
50 namespace testing
51 {
53 {
54 public:
56  int numParam;
64 
65  LinearMethodTestSupport(const std::vector<int>& walkers_per_crowd, Communicate* comm)
66  : w(simulation_cell), costFn(w, psi, h, samples, walkers_per_crowd, comm)
67  {}
68 
69  std::vector<QMCCostFunctionBase::Return_rt>& getSumValue() { return costFn.SumValue; }
73 
74  void set_samples_and_param(int nsamples, int nparam)
75  {
76  numSamples = nsamples;
77  numParam = nparam;
78 
80 
81  for (int i = 0; i < nparam; i++)
82  {
83  std::string varname = "var" + std::to_string(i);
84  costFn.OptVariables.insert(varname, 1.0);
85  }
86 
88 
92  }
93 };
94 
95 } // namespace testing
96 
97 TEST_CASE("fillOverlapAndHamiltonianMatrices", "[drivers]")
98 {
99  std::vector<int> walkers_per_crowd{1};
100 
101  using Return_rt = qmcplusplus::QMCTraits::RealType;
102 
104 
105  testing::LinearMethodTestSupport lin(walkers_per_crowd, comm);
106 
107  int numSamples = 1;
108  int numParam = 1;
109  lin.set_samples_and_param(numSamples, numParam);
110 
111  std::vector<Return_rt>& SumValue = lin.getSumValue();
112  SumValue[QMCCostFunctionBase::SUM_WGT] = 1.0;
113  SumValue[QMCCostFunctionBase::SUM_E_WGT] = -1.3;
114  SumValue[QMCCostFunctionBase::SUM_ESQ_WGT] = 1.69;
115 
116  auto& RecordsOnNode = lin.getRecordsOnNode();
117  RecordsOnNode(0, QMCCostFunctionBase::REWEIGHT) = 1.0;
118  RecordsOnNode(0, QMCCostFunctionBase::ENERGY_NEW) = -1.4;
119 
120  auto& derivRecords = lin.getDerivRecords();
121  derivRecords(0, 0) = 1.1;
122 
123  auto& HDerivRecords = lin.getHDerivRecords();
124  HDerivRecords(0, 0) = -1.2;
125 
126  int N = numParam + 1;
128  Matrix<Return_rt> ovlp(N, N);
130 
131  CHECK(ovlp(0, 0) == Approx(1.0));
132  CHECK(ovlp(1, 0) == Approx(0.0));
133  CHECK(ovlp(0, 1) == Approx(0.0));
134  // With one sample, value is always zero
135  CHECK(ovlp(1, 1) == Approx(0.0));
136 
137  CHECK(ham(0, 0) == Approx(-1.3));
138  // With one sample, values are always zero
139  CHECK(ham(1, 0) == Approx(0.0));
140  CHECK(ham(0, 1) == Approx(-1.2));
141  CHECK(ham(1, 1) == Approx(0.0));
142 }
143 
144 // Test QMCCostFunctionBatched::fillOverlapHamiltonianMatrices
145 // Inputs are the number of crowds (threads) and
146 // the input/gold data (from a file created by convert_hdf_to_cpp.py)
147 void fill_from_text(int num_opt_crowds, FillData& fd)
148 {
149  std::vector<int> walkers_per_crowd(num_opt_crowds, 1);
150 
151  using Return_rt = qmcplusplus::QMCTraits::RealType;
152 
154 
155  testing::LinearMethodTestSupport lin(walkers_per_crowd, comm);
156 
157  int numSamples = fd.numSamples;
158  int numParam = fd.numParam;
159  lin.set_samples_and_param(numSamples, numParam);
160 
161  std::vector<Return_rt>& SumValue = lin.getSumValue();
162  SumValue[QMCCostFunctionBase::SUM_WGT] = fd.sum_wgt;
165 
166  auto& RecordsOnNode = lin.getRecordsOnNode();
167  for (int iw = 0; iw < numSamples; iw++)
168  {
169  RecordsOnNode(iw, QMCCostFunctionBase::REWEIGHT) = fd.reweight[iw];
170  RecordsOnNode(iw, QMCCostFunctionBase::ENERGY_NEW) = fd.energy_new[iw];
171  }
172 
173  auto& derivRecords = lin.getDerivRecords();
174  derivRecords = fd.derivRecords;
175 
176  auto& HDerivRecords = lin.getHDerivRecords();
177  HDerivRecords = fd.HDerivRecords;
178 
179  int N = numParam + 1;
181  Matrix<Return_rt> ovlp(N, N);
183 
184  for (int iw = 0; iw < numParam; iw++)
185  {
186  for (int iw2 = 0; iw2 < numParam; iw2++)
187  {
188  //app_log() << "iw = " << iw << " iw2 = " << iw2 << " ovlp = " << ovlp(iw,iw2) << " " << ovlp_gold(iw,iw2);
189  //app_log() << " ham = " << ham(iw,iw2) << " " << ham_gold(iw,iw2) << std::endl;
190  CHECK(ovlp(iw, iw2) == Approx(fd.ovlp_gold(iw, iw2)));
191  CHECK(ham(iw, iw2) == Approx(fd.ham_gold(iw, iw2)));
192  }
193  }
194 }
195 
196 
197 // Test fillOverlapHamiltonianMatrices function using gold data
198 // This can test the parallelization of that function using different numbers of crowds
199 TEST_CASE("fillfromText", "[drivers]")
200 {
201  FillData fd;
202 
203  // Generated from short-diamondC_1x1x1_pp-opt_sdj-1-16 with 1 thread and 10 samples
204  // And the bsplines sizes were reduced from 8 to 4 to give a total of 12 parameters
206 
207  // Test for 1 and 2 crowds (threads)
208  for (int num_opt_crowds = 1; num_opt_crowds < 3; num_opt_crowds++)
209  {
210  fill_from_text(num_opt_crowds, fd);
211  }
212 }
213 
214 
215 } // namespace qmcplusplus
Matrix< QMCCostFunctionBase::Return_rt > & getHDerivRecords()
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::RealType RealType
Definition: Configuration.h:58
TEST_CASE("complex_helper", "[type_traits]")
int NumOptimizables
total number of optimizable variables
Collection of Local Energy Operators.
opt_variables_type OptVariables
list of optimizables
Matrix< QMCCostFunctionBase::Return_rt > & getRecordsOnNode()
Matrix< double > HDerivRecords
Definition: FillData.h:30
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
Wrapping information on parallelism.
Definition: Communicate.h:68
void fill_from_text(int num_opt_crowds, FillData &fd)
void compute_batch_parameters(int sample_size, int batch_size, int &num_batches, int &final_batch_size)
Compute number of batches and final batch size given the number of samples and a batch size...
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Definition: VariableSet.h:133
Matrix< double > ham_gold
Definition: FillData.h:34
Matrix< double > derivRecords
Definition: FillData.h:29
LinearMethodTestSupport(const std::vector< int > &walkers_per_crowd, Communicate *comm)
Class to represent a many-body trial wave function.
std::vector< double > reweight
Definition: FillData.h:26
std::vector< Return_rt > SumValue
Sum of energies and weights for averages.
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
std::vector< QMCCostFunctionBase::Return_rt > & getSumValue()
Matrix< QMCCostFunctionBase::Return_t > & getDerivRecords()
Matrix< Return_t > DerivRecords_
Temp derivative properties and Hderivative properties of all the walkers.
Matrix< double > ovlp_gold
Definition: FillData.h:33
Return_rt fillOverlapHamiltonianMatrices(Matrix< Return_rt > &Left, Matrix< Return_rt > &Right) override
void get_diamond_fill_data(qmcplusplus::FillData &fd)
std::vector< double > energy_new
Definition: FillData.h:27