From a6d3e61424915566412ad8bdbd22dc2413dd0bf2 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Thu, 20 Jan 2022 19:46:43 -0700 Subject: [PATCH 01/34] add abstraction layer in batched qmc --- .../ParticleBase/RandomSeqGenerator.h | 6 + src/QMCDrivers/MoveAbstraction.h | 185 ++++++++++++++++++ src/QMCDrivers/QMCDriverNew.h | 5 + src/QMCDrivers/VMC/VMCBatched.cpp | 70 +++---- src/QMCDrivers/VMC/VMCBatched.h | 1 + 5 files changed, 220 insertions(+), 47 deletions(-) create mode 100644 src/QMCDrivers/MoveAbstraction.h diff --git a/src/Particle/ParticleBase/RandomSeqGenerator.h b/src/Particle/ParticleBase/RandomSeqGenerator.h index b79ba65ba7..5928ed9f0b 100644 --- a/src/Particle/ParticleBase/RandomSeqGenerator.h +++ b/src/Particle/ParticleBase/RandomSeqGenerator.h @@ -82,6 +82,12 @@ inline void makeGaussRandomWithEngine(ParticleAttrib& a, RG& rng) assignGaussRand(&(a[0]), a.size(), rng); } +template +inline void makeGaussRandomWithEngine(std::vector& a, RG& rng) +{ + assignGaussRand(&(a[0]), a.size(), rng); +} + } // namespace qmcplusplus diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h new file mode 100644 index 0000000000..41d21a0816 --- /dev/null +++ b/src/QMCDrivers/MoveAbstraction.h @@ -0,0 +1,185 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +// +// File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +////////////////////////////////////////////////////////////////////////////////////// + +#ifndef QMCPLUSPLUS_MOVEABSTRACTION_H +#define QMCPLUSPLUS_MOVEABSTRACTION_H + +#include "QMCDriverNew.h" +#include "Particle/PSdispatcher.h" +#include "Particle/ParticleSet.h" +#include "QMCWaveFunctions/TWFdispatcher.h" +#include "QMCWaveFunctions/TrialWaveFunction.h" +#include "ParticleBase/RandomSeqGenerator.h" +#include "QMCDriverInput.h" +#include "type_traits/RefVectorWithLeader.h" +#include "QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h" +#include "Crowd.h" + +namespace qmcplusplus +{ +template +class MoveAbstraction +{ + using Pos = ParticleSet::PosType; + using Real = ParticleSet::RealType; + using Grad = TrialWaveFunction::GradType; + using PsiV = TrialWaveFunction::PsiValueType; + +public: + MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles); + + void generateDeltas(); + + void setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass); + + void calcForwardMoveWithDrift(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat); + + void calcForwardMove(const int iat); + + void makeMove(const RefVectorWithLeader& elecs, const int iat); + + void updateGreensFunctionWithDrift(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + Crowd& crowd, + const int iat, + std::vector& ratios, + std::vector& log_gf, + std::vector& log_bg); + + void updateGreensFunction(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat, + std::vector& ratios); + +private: + std::vector drifts_; + std::vector walker_deltas_; + std::vector grads_now_, grads_new_; + const PSdispatcher& ps_dispatcher_; + const TWFdispatcher& twf_dispatcher_; + RandomGenerator& random_gen_; + const DriftModifierBase& drift_modifier_; + Real tauovermass_; + Real oneover2tau_; + Real sqrttau_; + const int num_walkers_; +}; + +template<> +MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) + : ps_dispatcher_(ps_dispatcher), + twf_dispatcher_(twf_dispatcher), + random_gen_(random_gen), + drift_modifier_(drift_modifier), + num_walkers_(num_walkers) +{ + drifts_.resize(num_walkers_); + walker_deltas_.resize(num_walkers_ * num_particles); + grads_now_.resize(num_walkers_); + grads_new_.resize(num_walkers_); +} + +template<> +void MoveAbstraction::generateDeltas() +{ + makeGaussRandomWithEngine(walker_deltas_, random_gen_); +} + +template<> +void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) +{ + tauovermass_ = qmcdrv_input.get_tau() * invmass; + oneover2tau_ = 0.5 / tauovermass_; + sqrttau_ = std::sqrt(tauovermass_); +} + +template<> +void MoveAbstraction::calcForwardMoveWithDrift( + const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + + twf_dispatcher_.flex_evalGrad(twfs, elecs, iat, grads_now_); + drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); + std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), + [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); +} + +template<> +void MoveAbstraction::calcForwardMove(const int iat) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + + std::transform(delta_r_start, delta_r_end, drifts_.begin(), + [st = sqrttau_](const Pos& delta_r) { return st * delta_r; }); +} + +template<> +void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) +{ + ps_dispatcher_.flex_makeMove(elecs, iat, drifts_); +} + +template<> +void MoveAbstraction::updateGreensFunctionWithDrift( + const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + Crowd& crowd, + const int iat, + std::vector& ratios, + std::vector& log_gf, + std::vector& log_gb) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + + twf_dispatcher_.flex_calcRatioGrad(twfs, elecs, iat, ratios, grads_new_); + std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { + constexpr Real mhalf(-0.5); + return mhalf * dot(delta_r, delta_r); + }); + drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); + + std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts_.begin(), drifts_.begin(), + [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); + + std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), + [halfovertau = oneover2tau_](const Pos& drift) { return -halfovertau * dot(drift, drift); }); +} + +template<> +void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat, + std::vector& ratios) +{ + twf_dispatcher_.flex_calcRatio(twfs, elecs, iat, ratios); +} + +} // namespace qmcplusplus + +#endif diff --git a/src/QMCDrivers/QMCDriverNew.h b/src/QMCDrivers/QMCDriverNew.h index 7042070e8f..23226a0abc 100644 --- a/src/QMCDrivers/QMCDriverNew.h +++ b/src/QMCDrivers/QMCDriverNew.h @@ -86,6 +86,11 @@ class QMCDriverNew : public QMCDriverInterface, public MPIObjectBase QMC_WARMUP }; + enum CoordsToMove + { + POSITIONS, + }; + using MCPWalker = MCPopulation::MCPWalker; using WFBuffer = MCPopulation::WFBuffer; diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 8798cce367..43efb5280c 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -18,6 +18,7 @@ #include "ParticleBase/RandomSeqGenerator.h" #include "Particle/MCSample.h" #include "MemoryUsage.h" +#include "MoveAbstraction.h" namespace qmcplusplus { @@ -35,6 +36,7 @@ VMCBatched::VMCBatched(const ProjectData& project_data, collect_samples_(false) {} +template void VMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, QMCDriverNew::DriverTimers& timers, @@ -66,11 +68,8 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector moved(num_walkers, false); constexpr RealType mhalf(-0.5); const bool use_drift = sft.vmcdrv_input.get_use_drift(); - std::vector grads_now(num_walkers); - std::vector grads_new(num_walkers); - std::vector ratios(num_walkers); - std::vector drifts(num_walkers); + std::vector ratios(num_walkers); std::vector log_gf(num_walkers); std::vector log_gb(num_walkers); std::vector prob(num_walkers); @@ -80,17 +79,18 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector> twf_accept_list, twf_reject_list; isAccepted.reserve(num_walkers); + MoveAbstraction move(ps_dispatcher, twf_dispatcher, step_context.get_random_gen(), sft.drift_modifier, + num_walkers, sft.population.get_num_particles()); + for (int sub_step = 0; sub_step < sft.qmcdrv_input.get_sub_steps(); sub_step++) { //This generates an entire steps worth of deltas. - step_context.nextDeltaRs(num_walkers * sft.population.get_num_particles()); + move.generateDeltas(); // up and down electrons are "species" within qmpack for (int ig = 0; ig < step_context.get_num_groups(); ++ig) //loop over species { - RealType tauovermass = sft.qmcdrv_input.get_tau() * sft.population.get_ptclgrp_inv_mass()[ig]; - RealType oneover2tau = 0.5 / (tauovermass); - RealType sqrttau = std::sqrt(tauovermass); + move.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -98,50 +98,18 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, int end_index = step_context.getPtclGroupEnd(ig); for (int iat = start_index; iat < end_index; ++iat) { - // step_context.deltaRsBegin returns an iterator to a flat series of PosTypes - // fastest in walkers then particles - auto delta_r_start = step_context.deltaRsBegin() + iat * num_walkers; - auto delta_r_end = delta_r_start + num_walkers; - if (use_drift) - { - twf_dispatcher.flex_evalGrad(walker_twfs, walker_elecs, iat, grads_now); - sft.drift_modifier.getDrifts(tauovermass, grads_now, drifts); - - std::transform(drifts.begin(), drifts.end(), delta_r_start, drifts.begin(), - [sqrttau](const PosType& drift, const PosType& delta_r) { - return drift + (sqrttau * delta_r); - }); - } + move.calcForwardMoveWithDrift(walker_twfs, walker_elecs, iat); else - { - std::transform(delta_r_start, delta_r_end, drifts.begin(), - [sqrttau](const PosType& delta_r) { return sqrttau * delta_r; }); - } + move.calcForwardMove(iat); - ps_dispatcher.flex_makeMove(walker_elecs, iat, drifts); + move.makeMove(walker_elecs, iat); // This is inelegant if (use_drift) - { - twf_dispatcher.flex_calcRatioGrad(walker_twfs, walker_elecs, iat, ratios, grads_new); - std::transform(delta_r_start, delta_r_end, log_gf.begin(), - [](const PosType& delta_r) { return mhalf * dot(delta_r, delta_r); }); - - sft.drift_modifier.getDrifts(tauovermass, grads_new, drifts); - - std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts.begin(), drifts.begin(), - [iat](const ParticleSet& elecs, const PosType& drift) { - return elecs.R[iat] - elecs.getActivePos() - drift; - }); - - std::transform(drifts.begin(), drifts.end(), log_gb.begin(), - [oneover2tau](const PosType& drift) { return -oneover2tau * dot(drift, drift); }); - } + move.updateGreensFunctionWithDrift(walker_twfs, walker_elecs, crowd, iat, ratios, log_gf, log_gb); else - { - twf_dispatcher.flex_calcRatio(walker_twfs, walker_elecs, iat, ratios); - } + move.updateGreensFunction(walker_twfs, walker_elecs, iat, ratios); std::transform(ratios.begin(), ratios.end(), prob.begin(), [](auto ratio) { return std::norm(ratio); }); @@ -215,6 +183,12 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, // check if all moves failed } +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); /** Thread body for VMC step * @@ -233,7 +207,9 @@ void VMCBatched::runVMCStep(int crowd_id, const bool recompute_this_step = (sft.is_recomputing_block && (step + 1) == max_steps); // For VMC we don't call this method for warmup steps. const bool accumulate_this_step = true; - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, accumulate_this_step); + const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); } void VMCBatched::process(xmlNodePtr node) @@ -304,7 +280,7 @@ bool VMCBatched::run() Crowd& crowd = *(crowds[crowd_id]); const bool recompute = false; const bool accumulate_this_step = false; - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); }; for (int step = 0; step < qmcdriver_input_.get_warmup_steps(); ++step) diff --git a/src/QMCDrivers/VMC/VMCBatched.h b/src/QMCDrivers/VMC/VMCBatched.h index 78380eddb4..0080f3f6cc 100644 --- a/src/QMCDrivers/VMC/VMCBatched.h +++ b/src/QMCDrivers/VMC/VMCBatched.h @@ -80,6 +80,7 @@ class VMCBatched : public QMCDriverNew * MCWalkerConfiguration layer removed. * Obfuscation of state changes via buffer and MCWalkerconfiguration require this be tested well */ + template static void advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, From b1c36c002ed195eda16ab4b420ba9ae4dbee6699 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 24 Jan 2022 14:08:42 -0700 Subject: [PATCH 02/34] add spin moves to abstraction --- .../DriftModifierBase.h | 4 + .../DriftModifierUNR.cpp | 16 +- .../GreenFunctionModifiers/DriftModifierUNR.h | 4 + src/QMCDrivers/MoveAbstraction.h | 155 +++++++++++++++++- src/QMCDrivers/QMCDriverInput.cpp | 1 + src/QMCDrivers/QMCDriverInput.h | 2 + src/QMCDrivers/QMCDriverNew.h | 3 + 7 files changed, 174 insertions(+), 11 deletions(-) diff --git a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h index cb70d532cc..65311b347d 100644 --- a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h +++ b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h @@ -40,6 +40,10 @@ class DriftModifierBase virtual void getDrifts(RealType tau, const std::vector& qf, std::vector&) const = 0; + virtual void getDrifts(RealType tau, + const std::vector& qf, + std::vector&) const = 0; + virtual bool parseXML(xmlNodePtr cur) { return true; } virtual ~DriftModifierBase() {} diff --git a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.cpp b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.cpp index 82422eb4c6..21d20577c3 100644 --- a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.cpp +++ b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.cpp @@ -35,10 +35,10 @@ void DriftModifierUNR::getDrift(RealType tau, const GradType& qf, PosType& drift // Generally we hope that this would only occur as the result of bad input // which would hopefully be the result of development time error and // therefore caught when run in a Debug build. - if( std::isnan(vsq) ) + if (std::isnan(vsq)) { std::ostringstream error_message; - for(int i = 0; i < drift.size(); ++i) + for (int i = 0; i < drift.size(); ++i) { if (std::isnan(drift[i])) { @@ -66,7 +66,7 @@ void DriftModifierUNR::getDrift(RealType tau, const ComplexType& qf, ParticleSet // Generally we hope that this would only occur as the result of bad input // which would hopefully be the result of development time error and // therefore caught when run in a Debug build. - if( std::isnan(vsq) ) + if (std::isnan(vsq)) { std::ostringstream error_message; if (std::isnan(drift)) @@ -90,6 +90,16 @@ void DriftModifierUNR::getDrifts(RealType tau, const std::vector& qf, } } +void DriftModifierUNR::getDrifts(RealType tau, + const std::vector& qf, + std::vector& drift) const +{ + for (int i = 0; i < qf.size(); ++i) + { + getDrift(tau, qf[i], drift[i]); + } +} + bool DriftModifierUNR::parseXML(xmlNodePtr cur) { ParameterSet m_param; diff --git a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.h b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.h index 64a578007b..f0c5a28569 100644 --- a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.h +++ b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierUNR.h @@ -27,6 +27,10 @@ class DriftModifierUNR : public DriftModifierBase void getDrift(RealType tau, const GradType& qf, PosType& drift) const final; + void getDrifts(RealType tau, + const std::vector& qf, + std::vector& drift) const final; + void getDrift(RealType tau, const ComplexType& qf, ParticleSet::Scalar_t& drift) const final; bool parseXML(xmlNodePtr cur) final; diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 41d21a0816..33bcadf6fe 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -28,10 +28,12 @@ namespace qmcplusplus template class MoveAbstraction { - using Pos = ParticleSet::PosType; - using Real = ParticleSet::RealType; - using Grad = TrialWaveFunction::GradType; - using PsiV = TrialWaveFunction::PsiValueType; + using Pos = ParticleSet::PosType; + using Scalar = ParticleSet::Scalar_t; + using Real = ParticleSet::RealType; + using Grad = TrialWaveFunction::GradType; + using Complex = TrialWaveFunction::ComplexType; + using PsiV = TrialWaveFunction::PsiValueType; public: MoveAbstraction(const PSdispatcher& ps_dispatcher, @@ -70,6 +72,9 @@ class MoveAbstraction std::vector drifts_; std::vector walker_deltas_; std::vector grads_now_, grads_new_; + std::vector spindrifts_; + std::vector walker_spindeltas_; + std::vector spingrads_now_, spingrads_new_; const PSdispatcher& ps_dispatcher_; const TWFdispatcher& twf_dispatcher_; RandomGenerator& random_gen_; @@ -77,6 +82,9 @@ class MoveAbstraction Real tauovermass_; Real oneover2tau_; Real sqrttau_; + Real spintauovermass_; + Real oneover2spintau_; + Real sqrtspintau_; const int num_walkers_; }; @@ -99,12 +107,42 @@ MoveAbstraction::MoveAbstraction(const PSdispatcher& ps grads_new_.resize(num_walkers_); } +template<> +MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) + : ps_dispatcher_(ps_dispatcher), + twf_dispatcher_(twf_dispatcher), + random_gen_(random_gen), + drift_modifier_(drift_modifier), + num_walkers_(num_walkers) +{ + drifts_.resize(num_walkers_); + walker_deltas_.resize(num_walkers_ * num_particles); + grads_now_.resize(num_walkers_); + grads_new_.resize(num_walkers_); + spindrifts_.resize(num_walkers_); + walker_spindeltas_.resize(num_walkers_ * num_particles); + spingrads_now_.resize(num_walkers_); + spingrads_new_.resize(num_walkers_); +} + template<> void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); } +template<> +void MoveAbstraction::generateDeltas() +{ + makeGaussRandomWithEngine(walker_deltas_, random_gen_); + makeGaussRandomWithEngine(walker_spindeltas_, random_gen_); +} + template<> void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { @@ -113,6 +151,18 @@ void MoveAbstraction::setTauForGroup(const QMCDriverInp sqrttau_ = std::sqrt(tauovermass_); } +template<> +void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, + const Real& invmass) +{ + tauovermass_ = qmcdrv_input.get_tau() * invmass; + oneover2tau_ = 0.5 / tauovermass_; + sqrttau_ = std::sqrt(tauovermass_); + spintauovermass_ = tauovermass_ / qmcdrv_input.get_spin_mass(); + oneover2spintau_ = 0.5 / spintauovermass_; + sqrtspintau_ = std::sqrt(spintauovermass_); +} + template<> void MoveAbstraction::calcForwardMoveWithDrift( const RefVectorWithLeader& twfs, @@ -128,6 +178,29 @@ void MoveAbstraction::calcForwardMoveWithDrift( [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); } +template<> +void MoveAbstraction::calcForwardMoveWithDrift( + const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + + twf_dispatcher_.flex_evalGradWithSpin(twfs, elecs, iat, grads_now_, spingrads_now_); + drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); + std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), + [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); + //spin part of forward move + drift_modifier_.getDrifts(spintauovermass_, spingrads_now_, spindrifts_); + std::transform(spindrifts_.begin(), spindrifts_.end(), delta_spin_start, spindrifts_.begin(), + [st = sqrtspintau_](const Scalar& spindrift, const Scalar& delta_spin) { + return spindrift + (st * delta_spin); + }); +} + template<> void MoveAbstraction::calcForwardMove(const int iat) { @@ -138,12 +211,33 @@ void MoveAbstraction::calcForwardMove(const int iat) [st = sqrttau_](const Pos& delta_r) { return st * delta_r; }); } +template<> +void MoveAbstraction::calcForwardMove(const int iat) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + + std::transform(delta_r_start, delta_r_end, drifts_.begin(), + [st = sqrttau_](const Pos& delta_r) { return st * delta_r; }); + std::transform(delta_spin_start, delta_spin_end, spindrifts_.begin(), + [st = sqrtspintau_](const Scalar& delta_spin) { return st * delta_spin; }); +} + template<> void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) { ps_dispatcher_.flex_makeMove(elecs, iat, drifts_); } +template<> +void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, + const int iat) +{ + ps_dispatcher_.flex_makeMoveWithSpin(elecs, iat, drifts_, spindrifts_); +} + template<> void MoveAbstraction::updateGreensFunctionWithDrift( const RefVectorWithLeader& twfs, @@ -172,10 +266,55 @@ void MoveAbstraction::updateGreensFunctionWithDrift( } template<> -void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - const int iat, - std::vector& ratios) +void MoveAbstraction::updateGreensFunctionWithDrift( + const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + Crowd& crowd, + const int iat, + std::vector& ratios, + std::vector& log_gf, + std::vector& log_gb) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + + twf_dispatcher_.flex_calcRatioGradWithSpin(twfs, elecs, iat, ratios, grads_new_, spingrads_new_); + std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { + constexpr Real mhalf(-0.5); + return mhalf * dot(delta_r, delta_r); + }); + //add spin part to greens function + std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), + [](const Scalar& delta_spin, const Real& loggf) { + constexpr Real mhalf(-0.5); + return loggf + mhalf * delta_spin * delta_spin; + }); + + drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); + drift_modifier_.getDrifts(spintauovermass_, spingrads_new_, spindrifts_); + + std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts_.begin(), drifts_.begin(), + [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); + std::transform(crowd.beginElectrons(), crowd.endElectrons(), spindrifts_.begin(), spindrifts_.begin(), + [iat](const ParticleSet& ps, const Scalar& spindrift) { + return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; + }); + + std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), + [halfovertau = oneover2tau_](const Pos& drift) { return -halfovertau * dot(drift, drift); }); + //add spin part to greens function + std::transform(spindrifts_.begin(), spindrifts_.end(), log_gb.begin(), log_gb.begin(), + [halfovertau = oneover2spintau_](const Scalar& spindrift, const Real& loggb) { + return loggb - halfovertau * spindrift * spindrift; + }); +} +template +void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat, + std::vector& ratios) { twf_dispatcher_.flex_calcRatio(twfs, elecs, iat, ratios); } diff --git a/src/QMCDrivers/QMCDriverInput.cpp b/src/QMCDrivers/QMCDriverInput.cpp index 4c409d225a..3970ecd4f2 100644 --- a/src/QMCDrivers/QMCDriverInput.cpp +++ b/src/QMCDrivers/QMCDriverInput.cpp @@ -63,6 +63,7 @@ void QMCDriverInput::readXML(xmlNodePtr cur) parameter_set.add(tau_, "timestep"); parameter_set.add(tau_, "time_step"); parameter_set.add(tau_, "tau"); + parameter_set.add(spin_mass_, "spin_mass"); parameter_set.add(blocks_between_recompute_, "blocks_between_recompute"); parameter_set.add(drift_modifier_, "drift_modifier"); parameter_set.add(drift_modifier_unr_a_, "drift_UNR_a"); diff --git a/src/QMCDrivers/QMCDriverInput.h b/src/QMCDrivers/QMCDriverInput.h index b311f12a3e..08c545d97b 100644 --- a/src/QMCDrivers/QMCDriverInput.h +++ b/src/QMCDrivers/QMCDriverInput.h @@ -71,6 +71,7 @@ class QMCDriverInput IndexType steps_between_samples_ = 1; IndexType samples_per_thread_ = 0; RealType tau_ = 0.1; + RealType spin_mass_ = 1.0; // call recompute at the end of each block in the full/mixed precision case. IndexType blocks_between_recompute_ = std::is_same::value ? 0 : 1; bool append_run_ = false; @@ -113,6 +114,7 @@ class QMCDriverInput IndexType get_steps_between_samples() const { return steps_between_samples_; } IndexType get_samples_per_thread() const { return samples_per_thread_; } RealType get_tau() const { return tau_; } + RealType get_spin_mass() const { return spin_mass_; } IndexType get_blocks_between_recompute() const { return blocks_between_recompute_; } bool get_append_run() const { return append_run_; } input::PeriodStride get_walker_dump_period() const { return walker_dump_period_; } diff --git a/src/QMCDrivers/QMCDriverNew.h b/src/QMCDrivers/QMCDriverNew.h index 23226a0abc..c6d117890c 100644 --- a/src/QMCDrivers/QMCDriverNew.h +++ b/src/QMCDrivers/QMCDriverNew.h @@ -89,6 +89,7 @@ class QMCDriverNew : public QMCDriverInterface, public MPIObjectBase enum CoordsToMove { POSITIONS, + POSITIONS_SPINS, }; using MCPWalker = MCPopulation::MCPWalker; @@ -430,6 +431,8 @@ class QMCDriverNew : public QMCDriverInterface, public MPIObjectBase static void defaultSetNonLocalMoveHandler(QMCHamiltonian& gold_ham); + CoordsToMove coords; + friend class qmcplusplus::testing::VMCBatchedTest; friend class qmcplusplus::testing::DMCBatchedTest; friend class qmcplusplus::testing::QMCDriverNewTestWrapper; From c7a11ff70636dba5630d4e505a23b4d2643f9f6f Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 24 Jan 2022 14:24:47 -0700 Subject: [PATCH 03/34] add selection in VMCBatched --- src/QMCDrivers/VMC/VMCBatched.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 43efb5280c..fa40b62f04 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -208,8 +208,12 @@ void VMCBatched::runVMCStep(int crowd_id, // For VMC we don't call this method for warmup steps. const bool accumulate_this_step = true; const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + if (spin_move) + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); + else + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); } void VMCBatched::process(xmlNodePtr node) @@ -280,7 +284,12 @@ bool VMCBatched::run() Crowd& crowd = *(crowds[crowd_id]); const bool recompute = false; const bool accumulate_this_step = false; - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); + const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + if (spin_move) + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, + accumulate_this_step); + else + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); }; for (int step = 0; step < qmcdriver_input_.get_warmup_steps(); ++step) From 5b8cd853b6160fd54d4f4d316cb4f85c6abc1e84 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 24 Jan 2022 15:05:39 -0700 Subject: [PATCH 04/34] add advanceWalkers to vmcbatched --- src/QMCDrivers/VMC/VMCBatched.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index fa40b62f04..1e7a7b7a41 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -189,6 +189,12 @@ template void VMCBatched::advanceWalkers(const StateFor ContextForSteps& step_context, bool recompute, bool accumulate_this_step); +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); /** Thread body for VMC step * From b71e250a208cb7fb6b047ca9faacca2b18ce8409 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 24 Jan 2022 15:42:41 -0700 Subject: [PATCH 05/34] add DMC move abstraction --- src/QMCDrivers/DMC/DMCBatched.cpp | 69 ++++++++++++---------------- src/QMCDrivers/DMC/DMCBatched.h | 1 + src/QMCDrivers/MoveAbstraction.h | 75 ++++++++++++++++++------------- src/QMCDrivers/VMC/VMCBatched.cpp | 6 --- 4 files changed, 75 insertions(+), 76 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index b0123832c1..565a835613 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -26,6 +26,7 @@ #include "QMCDrivers/DMC/WalkerControl.h" #include "QMCDrivers/SFNBranch.h" #include "MemoryUsage.h" +#include "MoveAbstraction.h" namespace qmcplusplus { @@ -60,6 +61,7 @@ void DMCBatched::setNonLocalMoveHandler(QMCHamiltonian& golden_hamiltonian) dmcdriver_input_.get_alpha(), dmcdriver_input_.get_gamma()); } +template void DMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, @@ -102,14 +104,13 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, const int num_walkers = crowd.size(); + MoveAbstraction move(ps_dispatcher, twf_dispatcher, step_context.get_random_gen(), sft.drift_modifier, + num_walkers, sft.population.get_num_particles()); + //This generates an entire steps worth of deltas. - step_context.nextDeltaRs(num_walkers * sft.population.get_num_particles()); - auto it_delta_r = step_context.deltaRsBegin(); + move.generateDeltas(); - std::vector grads_now(num_walkers, TrialWaveFunction::GradType(0.0)); - std::vector grads_new(num_walkers, TrialWaveFunction::GradType(0.0)); std::vector ratios(num_walkers, TrialWaveFunction::PsiValueType(0.0)); - std::vector drifts(num_walkers, 0.0); std::vector log_gf(num_walkers, 0.0); std::vector log_gb(num_walkers, 0.0); std::vector prob(num_walkers, 0.0); @@ -130,9 +131,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, ScopedTimer pbyp_local_timer(timers.movepbyp_timer); for (int ig = 0; ig < step_context.get_num_groups(); ++ig) { - RealType tauovermass = sft.qmcdrv_input.get_tau() * sft.population.get_ptclgrp_inv_mass()[ig]; - RealType oneover2tau = 0.5 / (tauovermass); - RealType sqrttau = std::sqrt(tauovermass); + move.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -140,9 +139,6 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, int end_index = step_context.getPtclGroupEnd(ig); for (int iat = start_index; iat < end_index; ++iat) { - auto delta_r_start = it_delta_r + iat * num_walkers; - auto delta_r_end = delta_r_start + num_walkers; - //This is very useful thing to be able to look at in the debugger #ifndef NDEBUG std::vector walkers_who_have_been_on_wire(num_walkers, 0); @@ -152,19 +148,12 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, : walkers_who_have_been_on_wire[iw] = 0; } #endif - //get the displacement - twf_dispatcher.flex_evalGrad(walker_twfs, walker_elecs, iat, grads_now); - sft.drift_modifier.getDrifts(tauovermass, grads_now, drifts); - - std::transform(drifts.begin(), drifts.end(), delta_r_start, drifts.begin(), - [sqrttau](PosType& drift, PosType& delta_r) { return drift + (sqrttau * delta_r); }); + move.calcForwardMoveWithDrift(walker_twfs, walker_elecs, iat); // only DMC does this // TODO: rr needs a real name std::vector rr(num_walkers, 0.0); - assert(rr.size() == delta_r_end - delta_r_start); - std::transform(delta_r_start, delta_r_end, rr.begin(), - [tauovermass](auto& delta_r) { return tauovermass * dot(delta_r, delta_r); }); + move.updaterr(iat, rr); // in DMC this was done here, changed to match VMCBatched pending factoring to common source // if (rr > m_r2max) @@ -176,9 +165,9 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int i = 0; i < rr.size(); ++i) assert(std::isfinite(rr[i])); #endif - ps_dispatcher.flex_makeMove(walker_elecs, iat, drifts); + move.makeMove(walker_elecs, iat); - twf_dispatcher.flex_calcRatioGrad(walker_twfs, walker_elecs, iat, ratios, grads_new); + move.updateGreensFunctionWithDrift(walker_twfs, walker_elecs, crowd, iat, ratios, log_gf, log_gb); auto checkPhaseChanged = [&sft](const TrialWaveFunction& twf, int& is_reject) { if (sft.branch_engine.phaseChanged(twf.getPhaseDiff())) @@ -196,19 +185,6 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, rr_proposed[iw] += rr[iw]; } - std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](auto& delta_r) { - constexpr RealType mhalf(-0.5); - return mhalf * dot(delta_r, delta_r); - }); - - sft.drift_modifier.getDrifts(tauovermass, grads_new, drifts); - - std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts.begin(), drifts.begin(), - [iat](auto& elecs, auto& drift) { return elecs.get().R[iat] - elecs.get().getActivePos() - drift; }); - - std::transform(drifts.begin(), drifts.end(), log_gb.begin(), - [oneover2tau](auto& drift) { return -oneover2tau * dot(drift, drift); }); - for (int iw = 0; iw < num_walkers; ++iw) prob[iw] = std::norm(ratios[iw]) * std::exp(log_gb[iw] - log_gf[iw]); @@ -324,6 +300,14 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, } } +template void DMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + DriverTimers& timers, + DMCTimers& dmc_timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + void DMCBatched::runDMCStep(int crowd_id, const StateForThread& sft, DriverTimers& timers, @@ -344,8 +328,13 @@ void DMCBatched::runDMCStep(int crowd_id, // Are we entering the the last step of a block to recompute at? const bool recompute_this_step = (sft.is_recomputing_block && (step + 1) == max_steps); const bool accumulate_this_step = true; - advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + if (spin_move) + advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); + else + advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); } void DMCBatched::process(xmlNodePtr node) @@ -433,9 +422,9 @@ bool DMCBatched::run() dmc_state.recalculate_properties_period = (qmc_driver_mode_[QMC_UPDATE_MODE]) ? qmcdriver_input_.get_recalculate_properties_period() : (qmcdriver_input_.get_max_blocks() + 1) * qmcdriver_input_.get_max_steps(); - dmc_state.is_recomputing_block = qmcdriver_input_.get_blocks_between_recompute() - ? (1 + block) % qmcdriver_input_.get_blocks_between_recompute() == 0 - : false; + dmc_state.is_recomputing_block = qmcdriver_input_.get_blocks_between_recompute() + ? (1 + block) % qmcdriver_input_.get_blocks_between_recompute() == 0 + : false; for (UPtr& crowd : crowds_) crowd->startBlock(qmcdriver_input_.get_max_steps()); diff --git a/src/QMCDrivers/DMC/DMCBatched.h b/src/QMCDrivers/DMC/DMCBatched.h index bb397a2fda..713022478c 100644 --- a/src/QMCDrivers/DMC/DMCBatched.h +++ b/src/QMCDrivers/DMC/DMCBatched.h @@ -132,6 +132,7 @@ class DMCBatched : public QMCDriverNew ///walker controller for load-balance std::unique_ptr walker_controller_; + template static void advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 33bcadf6fe..d693906f9b 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -68,6 +68,8 @@ class MoveAbstraction const int iat, std::vector& ratios); + void updaterr(const int iat, std::vector& rr); + private: std::vector drifts_; std::vector walker_deltas_; @@ -89,12 +91,12 @@ class MoveAbstraction }; template<> -MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), twf_dispatcher_(twf_dispatcher), random_gen_(random_gen), @@ -108,12 +110,12 @@ MoveAbstraction::MoveAbstraction(const PSdispatcher& ps } template<> -MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), twf_dispatcher_(twf_dispatcher), random_gen_(random_gen), @@ -131,20 +133,21 @@ MoveAbstraction::MoveAbstraction(const PSdispatch } template<> -void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); } template<> -void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); makeGaussRandomWithEngine(walker_spindeltas_, random_gen_); } template<> -void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, + const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -152,8 +155,8 @@ void MoveAbstraction::setTauForGroup(const QMCDriverInp } template<> -void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, - const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, + const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -164,7 +167,7 @@ void MoveAbstraction::setTauForGroup(const QMCDri } template<> -void MoveAbstraction::calcForwardMoveWithDrift( +inline void MoveAbstraction::calcForwardMoveWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat) @@ -179,7 +182,7 @@ void MoveAbstraction::calcForwardMoveWithDrift( } template<> -void MoveAbstraction::calcForwardMoveWithDrift( +inline void MoveAbstraction::calcForwardMoveWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat) @@ -202,7 +205,7 @@ void MoveAbstraction::calcForwardMoveWithDrift( } template<> -void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -212,7 +215,7 @@ void MoveAbstraction::calcForwardMove(const int iat) } template<> -void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -226,20 +229,21 @@ void MoveAbstraction::calcForwardMove(const int i } template<> -void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) +inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, + const int iat) { ps_dispatcher_.flex_makeMove(elecs, iat, drifts_); } template<> -void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, - const int iat) +inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, + const int iat) { ps_dispatcher_.flex_makeMoveWithSpin(elecs, iat, drifts_, spindrifts_); } template<> -void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, Crowd& crowd, @@ -266,7 +270,7 @@ void MoveAbstraction::updateGreensFunctionWithDrift( } template<> -void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, Crowd& crowd, @@ -310,15 +314,26 @@ void MoveAbstraction::updateGreensFunctionWithDri return loggb - halfovertau * spindrift * spindrift; }); } + template -void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - const int iat, - std::vector& ratios) +inline void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat, + std::vector& ratios) { twf_dispatcher_.flex_calcRatio(twfs, elecs, iat, ratios); } +template +inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) +{ + auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; + assert(rr.size() == delta_r_end - delta_r_start); + std::transform(delta_r_start, delta_r_end, rr.begin(), + [t = tauovermass_](auto& delta_r) { return t * dot(delta_r, delta_r); }); +} + } // namespace qmcplusplus #endif diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 1e7a7b7a41..fa40b62f04 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -189,12 +189,6 @@ template void VMCBatched::advanceWalkers(const StateFor ContextForSteps& step_context, bool recompute, bool accumulate_this_step); -template void VMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - QMCDriverNew::DriverTimers& timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); /** Thread body for VMC step * From 39e809a029f1b10cb7f383d9bbbcd78a2847cc07 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 24 Jan 2022 16:24:30 -0700 Subject: [PATCH 06/34] add advanceWalkers --- src/QMCDrivers/DMC/DMCBatched.cpp | 8 ++++++++ src/QMCDrivers/VMC/VMCBatched.cpp | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 565a835613..fd22f8b471 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -308,6 +308,14 @@ template void DMCBatched::advanceWalkers(const StateFor bool recompute, bool accumulate_this_step); +template void DMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + DriverTimers& timers, + DMCTimers& dmc_timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + void DMCBatched::runDMCStep(int crowd_id, const StateForThread& sft, DriverTimers& timers, diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index fa40b62f04..a2cd3ccd0a 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -190,6 +190,13 @@ template void VMCBatched::advanceWalkers(const StateFor bool recompute, bool accumulate_this_step); +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + /** Thread body for VMC step * */ From 71ef608f47327773e57db28af246ada0004855ef Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 10:32:38 -0700 Subject: [PATCH 07/34] add comments to MoveAbtraction class --- src/QMCDrivers/MoveAbstraction.h | 105 +++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index d693906f9b..bdae044e53 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -25,6 +25,11 @@ namespace qmcplusplus { +/** abstraction class to handle particle moves in batched QMC drivers + * + * QMCDriverNew has an enum CoordsToMove + * Currently supports POSITIONS and POSITIONS_SPINS, which includes dynamic spins in particle moves + */ template class MoveAbstraction { @@ -36,6 +41,12 @@ class MoveAbstraction using PsiV = TrialWaveFunction::PsiValueType; public: + /** Constructor + * + * uses references to dispatchers, rng, drift_modifers, etc which are used regularly for the particle moves + * resizes all of the vectors to be used throughout this move + * \param[in] random_gen + */ MoveAbstraction(const PSdispatcher& ps_dispatcher, const TWFdispatcher& twf_dispatcher, RandomGenerator& random_gen, @@ -43,18 +54,66 @@ class MoveAbstraction const int num_walkers, const int num_particles); + /** generates an entire steps worth of displacements + * + * Uses gaussian random numbers to generate the displacements for all coordinates over all walkers + */ void generateDeltas(); + /** sets the timestep information for the move + * + * e.g. spatial only moves (POSITIONS) only need the spatial timestep, whereas + * spin moves need a spin timestep defined by the spin mass provided by the driver input + */ void setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass); + /** Calulates the forward move for all particle coordinates + * + * updates all the walkers in the crowd for particle iat using + * \f[ + * \mathbf{r}' - \mathbf{r} = \sqrt{\tau} \eta + \tau \mathbf{v} + * \f] + * using drift and diffusion. + * This also includes the spin drift and diffusion depending on the template parameter + */ void calcForwardMoveWithDrift(const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat); + /** Calculates the forward move for all particle coordinates without drift + * + * updates all the walkers in the crowd for particle iat using + * \f[ + * \mathbf{r}' - \mathbf{r} = \sqrt{\tau}\eta + * \f] + * i.e., diffusion only. Only used in VMCBatched + * This also includes the spin diffusion depending on the template parameter. + */ void calcForwardMove(const int iat); + /** makes particle move + * + * updates particle iat for all walkers in the crowd. This uses the PSdispatcher to use flex_ move APIs + */ void makeMove(const RefVectorWithLeader& elecs, const int iat); + /** calculates the greens functions for forward and reverse moves and TWF ratio, used for acceptance in QMCDrivers + * + * includes the drift part of the move, i.e. + * \f[ + * G(\mathbf{r}'\leftarrow\mathbf{r}, \tau = \exp\left[-\frac{1}{2\tau}|\mathbf{r}'-\mathbf{r} - \tau \mathbf{v}(\mathbf{r})|^2 \right] + * \f] + * and the ratio + * \f[ + * \frac{\Psi_T(\mathbf{r}')}{\Psi_T(\mathbf{r})} + * \f] + * This is the necessary data to calculate the acceptance ratio in the QMCDriver + * also adds to the spin move component depending on the template parameter + * \param[in] crowd + * \param[out] ratios ratio of trial wave functions for all walkers for particle iat + * \param[out] log_gf log of greens function for forward move for all walkers for particle iat + * \param[out] log_gb log of greens function for reverse move for all walkers for particle iat + */ void updateGreensFunctionWithDrift(const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, Crowd& crowd, @@ -63,30 +122,76 @@ class MoveAbstraction std::vector& log_gf, std::vector& log_bg); + /** calculates TWF ratio, used for acceptance in QMCDrivers + * + * does not include the drift part of the move. + * \f[ + * G(\mathbf{r}'\leftarrow\mathbf{r}, \tau = \exp\left[-\frac{1}{2\tau}|\mathbf{r}'-\mathbf{r}|^2 \right] + * \f] + * Therefore, in the acceptance ratio this cancels since \f$G(\mathbf{r}'\leftarrow \mathbf{r}, \tau) = G(\mathbf{r}\leftarrow\mathbf{r}',\tau)$\f. + * Therefore, we only need to calculate the ratios + * \f[ + * \frac{\Psi_T(\mathbf{r}')}{\Psi_T(\mathbf{r})} + * \f] + * This is the necessary data to calculate the acceptance ratio in the VMCBatched, without drift + * \param[out] ratios ratio of trial wave functions for all walkers for particle iat + */ void updateGreensFunction(const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat, std::vector& ratios); + /** accumulates the data to help construct the effective timestep + * + * the effective timestep used in DMC algorithm is + * \f[ + * \tau_{\rm eff} = \frac{\sum R_{\rm accepted}^2}{\sum R_{\rm proposed}^2} + * \f] + * which is accumulated over all electrons in a walker + * + * rr below is the \f$r^2$\f for the current particle iat for the walkers and will be accumulated for + * each particle in the DMC driver. + * \param[out] rr + */ void updaterr(const int iat, std::vector& rr); private: + /// spatial drift part of move for a single particle across multiple walkers std::vector drifts_; + /// all of the spatial diffusion moves for all walkers and particles for a given step std::vector walker_deltas_; + /// spatial gradients for a single electron across multiple walkers std::vector grads_now_, grads_new_; + /// spin drift part of move for a single particle across multiple walkers std::vector spindrifts_; + /// all of the spin diffusion moves for all walkerss and particles for a given step std::vector walker_spindeltas_; + /// spin gradients for a single electrons across multiple walkers std::vector spingrads_now_, spingrads_new_; + /// provides flex_ APIs to do select sw/mw updates of particle set const PSdispatcher& ps_dispatcher_; + /// provides flex_ APIs to do select sw/mw updates of trial wave function const TWFdispatcher& twf_dispatcher_; + /// rng, provided by ContextForSteps RandomGenerator& random_gen_; + /// drift modifer used to limit size of drift velocity const DriftModifierBase& drift_modifier_; + /// spatial timestep Real tauovermass_; + /// \f$\frac{1}{2\tau}$\f, used in Green's function Real oneover2tau_; + /// \f$\sqrt{\tau}$\f, used to scale the diffusion part of move Real sqrttau_; + /** spin timestep, defined by spin mass and spatial timestep. + * + * Note that \f$\tau_{\rm spin} = \frac{\tau}{m_{\rm spin}}$\f} + */ Real spintauovermass_; + /// \f$\frac{1}{2\tau_{\rm spin}}$\f, used in spin part of green's function Real oneover2spintau_; + /// \f$\sqrt{\tau_{\rm spin}}$\f, used to scale the diffusion part of spin move Real sqrtspintau_; + /// total number of walkers in crowd. const int num_walkers_; }; From f46a9bf6b4d009180677bc3bd1e822ac8bf38a8b Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 10:38:18 -0700 Subject: [PATCH 08/34] add docs for spin mass to batched drivers --- docs/methods.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/methods.rst b/docs/methods.rst index 0ed83393b0..fe27ed7610 100644 --- a/docs/methods.rst +++ b/docs/methods.rst @@ -302,6 +302,9 @@ The following is an example of VMC section storing configurations (walker sample +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ | ``debug_checks`` | text | see additional info | dep. | Turn on/off additional recompute and checks | +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ + | ``spin_mass`` | real | :math:`\geq 0` | 1.0 | Effective mass for spin sampling | + +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ + Additional information: @@ -357,6 +360,9 @@ Additional information: - ``debug_checks`` valid values are 'no', 'all', 'checkGL_after_load', 'checkGL_after_moves', 'checkGL_after_tmove'. If the build type is `debug`, the default value is 'all'. Otherwise, the default value is 'no'. +- ``spin_mass`` Optional parameter to allow the user to change the rate of spin sampling. If spin sampling is on using ``spinor`` == yes in the electron ParticleSet input, the spin mass determines the rate + of spin sampling, resulting in an effective spin timestep :math:`\tau_s = \frac{\tau}{\mu_s}`. The algorithm is described in detail in :cite:`Melton2016-1` and :cite:`Melton2016-2`. + An example VMC section for a simple ``vmc_batch`` run: :: @@ -1550,6 +1556,9 @@ Combining VMC and DMC in a single run (wavefunction optimization can be combined +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ | ``debug_checks`` | text | see additional info | dep. | Turn on/off additional recompute and checks | +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ + | ``spin_mass`` | real | :math:`\geq 0` | 1.0 | Effective mass for spin sampling | + +--------------------------------+--------------+-------------------------+-------------+-----------------------------------------------+ + - ``crowds`` The number of crowds that the walkers are subdivided into on each MPI rank. If not provided, it is set equal to the number of OpenMP threads. @@ -1566,6 +1575,9 @@ Combining VMC and DMC in a single run (wavefunction optimization can be combined - ``debug_checks`` valid values are 'no', 'all', 'checkGL_after_load', 'checkGL_after_moves', 'checkGL_after_tmove'. If the build type is `debug`, the default value is 'all'. Otherwise, the default value is 'no'. +- ``spin_mass`` Optional parameter to allow the user to change the rate of spin sampling. If spin sampling is on using ``spinor`` == yes in the electron ParticleSet input, the spin mass determines the rate + of spin sampling, resulting in an effective spin timestep :math:`\tau_s = \frac{\tau}{\mu_s}`. The algorithm is described in detail in :cite:`Melton2016-1` and :cite:`Melton2016-2`. + .. code-block:: :caption: The following is an example of a minimal DMC section using the ``dmc_batch`` driver :name: Listing 48b From 51695901e5acfa1e0ef6eae29f058968e68b2fd8 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 10:42:41 -0700 Subject: [PATCH 09/34] remove unused variable --- src/QMCDrivers/QMCDriverNew.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/QMCDrivers/QMCDriverNew.h b/src/QMCDrivers/QMCDriverNew.h index 7652111da5..10600fdd62 100644 --- a/src/QMCDrivers/QMCDriverNew.h +++ b/src/QMCDrivers/QMCDriverNew.h @@ -431,8 +431,6 @@ class QMCDriverNew : public QMCDriverInterface, public MPIObjectBase static void defaultSetNonLocalMoveHandler(QMCHamiltonian& gold_ham); - CoordsToMove coords; - friend class qmcplusplus::testing::VMCBatchedTest; friend class qmcplusplus::testing::DMCBatchedTest; friend class qmcplusplus::testing::QMCDriverNewTestWrapper; From 1f28c8b7ceb625ba6ced3b604a2db3ff0a7b43f2 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 13:07:24 -0700 Subject: [PATCH 10/34] move CoordsToMove enum to MoveAbstraction --- src/QMCDrivers/DMC/DMCBatched.cpp | 33 +++++++------- src/QMCDrivers/DMC/DMCBatched.h | 1 + src/QMCDrivers/MoveAbstraction.h | 74 +++++++++++++++---------------- src/QMCDrivers/QMCDriverNew.h | 6 --- src/QMCDrivers/VMC/VMCBatched.cpp | 29 ++++++------ src/QMCDrivers/VMC/VMCBatched.h | 1 + 6 files changed, 69 insertions(+), 75 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index fd22f8b471..9b2e74d28b 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -26,7 +26,6 @@ #include "QMCDrivers/DMC/WalkerControl.h" #include "QMCDrivers/SFNBranch.h" #include "MemoryUsage.h" -#include "MoveAbstraction.h" namespace qmcplusplus { @@ -61,7 +60,7 @@ void DMCBatched::setNonLocalMoveHandler(QMCHamiltonian& golden_hamiltonian) dmcdriver_input_.get_alpha(), dmcdriver_input_.get_gamma()); } -template +template void DMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, @@ -300,21 +299,21 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, } } -template void DMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - DriverTimers& timers, - DMCTimers& dmc_timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); - -template void DMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - DriverTimers& timers, - DMCTimers& dmc_timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); +template void DMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + DriverTimers& timers, + DMCTimers& dmc_timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + +template void DMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + DriverTimers& timers, + DMCTimers& dmc_timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); void DMCBatched::runDMCStep(int crowd_id, const StateForThread& sft, diff --git a/src/QMCDrivers/DMC/DMCBatched.h b/src/QMCDrivers/DMC/DMCBatched.h index 79b0fc1875..2ff802bb1d 100644 --- a/src/QMCDrivers/DMC/DMCBatched.h +++ b/src/QMCDrivers/DMC/DMCBatched.h @@ -17,6 +17,7 @@ #include "QMCDrivers/DMC/DMCDriverInput.h" #include "QMCDrivers/MCPopulation.h" #include "QMCDrivers/ContextForSteps.h" +#include "QMCDrivers/MoveAbstraction.h" namespace qmcplusplus { diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index bdae044e53..770ca20971 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -12,7 +12,6 @@ #ifndef QMCPLUSPLUS_MOVEABSTRACTION_H #define QMCPLUSPLUS_MOVEABSTRACTION_H -#include "QMCDriverNew.h" #include "Particle/PSdispatcher.h" #include "Particle/ParticleSet.h" #include "QMCWaveFunctions/TWFdispatcher.h" @@ -25,12 +24,18 @@ namespace qmcplusplus { +enum CoordsToMove +{ + POSITIONS, + POSITIONS_SPINS, +}; + /** abstraction class to handle particle moves in batched QMC drivers * - * QMCDriverNew has an enum CoordsToMove + * Templated on enum CoordsToMove * Currently supports POSITIONS and POSITIONS_SPINS, which includes dynamic spins in particle moves */ -template +template class MoveAbstraction { using Pos = ParticleSet::PosType; @@ -172,7 +177,7 @@ class MoveAbstraction const PSdispatcher& ps_dispatcher_; /// provides flex_ APIs to do select sw/mw updates of trial wave function const TWFdispatcher& twf_dispatcher_; - /// rng, provided by ContextForSteps + /// rng, provided by ContextForSteps RandomGenerator& random_gen_; /// drift modifer used to limit size of drift velocity const DriftModifierBase& drift_modifier_; @@ -196,12 +201,12 @@ class MoveAbstraction }; template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), twf_dispatcher_(twf_dispatcher), random_gen_(random_gen), @@ -215,12 +220,12 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatc } template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const TWFdispatcher& twf_dispatcher, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), twf_dispatcher_(twf_dispatcher), random_gen_(random_gen), @@ -238,21 +243,20 @@ inline MoveAbstraction::MoveAbstraction(const PSd } template<> -inline void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); } template<> -inline void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); makeGaussRandomWithEngine(walker_spindeltas_, random_gen_); } template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, - const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -260,8 +264,7 @@ inline void MoveAbstraction::setTauForGroup(const QMCDr } template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, - const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -272,10 +275,9 @@ inline void MoveAbstraction::setTauForGroup(const } template<> -inline void MoveAbstraction::calcForwardMoveWithDrift( - const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - const int iat) +inline void MoveAbstraction::calcForwardMoveWithDrift(const RefVectorWithLeader& twfs, + const RefVectorWithLeader& elecs, + const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -287,7 +289,7 @@ inline void MoveAbstraction::calcForwardMoveWithDrift( } template<> -inline void MoveAbstraction::calcForwardMoveWithDrift( +inline void MoveAbstraction::calcForwardMoveWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat) @@ -310,7 +312,7 @@ inline void MoveAbstraction::calcForwardMoveWithD } template<> -inline void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -320,7 +322,7 @@ inline void MoveAbstraction::calcForwardMove(const int } template<> -inline void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -334,21 +336,19 @@ inline void MoveAbstraction::calcForwardMove(cons } template<> -inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, - const int iat) +inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) { ps_dispatcher_.flex_makeMove(elecs, iat, drifts_); } template<> -inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, - const int iat) +inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) { ps_dispatcher_.flex_makeMoveWithSpin(elecs, iat, drifts_, spindrifts_); } template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, Crowd& crowd, @@ -375,7 +375,7 @@ inline void MoveAbstraction::updateGreensFunctionWithDr } template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, Crowd& crowd, @@ -420,7 +420,7 @@ inline void MoveAbstraction::updateGreensFunction }); } -template +template inline void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, const RefVectorWithLeader& elecs, const int iat, @@ -429,7 +429,7 @@ inline void MoveAbstraction::updateGreensFunction(const RefVectorWithLea twf_dispatcher_.flex_calcRatio(twfs, elecs, iat, ratios); } -template +template inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; diff --git a/src/QMCDrivers/QMCDriverNew.h b/src/QMCDrivers/QMCDriverNew.h index 10600fdd62..c3d78b2bff 100644 --- a/src/QMCDrivers/QMCDriverNew.h +++ b/src/QMCDrivers/QMCDriverNew.h @@ -86,12 +86,6 @@ class QMCDriverNew : public QMCDriverInterface, public MPIObjectBase QMC_WARMUP }; - enum CoordsToMove - { - POSITIONS, - POSITIONS_SPINS, - }; - using MCPWalker = MCPopulation::MCPWalker; using WFBuffer = MCPopulation::WFBuffer; diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index a2cd3ccd0a..e021226d3a 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -18,7 +18,6 @@ #include "ParticleBase/RandomSeqGenerator.h" #include "Particle/MCSample.h" #include "MemoryUsage.h" -#include "MoveAbstraction.h" namespace qmcplusplus { @@ -36,7 +35,7 @@ VMCBatched::VMCBatched(const ProjectData& project_data, collect_samples_(false) {} -template +template void VMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, QMCDriverNew::DriverTimers& timers, @@ -183,19 +182,19 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, // check if all moves failed } -template void VMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - QMCDriverNew::DriverTimers& timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); - -template void VMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - QMCDriverNew::DriverTimers& timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); /** Thread body for VMC step * diff --git a/src/QMCDrivers/VMC/VMCBatched.h b/src/QMCDrivers/VMC/VMCBatched.h index 37ea72efac..69f2bd45b6 100644 --- a/src/QMCDrivers/VMC/VMCBatched.h +++ b/src/QMCDrivers/VMC/VMCBatched.h @@ -18,6 +18,7 @@ #include "QMCDrivers/MCPopulation.h" #include "QMCDrivers/ContextForSteps.h" #include "QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h" +#include "QMCDrivers/MoveAbstraction.h" #include "Utilities/Timer.h" From cd29a559dbce816d75a78eee2a65101b93ac39de Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 14:33:28 -0700 Subject: [PATCH 11/34] remove TWFdispatcher from MoveAbstraction, add elecs --- src/QMCDrivers/DMC/DMCBatched.cpp | 8 ++-- src/QMCDrivers/MoveAbstraction.h | 70 +++++++++++++++---------------- src/QMCDrivers/VMC/VMCBatched.cpp | 10 ++--- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 9b2e74d28b..60c241e905 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -103,7 +103,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, const int num_walkers = crowd.size(); - MoveAbstraction move(ps_dispatcher, twf_dispatcher, step_context.get_random_gen(), sft.drift_modifier, + MoveAbstraction move(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, sft.population.get_num_particles()); //This generates an entire steps worth of deltas. @@ -147,7 +147,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, : walkers_who_have_been_on_wire[iw] = 0; } #endif - move.calcForwardMoveWithDrift(walker_twfs, walker_elecs, iat); + move.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); // only DMC does this // TODO: rr needs a real name @@ -164,9 +164,9 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int i = 0; i < rr.size(); ++i) assert(std::isfinite(rr[i])); #endif - move.makeMove(walker_elecs, iat); + move.makeMove(iat); - move.updateGreensFunctionWithDrift(walker_twfs, walker_elecs, crowd, iat, ratios, log_gf, log_gb); + move.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); auto checkPhaseChanged = [&sft](const TrialWaveFunction& twf, int& is_reject) { if (sft.branch_engine.phaseChanged(twf.getPhaseDiff())) diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 770ca20971..0df9ade45d 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -53,7 +53,7 @@ class MoveAbstraction * \param[in] random_gen */ MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& elecs, RandomGenerator& random_gen, const DriftModifierBase& drift_modifier, const int num_walkers, @@ -81,8 +81,8 @@ class MoveAbstraction * using drift and diffusion. * This also includes the spin drift and diffusion depending on the template parameter */ - void calcForwardMoveWithDrift(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, + void calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, const int iat); /** Calculates the forward move for all particle coordinates without drift @@ -100,7 +100,7 @@ class MoveAbstraction * * updates particle iat for all walkers in the crowd. This uses the PSdispatcher to use flex_ move APIs */ - void makeMove(const RefVectorWithLeader& elecs, const int iat); + void makeMove(const int iat); /** calculates the greens functions for forward and reverse moves and TWF ratio, used for acceptance in QMCDrivers * @@ -114,14 +114,12 @@ class MoveAbstraction * \f] * This is the necessary data to calculate the acceptance ratio in the QMCDriver * also adds to the spin move component depending on the template parameter - * \param[in] crowd * \param[out] ratios ratio of trial wave functions for all walkers for particle iat * \param[out] log_gf log of greens function for forward move for all walkers for particle iat * \param[out] log_gb log of greens function for reverse move for all walkers for particle iat */ - void updateGreensFunctionWithDrift(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - Crowd& crowd, + void updateGreensFunctionWithDrift(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, const int iat, std::vector& ratios, std::vector& log_gf, @@ -141,8 +139,8 @@ class MoveAbstraction * This is the necessary data to calculate the acceptance ratio in the VMCBatched, without drift * \param[out] ratios ratio of trial wave functions for all walkers for particle iat */ - void updateGreensFunction(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, + void updateGreensFunction(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, const int iat, std::vector& ratios); @@ -175,8 +173,8 @@ class MoveAbstraction std::vector spingrads_now_, spingrads_new_; /// provides flex_ APIs to do select sw/mw updates of particle set const PSdispatcher& ps_dispatcher_; - /// provides flex_ APIs to do select sw/mw updates of trial wave function - const TWFdispatcher& twf_dispatcher_; + /// ParticleSets for each walker + const RefVectorWithLeader& elecs_; /// rng, provided by ContextForSteps RandomGenerator& random_gen_; /// drift modifer used to limit size of drift velocity @@ -202,13 +200,13 @@ class MoveAbstraction template<> inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& elecs, RandomGenerator& random_gen, const DriftModifierBase& drift_modifier, const int num_walkers, const int num_particles) : ps_dispatcher_(ps_dispatcher), - twf_dispatcher_(twf_dispatcher), + elecs_(elecs), random_gen_(random_gen), drift_modifier_(drift_modifier), num_walkers_(num_walkers) @@ -221,13 +219,13 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispat template<> inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& elecs, RandomGenerator& random_gen, const DriftModifierBase& drift_modifier, const int num_walkers, const int num_particles) : ps_dispatcher_(ps_dispatcher), - twf_dispatcher_(twf_dispatcher), + elecs_(elecs), random_gen_(random_gen), drift_modifier_(drift_modifier), num_walkers_(num_walkers) @@ -275,14 +273,14 @@ inline void MoveAbstraction::setTauForGroup(const QMCDriverInpu } template<> -inline void MoveAbstraction::calcForwardMoveWithDrift(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, +inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; - twf_dispatcher_.flex_evalGrad(twfs, elecs, iat, grads_now_); + twf_dispatcher.flex_evalGrad(twfs, elecs_, iat, grads_now_); drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); @@ -290,8 +288,8 @@ inline void MoveAbstraction::calcForwardMoveWithDrift(const RefVector template<> inline void MoveAbstraction::calcForwardMoveWithDrift( + const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; @@ -299,7 +297,7 @@ inline void MoveAbstraction::calcForwardMoveWithDrift( auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; auto delta_spin_end = delta_spin_start + num_walkers_; - twf_dispatcher_.flex_evalGradWithSpin(twfs, elecs, iat, grads_now_, spingrads_now_); + twf_dispatcher.flex_evalGradWithSpin(twfs, elecs_, iat, grads_now_, spingrads_now_); drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); @@ -336,22 +334,21 @@ inline void MoveAbstraction::calcForwardMove(const int iat) } template<> -inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) +inline void MoveAbstraction::makeMove(const int iat) { - ps_dispatcher_.flex_makeMove(elecs, iat, drifts_); + ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); } template<> -inline void MoveAbstraction::makeMove(const RefVectorWithLeader& elecs, const int iat) +inline void MoveAbstraction::makeMove(const int iat) { - ps_dispatcher_.flex_makeMoveWithSpin(elecs, iat, drifts_, spindrifts_); + ps_dispatcher_.flex_makeMoveWithSpin(elecs_, iat, drifts_, spindrifts_); } template<> inline void MoveAbstraction::updateGreensFunctionWithDrift( + const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - Crowd& crowd, const int iat, std::vector& ratios, std::vector& log_gf, @@ -360,14 +357,14 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; - twf_dispatcher_.flex_calcRatioGrad(twfs, elecs, iat, ratios, grads_new_); + twf_dispatcher.flex_calcRatioGrad(twfs, elecs_, iat, ratios, grads_new_); std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { constexpr Real mhalf(-0.5); return mhalf * dot(delta_r, delta_r); }); drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); - std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts_.begin(), drifts_.begin(), + std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), @@ -376,9 +373,8 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( template<> inline void MoveAbstraction::updateGreensFunctionWithDrift( + const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, - Crowd& crowd, const int iat, std::vector& ratios, std::vector& log_gf, @@ -389,7 +385,7 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; auto delta_spin_end = delta_spin_start + num_walkers_; - twf_dispatcher_.flex_calcRatioGradWithSpin(twfs, elecs, iat, ratios, grads_new_, spingrads_new_); + twf_dispatcher.flex_calcRatioGradWithSpin(twfs, elecs_, iat, ratios, grads_new_, spingrads_new_); std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { constexpr Real mhalf(-0.5); return mhalf * dot(delta_r, delta_r); @@ -404,9 +400,9 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); drift_modifier_.getDrifts(spintauovermass_, spingrads_new_, spindrifts_); - std::transform(crowd.beginElectrons(), crowd.endElectrons(), drifts_.begin(), drifts_.begin(), + std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); - std::transform(crowd.beginElectrons(), crowd.endElectrons(), spindrifts_.begin(), spindrifts_.begin(), + std::transform(elecs_.begin(), elecs_.end(), spindrifts_.begin(), spindrifts_.begin(), [iat](const ParticleSet& ps, const Scalar& spindrift) { return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; }); @@ -421,12 +417,12 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( } template -inline void MoveAbstraction::updateGreensFunction(const RefVectorWithLeader& twfs, - const RefVectorWithLeader& elecs, +inline void MoveAbstraction::updateGreensFunction(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, const int iat, std::vector& ratios) { - twf_dispatcher_.flex_calcRatio(twfs, elecs, iat, ratios); + twf_dispatcher.flex_calcRatio(twfs, elecs_, iat, ratios); } template diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index e021226d3a..b5fcaf8304 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -78,7 +78,7 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector> twf_accept_list, twf_reject_list; isAccepted.reserve(num_walkers); - MoveAbstraction move(ps_dispatcher, twf_dispatcher, step_context.get_random_gen(), sft.drift_modifier, + MoveAbstraction move(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, sft.population.get_num_particles()); for (int sub_step = 0; sub_step < sft.qmcdrv_input.get_sub_steps(); sub_step++) @@ -98,17 +98,17 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, for (int iat = start_index; iat < end_index; ++iat) { if (use_drift) - move.calcForwardMoveWithDrift(walker_twfs, walker_elecs, iat); + move.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); else move.calcForwardMove(iat); - move.makeMove(walker_elecs, iat); + move.makeMove(iat); // This is inelegant if (use_drift) - move.updateGreensFunctionWithDrift(walker_twfs, walker_elecs, crowd, iat, ratios, log_gf, log_gb); + move.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); else - move.updateGreensFunction(walker_twfs, walker_elecs, iat, ratios); + move.updateGreensFunction(twf_dispatcher, walker_twfs, iat, ratios); std::transform(ratios.begin(), ratios.end(), prob.begin(), [](auto ratio) { return std::norm(ratio); }); From cdbe3954bfea50d7deb309954980f4bc2e36e4b6 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 25 Jan 2022 18:29:27 -0700 Subject: [PATCH 12/34] separate mw_ spatial and spin moves in ParticleSet to individual APIs --- src/Particle/PSdispatcher.cpp | 12 ------- src/Particle/PSdispatcher.h | 5 --- src/Particle/ParticleSet.cpp | 32 ++++++++++++------- src/Particle/ParticleSet.h | 19 ++++++++--- src/QMCDrivers/MoveAbstraction.h | 4 ++- src/QMCWaveFunctions/tests/test_MO_spinor.cpp | 20 ++++++++++++ .../tests/test_einset_spinor.cpp | 10 ++++++ 7 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/Particle/PSdispatcher.cpp b/src/Particle/PSdispatcher.cpp index ce73bd3d12..1decfb64d4 100644 --- a/src/Particle/PSdispatcher.cpp +++ b/src/Particle/PSdispatcher.cpp @@ -53,18 +53,6 @@ void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, p_list[iw].makeMove(iat, displs[iw]); } -void PSdispatcher::flex_makeMoveWithSpin(const RefVectorWithLeader& p_list, - int iat, - const std::vector& displs, - const std::vector& sdispls) const -{ - if (use_batch_) - ParticleSet::mw_makeMoveWithSpin(p_list, iat, displs, sdispls); - else - for (size_t iw = 0; iw < p_list.size(); iw++) - p_list[iw].makeMoveWithSpin(iat, displs[iw], sdispls[iw]); -} - void PSdispatcher::flex_accept_rejectMove(const RefVectorWithLeader& p_list, int iat, const std::vector& isAccepted, diff --git a/src/Particle/PSdispatcher.h b/src/Particle/PSdispatcher.h index be24f8d6bf..583a3a1627 100644 --- a/src/Particle/PSdispatcher.h +++ b/src/Particle/PSdispatcher.h @@ -41,11 +41,6 @@ class PSdispatcher int iat, const std::vector& displs) const; - void flex_makeMoveWithSpin(const RefVectorWithLeader& p_list, - int iat, - const std::vector& displs, - const std::vector& sdispls) const; - void flex_accept_rejectMove(const RefVectorWithLeader& p_list, int iat, const std::vector& isAccepted, diff --git a/src/Particle/ParticleSet.cpp b/src/Particle/ParticleSet.cpp index 7834d6f009..b235de9426 100644 --- a/src/Particle/ParticleSet.cpp +++ b/src/Particle/ParticleSet.cpp @@ -71,6 +71,7 @@ ParticleSet::ParticleSet(const SimulationCell& simulation_cell, const DynamicCoo same_mass_(true), is_spinor_(false), active_ptcl_(-1), + active_spin_val_(0.0), myTwist(0.0), ParentName("0"), TotalNum(0), @@ -87,6 +88,7 @@ ParticleSet::ParticleSet(const ParticleSet& p) same_mass_(true), is_spinor_(false), active_ptcl_(-1), + active_spin_val_(0.0), my_species_(p.getSpeciesSet()), myTwist(0.0), ParentName(p.parentName()), @@ -451,23 +453,20 @@ void ParticleSet::mw_makeMove(const RefVectorWithLeader& p_list, for (int iw = 0; iw < p_list.size(); iw++) { - p_list[iw].active_ptcl_ = iat; - p_list[iw].active_pos_ = p_list[iw].R[iat] + displs[iw]; - p_list[iw].active_spin_val_ = p_list[iw].spins[iat]; + p_list[iw].active_ptcl_ = iat; + p_list[iw].active_pos_ = p_list[iw].R[iat] + displs[iw]; new_positions.push_back(p_list[iw].active_pos_); } mw_computeNewPosDistTablesAndSK(p_list, iat, new_positions); } -void ParticleSet::mw_makeMoveWithSpin(const RefVectorWithLeader& p_list, - Index_t iat, - const std::vector& displs, - const std::vector& sdispls) +void ParticleSet::mw_makeSpinMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& sdispls) { - mw_makeMove(p_list, iat, displs); for (int iw = 0; iw < p_list.size(); iw++) - p_list[iw].active_spin_val_ += sdispls[iw]; + p_list[iw].active_spin_val_ = p_list[iw].spins[iat] + sdispls[iw]; } @@ -797,8 +796,7 @@ void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p if (SK && SK->DoUpdate) p_list[iw].structure_factor_->acceptMove(iat, p_list[iw].GroupID[iat], p_list[iw].R[iat]); - p_list[iw].R[iat] = p_list[iw].active_pos_; - p_list[iw].spins[iat] = p_list[iw].active_spin_val_; + p_list[iw].R[iat] = p_list[iw].active_pos_; } p_list[iw].active_ptcl_ = -1; assert(p_list[iw].R[iat] == p_list[iw].coordinates_->getAllParticlePos()[iat]); @@ -814,6 +812,18 @@ void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p } } +void ParticleSet::mw_accept_rejectSpinMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted) +{ + for (int iw = 0; iw < p_list.size(); iw++) + { + assert(iat == p_list[iw].active_ptcl_); + if (isAccepted[iw]) + p_list[iw].spins[iat] = p_list[iw].active_spin_val_; + } +} + void ParticleSet::donePbyP(bool skipSK) { ScopedTimer donePbyP_scope(myTimers[PS_donePbyP]); diff --git a/src/Particle/ParticleSet.h b/src/Particle/ParticleSet.h index 5aa7d2e06b..e27bb550c5 100644 --- a/src/Particle/ParticleSet.h +++ b/src/Particle/ParticleSet.h @@ -292,11 +292,10 @@ class ParticleSet : public QMCTraits, public OhmmsElementBase, public PtclOnLatt int iat, const std::vector& displs); - /// batched version of makeMoveWithSpin - static void mw_makeMoveWithSpin(const RefVectorWithLeader& p_list, - int iat, - const std::vector& displs, - const std::vector& sdispls); + /// batched version makeMove for spin variable only + static void mw_makeSpinMove(const RefVectorWithLeader& p_list, + int iat, + const std::vector& sdispls); /** move the iat-th particle to active_pos_ * @param iat the index of the particle to be moved @@ -386,6 +385,16 @@ class ParticleSet : public QMCTraits, public OhmmsElementBase, public PtclOnLatt const std::vector& isAccepted, bool forward_mode = true); + /** batched version of acceptMove and reject Move fused, but only for spins + * + * note: should be called BEFORE mw_accept_rejectMove since the active_ptcl_ gets reset to -1 + * This would cause the assertion that we have the right particle index to fail if done in the + * wrong order + */ + static void mw_accept_rejectSpinMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted); + void initPropertyList(); inline int addProperty(const std::string& pname) { return PropertyList.add(pname.c_str()); } diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 0df9ade45d..0ed00293ce 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -342,7 +342,9 @@ inline void MoveAbstraction::makeMove(const int iat) template<> inline void MoveAbstraction::makeMove(const int iat) { - ps_dispatcher_.flex_makeMoveWithSpin(elecs_, iat, drifts_, spindrifts_); + ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); + ParticleSet& elec_leader = elecs_.getLeader(); + elec_leader.mw_makeSpinMove(elecs_, iat, spindrifts_); } template<> diff --git a/src/QMCWaveFunctions/tests/test_MO_spinor.cpp b/src/QMCWaveFunctions/tests/test_MO_spinor.cpp index d994a4bf40..6994e95e4d 100644 --- a/src/QMCWaveFunctions/tests/test_MO_spinor.cpp +++ b/src/QMCWaveFunctions/tests/test_MO_spinor.cpp @@ -206,6 +206,10 @@ void test_lcao_spinor() elec_.R = Rnew; elec_.update(); + //make a spin displacement, just set to zero for the test + ParticleSet::ParticleScalar dS; + dS.resize(1); + //now create second walker ParticleSet elec_2(elec_); elec_2.R[0][0] = -0.4; @@ -271,8 +275,11 @@ void test_lcao_spinor() for (int iat = 0; iat < 1; iat++) { std::vector displs = {dR[iat], dR[iat]}; + std::vector sdispls = {dS[iat], dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -300,7 +307,9 @@ void test_lcao_spinor() dspsi_work_2 = 0.0; std::vector displs = {-dR[iat], -dR[iat]}; + std::vector sdispls = {-dS[iat], -dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(val).epsilon(eps)); @@ -318,6 +327,7 @@ void test_lcao_spinor() CHECK(dspsi_v_list[1].get()[0] == ComplexApprox(vds2).epsilon(eps)); std::vector accept = {false, false}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } } @@ -511,6 +521,10 @@ void test_lcao_spinor_excited() elec_.R = Rnew; elec_.update(); + //add spin displacement, just set to zero for sake of test + ParticleSet::ParticleScalar dS; + dS.resize(1); + //now create second walker ParticleSet elec_2(elec_); elec_2.R[0][0] = -0.4; @@ -576,8 +590,11 @@ void test_lcao_spinor_excited() for (int iat = 0; iat < 1; iat++) { std::vector displs = {dR[iat], dR[iat]}; + std::vector sdispls = {dS[iat], dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -605,7 +622,9 @@ void test_lcao_spinor_excited() dspsi_work_2 = 0.0; std::vector displs = {-dR[iat], -dR[iat]}; + std::vector sdispls = {-dS[iat], -dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(val).epsilon(eps)); @@ -623,6 +642,7 @@ void test_lcao_spinor_excited() CHECK(dspsi_v_list[1].get()[0] == ComplexApprox(vds2).epsilon(eps)); std::vector accept = {false, false}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } } diff --git a/src/QMCWaveFunctions/tests/test_einset_spinor.cpp b/src/QMCWaveFunctions/tests/test_einset_spinor.cpp index d5d13b32a9..04dada3208 100644 --- a/src/QMCWaveFunctions/tests/test_einset_spinor.cpp +++ b/src/QMCWaveFunctions/tests/test_einset_spinor.cpp @@ -445,6 +445,10 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") elec_.R = Rnew; elec_.update(); + //just going to use zero displacements for particle spins in test + ParticleSet::ParticleScalar dS; + dS.resize(3); + //now create second walker, with permuted particle positions ParticleSet elec_2(elec_); // permute electrons @@ -529,8 +533,11 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") for (int iat = 0; iat < 3; iat++) { std::vector displs = {dR[iat], dR[iat]}; + std::vector sdispls = {dS[iat], dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -558,7 +565,9 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") dspsi_work_2 = 0.0; std::vector displs = {-dR[iat], -dR[iat]}; + std::vector sdispls = {-dS[iat], -dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); + elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(psiM_ref[iat][0]).epsilon(h)); @@ -611,6 +620,7 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") CHECK(dspsi_v_list[1].get()[2] == ComplexApprox(dspsiM_ref[(iat + 1) % 3][2]).epsilon(h)); std::vector accept = {false, false}; + elec_.mw_accept_rejectSpinMove(p_list, iat, accept); elec_.mw_accept_rejectMove(p_list, iat, accept); } } From 9132b29c004c904670d847b7ede4b4e9d3c2bad4 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 26 Jan 2022 10:02:16 -0700 Subject: [PATCH 13/34] change move to mover in drivers --- src/QMCDrivers/DMC/DMCBatched.cpp | 16 ++++++++-------- src/QMCDrivers/VMC/VMCBatched.cpp | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 60c241e905..23097c7b44 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -103,11 +103,11 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, const int num_walkers = crowd.size(); - MoveAbstraction move(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, + MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, sft.population.get_num_particles()); //This generates an entire steps worth of deltas. - move.generateDeltas(); + mover.generateDeltas(); std::vector ratios(num_walkers, TrialWaveFunction::PsiValueType(0.0)); std::vector log_gf(num_walkers, 0.0); @@ -130,7 +130,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, ScopedTimer pbyp_local_timer(timers.movepbyp_timer); for (int ig = 0; ig < step_context.get_num_groups(); ++ig) { - move.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); + mover.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -147,12 +147,12 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, : walkers_who_have_been_on_wire[iw] = 0; } #endif - move.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); + mover.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); // only DMC does this // TODO: rr needs a real name std::vector rr(num_walkers, 0.0); - move.updaterr(iat, rr); + mover.updaterr(iat, rr); // in DMC this was done here, changed to match VMCBatched pending factoring to common source // if (rr > m_r2max) @@ -164,9 +164,9 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int i = 0; i < rr.size(); ++i) assert(std::isfinite(rr[i])); #endif - move.makeMove(iat); + mover.makeMove(iat); - move.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); + mover.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); auto checkPhaseChanged = [&sft](const TrialWaveFunction& twf, int& is_reject) { if (sft.branch_engine.phaseChanged(twf.getPhaseDiff())) @@ -187,7 +187,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int iw = 0; iw < num_walkers; ++iw) prob[iw] = std::norm(ratios[iw]) * std::exp(log_gb[iw] - log_gf[iw]); - isAccepted.clear(); + isAcepted.clear(); for (int iw = 0; iw < num_walkers; ++iw) { diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index b5fcaf8304..1c7bf1062b 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -78,18 +78,18 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector> twf_accept_list, twf_reject_list; isAccepted.reserve(num_walkers); - MoveAbstraction move(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, + MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, sft.population.get_num_particles()); for (int sub_step = 0; sub_step < sft.qmcdrv_input.get_sub_steps(); sub_step++) { //This generates an entire steps worth of deltas. - move.generateDeltas(); + mover.generateDeltas(); // up and down electrons are "species" within qmpack for (int ig = 0; ig < step_context.get_num_groups(); ++ig) //loop over species { - move.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); + mover.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -98,17 +98,17 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, for (int iat = start_index; iat < end_index; ++iat) { if (use_drift) - move.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); + mover.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); else - move.calcForwardMove(iat); + mover.calcForwardMove(iat); - move.makeMove(iat); + mover.makeMove(iat); // This is inelegant if (use_drift) - move.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); + mover.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); else - move.updateGreensFunction(twf_dispatcher, walker_twfs, iat, ratios); + mover.updateGreensFunction(twf_dispatcher, walker_twfs, iat, ratios); std::transform(ratios.begin(), ratios.end(), prob.begin(), [](auto ratio) { return std::norm(ratio); }); From b65d66438e7073aad21c5c1bb59c406a76bc1a61 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 26 Jan 2022 10:07:57 -0700 Subject: [PATCH 14/34] use golden_electrons to get spinor info --- src/QMCDrivers/DMC/DMCBatched.cpp | 4 ++-- src/QMCDrivers/VMC/VMCBatched.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 23097c7b44..f235292ca2 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -187,7 +187,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int iw = 0; iw < num_walkers; ++iw) prob[iw] = std::norm(ratios[iw]) * std::exp(log_gb[iw] - log_gf[iw]); - isAcepted.clear(); + isAccepted.clear(); for (int iw = 0; iw < num_walkers; ++iw) { @@ -335,7 +335,7 @@ void DMCBatched::runDMCStep(int crowd_id, // Are we entering the the last step of a block to recompute at? const bool recompute_this_step = (sft.is_recomputing_block && (step + 1) == max_steps); const bool accumulate_this_step = true; - const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, accumulate_this_step); diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 1c7bf1062b..edeb041311 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -213,7 +213,7 @@ void VMCBatched::runVMCStep(int crowd_id, const bool recompute_this_step = (sft.is_recomputing_block && (step + 1) == max_steps); // For VMC we don't call this method for warmup steps. const bool accumulate_this_step = true; - const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, accumulate_this_step); From 7e4e77e804660b7a8eeb9299ef6bf1683d404f08 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 26 Jan 2022 10:10:18 -0700 Subject: [PATCH 15/34] use golden electrons --- src/QMCDrivers/VMC/VMCBatched.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index edeb041311..1196c4095c 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -290,7 +290,7 @@ bool VMCBatched::run() Crowd& crowd = *(crowds[crowd_id]); const bool recompute = false; const bool accumulate_this_step = false; - const bool spin_move = crowd.get_walker_elecs()[0].get().isSpinor(); + const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); From ea7c6811bbb33d1fd0e5917e62f06de67ca89622 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 7 Feb 2022 15:56:42 -0700 Subject: [PATCH 16/34] remove extra definition of function --- src/Particle/ParticleBase/RandomSeqGenerator.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Particle/ParticleBase/RandomSeqGenerator.h b/src/Particle/ParticleBase/RandomSeqGenerator.h index 238e2a0462..eadc655c5c 100644 --- a/src/Particle/ParticleBase/RandomSeqGenerator.h +++ b/src/Particle/ParticleBase/RandomSeqGenerator.h @@ -101,11 +101,5 @@ inline void makeGaussRandomWithEngine(ParticleAttrib& a, RG& rng) assignGaussRand(&(a[0]), a.size(), rng); } -template -inline void makeGaussRandomWithEngine(std::vector& a, RG& rng) -{ - assignGaussRand(&(a[0]), a.size(), rng); -} - } // namespace qmcplusplus #endif From 8d0eab8bd02b308278a3b324451c700cf50a5646 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Mon, 7 Feb 2022 17:12:51 -0700 Subject: [PATCH 17/34] change MoveAbtraction to template on CoordsType --- src/QMCDrivers/DMC/DMCBatched.cpp | 33 ++++++------ src/QMCDrivers/DMC/DMCBatched.h | 3 +- src/QMCDrivers/MoveAbstraction.h | 84 +++++++++++++++---------------- src/QMCDrivers/VMC/VMCBatched.cpp | 37 +++++++------- src/QMCDrivers/VMC/VMCBatched.h | 2 +- 5 files changed, 78 insertions(+), 81 deletions(-) diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index f235292ca2..06daace69d 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -1,4 +1,3 @@ - ////////////////////////////////////////////////////////////////////////////////////// // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. @@ -60,7 +59,7 @@ void DMCBatched::setNonLocalMoveHandler(QMCHamiltonian& golden_hamiltonian) dmcdriver_input_.get_alpha(), dmcdriver_input_.get_gamma()); } -template +template void DMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, @@ -103,8 +102,8 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, const int num_walkers = crowd.size(); - MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, - num_walkers, sft.population.get_num_particles()); + MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, + sft.population.get_num_particles()); //This generates an entire steps worth of deltas. mover.generateDeltas(); @@ -299,15 +298,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, } } -template void DMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - DriverTimers& timers, - DMCTimers& dmc_timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); - -template void DMCBatched::advanceWalkers(const StateForThread& sft, +template void DMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, DMCTimers& dmc_timers, @@ -315,6 +306,14 @@ template void DMCBatched::advanceWalkers(const StateForThread& bool recompute, bool accumulate_this_step); +template void DMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + DriverTimers& timers, + DMCTimers& dmc_timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + void DMCBatched::runDMCStep(int crowd_id, const StateForThread& sft, DriverTimers& timers, @@ -337,11 +336,11 @@ void DMCBatched::runDMCStep(int crowd_id, const bool accumulate_this_step = true; const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) - advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], + recompute_this_step, accumulate_this_step); else - advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + advanceWalkers(sft, crowd, timers, dmc_timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); } void DMCBatched::process(xmlNodePtr node) diff --git a/src/QMCDrivers/DMC/DMCBatched.h b/src/QMCDrivers/DMC/DMCBatched.h index 2ff802bb1d..2e969408c6 100644 --- a/src/QMCDrivers/DMC/DMCBatched.h +++ b/src/QMCDrivers/DMC/DMCBatched.h @@ -18,6 +18,7 @@ #include "QMCDrivers/MCPopulation.h" #include "QMCDrivers/ContextForSteps.h" #include "QMCDrivers/MoveAbstraction.h" +#include "Particle/MCCoords.hpp" namespace qmcplusplus { @@ -133,7 +134,7 @@ class DMCBatched : public QMCDriverNew ///walker controller for load-balance std::unique_ptr walker_controller_; - template + template static void advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 0ed00293ce..a5cb7e2f48 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -20,22 +20,16 @@ #include "QMCDriverInput.h" #include "type_traits/RefVectorWithLeader.h" #include "QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h" -#include "Crowd.h" +#include "Particle/MCCoords.hpp" namespace qmcplusplus { -enum CoordsToMove -{ - POSITIONS, - POSITIONS_SPINS, -}; - /** abstraction class to handle particle moves in batched QMC drivers * - * Templated on enum CoordsToMove - * Currently supports POSITIONS and POSITIONS_SPINS, which includes dynamic spins in particle moves + * Templated on CoordsType defined in MC + * Currently supports CoordsType::POS and CoordsType::POS_SPIN, which includes dynamic spins in particle moves */ -template +template class MoveAbstraction { using Pos = ParticleSet::PosType; @@ -67,7 +61,7 @@ class MoveAbstraction /** sets the timestep information for the move * - * e.g. spatial only moves (POSITIONS) only need the spatial timestep, whereas + * e.g. spatial only moves (CoordsType::POS) only need the spatial timestep, whereas * spin moves need a spin timestep defined by the spin mass provided by the driver input */ void setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass); @@ -199,12 +193,12 @@ class MoveAbstraction }; template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const RefVectorWithLeader& elecs, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), elecs_(elecs), random_gen_(random_gen), @@ -218,12 +212,12 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispat } template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const RefVectorWithLeader& elecs, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), elecs_(elecs), random_gen_(random_gen), @@ -241,20 +235,20 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_ } template<> -inline void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); } template<> -inline void MoveAbstraction::generateDeltas() +inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_, random_gen_); makeGaussRandomWithEngine(walker_spindeltas_, random_gen_); } template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -262,7 +256,8 @@ inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmc } template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, + const Real& invmass) { tauovermass_ = qmcdrv_input.get_tau() * invmass; oneover2tau_ = 0.5 / tauovermass_; @@ -273,9 +268,10 @@ inline void MoveAbstraction::setTauForGroup(const QMCDriverInpu } template<> -inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat) +inline void MoveAbstraction::calcForwardMoveWithDrift( + const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, + const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -287,7 +283,7 @@ inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispat } template<> -inline void MoveAbstraction::calcForwardMoveWithDrift( +inline void MoveAbstraction::calcForwardMoveWithDrift( const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, const int iat) @@ -310,7 +306,7 @@ inline void MoveAbstraction::calcForwardMoveWithDrift( } template<> -inline void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -320,7 +316,7 @@ inline void MoveAbstraction::calcForwardMove(const int iat) } template<> -inline void MoveAbstraction::calcForwardMove(const int iat) +inline void MoveAbstraction::calcForwardMove(const int iat) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; @@ -334,13 +330,13 @@ inline void MoveAbstraction::calcForwardMove(const int iat) } template<> -inline void MoveAbstraction::makeMove(const int iat) +inline void MoveAbstraction::makeMove(const int iat) { ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); } template<> -inline void MoveAbstraction::makeMove(const int iat) +inline void MoveAbstraction::makeMove(const int iat) { ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); ParticleSet& elec_leader = elecs_.getLeader(); @@ -348,7 +344,7 @@ inline void MoveAbstraction::makeMove(const int iat) } template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, const int iat, @@ -374,7 +370,7 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( } template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( +inline void MoveAbstraction::updateGreensFunctionWithDrift( const TWFdispatcher& twf_dispatcher, const RefVectorWithLeader& twfs, const int iat, @@ -418,17 +414,17 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( }); } -template -inline void MoveAbstraction::updateGreensFunction(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios) +template +inline void MoveAbstraction::updateGreensFunction(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, + const int iat, + std::vector& ratios) { twf_dispatcher.flex_calcRatio(twfs, elecs_, iat, ratios); } -template -inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) +template +inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) { auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 1196c4095c..8ad889d99d 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -35,7 +35,7 @@ VMCBatched::VMCBatched(const ProjectData& project_data, collect_samples_(false) {} -template +template void VMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, QMCDriverNew::DriverTimers& timers, @@ -78,8 +78,8 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector> twf_accept_list, twf_reject_list; isAccepted.reserve(num_walkers); - MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, - num_walkers, sft.population.get_num_particles()); + MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, + sft.population.get_num_particles()); for (int sub_step = 0; sub_step < sft.qmcdrv_input.get_sub_steps(); sub_step++) { @@ -182,20 +182,20 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, // check if all moves failed } -template void VMCBatched::advanceWalkers(const StateForThread& sft, - Crowd& crowd, - QMCDriverNew::DriverTimers& timers, - ContextForSteps& step_context, - bool recompute, - bool accumulate_this_step); - -template void VMCBatched::advanceWalkers(const StateForThread& sft, +template void VMCBatched::advanceWalkers(const StateForThread& sft, Crowd& crowd, QMCDriverNew::DriverTimers& timers, ContextForSteps& step_context, bool recompute, bool accumulate_this_step); +template void VMCBatched::advanceWalkers(const StateForThread& sft, + Crowd& crowd, + QMCDriverNew::DriverTimers& timers, + ContextForSteps& step_context, + bool recompute, + bool accumulate_this_step); + /** Thread body for VMC step * */ @@ -215,11 +215,11 @@ void VMCBatched::runVMCStep(int crowd_id, const bool accumulate_this_step = true; const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); else - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, - accumulate_this_step); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute_this_step, + accumulate_this_step); } void VMCBatched::process(xmlNodePtr node) @@ -292,10 +292,11 @@ bool VMCBatched::run() const bool accumulate_this_step = false; const bool spin_move = sft.population.get_golden_electrons()->isSpinor(); if (spin_move) - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, - accumulate_this_step); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, + accumulate_this_step); else - advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, accumulate_this_step); + advanceWalkers(sft, crowd, timers, *context_for_steps[crowd_id], recompute, + accumulate_this_step); }; for (int step = 0; step < qmcdriver_input_.get_warmup_steps(); ++step) diff --git a/src/QMCDrivers/VMC/VMCBatched.h b/src/QMCDrivers/VMC/VMCBatched.h index 69f2bd45b6..e00a23ae18 100644 --- a/src/QMCDrivers/VMC/VMCBatched.h +++ b/src/QMCDrivers/VMC/VMCBatched.h @@ -81,7 +81,7 @@ class VMCBatched : public QMCDriverNew * MCWalkerConfiguration layer removed. * Obfuscation of state changes via buffer and MCWalkerconfiguration require this be tested well */ - template + template static void advanceWalkers(const StateForThread& sft, Crowd& crowd, DriverTimers& timers, From 97a737c802b24000bb1ff7f455212d8b70b8759a Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 8 Feb 2022 09:44:14 -0700 Subject: [PATCH 18/34] use Taus object --- src/QMCDrivers/MoveAbstraction.h | 63 +++++++++++--------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index a5cb7e2f48..f23e3f2df9 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -173,22 +173,8 @@ class MoveAbstraction RandomGenerator& random_gen_; /// drift modifer used to limit size of drift velocity const DriftModifierBase& drift_modifier_; - /// spatial timestep - Real tauovermass_; - /// \f$\frac{1}{2\tau}$\f, used in Green's function - Real oneover2tau_; - /// \f$\sqrt{\tau}$\f, used to scale the diffusion part of move - Real sqrttau_; - /** spin timestep, defined by spin mass and spatial timestep. - * - * Note that \f$\tau_{\rm spin} = \frac{\tau}{m_{\rm spin}}$\f} - */ - Real spintauovermass_; - /// \f$\frac{1}{2\tau_{\rm spin}}$\f, used in spin part of green's function - Real oneover2spintau_; - /// \f$\sqrt{\tau_{\rm spin}}$\f, used to scale the diffusion part of spin move - Real sqrtspintau_; - /// total number of walkers in crowd. + /// timesteps + std::unique_ptr> taus_; const int num_walkers_; }; @@ -203,6 +189,7 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_ elecs_(elecs), random_gen_(random_gen), drift_modifier_(drift_modifier), + taus_(nullptr), num_walkers_(num_walkers) { drifts_.resize(num_walkers_); @@ -222,6 +209,7 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher elecs_(elecs), random_gen_(random_gen), drift_modifier_(drift_modifier), + taus_(nullptr), num_walkers_(num_walkers) { drifts_.resize(num_walkers_); @@ -250,21 +238,14 @@ inline void MoveAbstraction::generateDeltas() template<> inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { - tauovermass_ = qmcdrv_input.get_tau() * invmass; - oneover2tau_ = 0.5 / tauovermass_; - sqrttau_ = std::sqrt(tauovermass_); + taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass); } template<> inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { - tauovermass_ = qmcdrv_input.get_tau() * invmass; - oneover2tau_ = 0.5 / tauovermass_; - sqrttau_ = std::sqrt(tauovermass_); - spintauovermass_ = tauovermass_ / qmcdrv_input.get_spin_mass(); - oneover2spintau_ = 0.5 / spintauovermass_; - sqrtspintau_ = std::sqrt(spintauovermass_); + taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass, qmcdrv_input.get_spin_mass()); } template<> @@ -277,9 +258,9 @@ inline void MoveAbstraction::calcForwardMoveWithDrift( auto delta_r_end = delta_r_start + num_walkers_; twf_dispatcher.flex_evalGrad(twfs, elecs_, iat, grads_now_); - drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); + drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_); std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), - [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); + [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); } template<> @@ -294,13 +275,13 @@ inline void MoveAbstraction::calcForwardMoveWithDrift( auto delta_spin_end = delta_spin_start + num_walkers_; twf_dispatcher.flex_evalGradWithSpin(twfs, elecs_, iat, grads_now_, spingrads_now_); - drift_modifier_.getDrifts(tauovermass_, grads_now_, drifts_); + drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_); std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), - [st = sqrttau_](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); + [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); //spin part of forward move - drift_modifier_.getDrifts(spintauovermass_, spingrads_now_, spindrifts_); + drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_now_, spindrifts_); std::transform(spindrifts_.begin(), spindrifts_.end(), delta_spin_start, spindrifts_.begin(), - [st = sqrtspintau_](const Scalar& spindrift, const Scalar& delta_spin) { + [st = taus_->spin_sqrttau](const Scalar& spindrift, const Scalar& delta_spin) { return spindrift + (st * delta_spin); }); } @@ -312,7 +293,7 @@ inline void MoveAbstraction::calcForwardMove(const int iat) auto delta_r_end = delta_r_start + num_walkers_; std::transform(delta_r_start, delta_r_end, drifts_.begin(), - [st = sqrttau_](const Pos& delta_r) { return st * delta_r; }); + [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); } template<> @@ -324,9 +305,9 @@ inline void MoveAbstraction::calcForwardMove(const int iat auto delta_spin_end = delta_spin_start + num_walkers_; std::transform(delta_r_start, delta_r_end, drifts_.begin(), - [st = sqrttau_](const Pos& delta_r) { return st * delta_r; }); + [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); std::transform(delta_spin_start, delta_spin_end, spindrifts_.begin(), - [st = sqrtspintau_](const Scalar& delta_spin) { return st * delta_spin; }); + [st = taus_->spin_sqrttau](const Scalar& delta_spin) { return st * delta_spin; }); } template<> @@ -360,13 +341,13 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift( constexpr Real mhalf(-0.5); return mhalf * dot(delta_r, delta_r); }); - drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); + drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_); std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), - [halfovertau = oneover2tau_](const Pos& drift) { return -halfovertau * dot(drift, drift); }); + [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); } template<> @@ -395,8 +376,8 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift return loggf + mhalf * delta_spin * delta_spin; }); - drift_modifier_.getDrifts(tauovermass_, grads_new_, drifts_); - drift_modifier_.getDrifts(spintauovermass_, spingrads_new_, spindrifts_); + drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_); + drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_new_, spindrifts_); std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); @@ -406,10 +387,10 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift }); std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), - [halfovertau = oneover2tau_](const Pos& drift) { return -halfovertau * dot(drift, drift); }); + [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); //add spin part to greens function std::transform(spindrifts_.begin(), spindrifts_.end(), log_gb.begin(), log_gb.begin(), - [halfovertau = oneover2spintau_](const Scalar& spindrift, const Real& loggb) { + [halfovertau = taus_->spin_oneover2tau](const Scalar& spindrift, const Real& loggb) { return loggb - halfovertau * spindrift * spindrift; }); } @@ -430,7 +411,7 @@ inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) auto delta_r_end = delta_r_start + num_walkers_; assert(rr.size() == delta_r_end - delta_r_start); std::transform(delta_r_start, delta_r_end, rr.begin(), - [t = tauovermass_](auto& delta_r) { return t * dot(delta_r, delta_r); }); + [t = taus_->tauovermass](auto& delta_r) { return t * dot(delta_r, delta_r); }); } } // namespace qmcplusplus From b10e42d96ebd71b3d1ebd6215bf77ef2240587d4 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 8 Feb 2022 17:28:27 -0700 Subject: [PATCH 19/34] change MoveAbstraction members to use MCCoords --- src/Particle/MCCoords.hpp | 3 +- src/QMCDrivers/MoveAbstraction.h | 273 +++++++++++-------------------- 2 files changed, 101 insertions(+), 175 deletions(-) diff --git a/src/Particle/MCCoords.hpp b/src/Particle/MCCoords.hpp index 1caabd8bd5..8d3339c1a7 100644 --- a/src/Particle/MCCoords.hpp +++ b/src/Particle/MCCoords.hpp @@ -19,7 +19,6 @@ namespace qmcplusplus { - enum class CoordsType { POS, @@ -44,7 +43,7 @@ struct MCCoords spins.resize(size); } std::vector positions; - std::vector spins; + std::vector spins; }; /** Object to encapsulate appropriate tau derived values diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index f23e3f2df9..8e136ee16a 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -154,15 +154,11 @@ class MoveAbstraction private: /// spatial drift part of move for a single particle across multiple walkers - std::vector drifts_; + MCCoords drifts_; /// all of the spatial diffusion moves for all walkers and particles for a given step - std::vector walker_deltas_; + MCCoords walker_deltas_; /// spatial gradients for a single electron across multiple walkers std::vector grads_now_, grads_new_; - /// spin drift part of move for a single particle across multiple walkers - std::vector spindrifts_; - /// all of the spin diffusion moves for all walkerss and particles for a given step - std::vector walker_spindeltas_; /// spin gradients for a single electrons across multiple walkers std::vector spingrads_now_, spingrads_new_; /// provides flex_ APIs to do select sw/mw updates of particle set @@ -174,37 +170,17 @@ class MoveAbstraction /// drift modifer used to limit size of drift velocity const DriftModifierBase& drift_modifier_; /// timesteps - std::unique_ptr> taus_; + std::unique_ptr> taus_; const int num_walkers_; }; -template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) - : ps_dispatcher_(ps_dispatcher), - elecs_(elecs), - random_gen_(random_gen), - drift_modifier_(drift_modifier), - taus_(nullptr), - num_walkers_(num_walkers) -{ - drifts_.resize(num_walkers_); - walker_deltas_.resize(num_walkers_ * num_particles); - grads_now_.resize(num_walkers_); - grads_new_.resize(num_walkers_); -} - -template<> -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) +template +inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, + const RefVectorWithLeader& elecs, + RandomGenerator& random_gen, + const DriftModifierBase& drift_modifier, + const int num_walkers, + const int num_particles) : ps_dispatcher_(ps_dispatcher), elecs_(elecs), random_gen_(random_gen), @@ -216,183 +192,134 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher walker_deltas_.resize(num_walkers_ * num_particles); grads_now_.resize(num_walkers_); grads_new_.resize(num_walkers_); - spindrifts_.resize(num_walkers_); - walker_spindeltas_.resize(num_walkers_ * num_particles); - spingrads_now_.resize(num_walkers_); - spingrads_new_.resize(num_walkers_); -} - -template<> -inline void MoveAbstraction::generateDeltas() -{ - makeGaussRandomWithEngine(walker_deltas_, random_gen_); -} - -template<> -inline void MoveAbstraction::generateDeltas() -{ - makeGaussRandomWithEngine(walker_deltas_, random_gen_); - makeGaussRandomWithEngine(walker_spindeltas_, random_gen_); + if constexpr (std::is_same, MCCoords>::value) + { + spingrads_now_.resize(num_walkers_); + spingrads_new_.resize(num_walkers_); + } } -template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) +template +inline void MoveAbstraction::generateDeltas() { - taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass); + makeGaussRandomWithEngine(walker_deltas_.positions, random_gen_); + if constexpr (std::is_same, MCCoords>::value) + makeGaussRandomWithEngine(walker_deltas_.spins, random_gen_); } -template<> -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, - const Real& invmass) +template +inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { - taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass, qmcdrv_input.get_spin_mass()); + if constexpr (std::is_same, MCCoords>::value) + taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass, + qmcdrv_input.get_spin_mass()); + else + taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass); } -template<> -inline void MoveAbstraction::calcForwardMoveWithDrift( - const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat) +template +inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, + const int iat) { - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; twf_dispatcher.flex_evalGrad(twfs, elecs_, iat, grads_now_); - drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_); - std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), + drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_.positions); + std::transform(drifts_.positions.begin(), drifts_.positions.end(), delta_r_start, drifts_.positions.begin(), [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); -} -template<> -inline void MoveAbstraction::calcForwardMoveWithDrift( - const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat) -{ - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; - - twf_dispatcher.flex_evalGradWithSpin(twfs, elecs_, iat, grads_now_, spingrads_now_); - drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_); - std::transform(drifts_.begin(), drifts_.end(), delta_r_start, drifts_.begin(), - [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); - //spin part of forward move - drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_now_, spindrifts_); - std::transform(spindrifts_.begin(), spindrifts_.end(), delta_spin_start, spindrifts_.begin(), - [st = taus_->spin_sqrttau](const Scalar& spindrift, const Scalar& delta_spin) { - return spindrift + (st * delta_spin); - }); + if constexpr (std::is_same, MCCoords>::value) + { + auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_now_, drifts_.spins); + std::transform(drifts_.spins.begin(), drifts_.spins.end(), delta_spin_start, drifts_.spins.begin(), + [st = taus_->spin_sqrttau](const Scalar& spindrift, const Scalar& delta_spin) { + return spindrift + (st * delta_spin); + }); + } } -template<> -inline void MoveAbstraction::calcForwardMove(const int iat) +template +inline void MoveAbstraction::calcForwardMove(const int iat) { - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; - std::transform(delta_r_start, delta_r_end, drifts_.begin(), + std::transform(delta_r_start, delta_r_end, drifts_.positions.begin(), [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); -} -template<> -inline void MoveAbstraction::calcForwardMove(const int iat) -{ - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; - - std::transform(delta_r_start, delta_r_end, drifts_.begin(), - [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); - std::transform(delta_spin_start, delta_spin_end, spindrifts_.begin(), - [st = taus_->spin_sqrttau](const Scalar& delta_spin) { return st * delta_spin; }); + if constexpr (std::is_same, MCCoords>::value) + { + auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + std::transform(delta_spin_start, delta_spin_end, drifts_.spins.begin(), + [st = taus_->spin_sqrttau](const Scalar& delta_spin) { return st * delta_spin; }); + } } -template<> -inline void MoveAbstraction::makeMove(const int iat) +template +inline void MoveAbstraction::makeMove(const int iat) { - ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); + ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_.positions); + if constexpr (std::is_same, MCCoords>::value) + { + ParticleSet& elec_leader = elecs_.getLeader(); + elec_leader.mw_makeSpinMove(elecs_, iat, drifts_.spins); + } } -template<> -inline void MoveAbstraction::makeMove(const int iat) -{ - ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); - ParticleSet& elec_leader = elecs_.getLeader(); - elec_leader.mw_makeSpinMove(elecs_, iat, spindrifts_); -} -template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( - const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios, - std::vector& log_gf, - std::vector& log_gb) +template +inline void MoveAbstraction::updateGreensFunctionWithDrift(const TWFdispatcher& twf_dispatcher, + const RefVectorWithLeader& twfs, + const int iat, + std::vector& ratios, + std::vector& log_gf, + std::vector& log_gb) { - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - - twf_dispatcher.flex_calcRatioGrad(twfs, elecs_, iat, ratios, grads_new_); - std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { - constexpr Real mhalf(-0.5); - return mhalf * dot(delta_r, delta_r); - }); - drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_); - - std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), - [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); + if constexpr (std::is_same, MCCoords>::value) + twf_dispatcher.flex_calcRatioGradWithSpin(twfs, elecs_, iat, ratios, grads_new_, spingrads_new_); + else + twf_dispatcher.flex_calcRatioGrad(twfs, elecs_, iat, ratios, grads_new_); - std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), - [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); -} - -template<> -inline void MoveAbstraction::updateGreensFunctionWithDrift( - const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios, - std::vector& log_gf, - std::vector& log_gb) -{ - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - auto delta_spin_start = walker_spindeltas_.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; + auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; + auto delta_r_end = delta_r_start + num_walkers_; - twf_dispatcher.flex_calcRatioGradWithSpin(twfs, elecs_, iat, ratios, grads_new_, spingrads_new_); std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { constexpr Real mhalf(-0.5); return mhalf * dot(delta_r, delta_r); }); - //add spin part to greens function - std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), - [](const Scalar& delta_spin, const Real& loggf) { - constexpr Real mhalf(-0.5); - return loggf + mhalf * delta_spin * delta_spin; - }); - drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_); - drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_new_, spindrifts_); + drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_.positions); - std::transform(elecs_.begin(), elecs_.end(), drifts_.begin(), drifts_.begin(), + std::transform(elecs_.begin(), elecs_.end(), drifts_.positions.begin(), drifts_.positions.begin(), [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); - std::transform(elecs_.begin(), elecs_.end(), spindrifts_.begin(), spindrifts_.begin(), - [iat](const ParticleSet& ps, const Scalar& spindrift) { - return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; - }); - std::transform(drifts_.begin(), drifts_.end(), log_gb.begin(), + std::transform(drifts_.positions.begin(), drifts_.positions.end(), log_gb.begin(), [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); - //add spin part to greens function - std::transform(spindrifts_.begin(), spindrifts_.end(), log_gb.begin(), log_gb.begin(), - [halfovertau = taus_->spin_oneover2tau](const Scalar& spindrift, const Real& loggb) { - return loggb - halfovertau * spindrift * spindrift; - }); + + if constexpr (std::is_same, MCCoords>::value) + { + auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; + auto delta_spin_end = delta_spin_start + num_walkers_; + std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), + [](const Scalar& delta_spin, const Real& loggf) { + constexpr Real mhalf(-0.5); + return loggf + mhalf * delta_spin * delta_spin; + }); + drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_new_, drifts_.spins); + std::transform(elecs_.begin(), elecs_.end(), drifts_.spins.begin(), drifts_.spins.begin(), + [iat](const ParticleSet& ps, const Scalar& spindrift) { + return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; + }); + std::transform(drifts_.spins.begin(), drifts_.spins.end(), log_gb.begin(), log_gb.begin(), + [halfovertau = taus_->spin_oneover2tau](const Scalar& spindrift, const Real& loggb) { + return loggb - halfovertau * spindrift * spindrift; + }); + } } template @@ -407,7 +334,7 @@ inline void MoveAbstraction::updateGreensFunction(const TWFdispatcher& twf_d template inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) { - auto delta_r_start = walker_deltas_.begin() + iat * num_walkers_; + auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; auto delta_r_end = delta_r_start + num_walkers_; assert(rr.size() == delta_r_end - delta_r_start); std::transform(delta_r_start, delta_r_end, rr.begin(), From c98ed9c23c5fe6fa74e0e2fcabfbd9970c475502 Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Tue, 8 Feb 2022 20:43:37 -0600 Subject: [PATCH 20/34] Add template version of flex/mw_makeMove. --- src/Particle/PSdispatcher.cpp | 14 ++++++++++++-- src/Particle/PSdispatcher.h | 5 ++--- src/Particle/ParticleSet.h | 9 +++++++++ src/QMCDrivers/MoveAbstraction.h | 7 +------ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Particle/PSdispatcher.cpp b/src/Particle/PSdispatcher.cpp index 1decfb64d4..e74e1f4863 100644 --- a/src/Particle/PSdispatcher.cpp +++ b/src/Particle/PSdispatcher.cpp @@ -42,15 +42,19 @@ void PSdispatcher::flex_update(const RefVectorWithLeader& p_list, b pset.update(skipSK); } +template void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, int iat, - const std::vector& displs) const + const MCCoords& displs) const { if (use_batch_) ParticleSet::mw_makeMove(p_list, iat, displs); else for (size_t iw = 0; iw < p_list.size(); iw++) - p_list[iw].makeMove(iat, displs[iw]); + if constexpr (CT == CoordsType::POS_SPIN) + p_list[iw].makeMoveWithSpin(iat, displs.positions[iw], displs.spins[iw]); + else + p_list[iw].makeMove(iat, displs.positions[iw]); } void PSdispatcher::flex_accept_rejectMove(const RefVectorWithLeader& p_list, @@ -84,4 +88,10 @@ void PSdispatcher::flex_saveWalker(const RefVectorWithLeader& p_lis p_list[iw].saveWalker(walkers[iw]); } +template void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, + int iat, + const MCCoords& displs) const; +template void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, + int iat, + const MCCoords& displs) const; } // namespace qmcplusplus diff --git a/src/Particle/PSdispatcher.h b/src/Particle/PSdispatcher.h index 583a3a1627..bf0b4a9d27 100644 --- a/src/Particle/PSdispatcher.h +++ b/src/Particle/PSdispatcher.h @@ -37,9 +37,8 @@ class PSdispatcher void flex_update(const RefVectorWithLeader& p_list, bool skipSK = false) const; - void flex_makeMove(const RefVectorWithLeader& p_list, - int iat, - const std::vector& displs) const; + template + void flex_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs) const; void flex_accept_rejectMove(const RefVectorWithLeader& p_list, int iat, diff --git a/src/Particle/ParticleSet.h b/src/Particle/ParticleSet.h index 762a74e8ab..41fe8c050e 100644 --- a/src/Particle/ParticleSet.h +++ b/src/Particle/ParticleSet.h @@ -31,6 +31,7 @@ #include "OhmmsSoA/VectorSoaContainer.h" #include "type_traits/template_types.hpp" #include "SimulationCell.h" +#include "MCCoords.hpp" #include "DTModes.h" namespace qmcplusplus @@ -281,6 +282,14 @@ class ParticleSet : public QMCTraits, public OhmmsElementBase, public PtclOnLatt void makeMoveWithSpin(Index_t iat, const SingleParticlePos& displ, const Scalar_t& sdispl); /// batched version of makeMove + template + static void mw_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs) + { + mw_makeMove(p_list, iat, displs.positions); + if constexpr (CT == CoordsType::POS_SPIN) + mw_makeSpinMove(p_list, iat, displs.spins); + } + static void mw_makeMove(const RefVectorWithLeader& p_list, int iat, const std::vector& displs); diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 8e136ee16a..213adcf111 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -263,12 +263,7 @@ inline void MoveAbstraction::calcForwardMove(const int iat) template inline void MoveAbstraction::makeMove(const int iat) { - ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_.positions); - if constexpr (std::is_same, MCCoords>::value) - { - ParticleSet& elec_leader = elecs_.getLeader(); - elec_leader.mw_makeSpinMove(elecs_, iat, drifts_.spins); - } + ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); } From fe2b0546ed138d6adbbdea66178e41d4dcf90c41 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 9 Feb 2022 10:16:13 -0700 Subject: [PATCH 21/34] add template version of flex accept/reject --- src/Particle/PSdispatcher.cpp | 11 +++++++++- src/Particle/PSdispatcher.h | 1 + src/Particle/ParticleSet.cpp | 36 +++++++++++++++++++++++++++++++ src/Particle/ParticleSet.h | 15 +++++++------ src/QMCDrivers/DMC/DMCBatched.cpp | 2 +- src/QMCDrivers/VMC/VMCBatched.cpp | 2 +- 6 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/Particle/PSdispatcher.cpp b/src/Particle/PSdispatcher.cpp index e74e1f4863..44dbb23965 100644 --- a/src/Particle/PSdispatcher.cpp +++ b/src/Particle/PSdispatcher.cpp @@ -57,13 +57,14 @@ void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, p_list[iw].makeMove(iat, displs.positions[iw]); } +template void PSdispatcher::flex_accept_rejectMove(const RefVectorWithLeader& p_list, int iat, const std::vector& isAccepted, bool forward_mode) const { if (use_batch_) - ParticleSet::mw_accept_rejectMove(p_list, iat, isAccepted, forward_mode); + ParticleSet::mw_accept_rejectMove(p_list, iat, isAccepted, forward_mode); else for (size_t iw = 0; iw < p_list.size(); iw++) p_list[iw].accept_rejectMove(iat, isAccepted[iw], forward_mode); @@ -94,4 +95,12 @@ template void PSdispatcher::flex_makeMove(const RefVectorWithLe template void PSdispatcher::flex_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs) const; +template void PSdispatcher::flex_accept_rejectMove(const RefVectorWithLeader& p_list, + int iat, + const std::vector& isAccepted, + bool forward_mode) const; +template void PSdispatcher::flex_accept_rejectMove(const RefVectorWithLeader& p_list, + int iat, + const std::vector& isAccepted, + bool forward_mode) const; } // namespace qmcplusplus diff --git a/src/Particle/PSdispatcher.h b/src/Particle/PSdispatcher.h index bf0b4a9d27..50b03c13a6 100644 --- a/src/Particle/PSdispatcher.h +++ b/src/Particle/PSdispatcher.h @@ -40,6 +40,7 @@ class PSdispatcher template void flex_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs) const; + template void flex_accept_rejectMove(const RefVectorWithLeader& p_list, int iat, const std::vector& isAccepted, diff --git a/src/Particle/ParticleSet.cpp b/src/Particle/ParticleSet.cpp index 30ff28bafe..8619a02838 100644 --- a/src/Particle/ParticleSet.cpp +++ b/src/Particle/ParticleSet.cpp @@ -398,6 +398,14 @@ void ParticleSet::makeMoveWithSpin(Index_t iat, const SingleParticlePos& displ, active_spin_val_ += sdispl; } +template +void ParticleSet::mw_makeMove(const RefVectorWithLeader& p_list, Index_t iat, const MCCoords& displs) +{ + mw_makeMove(p_list, iat, displs.positions); + if constexpr (CT == CoordsType::POS_SPIN) + mw_makeSpinMove(p_list, iat, displs.spins); +} + void ParticleSet::mw_makeMove(const RefVectorWithLeader& p_list, Index_t iat, const std::vector& displs) @@ -697,6 +705,18 @@ void ParticleSet::rejectMoveForwardMode(Index_t iat) active_ptcl_ = -1; } +template +void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted, + bool forward_mode) +{ + mw_accept_rejectMove(p_list, iat, isAccepted, forward_mode); + if (CT == CoordsType::POS_SPIN) + mw_accept_rejectSpinMove(p_list, iat, isAccepted); +} + + void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p_list, Index_t iat, const std::vector& isAccepted, @@ -980,4 +1000,20 @@ RefVectorWithLeader ParticleSet::extractSKRefList(const RefVectorWit return sk_list; } +//explicit instantiations +template void ParticleSet::mw_makeMove(const RefVectorWithLeader& p_list, + Index_t iat, + const MCCoords& displs); +template void ParticleSet::mw_makeMove(const RefVectorWithLeader& p_list, + Index_t iat, + const MCCoords& displs); +template void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted, + bool forward_mode); +template void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted, + bool forward_mode); + } // namespace qmcplusplus diff --git a/src/Particle/ParticleSet.h b/src/Particle/ParticleSet.h index 41fe8c050e..dffed313b6 100644 --- a/src/Particle/ParticleSet.h +++ b/src/Particle/ParticleSet.h @@ -283,12 +283,7 @@ class ParticleSet : public QMCTraits, public OhmmsElementBase, public PtclOnLatt /// batched version of makeMove template - static void mw_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs) - { - mw_makeMove(p_list, iat, displs.positions); - if constexpr (CT == CoordsType::POS_SPIN) - mw_makeSpinMove(p_list, iat, displs.spins); - } + static void mw_makeMove(const RefVectorWithLeader& p_list, int iat, const MCCoords& displs); static void mw_makeMove(const RefVectorWithLeader& p_list, int iat, @@ -381,6 +376,14 @@ class ParticleSet : public QMCTraits, public OhmmsElementBase, public PtclOnLatt * @param iat the electron whose proposed move gets rejected. */ void rejectMove(Index_t iat); + + /// batched version of acceptMove and rejectMove fused, templated on CoordsType + template + static void mw_accept_rejectMove(const RefVectorWithLeader& p_list, + Index_t iat, + const std::vector& isAccepted, + bool forward_mode = true); + /// batched version of acceptMove and rejectMove fused static void mw_accept_rejectMove(const RefVectorWithLeader& p_list, Index_t iat, diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 06daace69d..aee3e5da43 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -206,7 +206,7 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, twf_dispatcher.flex_accept_rejectMove(walker_twfs, walker_elecs, iat, isAccepted, true); - ps_dispatcher.flex_accept_rejectMove(walker_elecs, iat, isAccepted); + ps_dispatcher.flex_accept_rejectMove(walker_elecs, iat, isAccepted); } } diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 8ad889d99d..677a0bcba2 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -129,7 +129,7 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, twf_dispatcher.flex_accept_rejectMove(walker_twfs, walker_elecs, iat, isAccepted, true); - ps_dispatcher.flex_accept_rejectMove(walker_elecs, iat, isAccepted); + ps_dispatcher.flex_accept_rejectMove(walker_elecs, iat, isAccepted); } } twf_dispatcher.flex_completeUpdates(walker_twfs); From 28dc604be9ca9c00f5457a1853df5b74e169e36c Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 9 Feb 2022 10:19:49 -0700 Subject: [PATCH 22/34] simplify check on CoordsType in MoveAbstraction --- src/QMCDrivers/MoveAbstraction.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h index 213adcf111..e455b3650c 100644 --- a/src/QMCDrivers/MoveAbstraction.h +++ b/src/QMCDrivers/MoveAbstraction.h @@ -192,7 +192,7 @@ inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, walker_deltas_.resize(num_walkers_ * num_particles); grads_now_.resize(num_walkers_); grads_new_.resize(num_walkers_); - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) { spingrads_now_.resize(num_walkers_); spingrads_new_.resize(num_walkers_); @@ -203,14 +203,14 @@ template inline void MoveAbstraction::generateDeltas() { makeGaussRandomWithEngine(walker_deltas_.positions, random_gen_); - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) makeGaussRandomWithEngine(walker_deltas_.spins, random_gen_); } template inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) { - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass, qmcdrv_input.get_spin_mass()); else @@ -230,7 +230,7 @@ inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispatcher& t std::transform(drifts_.positions.begin(), drifts_.positions.end(), delta_r_start, drifts_.positions.begin(), [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) { auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; auto delta_spin_end = delta_spin_start + num_walkers_; @@ -251,7 +251,7 @@ inline void MoveAbstraction::calcForwardMove(const int iat) std::transform(delta_r_start, delta_r_end, drifts_.positions.begin(), [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) { auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; auto delta_spin_end = delta_spin_start + num_walkers_; @@ -275,7 +275,7 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift(const TWFdispatch std::vector& log_gf, std::vector& log_gb) { - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) twf_dispatcher.flex_calcRatioGradWithSpin(twfs, elecs_, iat, ratios, grads_new_, spingrads_new_); else twf_dispatcher.flex_calcRatioGrad(twfs, elecs_, iat, ratios, grads_new_); @@ -296,7 +296,7 @@ inline void MoveAbstraction::updateGreensFunctionWithDrift(const TWFdispatch std::transform(drifts_.positions.begin(), drifts_.positions.end(), log_gb.begin(), [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); - if constexpr (std::is_same, MCCoords>::value) + if constexpr (CT == CoordsType::POS_SPIN) { auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; auto delta_spin_end = delta_spin_start + num_walkers_; From c5991a41db9d06e2a08b49b02ec145ecd41a1c59 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 9 Feb 2022 15:59:21 -0700 Subject: [PATCH 23/34] add TWFGrads and template TWFdispatcher flex_ and DriftModifierBase --- src/Particle/MCCoords.hpp | 2 +- .../ParticleBase/RandomSeqGenerator.h | 16 +-- src/QMCDrivers/DMC/DMCBatched.cpp | 92 ++++++++++++-- .../DriftModifierBase.h | 13 ++ src/QMCDrivers/VMC/VMCBatched.cpp | 114 ++++++++++++++++-- src/QMCWaveFunctions/TWFGrads.hpp | 44 +++++++ src/QMCWaveFunctions/TWFdispatcher.cpp | 45 +++++++ src/QMCWaveFunctions/TWFdispatcher.h | 14 +++ 8 files changed, 312 insertions(+), 28 deletions(-) create mode 100644 src/QMCWaveFunctions/TWFGrads.hpp diff --git a/src/Particle/MCCoords.hpp b/src/Particle/MCCoords.hpp index 8d3339c1a7..593baf0635 100644 --- a/src/Particle/MCCoords.hpp +++ b/src/Particle/MCCoords.hpp @@ -43,7 +43,7 @@ struct MCCoords spins.resize(size); } std::vector positions; - std::vector spins; + std::vector spins; }; /** Object to encapsulate appropriate tau derived values diff --git a/src/Particle/ParticleBase/RandomSeqGenerator.h b/src/Particle/ParticleBase/RandomSeqGenerator.h index eadc655c5c..9377992c94 100644 --- a/src/Particle/ParticleBase/RandomSeqGenerator.h +++ b/src/Particle/ParticleBase/RandomSeqGenerator.h @@ -79,14 +79,6 @@ inline void makeGaussRandomWithEngine(std::vector>& a, RG& rng) assignGaussRand(&(a[0][0]), a.size() * D, rng); } -template -inline void makeGaussRandomWithEngine(MCCoords& a, RG& rng) -{ - makeGaussRandomWithEngine(a.positions, rng); - if constexpr (std::is_same, MCCoords>::value) - makeGaussRandomWithEngine(a.spins, rng); -} - template inline void makeGaussRandomWithEngine(std::vector& a, RG& rng) { @@ -101,5 +93,13 @@ inline void makeGaussRandomWithEngine(ParticleAttrib& a, RG& rng) assignGaussRand(&(a[0]), a.size(), rng); } +template +inline void makeGaussRandomWithEngine(MCCoords& a, RG& rng) +{ + makeGaussRandomWithEngine(a.positions, rng); + if constexpr (CT == CoordsType::POS_SPIN) + makeGaussRandomWithEngine(a.spins, rng); +} + } // namespace qmcplusplus #endif diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index aee3e5da43..857da149f7 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -25,6 +25,7 @@ #include "QMCDrivers/DMC/WalkerControl.h" #include "QMCDrivers/SFNBranch.h" #include "MemoryUsage.h" +#include "QMCWaveFunctions/TWFGrads.hpp" namespace qmcplusplus { @@ -100,13 +101,18 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, twf_dispatcher.flex_recompute(walker_twfs, walker_elecs, recompute_mask); } - const int num_walkers = crowd.size(); + const int num_walkers = crowd.size(); + const int num_particles = sft.population.get_num_particles(); - MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, - sft.population.get_num_particles()); + MCCoords drifts, walker_deltas; + TWFGrads grads_now, grads_new; + drifts.resize(num_walkers); + walker_deltas.resize(num_walkers * num_particles); + grads_now.resize(num_walkers); + grads_new.resize(num_walkers); //This generates an entire steps worth of deltas. - mover.generateDeltas(); + makeGaussRandomWithEngine(walker_deltas, step_context.get_random_gen()); std::vector ratios(num_walkers, TrialWaveFunction::PsiValueType(0.0)); std::vector log_gf(num_walkers, 0.0); @@ -129,7 +135,16 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, ScopedTimer pbyp_local_timer(timers.movepbyp_timer); for (int ig = 0; ig < step_context.get_num_groups(); ++ig) { - mover.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + auto getTaus = [&](const int ig) { + if constexpr (CT == CoordsType::POS_SPIN) + return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], + sft.qmcdrv_input.get_spin_mass()); + else + return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig]); + }; + + Taus taus = getTaus(ig); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -146,12 +161,33 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, : walkers_who_have_been_on_wire[iw] = 0; } #endif - mover.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); + twf_dispatcher.flex_evalGrad(walker_twfs, walker_elecs, iat, grads_now); + sft.drift_modifier.getDrifts(taus, grads_now, drifts); + //need to abstract this next bit of code + auto delta_r_start = walker_deltas.positions.begin() + iat * num_walkers; + auto delta_r_end = delta_r_start + num_walkers; + std::transform(drifts.positions.begin(), drifts.positions.end(), delta_r_start, drifts.positions.begin(), + [st = taus.sqrttau](const PosType& drift, const PosType& delta_r) { + return drift + (st * delta_r); + }); + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + if constexpr (CT == CoordsType::POS_SPIN) + { + auto delta_spin_start = walker_deltas.spins.begin() + iat * num_walkers; + auto delta_spin_end = delta_spin_start + num_walkers; + std::transform(drifts.spins.begin(), drifts.spins.end(), delta_spin_start, drifts.spins.begin(), + [st = taus.spin_sqrttau](const ParticleSet::Scalar_t& spindrift, + const ParticleSet::Scalar_t& delta_spin) { + return spindrift + (st * delta_spin); + }); + } // only DMC does this // TODO: rr needs a real name std::vector rr(num_walkers, 0.0); - mover.updaterr(iat, rr); + assert(rr.size() == delta_r_end - delta_r_start); + std::transform(delta_r_start, delta_r_end, rr.begin(), + [t = taus.tauovermass](auto& delta_r) { return t * dot(delta_r, delta_r); }); // in DMC this was done here, changed to match VMCBatched pending factoring to common source // if (rr > m_r2max) @@ -163,9 +199,47 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, for (int i = 0; i < rr.size(); ++i) assert(std::isfinite(rr[i])); #endif - mover.makeMove(iat); - mover.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); + ps_dispatcher.flex_makeMove(walker_elecs, iat, drifts); + + twf_dispatcher.flex_calcRatioGrad(walker_twfs, walker_elecs, iat, ratios, grads_new); + + std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const PosType& delta_r) { + constexpr RealType mhalf(-0.5); + return mhalf * dot(delta_r, delta_r); + }); + + sft.drift_modifier.getDrifts(taus, grads_new, drifts); + std::transform(walker_elecs.begin(), walker_elecs.end(), drifts.positions.begin(), drifts.positions.begin(), + [iat](const ParticleSet& ps, const PosType& drift) { + return ps.R[iat] - ps.getActivePos() - drift; + }); + + std::transform(drifts.positions.begin(), drifts.positions.end(), log_gb.begin(), + [halfovertau = taus.oneover2tau](const PosType& drift) { + return -halfovertau * dot(drift, drift); + }); + + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + if constexpr (CT == CoordsType::POS_SPIN) + { + auto delta_spin_start = walker_deltas.spins.begin() + iat * num_walkers; + auto delta_spin_end = delta_spin_start + num_walkers; + std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), + [](const ParticleSet::Scalar_t& delta_spin, const RealType& loggf) { + constexpr RealType mhalf(-0.5); + return loggf + mhalf * delta_spin * delta_spin; + }); + std::transform(walker_elecs.begin(), walker_elecs.end(), drifts.spins.begin(), drifts.spins.begin(), + [iat](const ParticleSet& ps, const ParticleSet::Scalar_t& spindrift) { + return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; + }); + std::transform(drifts.spins.begin(), drifts.spins.end(), log_gb.begin(), log_gb.begin(), + [halfovertau = taus.spin_oneover2tau](const ParticleSet::Scalar_t& spindrift, + const RealType& loggb) { + return loggb - halfovertau * spindrift * spindrift; + }); + } auto checkPhaseChanged = [&sft](const TrialWaveFunction& twf, int& is_reject) { if (sft.branch_engine.phaseChanged(twf.getPhaseDiff())) diff --git a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h index 65311b347d..c61fedac77 100644 --- a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h +++ b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h @@ -17,6 +17,8 @@ #include "Particle/ParticleSet.h" #include "QMCWaveFunctions/TrialWaveFunction.h" #include "QMCHamiltonians/QMCHamiltonian.h" +#include "QMCWaveFunctions/TWFGrads.hpp" +#include "Particle/MCCoords.hpp" namespace qmcplusplus { @@ -44,6 +46,9 @@ class DriftModifierBase const std::vector& qf, std::vector&) const = 0; + template + void getDrifts(const Taus& taus, const TWFGrads& qf, MCCoords& drifts) const; + virtual bool parseXML(xmlNodePtr cur) { return true; } virtual ~DriftModifierBase() {} @@ -53,6 +58,14 @@ class DriftModifierBase std::string ClassName; }; +template +void DriftModifierBase::getDrifts(const Taus& taus, const TWFGrads& qf, MCCoords& drifts) const +{ + getDrifts(taus.tauovermass, qf.grads_positions, drifts.positions); + if constexpr (CT == CoordsType::POS_SPIN) + getDrifts(taus.spin_tauovermass, qf.grads_spins, drifts.spins); +} + } // namespace qmcplusplus #endif diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index 677a0bcba2..dfdc917cd5 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -18,6 +18,7 @@ #include "ParticleBase/RandomSeqGenerator.h" #include "Particle/MCSample.h" #include "MemoryUsage.h" +#include "QMCWaveFunctions/TWFGrads.hpp" namespace qmcplusplus { @@ -62,7 +63,8 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, checkLogAndGL(crowd, "checkGL_after_load"); timers.movepbyp_timer.start(); - const int num_walkers = crowd.size(); + const int num_walkers = crowd.size(); + const int num_particles = sft.population.get_num_particles(); // Note std::vector is not like the rest of stl. std::vector moved(num_walkers, false); constexpr RealType mhalf(-0.5); @@ -78,18 +80,31 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, std::vector> twf_accept_list, twf_reject_list; isAccepted.reserve(num_walkers); - MoveAbstraction mover(ps_dispatcher, walker_elecs, step_context.get_random_gen(), sft.drift_modifier, num_walkers, - sft.population.get_num_particles()); + MCCoords drifts, walker_deltas; + TWFGrads grads_now, grads_new; + drifts.resize(num_walkers); + walker_deltas.resize(num_walkers * num_particles); + grads_now.resize(num_walkers); + grads_new.resize(num_walkers); for (int sub_step = 0; sub_step < sft.qmcdrv_input.get_sub_steps(); sub_step++) { //This generates an entire steps worth of deltas. - mover.generateDeltas(); + makeGaussRandomWithEngine(walker_deltas, step_context.get_random_gen()); // up and down electrons are "species" within qmpack for (int ig = 0; ig < step_context.get_num_groups(); ++ig) //loop over species { - mover.setTauForGroup(sft.qmcdrv_input, sft.population.get_ptclgrp_inv_mass()[ig]); + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + auto getTaus = [&](const int ig) { + if constexpr (CT == CoordsType::POS_SPIN) + return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], + sft.qmcdrv_input.get_spin_mass()); + else + return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig]); + }; + + Taus taus = getTaus(ig); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); @@ -98,17 +113,96 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, for (int iat = start_index; iat < end_index; ++iat) { if (use_drift) - mover.calcForwardMoveWithDrift(twf_dispatcher, walker_twfs, iat); + { + twf_dispatcher.flex_evalGrad(walker_twfs, walker_elecs, iat, grads_now); + sft.drift_modifier.getDrifts(taus, grads_now, drifts); + auto delta_r_start = walker_deltas.positions.begin() + iat * num_walkers; + auto delta_r_end = delta_r_start + num_walkers; + std::transform(drifts.positions.begin(), drifts.positions.end(), delta_r_start, drifts.positions.begin(), + [st = taus.sqrttau](const PosType& drift, const PosType& delta_r) { + return drift + (st * delta_r); + }); + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + if constexpr (CT == CoordsType::POS_SPIN) + { + auto delta_spin_start = walker_deltas.spins.begin() + iat * num_walkers; + auto delta_spin_end = delta_spin_start + num_walkers; + std::transform(drifts.spins.begin(), drifts.spins.end(), delta_spin_start, drifts.spins.begin(), + [st = taus.spin_sqrttau](const ParticleSet::Scalar_t& spindrift, + const ParticleSet::Scalar_t& delta_spin) { + return spindrift + (st * delta_spin); + }); + } + } else - mover.calcForwardMove(iat); + { + auto delta_r_start = walker_deltas.positions.begin() + iat * num_walkers; + auto delta_r_end = delta_r_start + num_walkers; + std::transform(delta_r_start, delta_r_end, drifts.positions.begin(), + [st = taus.sqrttau](const PosType& delta_r) { return st * delta_r; }); + + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + if constexpr (CT == CoordsType::POS_SPIN) + { + auto delta_spin_start = walker_deltas.spins.begin() + iat * num_walkers; + auto delta_spin_end = delta_spin_start + num_walkers; + std::transform(delta_spin_start, delta_spin_end, drifts.spins.begin(), + [st = taus.spin_sqrttau](const ParticleSet::Scalar_t& delta_spin) { + return st * delta_spin; + }); + } + } - mover.makeMove(iat); + ps_dispatcher.flex_makeMove(walker_elecs, iat, drifts); // This is inelegant if (use_drift) - mover.updateGreensFunctionWithDrift(twf_dispatcher, walker_twfs, iat, ratios, log_gf, log_gb); + { + twf_dispatcher.flex_calcRatioGrad(walker_twfs, walker_elecs, iat, ratios, grads_new); + + auto delta_r_start = walker_deltas.positions.begin() + iat * num_walkers; + auto delta_r_end = delta_r_start + num_walkers; + + std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const PosType& delta_r) { + constexpr RealType mhalf(-0.5); + return mhalf * dot(delta_r, delta_r); + }); + + sft.drift_modifier.getDrifts(taus, grads_new, drifts); + + std::transform(walker_elecs.begin(), walker_elecs.end(), drifts.positions.begin(), drifts.positions.begin(), + [iat](const ParticleSet& ps, const PosType& drift) { + return ps.R[iat] - ps.getActivePos() - drift; + }); + + std::transform(drifts.positions.begin(), drifts.positions.end(), log_gb.begin(), + [halfovertau = taus.oneover2tau](const PosType& drift) { + return -halfovertau * dot(drift, drift); + }); + + //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract + if constexpr (CT == CoordsType::POS_SPIN) + { + auto delta_spin_start = walker_deltas.spins.begin() + iat * num_walkers; + auto delta_spin_end = delta_spin_start + num_walkers; + std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), + [](const ParticleSet::Scalar_t& delta_spin, const RealType& loggf) { + constexpr RealType mhalf(-0.5); + return loggf + mhalf * delta_spin * delta_spin; + }); + std::transform(walker_elecs.begin(), walker_elecs.end(), drifts.spins.begin(), drifts.spins.begin(), + [iat](const ParticleSet& ps, const ParticleSet::Scalar_t& spindrift) { + return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; + }); + std::transform(drifts.spins.begin(), drifts.spins.end(), log_gb.begin(), log_gb.begin(), + [halfovertau = taus.spin_oneover2tau](const ParticleSet::Scalar_t& spindrift, + const RealType& loggb) { + return loggb - halfovertau * spindrift * spindrift; + }); + } + } else - mover.updateGreensFunction(twf_dispatcher, walker_twfs, iat, ratios); + twf_dispatcher.flex_calcRatio(walker_twfs, walker_elecs, iat, ratios); std::transform(ratios.begin(), ratios.end(), prob.begin(), [](auto ratio) { return std::norm(ratio); }); diff --git a/src/QMCWaveFunctions/TWFGrads.hpp b/src/QMCWaveFunctions/TWFGrads.hpp new file mode 100644 index 0000000000..24fd6a1b82 --- /dev/null +++ b/src/QMCWaveFunctions/TWFGrads.hpp @@ -0,0 +1,44 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +// +// File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +////////////////////////////////////////////////////////////////////////////////////// + + +#ifndef QMCPLUSPLUS_TWFGRADS_HPP +#define QMCPLUSPLUS_TWFGRADS_HPP + +#include "Configuration.h" +#include "Particle/MCCoords.hpp" + +namespace qmcplusplus +{ +template +struct TWFGrads +{ + void resize(const std::size_t size) { grads_positions.resize(size); } + std::vector grads_positions; +}; + +template<> +struct TWFGrads +{ + void resize(const std::size_t size) + { + grads_positions.resize(size); + grads_spins.resize(size); + } + std::vector grads_positions; + std::vector grads_spins; +}; + +extern template struct TWFGrads; +extern template struct TWFGrads; +} // namespace qmcplusplus + +#endif diff --git a/src/QMCWaveFunctions/TWFdispatcher.cpp b/src/QMCWaveFunctions/TWFdispatcher.cpp index 8b83ef2dd8..89a0414802 100644 --- a/src/QMCWaveFunctions/TWFdispatcher.cpp +++ b/src/QMCWaveFunctions/TWFdispatcher.cpp @@ -72,6 +72,18 @@ void TWFdispatcher::flex_prepareGroup(const RefVectorWithLeader +void TWFdispatcher::flex_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads) const +{ + if constexpr (CT == CoordsType::POS_SPIN) + flex_evalGradWithSpin(wf_list, p_list, iat, grads.grads_positions, grads.grads_spins); + else + flex_evalGrad(wf_list, p_list, iat, grads.grads_positions); +} + void TWFdispatcher::flex_evalGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, @@ -108,6 +120,19 @@ void TWFdispatcher::flex_evalGradWithSpin(const RefVectorWithLeader +void TWFdispatcher::flex_calcRatioGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads) const +{ + if constexpr (CT == CoordsType::POS_SPIN) + flex_calcRatioGradWithSpin(wf_list, p_list, iat, ratios, grads.grads_positions, grads.grads_spins); + else + flex_calcRatioGrad(wf_list, p_list, iat, ratios, grads.grads_positions); +} + void TWFdispatcher::flex_calcRatioGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, @@ -200,4 +225,24 @@ void TWFdispatcher::flex_evaluateRatios(const RefVectorWithLeader(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads) const; +template void TWFdispatcher::flex_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads) const; +template void TWFdispatcher::flex_calcRatioGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads) const; +template void TWFdispatcher::flex_calcRatioGrad( + const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads) const; + } // namespace qmcplusplus diff --git a/src/QMCWaveFunctions/TWFdispatcher.h b/src/QMCWaveFunctions/TWFdispatcher.h index 584e3cc764..961edd2eea 100644 --- a/src/QMCWaveFunctions/TWFdispatcher.h +++ b/src/QMCWaveFunctions/TWFdispatcher.h @@ -14,6 +14,7 @@ #define QMCPLUSPLUS_TWFDISPATCH_H #include "TrialWaveFunction.h" +#include "TWFGrads.hpp" namespace qmcplusplus { @@ -49,6 +50,12 @@ class TWFdispatcher const RefVectorWithLeader& p_list, int ig) const; + template + void flex_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads) const; + void flex_evalGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, @@ -60,6 +67,13 @@ class TWFdispatcher std::vector& grad_now, std::vector& spingrad_now) const; + template + void flex_calcRatioGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads) const; + void flex_calcRatioGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, From 618b26288d488f2a3feb40568439c4865bd3e836 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 9 Feb 2022 16:01:25 -0700 Subject: [PATCH 24/34] remove MoveAbstraction --- src/QMCDrivers/DMC/DMCBatched.h | 1 - src/QMCDrivers/MoveAbstraction.h | 341 ------------------------------- src/QMCDrivers/VMC/VMCBatched.h | 1 - 3 files changed, 343 deletions(-) delete mode 100644 src/QMCDrivers/MoveAbstraction.h diff --git a/src/QMCDrivers/DMC/DMCBatched.h b/src/QMCDrivers/DMC/DMCBatched.h index 2e969408c6..b6a1f44857 100644 --- a/src/QMCDrivers/DMC/DMCBatched.h +++ b/src/QMCDrivers/DMC/DMCBatched.h @@ -17,7 +17,6 @@ #include "QMCDrivers/DMC/DMCDriverInput.h" #include "QMCDrivers/MCPopulation.h" #include "QMCDrivers/ContextForSteps.h" -#include "QMCDrivers/MoveAbstraction.h" #include "Particle/MCCoords.hpp" namespace qmcplusplus diff --git a/src/QMCDrivers/MoveAbstraction.h b/src/QMCDrivers/MoveAbstraction.h deleted file mode 100644 index e455b3650c..0000000000 --- a/src/QMCDrivers/MoveAbstraction.h +++ /dev/null @@ -1,341 +0,0 @@ -////////////////////////////////////////////////////////////////////////////////////// -// This file is distributed under the University of Illinois/NCSA Open Source License. -// See LICENSE file in top directory for details. -// -// Copyright (c) 2022 QMCPACK developers. -// -// File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories -// -// File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories -////////////////////////////////////////////////////////////////////////////////////// - -#ifndef QMCPLUSPLUS_MOVEABSTRACTION_H -#define QMCPLUSPLUS_MOVEABSTRACTION_H - -#include "Particle/PSdispatcher.h" -#include "Particle/ParticleSet.h" -#include "QMCWaveFunctions/TWFdispatcher.h" -#include "QMCWaveFunctions/TrialWaveFunction.h" -#include "ParticleBase/RandomSeqGenerator.h" -#include "QMCDriverInput.h" -#include "type_traits/RefVectorWithLeader.h" -#include "QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h" -#include "Particle/MCCoords.hpp" - -namespace qmcplusplus -{ -/** abstraction class to handle particle moves in batched QMC drivers - * - * Templated on CoordsType defined in MC - * Currently supports CoordsType::POS and CoordsType::POS_SPIN, which includes dynamic spins in particle moves - */ -template -class MoveAbstraction -{ - using Pos = ParticleSet::PosType; - using Scalar = ParticleSet::Scalar_t; - using Real = ParticleSet::RealType; - using Grad = TrialWaveFunction::GradType; - using Complex = TrialWaveFunction::ComplexType; - using PsiV = TrialWaveFunction::PsiValueType; - -public: - /** Constructor - * - * uses references to dispatchers, rng, drift_modifers, etc which are used regularly for the particle moves - * resizes all of the vectors to be used throughout this move - * \param[in] random_gen - */ - MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles); - - /** generates an entire steps worth of displacements - * - * Uses gaussian random numbers to generate the displacements for all coordinates over all walkers - */ - void generateDeltas(); - - /** sets the timestep information for the move - * - * e.g. spatial only moves (CoordsType::POS) only need the spatial timestep, whereas - * spin moves need a spin timestep defined by the spin mass provided by the driver input - */ - void setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass); - - /** Calulates the forward move for all particle coordinates - * - * updates all the walkers in the crowd for particle iat using - * \f[ - * \mathbf{r}' - \mathbf{r} = \sqrt{\tau} \eta + \tau \mathbf{v} - * \f] - * using drift and diffusion. - * This also includes the spin drift and diffusion depending on the template parameter - */ - void calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat); - - /** Calculates the forward move for all particle coordinates without drift - * - * updates all the walkers in the crowd for particle iat using - * \f[ - * \mathbf{r}' - \mathbf{r} = \sqrt{\tau}\eta - * \f] - * i.e., diffusion only. Only used in VMCBatched - * This also includes the spin diffusion depending on the template parameter. - */ - void calcForwardMove(const int iat); - - /** makes particle move - * - * updates particle iat for all walkers in the crowd. This uses the PSdispatcher to use flex_ move APIs - */ - void makeMove(const int iat); - - /** calculates the greens functions for forward and reverse moves and TWF ratio, used for acceptance in QMCDrivers - * - * includes the drift part of the move, i.e. - * \f[ - * G(\mathbf{r}'\leftarrow\mathbf{r}, \tau = \exp\left[-\frac{1}{2\tau}|\mathbf{r}'-\mathbf{r} - \tau \mathbf{v}(\mathbf{r})|^2 \right] - * \f] - * and the ratio - * \f[ - * \frac{\Psi_T(\mathbf{r}')}{\Psi_T(\mathbf{r})} - * \f] - * This is the necessary data to calculate the acceptance ratio in the QMCDriver - * also adds to the spin move component depending on the template parameter - * \param[out] ratios ratio of trial wave functions for all walkers for particle iat - * \param[out] log_gf log of greens function for forward move for all walkers for particle iat - * \param[out] log_gb log of greens function for reverse move for all walkers for particle iat - */ - void updateGreensFunctionWithDrift(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios, - std::vector& log_gf, - std::vector& log_bg); - - /** calculates TWF ratio, used for acceptance in QMCDrivers - * - * does not include the drift part of the move. - * \f[ - * G(\mathbf{r}'\leftarrow\mathbf{r}, \tau = \exp\left[-\frac{1}{2\tau}|\mathbf{r}'-\mathbf{r}|^2 \right] - * \f] - * Therefore, in the acceptance ratio this cancels since \f$G(\mathbf{r}'\leftarrow \mathbf{r}, \tau) = G(\mathbf{r}\leftarrow\mathbf{r}',\tau)$\f. - * Therefore, we only need to calculate the ratios - * \f[ - * \frac{\Psi_T(\mathbf{r}')}{\Psi_T(\mathbf{r})} - * \f] - * This is the necessary data to calculate the acceptance ratio in the VMCBatched, without drift - * \param[out] ratios ratio of trial wave functions for all walkers for particle iat - */ - void updateGreensFunction(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios); - - /** accumulates the data to help construct the effective timestep - * - * the effective timestep used in DMC algorithm is - * \f[ - * \tau_{\rm eff} = \frac{\sum R_{\rm accepted}^2}{\sum R_{\rm proposed}^2} - * \f] - * which is accumulated over all electrons in a walker - * - * rr below is the \f$r^2$\f for the current particle iat for the walkers and will be accumulated for - * each particle in the DMC driver. - * \param[out] rr - */ - void updaterr(const int iat, std::vector& rr); - -private: - /// spatial drift part of move for a single particle across multiple walkers - MCCoords drifts_; - /// all of the spatial diffusion moves for all walkers and particles for a given step - MCCoords walker_deltas_; - /// spatial gradients for a single electron across multiple walkers - std::vector grads_now_, grads_new_; - /// spin gradients for a single electrons across multiple walkers - std::vector spingrads_now_, spingrads_new_; - /// provides flex_ APIs to do select sw/mw updates of particle set - const PSdispatcher& ps_dispatcher_; - /// ParticleSets for each walker - const RefVectorWithLeader& elecs_; - /// rng, provided by ContextForSteps - RandomGenerator& random_gen_; - /// drift modifer used to limit size of drift velocity - const DriftModifierBase& drift_modifier_; - /// timesteps - std::unique_ptr> taus_; - const int num_walkers_; -}; - -template -inline MoveAbstraction::MoveAbstraction(const PSdispatcher& ps_dispatcher, - const RefVectorWithLeader& elecs, - RandomGenerator& random_gen, - const DriftModifierBase& drift_modifier, - const int num_walkers, - const int num_particles) - : ps_dispatcher_(ps_dispatcher), - elecs_(elecs), - random_gen_(random_gen), - drift_modifier_(drift_modifier), - taus_(nullptr), - num_walkers_(num_walkers) -{ - drifts_.resize(num_walkers_); - walker_deltas_.resize(num_walkers_ * num_particles); - grads_now_.resize(num_walkers_); - grads_new_.resize(num_walkers_); - if constexpr (CT == CoordsType::POS_SPIN) - { - spingrads_now_.resize(num_walkers_); - spingrads_new_.resize(num_walkers_); - } -} - -template -inline void MoveAbstraction::generateDeltas() -{ - makeGaussRandomWithEngine(walker_deltas_.positions, random_gen_); - if constexpr (CT == CoordsType::POS_SPIN) - makeGaussRandomWithEngine(walker_deltas_.spins, random_gen_); -} - -template -inline void MoveAbstraction::setTauForGroup(const QMCDriverInput& qmcdrv_input, const Real& invmass) -{ - if constexpr (CT == CoordsType::POS_SPIN) - taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass, - qmcdrv_input.get_spin_mass()); - else - taus_ = std::make_unique>(qmcdrv_input.get_tau(), invmass); -} - -template -inline void MoveAbstraction::calcForwardMoveWithDrift(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat) -{ - auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - - twf_dispatcher.flex_evalGrad(twfs, elecs_, iat, grads_now_); - drift_modifier_.getDrifts(taus_->tauovermass, grads_now_, drifts_.positions); - std::transform(drifts_.positions.begin(), drifts_.positions.end(), delta_r_start, drifts_.positions.begin(), - [st = taus_->sqrttau](const Pos& drift, const Pos& delta_r) { return drift + (st * delta_r); }); - - if constexpr (CT == CoordsType::POS_SPIN) - { - auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; - drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_now_, drifts_.spins); - std::transform(drifts_.spins.begin(), drifts_.spins.end(), delta_spin_start, drifts_.spins.begin(), - [st = taus_->spin_sqrttau](const Scalar& spindrift, const Scalar& delta_spin) { - return spindrift + (st * delta_spin); - }); - } -} - -template -inline void MoveAbstraction::calcForwardMove(const int iat) -{ - auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - - std::transform(delta_r_start, delta_r_end, drifts_.positions.begin(), - [st = taus_->sqrttau](const Pos& delta_r) { return st * delta_r; }); - - if constexpr (CT == CoordsType::POS_SPIN) - { - auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; - std::transform(delta_spin_start, delta_spin_end, drifts_.spins.begin(), - [st = taus_->spin_sqrttau](const Scalar& delta_spin) { return st * delta_spin; }); - } -} - -template -inline void MoveAbstraction::makeMove(const int iat) -{ - ps_dispatcher_.flex_makeMove(elecs_, iat, drifts_); -} - - -template -inline void MoveAbstraction::updateGreensFunctionWithDrift(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios, - std::vector& log_gf, - std::vector& log_gb) -{ - if constexpr (CT == CoordsType::POS_SPIN) - twf_dispatcher.flex_calcRatioGradWithSpin(twfs, elecs_, iat, ratios, grads_new_, spingrads_new_); - else - twf_dispatcher.flex_calcRatioGrad(twfs, elecs_, iat, ratios, grads_new_); - - auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - - std::transform(delta_r_start, delta_r_end, log_gf.begin(), [](const Pos& delta_r) { - constexpr Real mhalf(-0.5); - return mhalf * dot(delta_r, delta_r); - }); - - drift_modifier_.getDrifts(taus_->tauovermass, grads_new_, drifts_.positions); - - std::transform(elecs_.begin(), elecs_.end(), drifts_.positions.begin(), drifts_.positions.begin(), - [iat](const ParticleSet& ps, const Pos& drift) { return ps.R[iat] - ps.getActivePos() - drift; }); - - std::transform(drifts_.positions.begin(), drifts_.positions.end(), log_gb.begin(), - [halfovertau = taus_->oneover2tau](const Pos& drift) { return -halfovertau * dot(drift, drift); }); - - if constexpr (CT == CoordsType::POS_SPIN) - { - auto delta_spin_start = walker_deltas_.spins.begin() + iat * num_walkers_; - auto delta_spin_end = delta_spin_start + num_walkers_; - std::transform(delta_spin_start, delta_spin_end, log_gf.begin(), log_gf.begin(), - [](const Scalar& delta_spin, const Real& loggf) { - constexpr Real mhalf(-0.5); - return loggf + mhalf * delta_spin * delta_spin; - }); - drift_modifier_.getDrifts(taus_->spin_tauovermass, spingrads_new_, drifts_.spins); - std::transform(elecs_.begin(), elecs_.end(), drifts_.spins.begin(), drifts_.spins.begin(), - [iat](const ParticleSet& ps, const Scalar& spindrift) { - return ps.spins[iat] - ps.getActiveSpinVal() - spindrift; - }); - std::transform(drifts_.spins.begin(), drifts_.spins.end(), log_gb.begin(), log_gb.begin(), - [halfovertau = taus_->spin_oneover2tau](const Scalar& spindrift, const Real& loggb) { - return loggb - halfovertau * spindrift * spindrift; - }); - } -} - -template -inline void MoveAbstraction::updateGreensFunction(const TWFdispatcher& twf_dispatcher, - const RefVectorWithLeader& twfs, - const int iat, - std::vector& ratios) -{ - twf_dispatcher.flex_calcRatio(twfs, elecs_, iat, ratios); -} - -template -inline void MoveAbstraction::updaterr(const int iat, std::vector& rr) -{ - auto delta_r_start = walker_deltas_.positions.begin() + iat * num_walkers_; - auto delta_r_end = delta_r_start + num_walkers_; - assert(rr.size() == delta_r_end - delta_r_start); - std::transform(delta_r_start, delta_r_end, rr.begin(), - [t = taus_->tauovermass](auto& delta_r) { return t * dot(delta_r, delta_r); }); -} - -} // namespace qmcplusplus - -#endif diff --git a/src/QMCDrivers/VMC/VMCBatched.h b/src/QMCDrivers/VMC/VMCBatched.h index e00a23ae18..b5fa1197a6 100644 --- a/src/QMCDrivers/VMC/VMCBatched.h +++ b/src/QMCDrivers/VMC/VMCBatched.h @@ -18,7 +18,6 @@ #include "QMCDrivers/MCPopulation.h" #include "QMCDrivers/ContextForSteps.h" #include "QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h" -#include "QMCDrivers/MoveAbstraction.h" #include "Utilities/Timer.h" From 2f58150f0a92a02cd40f6211080ba1fd3762c3cb Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 9 Feb 2022 16:05:52 -0700 Subject: [PATCH 25/34] call accept_rejectSpinMove first --- src/Particle/ParticleSet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Particle/ParticleSet.cpp b/src/Particle/ParticleSet.cpp index 8619a02838..d95a1d00f0 100644 --- a/src/Particle/ParticleSet.cpp +++ b/src/Particle/ParticleSet.cpp @@ -711,9 +711,9 @@ void ParticleSet::mw_accept_rejectMove(const RefVectorWithLeader& p const std::vector& isAccepted, bool forward_mode) { - mw_accept_rejectMove(p_list, iat, isAccepted, forward_mode); if (CT == CoordsType::POS_SPIN) mw_accept_rejectSpinMove(p_list, iat, isAccepted); + mw_accept_rejectMove(p_list, iat, isAccepted, forward_mode); } From 877233d2c1599289d0b5c9ed733b53debfe31126 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Thu, 10 Feb 2022 10:13:06 -0700 Subject: [PATCH 26/34] fix failing test --- src/QMCWaveFunctions/CMakeLists.txt | 1 + src/QMCWaveFunctions/TWFGrads.cpp | 25 +++++++++++++++++++++++++ src/QMCWaveFunctions/TWFGrads.hpp | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/QMCWaveFunctions/TWFGrads.cpp diff --git a/src/QMCWaveFunctions/CMakeLists.txt b/src/QMCWaveFunctions/CMakeLists.txt index 898b5b1b3c..499e638615 100644 --- a/src/QMCWaveFunctions/CMakeLists.txt +++ b/src/QMCWaveFunctions/CMakeLists.txt @@ -165,6 +165,7 @@ set(FERMION_SRCS TrialWaveFunction.cpp TWFdispatcher.cpp TWFFastDerivWrapper.cpp + TWFGrads.cpp WaveFunctionFactory.cpp) if(ENABLE_CUDA) diff --git a/src/QMCWaveFunctions/TWFGrads.cpp b/src/QMCWaveFunctions/TWFGrads.cpp new file mode 100644 index 0000000000..0878efcefa --- /dev/null +++ b/src/QMCWaveFunctions/TWFGrads.cpp @@ -0,0 +1,25 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +// +// File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +////////////////////////////////////////////////////////////////////////////////////// + +#include "TWFGrads.hpp" + +namespace qmcplusplus +{ + +template +void TWFGrads::resize(const std::size_t size) +{ + grads_positions.resize(size); +} + +template struct TWFGrads; +template struct TWFGrads; +} diff --git a/src/QMCWaveFunctions/TWFGrads.hpp b/src/QMCWaveFunctions/TWFGrads.hpp index 24fd6a1b82..c515554e0b 100644 --- a/src/QMCWaveFunctions/TWFGrads.hpp +++ b/src/QMCWaveFunctions/TWFGrads.hpp @@ -21,7 +21,7 @@ namespace qmcplusplus template struct TWFGrads { - void resize(const std::size_t size) { grads_positions.resize(size); } + void resize(const std::size_t size); std::vector grads_positions; }; From a6a2f15ea7b89e9138ac653ff9cee805e33eca9a Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 10 Feb 2022 18:52:57 -0600 Subject: [PATCH 27/34] Fix template. --- src/Particle/MCCoords.cpp | 7 ++++--- src/Particle/MCCoords.hpp | 17 ++++++++--------- src/QMCWaveFunctions/TWFGrads.cpp | 8 +++++--- src/QMCWaveFunctions/TWFGrads.hpp | 15 ++++++++------- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/Particle/MCCoords.cpp b/src/Particle/MCCoords.cpp index ea10a55b19..9c606853d1 100644 --- a/src/Particle/MCCoords.cpp +++ b/src/Particle/MCCoords.cpp @@ -13,12 +13,13 @@ namespace qmcplusplus { -template -void MCCoords::resize(const std::size_t size) +void MCCoords::resize(const std::size_t size) { positions.resize(size); } + +void MCCoords::resize(const std::size_t size) { positions.resize(size); + spins.resize(size); } - template struct MCCoords; template struct MCCoords; } // namespace qmcplusplus diff --git a/src/Particle/MCCoords.hpp b/src/Particle/MCCoords.hpp index 593baf0635..ccde3ffcc1 100644 --- a/src/Particle/MCCoords.hpp +++ b/src/Particle/MCCoords.hpp @@ -25,23 +25,22 @@ enum class CoordsType POS_SPIN }; -template -struct MCCoords +template +struct MCCoords; + +template<> +struct MCCoords { - // This cleans up some other code. void resize(const std::size_t size); + std::vector positions; }; template<> struct MCCoords { - // This cleans up some other code. - void resize(const std::size_t size) - { - positions.resize(size); - spins.resize(size); - } + void resize(const std::size_t size); + std::vector positions; std::vector spins; }; diff --git a/src/QMCWaveFunctions/TWFGrads.cpp b/src/QMCWaveFunctions/TWFGrads.cpp index 0878efcefa..c6da3f3337 100644 --- a/src/QMCWaveFunctions/TWFGrads.cpp +++ b/src/QMCWaveFunctions/TWFGrads.cpp @@ -14,12 +14,14 @@ namespace qmcplusplus { -template -void TWFGrads::resize(const std::size_t size) +void TWFGrads::resize(const std::size_t size) { grads_positions.resize(size); } + +void TWFGrads::resize(const std::size_t size) { grads_positions.resize(size); + grads_spins.resize(size); } template struct TWFGrads; template struct TWFGrads; -} +} // namespace qmcplusplus diff --git a/src/QMCWaveFunctions/TWFGrads.hpp b/src/QMCWaveFunctions/TWFGrads.hpp index c515554e0b..22dbace45b 100644 --- a/src/QMCWaveFunctions/TWFGrads.hpp +++ b/src/QMCWaveFunctions/TWFGrads.hpp @@ -18,21 +18,22 @@ namespace qmcplusplus { -template -struct TWFGrads +template +struct TWFGrads; + +template<> +struct TWFGrads { void resize(const std::size_t size); + std::vector grads_positions; }; template<> struct TWFGrads { - void resize(const std::size_t size) - { - grads_positions.resize(size); - grads_spins.resize(size); - } + void resize(const std::size_t size); + std::vector grads_positions; std::vector grads_spins; }; From 4e5e96552896d7ecd29834949803e169db61b1af Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 10 Feb 2022 20:26:41 -0600 Subject: [PATCH 28/34] Add TauParams.hpp --- src/Particle/MCCoords.hpp | 41 ------------ src/Particle/tests/test_MCCoords.cpp | 17 ----- src/QMCDrivers/DMC/DMCBatched.cpp | 13 +--- .../DriftModifierBase.h | 6 +- src/QMCDrivers/TauParams.hpp | 65 +++++++++++++++++++ src/QMCDrivers/VMC/VMCBatched.cpp | 13 +--- src/QMCDrivers/tests/CMakeLists.txt | 1 + src/QMCDrivers/tests/test_TauParams.cpp | 35 ++++++++++ 8 files changed, 110 insertions(+), 81 deletions(-) create mode 100644 src/QMCDrivers/TauParams.hpp create mode 100644 src/QMCDrivers/tests/test_TauParams.cpp diff --git a/src/Particle/MCCoords.hpp b/src/Particle/MCCoords.hpp index ccde3ffcc1..02a9878b9f 100644 --- a/src/Particle/MCCoords.hpp +++ b/src/Particle/MCCoords.hpp @@ -45,47 +45,6 @@ struct MCCoords std::vector spins; }; -/** Object to encapsulate appropriate tau derived values - * for a particular MCCoords specialization - */ -template -struct Taus -{ - Real tauovermass; - Real oneover2tau; - Real sqrttau; - Taus(Real tau, Real grp_inv_mass) - { - tauovermass = tau * grp_inv_mass; - oneover2tau = 0.5 / (tauovermass); - sqrttau = std::sqrt(tauovermass); - } -}; - -template -struct Taus : public Taus -{ - using Base = Taus; - Real spin_tauovermass; - Real spin_oneover2tau; - Real spin_sqrttau; - Taus(Real tau, Real grp_inv_mass, Real spin_mass) : Base(tau, grp_inv_mass) - { - spin_tauovermass = Base::tauovermass / spin_mass; - spin_oneover2tau = 0.5 / (spin_tauovermass); - spin_sqrttau = std::sqrt(spin_tauovermass); - } -}; - -/** Factory function for Taus based on MCCoordsTypes - * Note as in previous code value of tau derived values is not full precision. - */ -template -auto makeTaus(MCCoords& mc_coords, const ARGS&... args) -{ - return Taus(args...); -} - extern template struct MCCoords; extern template struct MCCoords; } // namespace qmcplusplus diff --git a/src/Particle/tests/test_MCCoords.cpp b/src/Particle/tests/test_MCCoords.cpp index bd7029412b..1390df16e8 100644 --- a/src/Particle/tests/test_MCCoords.cpp +++ b/src/Particle/tests/test_MCCoords.cpp @@ -37,21 +37,4 @@ TEST_CASE("MCCoords", "[Particle]") } } -TEST_CASE("Taus", "[Particle]") -{ - MCCoords mc_coords_rs; - auto tau = 1.0; - auto invmass = 0.2; - auto taus_rs{makeTaus(mc_coords_rs, tau, invmass)}; - CHECK(Approx(taus_rs.tauovermass) == 0.2); - CHECK(Approx(taus_rs.oneover2tau) == 2.5); - CHECK(Approx(taus_rs.sqrttau) == 0.447213595499957927703605); - MCCoords mc_coords_rsspins; - auto spin_mass = 0.5; - auto taus_rsspins = makeTaus(mc_coords_rsspins, tau, invmass, spin_mass); - CHECK(Approx(taus_rsspins.spin_tauovermass) == 0.4); - CHECK(Approx(taus_rsspins.spin_oneover2tau) == 1.25); - CHECK(Approx(taus_rsspins.spin_sqrttau) == 0.632455532033675882352952); -} - } // namespace qmcplusplus diff --git a/src/QMCDrivers/DMC/DMCBatched.cpp b/src/QMCDrivers/DMC/DMCBatched.cpp index 857da149f7..6c7c98c376 100644 --- a/src/QMCDrivers/DMC/DMCBatched.cpp +++ b/src/QMCDrivers/DMC/DMCBatched.cpp @@ -26,6 +26,7 @@ #include "QMCDrivers/SFNBranch.h" #include "MemoryUsage.h" #include "QMCWaveFunctions/TWFGrads.hpp" +#include "TauParams.hpp" namespace qmcplusplus { @@ -135,16 +136,8 @@ void DMCBatched::advanceWalkers(const StateForThread& sft, ScopedTimer pbyp_local_timer(timers.movepbyp_timer); for (int ig = 0; ig < step_context.get_num_groups(); ++ig) { - //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract - auto getTaus = [&](const int ig) { - if constexpr (CT == CoordsType::POS_SPIN) - return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], - sft.qmcdrv_input.get_spin_mass()); - else - return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig]); - }; - - Taus taus = getTaus(ig); + TauParams taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], + sft.qmcdrv_input.get_spin_mass()); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); diff --git a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h index c61fedac77..ac761f3e8c 100644 --- a/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h +++ b/src/QMCDrivers/GreenFunctionModifiers/DriftModifierBase.h @@ -18,7 +18,7 @@ #include "QMCWaveFunctions/TrialWaveFunction.h" #include "QMCHamiltonians/QMCHamiltonian.h" #include "QMCWaveFunctions/TWFGrads.hpp" -#include "Particle/MCCoords.hpp" +#include "TauParams.hpp" namespace qmcplusplus { @@ -47,7 +47,7 @@ class DriftModifierBase std::vector&) const = 0; template - void getDrifts(const Taus& taus, const TWFGrads& qf, MCCoords& drifts) const; + void getDrifts(const TauParams& taus, const TWFGrads& qf, MCCoords& drifts) const; virtual bool parseXML(xmlNodePtr cur) { return true; } @@ -59,7 +59,7 @@ class DriftModifierBase }; template -void DriftModifierBase::getDrifts(const Taus& taus, const TWFGrads& qf, MCCoords& drifts) const +void DriftModifierBase::getDrifts(const TauParams& taus, const TWFGrads& qf, MCCoords& drifts) const { getDrifts(taus.tauovermass, qf.grads_positions, drifts.positions); if constexpr (CT == CoordsType::POS_SPIN) diff --git a/src/QMCDrivers/TauParams.hpp b/src/QMCDrivers/TauParams.hpp new file mode 100644 index 0000000000..4c3d067d18 --- /dev/null +++ b/src/QMCDrivers/TauParams.hpp @@ -0,0 +1,65 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory +// +// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + + +#ifndef QMCPLUSPLUS_TAU_PARAMS_HPP +#define QMCPLUSPLUS_TAU_PARAMS_HPP + +#include + +namespace qmcplusplus +{ +/** Object to encapsulate appropriate tau derived parameters + * for a particular CoordsType specialization + */ +template +struct TauParams; + +template +struct TauParams +{ + RT tauovermass; + RT oneover2tau; + RT sqrttau; + + TauParams(RT tau, RT grp_inv_mass, RT spin_mass) + { + tauovermass = tau * grp_inv_mass; + oneover2tau = 0.5 / (tauovermass); + sqrttau = std::sqrt(tauovermass); + } +}; + +template +struct TauParams +{ + RT tauovermass; + RT oneover2tau; + RT sqrttau; + + RT spin_tauovermass; + RT spin_oneover2tau; + RT spin_sqrttau; + + TauParams(RT tau, RT grp_inv_mass, RT spin_mass) + { + tauovermass = tau * grp_inv_mass; + oneover2tau = 0.5 / (tauovermass); + sqrttau = std::sqrt(tauovermass); + + spin_tauovermass = tauovermass / spin_mass; + spin_oneover2tau = 0.5 / (spin_tauovermass); + spin_sqrttau = std::sqrt(spin_tauovermass); + } +}; +} // namespace qmcplusplus + +#endif diff --git a/src/QMCDrivers/VMC/VMCBatched.cpp b/src/QMCDrivers/VMC/VMCBatched.cpp index dfdc917cd5..8d01d64a43 100644 --- a/src/QMCDrivers/VMC/VMCBatched.cpp +++ b/src/QMCDrivers/VMC/VMCBatched.cpp @@ -19,6 +19,7 @@ #include "Particle/MCSample.h" #include "MemoryUsage.h" #include "QMCWaveFunctions/TWFGrads.hpp" +#include "TauParams.hpp" namespace qmcplusplus { @@ -95,16 +96,8 @@ void VMCBatched::advanceWalkers(const StateForThread& sft, // up and down electrons are "species" within qmpack for (int ig = 0; ig < step_context.get_num_groups(); ++ig) //loop over species { - //want to remove CT==CoordsType::POS_SPIN from advanceWalkers. Need to abstract - auto getTaus = [&](const int ig) { - if constexpr (CT == CoordsType::POS_SPIN) - return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], - sft.qmcdrv_input.get_spin_mass()); - else - return Taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig]); - }; - - Taus taus = getTaus(ig); + TauParams taus(sft.qmcdrv_input.get_tau(), sft.population.get_ptclgrp_inv_mass()[ig], + sft.qmcdrv_input.get_spin_mass()); twf_dispatcher.flex_prepareGroup(walker_twfs, walker_elecs, ig); diff --git a/src/QMCDrivers/tests/CMakeLists.txt b/src/QMCDrivers/tests/CMakeLists.txt index a55040c71d..5d7d3b3c23 100644 --- a/src/QMCDrivers/tests/CMakeLists.txt +++ b/src/QMCDrivers/tests/CMakeLists.txt @@ -21,6 +21,7 @@ set(UTEST_HDF_INPUT ${qmcpack_SOURCE_DIR}/tests/solids/diamondC_1x1x1_pp/pwscf.p maybe_symlink(${UTEST_HDF_INPUT} ${UTEST_DIR}/diamondC_1x1x1.pwscf.h5) set(DRIVER_TEST_SRC + test_TauParams.cpp test_vmc.cpp test_dmc.cpp test_drift.cpp diff --git a/src/QMCDrivers/tests/test_TauParams.cpp b/src/QMCDrivers/tests/test_TauParams.cpp new file mode 100644 index 0000000000..68a8dd4e61 --- /dev/null +++ b/src/QMCDrivers/tests/test_TauParams.cpp @@ -0,0 +1,35 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory +// +// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + +#include "catch.hpp" +#include "TauParams.hpp" + +namespace qmcplusplus +{ + +TEST_CASE("Taus", "[Particle]") +{ + auto tau = 1.0; + auto invmass = 0.2; + auto spin_mass = 0.5; + + TauParams taus_rs(tau, invmass, spin_mass); + CHECK(Approx(taus_rs.tauovermass) == 0.2); + CHECK(Approx(taus_rs.oneover2tau) == 2.5); + CHECK(Approx(taus_rs.sqrttau) == 0.447213595499957927703605); + + TauParams taus_rsspins(tau, invmass, spin_mass); + CHECK(Approx(taus_rsspins.spin_tauovermass) == 0.4); + CHECK(Approx(taus_rsspins.spin_oneover2tau) == 1.25); + CHECK(Approx(taus_rsspins.spin_sqrttau) == 0.632455532033675882352952); +} + +} // namespace qmcplusplus From bda3f0bcca067babc7b8acf83fb63d78689bb25e Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 10 Feb 2022 21:08:23 -0600 Subject: [PATCH 29/34] Simplify ContextForSteps --- src/QMCDrivers/ContextForSteps.cpp | 13 +------------ src/QMCDrivers/ContextForSteps.h | 12 ------------ 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/src/QMCDrivers/ContextForSteps.cpp b/src/QMCDrivers/ContextForSteps.cpp index 8a33760b26..38e96c4394 100644 --- a/src/QMCDrivers/ContextForSteps.cpp +++ b/src/QMCDrivers/ContextForSteps.cpp @@ -19,17 +19,6 @@ ContextForSteps::ContextForSteps(int num_walkers, std::vector> particle_group_indexes, RandomGenerator& random_gen) : particle_group_indexes_(particle_group_indexes), random_gen_(random_gen) -{ - /** glambda to create type T with constructor T(int) and put in it unique_ptr - * - * captures num_particles to use as argument to constructor - * gets T for type unique_ptr unique is templated on - */ - auto constructT = [num_particles](auto& unique) { - unique.reset(new typename std::remove_pointer::type(num_particles)); - }; - - walker_deltas_.resize(num_walkers * num_particles); -} +{} } // namespace qmcplusplus diff --git a/src/QMCDrivers/ContextForSteps.h b/src/QMCDrivers/ContextForSteps.h index 825a23a5ef..0d1aead3d3 100644 --- a/src/QMCDrivers/ContextForSteps.h +++ b/src/QMCDrivers/ContextForSteps.h @@ -46,22 +46,10 @@ class ContextForSteps int get_num_groups() const { return particle_group_indexes_.size(); } RandomGenerator& get_random_gen() { return random_gen_; } - void nextDeltaRs(size_t num_rs) - { - // hate to repeat this pattern, this should never resize. - walker_deltas_.resize(num_rs); - makeGaussRandomWithEngine(walker_deltas_, random_gen_); - } - - std::vector& get_walker_deltas() { return walker_deltas_; } - auto deltaRsBegin() { return walker_deltas_.begin(); }; - int getPtclGroupStart(int group) const { return particle_group_indexes_[group].first; } int getPtclGroupEnd(int group) const { return particle_group_indexes_[group].second; } protected: - std::vector walker_deltas_; - /** indexes of start and stop of each particle group; * * Seems like these should be iterators but haven't thought through the implications. From 95ae489a125d52c0191e457301347f62ee84c4a1 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Tue, 15 Feb 2022 16:16:34 -0700 Subject: [PATCH 30/34] move switch from TWFdispatcher into TrialWaveFunction --- src/QMCWaveFunctions/TWFdispatcher.cpp | 81 ++++------------------ src/QMCWaveFunctions/TWFdispatcher.h | 24 ------- src/QMCWaveFunctions/TrialWaveFunction.cpp | 60 ++++++++++++++-- src/QMCWaveFunctions/TrialWaveFunction.h | 25 +++++++ 4 files changed, 91 insertions(+), 99 deletions(-) diff --git a/src/QMCWaveFunctions/TWFdispatcher.cpp b/src/QMCWaveFunctions/TWFdispatcher.cpp index 89a0414802..79ce0b5907 100644 --- a/src/QMCWaveFunctions/TWFdispatcher.cpp +++ b/src/QMCWaveFunctions/TWFdispatcher.cpp @@ -77,46 +77,19 @@ void TWFdispatcher::flex_evalGrad(const RefVectorWithLeader& const RefVectorWithLeader& p_list, int iat, TWFGrads& grads) const -{ - if constexpr (CT == CoordsType::POS_SPIN) - flex_evalGradWithSpin(wf_list, p_list, iat, grads.grads_positions, grads.grads_spins); - else - flex_evalGrad(wf_list, p_list, iat, grads.grads_positions); -} - -void TWFdispatcher::flex_evalGrad(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& grad_now) const { assert(wf_list.size() == p_list.size()); if (use_batch_) - TrialWaveFunction::mw_evalGrad(wf_list, p_list, iat, grad_now); + TrialWaveFunction::mw_evalGrad(wf_list, p_list, iat, grads); else { const int num_wf = wf_list.size(); - grad_now.resize(num_wf); + grads.resize(num_wf); for (size_t iw = 0; iw < num_wf; iw++) - grad_now[iw] = wf_list[iw].evalGrad(p_list[iw], iat); - } -} - -void TWFdispatcher::flex_evalGradWithSpin(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& grad_now, - std::vector& spingrad_now) const -{ - assert(wf_list.size() == p_list.size()); - if (use_batch_) - TrialWaveFunction::mw_evalGradWithSpin(wf_list, p_list, iat, grad_now, spingrad_now); - else - { - const int num_wf = wf_list.size(); - grad_now.resize(num_wf); - spingrad_now.resize(num_wf); - for (size_t iw = 0; iw < num_wf; iw++) - grad_now[iw] = wf_list[iw].evalGradWithSpin(p_list[iw], iat, spingrad_now[iw]); + if constexpr (CT == CoordsType::POS_SPIN) + grads.grads_positions[iw] = wf_list[iw].evalGradWithSpin(p_list[iw], iat, grads.grads_spins[iw]); + else + grads.grads_positions[iw] = wf_list[iw].evalGrad(p_list[iw], iat); } } @@ -126,50 +99,20 @@ void TWFdispatcher::flex_calcRatioGrad(const RefVectorWithLeader& ratios, TWFGrads& grads) const -{ - if constexpr (CT == CoordsType::POS_SPIN) - flex_calcRatioGradWithSpin(wf_list, p_list, iat, ratios, grads.grads_positions, grads.grads_spins); - else - flex_calcRatioGrad(wf_list, p_list, iat, ratios, grads.grads_positions); -} - -void TWFdispatcher::flex_calcRatioGrad(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& ratios, - std::vector& grad_new) const { assert(wf_list.size() == p_list.size()); if (use_batch_) - TrialWaveFunction::mw_calcRatioGrad(wf_list, p_list, iat, ratios, grad_new); + TrialWaveFunction::mw_calcRatioGrad(wf_list, p_list, iat, ratios, grads); else { const int num_wf = wf_list.size(); ratios.resize(num_wf); - grad_new.resize(num_wf); + grads.resize(num_wf); for (size_t iw = 0; iw < num_wf; iw++) - ratios[iw] = wf_list[iw].calcRatioGrad(p_list[iw], iat, grad_new[iw]); - } -} - -void TWFdispatcher::flex_calcRatioGradWithSpin(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& ratios, - std::vector& grad_new, - std::vector& spingrad_new) const -{ - assert(wf_list.size() == p_list.size()); - if (use_batch_) - TrialWaveFunction::mw_calcRatioGradWithSpin(wf_list, p_list, iat, ratios, grad_new, spingrad_new); - else - { - const int num_wf = wf_list.size(); - ratios.resize(num_wf); - grad_new.resize(num_wf); - spingrad_new.resize(num_wf); - for (size_t iw = 0; iw < num_wf; iw++) - ratios[iw] = wf_list[iw].calcRatioGradWithSpin(p_list[iw], iat, grad_new[iw], spingrad_new[iw]); + if constexpr (CT == CoordsType::POS_SPIN) + ratios[iw] = wf_list[iw].calcRatioGradWithSpin(p_list[iw], iat, grads.grads_positions[iw], grads.grads_spins[iw]); + else + ratios[iw] = wf_list[iw].calcRatioGrad(p_list[iw], iat, grads.grads_positions[iw]); } } diff --git a/src/QMCWaveFunctions/TWFdispatcher.h b/src/QMCWaveFunctions/TWFdispatcher.h index 961edd2eea..8a44d39be4 100644 --- a/src/QMCWaveFunctions/TWFdispatcher.h +++ b/src/QMCWaveFunctions/TWFdispatcher.h @@ -56,17 +56,6 @@ class TWFdispatcher int iat, TWFGrads& grads) const; - void flex_evalGrad(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& grad_now) const; - - void flex_evalGradWithSpin(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& grad_now, - std::vector& spingrad_now) const; - template void flex_calcRatioGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, @@ -74,19 +63,6 @@ class TWFdispatcher std::vector& ratios, TWFGrads& grads) const; - void flex_calcRatioGrad(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& ratios, - std::vector& grad_new) const; - - void flex_calcRatioGradWithSpin(const RefVectorWithLeader& wf_list, - const RefVectorWithLeader& p_list, - int iat, - std::vector& ratios, - std::vector& grad_new, - std::vector& spingrad_new) const; - void flex_accept_rejectMove(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, diff --git a/src/QMCWaveFunctions/TrialWaveFunction.cpp b/src/QMCWaveFunctions/TrialWaveFunction.cpp index 4d8c4f456c..c3add7761b 100644 --- a/src/QMCWaveFunctions/TrialWaveFunction.cpp +++ b/src/QMCWaveFunctions/TrialWaveFunction.cpp @@ -150,12 +150,12 @@ void TrialWaveFunction::mw_evaluateLog(const RefVectorWithLeader +void TrialWaveFunction::mw_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads) +{ + if constexpr (CT == CoordsType::POS_SPIN) + mw_evalGradWithSpin(wf_list, p_list, iat, grads.grads_positions, grads.grads_spins); + else + mw_evalGrad(wf_list, p_list, iat, grads.grads_positions); +} + void TrialWaveFunction::mw_evalGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, @@ -668,6 +680,19 @@ TrialWaveFunction::ValueType TrialWaveFunction::calcRatioGradWithSpin(ParticleSe return static_cast(r); } +template +void TrialWaveFunction::mw_calcRatioGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads) +{ + if constexpr (CT == CoordsType::POS_SPIN) + mw_calcRatioGradWithSpin(wf_list, p_list, iat, ratios, grads.grads_positions, grads.grads_spins); + else + mw_calcRatioGrad(wf_list, p_list, iat, ratios, grads.grads_positions); +} + void TrialWaveFunction::mw_calcRatioGrad(const RefVectorWithLeader& wf_list, const RefVectorWithLeader& p_list, int iat, @@ -1332,4 +1357,27 @@ void TrialWaveFunction::initializeTWFFastDerivWrapper(const ParticleSet& P, TWFF } } +//explicit instantiations +template void TrialWaveFunction::mw_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads); +template void TrialWaveFunction::mw_evalGrad( + const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads); +template void TrialWaveFunction::mw_calcRatioGrad( + const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads); +template void TrialWaveFunction::mw_calcRatioGrad( + const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads); + } // namespace qmcplusplus diff --git a/src/QMCWaveFunctions/TrialWaveFunction.h b/src/QMCWaveFunctions/TrialWaveFunction.h index 2f54492fcc..2b5b03bfdc 100644 --- a/src/QMCWaveFunctions/TrialWaveFunction.h +++ b/src/QMCWaveFunctions/TrialWaveFunction.h @@ -29,6 +29,7 @@ #include "type_traits/template_types.hpp" #include "Containers/MinimalContainers/RecordArray.hpp" #include "QMCWaveFunctions/TWFFastDerivWrapper.h" +#include "TWFGrads.hpp" #ifdef QMC_CUDA #include "type_traits/CUDATypes.h" #endif @@ -348,6 +349,18 @@ class TrialWaveFunction */ ValueType calcRatioGradWithSpin(ParticleSet& P, int iat, GradType& grad_iat, ComplexType& spingrad_iat); + /** batched version of ratioGrad + * + * all vector sizes must match + * implements switch between normal and WithSpin version + */ + template + static void mw_calcRatioGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + std::vector& ratios, + TWFGrads& grads); + /** batched version of ratioGrad * * all vector sizes must match @@ -396,6 +409,18 @@ class TrialWaveFunction */ GradType evalGradWithSpin(ParticleSet& P, int iat, ComplexType& spingrad); + /** batched version of evalGrad + * + * This is static because it should have no direct access + * to any TWF. + * implements switch between normal and WithSpin version + */ + template + static void mw_evalGrad(const RefVectorWithLeader& wf_list, + const RefVectorWithLeader& p_list, + int iat, + TWFGrads& grads); + /** batched version of evalGrad * * This is static because it should have no direct access From 60abdad34e476726cd2b6e52b78262bbe86acd36 Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Wed, 16 Feb 2022 15:49:06 -0600 Subject: [PATCH 31/34] Update header info --- src/Particle/MCCoords.cpp | 2 +- src/Particle/MCCoords.hpp | 2 +- src/QMCDrivers/ContextForSteps.cpp | 2 +- src/QMCDrivers/ContextForSteps.h | 2 +- src/QMCDrivers/QMCDriverInterface.h | 2 +- src/QMCDrivers/TauParams.hpp | 3 ++- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Particle/MCCoords.cpp b/src/Particle/MCCoords.cpp index 9c606853d1..f0b789461a 100644 --- a/src/Particle/MCCoords.cpp +++ b/src/Particle/MCCoords.cpp @@ -2,7 +2,7 @@ // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. // -// Copyright (c) 2022 developers. +// Copyright (c) 2022 QMCPACK developers. // // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory // diff --git a/src/Particle/MCCoords.hpp b/src/Particle/MCCoords.hpp index 02a9878b9f..f4eded2ad8 100644 --- a/src/Particle/MCCoords.hpp +++ b/src/Particle/MCCoords.hpp @@ -2,7 +2,7 @@ // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. // -// Copyright (c) 2022 developers. +// Copyright (c) 2022 QMCPACK developers. // // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory // diff --git a/src/QMCDrivers/ContextForSteps.cpp b/src/QMCDrivers/ContextForSteps.cpp index 38e96c4394..92bba6ee42 100644 --- a/src/QMCDrivers/ContextForSteps.cpp +++ b/src/QMCDrivers/ContextForSteps.cpp @@ -2,7 +2,7 @@ // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. // -// Copyright (c) 2019 developers. +// Copyright (c) 2019 QMCPACK developers. // // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory // diff --git a/src/QMCDrivers/ContextForSteps.h b/src/QMCDrivers/ContextForSteps.h index 0d1aead3d3..04cb6fdc28 100644 --- a/src/QMCDrivers/ContextForSteps.h +++ b/src/QMCDrivers/ContextForSteps.h @@ -2,7 +2,7 @@ // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. // -// Copyright (c) 2019 developers. +// Copyright (c) 2019 QMCPACK developers. // // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory // diff --git a/src/QMCDrivers/QMCDriverInterface.h b/src/QMCDrivers/QMCDriverInterface.h index 0e8d30a4cb..0e1ec53114 100644 --- a/src/QMCDrivers/QMCDriverInterface.h +++ b/src/QMCDrivers/QMCDriverInterface.h @@ -2,7 +2,7 @@ // This file is distributed under the University of Illinois/NCSA Open Source License. // See LICENSE file in top directory for details. // -// Copyright (c) 2019 developers. +// Copyright (c) 2019 QMCPACK developers. // // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory // diff --git a/src/QMCDrivers/TauParams.hpp b/src/QMCDrivers/TauParams.hpp index 4c3d067d18..af1e2ca658 100644 --- a/src/QMCDrivers/TauParams.hpp +++ b/src/QMCDrivers/TauParams.hpp @@ -4,7 +4,8 @@ // // Copyright (c) 2022 QMCPACK developers. // -// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory +// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory +// Ye Luo, yeluo@anl.gov, Argonne National Laboratory // // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory ////////////////////////////////////////////////////////////////////////////////////// From c2220aeebc1ff4a73fced552ea6e5f2c4c237dd0 Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 16 Feb 2022 15:32:10 -0700 Subject: [PATCH 32/34] update spinor tests to use templated APIs in ParticleSet --- src/QMCWaveFunctions/tests/test_MO_spinor.cpp | 38 +++++++++---------- .../tests/test_einset_spinor.cpp | 20 +++++----- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/QMCWaveFunctions/tests/test_MO_spinor.cpp b/src/QMCWaveFunctions/tests/test_MO_spinor.cpp index 93bf6d314a..8501e9192a 100644 --- a/src/QMCWaveFunctions/tests/test_MO_spinor.cpp +++ b/src/QMCWaveFunctions/tests/test_MO_spinor.cpp @@ -274,13 +274,12 @@ void test_lcao_spinor() //first, lets displace all the elec in each walker for (int iat = 0; iat < 1; iat++) { - std::vector displs = {dR[iat], dR[iat]}; - std::vector sdispls = {dS[iat], dS[iat]}; + MCCoords displs; + displs.positions = {dR[iat], dR[iat]}; + displs.spins = {dS[iat], dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -305,11 +304,11 @@ void test_lcao_spinor() dpsi_work_2 = 0.0; d2psi_work_2 = 0.0; dspsi_work_2 = 0.0; - - std::vector displs = {-dR[iat], -dR[iat]}; - std::vector sdispls = {-dS[iat], -dS[iat]}; + + MCCoords displs; + displs.positions = {-dR[iat], -dR[iat]}; + displs.spins = {-dS[iat], -dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(val).epsilon(eps)); @@ -327,8 +326,7 @@ void test_lcao_spinor() CHECK(dspsi_v_list[1].get()[0] == ComplexApprox(vds2).epsilon(eps)); std::vector accept = {false, false}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } } @@ -589,13 +587,12 @@ void test_lcao_spinor_excited() //first, lets displace all the elec in each walker for (int iat = 0; iat < 1; iat++) { - std::vector displs = {dR[iat], dR[iat]}; - std::vector sdispls = {dS[iat], dS[iat]}; + MCCoords displs; + displs.positions = {dR[iat], dR[iat]}; + displs.spins = {dS[iat], dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -621,10 +618,10 @@ void test_lcao_spinor_excited() d2psi_work_2 = 0.0; dspsi_work_2 = 0.0; - std::vector displs = {-dR[iat], -dR[iat]}; - std::vector sdispls = {-dS[iat], -dS[iat]}; + MCCoords displs; + displs.positions = {-dR[iat], -dR[iat]}; + displs.spins = {-dS[iat], -dS[iat]}; elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(val).epsilon(eps)); @@ -642,8 +639,7 @@ void test_lcao_spinor_excited() CHECK(dspsi_v_list[1].get()[0] == ComplexApprox(vds2).epsilon(eps)); std::vector accept = {false, false}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } } diff --git a/src/QMCWaveFunctions/tests/test_einset_spinor.cpp b/src/QMCWaveFunctions/tests/test_einset_spinor.cpp index 8291adfddf..6ef0ca9450 100644 --- a/src/QMCWaveFunctions/tests/test_einset_spinor.cpp +++ b/src/QMCWaveFunctions/tests/test_einset_spinor.cpp @@ -532,13 +532,13 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") //first, lets displace all the electrons in each walker. for (int iat = 0; iat < 3; iat++) { - std::vector displs = {dR[iat], dR[iat]}; - std::vector sdispls = {dS[iat], dS[iat]}; + MCCoords displs; + displs.positions = {dR[iat], dR[iat]}; + displs.spins = {dS[iat], dS[iat]}; + elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); std::vector accept = {true, true}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } elec_.mw_update(p_list); @@ -564,10 +564,11 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") d2psi_work_2 = 0.0; dspsi_work_2 = 0.0; - std::vector displs = {-dR[iat], -dR[iat]}; - std::vector sdispls = {-dS[iat], -dS[iat]}; + MCCoords displs; + displs.positions = {-dR[iat], -dR[iat]}; + displs.spins = {-dS[iat], -dS[iat]}; + elec_.mw_makeMove(p_list, iat, displs); - elec_.mw_makeSpinMove(p_list, iat, sdispls); spo->mw_evaluateVGLWithSpin(spo_list, p_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list, dspsi_v_list); //walker 0 CHECK(psi_v_list[0].get()[0] == ComplexApprox(psiM_ref[iat][0]).epsilon(h)); @@ -620,8 +621,7 @@ TEST_CASE("Einspline SpinorSet from HDF", "[wavefunction]") CHECK(dspsi_v_list[1].get()[2] == ComplexApprox(dspsiM_ref[(iat + 1) % 3][2]).epsilon(h)); std::vector accept = {false, false}; - elec_.mw_accept_rejectSpinMove(p_list, iat, accept); - elec_.mw_accept_rejectMove(p_list, iat, accept); + elec_.mw_accept_rejectMove(p_list, iat, accept); } } From 22521d211c5b5a0bf187d12fe5a10ba01704adbd Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 16 Feb 2022 16:02:37 -0700 Subject: [PATCH 33/34] add unit test for TWFGrads --- src/QMCWaveFunctions/tests/test_TWFGrads.cpp | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/QMCWaveFunctions/tests/test_TWFGrads.cpp diff --git a/src/QMCWaveFunctions/tests/test_TWFGrads.cpp b/src/QMCWaveFunctions/tests/test_TWFGrads.cpp new file mode 100644 index 0000000000..af1a14b39d --- /dev/null +++ b/src/QMCWaveFunctions/tests/test_TWFGrads.cpp @@ -0,0 +1,38 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2022 QMCPACK developers. +// +// File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +// +// File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories +////////////////////////////////////////////////////////////////////////////////////// + +#include "catch.hpp" +#include "TWFGrads.hpp" + +namespace qmcplusplus +{ + +TEST_CASE("TWFGrads", "[QMCWaveFunctions]") +{ + { + constexpr auto CT = CoordsType::POS; + auto grads = TWFGrads(); + REQUIRE(grads.grads_positions.size() == 0); + grads.resize(7); + REQUIRE(grads.grads_positions.size() == 7); + } + { + constexpr auto CT = CoordsType::POS_SPIN; + auto grads = TWFGrads(); + REQUIRE(grads.grads_positions.size() == 0); + REQUIRE(grads.grads_spins.size() == 0); + grads.resize(5); + REQUIRE(grads.grads_positions.size() == 5); + REQUIRE(grads.grads_spins.size() == 5); + } +} + +} // namespace qmcplusplus From 3c7aeb618eea6a7f5f0da55f1a3e902187d3b12f Mon Sep 17 00:00:00 2001 From: camelto2 Date: Wed, 16 Feb 2022 16:32:39 -0700 Subject: [PATCH 34/34] add test_TWFGrads to cmake --- src/QMCWaveFunctions/tests/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/QMCWaveFunctions/tests/CMakeLists.txt b/src/QMCWaveFunctions/tests/CMakeLists.txt index 975ae231da..3a3f66fea6 100644 --- a/src/QMCWaveFunctions/tests/CMakeLists.txt +++ b/src/QMCWaveFunctions/tests/CMakeLists.txt @@ -75,7 +75,8 @@ set(TRIALWF_SRC test_TrialWaveFunction_He.cpp test_wavefunction_pool.cpp test_example_he.cpp - test_lattice_gaussian.cpp) + test_lattice_gaussian.cpp + test_TWFGrads.cpp) set(SPOSET_SRC test_spo_collection_input_spline.cpp test_spo_collection_input_LCAO_xml.cpp