Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference Space Warp Transformation for ZVZB Forces #2828

Merged
merged 25 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d6759c3
Output SWCT data to scalar.dat
rcclay Dec 18, 2020
7fe36e0
New evaluate routine that doesn't randomize the pseudo grid
rcclay Dec 21, 2020
fb3af86
Add Space warp transformation skeleton class
rcclay Dec 21, 2020
bb5c921
Start evaluating components needed for spacewarp in ACForce
rcclay Dec 21, 2020
cc00679
Initial bugged implementation of space warp
rcclay Dec 22, 2020
6f082a8
Fix bug. Tentatively working and ready for testing
rcclay Dec 23, 2020
12ec904
Allow user to change exponent of power law decay of SWT
rcclay Dec 23, 2020
9af3f0b
Add unit test for spacewarp
rcclay Dec 24, 2020
08c2040
Move grad(E_L) computation to QMCHamiltonian
rcclay Dec 24, 2020
678e4d5
Eliminate extra SW variables in scalar.dat.
rcclay Dec 24, 2020
f79c640
Add some documentation
rcclay Dec 24, 2020
d141966
rename evaluate2 to evaluateDeterministic
Dec 24, 2020
2e018ac
Merge branch 'develop' into space_warp_finite_diff
rcclay Dec 24, 2020
b90dd26
Add return value to put
rcclay Dec 24, 2020
fca68a6
Swap ordering of source/target args :|
rcclay Jan 4, 2021
cb0e225
Make SpaceWarp members and args const
rcclay Jan 4, 2021
92b865a
clang-format everything
rcclay Jan 4, 2021
eab8162
Make Nions const, also add comments
rcclay Jan 4, 2021
a324ec3
Make comments Doxygen friendly
rcclay Jan 4, 2021
65091e7
Change SpaceWarpTransformation from struct to class
rcclay Jan 4, 2021
6e8cb4d
Move space warp temp array allocation within spacewarp block
rcclay Jan 4, 2021
659156e
More doxygen stuff
rcclay Jan 4, 2021
1d7e640
Merge branch 'develop' into space_warp_finite_diff
rcclay Jan 4, 2021
783225a
Last bit of formatting/commenting changes
rcclay Jan 4, 2021
014a7cb
Merge branch 'develop' into space_warp_finite_diff
rcclay Jan 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions src/QMCHamiltonians/ACForce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
*/
#include "ACForce.h"
#include <sstream>
#include "OhmmsData/AttributeSet.h"

namespace qmcplusplus
{
ACForce::ACForce(ParticleSet& source, ParticleSet& target, TrialWaveFunction& psi_in, QMCHamiltonian& H)
: ions(source), elns(target), psi(psi_in), ham(H), FirstForceIndex(-1), Nions(0)
: ions(source), elns(target), psi(psi_in), ham(H), FirstForceIndex(-1), Nions(0), useSpaceWarp(false), swt(target,source)
ye-luo marked this conversation as resolved.
Show resolved Hide resolved
{
prefix = "ACForce";
myName = prefix;
Nions = ions.getTotalNum();
rcclay marked this conversation as resolved.
Show resolved Hide resolved

hf_force.resize(Nions);
pulay_force.resize(Nions);
wf_grad.resize(Nions);
sw_pulay.resize(Nions);
sw_grad.resize(Nions);
delta=1e-4;
};

OperatorBase* ACForce::makeClone(ParticleSet& qp, TrialWaveFunction& psi)
Expand All @@ -40,6 +45,27 @@ OperatorBase* ACForce::makeClone(ParticleSet& qp, TrialWaveFunction& psi_in, QMC
OperatorBase* myclone = new ACForce(qp, ions, psi_in, ham_in);
return myclone;
}

bool ACForce::put(xmlNodePtr cur)
{
std::string useSpaceWarpString("no");
std::string ionionforce("yes");
RealType swpow(4);
OhmmsAttributeSet attr;
attr.add(useSpaceWarpString,"spacewarp"); //"yes" or "no"
attr.add(swpow, "swpow"); //Real number"
attr.add(delta, "delta"); //Real number"
attr.put(cur);

useSpaceWarp = (useSpaceWarpString == "yes") || (useSpaceWarpString == "true");
swt.setPow(swpow);

if(useSpaceWarp) app_log()<<"ACForce is using space warp with power="<<swpow<<std::endl;
else app_log()<<"ACForce is not using space warp\n";

return true;
}

void ACForce::add2Hamiltonian(ParticleSet& qp, TrialWaveFunction& psi, QMCHamiltonian& ham_in)
{
//The following line is modified
Expand All @@ -52,10 +78,20 @@ ACForce::Return_t ACForce::evaluate(ParticleSet& P)
hf_force = 0;
pulay_force = 0;
wf_grad = 0;

sw_pulay = 0;
sw_grad = 0;
Force_t el_grad;
el_grad.resize(P.getTotalNum());
el_grad=0;
//This function returns d/dR of the sum of all observables in the physical hamiltonian.
//Note that the sign will be flipped based on definition of force = -d/dR.
Value = ham.evaluateIonDerivs(P, ions, psi, hf_force, pulay_force, wf_grad);

if(useSpaceWarp)
{
ham.evaluateElecGrad(P,psi,el_grad,delta);
rcclay marked this conversation as resolved.
Show resolved Hide resolved
swt.computeSWT(P,ions,el_grad,P.G,sw_pulay,sw_grad);
}
return 0.0;
};

Expand All @@ -75,10 +111,25 @@ void ACForce::addObservables(PropertySetType& plist, BufferType& collectables)
pulayname << prefix << "_pulay_" << iat << "_" << x;
wfgradname1 << prefix << "_Ewfgrad_" << iat << "_" << x;
wfgradname2 << prefix << "_wfgrad_" << iat << "_" << x;

plist.add(hfname.str());
plist.add(pulayname.str());
plist.add(wfgradname1.str());
plist.add(wfgradname2.str());

//Remove when ACForce is production ready.
rcclay marked this conversation as resolved.
Show resolved Hide resolved
// if(useSpaceWarp)
// {
// std::ostringstream swctname1;
// std::ostringstream swctname2;
// std::ostringstream swctname3;
// swctname1 << prefix << "_swct1_" << iat << "_" << x;
// swctname2 << prefix << "_swct2_" << iat << "_" << x;
// swctname3 << prefix << "_swct3_" << iat << "_" << x;
// plist.add(swctname1.str());
// plist.add(swctname2.str());
// plist.add(swctname3.str());
// }
}
}
};
Expand All @@ -92,9 +143,17 @@ void ACForce::setObservables(PropertySetType& plist)
//Flipping the sign, since these terms currently store d/dR values.
// add the minus one to be a force.
plist[myindex++] = -hf_force[iat][iondim];
plist[myindex++] = -pulay_force[iat][iondim];
plist[myindex++] = -Value * wf_grad[iat][iondim];
plist[myindex++] = -wf_grad[iat][iondim];
plist[myindex++] = -(pulay_force[iat][iondim]+sw_pulay[iat][iondim]);
plist[myindex++] = -Value * (wf_grad[iat][iondim]+sw_grad[iat][iondim]);
plist[myindex++] = -(wf_grad[iat][iondim]+sw_grad[iat][iondim]);

//Remove when ACForce is production ready
// if(useSpaceWarp)
// {
// plist[myindex++] = -sw_pulay[iat][iondim];
// plist[myindex++] = -Value*sw_grad[iat][iondim];
// plist[myindex++] = -sw_grad[iat][iondim];
// }
}
}
};
Expand All @@ -106,9 +165,15 @@ void ACForce::setParticlePropertyList(PropertySetType& plist, int offset)
for (int iondim = 0; iondim < OHMMS_DIM; iondim++)
{
plist[myindex++] = -hf_force[iat][iondim];
plist[myindex++] = -pulay_force[iat][iondim];
plist[myindex++] = -Value * wf_grad[iat][iondim];
plist[myindex++] = -wf_grad[iat][iondim];
plist[myindex++] = -(pulay_force[iat][iondim]+sw_pulay[iat][iondim]);
plist[myindex++] = -Value * (wf_grad[iat][iondim]+sw_grad[iat][iondim]);
plist[myindex++] = -(wf_grad[iat][iondim]+sw_grad[iat][iondim]);
// if(useSpaceWarp)
// {
// plist[myindex++] = -sw_pulay[iat][iondim];
// plist[myindex++] = -Value*sw_grad[iat][iondim];
// plist[myindex++] = -sw_grad[iat][iondim];
// }
}
}
};
Expand Down
9 changes: 8 additions & 1 deletion src/QMCHamiltonians/ACForce.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "QMCHamiltonians/OperatorBase.h"
#include "QMCWaveFunctions/TrialWaveFunction.h"
#include "QMCHamiltonians/QMCHamiltonian.h"
#include "QMCHamiltonians/SpaceWarpTransformation.h"

namespace qmcplusplus
{
Expand All @@ -33,7 +34,7 @@ struct ACForce : public OperatorBase
//ACForce(const ACForce& ac) {};

/** I/O Routines */
bool put(xmlNodePtr cur) { return true; };
bool put(xmlNodePtr cur);
bool get(std::ostream& os) const { return true; };

/** Cloning **/
Expand All @@ -54,6 +55,8 @@ struct ACForce : public OperatorBase
/** Evaluate **/
Return_t evaluate(ParticleSet& P);

RealType delta; //finite difference time step
rcclay marked this conversation as resolved.
Show resolved Hide resolved

//** Internal variables **/
// I'm assuming that psi, ions, elns, and the hamiltonian are bound to this
// instantiation. Making sure no crosstalk happens is the job of whatever clones this.
Expand All @@ -70,7 +73,11 @@ struct ACForce : public OperatorBase
Force_t hf_force;
Force_t pulay_force;
Force_t wf_grad;
Force_t sw_pulay;
Force_t sw_grad;

bool useSpaceWarp;
SpaceWarpTransformation swt;
//Class info.
std::string prefix;
//We also set the following from the OperatorBase class.
Expand Down
1 change: 1 addition & 0 deletions src/QMCHamiltonians/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SET(HAMSRCS
SpinDensity.cpp
SpeciesKineticEnergy.cpp
LatticeDeviationEstimator.cpp
SpaceWarpTransformation.cpp
)

IF(OHMMS_DIM MATCHES 3)
Expand Down
1 change: 1 addition & 0 deletions src/QMCHamiltonians/CoulombPotentialFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void HamiltonianFactory::addForceHam(xmlNodePtr cur)
}
TrialWaveFunction& psi = *psi_it->second->getTWF();
ACForce* acforce = new ACForce(*source, *target, psi, *targetH);
acforce->put(cur);
targetH->addOperator(acforce, title, false);
}
else
Expand Down
11 changes: 9 additions & 2 deletions src/QMCHamiltonians/NonLocalECPotential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ NonLocalECPotential::Return_t NonLocalECPotential::evaluate(ParticleSet& P)
return Value;
}

NonLocalECPotential::Return_t NonLocalECPotential::evaluateDeterministic(ParticleSet& P)
{
evaluateImpl(P, false, true);
return Value;
}

void NonLocalECPotential::mw_evaluate(const RefVector<OperatorBase>& O_list, const RefVector<ParticleSet>& P_list)
{
mw_evaluateImpl(O_list, P_list, false);
Expand All @@ -127,7 +133,7 @@ void NonLocalECPotential::mw_evaluateWithToperator(const RefVector<OperatorBase>
mw_evaluateImpl(O_list, P_list, false);
}

void NonLocalECPotential::evaluateImpl(ParticleSet& P, bool Tmove)
void NonLocalECPotential::evaluateImpl(ParticleSet& P, bool Tmove, bool keepGrid)
{
if (Tmove)
nonLocalOps.reset();
Expand All @@ -144,7 +150,8 @@ void NonLocalECPotential::evaluateImpl(ParticleSet& P, bool Tmove)
#endif
for (int ipp = 0; ipp < PPset.size(); ipp++)
if (PPset[ipp])
PPset[ipp]->randomize_grid(*myRNG);
if(!keepGrid)
PPset[ipp]->randomize_grid(*myRNG);
//loop over all the ions
const auto& myTable = P.getDistTable(myTableIndex);
// clear all the electron and ion neighbor lists
Expand Down
4 changes: 2 additions & 2 deletions src/QMCHamiltonians/NonLocalECPotential.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class NonLocalECPotential : public OperatorBase, public ForceBase
#endif

Return_t evaluate(ParticleSet& P) override;

Return_t evaluateDeterministic(ParticleSet& P) override;
void mw_evaluate(const RefVector<OperatorBase>& O_list, const RefVector<ParticleSet>& P_list) override;

Return_t evaluateWithToperator(ParticleSet& P) override;
Expand Down Expand Up @@ -154,7 +154,7 @@ class NonLocalECPotential : public OperatorBase, public ForceBase
* @param P particle set
* @param Tmove whether Txy for Tmove is updated
*/
void evaluateImpl(ParticleSet& P, bool Tmove);
void evaluateImpl(ParticleSet& P, bool Tmove, bool keepGrid=false);
rcclay marked this conversation as resolved.
Show resolved Hide resolved

/** the actual implementation for batched walkers, used by mw_evaluate and mw_evaluateWithToperator
* @param O_list the list of NonLocalECPotential in a walker batch
Expand Down
4 changes: 4 additions & 0 deletions src/QMCHamiltonians/OperatorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ OperatorBase::OperatorBase() : myIndex(-1), Dependants(0), Value(0.0), tWalker(0
UpdateMode.set(PRIMARY, 1);
}

OperatorBase::Return_t OperatorBase::evaluateDeterministic(ParticleSet& P)
{
return evaluate(P);
rcclay marked this conversation as resolved.
Show resolved Hide resolved
}
/** Take o_list and p_list update evaluation result variables in o_list?
*
* really should reduce vector of local_energies. matching the ordering and size of o list
Expand Down
1 change: 1 addition & 0 deletions src/QMCHamiltonians/OperatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ struct OperatorBase : public QMCTraits
*@return the value of the Hamiltonian component
*/
virtual Return_t evaluate(ParticleSet& P) = 0;
virtual Return_t evaluateDeterministic(ParticleSet& P);
rcclay marked this conversation as resolved.
Show resolved Hide resolved
/** Evaluate the contribution of this component of multiple walkers */
virtual void mw_evaluate(const RefVector<OperatorBase>& O_list, const RefVector<ParticleSet>& P_list);

Expand Down
57 changes: 56 additions & 1 deletion src/QMCHamiltonians/QMCHamiltonian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,29 @@ QMCHamiltonian::FullPrecRealType QMCHamiltonian::evaluate(ParticleSet& P)
return LocalEnergy;
}

QMCHamiltonian::FullPrecRealType QMCHamiltonian::evaluateDeterministic(ParticleSet& P)
{
ScopedTimer local_timer(ham_timer_);
LocalEnergy = 0.0;
for (int i = 0; i < H.size(); ++i)
{
ScopedTimer h_timer(my_timers_[i]);
const auto LocalEnergyComponent = H[i]->evaluateDeterministic(P);
if (std::isnan(LocalEnergyComponent))
APP_ABORT("QMCHamiltonian::evaluate component " + H[i]->myName + " returns NaN\n");
LocalEnergy += LocalEnergyComponent;
H[i]->setObservables(Observables);
#if !defined(REMOVE_TRACEMANAGER)
H[i]->collect_scalar_traces();
#endif
H[i]->setParticlePropertyList(P.PropertyList, myIndex);
}
KineticEnergy = H[0]->Value;
P.PropertyList[WP::LOCALENERGY] = LocalEnergy;
P.PropertyList[WP::LOCALPOTENTIAL] = LocalEnergy - KineticEnergy;
// auxHevaluate(P);
return LocalEnergy;
}
void QMCHamiltonian::updateNonKinetic(OperatorBase& op, QMCHamiltonian& ham, ParticleSet& pset)
{
if (std::isnan(op.Value))
Expand Down Expand Up @@ -794,7 +817,39 @@ std::vector<QMCHamiltonian::FullPrecRealType> QMCHamiltonian::flex_evaluateWithT
}
return local_energies;
}

void QMCHamiltonian::evaluateElecGrad(ParticleSet& P, TrialWaveFunction& psi, ParticleSet::ParticlePos_t& Egrad, RealType delta)
{
int nelec=P.getTotalNum();
RealType ep(0.0);
RealType em(0.0);
RealType e0(0.0);
for(int iel=0; iel<nelec; iel++)
{
for(int dim=0; dim<OHMMS_DIM; dim++)
{
RealType r0=P.R[iel][dim];
ep=0; em=0;
//Plus
RealType rp=r0+delta;
P.R[iel][dim]=rp;
P.update();
psi.evaluateLog(P);
ep=evaluateDeterministic(P);

//minus
RealType rm=r0-delta;
P.R[iel][dim]=rm;
P.update();
psi.evaluateLog(P);
em=evaluateDeterministic(P);

Egrad[iel][dim]=(ep-em)/(2.0*delta);
P.R[iel][dim]=r0;
P.update();
psi.evaluateLog(P);
}
}
}
QMCHamiltonian::FullPrecRealType QMCHamiltonian::evaluateIonDerivs(ParticleSet& P,
ParticleSet& ions,
TrialWaveFunction& psi,
Expand Down
15 changes: 15 additions & 0 deletions src/QMCHamiltonians/QMCHamiltonian.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ class QMCHamiltonian
*/
FullPrecRealType evaluate(ParticleSet& P);

/** evaluate Local Energy deterministically. Defaults to evaluate(P) for operators without a stochastic component. For the nonlocal PP, the quadrature grid is not rerandomized.
* @param P ParticleSet
* @return Local energy.
*
rcclay marked this conversation as resolved.
Show resolved Hide resolved
*/
FullPrecRealType evaluateDeterministic(ParticleSet& P);
/** batched version of evaluate for LocalEnergy
*
* Encapsulation is ignored for H_list hamiltonians method uses its status as QMCHamiltonian to break encapsulation.
Expand Down Expand Up @@ -282,6 +288,15 @@ class QMCHamiltonian
RecordArray<ValueType>& dhpsioverpsi);


/** Evaluate the electron gradient of the local energy.
* @param psi Trial Wave Function
* @param P electron particle set
* @param EGrad an Nelec x 3 real array which corresponds to d/d[r_i]_j E_L
* @param A finite difference step size if applicable. Default is to use finite diff with delta=1e-5.
* @return EGrad. Function itself returns nothing.
*/
void evaluateElecGrad(ParticleSet& P, TrialWaveFunction& psi, ParticleSet::ParticlePos_t& EGrad, RealType delta=1e-5);

/** evaluate local energy and derivatives w.r.t ionic coordinates.
* @param P target particle set (electrons)
* @param ions source particle set (ions)
Expand Down
Loading