Skip to content

Commit 1469a59

Browse files
authored
Merge pull request QMCPACK#5638 from ye-luo/VP-relocat
Change from per NonLocalECPComponent VP to a single NonLocalECPotential VP
2 parents 3eac8b9 + dd53284 commit 1469a59

14 files changed

Lines changed: 296 additions & 241 deletions

src/QMCHamiltonians/ECPotentialBuilder.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "QMCHamiltonians/L2Potential.h"
2323
#include "OhmmsData/AttributeSet.h"
2424
#include "Numerics/OneDimNumGridFunctor.h"
25+
#include "Message/UniformCommunicateError.h"
2526

2627
namespace qmcplusplus
2728
{
@@ -111,30 +112,26 @@ bool ECPotentialBuilder::put(xmlNodePtr cur)
111112
app_log() << " Will compute forces in CoulombPBCAB.\n" << std::endl;
112113
std::unique_ptr<CoulombPBCAB> apot = std::make_unique<CoulombPBCAB>(IonConfig, targetPtcl, doForces);
113114
for (int i = 0; i < localPot.size(); i++)
114-
{
115115
if (localPot[i])
116116
apot->add(i, std::move(localPot[i]));
117-
}
118117
targetH.addOperator(std::move(apot), "LocalECP");
119118
}
120119
}
121120
if (hasNonLocalPot)
122121
{
123122
std::unique_ptr<NonLocalECPotential> apot =
124-
std::make_unique<NonLocalECPotential>(IonConfig, targetPtcl, targetPsi, use_DLA == "yes");
123+
std::make_unique<NonLocalECPotential>(IonConfig, targetPtcl, targetPsi, use_DLA == "yes",
124+
NLPP_algo == "batched");
125125

126126
int nknot_max = 0;
127127
// These are actually NonLocalECPComponents
128128
for (int i = 0; i < nonLocalPot.size(); i++)
129-
{
130129
if (nonLocalPot[i])
131130
{
132131
nknot_max = std::max(nknot_max, nonLocalPot[i]->getNknot());
133-
if (NLPP_algo == "batched")
134-
nonLocalPot[i]->initVirtualParticle(targetPtcl);
135132
apot->addComponent(i, std::move(nonLocalPot[i]));
136133
}
137-
}
134+
138135
app_log() << "\n Using NonLocalECP potential \n"
139136
<< " Maximum grid on a sphere for NonLocalECPotential: " << nknot_max << std::endl;
140137
if (NLPP_algo == "batched")
@@ -145,29 +142,30 @@ bool ECPotentialBuilder::put(xmlNodePtr cur)
145142
if (hasSOPot)
146143
{
147144
#ifndef QMC_COMPLEX
148-
APP_ABORT("SOECPotential evaluations require complex build. Rebuild with -D QMC_COMPLEX=1\n");
145+
throw UniformCommunicateError("SOECPotential evaluations require complex build. Rebuild with -D QMC_COMPLEX=1\n");
149146
#endif
150147
if (physicalSO == "yes")
151148
app_log() << " Spin-Orbit potential included in local energy" << std::endl;
152149
else if (physicalSO == "no")
153150
app_log() << " Spin-Orbit potential is not included in local energy" << std::endl;
154151
else
155-
APP_ABORT("physicalSO must be set to yes/no. Unknown option given\n");
152+
throw UniformCommunicateError("physicalSO must be set to yes/no. Unknown option given\n");
156153

157-
std::unique_ptr<SOECPotential> apot = std::make_unique<SOECPotential>(IonConfig, targetPtcl, targetPsi, use_exact_spin);
158-
int nknot_max = 0;
159-
int sknot_max = 0;
154+
if (use_exact_spin && NLPP_algo != "batched")
155+
throw UniformCommunicateError("spin_integrator=\"exact\" requires algorithm=\"batched\".\n");
156+
157+
std::unique_ptr<SOECPotential> apot =
158+
std::make_unique<SOECPotential>(IonConfig, targetPtcl, targetPsi, use_exact_spin, NLPP_algo == "batched");
159+
int nknot_max = 0;
160+
int sknot_max = 0;
160161
for (int i = 0; i < soPot.size(); i++)
161-
{
162162
if (soPot[i])
163163
{
164164
nknot_max = std::max(nknot_max, soPot[i]->getNknot());
165165
sknot_max = std::max(sknot_max, soPot[i]->getSknot());
166-
if (NLPP_algo == "batched")
167-
soPot[i]->initVirtualParticle(targetPtcl);
168166
apot->addComponent(i, std::move(soPot[i]));
169167
}
170-
}
168+
171169
app_log() << "\n Using SOECP potential \n"
172170
<< " Maximum grid on a sphere for SOECPotential: " << nknot_max << std::endl;
173171
if (use_exact_spin)

src/QMCHamiltonians/NonLocalECPComponent.cpp

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
namespace qmcplusplus
2424
{
25-
NonLocalECPComponent::NonLocalECPComponent()
26-
: lmax(0), nchannel(0), nknot(0), Rmax(-1), VP(nullptr), do_randomize_grid_(true)
27-
{}
25+
NonLocalECPComponent::NonLocalECPComponent() : lmax(0), nchannel(0), nknot(0), Rmax(-1), do_randomize_grid_(true) {}
2826

2927
// unfortunately we continue the sloppy use of the default copy constructor followed by reassigning pointers.
3028
// This prevents use of smart pointers and concievably sets us up for trouble with double frees and the destructor.
@@ -33,35 +31,16 @@ NonLocalECPComponent::NonLocalECPComponent(const NonLocalECPComponent& nl_ecpc,
3331
{
3432
for (int i = 0; i < nl_ecpc.nlpp_m.size(); ++i)
3533
nlpp_m[i] = nl_ecpc.nlpp_m[i]->makeClone();
36-
if (nl_ecpc.VP)
37-
VP = new VirtualParticleSet(pset, nl_ecpc.VP->getNumDistTables());
3834
}
3935

4036
NonLocalECPComponent::~NonLocalECPComponent()
4137
{
4238
for (int ip = 0; ip < nlpp_m.size(); ip++)
4339
delete nlpp_m[ip];
44-
if (VP)
45-
delete VP;
4640
}
4741

4842
void NonLocalECPComponent::set_randomize_grid(bool do_randomize_grid) { do_randomize_grid_ = do_randomize_grid; }
4943

50-
void NonLocalECPComponent::initVirtualParticle(const ParticleSet& qp)
51-
{
52-
assert(VP == 0);
53-
outputManager.pause();
54-
VP = new VirtualParticleSet(qp);
55-
outputManager.resume();
56-
}
57-
58-
void NonLocalECPComponent::deleteVirtualParticle()
59-
{
60-
if (VP)
61-
delete VP;
62-
VP = nullptr;
63-
}
64-
6544
void NonLocalECPComponent::add(int l, RadialPotentialType* pp)
6645
{
6746
angpp_m.push_back(l);
@@ -128,6 +107,7 @@ void NonLocalECPComponent::print(std::ostream& os)
128107
}
129108

130109
NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOne(ParticleSet& W,
110+
const OptionalRef<VirtualParticleSet> vp,
131111
int iat,
132112
TrialWaveFunction& psi,
133113
int iel,
@@ -140,21 +120,22 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOne(ParticleSet& W,
140120

141121
const bool use_TMDLA = tmove_xy && use_DLA;
142122

143-
if (VP)
123+
if (vp)
144124
{
125+
VirtualParticleSet& vp_set(*vp);
145126
// Compute ratios with VP
146-
VP->makeMoves(W, iel, deltaV, true, iat);
127+
vp_set.makeMoves(W, iel, deltaV, true, iat);
147128
if (use_TMDLA)
148129
{
149-
psi.evaluateRatios(*VP, psiratio_det, TrialWaveFunction::ComputeType::FERMIONIC);
150-
psi.evaluateRatios(*VP, psiratio, TrialWaveFunction::ComputeType::NONFERMIONIC);
130+
psi.evaluateRatios(vp_set, psiratio_det, TrialWaveFunction::ComputeType::FERMIONIC);
131+
psi.evaluateRatios(vp_set, psiratio, TrialWaveFunction::ComputeType::NONFERMIONIC);
151132
for (int j = 0; j < nknot; j++)
152133
psiratio[j] *= psiratio_det[j];
153134
}
154135
else if (use_DLA)
155-
psi.evaluateRatios(*VP, psiratio, TrialWaveFunction::ComputeType::FERMIONIC);
136+
psi.evaluateRatios(vp_set, psiratio, TrialWaveFunction::ComputeType::FERMIONIC);
156137
else
157-
psi.evaluateRatios(*VP, psiratio);
138+
psi.evaluateRatios(vp_set, psiratio);
158139
}
159140
else
160141
{
@@ -233,6 +214,7 @@ NonLocalECPComponent::RealType NonLocalECPComponent::calculatePotential(RealType
233214

234215
void NonLocalECPComponent::mw_evaluateOne(const RefVectorWithLeader<NonLocalECPComponent>& ecp_component_list,
235216
const RefVectorWithLeader<ParticleSet>& p_list,
217+
const std::optional<const RefVectorWithLeader<VirtualParticleSet>> vp_list,
236218
const RefVectorWithLeader<TrialWaveFunction>& psi_list,
237219
const RefVector<const NLPPJob<RealType>>& joblist,
238220
std::vector<RealType>& pairpots,
@@ -243,16 +225,13 @@ void NonLocalECPComponent::mw_evaluateOne(const RefVectorWithLeader<NonLocalECPC
243225
const bool use_TMDLA = (!tmove_xy_all_list.empty()) && use_DLA;
244226

245227
auto& ecp_component_leader = ecp_component_list.getLeader();
246-
if (ecp_component_leader.VP)
228+
if (vp_list)
247229
{
248230
// Compute ratios with VP
249-
RefVectorWithLeader<VirtualParticleSet> vp_list(*ecp_component_leader.VP);
250-
RefVectorWithLeader<const VirtualParticleSet> const_vp_list(*ecp_component_leader.VP);
231+
const auto& vp_set_list(*vp_list);
251232
RefVector<const std::vector<PosType>> deltaV_list;
252233
RefVector<std::vector<ValueType>> psiratios_list;
253234
RefVector<std::vector<ValueType>> psiratios_det_list;
254-
vp_list.reserve(ecp_component_list.size());
255-
const_vp_list.reserve(ecp_component_list.size());
256235
deltaV_list.reserve(ecp_component_list.size());
257236
psiratios_list.reserve(ecp_component_list.size());
258237
psiratios_det_list.reserve(ecp_component_list.size());
@@ -264,16 +243,19 @@ void NonLocalECPComponent::mw_evaluateOne(const RefVectorWithLeader<NonLocalECPC
264243

265244
component.buildQuadraturePointDeltaPositions(job.ion_elec_dist, job.ion_elec_displ, component.deltaV);
266245

267-
vp_list.push_back(*component.VP);
268-
const_vp_list.push_back(*component.VP);
269246
deltaV_list.push_back(component.deltaV);
270247
psiratios_list.push_back(component.psiratio);
271248
psiratios_det_list.push_back(component.psiratio_det);
272249
}
273250

274-
ResourceCollectionTeamLock<VirtualParticleSet> vp_res_lock(collection, vp_list);
251+
RefVectorWithLeader<const VirtualParticleSet> const_vp_list(vp_set_list.getLeader());
252+
const_vp_list.reserve(vp_set_list.size());
253+
for (VirtualParticleSet& vp_set : vp_set_list)
254+
const_vp_list.push_back(vp_set);
255+
256+
ResourceCollectionTeamLock<VirtualParticleSet> vp_res_lock(collection, vp_set_list);
275257

276-
VirtualParticleSet::mw_makeMoves(vp_list, p_list, deltaV_list, joblist, true);
258+
VirtualParticleSet::mw_makeMoves(vp_set_list, p_list, deltaV_list, joblist, true);
277259

278260
if (use_TMDLA)
279261
{
@@ -341,6 +323,7 @@ void NonLocalECPComponent::mw_evaluateOne(const RefVectorWithLeader<NonLocalECPC
341323
}
342324

343325
NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOneWithForces(ParticleSet& W,
326+
const OptionalRef<VirtualParticleSet> vp,
344327
int iat,
345328
TrialWaveFunction& psi,
346329
int iel,
@@ -372,12 +355,13 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOneWithForces(Parti
372355
// term coming from dependence of quadrature grid on ion position.
373356
PosType gradwfnterm_(0);
374357

375-
if (VP)
358+
if (vp)
376359
{
377360
APP_ABORT("NonLocalECPComponent::evaluateOneWithForces(...): Forces not implemented with virtual particle moves\n");
361+
VirtualParticleSet& vp_set(*vp);
378362
// Compute ratios with VP
379-
VP->makeMoves(W, iel, deltaV, true, iat);
380-
psi.evaluateRatios(*VP, psiratio);
363+
vp_set.makeMoves(W, iel, deltaV, true, iat);
364+
psi.evaluateRatios(vp_set, psiratio);
381365
}
382366
else
383367
{
@@ -475,6 +459,7 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOneWithForces(Parti
475459
}
476460

477461
NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOneWithForces(ParticleSet& W,
462+
const OptionalRef<VirtualParticleSet> vp,
478463
ParticleSet& ions,
479464
int iat,
480465
TrialWaveFunction& psi,
@@ -522,12 +507,13 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateOneWithForces(Parti
522507
for (size_t j = 0; j < nknot; j++)
523508
pulay_quad[j].resize(ions.getTotalNum());
524509

525-
if (VP)
510+
if (vp)
526511
{
527512
APP_ABORT("NonLocalECPComponent::evaluateOneWithForces(...): Forces not implemented with virtual particle moves\n");
513+
VirtualParticleSet& vp_set(*vp);
528514
// Compute ratios with VP
529-
VP->makeMoves(W, iel, deltaV, true, iat);
530-
psi.evaluateRatios(*VP, psiratio);
515+
vp_set.makeMoves(W, iel, deltaV, true, iat);
516+
psi.evaluateRatios(vp_set, psiratio);
531517
}
532518
else
533519
{

src/QMCHamiltonians/NonLocalECPComponent.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ class NonLocalECPComponent : public QMCTraits
115115
///The gradient of the wave function w.r.t. the ion position
116116
ParticleSet::ParticleGradient Gion;
117117

118-
///virtual particle set: delayed initialization
119-
VirtualParticleSet* VP;
120-
121118
/// Can disable grid randomization for testing
122119
bool do_randomize_grid_;
123120

@@ -177,6 +174,7 @@ class NonLocalECPComponent : public QMCTraits
177174
* @return RealType Contribution to $\frac{V\Psi_T}{\Psi_T}$ from ion iat and electron iel.
178175
*/
179176
RealType evaluateOne(ParticleSet& W,
177+
const OptionalRef<VirtualParticleSet> vp,
180178
int iat,
181179
TrialWaveFunction& Psi,
182180
int iel,
@@ -201,6 +199,7 @@ class NonLocalECPComponent : public QMCTraits
201199
*/
202200
static void mw_evaluateOne(const RefVectorWithLeader<NonLocalECPComponent>& ecp_component_list,
203201
const RefVectorWithLeader<ParticleSet>& p_list,
202+
const std::optional<const RefVectorWithLeader<VirtualParticleSet>> vp_list,
204203
const RefVectorWithLeader<TrialWaveFunction>& psi_list,
205204
const RefVector<const NLPPJob<RealType>>& joblist,
206205
std::vector<RealType>& pairpots,
@@ -222,6 +221,7 @@ class NonLocalECPComponent : public QMCTraits
222221
* @return RealType Contribution to $\frac{V\Psi_T}{\Psi_T}$ from ion iat and electron iel.
223222
*/
224223
RealType evaluateOneWithForces(ParticleSet& W,
224+
const OptionalRef<VirtualParticleSet> vp,
225225
int iat,
226226
TrialWaveFunction& Psi,
227227
int iel,
@@ -245,6 +245,7 @@ class NonLocalECPComponent : public QMCTraits
245245
* @return RealType Contribution to $\frac{V\Psi_T}{\Psi_T}$ from ion iat and electron iel.
246246
*/
247247
RealType evaluateOneWithForces(ParticleSet& W,
248+
const OptionalRef<VirtualParticleSet> vp,
248249
ParticleSet& ions,
249250
int iat,
250251
TrialWaveFunction& Psi,
@@ -256,6 +257,7 @@ class NonLocalECPComponent : public QMCTraits
256257

257258
// This function needs to be updated to SoA. myTableIndex is introduced temporarily.
258259
RealType evaluateValueAndDerivatives(ParticleSet& P,
260+
const OptionalRef<VirtualParticleSet> vp,
259261
int iat,
260262
TrialWaveFunction& psi,
261263
int iel,
@@ -313,15 +315,11 @@ class NonLocalECPComponent : public QMCTraits
313315

314316
void print(std::ostream& os);
315317

316-
void initVirtualParticle(const ParticleSet& qp);
317-
void deleteVirtualParticle();
318-
319318
inline void setRmax(int rmax) { Rmax = rmax; }
320319
inline RealType getRmax() const { return Rmax; }
321320
inline int getNknot() const { return nknot; }
322321
inline void setLmax(int Lmax) { lmax = Lmax; }
323322
inline int getLmax() const { return lmax; }
324-
const VirtualParticleSet* getVP() const { return VP; };
325323

326324
// copy sgridxyz_m to rrotsgrid_m without rotation. For testing only.
327325
friend void copyGridUnrotatedForTest(NonLocalECPComponent& nlpp);

0 commit comments

Comments
 (0)