From 5c1bc51d96702829e30090aa1f0f795f6c7aa416 Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 28 Sep 2023 15:47:43 -0500 Subject: [PATCH] Fix pw unit tests. --- src/QMCWaveFunctions/PlaneWave/PWBasis.cpp | 6 +- .../PlaneWave/PWOrbitalSetBuilder.cpp | 23 +++--- .../PlaneWave/PWOrbitalSetBuilder.h | 2 +- src/QMCWaveFunctions/tests/test_pw.cpp | 70 ++++++------------- 4 files changed, 34 insertions(+), 67 deletions(-) diff --git a/src/QMCWaveFunctions/PlaneWave/PWBasis.cpp b/src/QMCWaveFunctions/PlaneWave/PWBasis.cpp index fb613687f5..68ea84e3e9 100644 --- a/src/QMCWaveFunctions/PlaneWave/PWBasis.cpp +++ b/src/QMCWaveFunctions/PlaneWave/PWBasis.cpp @@ -34,10 +34,8 @@ int PWBasis::readbasis(hdf_archive& h5basisgroup, h5basisgroup.read(gvecs, "/electrons/kpoint_0/gvectors"); NumPlaneWaves = std::max(gvecs.size(), kplusgvecs_cart.size()); if (NumPlaneWaves == 0) - { - app_error() << " PWBasis::readbasis Basis is missing. Abort " << std::endl; - abort(); //FIX_ABORT - } + throw std::runtime_error(" PWBasis::readbasis Basis is missing."); + if (kplusgvecs_cart.empty()) { kplusgvecs_cart.resize(NumPlaneWaves); diff --git a/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.cpp b/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.cpp index e64f578950..3ea58fdb14 100644 --- a/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.cpp +++ b/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.cpp @@ -34,13 +34,10 @@ PWOrbitalSetBuilder::PWOrbitalSetBuilder(const ParticleSet& p, Communicate* comm //check the current href bool success = getH5(cur, "href"); //Move through the XML tree and read basis information - cur = cur->children; - while (cur != nullptr) - { - std::string cname((const char*)(cur->name)); + processChildren(cur, [&](const std::string& cname, const xmlNodePtr element) { if (cname == "basisset") { - const std::string a(getXMLAttributeValue(cur, "ecut")); + const std::string a(getXMLAttributeValue(element, "ecut")); if (!a.empty()) myParam->Ecut = std::stod(a); } @@ -49,13 +46,14 @@ PWOrbitalSetBuilder::PWOrbitalSetBuilder(const ParticleSet& p, Communicate* comm //close if (success) hfile.close(); - success = getH5(cur, "hdata"); + success = getH5(element, "hdata"); } - cur = cur->next; - } + }); + if (!success) + throw std::runtime_error("h5 cannot be open for creating PW basis!"); //create PW Basis - createPWBasis(cur); + createPWBasis(); } PWOrbitalSetBuilder::~PWOrbitalSetBuilder() = default; @@ -82,10 +80,10 @@ std::unique_ptr PWOrbitalSetBuilder::createSPOSetFromXML(xmlNodePtr cur) * -- maximum_ecut * - basis */ -bool PWOrbitalSetBuilder::createPWBasis(xmlNodePtr cur) +bool PWOrbitalSetBuilder::createPWBasis() { //recycle int and double reader - int idata; + int idata = 0; //start of parameters hfile.read(idata, "electrons/number_of_kpoints"); int nkpts = idata; @@ -110,9 +108,8 @@ bool PWOrbitalSetBuilder::createPWBasis(xmlNodePtr cur) hfile.read(TwistAngle_DP, "/electrons/kpoint_0/reduced_k"); TwistAngle = TwistAngle_DP; if (!myBasisSet) - { myBasisSet = std::make_unique(TwistAngle); - } + //Read the planewave basisset. //Note that the same data is opened here for each twist angle-avoids duplication in the //h5 file (which may become very large). diff --git a/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.h b/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.h index 6b093d0411..96081dcdaf 100644 --- a/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.h +++ b/src/QMCWaveFunctions/PlaneWave/PWOrbitalSetBuilder.h @@ -65,7 +65,7 @@ class PWOrbitalSetBuilder : public SPOSetBuilder private: bool getH5(xmlNodePtr cur, const char* aname); - bool createPWBasis(xmlNodePtr cur); + bool createPWBasis(); std::unique_ptr createPW(xmlNodePtr cur, const std::string& objname, int spinIndex); #if defined(QMC_COMPLEX) void transform2GridData(PWBasis::GIndex_t& nG, int spinIndex, PWOrbitalSet& pwFunc); diff --git a/src/QMCWaveFunctions/tests/test_pw.cpp b/src/QMCWaveFunctions/tests/test_pw.cpp index d68a5160cd..a44e8b599a 100644 --- a/src/QMCWaveFunctions/tests/test_pw.cpp +++ b/src/QMCWaveFunctions/tests/test_pw.cpp @@ -16,9 +16,7 @@ #include "OhmmsPETE/OhmmsMatrix.h" #include "Particle/ParticleSet.h" #include "Particle/ParticleSetPool.h" -#include "QMCWaveFunctions/WaveFunctionComponent.h" -#include "QMCWaveFunctions/PlaneWave/PWOrbitalBuilder.h" -#include "QMCWaveFunctions/Fermion/SlaterDet.h" +#include "PlaneWave/PWOrbitalSetBuilder.h" #include @@ -70,18 +68,12 @@ TEST_CASE("PlaneWave SPO from HDF for BCC H", "[wavefunction]") elec.update(); //BCC H - const char* particles = R"( - - - - - - - - - - - + const char* particles = R"( + + + + + )"; Libxml2Document doc; @@ -89,21 +81,14 @@ TEST_CASE("PlaneWave SPO from HDF for BCC H", "[wavefunction]") REQUIRE(okay); xmlNodePtr root = doc.getRoot(); - xmlNodePtr pw1 = xmlFirstElementChild(root); - PWOrbitalBuilder pw_builder(c, elec, ptcl.getPool()); - auto orb = pw_builder.buildComponent(pw1); - SlaterDet* sd = dynamic_cast(orb.get()); - REQUIRE(sd != nullptr); - REQUIRE(sd->Dets.size() == 2); - SPOSetPtr spo = sd->getPhi(0); - REQUIRE(spo != nullptr); - //SPOSet *spo = einSet.createSPOSetFromXML(ein1); - //REQUIRE(spo != nullptr); + PWOrbitalSetBuilder pw_builder(elec, c, root); + auto spo = pw_builder.createSPOSet(pw1); + REQUIRE(spo); - int orbSize = spo->getOrbitalSetSize(); + const int orbSize = spo->getOrbitalSetSize(); elec.update(); SPOSet::ValueVector orbs(orbSize); spo->evaluateValue(elec, 0, orbs); @@ -189,18 +174,12 @@ TEST_CASE("PlaneWave SPO from HDF for LiH arb", "[wavefunction]") elec.update(); //diamondC_1x1x1 - const char* particles = R"( - - - - - - - - - - - + const char* particles = R"( + + + + + )"; Libxml2Document doc; @@ -208,21 +187,14 @@ TEST_CASE("PlaneWave SPO from HDF for LiH arb", "[wavefunction]") REQUIRE(okay); xmlNodePtr root = doc.getRoot(); - xmlNodePtr pw1 = xmlFirstElementChild(root); - PWOrbitalBuilder pw_builder(c, elec, ptcl.getPool()); - auto orb = pw_builder.buildComponent(pw1); - SlaterDet* sd = dynamic_cast(orb.get()); - REQUIRE(sd != nullptr); - REQUIRE(sd->Dets.size() == 2); - SPOSetPtr spo = sd->getPhi(0); - REQUIRE(spo != nullptr); - //SPOSet *spo = einSet.createSPOSetFromXML(ein1); - //REQUIRE(spo != nullptr); + PWOrbitalSetBuilder pw_builder(elec, c, root); + auto spo = pw_builder.createSPOSet(pw1); + REQUIRE(spo); - int orbSize = spo->getOrbitalSetSize(); + const int orbSize = spo->getOrbitalSetSize(); elec.update(); SPOSet::ValueVector orbs(orbSize); spo->evaluateValue(elec, 0, orbs);