QMCPACK
LMYE_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) 2022 QMCPACK developers.
6 //
7 // File developed by: Leon Otis, leon_otis@berkeley.edu, University of California, Berkeley
8 //
9 // File created by: Leon Otis, leon_otis@berkeley.edu, University of California, Berkeley
10 //////////////////////////////////////////////////////////////////////////////////////
11 
15 #include "Message/CommOperators.h"
16 
17 #include "formic/utils/matrix.h"
18 #include "formic/utils/lmyengine/engine.h"
19 
20 namespace qmcplusplus
21 {
22 size_t QMCCostFunctionBatched::total_samples() { return samples_.getGlobalNumSamples(); }
23 
24 ///////////////////////////////////////////////////////////////////////////////////////////////////
25 /// \brief Computes the cost function using the LMYEngine for interfacing with batched driver
26 ///
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
28 QMCCostFunctionBatched::Return_rt QMCCostFunctionBatched::LMYEngineCost_detail(
29  cqmc::engine::LMYEngine<Return_t>* EngineObj)
30 {
31  // get total number of samples
32  const size_t m = this->total_samples();
33  // reset Engine object
34  EngineObj->reset();
35 
36  // turn off wavefunction update mode
37  EngineObj->turn_off_update();
38 
39 #pragma omp parallel
40  {
41  int ip = omp_get_thread_num();
42  // for each thread, loop over samples
43  const int nw = rank_local_num_samples_;
44  for (int iw = 0; iw < nw; iw++)
45  {
46  // get a pointer to the record for this sample
47  const Return_rt* restrict saved = RecordsOnNode_[iw];
48 
49  // take sample
50  EngineObj->take_sample(saved[ENERGY_NEW], 1.0, saved[REWEIGHT] / SumValue[SUM_WGT]);
51  }
52  }
53  //}
54  // finish taking sample
55  EngineObj->sample_finish();
56 
57  // compute energy and target relevant quantities
58  EngineObj->energy_target_compute();
59 
60  // prepare variables to hold the output of the engine call
61  double energy_avg = EngineObj->energy_mean();
62  double energy_sdev = EngineObj->energy_sdev();
63  double energy_serr = EngineObj->energy_statistical_err();
64  double target_avg = EngineObj->target_value();
65  double target_serr = EngineObj->target_statistical_err();
66 
67 
68  // return the cost function value (target function if we are targeting excited states and energy if we are doing groud state calculations)
69  double cost_value = (targetExcited ? target_avg : energy_avg);
70  return cost_value;
71 }
72 
73 } // namespace qmcplusplus
bool targetExcited
whether we are targeting an excited state
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
omp_int_t omp_get_thread_num()
Definition: OpenMP.h:25
Declaration of a TrialWaveFunction.
std::vector< Return_rt > SumValue
Sum of energies and weights for averages.
size_t getGlobalNumSamples() const
Global number of samples is number of samples per rank * number of ranks.
Definition: SampleStack.h:45
Declaration of a MCWalkerConfiguration.