QMCPACK
GradientTest.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 
13 #include "GradientTest.h"
15 
16 namespace qmcplusplus
17 {
18 
19 void GradientTest::run(QMCCostFunctionBase& costFunc, const std::string& root_name)
20 {
21  int num_params = costFunc.getNumParams();
22  std::vector<Return_t> numeric_grad(num_params);
23  std::vector<Return_t> params(num_params);
24  std::vector<Return_t> analytic_grad(num_params);
25 
26  for (int i = 0; i < num_params; i++)
27  {
28  params[i] = std::real(costFunc.Params(i));
29  }
30 
31 
32  if (input_.do_param_output() && first_)
33  {
34  int namelen = root_name.length();
35  // Assume that the root_name has the series suffix (".s000").
36  // Remove the series suffix to get the project id
37  std::string fname = root_name.substr(0, namelen - 5) + ".param.s000.scalar.dat";
38 
39  param_deriv_file_.open(fname);
40  param_deriv_file_ << "# Index ";
41  for (int i = 0; i < num_params; i++)
42  {
43  param_deriv_file_ << " " << costFunc.getParamName(i);
44  }
45  param_deriv_file_ << std::endl;
46  first_ = false;
47  }
48 
49  // Numerical gradient
50  costFunc.GradCost(numeric_grad, params, input_.get_finite_diff_delta());
51 
52  // Analytic gradient
53  costFunc.GradCost(analytic_grad, params);
54 
55 
56  if (input_.do_param_output())
57  {
60  }
61 
62  std::string_view param_name_header("Param_Name");
63  size_t max_name_len = param_name_header.size();
64  for (int k = 0; k < num_params; k++)
65  {
66  std::string vname = costFunc.getParamName(k);
67  max_name_len = std::max(vname.size(), max_name_len);
68  }
69  max_name_len += 2; // add some padding
70 
71  // clang-format off
72  app_log() << std::setw(max_name_len) << std::left << param_name_header
73  << std::setw(14) << std::right << " Value "
74  << std::setw(20) << std::right << " Numeric "
75  << std::setw(20) << std::right << " Analytic "
76  << std::setw(14) << " Percent" << std::endl;
77  // clang-format on
78 
79  for (int k = 0; k < num_params; k++)
80  {
81  std::string vname = costFunc.getParamName(k);
82  std::ostringstream rel_diff_str;
83  std::string over_threshold;
84  if (numeric_grad[k] != 0)
85  {
86  double rel_diff_percent = 100 * (numeric_grad[k] - analytic_grad[k]) / numeric_grad[k];
87  rel_diff_str << std::scientific << std::setprecision(2) << rel_diff_percent;
88  // Highlight problematic differences
89  // The thresholds are arbitrary.
90  if (std::abs(rel_diff_percent) > 1e-3)
91  over_threshold = " !";
92  if (std::abs(rel_diff_percent) > 1e-2)
93  over_threshold = " !!";
94  if (std::abs(rel_diff_percent) > 1e-1)
95  over_threshold = " !!!";
96  }
97  else
98  {
99  rel_diff_str << "inf";
100  over_threshold = " !!!";
101  }
102 
103 
104  // clang-format off
105  app_log() << std::setw(max_name_len) << std::left << vname
106  << std::setprecision(6) << std::setw(14) << std::right << params[k]
107  << std::setprecision(10) << std::setw(20) << std::right << numeric_grad[k]
108  << std::setw(20) << std::right << analytic_grad[k]
109  << std::setw(14) << std::right << rel_diff_str.str() << over_threshold << std::endl;
110  // clang-format on
111 
112  if (input_.do_param_output())
113  param_deriv_file_ << std::setprecision(10) << analytic_grad[k] << " ";
114  }
115 
116  // Reset precision to the default
117  app_log() << std::setprecision(6);
118 
119  if (input_.do_param_output())
120  param_deriv_file_ << std::endl;
121 
122  app_log() << std::endl;
123 }
124 
125 } // namespace qmcplusplus
void run(QMCCostFunctionBase &costFunc, const std::string &root_name)
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::string getParamName(int i) const override
void GradCost(std::vector< Return_rt > &PGradient, const std::vector< Return_rt > &PM, Return_rt FiniteDiff=0) override
return the gradient of cost value for CGMinimization
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
std::ostream & app_log()
Definition: OutputManager.h:65
Tests for variational parameter derivatives.
Implements wave-function optimization.
Return_rt & Params(int i) override
assign optimization parameter i
int getNumParams() const override
return the number of optimizable parameters
GradientTestInput input_
Definition: GradientTest.h:63
std::ofstream param_deriv_file_
Definition: GradientTest.h:65