QMCPACK
LMYE_QMCCostFunction.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Luning Zhao, zhaoln@berkeley.edu, University of California, Berkeley
8 // Eric Neuscamman, eneuscamman@berkeley.edu, University of California, Berkeley
9 //
10 // File created by: Luning Zhao, zhaoln@berkeley.edu, University of California, Berkeley
11 //////////////////////////////////////////////////////////////////////////////////////
12 
16 #include "Message/CommOperators.h"
17 
18 //#include "Eigen/Dense"
19 #include "formic/utils/matrix.h"
20 #include "formic/utils/lmyengine/engine.h"
21 
22 namespace qmcplusplus
23 {
24 size_t QMCCostFunction::total_samples()
25 {
26  // for the unfamiliar, the [] starts a lambda function
27  return std::accumulate(wClones.begin(), wClones.begin() + NumThreads, size_t{0},
28  [](size_t x, const auto& p) { return x + p->numSamples(); });
29 }
30 
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32 /// \brief Computes the cost function using the LMYEngine
33 ///
34 ///////////////////////////////////////////////////////////////////////////////////////////////////
35 QMCCostFunction::Return_rt QMCCostFunction::LMYEngineCost_detail(cqmc::engine::LMYEngine<ValueType>* EngineObj)
36 {
37  // get total number of samples
38  const size_t m = this->total_samples();
39 
40  // reset Engine object
41  EngineObj->reset();
42 
43  // turn off wavefunction update mode
44  EngineObj->turn_off_update();
45 
46  //for (int ip = 0, j = 0; ip < NumThreads; ip++) {
47 #pragma omp parallel
48  {
49  int ip = omp_get_thread_num();
50  // for each thread, loop over samples
51  const int nw = wClones[ip]->numSamples();
52  for (int iw = 0; iw < nw; iw++)
53  {
54  // get a pointer to the record for this sample
55  const Return_rt* restrict saved = (*RecordsOnNode[ip])[iw];
56 
57  // record this sample's weight (normalized by division by the sum of weights I think...)
58  //wgt_vec.at(j) = saved[REWEIGHT] / SumValue[SUM_WGT];
59 
60  // record the sample's local energy
61  //lce_vec.at(j) = saved[ENERGY_NEW];
62 
63  // take sample
64  EngineObj->take_sample(saved[ENERGY_NEW], 1.0, saved[REWEIGHT] / SumValue[SUM_WGT]);
65  }
66  }
67  //}
68  // finish taking sample
69  EngineObj->sample_finish();
70 
71  // compute energy and target relevant quantities
72  EngineObj->energy_target_compute();
73 
74  // prepare variables to hold the output of the engine call
75  double energy_avg = EngineObj->energy_mean();
76  double energy_sdev = EngineObj->energy_sdev();
77  double energy_serr = EngineObj->energy_statistical_err();
78  double target_avg = EngineObj->target_value();
79  double target_serr = EngineObj->target_statistical_err();
80 
81  // prepare a stream to hold engine printout
82  //std::stringstream engine_out;
83 
84  // call the LMYEngine to compute the cost function
85  //cqmc::engine::call_engine(false, // exact_sampling
86  // !targetExcited, // ground_state
87  // false, // variance_correct
88  // true, // print
89  // omega_shift, // hd_lm_shift
90  // 0.0, // var_weight
91  // lce_vec, vgs_vec, wgt_vec,
92  // energy_avg, energy_sdev, energy_serr,
93  // target_avg, target_serr, engine_out);
94 
95  // print the engine output
96  //app_log() << engine_out.str();
97 
98  // return the cost function value (target function if we are targeting excited states and energy if we are doing groud state calculations)
99  double cost_value = (targetExcited ? target_avg : energy_avg);
100  return cost_value;
101 }
102 
103 } // namespace qmcplusplus
bool targetExcited
whether we are targeting an excited state
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
static std::vector< MCWalkerConfiguration * > wClones
Definition: CloneManager.h:61
omp_int_t omp_get_thread_num()
Definition: OpenMP.h:25
const IndexType NumThreads
number of threads
Definition: CloneManager.h:54
Declaration of a TrialWaveFunction.
std::vector< Return_rt > SumValue
Sum of energies and weights for averages.
Declaration of a MCWalkerConfiguration.
std::vector< Matrix< Return_rt > * > RecordsOnNode