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

[WIP] Next orbital rotation prototype #4351

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
8 changes: 7 additions & 1 deletion src/QMCDrivers/WFOpt/QMCCostFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ void QMCCostFunctionBase::reportParameters()
{
std::ostringstream vp_filename;
vp_filename << RootName << ".vp.h5";
OptVariables.saveAsHDF(vp_filename.str());
hdf_archive hout;
OptVariables.saveAsHDF(vp_filename.str(), hout);

UniqueOptObjRefs opt_obj_refs = Psi.extractOptimizableObjectRefs();
for (auto opt_obj : opt_obj_refs)
opt_obj.get().saveExtraParameters(hout);


char newxml[128];
sprintf(newxml, "%s.opt.xml", RootName.c_str());
Expand Down
3 changes: 3 additions & 0 deletions src/QMCHamiltonians/ECPotentialBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ void ECPotentialBuilder::useXmlFormat(xmlNodePtr cur)
app_warning() << "NLPP grid randomization is turned off. This setting should only be used for testing." << std::endl;
ecp.pp_nonloc->set_randomize_grid(!disable_randomize_grid);
hasNonLocalPot = true;
if (disable_randomize_grid)
app_warning() << "NLPP grid randomization is turned off. This setting should only be used for testing." << std::endl;
ecp.pp_nonloc->set_randomize_grid(!disable_randomize_grid);
nonLocalPot[speciesIndex] = std::move(ecp.pp_nonloc);
}
if (ecp.pp_so)
Expand Down
7 changes: 5 additions & 2 deletions src/QMCHamiltonians/NonLocalECPComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ NonLocalECPComponent::RealType NonLocalECPComponent::calculateProjector(RealType
psiratio[j] *= sgridweight_m[j];

// Compute radial potential, multiplied by (2l+1) factor.
for (int ip = 0; ip < nchannel; ip++)
for (int ip = 0; ip < nchannel; ip++) {
vrad[ip] = nlpp_m[ip]->splint(r) * wgt_angpp_m[ip];
}

constexpr RealType czero(0);
constexpr RealType cone(1);
Expand All @@ -191,9 +192,11 @@ NonLocalECPComponent::RealType NonLocalECPComponent::calculateProjector(RealType
lpolprev = lpol[l];
}


RealType lsum = 0.0;
for (int l = 0; l < nchannel; l++)
for (int l = 0; l < nchannel; l++) {
lsum += vrad[l] * lpol[angpp_m[l]];
}
knot_pots[j] = lsum * std::real(psiratio[j]);
pairpot += knot_pots[j];
}
Expand Down
2 changes: 1 addition & 1 deletion src/QMCHamiltonians/NonLocalECPComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class NonLocalECPComponent : public QMCTraits
sgridweight_m.push_back(weight);
}

void set_randomize_grid(bool do_randomize_grid_);
void set_randomize_grid(bool do_randomize_grid);

void resize_warrays(int n, int m, int l);

Expand Down
5 changes: 3 additions & 2 deletions src/QMCHamiltonians/NonLocalECPotential.deriv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateValueAndDerivatives
{
// Compute ratios with VP
VP->makeMoves(W, iel, deltaV, true, iat);
psi.evaluateDerivRatios(*VP, optvars, psiratio, dratio);
psi.evaluateDerivRatios(*VP, W, iel, optvars, psiratio, dratio);
}
else
{
Expand All @@ -97,8 +97,9 @@ NonLocalECPComponent::RealType NonLocalECPComponent::evaluateValueAndDerivatives
//use existing methods
std::fill(dlogpsi_vp.begin(), dlogpsi_vp.end(), 0.0);
psi.evaluateDerivativesWF(W, optvars, dlogpsi_vp);
for (int v = 0; v < dlogpsi_vp.size(); ++v)
for (int v = 0; v < dlogpsi_vp.size(); ++v) {
dratio(j, v) = dlogpsi_vp[v] - dlogpsi[v];
}

W.makeMove(iel, -deltaV[j]);
psi.calcRatio(W, iel);
Expand Down
3 changes: 3 additions & 0 deletions src/QMCHamiltonians/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ foreach(fname C.BFD.xml Na.BFD.xml so_ecp_test.xml C.ccECP.xml N.ccECP.xml Be.BF
maybe_symlink(${qmcpack_SOURCE_DIR}/tests/pseudopotentials_for_tests/${fname} ${UTEST_DIR}/${fname})
endforeach()

maybe_symlink(${qmcpack_SOURCE_DIR}/tests/solids/hcpBe_1x1x1_pp/Be.BFD.xml ${UTEST_DIR}/Be.BFD.xml)
maybe_symlink(${qmcpack_SOURCE_DIR}/tests/solids/hcpBe_1x1x1_pp/pwscf.pwscf.h5 ${UTEST_DIR}/hcpBe.pwscf.h5)

foreach(CATEGORY coulomb force ham ewald2d)
set(UTEST_EXE test_${SRC_DIR}_${CATEGORY})
set(UTEST_NAME deterministic-unit_${UTEST_EXE})
Expand Down
20 changes: 10 additions & 10 deletions src/QMCHamiltonians/tests/test_RotatedSPOs_NLPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ TEST_CASE("RotatedSPOs SplineR2R hcpBe values multi det", "[wavefunction]")

TrialWaveFunction::mw_evaluateParameterDerivatives(wf_list, p_list, opt_vars, dlogpsi_list, dhpsi_over_psi_list);

CHECK(dlogpsi_list[0][0] == Approx(dlogpsi[0]));
CHECK(dlogpsi_list[0][1] == Approx(dlogpsi[1]));
CHECK(dhpsi_over_psi_list[0][0] == Approx(dhpsioverpsi[0]));
CHECK(dhpsi_over_psi_list[0][1] == Approx(dhpsioverpsi[1]));
CHECK(dlogpsi_list[0][0] == ValueApprox(dlogpsi[0]));
CHECK(dlogpsi_list[0][1] == ValueApprox(dlogpsi[1]));
CHECK(dhpsi_over_psi_list[0][0] == ValueApprox(dhpsioverpsi[0]));
CHECK(dhpsi_over_psi_list[0][1] == ValueApprox(dhpsioverpsi[1]));


QMCHamiltonian* h = hf.getH();
Expand All @@ -215,8 +215,8 @@ TEST_CASE("RotatedSPOs SplineR2R hcpBe values multi det", "[wavefunction]")

h->evaluateValueAndDerivatives(elec, opt_vars, dlogpsi2, dhpsioverpsi2);
// Derivative the wavefunction is unchanged by NLPP
CHECK(dlogpsi2[0] == Approx(dlogpsi[0]));
CHECK(dlogpsi2[1] == Approx(dlogpsi[1]));
CHECK(dlogpsi2[0] == ValueApprox(dlogpsi[0]));
CHECK(dlogpsi2[1] == ValueApprox(dlogpsi[1]));

// Derivative of H is different with NLPP included
CHECK(dhpsioverpsi2[0] == ValueApprox(-5.45054261));
Expand All @@ -230,11 +230,11 @@ TEST_CASE("RotatedSPOs SplineR2R hcpBe values multi det", "[wavefunction]")

h->mw_evaluateValueAndDerivatives(h_list, wf_list, p_list, opt_vars, dlogpsi_list2, dhpsi_over_psi_list2);

CHECK(dlogpsi_list2[0][0] == Approx(dlogpsi2[0]));
CHECK(dlogpsi_list2[0][1] == Approx(dlogpsi2[1]));
CHECK(dlogpsi_list2[0][0] == ValueApprox(dlogpsi2[0]));
CHECK(dlogpsi_list2[0][1] == ValueApprox(dlogpsi2[1]));

CHECK(dhpsi_over_psi_list2[0][0] == Approx(dhpsioverpsi2[0]));
CHECK(dhpsi_over_psi_list2[0][1] == Approx(dhpsioverpsi2[1]));
CHECK(dhpsi_over_psi_list2[0][0] == ValueApprox(dhpsioverpsi2[0]));
CHECK(dhpsi_over_psi_list2[0][1] == ValueApprox(dhpsioverpsi2[1]));
}

} // namespace qmcplusplus
34 changes: 30 additions & 4 deletions src/QMCWaveFunctions/BsplineFactory/SplineR2R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ bool SplineR2R<ST>::write_splines(hdf_archive& h5f)
return h5f.writeEntry(bigtable, o.str().c_str()); //"spline_0");
}

template<typename ST>
void SplineR2R<ST>::storeParamsBeforeRotation()
{
const auto spline_ptr = SplineInst->getSplinePtr();
const auto spl_coefs = spline_ptr->coefs;
const auto coefs_tot_size = spline_ptr->coefs_size;
coef_copy = std::make_shared<std::vector<RealType>>(coefs_tot_size);
for (size_t i = 0; i < coefs_tot_size; i++)
{
(*coef_copy)[i] = spl_coefs[i];
}
}


/*
~~ Notes for rotation ~~
spl_coefs = Raw pointer of spline coefficients
Expand Down Expand Up @@ -87,6 +101,16 @@ void SplineR2R<ST>::applyRotation(const ValueMatrix& rot_mat, bool use_stored_co
const auto TrueNOrbs = rot_mat.size1(); // == Nsplines - padding
assert(OrbitalSetSize >= TrueNOrbs);

if (!use_stored_copy)
{
if (!coef_copy)
coef_copy = std::make_shared<std::vector<RealType>>(coefs_tot_size);
for (size_t i = 0; i < coefs_tot_size; i++)
{
(*coef_copy)[i] = spl_coefs[i];
}
}

// Fill top left corner of tmpU with rot_mat
ValueMatrix tmpU;
tmpU.resize(Nsplines, Nsplines);
Expand All @@ -100,7 +124,7 @@ void SplineR2R<ST>::applyRotation(const ValueMatrix& rot_mat, bool use_stored_co
}

// Apply rotation the dumb way b/c I can't get BLAS::gemm to work...
std::vector<RealType> new_coefs(coefs_tot_size, 0);
//std::vector<RealType> new_coefs(coefs_tot_size, 0);
for (auto i = 0; i < BasisSetSize; i++)
{
for (auto j = 0; j < Nsplines; j++)
Expand All @@ -110,14 +134,16 @@ void SplineR2R<ST>::applyRotation(const ValueMatrix& rot_mat, bool use_stored_co
for (auto k = 0; k < Nsplines; k++)
{
const auto index = i * Nsplines + k;
newval += *(spl_coefs + index) * tmpU[k][j];
//newval += *(spl_coefs + index) * tmpU[k][j];
newval += *(coef_copy->data() + index) * tmpU[k][j];
}
new_coefs[cur_elem] = newval;
//new_coefs[cur_elem] = newval;
spl_coefs[cur_elem] = newval;
}
}

// Update the coefs
std::copy(new_coefs.begin(), new_coefs.end(), spl_coefs);
//std::copy(new_coefs.begin(), new_coefs.end(), spl_coefs);

/*
// Here is my attempt to use gemm but it doesn't work...
Expand Down
4 changes: 4 additions & 0 deletions src/QMCWaveFunctions/BsplineFactory/SplineR2R.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class SplineR2R : public BsplineSet
///multi bspline set
std::shared_ptr<MultiBspline<ST>> SplineInst;

// Copy of original splines for orbital rotation
std::shared_ptr<std::vector<RealType>> coef_copy;
///thread private ratios for reduction when using nested threading, numVP x numThread
Matrix<TT> ratios_private;

Expand All @@ -83,6 +85,8 @@ class SplineR2R : public BsplineSet

std::unique_ptr<SPOSet> makeClone() const override { return std::make_unique<SplineR2R>(*this); }


void storeParamsBeforeRotation() override;
/*
Implements orbital rotations via [1,2].
Should be called by RotatedSPOs::apply_rotation()
Expand Down
23 changes: 23 additions & 0 deletions src/QMCWaveFunctions/Fermion/DiracDeterminant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ void DiracDeterminant<DU_TYPE>::mw_evaluateRatios(const RefVectorWithLeader<Wave
}
}

template<typename DU_TYPE>
void DiracDeterminant<DU_TYPE>::evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios)
{
const int WorkingIndex = VP.refPtcl - FirstIndex;
assert(WorkingIndex >= 0);
std::copy_n(psiM[WorkingIndex], invRow.size(), invRow.data());
Phi->evaluateDerivRatios(VP, P, iel, optvars, psiV, invRow, ratios, dratios, FirstIndex, LastIndex);
}


template<typename DU_TYPE>
void DiracDeterminant<DU_TYPE>::evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& ratios)
{
Expand Down Expand Up @@ -673,6 +688,14 @@ void DiracDeterminant<DU_TYPE>::evaluateDerivatives(ParticleSet& P,
Phi->evaluateDerivatives(P, active, dlogpsi, dhpsioverpsi, FirstIndex, LastIndex);
}

template<typename DU_TYPE>
void DiracDeterminant<DU_TYPE>::evaluateDerivativesWF(ParticleSet& P,
const opt_variables_type& active,
Vector<ValueType>& dlogpsi)
{
Phi->evaluateDerivativesWF(P, active, dlogpsi, FirstIndex, LastIndex);
}

template<typename DU_TYPE>
std::unique_ptr<DiracDeterminantBase> DiracDeterminant<DU_TYPE>::makeCopy(std::unique_ptr<SPOSet>&& spo) const
{
Expand Down
11 changes: 11 additions & 0 deletions src/QMCWaveFunctions/Fermion/DiracDeterminant.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class DiracDeterminant : public DiracDeterminantBase
Vector<ValueType>& dlogpsi,
Vector<ValueType>& dhpsioverpsi) override;

void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& optvars, Vector<ValueType>& dlogpsi) override;


void registerData(ParticleSet& P, WFBufferType& buf) override;

void updateAfterSweep(const ParticleSet& P, ParticleSet::ParticleGradient& G, ParticleSet::ParticleLaplacian& L);
Expand Down Expand Up @@ -109,6 +112,14 @@ class DiracDeterminant : public DiracDeterminantBase
const RefVectorWithLeader<const VirtualParticleSet>& vp_list,
std::vector<std::vector<ValueType>>& ratios) const override;

void evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios) override;


PsiValueType ratioGrad(ParticleSet& P, int iat, GradType& grad_iat) override;

PsiValueType ratioGradWithSpin(ParticleSet& P, int iat, GradType& grad_iat, ComplexType& spingrad) final;
Expand Down
14 changes: 13 additions & 1 deletion src/QMCWaveFunctions/Fermion/MultiDiracDeterminant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ void MultiDiracDeterminant::buildOptVariables(std::vector<size_t>& C2node)

// create active rotation parameter indices
std::vector<std::pair<int, int>> m_act_rot_inds;
std::vector<std::pair<int, int>> other_rot_inds;

for (int i = 0; i < nmo; i++)
for (int j = i + 1; j < nmo; j++)
Expand All @@ -802,10 +803,21 @@ void MultiDiracDeterminant::buildOptVariables(std::vector<size_t>& C2node)
int,
int>(i,
j)); // orbital rotation parameter accepted as long as rotation isn't core-core or virtual-virtual
} else {
other_rot_inds.push_back(std::pair<int,int>(i,j));
}
}

Phi->buildOptVariables(m_act_rot_inds);
std::vector<std::pair<int, int>> full_rot_inds;
// copy the adjustable rotations first
full_rot_inds = m_act_rot_inds;
// add the other rotations later
full_rot_inds = m_act_rot_inds;
for (int i = 0; i < other_rot_inds.size(); i++) {
full_rot_inds.push_back(other_rot_inds[i]);
}

Phi->buildOptVariables(m_act_rot_inds, full_rot_inds);
}

int MultiDiracDeterminant::build_occ_vec(const OffloadVector<int>& data,
Expand Down
2 changes: 2 additions & 0 deletions src/QMCWaveFunctions/Fermion/MultiSlaterDetTableMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,8 @@ void MultiSlaterDetTableMethod::evaluateDerivativesMSD(const PsiValueType& multi
}

void MultiSlaterDetTableMethod::evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios)
Expand Down
10 changes: 4 additions & 6 deletions src/QMCWaveFunctions/Fermion/MultiSlaterDetTableMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ class MultiSlaterDetTableMethod : public WaveFunctionComponent, public Optimizab
Vector<ValueType>& dlogpsi,
Vector<ValueType>& dhpsioverpsi) override;

void evaluateDerivativesWF(ParticleSet& P,
const opt_variables_type& optvars,
Vector<ValueType>& dlogpsi) override;
void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& optvars, Vector<ValueType>& dlogpsi) override;

void evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios) override;
Expand Down Expand Up @@ -265,9 +265,7 @@ class MultiSlaterDetTableMethod : public WaveFunctionComponent, public Optimizab
* @param dlogpsi saved derivatives
* @param det_id provide this argument to affect determinant group id for virtual moves
*/
void evaluateDerivativesMSD(const PsiValueType& multi_det_to_ref,
Vector<ValueType>& dlogpsi,
int det_id = -1) const;
void evaluateDerivativesMSD(const PsiValueType& multi_det_to_ref, Vector<ValueType>& dlogpsi, int det_id = -1) const;

/// determinant collection
std::vector<std::unique_ptr<MultiDiracDeterminant>> Dets;
Expand Down
11 changes: 11 additions & 0 deletions src/QMCWaveFunctions/Fermion/SlaterDet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ void SlaterDet::evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& r
Dets[i]->evaluateRatiosAlltoOne(P, ratios);
}

void SlaterDet::evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios)
{
return Dets[getDetID(VP.refPtcl)]->evaluateDerivRatios(VP, P, iel, optvars, ratios, dratios);
}


SlaterDet::LogValueType SlaterDet::evaluateLog(const ParticleSet& P,
ParticleSet::ParticleGradient& G,
ParticleSet::ParticleLaplacian& L)
Expand Down
8 changes: 8 additions & 0 deletions src/QMCWaveFunctions/Fermion/SlaterDet.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class SlaterDet : public WaveFunctionComponent
return Dets[getDetID(VP.refPtcl)]->evaluateRatios(VP, ratios);
}

void evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios) override;


inline void mw_evaluateRatios(const RefVectorWithLeader<WaveFunctionComponent>& wfc_list,
const RefVectorWithLeader<const VirtualParticleSet>& vp_list,
std::vector<std::vector<ValueType>>& ratios) const override
Expand Down
2 changes: 2 additions & 0 deletions src/QMCWaveFunctions/Jastrow/J1OrbitalSoA.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ class J1OrbitalSoA : public WaveFunctionComponent
/**@} */

void evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios) override
Expand Down
2 changes: 2 additions & 0 deletions src/QMCWaveFunctions/Jastrow/JeeIOrbitalSoA.h
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ class JeeIOrbitalSoA : public WaveFunctionComponent
}

void evaluateDerivRatios(const VirtualParticleSet& VP,
ParticleSet& P,
int iel,
const opt_variables_type& optvars,
std::vector<ValueType>& ratios,
Matrix<ValueType>& dratios) override
Expand Down
Loading