33 app_log() <<
" Using QMCCostFunction::QMCCostFunction" << std::endl;
46 const std::vector<Return_rt>& PM,
60 PGradient[i] = (CostPlus - CostMinus) * dh;
80 Return_rt delE_bar = 0;
83 int nw =
wClones[ip]->numSamples();
84 for (
int iw = 0; iw < nw; iw++)
87 Return_rt weight = saved[
REWEIGHT] * wgtinv;
92 HD_avg[
pm] += HDsaved[
pm];
101 int nw =
wClones[ip]->numSamples();
102 for (
int iw = 0; iw < nw; iw++)
105 Return_rt weight = saved[
REWEIGHT] * wgtinv;
107 Return_rt delta_l = (eloc_new -
curAvg_w);
117 EDtotals_w[
pm] += weight * (HDsaved[
pm] + 2.0 *
std::real(Dsaved[
pm]) * delta_l);
118 URV[
pm] += 2.0 * (eloc_new * HDsaved[
pm] -
curAvg * HD_avg[
pm]);
120 EDtotals[
pm] += weight * (2.0 *
std::real(Dsaved[
pm]) * (delE - delE_bar) + ddelE * HDsaved[
pm]);
122 EDtotals[
pm] += weight * (2.0 *
std::real(Dsaved[
pm]) * (delE - delE_bar) - ddelE * HDsaved[
pm]);
129 Return_rt smpinv = 1.0 /
static_cast<Return_rt
>(
NumSamples);
132 int nw =
wClones[ip]->numSamples();
133 for (
int iw = 0; iw < nw; iw++)
136 Return_rt weight = saved[
REWEIGHT] * wgtinv;
138 Return_rt delta_l = (eloc_new -
curAvg_w);
139 Return_rt sigma_l = delta_l * delta_l;
156 PGradient[j] +=
w_var * E2Dtotals_w[j];
158 PGradient[j] +=
w_en * EDtotals_w[j];
160 PGradient[j] +=
w_w * URV[j];
162 PGradient[j] +=
w_abs * EDtotals[j];
175 app_log() <<
" QMCCostFunction is created with " <<
NumThreads <<
" threads." << std::endl;
187 auto components =
hClones[ip]->getTWFDependentComponents();
190 app_log() <<
" Found " << components.size() <<
" wavefunction dependent components in the Hamiltonian";
191 if (components.size())
193 app_log() <<
" '" << component.getName() <<
"'";
196 H_KE_Node[ip] = std::make_unique<HamiltonianRef>(components);
200 app_log() <<
" Number of samples loaded to each thread : ";
218 for (
int i = 0; i < nwtot; ++i)
220 for (
int i = 0; i < nwtot; ++i)
230 #pragma omp parallel reduction(+ : et_tot, e2_tot) 295 etmp =
hClones[ip]->evaluate(wRef);
300 const auto twf_dependent_components =
hClones[ip]->getTWFDependentComponents();
301 for (
const OperatorBase& component : twf_dependent_components)
313 std::vector<Return_rt> etemp(3);
318 Etarget =
static_cast<Return_rt
>(etemp[0] / etemp[1]);
322 app_log() <<
" Total weights = " << etemp[1] << std::endl;
337 #ifdef HAVE_LMY_ENGINE 341 void QMCCostFunction::engine_checkConfigurations(cqmc::engine::LMYEngine<Return_t>* EngineObj,
343 const std::string& MinMethod)
345 if (MinMethod ==
"descent")
354 #pragma omp parallel reduction(+ : et_tot, e2_tot) 357 MCWalkerConfiguration& wRef(*
wClones[ip]);
391 for (
int iw = 0, iwg =
wPerRank[ip]; iw < wRef.numSamples(); ++iw, ++iwg)
393 wRef.loadSample(wRef, iw);
413 der_rat_samp.at(0) = 1.0;
414 for (
int i = 0; i < Dsaved.size(); i++)
415 der_rat_samp[i + 1] = Dsaved[i];
418 le_der_samp.at(0) = etmp;
419 for (
int i = 0; i < HDsaved.size(); i++)
420 le_der_samp[i + 1] = HDsaved[i] + etmp * Dsaved[i];
422 #ifdef HAVE_LMY_ENGINE 423 if (MinMethod ==
"adaptive")
426 EngineObj->take_sample(der_rat_samp, le_der_samp, le_der_samp, 1.0, saved[
REWEIGHT]);
428 else if (MinMethod ==
"descent")
432 std::vector<FullPrecValueType> der_rat_samp_comp(der_rat_samp.begin(), der_rat_samp.end());
433 std::vector<FullPrecValueType> le_der_samp_comp(le_der_samp.begin(), le_der_samp.end());
435 descentEngineObj.
takeSample(ip, der_rat_samp_comp, le_der_samp_comp, le_der_samp_comp, 1.0, saved[
REWEIGHT]);
440 etmp =
hClones[ip]->evaluate(wRef);
446 const auto twf_dependent_components =
hClones[ip]->getTWFDependentComponents();
447 for (
const OperatorBase& component : twf_dependent_components)
460 std::vector<Return_rt> etemp(3);
465 Etarget =
static_cast<Return_rt
>(etemp[0] / etemp[1]);
469 app_log() <<
" Total weights = " << etemp[1] << std::endl;
472 #ifdef HAVE_LMY_ENGINE 474 if (MinMethod ==
"adaptive")
476 EngineObj->sample_finish();
478 if (EngineObj->block_first())
481 app_log() <<
"calling setComputed function" << std::endl;
484 else if (MinMethod ==
"descent")
511 for (
int i = 0; i <
psiClones.size(); ++i)
524 Return_rt wgt_tot = 0.0;
525 Return_rt wgt_tot2 = 0.0;
527 #pragma omp parallel reduction(+ : wgt_tot, wgt_tot2) 531 const bool compute_all_from_scratch =
hClones[ip]->getTWFDependentComponents().size() > 1;
534 Return_rt wgt_node = 0.0, wgt_node2 = 0.0;
541 logpsi =
psiClones[ip]->evaluateDeltaLog(wRef, compute_all_from_scratch);
572 wgt_node += inv_n_samples * weight;
573 wgt_node2 += inv_n_samples * weight * weight;
576 wgt_tot2 += wgt_node2;
584 Return_rt wgtnorm = (wgt_tot == 0) ? 0 : wgt_tot;
588 int nw =
wClones[ip]->numSamples();
589 for (
int iw = 0; iw < nw; iw++)
594 wgt_tot += inv_n_samples * saved[
REWEIGHT];
599 wgtnorm = (wgt_tot == 0) ? 1 : 1.0 / wgt_tot;
603 int nw =
wClones[ip]->numSamples();
604 for (
int iw = 0; iw < nw; iw++)
608 wgt_tot += inv_n_samples * saved[
REWEIGHT];
613 for (
int i = 0; i <
SumValue.size(); i++)
615 CSWeight = wgt_tot = (wgt_tot == 0) ? 1 : 1.0 / wgt_tot;
618 int nw =
wClones[ip]->numSamples();
619 for (
int iw = 0; iw < nw; iw++)
659 int nw =
wClones[ip]->numSamples();
660 for (
int iw = 0; iw < nw; iw++)
663 Return_rt weight = saved[
REWEIGHT] * wgtinv;
667 D_avg[
pm] += Dsaved[
pm] * weight;
676 int nw =
wClones[ip]->numSamples();
677 for (
int iw = 0; iw < nw; iw++)
680 Return_rt weight = saved[
REWEIGHT] * wgtinv;
684 #pragma omp parallel for 687 Return_t wfe = (HDsaved[
pm] + (Dsaved[
pm] - D_avg[
pm]) * eloc_new) * weight;
697 Left(
pm + 1, 0) += (1 - b2) *
std::real(wfd) * eloc_new;
701 Left(
pm + 1, pm2 + 1) +=
702 std::real((1 - b2) *
std::conj(wfd) * (HDsaved[pm2] + (Dsaved[pm2] - D_avg[pm2]) * eloc_new));
705 Right(
pm + 1, pm2 + 1) += ovlij;
709 (HDsaved[pm2] -
RealType(2.0) * (Dsaved[pm2] - D_avg[pm2]) * eloc_new));
712 Left(
pm + 1, pm2 + 1) += b2 * (varij + V_avg * ovlij);
719 Left(0, 0) = (1 - b2) *
curAvg_w + b2 * V_avg;
Return_rt Cost(bool needGrad=true) override
return the cost value for CGMinimization
Return_rt Etarget
target energy
std::vector< ParticleLaplacian * > d2LogPsi
Fixed Laplacian , , components.
bool recompute(int i) const
A set of walkers that are to be advanced by Metropolis Monte Carlo.
static std::vector< TrialWaveFunction * > psiClones
void delete_iter(IT first, IT last)
delete the pointers in [first,last)
void checkConfigurations(EngineHandle &handle) override
evaluate everything before optimization
std::vector< int > wPerRank
Walkers per MPI rank.
helper functions for EinsplineSetBuilder
int NumSamples
global number of samples to use in correlated sampling
QTBase::RealType RealType
void sample_finish()
Function that reduces all vector information from all processors to the root processor.
void GradCost(std::vector< Return_rt > &PGradient, const std::vector< Return_rt > &PM, Return_rt FiniteDiff=0) override
~QMCCostFunction() override
Destructor.
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
size_t getTotalNum() const
static std::vector< MCWalkerConfiguration * > wClones
std::vector< TinyVector< int, 2 > > equalVarMap
index mapping for <equal> constraints
Return_rt fillOverlapHamiltonianMatrices(Matrix< Return_rt > &Left, Matrix< Return_rt > &Right) override
int NumOptimizables
total number of optimizable variables
Collection of Local Energy Operators.
EffectiveWeight correlatedSampling(bool needGrad=true) override
run correlated sampling return effective walkers ( w_i)^2/(Nw * w^2_i)
QMCTraits::QTFull::RealType EffectiveWeight
Return_rt curAvg_w
current weighted average (correlated sampling)
opt_variables_type OptVariables
list of optimizables
UPtrVector< RandomBase< FullPrecRealType > > RngSaved
Random number generators.
void update(bool skipSK=false)
update the internal data
ParticleSet::ParticleLaplacian ParticleLaplacian
Communicate * Controller
Global Communicator for a process.
ParticleSet::ParticleGradient ParticleGradient
Saved derivative properties and Hderivative properties of all the walkers.
ParticleLaplacian L
laplacians of the particles
std::vector< std::unique_ptr< HamiltonianRef > > H_KE_Node
void prepareStorage(const int num_replicas, const int num_optimizables)
Prepare for taking samples.
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Wrapping information on parallelism.
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
omp_int_t omp_get_thread_num()
std::vector< ParticleGradient * > dLogPsi
Fixed Gradients , , components.
omp_int_t omp_get_max_threads()
Communicate * myComm
pointer to Communicate
std::vector< Matrix< Return_t > * > DerivRecords
Temp derivative properties and Hderivative properties of all the walkers.
Return_rt curAvg
current Average
ParticleGradient G
gradients of the particles
NewTimer & createGlobalTimer(const std::string &myname, timer_levels mylevel)
opt_variables_type OptVariablesForPsi
full list of optimizables
const IndexType NumThreads
number of threads
void resetPsi(bool final_reset=false) override
reset the wavefunction
Return_rt w_en
weights for energy and variance in the cost function
Implements wave-function optimization.
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
ParticleSet & W
Particle set.
bool isEffectiveWeightValid(EffectiveWeight effective_weight) const
check the validity of the effective weight calculated by correlatedSampling
void getConfigurations(const std::string &aroot) override
size_type size() const
return the size
float conj(const float &c)
Workaround to allow conj on scalar to return real instead of complex.
Declaration of a TrialWaveFunction.
int ReportCounter
counter for output
static std::vector< QMCHamiltonian * > hClones
An abstract class for Local Energy operators.
QMCTraits::RealType RealType
Class to represent a many-body trial wave function.
std::vector< Return_rt > SumValue
Sum of energies and weights for averages.
void resetOptimizableObjects(TrialWaveFunction &psi, const opt_variables_type &opt_variables) const
TrialWaveFunction & Psi
Trial function.
int getNumParams() const override
return the number of optimizable parameters
QMCCostFunction(MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, Communicate *comm)
Constructor.
void loadSample(ParticleSet &pset, size_t iw) const
load a single sample from SampleStack
Return_rt MaxWeight
maximum weight beyond which the weight is set to 1
std::vector< Matrix< Return_rt > * > HDerivRecords
int PowerE
|E-E_T|^PowerE is used for the cost function
void takeSample(const int replica_id, const std::vector< FullPrecValueType > &der_rat_samp, const std::vector< FullPrecValueType > &le_der_samp, const std::vector< FullPrecValueType > &ls_der_samp, ValueType vgs_samp, ValueType weight_samp)
Function that Take Sample Data from the Host Code.
Return_rt curVar_w
current weighted variance (correlated sampling)
Declaration of a MCWalkerConfiguration.
std::vector< Matrix< Return_rt > * > RecordsOnNode
BareKineticEnergy::Return_t Return_t
void setTargetEnergy(Return_rt et)
Return_rt EtargetEff
real target energy with the Correlation Factor
std::vector< RandomBase< FullPrecRealType > * > MoverRng