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

Read and write orbital rotation parameters #4580

Merged
merged 6 commits into from
May 1, 2023
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Add test for read/write of rotation parameters
  • Loading branch information
markdewing committed May 1, 2023
commit 0136a8ee5aaaa6e64e7c6557fedf8ddb3cc5589a
108 changes: 108 additions & 0 deletions src/QMCWaveFunctions/tests/test_RotatedSPOs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "QMCWaveFunctions/EinsplineSetBuilder.h"
#include "QMCWaveFunctions/RotatedSPOs.h"
#include "checkMatrix.hpp"
#include "FakeSPO.h"

#include <stdio.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdio.h->cstdio

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of change is starting to feel overly picky. Part of my motivation for #4559 was to make these kind of small but widespread updates to the code base separately. Then hopefully new code would pick up the changes by copying the updated code, and reviews would need to be less concerned about these kind of changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about. Fair point. It is a line not part of this PR.
I actually just wanted to take a note as I was reading through the code base and raise awareness of developers.

#include <string>
Expand Down Expand Up @@ -640,4 +641,111 @@ TEST_CASE("RotatedSPOs construct delta matrix", "[wavefunction]")
CHECK(new_params2[i] == Approx(old_params[i]));
}

namespace testing
{
opt_variables_type& getMyVars(SPOSet& rot) { return rot.myVars; }
opt_variables_type& getMyVarsFull(RotatedSPOs& rot) { return rot.myVarsFull; }
std::vector<std::vector<QMCTraits::RealType>>& getHistoryParams(RotatedSPOs& rot) { return rot.history_params_; }
} // namespace testing

// Test using global rotation
TEST_CASE("RotatedSPOs read and write parameters", "[wavefunction]")
{
auto fake_spo = std::make_unique<FakeSPO>();
fake_spo->setOrbitalSetSize(4);
RotatedSPOs rot("fake_rot", std::move(fake_spo));
int nel = 2;
rot.buildOptVariables(nel);

optimize::VariableSet vs;
rot.checkInVariablesExclusive(vs);
vs[0] = 0.1;
vs[1] = 0.15;
vs[2] = 0.2;
vs[4] = 0.25;
rot.resetParametersExclusive(vs);

hdf_archive hout;
vs.writeToHDF("rot_vp.h5", hout);

rot.writeVariationalParameters(hout);

hout.close();
markdewing marked this conversation as resolved.
Show resolved Hide resolved

auto fake_spo2 = std::make_unique<FakeSPO>();
fake_spo2->setOrbitalSetSize(4);

RotatedSPOs rot2("fake_rot", std::move(fake_spo2));
rot2.buildOptVariables(nel);

optimize::VariableSet vs2;
rot2.checkInVariablesExclusive(vs2);

hdf_archive hin;
ye-luo marked this conversation as resolved.
Show resolved Hide resolved
vs2.readFromHDF("rot_vp.h5", hin);
rot2.readVariationalParameters(hin);

opt_variables_type& var = testing::getMyVars(rot2);
CHECK(var[0] == Approx(vs[0]));
CHECK(var[1] == Approx(vs[1]));
CHECK(var[2] == Approx(vs[2]));
CHECK(var[3] == Approx(vs[3]));

opt_variables_type& full_var = testing::getMyVarsFull(rot2);
CHECK(full_var[0] == Approx(vs[0]));
CHECK(full_var[1] == Approx(vs[1]));
CHECK(full_var[2] == Approx(vs[2]));
CHECK(full_var[3] == Approx(vs[3]));
CHECK(full_var[4] == Approx(0.0));
CHECK(full_var[5] == Approx(0.0));
}

// Test using history list.
TEST_CASE("RotatedSPOs read and write parameters history", "[wavefunction]")
{
auto fake_spo = std::make_unique<FakeSPO>();
fake_spo->setOrbitalSetSize(4);
RotatedSPOs rot("fake_rot", std::move(fake_spo));
rot.set_use_global_rotation(false);
int nel = 2;
rot.buildOptVariables(nel);

optimize::VariableSet vs;
rot.checkInVariablesExclusive(vs);
vs[0] = 0.1;
vs[1] = 0.15;
vs[2] = 0.2;
vs[4] = 0.25;
rot.resetParametersExclusive(vs);

hdf_archive hout;
vs.writeToHDF("rot_vp_hist.h5", hout);

rot.writeVariationalParameters(hout);
hout.close();

auto fake_spo2 = std::make_unique<FakeSPO>();
fake_spo2->setOrbitalSetSize(4);

RotatedSPOs rot2("fake_rot", std::move(fake_spo2));
rot2.buildOptVariables(nel);

optimize::VariableSet vs2;
rot2.checkInVariablesExclusive(vs2);

hdf_archive hin;
vs2.readFromHDF("rot_vp_hist.h5", hin);
rot2.readVariationalParameters(hin);

opt_variables_type& var = testing::getMyVars(rot2);
CHECK(var[0] == Approx(vs[0]));
CHECK(var[1] == Approx(vs[1]));
CHECK(var[2] == Approx(vs[2]));
CHECK(var[3] == Approx(vs[3]));

auto hist = testing::getHistoryParams(rot2);
REQUIRE(hist.size() == 1);
REQUIRE(hist[0].size() == 4);
}

} // namespace qmcplusplus